
Modes
Mode 0 is the default and most chips accept it, so you rarely choose a mode. You meet the four SPI modes when a datasheet asks for another, or when a chip answers but its bytes arrive shifted. Check the mode first.
Chips wired correctly, a clock running, the right bytes on MOSI, and the answer is still wrong. Each bit sits on the wire for a whole clock cycle, but only one instant of it should be read, and nothing on the bus says which. So each end has two switches.
The two switches
CPOL is the clock polarity, the level SCK rests at when nothing is happening: 0 for low, 1 for high. CPHA is the clock phase. With CPHA 0 the data is read on the first edge of each cycle, and with CPHA 1 on the second. The sender changes its bit on the other edge, so the bit is steady when it is read. The names are Motorola's.
Two bits make four modes, numbered CPOL first, and Arduino's SPI_MODE0 to SPI_MODE3 use the same numbers.
| Mode | CPOL | CPHA | SCK rests | Read on |
|---|---|---|---|---|
| 0 | 0 | 0 | low | rising edge |
| 1 | 0 | 1 | low | falling edge |
| 2 | 1 | 0 | high | falling edge |
| 3 | 1 | 1 | high | rising edge |
Usually not your call
Mode 0 is what Arduino's SPISettings() starts with. The BME280 and the W25Q128JV accept mode 0 and mode 3, and the library for a peripheral often chooses its own settings. So most of the time nobody sets a mode.
The peripheral decides, and its datasheet says which. Motorola never made SPI a standard, so that datasheet is the only authority.
When they disagree
Each end obeys its own mode, so nothing reports a fault. A controller that reads a moment late gets every bit one place off: the byte comes back shifted down one place with a zero in front. Between other pairs, one end reads on the very edge the other changes its bit. That is a race, and which value wins depends on the parts.
Common mistakes
- Not checking the mode when bytes arrive shifted or scrambled. A chip that answers but gets the bits wrong is the mark of a mode fault.
- Trusting a pair that seems to work. A race can pass a test and fail at another clock speed.
- Assuming one mode fits the whole bus. The mode belongs to the peripheral, so two devices may need different modes, set before each chip select.
Edit this page — content/fundamentals/spi/modes.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.