3. Analyzing The Application Example

This section analyzes an example application demonstrating the use of the I2C adapter, based on the freertos_retarget sample code found in the SDK. It implements a simple request/response communication scenario over the I2C interface using the I2C adapter layer.

Interface I2C1 is configured as I2C master and I2C2 as slave, so there is the option to connect the two interfaces in loopback, or connect two SDKs together.

3.1. Application Structure

  1. I2C master

The I2C master initiates a write-then-read transaction sending a request string to the slave.

Depending on the value of the I2C_ASYNC_EN macro, a synchronous or asynchronous I2C read operation is attempted.

'I2C Write & Read - Main Execution Path'

Figure 2 I2C Write & Read - Main Execution Path

It is essential that developers must not call asynchronous related APIs without guaranteeing that the previous asynchronous transaction is finished. To ensure this, after triggering an asynchronous I2C read transaction, the calling task blocks its execution until the arrival of a freeRTOS signal, indicating the end of the current I2C operation.

'Async I2C write & read - Callback Function Execution Path'

Figure 3 Async I2C write & read - Callback function Execution Path

  1. I2C slave

The I2C slave handles the response with callback functions in ISR context. The response message consists of statistics about how many times each callback has been called. Both request and response are printed on the UART terminal by the I2C master.

'I2C write and read message flows'

Figure 4 I2C write and read message flows