nRF51 SDK - S210 SoftDevice
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
SPI Master

Before starting to use SPI master, the appropriate module or both modules need to be enabled.

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. All configuration options need to be configure in structure spi_master_config_t.

Before the configure SPI master, appropriate module (or both modules) need to be enabled by defined:

- #define SPI_MASTER_0_ENABLE //Enables module SPI_MASTER_0
- #define SPI_MASTER_1_ENABLE //Enables module SPI_MASTER_1
PINs configuration (PIN numbers from 0 to 31):
- SCK //Serial clock
- MISO //Master IN Slave OUT
- MOSI //Master OUT Slave IN
- SS //Slave Select
Serial clock polarity configuration

See spi_master_config_t::SPI_CONFIG_CPOL.

Supported serial clock polarities:

- Active high //SPI_CONFIG_CPOL_ActiveHigh
- Active low //SPI_CONFIG_CPOL_ActiveLow
Serial clock phase configuration

See spi_master_config_t::SPI_CONFIG_CPHA.

Supported serial clock phases:

- Leading //SPI_CONFIG_CPHA_Leading
- Trailing //SPI_CONFIG_CPHA_Trailing
Data endian-ness configuration.

See spi_master_config_t::SPI_CONFIG_ORDER.

Supported data endian-ness:

- Least significant bit shifted out first. //SPI_CONFIG_ORDER_LsbFirst
- Most significant bit shifted out first. //SPI_CONFIG_ORDER_MsbFirst
SPI master frequency configuration.

See spi_master_config_t::SPI_Freq.

Supported SPI master frequencies:

- 125 kbps //SPI_FREQUENCY_FREQUENCY_K125
- 250 kbps //SPI_FREQUENCY_FREQUENCY_K250
- 500 kbps //SPI_FREQUENCY_FREQUENCY_K500
- 1 Mbps //SPI_FREQUENCY_FREQUENCY_M1
- 2 Mbps //SPI_FREQUENCY_FREQUENCY_M2
- 4 Mbps //SPI_FREQUENCY_FREQUENCY_M4
- 8 Mbps //SPI_FREQUENCY_FREQUENCY_M8
Interrupt priority configuration.

See spi_master_config_t::SPI_PriorityIRQ.

If SoftDevice is enabled available interrupt priorities (values 3 and 1):

- Low //3 - NRF_APP_PRIORITY_LOW
- High //1 - NRF_APP_PRIORITY_HIGH

If SoftDevice isn't enabled available interrupt priorities (values from 0 to 3):

- Lowest //3 - NRF_APP_PRIORITY_LOW
- Low //2
- High //1 - NRF_APP_PRIORITY_HIGH
- Highest //0
Disable all IRQs during realization critical region inside the driver.

See spi_master_config_t::SPI_DisableAllIRQ.

- Disable all interrupts. //true
- Disable only SPI interrupt. //false
Quick start
  • Configure SPI master PINs.
Following sample snippet initializes SPI_MASTER_0 with default values.


#define SPI_MASTER_0_ENABLE /**< Enable SPI_MASTER_0 */
void spi_master_event_handler(spi_master_evt_t spi_master_evt)
{
switch (spi_master_evt.evt_type)
{
//Data transmission is ended successful. 'rx_buffer' has data received from SPI slave.
transmission_completed = true;
break;
default:
//No implementation needed.
break;
}
}
void spi_master_init(void)
{
//Structure for SPI master configuration, initialized by default values.
//Configure SPI master.
spi_config.SPI_Pin_SCK = SPIM0_SCK_PIN;
spi_config.SPI_Pin_MISO = SPIM0_MISO_PIN;
spi_config.SPI_Pin_MOSI = SPIM0_MOSI_PIN;
spi_config.SPI_Pin_SS = SPIM0_SS_PIN;
//Initialize SPI master.
uint32_t err_code = spi_master_open(SPI_MASTER_0, &spi_config);
if (err_code != NRF_SUCCESS)
{
//Module initialization failed. Take recovery action.
}
//Register SPI master event handler.
spi_master_evt_handler_reg(SPI_MASTER_0, spi_master_event_handler);
}

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.

uint16_t buf_len = TX_RX_MSG_LENGTH;
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.
volatile bool transmission_completed = false;
//Fill transmit buffer with sample data, numbers from (0 - (buf_len - 1)).
int index;
for (index = 0; index < TX_RX_MSG_LENGTH; index++)
{
tx_buffer[index] = index;
}
uint32_t err_code = spi_master_send_recv(SPI_MASTER_0, tx_buffer, buf_len, rx_buffer, buf_len);
if (err_code != NRF_SUCCESS)
{
//Data transmission failed.
}
//Data transmission is ended successful when the SPI_MASTER_EVT_TRANSFER_COMPLETED is received by the spi_master_event_handler(spi_master_evt_t spi_master_evt).