Module 10: Capstone Projects · Project 3 — SPI Master Controller

Project 3, Part 1: SPI and Its Four Modes

The UART protocol avoids a shared clock wire and relies on midpoint oversampling. In contrast, the Serial Peripheral Interface (SPI) provides a dedicated serial clock (SCLK). Sharing a synchronous clock eliminates timing drift and enables higher data transmission rates. This article defines the four standard SPI operational modes determined by clock polarity and phase.

Advantages of a Shared Synchronous Clock

A master-driven serial clock line eliminates clock recovery circuitry in slave devices. The master provides explicit sampling edges for data transmission on Master-Out Slave-In (MOSI) and Master-In Slave-Out (MISO) lines. Because data shifts on clock transitions, communication tolerances do not depend on matched oscillator frequencies.

Clock Polarity (CPOL) and Clock Phase (CPHA)

The SPI standard specifies two independent configuration parameters:

  • CPOL (Clock Polarity): Specifies the idle state of SCLK when no data transfer occurs. CPOL = 0 sets the idle line state to logic 0. CPOL = 1 sets the idle line state to logic 1.
  • CPHA (Clock Phase): Specifies the clock transition used to sample serial data. CPHA = 0 samples data on the leading (first) transition of each clock pulse. CPHA = 1 samples data on the trailing (second) transition of each clock pulse.

Combining these two parameters establishes the four standard SPI modes:

Mode CPOL CPHA Clock Idle State Data Sample Edge Data Shift Edge
0 0 0 Low (0) Rising (Leading) Falling (Trailing)
1 0 1 Low (0) Falling (Trailing) Rising (Leading)
2 1 0 High (1) Falling (Leading) Rising (Trailing)
3 1 1 High (1) Rising (Trailing) Falling (Leading)

SCLK for all four modes with the sampling edge marked, and MOSI below
Figure 1: The four SPI modes

In Mode 0 and Mode 2 (CPHA = 0), the transmitter places valid data on the bus before the first clock edge. In Mode 1 and Mode 3 (CPHA = 1), the transmitter shifts data on the first clock edge and samples on the second.

Mode Configuration Hazards

A master and slave must configure matching modes to communicate properly. If a master transmits in Mode 0 while a slave samples in Mode 1, data will be captured during unstable transitions. This mismatch causes corrupted bytes and communication timeouts. This capstone project implements a master controller that supports runtime configuration across all four modes.

Practice Exercise

A sensor datasheet states: “SCLK idles high. The master must sample incoming serial data on the falling edge of SCLK.” Determine the corresponding SPI mode.

Solution

Analyze the configuration requirements:

  1. SCLK idles high: CPOL = 1.
  2. The falling edge is the first (leading) edge moving away from the idle-high level.
  3. Sampling on the leading edge corresponds to CPHA = 0.

Cross-referencing the mode table confirms CPOL = 1 and CPHA = 0 defines Mode 2.

SPI uses a synchronous clock line to eliminate asynchronous timing drift. The protocol supports four operational modes defined by clock polarity (CPOL) and clock phase (CPHA). The next article designs the shift register datapath and chip-select control logic.

← Project 2, Part 4: The Loopback Test Project 3, Part 2: The Shift Register and Chip Select →