Draft preview. Lessons are being written and reviewed.

Digital Design / Module 12: Projects and interview preparation

65. Minor project: SPI controller

Handle clock modes and shift timing.

Lecture coming soon

Lab: Protocol waveforms

The goal

Build an SPI controller: a simple, fast, synchronous serial interface used for flash, sensors and displays. The controller generates a clock, so timing is easier than with UART. The challenge is getting the four modes right.

The signals

  • SCLK: clock, from the controller
  • MOSI: controller to peripheral
  • MISO: peripheral to controller
  • CS_n: chip select, active low, usually one per peripheral

The four modes

ModeCPOL (idle level)CPHAData sampled on
000Rising edge
101Falling edge
210Falling edge
311Rising edge

CPHA = 0 samples on the first clock edge after CS falls, so the data must be valid before that edge. CPHA = 1 changes data on the first edge, and samples on the second. See the waveforms for each mode in the protocol lab.

Architecture

  • A clock divider generates SCLK, usually with a programmable ratio.
  • It also produces internal enables, marking which cycles are "sample" and "shift" points.
  • hold transmit and receive data. Each transfer sends and receives at once.
  • A small FSM goes through idle, chip-select setup, transfer and chip-select hold.

Don't use SCLK as an internal clock. Keep everything on the system clock, and use enables. This avoids a second .

Requirements for your project

  • All four modes, chosen by configuration
  • Programmable clock divider, and MSB or LSB first
  • A valid/ready command interface
  • A behavioural peripheral model in your testbench, which checks setup and hold

Common mistake

Forgetting the sampling delay on MISO at high speed. Your clock goes out, the peripheral responds, and the data returns, which takes real time. At high SCLK rates, the round trip eats into your sampling margin, so calculate it. Many controllers add an adjustable sampling delay for this reason.

Key ideas

  • CPOL and CPHA define four modes.
  • Generate SCLK from a divider.
  • Check with a behavioural peripheral.

Your exercise

Build a mode-configurable SPI controller.

Interview questions

coreWhat do CPOL and CPHA set?

basicIn SPI mode 3, where does the clock rest, and on which edge is data sampled?

basicWhy is SPI called full duplex, and why does it not need oversampling like a UART?

coreSCLK is made from a 100 MHz system clock by toggling every div system clocks, so the SCLK period is 2 x div x 10 ns. A peripheral allows at most 12 MHz. What is the smallest div, and what SCLK do you get?

coreWhy should the controller not use SCLK as a clock for its own shift registers?

coreIn mode 0 the controller samples MISO half an SCLK period after the falling edge that launches it. SCLK takes 1.5 ns to reach the peripheral, the peripheral's clock-to-out is 7 ns, MISO takes 1.5 ns back, and the controller needs 1 ns of setup. Does 40 MHz work? What is the highest safe SCLK?

coreA controller in mode 0 talks to a peripheral that expects mode 1. Nothing hangs, but the data is wrong. What does the wrong data typically look like?

advancedIn CPHA = 0, the first clock edge samples. How does that affect when your controller loads the first MOSI bit?

advancedWhy does an SPI controller's state machine usually have separate chip-select setup and hold states around the transfer?

Practise these 9 questions with animations