This repository provides an overview of the I2C (Inter-Integrated Circuit) communication protocol.
I2C (Inter-Integrated Circuit) is a serial digital communication bus standard designed to interconnect integrated circuits (ICs) on the same board. It was first introduced by Philips in the 1980s to interconnect integrated circuits in televisions. It is widely used in various integrated devices that require data communication.
- Two-wire bus: SDA (Serial Data) and SCL (Serial Clock).
- Synchronous communication: The clock (SCL) is provided by the master to synchronize data transfer.
- Multiple device support: A master can communicate with multiple slaves, each identified by a unique address.
- Speed: Can reach a maximum speed of 400 Kbps.
- Master: The device that initiates and controls communication on the I2C bus. Typically a microcontroller (MCU). There can only be one master in an I2C network.
- Slaves: Devices that respond to requests from the master. Each slave has a unique address.
- SDA (Serial Data): Bidirectional data line used for serial bit transfer.
- SCL (Serial Clock): Unidirectional clock line provided by the master to synchronize transmission.
-
7-bit addresses: Widely used standard.
-
10-bit addresses: Used in some special cases.
-
Direction Bit: An additional bit (B0) specifies the direction of data transfer: "0" for writing and "1" for reading.
-
Example: Accelerometer with address
0xd6: -
Write:
0xd6(1 1 0 1 0 1 1 0) -
Read:
0xd7(1 1 0 1 0 1 1 1)
- The master sends a START CONDITION (S).
- The master sends the device address with bit B0 set to "0" (write).
- The master sends the register number to write.
- The master sends the register data to write.
- The previous steps are repeated for each subsequent register to write.
- The master sends a STOP CONDITION (P).
- The slave responds with an ACKNOWLEDGE (ACK), a 0 bit sent on the SDA line for each byte written.
- The master sends a START CONDITION (S).
- The master sends the device address with bit B0 set to "0" (write).
- The master sends the register number to be read.
- The master sends a new START CONDITION (S).
- The master sends the device address with bit B0 set to "1" (read).
- The slave sends the register data read.
- The previous steps are repeated for each subsequent register to be read.
- The master sends a STOP CONDITION (P).
- The peer device (master or slave) responds with an ACKNOWLEDGE (ACK), a 0 bit sent on the SDA line for each byte transmitted.
- BUS IDLE: Both the SDA and SCL lines are in the "1" state.
- START CONDITION (S): A high-to-low transition on SDA while SCL is high.
- STOP CONDITION (P): A low-to-high transition on SDA while SCL is high.
- Data transfer occurs serially, MSB-first.
- The bit value is set on the SDA line.
- A low-to-high-to-low pulse occurs on the SCL line.
- The next bit is sent.
- ACKNOWLEDGE (ACK): After all 8 bits have been transmitted, an acknowledge is expected.
- The master generates a ninth clock pulse.
- The receiving device holds the SDA line low to signal that it has understood the sent byte.
- The master starts communication with a Start Condition.
- The master sends the Write Command, an 8-bit data composed of:
- The 7-bit address of the slave device.
- The R/W bit set to 0, meaning "write-to-slave".
- The addressed slave responds with an ACK, holding the SDA line low in the ninth clock pulse.
- The master sends an 8-bit data that means the register number.
- The addressed slave responds with an ACK, holding the SDA line low in the ninth clock pulse.
- The master sends an 8-bit data that means the register value.
- The addressed slave responds with an ACK, holding the SDA line low in the ninth clock pulse.
- The master ends the transmission by sending a Stop Condition.
- The master starts the communication with a Start Condition.
- The master sends the Write Command, an 8-bit data composed of:
- The 7-bit address of the slave device.
- The R/W bit at 0, which means "write-to-slave".
- The addressed slave responds with an ACK, holding the SDA line low in the ninth clock pulse.
- After the address, the master sends an 8-bit data value that means register number.
- The addressed slave responds with an ACK, holding the SDA line low in the ninth clock pulse.
- The master sends a new Start Condition.
- The master sends the Read Command, an 8-bit data value composed of:
- The 7-bit address of the slave device.
- The R/W bit set to 1, meaning "read-from-slave".
- The addressed slave responds with an ACK, holding the SDA line low in the ninth clock pulse.
- The slave is now ready to send bytes.
- The slave sends an 8-bit data value.
- The master responds with an ACK, holding the SDA line low in the ninth clock pulse.
- The slave sends the next 8-bit data value (the next register value).
- The master responds with an ACK, holding the SDA line low on the 9th clock pulse.
- When the master is no longer interested in the data, it closes the communication by sending a NACK (holding the SDA line high on the 9th clock pulse) and then a Stop Condition.
- Initialization:
void I2C_init(I2C_TypeDef * port, int speed) port: Interface (usuallyI2C1).speed: Clock speed (e.g.100000,400000).- Writing a single register:
void I2C_write_register(I2C_TypeDef * port, short addr, short reg_adr, short reg_val) port: Interface (usuallyI2C1).addr: Device address.reg_adr: Register address.reg_val: Register value.- Reading a single register:
void I2C_read_register(I2C_TypeDef * port, short addr, short reg_adr, short * reg_val) port: Interface (usuallyI2C1).addr: Device address.reg_adr: Register address.reg_val: Pointer to the variable that will receive the register value.- Writing a set of registers:
void I2C_write_buffer(I2C_TypeDef * port, short addr, short reg_adr, unsigned char * data, int len) port: Interface (usuallyI2C1).addr: Device address.reg_adr: Register address.data: Buffer containing register values.len: Buffer length (number of registers).- Reading a set of registers:
void I2C_read_buffer(I2C_TypeDef * port, short addr, short reg_adr, unsigned char * data, int len) port: Interface (usuallyI2C1).addr: Device address.reg_adr: Register address.data: Buffer that will receive the register values.len: Length of the buffer (number of registers).
In this directory, you will find implementations of I2C using the
stm32_unict_lib.hlibrary for the STM32 microcontroller.
- Visual Studio Code with the PlatformIO.
stm32_unict_liblibrary provided by the university for the STM32 microcontroller.
This project is licensed under the MIT license. See the LICENSE file for more details.
Alessandro Ferrante