3.4.3. i2c — 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.

The master is normally fairly easy to implement since it controls the bus clock and no race conditions can occur. The slave, on the other hand, can be implemented in various ways depending on the application requirements. In this implementation the slave will always send an acknowledgement when addressed by the master, and lock the bus by pulling SCL low until it is ready for the transmission.

This driver is for systems with hardware I2C support. For systems without hardware I2C support the i2c_soft — Software I2C driver can be used.


Source code: src/drivers/network/i2c.h, src/drivers/network/i2c.c

Test code: tst/drivers/hardware/network/i2c/master/main.c


Defines

I2C_BAUDRATE_3_2MBPS
I2C_BAUDRATE_1MBPS
I2C_BAUDRATE_400KBPS
I2C_BAUDRATE_100KBPS

Functions

int i2c_module_init(void)

Initialize the i2c 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_init(struct i2c_driver_t *self_p, struct i2c_device_t *dev_p, int baudrate, int address)

Initialize given driver object. The same driver object is used for both master and slave modes. Use i2c_start() to start the device as a master, and i2c_slave_start() to start it as a slave.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to initialize.
  • dev_p: I2C device to use.
  • baudrates: Bus baudrate when in master mode. Unused in slave mode.
  • address: Slave address when in slave mode. Unused in master mode.

int i2c_start(struct i2c_driver_t *self_p)

Start given driver object in master mode. Enables data reception and transmission, but does not start any transmission. Use i2c_read() and i2c_write() to exchange data with the peer.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to initialize.

int i2c_stop(struct i2c_driver_t *self_p)

Stop given driver object. Disables data reception and transmission in master mode.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to initialize.

ssize_t i2c_read(struct i2c_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_write(struct i2c_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_scan(struct i2c_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.

int i2c_slave_start(struct i2c_driver_t *self_p)

Start given driver object in slave mode. Enables data reception and transmission, but does not start any transmission. Data transfers are started by calling the i2c_slave_read() and i2c_slave_write().

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to initialize.

int i2c_slave_stop(struct i2c_driver_t *self_p)

Stop given driver object. Disables data reception and transmission in slave mode.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to initialize.

ssize_t i2c_slave_read(struct i2c_driver_t *self_p, void *buf_p, size_t size)

Read into given buffer from the next master that addresses this slave.

Return
Number of bytes read or negative error code.
Parameters
  • self_p: Driver object.
  • buf_p: Buffer to read into.
  • size: Number of bytes to read.

ssize_t i2c_slave_write(struct i2c_driver_t *self_p, const void *buf_p, size_t size)

Write given buffer to the next master that addresses this slave.

Return
Number of bytes written or negative error code.
Parameters
  • self_p: Driver object.
  • buf_p: Buffer to write.
  • size: Number of bytes to write.

Variables

struct i2c_device_t i2c_device[I2C_DEVICE_MAX]