打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SPI 与 I2C 协议的差异和需要考虑的事项

当涉及到印刷电路板上的芯片之间的通信时,两种协议非常流行:串行外设接口(SPI)和内部集成电路(IIC或I2C)。这些有线协议被认为是“小”或“低端”,因为它们没有其他协议(如USB,以太网,SATA等)吹嘘的速度,鲁棒性和距离。
然而,SPI和I2C非常受欢迎,因为它们易于实现,只需要很少的组件和代码,而不是它们的重量级表亲。两者都依靠串行通信来传递数据,并在一条总线上支持多个设备。许多微控制器、传感器和外设(如LCD)依靠SPI和I2C相互通信。
如果您正在设计产品,需要在SPI和I2C之间进行选择,您会选择哪一个?

SPI

SPI 由摩托罗拉于 1980 年代开发,作为其早期微控制器和板载外设(如 EEPROM)之间通信的一种方式。摩托罗拉或任何其他组织都维护SPI协议的任何标准化。由于这种“事实上的”标准,实现可能因制造商而异。请务必仔细阅读每个支持SPI的器件的数据手册!
SPI使用四条有源信号线(不包括电源线和接地线)在器件之间进行通信。这些行包括:
  • SCLK:串行时钟(由主站控制)
  • MOSI:主出从入(由主控)
  • 味噌:主入从出(由从设备控制)
  • SS:从属选择(由主控制)
通过这四条线路,控制设备(主设备)可以与另一个外围设备(从设备)通信。
SPI 点对点连接
虽然SPI总线上可能只有一个主设备,但可以添加任意数量的外设。但是,对于添加的每个外设,必须添加额外的 SS 行。在示例图中,我们必须使用三条独立的SS线,每条线控制一个单独的外设。
用于 3 个从设备的 SPI 连接
当主设备希望向外设发送数据或从外设接收日期时,它通过将相应的SS线拉低来开始通信。同时,它激活时钟线(在给定频率下切换SCLK高低)。主器件在MOSI线路上发送数据,同时对MISO线路进行采样。因此,可以在主设备和外围设备之间同时发送数据(全双工)。
请注意,一次只能有一个外围设备与主设备通信。
模式 0 SPI 的时序图示例:MOSI 和 MISO 线路在上升的 SCLK 边沿上采样
SPI有四种不同的模式可以设置,这些模式决定了时钟的工作方式。主设备和外围设备必须使用相同的模式。模式 0 是迄今为止设备中最常见的模式。
  • Mode 0: data sampled on rising clock edge, clock idles low
  • Mode 1: data sampled on falling clock edge, clock idles low
  • Mode 2: data sampled on falling clock edge, clock idles high
  • Mode 3: data sampled on rising clock edge, clock idles high
SPI does not specify any particular voltage levels, maximum speed rates, or addressing schemes. As a result, it is up to you to decide these factors. SPI speeds can easily exceed 10 Mbps, so make sure you read the datasheets for all your parts, as that will determine the acceptable voltages, speed limits, and supported modes.
Because of these speeds, SPI is useful for transferring large amounts of data. SPI is often found on sensors that require fast update rates, like accelerometers, display devices, LCDs, and flash memory devices.

I2C

Philips Semiconductors (now known as NXP Semiconductors) created the I2C specification in 1982 to help standardize communication between chips on the same board. NXP does not charge anyone to use or implement I2C, but they do charge a fee if you would like to register a device address.
I2C uses 2 lines (not including power and ground) for communication:
  • SDA: Serial Data
  • SCL: Serial Clock
Any number of master devices and any number of slave devices can theoretically be attached to the same bus. Both SDA and SCL lines are required to be open-drain lines. As a result, devices can only pull each line low. A pull-up resistor is required on each line to pull the line back up to high.
I2C connections for multiple master and multiple slave devices
Because of the open-drain design, I2C supports multiple masters on the same bus. If two devices start transmitting at the same time, one of them will eventually back off in a process known as "arbitration." Devices monitor the SDA line while they communicate. If a device sees that the SDA line is low when it is trying to transmit a logic high, it knows that another device is trying to communicate, and it will stop transmitting.
To begin communication, a master device will issue a START condition, where the SDA line is pulled low while the SCL line is still high. The master then sends out the 7-bit address of the intended recipient on the bus, followed by a write bit (0) or read bit (1). If a device on the bus has that particular address, it will respond by pulling the SDA line low (ACK bit).
Data can then be sent by the master or peripheral device in packets of 1 byte at a time; each byte should be acknowledged by the recipient with an ACK bit. Once communication is complete, the master will issue a STOP condition by releasing the SDA line (which will be pulled high) while SCL is high.
Data rate was originally limited to 100 kbps (standard mode). In 1992, Philips raised the speed cap to 400 kbps (fast mode). A special 3.4 Mbps mode (high-speed mode) was added 6 years later. A special set of commands must be given at lower speeds between master and peripheral to set up a high-speed connection.
While any number of devices can be physically attached to an I2C bus, the 7-bit address limits the actual number of devices. Some of the addresses are reserved, and therefore, only 112 different devices can be present on the same bus. A special 10-bit addressing mode can be enabled to allow for more devices, if necessary.
I2C has a form of flow control known as "clock stretching." A peripheral device can hold the SCL line low, which tells the master device to slow the transmission rate. This technique allows the peripheral some time to process data before responding.
Due to the low pin count required by I2C, many sensor manufacturers use this protocol in their chips. For example, temperature sensors, accelerometers, analog-to-digital converters, etc. can be found with I2C.

Comparison

SPI
I2C
Pin drive
Push-pull
Open drain
Signal lines
4(每增加一个外设加 1)
2个
最大速度
无限制(10-100 Mbps 很常见)
快速模式下为 400 kbps(高速模式下可能达到 3.4 Mbps)
外围设备数量
仅受主机上 SS 线路可用的引脚数量限制
112 7 位寻址
多主机
是的
流量控制
是的

结论

这两种协议都适用于许多不同的应用。大多数情况下,您只能使用特定部件的制造商实施的协议。有一些,如Analog Devices ADXL345加速度计,在同一个芯片上同时提供I2C和SPI接口。
如果您必须在两人间做选择,如果您需要更快的传输速度,SPI通常是更好的工具。另外一方面,如果您的微控制器或微控制器处理器上的可用引脚有限公司,那么I2C是最佳选择。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Introduction to I2C and SPI protocols
I2C和SPI总线对比
【硬件】硬件基础小知识 之 SPI总线 (简洁要点)
一直蒙圈的SPI四种模式,你知道吗?
Understanding LVDS for Digital Test Systems -...
Direct memory access
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服