nRF51 SDK v10.0.0
SPI master

The SPI master driver includes two layers: the hardware access layer (HAL) and the driver layer (DRV).

The hardware access layer provides basic APIs for accessing the registers of the SPI and SPIM peripherals. See the API documentation for the SPI HAL and SPIM HAL for details.

The driver layer provides APIs on a higher level than the HAL. See the API documentation for the SPI master driver for details.

Key features include:

Note that peripherals using EasyDMA can work only with buffers that are placed in the data RAM region. Under certain circumstances, compilers might choose to use a different region for data placement and, for example, place a constant buffer in the code FLASH. In such a case, the SPIM peripheral cannot be used to transfer data from the buffer.

To quickly get started, use the sample code from the SPI Master Example to be used with SPI Slave Example.

Driver configuration

The SPI master driver can use multiple instances of the SPI/SPIM peripherals, and it provides a common API for both peripheral types. The instances of the peripherals that are to be assigned to the driver must be selected statically in nrf_drv_config.h. In the same way, you configure whether a given instance uses EasyDMA (thus whether SPIM or SPI is used).

For example:

#define SPI0_ENABLED 1
#define SPI0_USE_EASY_DMA 0

For enabled instances, you must provide the default configuration settings that are used by the NRF_DRV_SPI_DEFAULT_CONFIG macro. These settings are also defined in nrf_drv_config.h, for example:

#define SPI0_CONFIG_SCK_PIN 2
#define SPI0_CONFIG_MOSI_PIN 3
#define SPI0_CONFIG_MISO_PIN 4
#define SPI0_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW

Using the SPI master driver

Call the NRF_DRV_SPI_INSTANCE macro with the ID of the peripheral instance that you want to use to create a driver instance. For example, this code will create a driver instance that uses the SPI0 or SPIM0 peripheral (depending on the value assigned to SPI0_CONFIG_USE_EASY_DMA in nrf_drv_config.h):

static const nrf_drv_spi_t m_spi_master_0 = NRF_DRV_SPI_INSTANCE(0);

Next, call nrf_drv_spi_init to initialize and configure the instance. See the nrf_drv_spi_config_t structure documentation for possible configuration options.

For example:

uint32_t err_code;
err_code = nrf_drv_spi_init(&m_spi_master_0, &config, NULL);
if (err_code != NRF_SUCCESS)
{
// Initialization failed. Take recovery action.
}

After successful initialization, you can call nrf_drv_spi_transfer to request SPI transfers.

The SPI master driver can automatically control the Slave Select signal (see nrf_drv_spi_config_t::ss_pin). This feature can only be used if there is exactly one slave. If there are multiple slaves, the Slave Select signal must be controlled externally.

When the SPI master driver instance is no longer needed or its configuration must be changed, call nrf_drv_spi_uninit.


This document was last updated on Mon Nov 9 2015.
Please send us your feedback about the documentation! For technical questions, visit the Nordic Developer Zone.