In Inter-Integrated Circuit (I2C) slave asynchronous driver, a callback function can be registered in the driver by the application and triggered when the transfer is done. The driver io_read/io_write() function will attempt to read/write the data from/to the master device.
I2C Modes (standard mode/fastmode+/highspeed mode) can only be selected in START. Make sure that the selected speed mode is within the expected SCL clock frequency range of the i2c bus.
The error callback is executed as soon as an error is detected by the hardware.
The RX callback is invoked each time a byte is received by an I2C slave device, the byte is put into the ring buffer prior to the callback calling. Received data can be read out in the callback via the I/O read function.
The TX pending callback is invoked when a master device requests data from a slave device via sending slave device address with R/W bit set to one. A slave device can send data to a master device via the I/O write function.
The TX callback is invoked at the end of buffer transfer caused by a call to the I/O write function.
Initialize and deinitialize the driver and associated hardware
Register the I/O descriptor
Enable or disable the I2C slave
Hookup callback handlers on TX complete, RX complete, or error events
Set the address of the slave device
Read/Write message to/from the master
Set I2C slave device address
Which clock source is used
After the I2C hardware initialization, the i2c_s_async_get_io_descriptor function is needed to register an I/O descriptor. Then use i2c_s_async_register_callback to register the callback function for RX/TX complete, and enable the I2C hardware. At the end, start the read/write operation.
System Management Bus (SMBus) is not supported
Power Management Bus (PMBus) is not supported
During the write operation the buffer content should not be changed before the transfer is complete
The following shows a simple example of using the I2C slave. The I2C slave must have been initialized by i2c_s_async_init. This initialization will configure the operation of the I2C slave.
The example registers an I/O descriptor and enables the hardware. Then it registers a callback for RX complete, and finally starts a reading operation.
static struct io_descriptor *io;
static void I2C_0_rx_complete(const struct i2c_s_async_descriptor *const descr)
{
uint8_t c;
io_read(io, &c, 1);
}
void I2C_0_example(void)
{
i2c_s_async_get_io_descriptor(&I2C_0, &io);
i2c_s_async_register_callback(&I2C_0, I2C_S_RX_COMPLETE, I2C_0_rx_complete);
i2c_s_async_enable(&I2C_0);
}
The I2C slave peripheral and its related I/O lines and clocks
The NVIC must be configured so that I2C interrupt requests are periodically serviced