SPI Series · Part 3 of 5
How SPI Really Works: Clock Polarity, Phase & the Four SPI Modes
SPI looks simple on paper — four wires, one clock, done. But most SPI headaches come from one thing: mismatched clock settings between master and slave. This article explains CPOL, CPHA, and the four SPI modes so you never have to guess again.
🔁 How a Transfer Actually Happens
SPI is a shift register protocol at its core. The master and slave each have an internal shift register. On every clock pulse, one bit shifts out of the master into the slave on MOSI, and simultaneously one bit shifts out of the slave into the master on MISO. After 8 clock pulses, a full byte has been exchanged in both directions at once.
CPOL — Clock Polarity
CPOL = 0 → clock idles LOW, active edge rising
CPOL = 1 → clock idles HIGH, active edge falling
CPHA — Clock Phase
CPHA = 0 → data sampled on 1st (leading) edge
CPHA = 1 → data sampled on 2nd (trailing) edge
🎛️ The Four SPI Modes
| Mode | CPOL | CPHA | Clock Idle | Data Sampled 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 |
⚠️ Watch out: Mode 0 is the most common default, but you must match whatever mode the slave device's datasheet specifies. A mismatch usually produces garbled or shifted-by-one-bit data rather than a clean failure — which is exactly why this trips people up.
🎯 Why CS (Chip Select) Matters So Much
CS isn't just an "enable" pin. Pulling CS low tells a slave device "the next clock pulses are for you," and it's also what resets the slave's internal bit counter so both sides agree where a byte begins. Forgetting to properly toggle CS between transfers is one of the most common sources of SPI bugs, second only to using the wrong mode.
🚀 Clock Speed Considerations
SPI has no formally defined maximum speed — it's limited by the capabilities of the specific master and slave chips, plus the quality of your wiring. Long wires or breadboard jumpers add capacitance and can cause signal integrity issues at high clock speeds, so if you're seeing intermittent errors, try lowering the SPI clock frequency before assuming it's a software bug.
Up Next in This Series
Part 4: Hands-on SPI Programming with Arduino — full code for reading sensors and writing to displays.
Comments
Post a Comment