I2C Error Handling¶
Introduction¶
The I2C/IIC error subsystem provides a standardized set of error codes compatible with POSIX conventions. This enables consistent error handling across different platforms and I2C implementations within the CFBD project.
Error Code Reference¶
All I2C operations return an integer status code:
- I2C_OK (0): Operation completed successfully
- I2C_ERR_TIMEOUT (-110): Operation exceeded time limit
- I2C_ERR_NACK (-121): Device did not acknowledge (protocol error)
- I2C_ERR_BUSY (-16): Bus or device is currently busy
- I2C_ERR_IO (-5): Generic I/O error occurred
- I2C_ERR_INVAL (-22): Invalid arguments provided
Error Handling Patterns¶
Basic Error Checking¶
int status = CFBD_I2CTransfer(bus, messages, num_messages, 1000);
if (status != I2C_OK) {
// Handle error based on type
if (status == I2C_ERR_TIMEOUT) {
// Timeout occurred - retry or increase timeout
} else if (status == I2C_ERR_NACK) {
// Device not responding - check address and connections
}
}
Recovery Strategies¶
int attempt = 0;
const int MAX_RETRIES = 3;
int status;
do {
status = CFBD_I2CTransfer(bus, messages, num_messages, 1000);
if (status == I2C_OK) {
break; // Success
} else if (status == I2C_ERR_BUSY) {
// Wait before retry
delay_ms(10);
} else if (status == I2C_ERR_NACK || status == I2C_ERR_IO) {
// Attempt bus recovery
CFBD_I2CRecoverBus(bus);
delay_ms(50);
}
} while (++attempt < MAX_RETRIES && status != I2C_OK);
Updated on 2026-02-03 at 13:21:55 +0000