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:
nrf_drv_config.h
.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.
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:
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:
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
):
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:
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.