oveRTOS C API
Embedded RTOS framework — build system, configuration, and portable C API
Loading...
Searching...
No Matches
Data Structures | Macros | Enumerations | Functions
I2C

I2C bus master driver. More...

Data Structures

struct  ove_i2c_cfg
 I2C bus configuration descriptor. More...
 

Macros

#define OVE_I2C_REG_WRITE_MAX   32
 

Enumerations

enum  ove_i2c_speed_t { OVE_I2C_SPEED_STANDARD = 0 , OVE_I2C_SPEED_FAST = 1 , OVE_I2C_SPEED_FAST_PLUS = 2 }
 I2C bus speed grade. More...
 

Functions

int ove_i2c_init (ove_i2c_t *i2c, ove_i2c_storage_t *storage, const struct ove_i2c_cfg *cfg)
 Initialise an I2C bus using caller-provided static storage.
 
void ove_i2c_deinit (ove_i2c_t i2c)
 Deinitialise a statically-allocated I2C bus.
 
int ove_i2c_create (ove_i2c_t *i2c, const struct ove_i2c_cfg *cfg)
 Create a heap-allocated I2C bus controller.
 
void ove_i2c_destroy (ove_i2c_t i2c)
 Destroy a heap-allocated I2C bus controller.
 
int ove_i2c_write (ove_i2c_t i2c, uint16_t addr, const void *data, size_t len, uint64_t timeout_ns)
 Write data to an I2C device.
 
int ove_i2c_read (ove_i2c_t i2c, uint16_t addr, void *buf, size_t len, uint64_t timeout_ns)
 Read data from an I2C device.
 
int ove_i2c_write_read (ove_i2c_t i2c, uint16_t addr, const void *tx, size_t tx_len, void *rx, size_t rx_len, uint64_t timeout_ns)
 Combined write-then-read with I2C repeated start.
 
int ove_i2c_reg_write (ove_i2c_t i2c, uint16_t addr, uint8_t reg, const void *data, size_t len, uint64_t timeout_ns)
 Write to a single-byte-addressed register.
 
int ove_i2c_reg_read (ove_i2c_t i2c, uint16_t addr, uint8_t reg, void *buf, size_t len, uint64_t timeout_ns)
 Read from a single-byte-addressed register.
 
int ove_i2c_probe (ove_i2c_t i2c, uint16_t addr, uint64_t timeout_ns)
 Probe for a device at the given address.
 
int ove_i2c_write_read_async (ove_i2c_t i2c, uint16_t addr, const void *tx, size_t tx_len, void *rx, size_t rx_len, ove_dma_complete_cb cb, void *user_data)
 Submit an I2C write-then-read transaction asynchronously.
 

Detailed Description

I2C bus master driver.

Provides a portable I2C master API with configurable bus speed, thread-safe bus locking, register-level convenience functions, and device probing.

Two allocation strategies are supported:

All addresses are 7-bit (e.g. 0x50 for a typical EEPROM). The HAL shifts left and adds the R/W bit internally.

Note
Requires CONFIG_OVE_I2C. When the option is disabled every function is replaced by a no-op stub that returns OVE_ERR_NOT_SUPPORTED.

Macro Definition Documentation

◆ OVE_I2C_REG_WRITE_MAX

#define OVE_I2C_REG_WRITE_MAX   32

Maximum data bytes per ove_i2c_reg_write() call.

Enumeration Type Documentation

◆ ove_i2c_speed_t

I2C bus speed grade.

Enumerator
OVE_I2C_SPEED_STANDARD 

100 kHz.

OVE_I2C_SPEED_FAST 

400 kHz.

OVE_I2C_SPEED_FAST_PLUS 

1 MHz.

Function Documentation

◆ ove_i2c_init()

int ove_i2c_init ( ove_i2c_t i2c,
ove_i2c_storage_t *  storage,
const struct ove_i2c_cfg cfg 
)

Initialise an I2C bus using caller-provided static storage.

Parameters
[out]i2cReceives the initialised I2C handle.
[in]storagePointer to statically-allocated I2C storage.
[in]cfgBus configuration descriptor.
Returns
OVE_OK on success, negative error code on failure.

◆ ove_i2c_deinit()

void ove_i2c_deinit ( ove_i2c_t  i2c)

Deinitialise a statically-allocated I2C bus.

Parameters
[in]i2cI2C handle returned by ove_i2c_init.

◆ ove_i2c_create()

int ove_i2c_create ( ove_i2c_t i2c,
const struct ove_i2c_cfg cfg 
)

Create a heap-allocated I2C bus controller.

Parameters
[out]i2cReceives the created I2C handle.
[in]cfgBus configuration descriptor.
Returns
OVE_OK on success, negative error code on failure.

◆ ove_i2c_destroy()

void ove_i2c_destroy ( ove_i2c_t  i2c)

Destroy a heap-allocated I2C bus controller.

Parameters
[in]i2cI2C handle returned by ove_i2c_create.

◆ ove_i2c_write()

int ove_i2c_write ( ove_i2c_t  i2c,
uint16_t  addr,
const void *  data,
size_t  len,
uint64_t  timeout_ns 
)

Write data to an I2C device.

Parameters
[in]i2cI2C handle.
[in]addr7-bit device address.
[in]dataData to write.
[in]lenNumber of bytes to write.
[in]timeout_nsMaximum wait time; OVE_WAIT_FOREVER to block.
Returns
OVE_OK on success, negative error code on failure.

◆ ove_i2c_read()

int ove_i2c_read ( ove_i2c_t  i2c,
uint16_t  addr,
void *  buf,
size_t  len,
uint64_t  timeout_ns 
)

Read data from an I2C device.

Parameters
[in]i2cI2C handle.
[in]addr7-bit device address.
[out]bufBuffer to receive data.
[in]lenNumber of bytes to read.
[in]timeout_nsMaximum wait time; OVE_WAIT_FOREVER to block.
Returns
OVE_OK on success, negative error code on failure.

◆ ove_i2c_write_read()

int ove_i2c_write_read ( ove_i2c_t  i2c,
uint16_t  addr,
const void *  tx,
size_t  tx_len,
void *  rx,
size_t  rx_len,
uint64_t  timeout_ns 
)

Combined write-then-read with I2C repeated start.

Writes tx_len bytes from tx, then reads rx_len bytes into rx without releasing the bus (repeated start condition). This is the standard pattern for register reads on I2C sensors and codecs.

Parameters
[in]i2cI2C handle.
[in]addr7-bit device address.
[in]txTransmit buffer (e.g. register address).
[in]tx_lenNumber of bytes to write.
[out]rxReceive buffer.
[in]rx_lenNumber of bytes to read.
[in]timeout_nsMaximum wait time; OVE_WAIT_FOREVER to block.
Returns
OVE_OK on success, negative error code on failure.

◆ ove_i2c_reg_write()

int ove_i2c_reg_write ( ove_i2c_t  i2c,
uint16_t  addr,
uint8_t  reg,
const void *  data,
size_t  len,
uint64_t  timeout_ns 
)

Write to a single-byte-addressed register.

Prepends reg to data and performs a single I2C write. Implemented in the portable layer — not a HAL function.

Note
len must not exceed OVE_I2C_REG_WRITE_MAX (32 bytes).
Parameters
[in]i2cI2C handle.
[in]addr7-bit device address.
[in]regRegister address byte.
[in]dataData to write after the register byte.
[in]lenNumber of data bytes (max OVE_I2C_REG_WRITE_MAX).
[in]timeout_nsMaximum wait time.
Returns
OVE_OK on success, negative error code on failure.

◆ ove_i2c_reg_read()

int ove_i2c_reg_read ( ove_i2c_t  i2c,
uint16_t  addr,
uint8_t  reg,
void *  buf,
size_t  len,
uint64_t  timeout_ns 
)

Read from a single-byte-addressed register.

Writes reg, then reads len bytes via repeated start. Implemented in the portable layer — not a HAL function.

Parameters
[in]i2cI2C handle.
[in]addr7-bit device address.
[in]regRegister address byte.
[out]bufBuffer to receive register data.
[in]lenNumber of bytes to read.
[in]timeout_nsMaximum wait time.
Returns
OVE_OK on success, negative error code on failure.

◆ ove_i2c_probe()

int ove_i2c_probe ( ove_i2c_t  i2c,
uint16_t  addr,
uint64_t  timeout_ns 
)

Probe for a device at the given address.

Sends a zero-length write and checks for ACK. Useful for device enumeration and presence detection.

Parameters
[in]i2cI2C handle.
[in]addr7-bit device address to probe.
[in]timeout_nsMaximum wait time.
Returns
OVE_OK if the device ACKs, OVE_ERR_BUS_NACK if not, other negative error code on bus failure.

◆ ove_i2c_write_read_async()

int ove_i2c_write_read_async ( ove_i2c_t  i2c,
uint16_t  addr,
const void *  tx,
size_t  tx_len,
void *  rx,
size_t  rx_len,
ove_dma_complete_cb  cb,
void *  user_data 
)

Submit an I2C write-then-read transaction asynchronously.

Equivalent to ove_i2c_write_read but returns immediately. The completion callback fires when the device has been written, the repeated-start completed, and the read finished — or when an error (NACK, bus error, timeout) terminates the transaction.

Same backend completion-context contract as ove_spi_transfer_async (ISR on STM32 + FreeRTOS DMA, thread on Zephyr signal / NuttX-POSIX worker fallback).

Parameters
[in]i2cI2C handle.
[in]addr7-bit device address.
[in]txBytes to write before the repeated start. Must outlive the transaction. May be NULL if tx_len is 0 (pure read).
[in]tx_lenNumber of bytes to write.
[in]rxBuffer for read bytes. Must outlive the transaction. May be NULL if rx_len is 0 (pure write).
[in]rx_lenNumber of bytes to read.
[in]cbCompletion callback. Must not be NULL.
[in]user_dataOpaque pointer forwarded to cb.
Returns
OVE_OK if accepted; negative on submission failure (no callback in that case).