nRF5 SDK v13.0.0
SPI transaction manager

The SPI transaction manager library provides functions to create and execute transactions consisting of one or more data transfers via the serial peripheral interface (SPI).

Transactions can be run in the background or in blocking mode:

Transactions can be requested from different contexts, including interrupt handlers. The library takes care of proper synchronization.

The SPI transaction manager library supports setting transactions with different hardware configurations. For example, if you want to connect slave devices to different pins or with different frequency, you can set up a transaction with parameter p_required_spi_cfg that points to the required hardware configuration. The SPI driver will be reinitialized every time the required configuration will differ from the one from the previous transaction.

Code example:

static nrf_drv_spi_config_t const new_configuration =
{
.sck_pin = ARDUINO_A0_PIN,
.mosi_pin = ARDUINO_A1_PIN,
.miso_pin = ARDUINO_A2_PIN,
.ss_pin = ARDUINO_SDA_PIN,
.irq_priority = APP_IRQ_PRIORITY_LOWEST,
.orc = 0xFF,
.frequency = NRF_DRV_SPI_FREQ_1M,
};
static nrf_spi_mngr_transfer_t const transfers[] =
{
NRF_SPI_MNGR_TRANSFER(m_master_data_0, sizeof(m_master_data_0), m_master_buffer_rx, sizeof(m_master_buffer_rx))
NRF_SPI_MNGR_TRANSFER(m_master_data_1, sizeof(m_master_data_1), m_master_buffer_rx, sizeof(m_master_buffer_rx))
};
static nrf_spi_mngr_transaction_t transaction_1 =
{
.begin_callback = NULL,
.end_callback = cb_transaction_done,
.p_user_data = (void *)0,
.p_transfers = transfers,
.number_of_transfers = sizeof(transfers) / sizeof(transfers[0]),
.p_required_spi_cfg = NULL
};
static nrf_spi_mngr_transaction_t transaction_2 =
{
.begin_callback = NULL,
.end_callback = cb_transaction_done,
.p_user_data = (void *)0,
.p_transfers = transfers,
.number_of_transfers = sizeof(transfers) / sizeof(transfers[0]),
.p_required_spi_cfg = &new_configuration
};
static nrf_spi_mngr_transaction_t transaction_3 =
{
.begin_callback = NULL,
.end_callback = cb_transaction_done,
.p_user_data = (void *)0,
.p_transfers = transfers,
.number_of_transfers = sizeof(transfers) / sizeof(transfers[0]),
.p_required_spi_cfg = NULL
};
APP_ERROR_CHECK(nrf_spi_mngr_schedule(&m_nrf_spi_mngr, &transaction_1));
APP_ERROR_CHECK(nrf_spi_mngr_schedule(&m_nrf_spi_mngr, &transaction_2));
APP_ERROR_CHECK(nrf_spi_mngr_schedule(&m_nrf_spi_mngr, &transaction_3));

In the code example above, the new_configuration is different from the default configuration. In this case, SPI transaction manager will schedule and execute transaction_1 and then schedule the other two transactions. However, before executing transaction_2, it will reinitialize SPI hardware with the given configuration. Then, again, before executing transaction_3, SPI hardware will be reinitialized to the default configuration.

SPI transaction manager comes with two callbacks: begin_callback and end_callback. This approach allows you to execute some code before the SPI transaction is started. It might be useful when handling LCD displays (e.g. based on the ST7565 driver), which require changing the GPIO line to switch between frame types: command / data.

For API documentation of this library, refer to: SPI transaction manager.

See the SPI Transaction Manager Example for a usage example.


Documentation feedback | Developer Zone | Subscribe | Updated