1. Introduction to SPI bus
The SPI bus is a high-speed, full-duplex, synchronous communication bus developed by Motorola, which usually consists of four lines:
- CS chip select signal
- SCLK clock signal
- MOSI master output slave input interface
- MISO master input slave output interface
SPI is divided into master and slave, and the master provides the clock signal required for communication.
2. Four working modes of SPI
The two most important parameters of SPI are clock polarity (CPOL) and phase (CPHA).
- CPOL : Clock Polarity determines whether the idle state level of the clock is high or low
CPOL = 0: the clock is low when the clock is idle, and is valid when the clock is high
CPOL = 1: the clock is high when the clock is idle, and is valid when the clock is low - CPHA : Clock Phase determines the data transmission sampling and shifting method
CPHA = 0: sampling on the first transition edge of the clock signal SCK
CPHA = 1: sampling on the second transition edge of the clock signal SCK
Different combinations of clock and phase form four working modes of SPI:
model | CPOL | WASH |
---|---|---|
Mode 0 | 0 | 0 |
Mode 1 | 0 | 1 |
Mode 2 | 1 | 0 |
Mode 2 | 1 | 1 |
Let's take the spi bus of stm32 as an example:
Mode 0: when CPOL is 0 and CPHA is 0, data is sampled on the first transition edge (rising edge) of the clock; Mode 1: when CPOL is 0 and CPHA is 1, data is sampled on the first transition edge (falling edge); Mode 1: when CPOL is 1 and CPHA is 0, data is sampled on the first transition edge (falling edge); Mode 1: when CPOL is 1 and CPHA is 1, data is sampled on the first transition edge (
rising edge );
model | phase polarity | sampling | SCL idle level |
---|---|---|---|
Mode 0 | CPOL = 0, CPHA = 0 | rising edge | low level |
Mode 1 | CPOL = 0, CPHA = 1 | falling edge | low level |
Mode 2 | CPOL = 1, CPHA = 0 | falling edge | high level |
Mode 2 | CPOL = 1, CPHA = 1 | rising edge | high level |
Whether the SPI slave device SCL is high or low when idle determines whether CPOL is 0 or 1.
3. SPI full duplex and half duplex
When using mcu such as stm32, SPI usually has full-duplex and half-duplex options. The so-called full-duplex means that SPI can also receive data while sending data. The so-called half-duplex means that SPI sends and receives time-sharing, that is, it cannot receive data when sending, and cannot send data when receiving data.
4. Application circuit
One to one:
One-to-many: