</>EmbeddedInterview

What are the SPI modes (CPOL and CPHA)?

MediumCommunication Protocols

Short answer: CPOL (clock polarity) sets the idle level of SCLK: 0 = idle low, 1 = idle high. CPHA (clock phase) sets which edge samples data: 0 = sample on the first (leading) edge, 1 = sample on the second (trailing) edge. The four combinations are SPI modes 0–3, and the controller must use the mode the device's datasheet specifies.

Mode CPOL CPHA Idle clock Data sampled on Data changes on
0 0 0 low rising edge falling edge
1 0 1 low falling edge rising edge
2 1 0 high falling edge rising edge
3 1 1 high rising edge falling edge

Most devices use mode 0 or 3 (many support both, since in both cases data is sampled on the rising edge).

Symptoms of the wrong mode: data shifted by one bit (values doubled or halved), the first or last bit wrong, or garbage that changes with clock speed.

With CPHA = 0, the first data bit must be on the line before the first clock edge, so the device outputs it as soon as chip select goes active; some controllers need a delay between CS assertion and the first clock.

Other SPI settings to match: bit order (MSB first is most common), word size (8/16 bits), chip-select polarity (usually active low), maximum clock frequency, and the timing between CS and clock (setup/hold times in the datasheet).

On Linux: spi-cpol and spi-cpha properties in the device tree, or SPI_MODE_0..3 with spidev.