Controller transactions

Informative

How a controller-mode channel reads and writes on the bus. Binding rules: CHI-I2C-MUST-01, CHI-I2C-MUST-11CHI-I2C-MUST-14 in I2c — Requirements.

Write and read

/* write `len` bytes to the device at address `addr` */
I2c_StartTxTransaction(0u, addr, txData, len);

/* read `len` bytes from the device at address `addr` */
I2c_StartRxTransaction(0u, addr, rxBuffer, len);

Both start a transaction on an IDLE channel (moving it to BUSY) and return immediately; the transfer completes asynchronously. Keep txData / rxBuffer valid until the completion callout fires.

Completion and errors

Fill these in in I2c_Callout_Stubs.c (see The Callout Stub File):

/* in I2c_Callout_Stubs.c */
void I2c_Callout_OnTransmission(uint8 channelId, uint16 length) { /* write done */ }
void I2c_Callout_OnReception(uint8 channelId, uint16 length)    { /* read done  */ }

void I2c_Callout_OnError(uint8 channelId, I2c_ErrorType error)
{
    /* I2C_ERR_NACK_RECEIVED: the addressed device did not acknowledge */
}

Repeated START

To keep the bus without releasing it (a repeated START), start the next transaction from inside the completion callout — before the driver issues STOP:

void I2c_Callout_OnTransmission(uint8 channelId, uint16 length)
{
    /* register index written; now read the register value with a repeated START */
    I2c_StartRxTransaction(channelId, deviceAddr, rxBuffer, 1u);
}

If you do not start a new transaction in the callout, the driver issues STOP and the channel returns to IDLE.