I2C User Guide
Summary
I2C is a serial synchronous half duplex communication protocol. Multiple hosts and slaves can be attached to the bus at the same time. I2C bus is composed of serial data line (SDA) and serial clock line (SCL). These wires need pull-up resistance.
I2C has the advantages of simplicity and low manufacturing cost, and is mainly used for short distance communication of low-speed peripheral devices (within one foot)
I2C Connection
The above figure shows I2C hardware connection, in which:
SCL:Clock signal, which provides the clock for data transmission to synchronize data transmission
SDA:Used to transfer data.
Beken The chip usually has two I2C controllers, and BK7271 supports three I2Cs (I2C0, I2C1, and I2C2).
I2C Communication Flow
In master mode, the I2C interface initiates data transfer and generates a clock signal. Serial data transfer always begins with the appearance of a START bit (STA) and ends with the appearance of a STOP bit (STO). The start and stop bits are both generated by software in master mode. The START bit is generated by pulling the SDA line low while the SCL is high, and the STOP bit is generated by pulling the SDA line high while the SCL is high.
In slave mode, the interface is capable of recognizing its own address (7 or 10 bits) as well as a broadcast call address. Data and addresses are transmitted in 8-bit bytes, with the most significant bit (MSB) first. The address byte follows immediately after the START bit (a 7-bit address occupies one byte; a 10-bit address occupies two bytes). The address is always transmitted in master mode.
Transmission is ended by sending a STOP bit on the bus after an (N)ACK. After the STOP bit, any master device that wishes to initiate transfer on the bus may attempt to gain control of the bus. If the current master device wishes to initiate another transfer immediately after the current one, it can start a new transfer directly by sending a repeated START bit (Sr), rather than sending a STOP and then a START.
Figure 2. I2C_Communication Flow
I2C sequence flow
The interface supports 7-bit and 10-bit addressing modes. The interface transmission timing under different addressing modes is shown in the following figure. In 7-bit addressing mode, the master device only needs to send 1 address byte after sending STA, with the content of SLV_ADDR[6:0]+W. In 10-bit addressing mode, the master device needs to send 2 address bytes after sending STA, with the first address byte containing 11110+SLV_ADDR[9:8]+W, and the second address byte containing SLV_ADDR[7:0].
The slave device detects STA and starts receiving address bytes. In 7-bit addressing mode, when the received first address byte matches the software-configured slave address, an interrupt is reported. In 10-bit addressing mode, only when the received first 2 address bytes match the software-configured slave address will an interrupt be reported, and the address bytes will not be written into the internal receive FIFO.
Figure 3. I2C_sequence_flow
Use of I2C
Beken I2C It can work in both master mode and slave mode. Whether the host or slave, you need to call ‘bk_i2c_init()’ to configure I2C before reading and writing. bk_i2c_init() It mainly configures the clock frequency of I2C SCL. If it works in slave mode, it also needs to configure the device address of Beken I2C.
Note
For bk7236/bk7231n/bk7251, I2C_ID_1 (corresponding to I2C2 on address_mapping) shares GPIO with UART2, so when I2C_ID_1 is enabled, UART2 cannot be used.
Communication in I2C host mode
Call ‘bk_i2c_master_write()’ to write data to the slave device and ‘bk_i2c_master_read()’ to read data. For memory devices such as eeprom, Beken provides an A/B interface that can be assigned to the internal address of the slave device to perform read and write operations.For memory devices such as eeprom, Beken provides a ‘bk_i2c_memory_write()/bk_i2c_memory_read()’ interface that can be assigned to the internal address of the slave device to perform read and write operations.
I2C memory write
I2C memory read
I2C communication in slave mode
For slave mode, Beken provides ‘bk_i2c_slave_write()/bk_i2c_slave_read()’ for sending/receiving data.