Inter-Integrated Circuit (I2C) interface. More...
![]() |
Data Structures | |
struct | hw_i2c_t |
I2C adapter. More... | |
Typedefs | |
typedef struct hw_i2c_t | hw_i2c_t |
I2C adapter. More... | |
Functions | |
bool | hw_i2c_init_default (hw_i2c_t *adapter, uint32_t baudrate) |
Initialize an I2C interface using default pins and adapter. More... | |
bool | hw_i2c_init (hw_i2c_t *adapter, uint8_t index, uint8_t sda, uint8_t scl, uint32_t baudrate) |
Initialize an I2C interface with specific adapter and pins. More... | |
bool | hw_i2c_init_device (hw_i2c_t *adapter, const char *device, uint32_t baudrate) |
Initialize an I2C interface with device path. More... | |
void | hw_i2c_finalize (hw_i2c_t *i2c) |
Finalize and release an I2C interface. More... | |
uint8_t | hw_i2c_count (void) |
Get the total number of available I2C adapters. More... | |
bool | hw_i2c_valid (hw_i2c_t *i2c) |
Get true if the I2C interface is valid. More... | |
bool | hw_i2c_detect (hw_i2c_t *i2c, uint8_t addr) |
Detect if an I2C device is present at the specified address. More... | |
size_t | hw_i2c_xfr (hw_i2c_t *i2c, uint8_t addr, void *data, size_t tx, size_t rx, uint32_t timeout_ms) |
Perform an I2C transfer operation (read, write, or combined). More... | |
size_t | hw_i2c_read (hw_i2c_t *i2c, uint8_t addr, uint8_t reg, void *data, size_t len, uint32_t timeout_ms) |
Read data from a specific register of an I2C device. More... | |
size_t | hw_i2c_write (hw_i2c_t *i2c, uint8_t addr, uint8_t reg, const void *data, size_t len, uint32_t timeout_ms) |
Write data to a specific register of an I2C device. More... | |
Inter-Integrated Circuit (I2C) interface.
This module provides functions to initialize I2C peripherals, and bi-directional data transfer with I2C devicesin master mode.
I2C adapter.
Platform-specific I2C interface structure. The internal implementation is opaque and stored in reserved bytes.
uint8_t hw_i2c_count | ( | void | ) |
Get the total number of available I2C adapters.
Returns the number of logical I2C adapters that can be used in the system. These are usually numbered from 0 to hw_i2c_count() - 1. If zero is returned, it indicates that I2C functionality is not available.
bool hw_i2c_detect | ( | hw_i2c_t * | i2c, |
uint8_t | addr | ||
) |
Detect if an I2C device is present at the specified address.
i2c | Pointer to the I2C structure representing the interface. |
addr | The 7-bit I2C slave device address (without R/W bit). |
This function checks if an I2C device is present at the specified address by attempting to communicate with it. It returns true if the device responds to a basic read/write operation, false otherwise.
void hw_i2c_finalize | ( | hw_i2c_t * | i2c | ) |
Finalize and release an I2C interface.
i2c | Pointer to the I2C structure to finalize. |
This function releases the I2C interface and frees any associated resources. After calling this function, the I2C interface should not be used for further operations.
bool hw_i2c_init | ( | hw_i2c_t * | adapter, |
uint8_t | index, | ||
uint8_t | sda, | ||
uint8_t | scl, | ||
uint32_t | baudrate | ||
) |
Initialize an I2C interface with specific adapter and pins.
adapter | The I2C adapter structure |
index | The I2C adapter index to use (0 to hw_i2c_count()-1). |
sda | The GPIO pin number to use for I2C data (SDA). |
scl | The GPIO pin number to use for I2C clock (SCL). |
baudrate | The desired I2C baud rate (e.g., 100000 for 100kHz). |
This function initializes an I2C interface using the specified adapter and GPIO pins for SDA and SCL lines. The adapter index should be valid and the specified pins should be capable of I2C functionality.
bool hw_i2c_init_default | ( | hw_i2c_t * | adapter, |
uint32_t | baudrate | ||
) |
Initialize an I2C interface using default pins and adapter.
adapter | The I2C adapter structure |
baudrate | The desired I2C baud rate (e.g., 100000 for 100kHz). |
This function initializes the default I2C interface using platform-specific default pins and adapter settings. This is the simplest way to get I2C functionality without needing to specify pin assignments.
bool hw_i2c_init_device | ( | hw_i2c_t * | adapter, |
const char * | device, | ||
uint32_t | baudrate | ||
) |
Initialize an I2C interface with device path.
adapter | The I2C adapter structure |
device | The I2C device path (e.g., "/dev/i2c-0"). |
baudrate | The desired I2C baud rate (e.g., 100000 for 100kHz). |
This function initializes an I2C interface using the specified device path and baud rate. The device path should be valid and correspond to an I2C adapter on the system.
size_t hw_i2c_read | ( | hw_i2c_t * | i2c, |
uint8_t | addr, | ||
uint8_t | reg, | ||
void * | data, | ||
size_t | len, | ||
uint32_t | timeout_ms | ||
) |
Read data from a specific register of an I2C device.
i2c | Pointer to the I2C structure representing the interface. |
addr | The 7-bit I2C slave device address (without R/W bit). |
reg | The register address to read from. |
data | Pointer to the buffer where read data will be stored. |
len | Number of bytes to read from the register. |
timeout_ms | Timeout value in milliseconds for the operation. Set to 0 for no timeout. |
bool hw_i2c_valid | ( | hw_i2c_t * | i2c | ) |
Get true if the I2C interface is valid.
i2c | Pointer to the I2C structure representing the interface. |
The result of hw_i2c_init can return an empty I2C structure if the initialization fails. This function checks if the I2C interface is valid.
size_t hw_i2c_write | ( | hw_i2c_t * | i2c, |
uint8_t | addr, | ||
uint8_t | reg, | ||
const void * | data, | ||
size_t | len, | ||
uint32_t | timeout_ms | ||
) |
Write data to a specific register of an I2C device.
i2c | Pointer to the I2C structure representing the interface. |
addr | The 7-bit I2C slave device address (without R/W bit). |
reg | The register address to write to. |
data | Pointer to the buffer containing data to write. |
len | Number of bytes to write to the register. |
timeout_ms | Timeout value in milliseconds for the operation. Set to 0 for no timeout. |
size_t hw_i2c_xfr | ( | hw_i2c_t * | i2c, |
uint8_t | addr, | ||
void * | data, | ||
size_t | tx, | ||
size_t | rx, | ||
uint32_t | timeout_ms | ||
) |
Perform an I2C transfer operation (read, write, or combined).
i2c | Pointer to the I2C structure representing the interface. |
addr | The 7-bit I2C slave device address (without R/W bit). |
data | Pointer to the data buffer for transmission and/or reception. |
tx | Number of bytes to transmit from the data buffer. |
rx | Number of bytes to receive into the data buffer (after tx bytes). |
timeout_ms | Timeout value in milliseconds for each operation. Set to 0 for no timeout. |
This function performs I2C transfer operations which can be:
For combined operations, the function first transmits 'tx' bytes from the data buffer, then receives 'rx' bytes into the same buffer starting at offset 'tx'. The total buffer size should be at least (tx + rx) bytes.