I2C Integration Guide¶
Overview¶
The CFBD I2C abstraction provides a platform-independent interface for I2C communication. Backend implementations (e.g., STM32) provide concrete device drivers, while the abstraction layer ensures consistent behavior across different hardware platforms.
Architecture¶
The I2C system consists of:
- Operations Table (
[CFBD_I2COperations](Classes/structCFBD__I2COperations.md)): Backend implementation hooks - I2C Handle (
[CFBD_I2CHandle](Classes/structCFBD__I2CHandle.md)): Public interface + private backend state - Messages (
[CFBD_I2C_Message](Classes/structCFBD__I2C__Message.md)): Protocol-level read/write operations - High-Level Helpers (
CFBD_I2CRead,CFBD_I2CWrite): Convenience functions
Typical Usage Workflow¶
Initialization¶
// 1. Create I2C handle (typically done by backend platform code)
CFBD_I2CHandle i2c_bus = {
.ops = &i2c_stm_operations, // Backend operations table
.private_handle = &i2c_hardware // Platform-specific state
};
// 2. Initialize the I2C bus
int status = CFBD_I2CInit(&i2c_bus);
if (status != I2C_OK) {
// Handle initialization error
}
Message Transfer¶
// Example: Read from OLED at address 0x3C
CFBD_I2C_Message msgs[2];
uint8_t reg_addr = 0x00; // Register to read
uint8_t data_buffer[32]; // Read buffer
// Message 1: Write register address
msgs[0].addr = 0x3C;
msgs[0].flags = 0;
msgs[0].len = 1;
msgs[0].buf = ®_addr;
// Message 2: Read data (uses I2C_M_RD flag)
msgs[1].addr = 0x3C;
msgs[1].flags = I2C_M_RD;
msgs[1].len = sizeof(data_buffer);
msgs[1].buf = data_buffer;
// Execute combined write-read transaction
status = CFBD_I2CTransfer(&i2c_bus, msgs, 2, 1000);
if (status == I2C_OK) {
// data_buffer now contains read data
}
Using Convenience Helpers¶
CFBD_I2C_IORequestParams req = {
.addr7 = 0x3C,
.mem_addr = 0x10, // Device internal address
.mem_addr_size = 1, // 1 byte address
.data = buffer,
.len = sizeof(buffer),
.timeout_ms = 1000
};
// Simple high-level read
status = CFBD_I2CRead(&i2c_bus, &req);
Updated on 2026-02-03 at 13:21:55 +0000