nRF5 SDK  v12.1.0
Choose documentation:
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
TWI transaction manager

The TWI transaction manager library provides functions to create and execute transactions consisting of one or more data transfers via the two-wire interface (TWI).

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

  • Background: When the application schedules a transaction in the background (app_twi_schedule), it receives a callback from the library when the transaction has completed or when the transaction is interrupted due to an error.
  • Blocking mode: When an application schedules a transaction in blocking mode (app_twi_perform), the function does not return until the transaction has finished (successfully or with an error).

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

You can suppress the generation of a stop condition for selected transfers. In this case, the following transfer will start with a repeated start condition. The following example code shows how to first write one byte (for example, an register address) and then read 4 bytes, without a stop condition in between:

static app_twi_transfer_t const transfers[] =
{
APP_TWI_WRITE(device_address, &register_address, 1, APP_TWI_NO_STOP),
APP_TWI_READ (device_address, p_buffer, 4, 0)
};
static app_twi_transaction_t const transaction =
{
.callback = transaction_callback,
.p_user_data = NULL,
.p_transfers = transfers,
.number_of_transfers = sizeof(transfers)/sizeof(transfers[0])
};
APP_ERROR_CHECK(app_twi_schedule(&m_app_twi, &transaction));

The API for this module is available here: TWI transaction manager. See the TWI Transaction Manager Example for a usage example.