Chapter 6

Basic SPI

This module provides a basic interface to the microcontroller SPI in master mode. It utilizes an interrupt service routine to clock out the SPI data asynchronously.

6.1  Usage

To use the SPI module, place the following in your source files:

  #include <spi2.h>

6.2  Functions

void spi2_init(void)   function

This function places the SPI into “master” mode, enables the SPI, and enables the SPI interrupt. The library provides the required interrupt service routine.

bool spi2_busy(void)   function

This function returns true if the SPI module is busy clocking SPI data. While the SPI module is busy, spi2_send_byte() and spi2_send_data() will do nothing and will return false.

bool spi2_send_byte(uint8_t* b, volatile bool* done_flag)   function

This function begins an asynchronous SPI exchange of one byte. The data to transmit is the byte pointed to by b. If done_flag is not NULL, when the exchange is complete, the interrupt service routine sets this flag to true. After the exchange, the byte pointed to by b will contain the received byte.

While the module is busy exchanging the byte, it is important that the location pointed to by b remain valid. For example, it would be an error if b points to a location on the stack that goes out of scope while the exchange is taking place.

bool spi2_send_data(uint8_t* d, uint8_t s, volatile bool* done_flag)   function

This function begins an asynchronous SPI exchange of s bytes of the data pointed to by d. If done_flag is not NULL, when the exchange is complete, the interrupt service routine sets this flag to true. After the exchange, the data pointed to by d will contain the received data.

While the module is busy exchanging the byte, it is important that the location pointed to by d remain valid. For example, it would be an error if d points location on the stack that goes out of scope while the exchange is taking place.