跳转至

I2C Inline Wrappers

Convenience inline functions for I2C operations. More...

Functions

Name
int CFBD_I2CInit(CFBD_I2CHandle * bus)
Inline helper to initialize an I2C bus using the backend.
int CFBD_I2CDeInit(CFBD_I2CHandle * bus)
Inline helper to deinitialize an I2C bus.
int CFBD_I2CTransfer(CFBD_I2CHandle * bus, CFBD_I2C_Message * msgs, int num, uint32_t timeout_ms)
Inline wrapper for message transfers.
int CFBD_I2CTransferIsDeviceReady(CFBD_I2CHandle * bus, uint16_t addr, uint32_t trials, uint32_t timeout_ms)
Check if a device is ready using the backend provider.
int CFBD_I2CRecoverBus(CFBD_I2CHandle * bus)
Attempt bus recovery using backend hook.

Detailed Description

Convenience inline functions for I2C operations.

These inline functions wrap backend operations, adding basic validation and NULL-safety checks before delegating to the backend.

Functions Documentation

function CFBD_I2CInit

static inline int CFBD_I2CInit(
    CFBD_I2CHandle * bus
)

Inline helper to initialize an I2C bus using the backend.

Parameters:

  • bus Pointer to I2C handle

Return: int I2C_OK on success or a negative error code.

Par: Example

CFBD_I2CHandle bus = get_i2c_bus();
if (CFBD_I2CInit(&bus) != I2C_OK) {
    handle_init_error();
}

function CFBD_I2CDeInit

static inline int CFBD_I2CDeInit(
    CFBD_I2CHandle * bus
)

Inline helper to deinitialize an I2C bus.

Parameters:

  • bus Pointer to I2C handle

Return: int I2C_OK on success or a negative error code.

function CFBD_I2CTransfer

static inline int CFBD_I2CTransfer(
    CFBD_I2CHandle * bus,
    CFBD_I2C_Message * msgs,
    int num,
    uint32_t timeout_ms
)

Inline wrapper for message transfers.

Parameters:

  • bus I2C bus handle
  • msgs Array of I2C messages
  • num Number of messages in array
  • timeout_ms Operation timeout in milliseconds

Return: int I2C_OK on success or a negative error code.

Par: Example - Simple Write

uint8_t cmd[] = { 0x01, 0x02, 0x03 };
CFBD_I2C_Message msg = {
    .addr = 0x50,
    .flags = 0,  // Write
    .len = sizeof(cmd),
    .buf = cmd
};

int status = CFBD_I2CTransfer(bus, &msg, 1, 1000);

function CFBD_I2CTransferIsDeviceReady

static inline int CFBD_I2CTransferIsDeviceReady(
    CFBD_I2CHandle * bus,
    uint16_t addr,
    uint32_t trials,
    uint32_t timeout_ms
)

Check if a device is ready using the backend provider.

Parameters:

  • bus I2C bus handle
  • addr 7-bit device address
  • trials Number of probe attempts
  • timeout_ms Per-trial timeout in milliseconds

Return: int I2C_OK if device ready, negative error code otherwise.

Par: Example - Device Presence Check

// Wait for device to become ready (useful after power-on)
int status = CFBD_I2CTransferIsDeviceReady(bus, 0x3C, 10, 100);
if (status == I2C_OK) {
    // Device is present and responding
} else if (status == I2C_ERR_NACK) {
    // Device not responding
}

function CFBD_I2CRecoverBus

static inline int CFBD_I2CRecoverBus(
    CFBD_I2CHandle * bus
)

Attempt bus recovery using backend hook.

Parameters:

  • bus I2C bus handle

Return: int I2C_OK if recovery successful, error code if failed.

Par: Example - Stuck Bus Recovery

int retry_count = 0;
int status;

do {
    status = CFBD_I2CTransfer(bus, msgs, count, 100);
    if (status == I2C_OK) {
        break;
    }

    if (status == I2C_ERR_TIMEOUT || status == I2C_ERR_IO) {
        CFBD_I2CRecoverBus(bus);  // Attempt recovery
        delay_ms(50);
    }
} while (++retry_count < 3);

Call this when transfers are failing persistently. Recovery typically involves toggling SCL, sending dummy clocks, or issuing STOP conditions.


Updated on 2026-02-03 at 13:21:55 +0000