ElectroDesignForgeElectroDesignForge
Signals & InterfacesPCB Design

SPI interface: modes, timing and wiring

Understand CPOL, CPHA, MOSI, MISO, SCLK and CS, then build a reliable SPI bus with timing, logic levels, topologies and debugging guidance.

Written and technically reviewed byElectroDesignForge Engineering Team

View the editorial process

Reference guide

SPI (Serial Peripheral Interface) connects a controller to nearby ICs such as Flash memories, ADCs/DACs, displays, sensors and RF devices. This synchronous serial link is fast and full duplex, but it is not a complete standard: framing and electrical limits come from each datasheet.


Quick reference

SignalDirection from controllerFunctionKey point
SCLK / SCKoutputSerial clockIts idle level is set by CPOL.
MOSIcontroller → peripheralData transmittedAlso called COPI, SDI or DIN.
MISOperipheral → controllerData receivedAlso called CIPO, SDO or DOUT; high impedance when unselected.
CS / SS / nCSoutput, often active-lowSelection and frame boundaryOne independent CS is normally needed per peripheral.
GNDsharedReference and return pathKeep it alongside signals, especially off-board.

MOSI and MISO assume the controller viewpoint. COPI (Controller Out, Peripheral In) and CIPO (Controller In, Peripheral Out) make the roles clear when discussing a link without a schematic.

SPI defines neither addressing, acknowledgement nor a universal maximum rate. The application protocol defines commands, register addresses, dummy bytes and responses. A logic analyser can therefore decode valid bits while a device stays silent because CS or command framing is wrong.

Check before wiring: logic levels, allowed SCLK rate, SPI mode, MSB/LSB order, word length, CS active state and CS timing. 1.8 V, 2.5 V, 3.3 V and 5 V domains are not automatically compatible.


The four modes: CPOL and CPHA

  • CPOL (clock polarity) sets idle SCLK: 0 = low, 1 = high.
  • CPHA (clock phase) sets the sampling edge. With CPHA = 0, the first edge after CS samples the first bit; with CPHA = 1, it changes that bit and the following edge samples it.
ModeCPOLCPHASCLK idleSample edgeData changes on
000LowRisingFalling
101LowFallingRising
210HighFallingRising
311HighRisingFalling

The leading edge is the first transition away from idle and the trailing edge is the second. CPHA = 0 samples on the leading edge; CPHA = 1 samples on the trailing edge. This works for either CPOL setting.

Reading a timing diagram

  1. Check SCLK before CS: that is CPOL.
  2. Find the edge where stable data is read: that is CPHA.
  3. With CPHA = 0, the first bit must be valid before the first edge.
  4. Confirm output delays in the datasheet; data does not always change exactly on the opposite edge.

Wrong CPHA often shifts bits; wrong CPOL reads a plausible sequence at the wrong instants. 0x00, 0xFF or an invalid ID are possible symptoms, not conclusive diagnoses.


Timing beyond frequency

ParameterMeaningRisk when violated
fSCLKmaximum clock rateIntermittent errors or unstable sampling
tSU(D)MOSI/MISO setup before sample edgeBit read at the wrong level
tH(D)Data hold after sample edgeNext bit corrupts current bit
tCSSactive CS to first SCLK edgeFirst command ignored or shifted
tCSHlast edge to inactive CSLast bit not accepted
tCSHICS inactive time between framesInternal state not reset
tDOMISO output delaySampling too early at high speed

The timing budget includes level-translator delay, trace or cable flight time, real edge rate and capacitive load. A rate that works on a short board can fail after adding a connector or branch.

For the usual active-low CS: keep SCLK at its CPOL state, assert CS, wait tCSS, present the first bit early enough for CPHA = 0, generate edges, then wait tCSH before releasing CS. Some parts require CS low across an entire command-address-data sequence; others execute an operation when CS rises.


Wiring MOSI, MISO, SCLK and CS

