Appearance
I2C
I2C API.
Functions for the Inter-IC-Communication Bus
Sysprops
| sysprop | description |
|---|---|
| gpio_sda | SDA GPIO |
| gpio_scl | SCL GPIO |
Example
cpp
#include <i2c.h>
#define SENSOR_ADDR 0x68
#define REG_WHO_AM_I 0x75
int sensor_probe(void) {
int h = open("i2c0");
if (h < 0) return -1;
uint8_t id = 0;
// write-then-read with repeated-start: select register, then read its value
if (i2c_read_reg(h, SENSOR_ADDR, REG_WHO_AM_I, &id, 1) != 0) {
close(h);
return -1;
}
close(h);
return id;
}Functions Overview
| Name | |
|---|---|
| cw_driver_return_t | i2c_read(int hDev, uint8_t addr, uint8_t * data, uint8_t len) |
| cw_driver_return_t | i2c_read_reg(int hDev, uint8_t addr, uint8_t reg, uint8_t * data, uint8_t len) |
| cw_driver_return_t | i2c_write(int hDev, uint8_t addr, const uint8_t * data, uint8_t len) Writes a buffer of bytes to an I²C device. |
| cw_driver_return_t | i2c_write_reg(int hDev, uint8_t addr, uint8_t reg, const uint8_t * data, uint8_t len) |
| cw_driver_return_t | i2c_transfer(int hDev, uint8_t addr, const uint8_t * data_w, uint8_t len_w, uint8_t * data_r, uint8_t len_r) Performs a combined write-then-read transaction with a repeated start. |
| cw_driver_return_t | i2c_clear(int hDev) resets a stuck bus by sending 9 clock pulses |
Function Details
function i2c_read
cpp
cw_driver_return_t i2c_read(
int hDev,
uint8_t addr,
uint8_t * data,
uint8_t len
)Parameters:
- hDev device handle as returned by open(const char*)
- addr the 7bit address of the device to read from.
- data buffer holding the space for the data to be read.
- len length of the data buffer in bytes.
Return: 0 if the data has successfully been read.
function i2c_read_reg
cpp
cw_driver_return_t i2c_read_reg(
int hDev,
uint8_t addr,
uint8_t reg,
uint8_t * data,
uint8_t len
)Parameters:
- hDev device handle as returned by open(const char*)
- addr the 7bit address of the device to read from.
- reg the device register to read data from
- data buffer holding the space for the data to be read.
- len length of the data buffer in bytes.
Return: 0 if the data has successfully been read.
function i2c_write
cpp
cw_driver_return_t i2c_write(
int hDev,
uint8_t addr,
const uint8_t * data,
uint8_t len
)Writes a buffer of bytes to an I²C device.
Parameters:
- hDev device handle as returned by open(const char*)
- addr the 7bit address of the device to write to.
- data buffer containing the data to be written.
- len length of the data buffer in bytes.
Return: 0 if the data has been successfully written, negative on error.
function i2c_write_reg
cpp
cw_driver_return_t i2c_write_reg(
int hDev,
uint8_t addr,
uint8_t reg,
const uint8_t * data,
uint8_t len
)Parameters:
- hDev device handle as returned by open(const char*)
- addr the 7bit address of the device to write to.
- reg the device register to be written to
- data buffer containing the data to be written.
- len length of the data buffer in bytes.
Return: 0 if the data has been successfully written.
function i2c_transfer
cpp
cw_driver_return_t i2c_transfer(
int hDev,
uint8_t addr,
const uint8_t * data_w,
uint8_t len_w,
uint8_t * data_r,
uint8_t len_r
)Performs a combined write-then-read transaction with a repeated start.
Parameters:
- hDev device handle as returned by open(const char*)
- addr the 7bit address of the device to address.
- data_w buffer containing the bytes to be written.
- len_w length of the write buffer in bytes.
- data_r buffer that receives the bytes read back.
- len_r length of the read buffer in bytes.
Return: 0 if the transaction has been successfully completed, negative on error.
Writes len_w bytes from data_w to the device at addr, issues a repeated-start condition (no STOP in between), and then reads len_r bytes into data_r. This is the typical pattern for register-based devices where the read must immediately follow the register address selection without releasing the bus.
function i2c_clear
cpp
cw_driver_return_t i2c_clear(
int hDev
)resets a stuck bus by sending 9 clock pulses
Parameters:
- hDev device handle as returned by open(const char*)