3.14. i2c_soft
— Software I2C¶
I2C is a data transfer bus. Normally one master and one or more slaves are connected to the bus. The master addresses one slave at a time to transfer data between the devices.
This driver implements I2C in software for MCUs without I2C hardware support. For systems with hardware I2C support, the i2c — I2C driver will probably be preferable.
Source code: src/drivers/i2c_soft.h, src/drivers/i2c_soft.c
Test code: tst/drivers/i2c/master_soft/main.c
Functions
-
int
i2c_soft_module_init
(void)¶ Initialize the i2c soft module. This function must be called before calling any other function in this module.
The module will only be initialized once even if this function is called multiple times.
- Return
- zero(0) or negative error code.
-
int
i2c_soft_init
(struct i2c_soft_driver_t *self_p, struct pin_device_t *scl_dev_p, struct pin_device_t *sda_dev_p, long baudrate, long max_clock_stretching_us, long clock_stretching_sleep_us)¶ Initialize given driver object.
- Return
- zero(0) or negative error code.
- Parameters
self_p
: Driver object to initialize.scl_dev_p
: The I2C clock pin (SCL).sda_dev_p
: The I2C data pin (SDA).baudrate
: Bus baudrate.max_clock_stretching_us
: Maximum number of microseconds to wait for the clock stretching to end.clock_stretching_sleep_us
: SCL poll interval in number of microseconds waiting for clock stretching to end.
-
int
i2c_soft_start
(struct i2c_soft_driver_t *self_p)¶ Start given driver object. Enables data reception and transmission, but does not start any transmission. Data tarnsfers are started by calling the
i2c_soft_read()
andi2c_soft_write()
.- Return
- zero(0) or negative error code.
- Parameters
self_p
: Driver object to initialize.
-
int
i2c_soft_stop
(struct i2c_soft_driver_t *self_p)¶ Stop given driver object. Disables data reception and transmission.
- Return
- zero(0) or negative error code.
- Parameters
self_p
: Driver object to initialize.
-
ssize_t
i2c_soft_read
(struct i2c_soft_driver_t *self_p, int address, void *buf_p, size_t size)¶ Read given number of bytes into given buffer from given slave.
- Return
- Number of bytes read or negative error code.
- Parameters
self_p
: Driver object.address
: Slave address to read from.buf_p
: Buffer to read into.size
: Number of bytes to read.
-
ssize_t
i2c_soft_write
(struct i2c_soft_driver_t *self_p, int address, const void *buf_p, size_t size)¶ Write given number of bytes from given buffer to given slave.
- Return
- Number of bytes written or negative error code.
- Parameters
self_p
: Driver object.address
: Slave address to write to.buf_p
: Buffer to write.size
: Number of bytes to write.
-
int
i2c_soft_scan
(struct i2c_soft_driver_t *self_p, int address)¶ Scan the i2c bus for a slave with given address.
- Return
- true(1) if a slave responded to given address, otherwise false(0) or negative error code.
- Parameters
self_p
: Driver object.address
: Address of the slave to scan for.
-
struct
i2c_soft_driver_t
¶ - #include <i2c_soft.h>