Controller MOSI  ─────────►  Peripheral SDI / DIN
Controller MISO  ◄─────────  Peripheral SDO / DOUT
Controller SCLK  ─────────►  Peripheral SCK
Controller nCS   ─────────►  Peripheral nCS
GND              ─────────►  GND

MISO may only be shared when every unselected peripheral really becomes high impedance. A pull resistor sets an idle state but never resolves two active outputs fighting each other.

SCLK is often most critical: it switches every bit, synchronises every receiver and can ring. Keep it short, over a continuous ground plane and away from sensitive analogue inputs. A series resistor of a few tens of ohms at the driver may damp a point-to-point connection; validate the value with an oscilloscope.

CS permits only one peripheral to answer. A pull-up toward inactive is useful during controller reset, after checking internal pulls, power consumption and rise-time needs.


SPI topologies

Star with independent CS

SCLK and MOSI go to every IC; every IC receives its own CS. Only the selected part drives MISO.

                  ┌──────────► Peripheral A
Controller ─ SCLK ├──────────► Peripheral B
           ─ MOSI ├──────────► Peripheral C
           ─ MISO ◄────────── shared outputs, high impedance when unselected
           ─ CSA  ───────────► A
           ─ CSB  ───────────► B
           ─ CSC  ───────────► C

This is the recommended topology: it is straightforward to debug and does not route bytes through other devices. When GPIOs are scarce, a decoder or expander can generate CS signals; include its delay and power-up state in the design.

Daisy chain

Some shift registers and LED drivers are designed for daisy chains. Their serial output feeds the next input; they share SCLK and CS.

Controller MOSI ─► Device 1 ─► Device 2 ─► Device 3
Controller SCLK ─► all devices
Controller CS   ─► all devices
Controller MISO ◄─ final device output, if available

Frame length is the sum of all device words and physical order determines byte order. This is not appropriate for normal SPI peripherals that simply share a bus.

Shared bus and signal integrity

Each branch is a stub that degrades edges as SCLK increases. Prefer compact distribution and short branches. For cable or board-to-board links, reduce fSCLK, provide a close ground return and measure at the receiver. SPI is not a differential interface designed for long distances.

SituationRecommended practice
One IC, very short tracesDirect connection; validate mode and logic levels
Several ICs on one boardIndependent CS, MISO high impedance when unselected
SCLK ringingSeries resistor near controller; inspect with oscilloscope
Cable or interconnectLower rate, interleave GND, limit length, use transceiver if needed
Many CS linesDecoder, shift register or expander with safe power-up state

Logic levels and debugging

Compare VIH/VIL thresholds and absolute maximum ratings in both directions. A 1.8 V MISO can be too low for a 3.3 V input; a 5 V output can damage a non-tolerant 3.3 V input. Bidirectional MOSFET translators common in I²C are not automatically suitable for fast SCLK or MOSI: choose a buffer or translator rated for the chosen levels, rate and load.

  1. Start with one peripheral at 100 kHz or below.
  2. Check power, GND, reset and CS state before data.
  3. Measure SCLK, CS, MOSI and MISO together, triggered by active CS.
  4. Verify CPOL/CPHA, bit order and exact edge count per frame.
  5. Read a documented ID or status register, then raise the rate gradually.

A logic analyser checks protocol; an oscilloscope is required for edges, overshoot, noise and reflections. Probe near the peripheral input: measuring only at the controller can hide a line problem.


Checklist

  • CPOL, CPHA, bit order and word length explicitly documented in schematic and firmware.
  • One CS per peripheral unless a datasheet-defined chain is used.
  • MISO released when unselected; no output contention.
  • Logic thresholds, absolute maximums and timing checked in both directions.
  • Short SCLK over continuous ground; any series resistor validated.
  • Safe CS state during reset and power-up.
  • Receiver-side measurement after adding cable, connector, translator or branch.

References