Sub topic 5
Clock polarity and phase: the four SPI modes
This chapter assumes you know the four SPI signals (SCLK, MOSI, MISO, chip select) and that a transfer shifts bits out and in at the same time. It also assumes you can read a simple timing diagram. Here you will learn how two settings, clock polarity and clock phase, define when data changes and when it is read. You will also learn how to pick the right one from a datasheet.
Why SPI needs a mode
SPI has no start bit, no address and no agreed baud rate. The only thing that tells the receiver when to read a bit is an edge on SCLK. Two questions remain open, and both sides must answer them the same way:
- What level does SCLK rest at when nothing is happening?
- On which edge of the clock is a bit read, and on which edge is the next bit put on the line?
Clock polarity (CPOL) answers the first question. Clock phase (CPHA) answers the second. Each is one bit, so there are four combinations. These are the four SPI modes.
If the controller and the peripheral disagree, the bus still toggles and the peripheral still answers. The data is simply wrong, often shifted by one bit or inverted in some pattern. That makes a wrong mode one of the most common SPI faults.
Clock polarity (CPOL)
CPOL describes the idle state of SCLK, meaning its level while chip select is high or between bits.
- CPOL = 0: SCLK idles low. The first edge of a transfer is a rising edge.
- CPOL = 1: SCLK idles high. The first edge of a transfer is a falling edge.
CPOL changes nothing about how fast data moves. It only flips which edge is "leading" (the first edge of each clock cycle) and which is "trailing" (the second).
Clock phase (CPHA)
CPHA says whether data is read on the leading or the trailing edge.
- CPHA = 0: data is read (sampled) on the leading edge. It is changed (shifted) on the trailing edge.
- CPHA = 1: data is changed on the leading edge. It is read on the trailing edge.
With CPHA = 0, the first bit must already be on the line before the first clock edge arrives. The sender therefore places bit 7 on its output as soon as chip select goes low. With CPHA = 1, the first edge is used to put the first bit on the line, and the read happens half a clock period later.
The four modes
| Mode | CPOL | CPHA | Idle SCLK | Data read 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 |
Modes 0 and 3 both read on the rising edge. Modes 1 and 2 both read on the falling edge. The difference inside each pair is the idle level, and with it the timing of the first bit.
Notice that the "first edge" in the diagram is a rising edge for CPOL 0 and a falling edge for CPOL 1. That is why mode 2 reads on the falling edge even though CPHA is 0, just as in mode 0.
Seeing the mode in code
The bit-banged routine below shows mode 0 and mode 3 side by side. In mode 0 the clock idles low and is raised to read. In mode 3 the clock idles high, and the sampling happens on the rising edge at the end of each cycle. The peripheral changes its output on the preceding falling edge.
const uint8_t SCK_PIN = 13;
const uint8_t MOSI_PIN = 11;
const uint8_t MISO_PIN = 12;
// Mode 3 (CPOL=1, CPHA=1), most significant bit first.
// SCK idles high. Data changes on the falling edge and is read on the rising edge.
uint8_t softTransferMode3(uint8_t out) {
uint8_t in = 0;
for (int8_t bit = 7; bit >= 0; bit--) {
digitalWrite(SCK_PIN, LOW); // leading edge: shift the next bit out
digitalWrite(MOSI_PIN, (out >> bit) & 1);
digitalWrite(SCK_PIN, HIGH); // trailing edge: both sides read
in = (in << 1) | (digitalRead(MISO_PIN) ? 1 : 0);
}
return in;
}
void setup() {
pinMode(SCK_PIN, OUTPUT);
pinMode(MOSI_PIN, OUTPUT);
pinMode(MISO_PIN, INPUT);
digitalWrite(SCK_PIN, HIGH); // idle high for CPOL = 1
}
void loop() {}Compare this with the mode 0 routine from the earlier chapters. The data and read steps are the same. The idle level and the order of "set data" and "read" relative to the first edge have changed. Real hardware does this for you, so you only choose the mode.
Choosing the mode from a datasheet
Datasheets rarely say "use mode 1". They show a timing diagram, or they state CPOL and CPHA in a table. Follow these steps:
- Find the SCLK waveform in the diagram. Check its level before chip select falls. A low line means CPOL = 0. A high line means CPOL = 1.
- Find where the data lines are stable and where they change. The data must be stable at the read edge, so the read edge is the one the "valid data" window surrounds.
- Decide whether that read edge is the first or second edge of the clock cycle after chip select falls. First means CPHA = 0. Second means CPHA = 1.
- Convert to a mode number with
mode = (CPOL << 1) | CPHA.
Some datasheets list two acceptable modes, for example "mode 0 or mode 3". Such a device only looks at the rising edge and does not care about the idle level. Either works, though one may suit your other peripherals better.
Setting the mode in the Arduino library
The Arduino SPI library takes the mode in SPISettings. The constants are SPI_MODE0 through SPI_MODE3, and they match the table above.
#include <SPI.h>
const uint8_t CS_PIN = 10;
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
digitalWrite(CS_PIN, LOW);
SPI.transfer(0x01); // placeholder command byte
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
}
void loop() {}The mode is applied when beginTransaction runs, so each peripheral on a shared bus can use its own. The old SPI.setDataMode() call still exists on many boards but is deprecated, and it does not protect other users of the bus.
Finding the mode by trial
When the datasheet is unclear, you can try all four modes against a register with a known value, such as a device ID. This sketch reads a "who am I" register in each mode and reports which modes return the expected value. The register address, read flag and expected value below are examples. Replace them with the values from your datasheet.
#include <SPI.h>
const uint8_t CS_PIN = 10;
const uint8_t ID_REG = 0x0F;
const uint8_t READ_FLAG = 0x80;
const uint8_t EXPECTED_ID = 0x33;
const uint8_t modes[4] = {SPI_MODE0, SPI_MODE1, SPI_MODE2, SPI_MODE3};
uint8_t readId(uint8_t mode) {
SPI.beginTransaction(SPISettings(500000, MSBFIRST, mode));
digitalWrite(CS_PIN, LOW);
SPI.transfer(ID_REG | READ_FLAG);
uint8_t value = SPI.transfer(0x00);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
return value;
}
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
for (uint8_t i = 0; i < 4; i++) {
uint8_t id = readId(modes[i]);
Serial.print("Mode ");
Serial.print(i);
Serial.print(": 0x");
Serial.print(id, HEX);
Serial.println(id == EXPECTED_ID ? " <-- match" : "");
}
}
void loop() {}Read the output with care. A peripheral may return the right ID in two modes, because the pair of modes that share a read edge often work for simple reads. It may also return all zeros or all 0xFF, which points to wiring or chip select trouble rather than the mode. A match for one read does not prove the mode is right for writes, so confirm with a write followed by a read-back.
Typical symptoms of a wrong mode
- Every byte is shifted left or right by one bit, for example
0x66instead of0x33. This often means the read edge is off by half a cycle. - Values are plausible but noisy. This happens when the read edge falls near the moment the data changes.
- Reads work at a low clock rate and fail at a high one. The device may be reading on the correct edge, but with a mode mismatch the setup margin is only half a clock period instead of a whole one.
- The result is all
0x00or all0xFF. Suspect chip select, power or wiring first, then the mode.
Exercise
A datasheet timing diagram shows SCLK high while chip select is high. After chip select falls, the first SCLK edge is falling, and the device reads MOSI on the next, rising edge. Which Arduino constant do you pass in SPISettings?
Show answer
The clock idles high, so CPOL = 1. The device reads on the second edge of the cycle, so CPHA = 1. The mode number is (1 << 1) | 1 = 3, so use SPI_MODE3.
Exercise
A peripheral works in SPIMODE0 and in SPIMODE3 but fails in SPIMODE1 and SPIMODE2. What does this say about when the device reads its input, and why do both working modes behave the same?
Show answer
The device reads on the rising edge of SCLK. Modes 0 and 3 both read on the rising edge and differ only in the idle level of the clock. A device that ignores the idle level and looks only at rising edges cannot tell them apart. Modes 1 and 2 read on the falling edge, so the data arrives half a cycle out of step.