nRF51 SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
SPI Master
Features supported:
  • Two SPI modules, SPI0 and SP01 are currently supported.
  • The driver can used to use only one or both of the SPI modules.
  • Configuration of each SPI module.
  • Interface to transmit and receive data on each of the modules.

SPI Module Initialization and Configuration

The SPI modules need to be initialized and configured before they can be used for data exchange. An SPI module can be configured to exchange data in four configurations of clock.

  • Mode 0: Data sampled on rising edge of clock, shift data on falling edge, at clock polarity 0.
  • Mode 1: Data sampled on falling edge of clock, shift data on rising edge, at clock polarity 0.
  • Mode 2: Data sampled on rising edge of clock, shift data on falling edge, at clock polarity 1.
  • Mode 3: Data sampled on falling edge of clock, shift data on rising edge, at clock polarity 1.

See enumeration SPIMode that defines these modes.

Also, the application can indicate the endian-ness of data to be transmitted serially, either LSB first or MSB first.

SPI Module frequency is configured at compile time using the define SPI_OPERATING_FREQUENCY spi_master_config.h. Supported frequencies are defined in the enumeration SPIFrequency_t. Supported frequencies include:

  • 125 kbps
  • 250 kbps
  • 500 kbps
  • 1 Mbps
  • 2 Mbps
  • 4 Mbps
  • 8 Mbps

SDK Sample examples operate the SPI module at 250 kbps.

On successful initialization of the requested configuration for the SPI module, the physical address of the requested SPI module is provided as reference for all future data exchanges.

Following sample snippet initializes SPI0 in mode 3 with data transmission in little endian order.

uint32 * example_spi_base_addr; //SPI module address.
//SPI Master module 0 initialization in Mode0, LSB of data to be shifted first.
example_spi_base_addr = spi_master_init (SPI0, SPI_MODE0, true);
if (example_spi_base_addr == NULL)
{
//Module initialization failed. Take recovery action.
}
else
{
//Module initialization succeeded. Data can be exchanged on the module using example_spi_base_addr.
.
.
.
}

SPI Master Data Exchange API

Once successfully initialized, data can be exchanged serially on the module. Data transmission is synchronous and any data that the slave wants to transmit is fetched during the master transmission. The application is expected to provide a receive buffer when calling the Data Exchange API of size TX_RX_MSG_LENGTH. TX_RX_MSG_LENGTH is defined in spi_master_config.h.

bool rx_tx_result; //Result of data exchange on SPI.
uint8_t rx_buffer[TX_RX_MSG_LENGTH]; //Receive buffer to get data from SPI slave.
uint8_t tx_buffer[TX_RX_MSG_LENGTH]; //Transmit buffer to send data from SPI master with sample data.
//Fill transmit buffer with sample data, numbers from (0 - (TX_RX_MSG_LENGTH-1)).
int index;
for (index = 0; index < TX_RX_MSG_LENGTH; index++)
{
tx_buffer[index] = index;
}
rx_tx_result = spi_master_tx_rx (example_spi_base_addr, TX_RX_MSG_LENGTH, tx_buffer, rx_buffer);
if (rx_tx_result == false)
{
//Data transmission failed.
}
else
{
//Data transmission successful. 'rx_data' buffer has data received from SPI slave.
}