EE Primers · Communication interfaces · Board-Level Buses
SPI: how the four-wire bus works, from shift registers to timing margin
SPI is two shift registers connected in a ring and clocked together. The controller drives a clock and selects one peripheral; on every clock, one bit leaves each register and enters the other. There is no addressing on the wires, no acknowledgement and no specification body, which makes SPI simple, fast and full-duplex, and puts every detail that I²C standardises (clock polarity, sampling edge, framing, timing) into each device's data sheet. This page covers the circuit, the four modes, a bit-level worked example and the timing budget that limits how fast a real board runs.
Terminology: controller and peripheral, with the widely used pin names SCLK, MOSI (controller out, peripheral in), MISO (controller in, peripheral out) and CS# (chip select, active low). Data sheets also use SCK, SDI/SDO, COPI/CIPO, SS#, nCS, SYNC [6]. The I²C companion page uses the same examples from TI SBAA565 [1], so the two buses can be compared byte for byte.
01 · At a glance
What SPI is and how it differs from I²C
Motorola introduced SPI in the 1980s for its microcontrollers; the CPOL/CPHA naming comes from Motorola's SPI block documentation [2]. Everyone else copied the idea, not a standard, so details vary between devices. It is used wherever bandwidth matters more than wire count: serial flash and FRAM, ADCs and DACs, IMUs, display controllers, Ethernet and Wi-Fi controllers, SD cards (in SPI mode), touch controllers and FPGA configuration.
On a typical board, SPI links a microcontroller or SoC to small peripherals: sensors, EEPROM and flash memory, SD cards, displays, and radio modules such as Bluetooth and Wi-Fi. The bus uses separate lines for clock and data, plus a select line for each peripheral.
Roles are fixed. The side that generates SCLK is the controller (master). Everything else is a peripheral (slave). There is one controller and there can be many peripherals. The controller starts every transfer; a peripheral cannot clock the bus, cannot start a transfer, and cannot switch itself into the controller role. Some MCU SPI blocks can be configured as either, but that choice is made in firmware before the bus is used. It is not negotiated on the wires.
SPI
I²C
Wires
SCLK, MOSI, MISO + one CS# per peripheral
SCL, SDA for all devices
Duplex
Full: a bit moves each way on every clock
Half
Output stage
Push-pull (MISO tri-states when deselected)
Open-drain with pull-ups
Device selection
Hardware chip select
Address byte on the bus
Flow control / ACK
None; the controller assumes the peripheral keeps up
ACK per byte, clock stretching
Typical clock
1–50 MHz; flash 100–200 MHz (quad/octal, DTR)
100 kHz–1 MHz
Overhead
None beyond the device's command bytes
Address byte, 1 ACK bit per 8
Multi-controller
Not supported without external arbitration
Built in (arbitration)
Edges
Fast (≈ 1–5 ns), symmetric: a transmission-line problem
Slow RC rise, fast fall
02 · Principle
Two shift registers in a ring
Eight clocks exchange the two registers' contents. The MSB (b7) leaves first on each side; after the eighth clock the controller holds what the peripheral had and vice versa. CS# (not drawn) enables the peripheral's register and output.
Three consequences follow directly from the ring:
Every transfer is an exchange. To read, the controller must clock, and to clock it must send something. Reads therefore send dummy bytes (often 00h or FFh), and writes receive bytes the controller usually discards.
The controller sets the pace and the length. The peripheral has no way to say "wait" or "done". Frame length, command format and turnaround (the bytes between a command and its response) come from the peripheral's data sheet.
CS# frames the transaction. Its falling edge resets the peripheral's bit counter and enables its MISO driver; its rising edge ends the command, and many devices act (latch a DAC code, start a flash program) only on that rising edge. Holding CS# low across bytes is part of the protocol, not a convenience.
This is also why SPI is cheap to build. Transmitter and receiver are a single shift register with a bit counter. There is no oversampling or baud-rate recovery as in a UART, and no open-drain arbitration, ACK logic or address match as in I²C.
Step through the exchange, one clock at a time
Shift-register stepper
Bit orderMost devices send the most significant bit first, and the figures on this page assume that. Bit order is a setting, though: MCU SPI blocks have an LSB-first control bit, and a few peripherals (some display drivers and LED drivers) expect LSB first. Both ends must agree. A bit-order mismatch reverses each byte: 0x01 arrives as 0x80, and 0x3C reads back unchanged because its bit pattern is a palindrome.
Write-only transfersWhen the controller only writes, it still shifts in whatever is on MISO. If the peripheral does not drive MISO, the controller reads the idle level of the line: 00h with a pull-down or a peripheral that holds MISO low, FFh with a pull-up. That data is ignored, but firmware usually still has to read the receive register to clear its flags (§12).
03 · Circuits
Reference circuits
Two peripherals on one bus. Rs (typically 22–33 Ω) at each driver brings driver impedance plus Rs up to the trace impedance and kills ringing. CS# pull-ups keep peripherals deselected while the controller boots with its pins floating. A weak pull (10–100 kΩ) on MISO keeps it defined when nobody drives it.
Independent chip selects
The normal topology
One CS# per peripheral; SCLK, MOSI and MISO shared.
Only the selected peripheral drives MISO. A part that does not tri-state its MISO when deselected (some ADCs and display drivers) needs a buffer or its own bus.
Peripherals may use different modes and speeds; the controller reconfigures between transactions.
Where CS# comes from
GPIO or hardware chip select
Most MCU SPI blocks have one hardware select pin (often NSS) or none, so the usual approach is to drive each CS# from an ordinary GPIO (gpio1 → peripheral 1, gpio2 → peripheral 2, …). Firmware pulls the GPIO low, runs the transfer and pulls it high.
With GPIO selects, the SPI block is set to software slave management. This keeps its internal select input asserted, so it stays in controller mode while the GPIOs do the selecting (§12).
Hardware select can toggle CS# per frame without CPU involvement, but it serves only one peripheral, and its timing between bytes may not match what the device needs.
Fewer wires
Write-only, read-only and 3-wire
DACs, LED drivers and many display controllers have no data output; MISO is not connected.
"3-wire" SPI shares one bidirectional data line (SDIO). The controller switches direction after the command.
Section §04 covers all four line configurations and the direction-swap timing.
Daisy chain
One chip select, one frame of N × 8 clocks. The first byte sent ends up in the last device; CS# rising latches all of them at once. Used by LED drivers, multi-channel DACs, relay drivers and JTAG-like chains. MISO timing now includes every device's clock-to-output delay in series.
04 · Line configurations
Full duplex, simplex and half-duplex bidirectional
Standard SPI is a full-duplex bus built from unidirectional lines. MOSI only ever carries data from controller to peripheral, and MISO only from peripheral to controller. Many MCU SPI blocks let you drop one data line or merge the two. Use these options when a peripheral only listens, only talks, or has a single data pin, or when a spare pin is needed for something else. Not every SPI block supports every option, so check the controller's reference manual before planning a layout around one.
Configuration
Data lines
Direction
What happens to the unused pin
Watch for
Full duplex (default)
MOSI + MISO
Both ways on every clock
—
Read data comes back on every frame, even when it is ignored
Simplex, transmit only
MOSI
Controller → peripheral
MISO pin released as a GPIO or alternate function
Receive logic may still shift in and set an overrun flag. Disable reception, or ignore the flag and clear it
Simplex, receive only
MISO
Peripheral → controller
MOSI pin released
On many MCUs a receive-only controller clocks continuously as long as SPI is enabled. Disable it at exactly the right moment, or extra bytes are clocked out of the peripheral
Half duplex, bidirectional (3-wire)
one shared SDIO
One way at a time
One pin saved
Direction must swap between the command and the response, with no overlap where both sides drive
Bus direction swap on a single data line
In bidirectional mode, the same pin acts as the controller's transmit (MOSI) for the command phase and as its receive (MISO) for the response. Between the two, the controller stops the clock and turns its output driver off. The peripheral turns its driver on only after it has counted the end of the command. The data line is undriven for that short gap. A pull resistor holds its level, and no clock edges occur, so nothing is sampled. The direction bit is set in a control register, so the gap is only as short as the firmware or DMA makes it.
Bidirectional (3-wire) read. MOSI phase: the controller drives the shared line, launching on falling and sampling on rising edges (mode 0). Swap: SCLK is held idle while the drivers change over. MISO phase: the peripheral drives the line and the controller samples. The two output-enable traces never overlap; an overlap would mean contention on SDIO.
CPOL (polarity) is the idle level of SCLK: 0 = idles low, 1 = idles high. The first edge after CS# falls is the leading edge; the second is the trailing edge.
CPHA (phase) chooses which edge samples. CPHA = 0: data is sampled on the leading edge and changed on the trailing edge, so the first bit must already be on the line when CS# falls. CPHA = 1: data is changed on the leading edge and sampled on the trailing edge.
Toggling vs sampling.Toggling (or launching) is when the transmitter moves the line to the next bit. Sampling is when the receiver captures the bit. CPHA decides which edge does which. CPOL decides whether the leading edge is rising (CPOL = 0) or falling (CPOL = 1).
Outside a frame. While CS# is high, the peripheral ignores SCLK and MOSI and holds MISO in high impedance. The waveforms show this as the dashed lines before and after the frame.
The rule underneath all four modes: each side launches data on one edge and the other side samples it on the opposite edge, half a clock period later. That half-period separation is what makes SPI robust: the receiver's hold time is met by half a period, not by a few nanoseconds of delay matching.
Mode
CPOL
CPHA
SCLK idle
Data sampled on
Data shifted out on
First bit valid
0
0
0
Low
Rising (leading)
Falling (trailing)
At CS# fall
1
0
1
Low
Falling (trailing)
Rising (leading)
After first rising edge
2
1
0
High
Falling (leading)
Rising (trailing)
At CS# fall
3
1
1
High
Rising (trailing)
Falling (leading)
After first falling edge
Modes 0 and 3Both sample on the rising edge and shift on the falling edge; they differ only in the idle level. That is why most memories and sensors list "mode 0 and 3": they just sample on rising edges and don't care where SCLK idles. Modes 1 and 2 are the falling-edge-sampling pair, common on DACs. Beware that some vendors (notably some Microchip PIC documentation) define a "CKE" bit that is the inverse of CPHA.
Each panel sends A\(5h=1010\ 0101\). Dashed teal lines: sampling edges. Purple triangles: launch edges, where the transmitter changes the data. In modes 0 and 2 there is no launch edge for bit 7: the transmitter puts it out when CS# falls.
06 · Worked example
A transfer from controller to peripheral, bit by bit
The first preset is the transaction from the I²C page, sent over SPI: the DAC80501 from TI SBAA565 [1] also has an SPI port, and setting its output to 1.5 V takes the same three bytes, 08h then 4Ch CDh, framed by CS# (the part calls it SYNC) instead of an address and ACKs. The other presets show a register read from an IMU and a flash ID read, where the peripheral drives MISO. Change the mode to see the same bytes under each clock convention; the builder flags a mode the device would not accept.
How to read it: teal shading is the controller driving MOSI, amber is the peripheral driving MISO, a dashed or hatched line is high-impedance or don't-care. The number in each data window is the bit, MSB first; it is sampled at the dashed vertical line through it.
Sequence view of an IMU register read. There is no address or acknowledgement: CS# selects the peripheral, and every 8 clocks move one byte in each direction. The same read over I²C, with START, address, ACKs and a repeated START, is drawn in the I²C primer.
Frame builder
Scroll sideways on narrow screens. CS# low frames the transfer; there are no START, address or ACK bits.
CS# falls. The DAC resets its 24-bit input shift register and starts counting clocks. The controller must wait tCSS before the first clock edge.
Bit 23 (command MSB). CPHA = 1, so the controller changes MOSI just after the first rising edge and the DAC samples it on the following falling edge, half a period later.
Clocks 1–8: \(0000\ 1000=\text{08h}\), the DAC_DATA register address with the write flag. Clocks 9–16: \(0100\ 1100=\text{4Ch}\), clocks 17–24: 1100 1101 = CDh.
CS# rises after the 24th falling edge plus tCSH. The DAC transfers the shift register into DAC_DATA and the output moves to \(\text{4CCDh}/65536\times 5\ \text{V}\approx 1.500\ \text{V}\). Raising CS# early (say after 16 clocks) aborts the frame; a wrong clock count is the most common SPI firmware bug with DACs.
Compare the cost with I²C: SPI used 24 clocks for 16 bits of payload plus an 8-bit command and needed an extra pin (SYNC); I²C used 36 clocks plus START/STOP to move the same data, and needed no pin beyond the shared two. At 10 MHz SPI the frame takes 2.4 µs; at 400 kHz I²C, about 95 µs.
07 · Reading data back
Fixed-length and variable-length reads
A peripheral cannot generate a clock, so it can return data only while the controller is clocking. To read N bytes, the controller must keep generating SCLK for N × 8 more cycles after the command, sending dummy bytes on MOSI while the peripheral shifts its data out on MISO. Both sides must agree on that number in advance, before the controller starts the read. Peripherals do this in one of two ways:
Case 1 — fixed length. The data sheet fixes the response size. For example, a sensor always returns 4 bytes per sample, or a flash ID is always 3 bytes. The controller sends the read command and then clocks exactly that many dummy bytes.
Case 2 — variable length. The peripheral produces a varying amount of data, such as a FIFO depth or a radio module's packet. It answers the read command with a length byte first, and the controller then generates clocks for exactly that many bytes. Some devices return the length within the same CS# frame, as shown here. Others need a separate status read first. The data sheet says which.
Either way, for every byte the controller sends it receives one byte, whether or not that byte carries useful data.
Read-sequence builder
The same sequence as a waveform: one CS# frame. MISO is "don't care" while the command is going out; after that every MOSI byte is a dummy (00h) that exists only to make the clock.
Length disagreementClock too few bytes and the remaining data stays in the peripheral; some devices then return it at the start of the next read, or flag an error. Clock too many and the extra bytes are padding (often 00h or FFh), or data from the next record. In case 2, a variable-length read must not end until the peripheral has reported its length. Firmware therefore reads the length byte first and sets up the rest of the transfer, or a DMA count, only after that byte arrives.
08 · Timing
The timing parameters in a data sheet
SPI timing lives in each peripheral's data sheet, referenced to the threshold (typically 50 % of VDD, or VIL/VIH). Names vary; the quantities don't. The zoom below is mode 0, the first three bits of a frame.
Mode 0: MOSI changes after SCLK falls and is sampled on the next rising edge (dashed). MISO is launched by the peripheral after SCLK falls, after its clock-to-output delay tV.
Parameter
Also called
Meaning
Typical, 3.3 V flash or sensor
fSCLK
fC, fR
Maximum clock; often lower for the plain READ (03h) command than for fast read
10 MHz (sensor) to 133 MHz (flash)
tCH, tCL
tCLH, tCLL, tWH
Minimum clock high and low times; with duty-cycle distortion the shorter half sets the limit
≥ 45 % of period
tCSS
tSLCH, tLEAD
CS# falling to first clock edge
5–50 ns
tSU
tDVCH, tDS
Input data setup before the sampling edge
2–10 ns
tH
tCHDX, tDH
Input data hold after the sampling edge
2–10 ns
tV
tCLQV, tODLY
Clock edge to output valid (max): the peripheral's response delay
6–30 ns
tHO
tCLQX
Output hold after the clock edge (min): how long the old bit lingers
0–2 ns
tCSH
tCHSH, tLAG
Last clock edge to CS# rising
5–50 ns
tCSD
tSHSL, tTD
CS# high time between frames
10–100 ns (longer after writes)
tDIS
tSHQZ
CS# rising to MISO high-impedance
5–10 ns
Hold on MISOIf the controller samples late, it needs the peripheral's old bit to still be there: tHO + flight ≥ the controller's hold requirement. With \(t_{\text{HO}}=0\) that margin can be negative at the next edge, which is why "delayed sampling" options sample half a cycle later, not a full cycle.
09 · Speed limit
Timing budget: MOSI is source-synchronous, MISO is a round trip
The two data directions have very different timing, and the difference sets the practical clock limit.
MOSI travels with SCLK: the controller launches both, they cross the board together, and the peripheral sees them with only their relative skew. Data is launched on one edge and sampled on the opposite edge, so the setup window is half a period minus skew:
MISO is a loop: the clock goes out to the peripheral, the peripheral responds after tV, and the data comes back to the controller, which samples it on its own clock. Everything adds:
Nothing about the trace length cancels here, and any buffer or digital isolator in the path appears twice. A 25 ns isolator turns a 37 MHz link into one that fails above about 8 MHz. Controllers that run flash at 100+ MHz do it with a delayed sample point (sample a half-cycle late, or on a returned/loopback clock), or with DQS-style strobes in octal xSPI.
10 · Signal integrity
Rise time, fall time, skew and clock integrity
SPI drivers are ordinary CMOS outputs with 1–5 ns edges regardless of the clock rate. At those edges a trace becomes a transmission line beyond a few centimetres (critical \(\text{length}\approx t_{\text{r}}\cdot v/6\), so 2.5 cm for a 1 ns edge in FR-4 stripline) [5]. A 1 MHz SPI clock with 1 ns edges has the same ringing as a 100 MHz one.
SCLK is the signal that must be clean
Data lines are sampled once per bit, at a known instant; the clock is used at every crossing of the threshold. A ring-back, reflection ledge or crosstalk spike that pushes SCLK back across the threshold during an edge is counted as an extra edge. The peripheral then shifts one bit too many: every byte after the glitch is off by one bit, and the symptom is data that is correct but shifted, or a device that ignores commands until CS# is toggled. Rules for SCLK:
Monotonic edges through the threshold. Series-terminate at the driver (\(R_{\text{s}}\approx Z_{0}-R_{\text{drv}}\), typically 22–33 Ω), keep the route point-to-point or daisy-chained (fly-by) with short stubs, and avoid a star with long branches: each branch end reflects back into the others.
Multiple loads. Series termination works for loads at the far end of a line; a load in the middle sees the half-amplitude ledge for one round trip. Either cluster the loads at the end, keep the line electrically short, or give each peripheral its own buffered clock.
Crosstalk. Route SCLK away from other fast nets, with ground on both sides on long runs. A coupled spike on SCLK is far more damaging than one on MOSI.
Probing. A scope probe ground lead or a test point stub can create the ringing it measures. Use a short ground spring and measure at the receiver pin.
Rise and fall times
Edges should be slow enough not to ring or radiate and fast enough not to eat the timing budget. Two effects matter:
Threshold-crossing delay. A slow edge reaches VIH or VIL later; if the controller's and peripheral's thresholds differ (1.8 V vs 3.3 V logic, or through a translator), the effective delay differs between rising and falling edges.
Duty-cycle distortion. Unequal rise and fall times, or a threshold that is not at mid-swing, make SCLK's high and low times unequal. In modes where launch and sample are half a period apart, the shorter half sets the budget. A 50 MHz clock with 10 % distortion leaves 8 ns, not 10 ns, for setup.
Drive-strength and slew settings. Most MCUs have programmable output speed. Use the slowest setting that meets timing at the receiver; the fastest setting on a 5 cm trace is a common EMC failure.
Skew between data and clock
MOSI vs SCLK: matched routing isn't needed for timing at modest rates because the margin is half a period. At 50 MHz (10 ns half-period) a few centimetres of mismatch is still small; at 100+ MHz in quad/octal flash the data lines are matched to each other and to SCLK within a few millimetres, as in a parallel bus.
MISO vs SCLK: skew here is the whole round-trip delay; length matching doesn't help. Shorten the path, delay the sample point, or slow the clock.
CS#: not timing critical in the nanosecond sense, but it must never glitch while a frame is in progress. A CS# pulse narrower than tCSD can partially reset a device.
Rule setSeries-terminate SCLK and MOSI at the controller and MISO at each peripheral. Route SCLK point-to-point or fly-by with ground reference and short stubs; never let it ring through the threshold. Compute the MISO round trip before choosing the clock rate. Pull CS# up, and keep the slew rate as low as timing allows.
MOSI/MISO become IO0/IO1, both directions per phase
Flash reads
Quad SPI (1-4-4, 4-4-4)
4 bidirectional
4 bits per clock; dummy cycles before data for read latency
Execute-in-place flash, PSRAM
Octal / xSPI (JESD251)
8 + DQS strobe
DTR (data on both edges), read strobe solves the round-trip problem
Automotive and high-performance code storage
DTR / DDR
any
Data on both clock edges; launch-to-sample is a quarter period
Fast flash
TI SSP / frame format
1 + 1
Frame pulse instead of a held CS#
Some DSPs, codecs
Microwire
1 + 1
Half-duplex command/response, mode 0 like
Older EEPROMs
Serial flash enumerates its capabilities in the SFDP table (JESD216), so a controller can discover supported modes, dummy cycles and addressing at boot [4].
12 · Inside the controller
The MCU SPI peripheral: buffers, registers and the clock that makes SCLK
The bus-level view treats the controller as one shift register. Inside a microcontroller, the SPI block also has buffers, control and status registers, and a clock divider. Register and bit names below are typical, and many Cortex-M parts use these exact names. Other vendors provide the same functions under different names, and the reference manual's SPI chapter is the authority for each part [8].
Generic MCU SPI block. The shift register is double-buffered: firmware (or DMA) writes the next frame into the Tx buffer while the current one is shifting, and collects the previous frame from the Rx buffer. The data register (DR) is one address that reaches the Tx buffer on a write and the Rx buffer on a read.
Data path
Two buffers around one shift register
Transmit: write the byte to DR. It goes into the Tx buffer and moves to the shift register as soon as the shifter is free. Transmission starts there, and TXE goes high to say the buffer can take the next byte.
Receive: when the shift register has a complete frame, it moves the frame to the Rx buffer and sets RXNE. Reading DR returns the frame and clears RXNE.
Because SPI is full duplex, both buffers are needed: on every frame one byte leaves and one arrives. If the Rx buffer is not read before the next frame completes, OVR (overrun) is set and data is lost.
Configuration
Control and status registers
CR1/CR2 hold everything set before a transfer: controller or peripheral role, CPOL, CPHA, the baud-rate field BR[2:0], bit order, frame size, software or hardware select, the bidirectional and receive-only bits (§04), and interrupt and DMA enables.
SR reports events during a transfer: TXE, RXNE, BSY (a frame is still shifting, so do not raise CS# yet), OVR, MODF and CRC errors.
MODF is set when something pulls the hardware select input low while the block is the controller, because another device may be acting as controller. With GPIO chip selects, set software select management (SSM = 1, SSI = 1) so this never happens.
Firmware
A polled one-byte exchange
Pull the peripheral's CS# GPIO low.
Wait for TXE = 1, then write the byte to DR.
Wait for RXNE = 1, then read DR. This is the peripheral's byte, and the read clears the flag.
After the last byte, wait for BSY = 0, then pull CS# high. Raising CS# on TXE alone cuts off the final byte.
From oscillator to SCLK: the clock tree
SCLK is not a clock of its own. It is the SPI block's peripheral clock \(f_{\text{PCLK}}\) divided by the baud-rate prescaler, and \(f_{\text{PCLK}}\) is in turn the clock of the peripheral bus that the SPI block sits on:
Clock sources. A typical MCU can run from an internal RC oscillator (for example 16 MHz, no external parts, about ±1 % accuracy), an external crystal oscillator (for example 8 MHz, accurate), or a PLL that multiplies either source up to the maximum core clock. After reset, most parts run from the internal RC oscillator until firmware switches to another source.
System clock to bus clocks. The selected source becomes the system clock. Prescalers divide it down to each peripheral bus, and every bus has its own maximum frequency. MCUs often have a slower and a faster peripheral bus, with different SPI instances on each. In one widely used Cortex-M4 family, the slow bus is limited to 42 MHz and the fast bus to 84 MHz.
Bus clock to SCLK. The SPI baud-rate prescaler divides \(f_{\text{PCLK}}\) by \(2^{\text{BR}+1}\), from ÷2 to ÷256. The minimum divider is 2, so the fastest SCLK is half the bus clock. To lower the SCLK frequency for a slow peripheral, increase the divider.
On the reset clock, every SPI instance tops out at 8 MHz no matter which bus it sits on, because both buses run at the 16 MHz system clock. A fast SCLK needs the PLL. Check the MCU data sheet for each instance's own SCLK limit, since this can be below \(f_{\text{PCLK}}/2\) in some modes, and check the peripheral's limit too.
13 · When it breaks
Failure modes and debug signatures
Symptom
Likely cause
Check / fix
Read data is the expected value shifted by one bit (e.g. 6Ch reads as 36h or D8h)
Wrong mode (CPHA): the controller samples half a period early or late; or an extra clock edge from SCLK ringing
Match CPOL/CPHA to the data sheet's timing diagram, not its prose. Look at SCLK at the receiver for non-monotonic edges.
Reads return FFh or 00h always
MISO not driven: wrong CS#, peripheral in reset or wrong interface mode (e.g. part strapped for I²C), MISO not connected, pull-up/down holding the line
Scope CS# and MISO together; confirm the part's interface-select pin.
Works at low speed, fails above some frequency
MISO round trip exceeded; isolator or level translator in the path
Run the §09 budget; enable delayed sampling; slow the clock.
First transfer after boot fails, later ones work
CS# floated low during controller boot and the peripheral counted garbage clocks; SCLK idle level changed after CS# fell
CS# pull-ups; configure SPI mode before asserting CS#; send a dummy deselect at start-up.
Bus contention: MISO mid-level, hot parts
A deselected peripheral still drives MISO; two CS# asserted together
Check each part's MISO tri-state behaviour; buffer the offender.
DAC or shift register outputs wrong, intermittently
Frame length wrong (CS# rises early or late), or a glitch on CS# mid-frame
Count clocks per frame on the logic analyzer; check tCSH and tCSD.
EMC emissions at SCLK harmonics
Fast edges, unterminated lines, long return paths
Series R, lower slew setting, solid reference plane, shorter routes.
Debug setupTrigger on CS# falling, probe CS#, SCLK, MOSI and MISO at the peripheral, and set the decoder's CPOL/CPHA to match the device. Then measure what the decoder can't show: SCLK edge shape at the receiver, MISO delay after the launching edge (that is tV plus flight), and the margin to the next sampling edge.
14 · Design review
Schematic and layout checklist
Schematic
Mode, maximum fSCLK and frame length for every peripheral, written on the schematic
Pull-up on every CS#; weak pull on MISO
Every peripheral tri-states MISO when deselected
Series R footprints on SCLK, MOSI (at controller) and MISO (at each peripheral)
Level translation: direction-controlled buffers for SCLK/MOSI/CS#, and for MISO; avoid auto-direction translators above a few MHz
Isolators budgeted twice in the MISO loop
Layout
SCLK point-to-point or fly-by, continuous reference plane, no long stubs
Keep SCLK away from other fast nets; ground guard on long runs
Short MISO loop: place the fastest peripherals closest to the controller
Quad/octal: match IOx and SCLK lengths (and DQS) per the controller's guidelines
Test points on all lines without long stubs
Bring-up
Read an ID register first; compare bit-shifted results to spot a mode error
Check SCLK monotonicity at each receiver
Measure tV + flight on MISO and compare with the sample point
Margin test: raise fSCLK until failure, then run at ≤ 70 % of that over temperature
15 · Self-check
Questions
In mode 3, on which edge is MOSI sampled and on which does it change?
SCLK idles high. Data changes on the falling (leading) edge and is sampled on the rising (trailing) edge, the same edges as mode 0.
Why can a peripheral rated for "mode 0 and mode 3" work with either?
Both modes sample on rising edges and shift on falling edges. They differ only in the idle level of SCLK, which a device that just reacts to edges after CS# falls doesn't need to know. (A mode-3 frame starts with a falling edge, which such a device treats as a shift edge with nothing yet to shift.)
Why is MISO, not MOSI, the speed limit?
MOSI and SCLK travel together, so only their skew matters. MISO depends on the clock reaching the peripheral, the peripheral's clock-to-output delay, and the data returning: two flight times plus tV plus the controller's setup, all within half a period.
A reading of WHO_AM_I returns D8h instead of 6Ch. What's wrong?
\(D8h=1101\ 1000\) is \(\text{6Ch}=0110\ 1100\) shifted left by one bit: the controller sampled one edge late, typically CPHA set wrong, or MISO arriving after the intended sampling edge. Check the mode first, then the round-trip timing.
You add a 25 ns digital isolator between controller and ADC. What happens to the maximum SPI clock?
The isolator delay appears in both the SCLK path and the MISO path, adding 50 ns to the round trip. With normal sampling T/2 ≥ ~63 ns, so fSCLK ≤ ~8 MHz. Delayed sampling, or a returned clock from the far side, recovers some of it.
Why does a ringing SCLK corrupt data while equally ringing MOSI usually doesn't?
MOSI is looked at once per bit, at the sampling edge, by which time ringing has decayed. SCLK is used at every threshold crossing; a ring-back across the threshold is a real clock edge to the receiver and shifts an extra bit.
How many clocks does a daisy chain of three 16-bit DACs need, and when do they update?
48 clocks in one CS# low period. The first 16 bits sent end up in the last DAC. All three latch when CS# rises.
The controller holds A5h and the peripheral holds 3Ch, both MSB-first. What does each register hold after 3 clocks, and after 8?
Each clock moves one bit out of each register and into the other. After 3 clocks, the controller has shifted out 101 and taken in the peripheral's top three bits, 001, so it holds 0010 1001 = 29h. The peripheral holds 1110 0101 = E5h (its own remaining 1 1100 followed by A5h's top three bits, 101). After 8 clocks the contents are fully swapped: the controller holds 3Ch and the peripheral holds A5h.
A sensor returns a variable number of FIFO bytes. Why can't the controller just "keep reading until the sensor stops"?
The sensor can't stop anything, because the controller generates every clock. Without a length the controller can't tell real data from padding. The sensor reports a count first (case 2 in §07), and the controller then clocks exactly that many bytes.
Why can't firmware raise CS# as soon as TXE goes high after the last byte?
TXE means the transmit buffer is empty, not that the shift register has finished. The last byte is still being clocked out. Wait for BSY = 0 (or for RXNE of the last byte), then raise CS#.
An MCU runs from its 16 MHz internal RC oscillator. What is the fastest SCLK any of its SPI blocks can produce, and how do you go faster?
Both peripheral buses run at 16 MHz with no division, and the minimum SPI prescaler is ÷2, so every SPI instance is limited to 8 MHz. To go faster, switch the system clock to the PLL. With 168 MHz and bus clocks of 42 and 84 MHz, the limits rise to 21 and 42 MHz respectively.
You use a receive-only configuration to read a 2-byte ADC result, and the next conversion comes back corrupted. What is likely happening?
On many MCUs a receive-only controller generates SCLK continuously while SPI is enabled. If firmware disables it late, extra clocks shift out bits of the next result, or desynchronise the ADC's bit counter. Disable SPI at the documented point (often while the last frame is being received), or use full duplex and ignore MOSI.
16 · Further reading
References
J. Wu, A Basic Guide to I2C, Texas Instruments application note SBAA565, Nov. 2022. Includes the SPI versus I²C comparison (push-pull, full duplex, four wires) and the DAC80501 example used here. Link
Motorola (Freescale/NXP), SPI Block Guide, V04.01. The original CPOL/CPHA definitions and timing diagrams.
P. Dhaker, "Introduction to SPI Interface," Analog Dialogue, Analog Devices, 2018. Link
JEDEC, JESD216: Serial Flash Discoverable Parameters, and JESD251: xSPI.
H. Johnson and M. Graham, High-Speed Digital Design: A Handbook of Black Magic, Prentice Hall, 1993. Transmission-line behaviour, termination and clock integrity.
Open Source Hardware Association, "A Resolution to Redefine SPI Signal Names," 2020. Link
Texas Instruments DAC80501, ST LSM6DSO and Winbond W25Q128JV data sheets (register maps, IDs and timing used in the examples).
Microcontroller reference manuals, SPI chapter (block diagram, control and status registers, baud-rate prescaler, simplex and bidirectional modes) and clock-tree (reset and clock control) chapter. The register names in §12 follow common Cortex-M usage; check your part for its exact fields and bus limits.