SPI Protocol

Master the SPI (Serial Peripheral Interface) protocol and its VHDL implementation

What is SPI?

The SPI (Serial Peripheral Interface) protocol is a synchronous serial protocol developed by Motorola in the 1980s. It is widely used in embedded systems for communication between a microcontroller or FPGA and peripherals such as sensors, ADC/DAC converters, Flash memories, or displays.

Key characteristics:

  • Synchronous: a shared clock (SCK) drives all transfers
  • Full-duplex: simultaneous transmission and reception
  • Master/Slave: the master generates the clock and selects the slave
  • No addressing: selection is done via a dedicated SS_N signal

SPI Signals

SignalDirectionDescription
SCKMaster → SlaveSerial Clock
MOSIMaster → SlaveMaster Out Slave In
MISOSlave → MasterMaster In Slave Out
SS_NMaster → SlaveSlave Select, active low

With multiple slaves, the master has one SS_N signal per slave. MOSI, MISO, and SCK are shared (bus).


SPI Modes (CPOL / CPHA)

SPI defines 4 modes based on two parameters:

  • CPOL (Clock Polarity): idle state of SCK — 0 = low, 1 = high
  • CPHA (Clock Phase): sampling moment — 0 = leading edge, 1 = trailing edge
ModeCPOLCPHASamplingDescription
000SCK rising edgeSCK idle low, sample on rising
101SCK falling edgeSCK idle low, sample on falling
210SCK falling edgeSCK idle high, sample on falling
311SCK rising edgeSCK idle high, sample on rising

Mode 0 (CPOL=0, CPHA=0) is the most common and is used in platform exercises.


SPI Frame

A SPI transaction proceeds as follows:

  1. Master asserts SS_N low to select the slave
  2. Master generates SCK pulses
  3. Each SCK cycle transfers 1 bit in each direction (MOSI and MISO)
  4. Data is transmitted MSB first (most common convention)
  5. Master deasserts SS_N to end the transaction

For an 8-bit frame:

SPI timing diagram for 8-bit frame


VHDL Implementation

Put SPI implementation into practice with the following exercises:

Available SPI exercises:


Key Points

  • Synchronization: SPI signals (SCK, SS_N, MOSI) come from an external clock domain → always resynchronize them with a double register before use in the system domain
  • CPOL/CPHA: always check the slave device's datasheet. A mode mismatch causes corrupted data with no visible error
  • SS_N must stay asserted for the entire transaction. If SS_N deasserts between bytes, the slave resets its bit counter
  • SCK frequency: limited by the slave's maximum frequency and MISO propagation delay
  • Bit order: MSB first is standard, but some devices use LSB first — check the documentation

Advantages and Disadvantages

AdvantagesDisadvantages
Simple to implementOne SS_N pin per slave (many pins)
High speed (tens of MHz)No acknowledgment (ACK)
Full-duplexLimited distance
Flexible protocolNo native error detection