Serial Peripheral Interface (SPI) is an inexpensive chip interconnection bus, popular on circuit boards. Examples of SPI devices are SPI EEPROMs, SPI ADCs, MMC Cards, etc.
ZDev provides SPI master driver APIs to access SPI devices. The SPI
functions are declared in spi.h
. The following example writes a byte
to Microchip’s 25AA020A SPI EEPROM.
Listing 12.1. Accessing SPI EEPROM
#include <board.h>
#include <spi.h>
#define EEPROM_SS P1_4
#define EEPROM_WRITE 0x02
int main()
{
/*
* Intializes the SPI controller and sets the bus frequency to
* 1MHz, and SPI parameters as specified in the datasheet.
*/
spi_init(1000,
SPI_CPOL_IDLE_HIGH,
SPI_CPHASE_TRAIL_EDGE,
SPI_ENDIAN_MSB_FIRST);
EEPROM_SS = 0; /* Set chip select low */
spi_tx_rx(EEPROM_WRITE); /* Send write command. */
spi_tx_rx(0xBE); /* Send address of the memory location. */
spi_tx_rx(0xEF); /* Send data to be written. */
EEPROM_SS = 1; /* Set chip select high. */
}