Post

SPI modes

Let’s break down SPI modes slowly and cleanly from physical signals, timing, interpretation, real MCU usage.

SPI modes

Let’s break down SPI modes slowly and cleanly — from physical signals → timing → interpretation → real MCU usage.


What SPI “Mode” Actually Means

SPI has two timing parameters that define when data is sampled and what level the clock idles at.

Those two parameters are:

  1. CPOL → Clock Polarity
  2. CPHA → Clock Phase

Together, they define the SPI Mode.


CPOL — Clock Polarity

  • Decides the idle state of the clock (SCLK).
  • It doesn’t affect data, only the base level of the clock when nothing’s happening.
CPOLClock idle stateActive edge direction
0Idle Low (starts from 0)Active edge = rising edge
1Idle High (starts from 1)Active edge = falling edge

CPHA — Clock Phase

  • Decides when the data is sampled (read) and when it is shifted (written).
  • In simple words: “Should I read data on the first clock edge or the second clock edge?”
CPHASampling edgeShifting edge
01st edge2nd edge
12nd edge1st edge

Combine CPOL and CPHA → 4 SPI Modes

ModeCPOLCPHAClock IdleData Sample EdgeDescription
000LowRising edgeData valid on rising edge, change on falling edge
101LowFalling edgeData valid on falling edge, change on rising edge
210HighFalling edgeData valid on falling edge, change on rising edge
311HighRising edgeData valid on rising edge, change on falling edge

Visualization (conceptual)

Let’s imagine one bit transfer (Master → Slave):

Mode 0 (CPOL=0, CPHA=0)

1
2
3
Clock:  _-_-_-_-_
Data :  D7       D6       D5 ...
Sample ↑

Mode 3 (CPOL=1, CPHA=1)

1
2
3
Clock:  -_-_-_-_-
Data :      D7       D6 ...
Sample ↑
  • When CPOL=0, clock idles low → active high pulses.
  • When CPOL=1, clock idles high → active low pulses.
  • CPHA decides whether the first edge is “sample” or “setup”.

Practical Tip

Different SPI slaves require different modes — for example:

DeviceTypical SPI Mode
Flash memory (W25Qxx)Mode 0 or 3
Accelerometer (ADXL345)Mode 3
MCP3008 ADCMode 0
DAC MCP4921Mode 0

Always check the slave datasheet timing diagram to match CPOL/CPHA.

If you mismatch it:

  • Data gets shifted by half a clock — causing garbage reads.
  • Typical symptom: every byte is wrong, but consistently wrong.

Example on RA6E1 (or STM32-style pseudocode)

1
2
3
4
5
6
7
8
9
spi_cfg_t cfg = {
    .channel        = 0,
    .clk_phase      = SPI_CLK_PHASE_EDGE_ODD,   // CPHA = 1
    .clk_polarity   = SPI_CLK_POLARITY_HIGH,     // CPOL = 1
    .bit_order      = SPI_BIT_ORDER_MSB_FIRST,
    .bitrate        = 1000000,                   // 1 MHz
    .operating_mode = SPI_MODE_MASTER
};
R_SPI_Open(&g_spi_ctrl, &cfg);

Rule of Thumb to Remember

ModeClock idleSample edgeWhen to use
0LowRisingDefault safe mode
1LowFallingIf data valid after 1st edge
2HighFallingRare; check datasheet
3HighRisingFlash memories, sensors

Interview Insight

If interviewer asks:

“What happens if CPOL or CPHA mismatch between master and slave?”

You say:

“The master and slave will interpret data bits at different clock edges, causing bit misalignment — effectively corrupting every byte. You’ll see shifted data or fixed wrong values.”

This post is licensed under CC BY 4.0 by the author.