Home / Learn Embedded C / SPI Clock Modes: CPOL and CPHA
SPI Clock Modes: CPOL and CPHA
The four configuration combinations that determine exactly when data is sampled relative to the clock edge.
Breaking it down
What it is
SPI clock polarity (CPOL) sets the clock's idle level (high or low when not transferring), and clock phase (CPHA) sets whether data is sampled on the first or second clock edge of each cycle — together these define 4 possible "SPI modes" (0-3).
Why it matters
Master and slave must be configured to the same SPI mode, or data will be sampled at the wrong moments relative to the clock, producing garbled results — a very common first-bring-up SPI bug.
Mechanically speaking
A device's datasheet specifies exactly which SPI mode(s) it supports; matching the master's mode configuration to that spec is a required step before any SPI communication will work correctly.
Shows up in
- SPI device bring-up
- Datasheet-driven configuration
- Debugging unresponsive SPI devices
Try it
Looking this up correctly from the datasheet before configuring the master is exactly what avoids the "doesn't respond" bring-up trap mentioned above.
| Mode | CPOL | CPHA |
|---|---|---|
| 0 | 0 | 0 (most common default) |
| 1 | 0 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
Try it: detecting a mode mismatch between master and slave
This is exactly the kind of mismatch that makes a device "not respond" on first bring-up, even though every wire is connected correctly.
Check yourself
1. What do CPOL and CPHA together determine?
CPOL sets the clock's idle level and CPHA sets which clock edge data is sampled on — together they define one of 4 SPI modes that both ends must match.