Detailed explanation of IIC (I2C) protocol

1 Introduction

IIC, namely I²C, full name Inter-Integrated Circuit, literally means between integrated circuits, it is actually the abbreviation of I²C Bus, so Chinese should be called integrated circuit bus, it is a serial communication bus, using multi-master-slave architecture , developed by Philips in the 1980s to allow motherboards, embedded systems or mobile phones to connect low-speed peripherals.

More embedded/single chip microcomputer/c language/linux content: IIC (I2C) protocol detailed explanation

2. I2C hardware connection

The connection method of I2C on the hardware is as follows. The main control chip leads to two lines SCL (SCK) clock line and SDA data line . Many I2C devices can be connected to one I2C bus. The two lines SDA and SCL must be connected. A pull-up resistor, commonly used is 4.7K, generally between 4.7k-10k.

SCL and SDA are high when the bus is idle.

The speed of the IIC bus can reach 100Kb/s in standard mode, 400Kb/s in fast mode, and 3.4Mb/s in high-speed mode.

3. I2C signal

The unit of data transmission in the I2C protocol is byte, which is 8 bits. But 9 clocks are used: the first 8 clocks are used to transmit 8 data, and the 9th clock is used to transmit the response signal. When transmitting, the most significant bit (MSB) is transmitted first.

Start signal (S): When SCL is at high level, SDA jumps from high level to low level, and starts to transmit data.

End signal (P): When SCL is at high level, SDA transitions from low level to high level, ending data transmission.

Response signal (ACK): After receiving 8-bit data, the receiver pulls down SDA in the 9th clock cycle

The data transmitted on SDA must be stable during the high level of SCL, and the data on SDA can only change during the low level of SCL

The I2C protocol signals are as follows:

4. IIC 传输数据的格式

4.1写操作

1)主芯片要发出一个 start 信号

2)然后发出一个设备地址(用来确定是往哪一个芯片写数据),方向(读/写, 0 表示写, 1 表示读)

3)从设备回应(用来确定这个设备是否存在),然后就可以传输数据

4)主设备发送一个字节数据给从设备,并等待回应

5)每传输一字节数据,接收方要有一个回应信号(确定数据是否接受完成),然后再传输下一个数据。

6) 数据发送完之后,主芯片就会发送一个停止信号

7)下图:白色背景表示"主→从",灰色背景表示"从→主"

4.2读操作

1)主芯片要发出一个 start 信号

2)然后发出一个设备地址(用来确定是往哪一个芯片写数据),方向(读/写, 0 表示写, 1 表示读)

3)从设备回应(用来确定这个设备是否存在),然后就可以传输数据

4)从设备发送一个字节数据给主设备,并等待回应

5)每传输一字节数据,接收方要有一个回应信号(确定数据是否接受完成),然后再传输下一个数据。

6)数据发送完之后,主芯片就会发送一个停止信号

下图:白色背景表示"主→从",灰色背景表示"从→主"

5. 协议细节

5.1如何在 SDA 上实现双向传输?

主芯片通过一根 SDA 线既可以把数据发给从设备,也可以从 SDA 上读取数据,连接 SDA 线的引脚里面必然有两个引脚(发送引脚/接受引脚)。

5.2内部原理

设备的 SDA 中有一个三极管,使用开极/开漏电路(三极管是开极, CMOS管是开漏,作用一样),如下图:

真值表如下:

从真值表和电路图我们可以知道:

  • 当某一个芯片不想影响 SDA 线时,那就不驱动这个三极管

  • 想让 SDA 输出高电平,双方都不驱动三极管(SDA 通过上拉电阻变为高电平)

  • 想让 SDA 输出低电平,就驱动三极管

5.3为何 SCL 也要使用上拉电阻?

在第 9 个时钟之后,如果有某一方需要更多的时间来处理数据,它可以一直驱动三极管把SCL 拉低。

当 SCL 为低电平时候,大家都不应该使用 IIC 总线,只有当 SCL 从低电平变为高电平的时候, IIC 总线才能被使用。

当它就绪后,就可以不再驱动三极管,这是上拉电阻把 SCL 变为高电平,其他设备就可以继续使用 I2C 总线了。

5.4 I2C总线最多可以挂多少个设备

I2C是7位地址,2^7=128,但是地址0x00不用,那就是127个地址,所以理论上可以挂127个从器件。

尽管I2C协议没有规定总线上device最大数目,但是规定了总线电容不能超过400pF。

管脚都是有输入电容的,PCB上也会有寄生电容,所以会有一个限制。

实际设计中经验值大概是不超过8个器件

规定电容大小的原因:I2C的OD(漏极开路)要求外部有电阻上拉,电阻和总线电容产生了一个RC延时效应,电容越大信号的边沿就越缓,有可能带来信号质量风险。传输速度越快,信号的窗口就越小,上升沿下降沿时间要求更短更陡峭,所以RC乘积必须更小。

Guess you like

Origin blog.csdn.net/freestep96/article/details/128559772