SPI Modes Explained: CPOL and CPHA With Timing Diagrams
SPI has four modes, numbered 0 to 3, and a master and slave must use the same one. The mode is two bits. CPOL is the level the clock idles at. CPHA says which clock edge the data is sampled on: the first edge after the clock leaves idle (the leading edge), or the second (the trailing edge). Get either wrong and the bytes arrive shifted by a bit, or not at all. This page draws all four, then runs a Verilog master and slave in every mode and in two wrong pairings.
Learning objectives
- Derive the idle level, sampling edge and shifting edge of any SPI mode from CPOL and CPHA
- Read the four modes from a timing diagram
- Write an SPI master in Verilog that supports every mode
- Explain why a wrong mode can still pass in simulation, and what happens on a board
Prerequisites
- Part 1: what the SPI wires do
- Verilog: always blocks and nonblocking assignments; shift registers
The four modes
| Mode | CPOL | CPHA | SCLK idles | Sampled on | Shifted on |
|---|---|---|---|---|---|
| 0 | 0 | 0 | low | rising edge (leading) | falling edge |
| 1 | 0 | 1 | low | falling edge (trailing) | rising edge |
| 2 | 1 | 0 | high | falling edge (leading) | rising edge |
| 3 | 1 | 1 | high | rising edge (trailing) | falling edge |
With CPHA 0 the receiver samples on the leading edge, so the first bit has to be on the line before that edge: the sender puts it out when chip select falls. With CPHA 1 the sender puts each bit out on the leading edge and the receiver samples on the trailing edge, half a clock later. In both cases data changes on one edge and is sampled on the other, so it is stable for half a period around every sample. Mode 0 and mode 3 sample on the rising edge; many parts support both for that reason.
A master for every mode
The master below counts 16 clock edges per byte. Even-numbered edges (0, 2, 4 and so on) are leading edges. With CPHA 0 those are the sampling edges and the odd ones shift; with CPHA 1 it is the other way round. The test in the condition, (edges[0] == 0) == (CPHA == 0), is that rule in one line.
// SPI master for one byte, MSB first, in any of the four modes. CPOL is the clock's idle level. With CPHA = 0, data
// is put out before the first clock edge and sampled on the leading edge; with CPHA = 1, it is put out on the leading
// edge and sampled on the trailing edge. sclk runs at clk / (2 * HALF).
module spi_master #(parameter CPOL = 0, parameter CPHA = 0, parameter HALF = 4) (
input wire clk,
input wire rst,
input wire start,
input wire [7:0] tx,
output reg [7:0] rx,
output reg busy,
output reg done,
output reg sclk,
output reg mosi,
input wire miso,
output reg cs_n
);
reg [7:0] sh_out, sh_in;
reg [4:0] edges; // clock edges made so far, 0 to 16
reg [7:0] div;
always @(posedge clk) begin
done <= 0;
if (rst) begin
sclk <= CPOL; cs_n <= 1; busy <= 0; mosi <= 0; edges <= 0; div <= 0;
end else if (!busy) begin
if (start) begin
busy <= 1; cs_n <= 0; sh_out <= tx; edges <= 0; div <= 0;
if (CPHA == 0) mosi <= tx[7]; // first bit ready before the first edge
end
end else if (div != HALF - 1) begin
div <= div + 1;
end else begin
div <= 0;
if (edges == 16) begin // a half period after the last edge: deselect
cs_n <= 1; busy <= 0; done <= 1; rx <= sh_in;
end else begin
sclk <= ~sclk;
edges <= edges + 1;
if ((edges[0] == 0) == (CPHA == 0)) begin
sh_in <= {sh_in[6:0], miso}; // sampling edge
end else begin // shifting edge
if (CPHA == 0) begin sh_out <= {sh_out[6:0], 1'b0}; mosi <= sh_out[6]; end
else begin mosi <= sh_out[7]; sh_out <= {sh_out[6:0], 1'b0}; end
end
end
end
end
endmodule
The slave reacts to SCLK itself, as a real slave does. It knows its own CPOL, so it can tell a leading edge (sclk moving away from CPOL) from a trailing one.
// SPI slave model for one byte in any mode. It reacts to sclk itself, as a real slave does: the leading edge is the
// first edge away from the idle level CPOL. LSBF = 1 receives least significant bit first. ZOUT = 1 releases MISO
// (high impedance) while the slave is not selected, so several slaves can share one MISO line.
module spi_slave #(parameter CPOL = 0, parameter CPHA = 0, parameter LSBF = 0, parameter ZOUT = 0) (
input wire sclk,
input wire cs_n,
input wire mosi,
output wire miso,
input wire [7:0] tx,
output reg [7:0] rx
);
reg [7:0] sh_out;
reg out;
assign miso = (ZOUT && cs_n) ? 1'bz : out;
always @(negedge cs_n) begin
sh_out = tx;
if (CPHA == 0) begin out = sh_out[7]; sh_out = {sh_out[6:0], 1'b0}; end
end
always @(sclk) if (!cs_n) begin
if ((sclk != CPOL) == (CPHA == 0)) rx = LSBF ? {mosi, rx[7:1]} : {rx[6:0], mosi}; // sampling edge
else begin out = sh_out[7]; sh_out = {sh_out[6:0], 1'b0}; end // shifting edge
end
endmodule
Worked example: all four modes, and two wrong ones
The testbench connects a master and a slave in each mode. The master sends 0xA5 while the slave sends 0x3C, so both directions are checked. Then two wrong pairings: a mode 0 master with a mode 1 slave, and with a mode 2 slave. The data lines lag the clock by 2 ns, as they do on a board; without that, a zero-delay simulation can let a slave read a bit on the same edge that changes it.
module spi_tb;
spi_one #(0, 0, 0, 0, 1) m0 ();
spi_one #(0, 1, 0, 1, 1) m1 ();
spi_one #(1, 0, 1, 0, 1) m2 ();
spi_one #(1, 1, 1, 1, 1) m3 ();
spi_one #(0, 0, 0, 1) x01 (); // mismatched: master mode 0, slave mode 1
spi_one #(0, 0, 1, 0) x02 (); // master mode 0, slave mode 2
initial #2000 $finish;
endmodulemaster mode 0 (CPOL 0 CPHA 0), slave mode 0: slave got 0xa5, master got 0x3c
master mode 1 (CPOL 0 CPHA 1), slave mode 1: slave got 0xa5, master got 0x3c
master mode 2 (CPOL 1 CPHA 0), slave mode 2: slave got 0xa5, master got 0x3c
master mode 3 (CPOL 1 CPHA 1), slave mode 3: slave got 0xa5, master got 0x3c
master mode 0 (CPOL 0 CPHA 0), slave mode 1: slave got 0xa5, master got 0xXe: WRONG
master mode 0 (CPOL 0 CPHA 0), slave mode 2: slave got 0xa5, master got 0x3cAll four matched modes move both bytes. The mode 1 slave reads 0xA5 but its first output bit never reaches the master in time, which reads 0xXe: an unknown top bit and the rest shifted. The mode 2 slave got the right bytes, and that is the trap. A mode 2 slave samples on the falling edge, which is exactly where the mode 0 master changes MOSI. It read the old bit only because the wire is 2 ns slow. On a board that is a race against the hold time: it can work on the bench and fail with a longer cable or a warmer chip. Check the mode in the slave's datasheet; a passing simulation does not prove it.
Common mistakes
- Setting CPOL right and CPHA wrong. The bytes arrive shifted by one bit, so 0xA5 reads as 0x4A or 0xD2.
- Trusting a zero-delay simulation of a mismatched mode. Add a small wire delay, or check the mode against the datasheet.
- Changing the mode while chip select is low. Change it only between transfers, with the clock at its new idle level before CS falls.
- Sending least significant bit first to a part that expects most significant bit first. Exercise 3 shows the result.
- Leaving MISO driven by a deselected slave. Two outputs fight on one wire (exercise 4).
Quick check
1. A part's datasheet says data is sampled on the rising edge and the clock idles low. Which mode?
Mode 0: CPOL 0 (idles low), and the rising edge is the leading edge, so CPHA 0.
2. In mode 3, when does the master put out the first bit?
On the first (falling) edge of SCLK. With CPHA 1, each bit goes out on the leading edge and is sampled on the trailing, rising edge.
3. Why can SPI send and receive at the same time?
MOSI and MISO are separate wires, and both shift on the same clock: every clock moves one bit each way.
Exercises
E1 (easy). Pair every master mode with every slave mode. Which pairs work in the simulation, and which would you trust on a board?
Six pairs pass in the simulation: the four matched modes, and the two pairs that differ only in CPOL with CPHA 0 (master 0 with slave 2, master 2 with slave 0). The extra two pass only because of the 2 ns wire delay: the slave samples on the edge where the master changes the bit. Only the four matched modes are safe on a board.
// E1: every pairing of master mode and slave mode.
genvar mm, sm;
generate for (mm = 0; mm < 4; mm++) for (sm = 0; sm < 4; sm++) begin : g
pair #(mm / 2, mm % 2, sm / 2, sm % 2) p ();
initial begin #900; if (p.s_rx == 8'hA5 && p.m_rx == 8'h3C) $display("E1: master mode %0d, slave mode %0d: works", mm, sm); end
end endgenerateE1: master mode 0, slave mode 0: works
E1: master mode 0, slave mode 2: works
E1: master mode 1, slave mode 1: works
E1: master mode 2, slave mode 0: works
E1: master mode 2, slave mode 2: works
E1: master mode 3, slave mode 3: worksE2 (easy). With a 100 MHz system clock and HALF = 4, what is the SCLK frequency, and how long is chip select low for one byte?
Each half period is 4 system clocks, 40 ns, so SCLK runs at 80 ns, 12.5 MHz. Sixteen edges take 640 ns; the master waits one more half period before it releases CS, so CS is low for 680 ns.
// E2: with a 100 MHz clock and HALF = 4, how fast is sclk and how long is the slave selected for one byte?
pair p ();
initial begin @(negedge p.cs_n); @(posedge p.cs_n); #1 $display("E2: sclk period %0d ns, %0.1f MHz; one byte keeps CS low for %0.0f ns", 2 * 4 * 10, 1000.0 / 80, $realtime - 1 - p.t0); endE2: sclk period 80 ns, 12.5 MHz; one byte keeps CS low for 680 nsE3 (medium). The master sends 0x12 most significant bit first, but the slave was built to expect least significant bit first. What does it receive?
The bit order is reversed: 0x12 is 0001 0010, and reversed it is 0100 1000, 0x48. The mode is correct, so every bit arrives; they just land in the wrong places.
if ((sclk != CPOL) == (CPHA == 0)) rx = LSBF ? {mosi, rx[7:1]} : {rx[6:0], mosi}; // sampling edgeE3: master sent 0x12 MSB first, LSB-first slave got 0x48E4 (hard). Two slaves share MISO. Only slave B is selected. Why does the master read garbage, and how does the fix work?
Slave A keeps driving MISO although it is not selected, so A and B drive the same wire with different values and the simulator resolves the conflict to x. With ZOUT = 1, a deselected slave puts MISO in high impedance (z), and B is the only driver. Real SPI parts do this: MISO is three-state while CS is high.
assign miso = (ZOUT && cs_n) ? 1'bz : out;
// E4: two slaves share MISO. The master selects slave B only; slave A must let go of MISO.
reg clk = 0, rst = 1, start = 0;
always #5 clk = ~clk;
wire sclk, mosi, miso, cs_b, busy, done;
wire [7:0] m_rx, a_rx, b_rx;
spi_master m (.clk(clk), .rst(rst), .start(start), .tx(8'hA5), .rx(m_rx), .busy(busy), .done(done), .sclk(sclk), .mosi(mosi), .miso(miso), .cs_n(cs_b));
spi_slave #(.ZOUT(`ZOUT)) a (.sclk(sclk), .cs_n(1'b1), .mosi(mosi), .miso(miso), .tx(8'h81), .rx(a_rx)); // not selected
spi_slave #(.ZOUT(`ZOUT)) b (.sclk(sclk), .cs_n(cs_b), .mosi(mosi), .miso(miso), .tx(8'h3C), .rx(b_rx));
initial begin
#20 rst = 0; @(negedge clk) start = 1; @(negedge clk) start = 0; @(posedge done); #1;
$display("E4: ZOUT = %0d: master read 0x%02h from slave B (B sent 0x3C)", `ZOUT, m_rx);
endE4: ZOUT = 0: master read 0xxx from slave B (B sent 0x3C)
E4: ZOUT = 1: master read 0x3c from slave B (B sent 0x3C)Interview questions
- Q1. What do CPOL and CPHA mean, and how many modes do they give? Answer: CPOL is the clock's idle level; CPHA chooses whether data is sampled on the leading or the trailing edge. Two bits, four modes.
- Q2. A slave reads every byte shifted by one bit. What is the likely cause? Answer: the wrong CPHA. The receiver samples on the edge where the data changes, so it takes each bit half a clock early or late.
- Q3. How would you connect five SPI slaves to one master with few pins? Answer: shared SCLK, MOSI and MISO with a chip select each, a decoder to make the selects from fewer pins, or a daisy chain where the parts support it.
Summary
- Mode = CPOL * 2 + CPHA. CPOL is the idle level; CPHA picks the leading or the trailing edge for sampling.
- Data changes on one edge and is sampled on the other.
- With CPHA 0 the first bit must be on the line before the first edge.
- A mismatched mode can pass a simulation on a hold-time race. Match the datasheet.
Related
Sources
Last verified
Run with Icarus Verilog 12.0 and linted with Verilator 5.036 on 27 November 2026. Every program on this page is in scripts/embedded and run_all.sh reruns them all; the outputs shown are copied from scripts/embedded/results.