2.56.11 I2Cx_CallbackRegister Function
C
/* x = I2C instance number */ /* I2C master mode */ void I2Cx_CallbackRegister(I2C_CALLBACK callback, uintptr_t context) /* I2C slave mode in non-blocking/interrupt mode */ void I2Cx_CallbackRegister(I2C_SLAVE_CALLBACK callback, uintptr_t context)
Summary
Sets the pointer to the function (and it's context) to be called when the given I2C's transfer events occur.
Description
This function sets the pointer to a client/application function to be called "back" when the given I2C's transfer events occur. It also passes a context value (usually a pointer to a context structure) that is passed into the function when it is called. The specified callback function will be called from the peripheral interrupt context.
Precondition
I2Cx_Initialize must have been called for the associated I2C instance.
Parameters
| Param | Description |
|---|---|
| callback | A pointer to a function with a calling signature defined by the I2C_CALLBACK (in I2C master mode) or I2C_SLAVE_CALLBACK (in I2C slave mode) data type. Setting this to NULL disables the callback feature. |
| context | A value (usually a pointer) which is passed (unused) into the function identified by the callback parameter |
Returns
None.
Example
I2C master mode
void I2C1_Callback(uintptr_t context) { if(I2C1_ErrorGet() == I2C_ERROR_NONE) { //Transfer is completed successfully } else { //Error occurred during transfer. } } // Register Callback function which is defined above I2C1_CallbackRegister(I2C1_Callback, (uintptr_t)NULL);
I2C slave in non-blocking/interrupt mode
bool I2C1_Callback ( I2C_SLAVE_TRANSFER_EVENT event, uintptr_t context )
{
uint8_t rxData;
uint8_t txData;
switch(event)
{
case I2C_SLAVE_TRANSFER_EVENT_ADDR_MATCH:
// Handle address match event
break;
case I2C_SLAVE_TRANSFER_EVENT_RX_READY:
// I2C master is writing data to I2C slave.
// Read the received data byte.
rxData = I2C1_ReadByte();
break;
case I2C_SLAVE_TRANSFER_EVENT_TX_READY:
// I2C master is reading data from I2C slave.
// Provide data to I2C master.
I2C1_WriteByte(txData);
break;
}
}
// Register Callback function which is defined above
I2C1_CallbackRegister(I2C1_Callback, 0);
Remarks
When I2C is configured in master mode, the callback function prototype is I2C_CALLBACK.
When I2C is configured in slave mode, the callback function prototype is I2C_SLAVE_CALLBACK.
