串行外围设备接口。
SPI为De-facto standard,即没有特定的组织来管理定义其规范,这也导致其有很多不同的版本。
对于Basic SPI,Freescale发布了一个标准版本: SPI Block Guide V04.01
三个版本,Basic SPI、Dual SPI(DSPI)、Quad SPI(QSPI)。
DSPI和QSPI和Basic SPI的差异为数据线为双向,但其是半双工,即同一时间只能单向传输数据,不能像千兆网口一样,同时双向传输数据。
Basic SPI的管脚定义如下表:
Name | Function |
nCS | Chip select, a.k.a. slave select (nSS). This is driven low (hence the n) by the master to select a slave. There is a separate chip select line going from the master to each slave. |
SCLK | Clock. Driven by the master, this provides the clock signal to the slaves to clock data in and out with. The exact polarity and edges that are used depend on the values of CPOL and CPHA. |
MOSI | Master out, slave in. A.k.a. main out, subnode in. The master drives the line and provides data to the slaves. Only the slave with nCS asserted (low) listens to the data. |
MISO | Master in, slave out. A.k.a. main in, subnode out. The selected slave can drive this line to send data to the master. |
上图为四线的Basic SPI,另有几种变体:
Basic SPI有四种模式,按空闲时时钟电平,以及采样的时钟边缘分。
空闲时时钟电平为0对应CPOL = 0,1对应CPOL = 1。
第一个时钟边缘采样对应CPHA = 0,第二个则对应CPHA = 1。
不是每个芯片都支持这四种模式,一般均支持0和3,Master和Slave设备需要设定成同一个模式。
另Master设备可以在一个总线上和不同模式的slave设备通讯,只需要保证通讯时其他slave设备是inactive状态。
列表和示意图如下:
SPI Mode | Clock Polarity (CPOL) | Clock Phase (CPHA) | Which Clock Edge Is Used To Sample/Shift? |
0 | 0 | 0 | Data sampled on rising edge and shifted out on falling edge. |
1 | 0 | 1 | Data sampled on falling edge and shifted out on rising edge. |
2 | 1 | 0 | Data sampled on falling edge and shifted out on rising edge. |
3 | 1 | 1 | Data sampled on rising edge and shifted out on falling edge. |
两种拓扑,分别如下。
即菊花链模式,Master设备只有一个片选信号连接多个Slave设备,而Slave的MISO作为下一个Slave设备的MOSI,数据在多个Slave设备之间传递。如下图所示,注意这个不是所有的Slave都支持此功能。