I2C 总线精要

来自Jack's Lab
(版本间的差异)
跳转到: 导航, 搜索
(数据传输)
第26行: 第26行:
  
 
Start Condition
 
Start Condition
 +
 +
<br>
 +
 +
== 结束位 ==
 +
 +
Stop Condition
  
 
<br>
 
<br>
第71行: 第77行:
  
 
<br><br>
 
<br><br>
 
== 结束位 ==
 
 
Stop Condition
 
 
<br>
 
  
 
== 版本 ==
 
== 版本 ==

2018年11月4日 (日) 16:19的版本

目录

1 概述

由 Philips (现 NXP) 于 80 年代开发,是一种很可靠的低速外设总线,广泛应用于 PC 和嵌入式系统:各种 I2C 接口的传感器、OLED 显示屏、SMBus (System Management Bus, )、PMBus (Power Management Bus)、IPMI (Intelligent Platform Management Interface)、DDC (Display Data Channel)、ATCA (Advanced Telecom Computing Architecture)......


两根线: SDA, SCL,皆拉高 (pull-up),总线空闲时,两线皆为高电平

  • SDA : Serial Data Line, holds Data or address signal
  • SCL : Serial Clock Line, holds Clock signal
  • IO must be open drain (or open collector in TTL)


I2C data transfer 640.png

I2c com.png


  • SDA must be stable when SCL is high, excluding Start/Stop condition
  • 7bit 地址,保留 16 个,可用 112;标准模式 (100 Kbit/s)、低速模式 (10 Kbit/s)
  • 10bit 地址;快速模式 (400 Kbit/s)、高速模式 (3.4 Mbit/s)



2 开始位

Start Condition


3 结束位

Stop Condition


4 数据传输

  • 7bit mode: 发送 7bit 的address (MSB) 及一位的 W(0) / R(1) 后,该 address 的 slave 端会发送一个 Ack (acknowledge) 位,ACK = 0 表设定成功 (Slave 把 SDA 拉到 LOW),开始数据传送
  • 一次传一个 byte,每个 byte 都跟著一个 ACK bit ,在 ACK bit 时 Master 会释放 bus,此时 Slave 端需发送 ACK (把 SDA 拉 LOW),否则 Master 认为发送失败,发出 STOP 信号或是重新发送 START 信号
  • 在每个 byte 之间 Slave 端可以把 SCL 拉 LOW 来强制让传输暂停


  • SDA 只能在 SCL 为 LOW 时改变
  • 在 SCL 为 HIGH 时,SDA 必须是 stable 的



5 状态图

5.1 主发从收

  • 开始位后,发 7 位地址,随后读写位为 0 (Write)
  • 收到 slave 反馈的 ACK 位后,开始写字节
  • 每写一个字节,需要收到 slave 反馈的 ACK (SDA = 0) 后才开始下一个字节传输,否则传输中止,抛出出错异常
  • 写完所有字节,则发送 Stop condition,Slave 收到 Stop condition 后,才会中止数据接收。。。


I2c flow chart master tx slave rx.jpg


5.2 主收从发

  • 开始位后,发 7 位地址,随后读写位为 1 (Read)
  • 收到 slave 反馈的 ACK 位后,立即开始读取数据 <------
  • 每读一个字节,需要给一个 ACK (SDA = 0),如果是最后一个字节,则需给一个 NACK (SDA = 1)


I2c flow chart master rx slave tx.jpg





6 版本

The history of I²C specification releases:

  • In 1982, the original 100 kHz I²C system was created as a simple internal bus system for building control electronics with various Philips chips.
  • In 1992, Version 1 added 400 kHz Fast-mode (Fm) and a 10-bit addressing mode to increase capacity to 1008 nodes. This was the first standardized version.
  • In 1998, Version 2 added 3.4 MHz High-speed mode (Hs) with power-saving requirements for electric voltage and current.
  • In 2000, Version 2.1 clarified version 2, without significant functional changes.
  • In 2007, Version 3 added 1 MHz Fast-mode plus (Fm+) (using 20 mA drivers), and a device ID mechanism.
  • In 2012, Version 4 added 5 MHz Ultra Fast-mode (UFm) for new USDA (data) and USCL (clock) lines using push-pull logic without pull-up resistors, and added an assigned manufacturer ID table. It is only a unidirectional bus.
  • In 2012, Version 5 corrected mistakes.
  • In 2014, Version 6 corrected two graphs. This is the most recent standard


7 软件模拟

To initialize the ports set the output resisters to 0 and the tristate registers to 1 which disables the outputs and allows them to be pulled high by the resistors.

The following 4 functions provide the primitive start, stop, read and write sequences. All I2C transactions can be built up from these.

SDA = SCL = 1;
SCL_IN = SDA_IN = 0;

void i2c_dly(void)
{
}

void i2c_start(void)
{
  SDA = 1;             // i2c start bit sequence
  i2c_dly();
  SCL = 1;
  i2c_dly();
  SDA = 0;
  i2c_dly();
  SCL = 0;
  i2c_dly();
}

void i2c_stop(void)
{
  SDA = 0;             // i2c stop bit sequence
  i2c_dly();
  SCL = 1;
  i2c_dly();
  SDA = 1;
  i2c_dly();
}

unsigned char i2c_rx(char ack)
{
char x, d=0;
  SDA = 1; 
  for(x=0; x<8; x++) {
    d <<= 1;
    do {
      SCL = 1;
    }
    while(SCL_IN==0);    // wait for any SCL clock stretching
    i2c_dly();
    if(SDA_IN) d |= 1;
    SCL = 0;
  } 
  if(ack) SDA = 0;
  else SDA = 1;
  SCL = 1;
  i2c_dly();             // send (N)ACK bit
  SCL = 0;
  SDA = 1;
  return d;
}

bit i2c_tx(unsigned char d)
{
char x;
static bit b;
  for(x=8; x; x--) {
    if(d&0x80) SDA = 1;
    else SDA = 0;
    SCL = 1;
    d <<= 1;
    SCL = 0;
  }
  SDA = 1;
  SCL = 1;
  i2c_dly();
  b = SDA_IN;          // possible ACK bit
  SCL = 0;
  return b;
}


使用示例:

The 4 primitive functions above can easily be put together to form complete I2C transactions. Here's and example to start an SRF08 ranging in cm:

i2c_start();              // send start sequence
i2c_tx(0xE0);             // SRF08 I2C address with R/W bit clear
i2c_tx(0x00);             // SRF08 command register address
i2c_tx(0x51);             // command to start ranging in cm
i2c_stop();               // send stop sequence


Now after waiting 65mS for the ranging to complete (I've left that to you) the following example shows how to read the light sensor value from register 1 and the range result from registers 2 & 3.

i2c_start();              // send start sequence
i2c_tx(0xE0);             // SRF08 I2C address with R/W bit clear
i2c_tx(0x01);             // SRF08 light sensor register address
i2c_start();              // send a restart sequence
i2c_tx(0xE1);             // SRF08 I2C address with R/W bit set
lightsensor = i2c_rx(1);  // get light sensor and send acknowledge. Internal register address will increment automatically.
rangehigh = i2c_rx(1);    // get the high byte of the range and send acknowledge.
rangelow = i2c_rx(0);     // get low byte of the range - note we don't acknowledge the last byte.
i2c_stop();               // send stop sequence



8 参考































个人工具
名字空间

变换
操作
导航
工具箱