TactivergeLite

Lite / Intelligent Robotic Systems / Week 30

SPI: swapping bytes at full speed

A tiny colour screen needs over a megabyte every second. Which bus can keep up, and why does reading from it mean sending something too?

About 35 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Displays, SD cards, fast ADCs and radio chips use SPI because it is simple and fast: no addresses, no acknowledges, no start or stop bits, just one bit per clock in each direction. The price is pins. Picking the right bus for each part of a robot is a real design decision you will make on every board.

Predict

A controller wants to read one byte from an SPI sensor. What must it do on MOSI while the byte arrives on MISO?

  1. Nothing: MOSI stays idle
  2. Send a byte too, often a dummy 0x00
  3. Send the sensor's address
Guess first, then reveal

It must send a byte. SPI is a shift-register swap: every clock moves one bit out and one bit in at the same time. To receive 8 bits the controller must clock 8 bits out, so it sends a dummy byte (often 0x00 or 0xFF).

Discover

Set the controller to 0x9F and the peripheral to 0x3C. Clock once at a time and watch both registers. After how many clocks have the two bytes completely swapped places?

What the interactive shows (text version)

The SPI diagram shows two 8-bit registers. Starting with the controller holding 0x9F (10011111) and the peripheral holding 0x3C (00111100), each press of Clock once shifts both registers left by one: the controller's top bit goes into the peripheral's bottom bit over MOSI and the peripheral's top bit goes into the controller's bottom bit over MISO. After 8 clocks the controller holds 0x3C and the peripheral holds 0x9F: the bytes have swapped.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: Watch the chip-select line go low before the clock starts, and notice data moving in both directions on every clock.

Probes a real SPI bus with a logic analyser so you can see clock, data and chip-select line by line.

“SPI: The serial peripheral interface” by Ben Eater (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

Understand

Four wires, one conversation at a time

SCLK is the clock from the controller. MOSI (controller out, peripheral in) and MISO (controller in, peripheral out) carry data. CS (chip select, active low) picks which peripheral is listening: each peripheral normally gets its own CS line, so four devices need four CS pins. There are no addresses: pulling a CS low is the address.

Two shift registers in a ring

Inside, the controller and the peripheral each hold an 8-bit shift register, joined in a ring by MOSI and MISO. On each clock, each side shifts its most significant bit out and takes the other side's bit in. After 8 clocks the bytes have swapped. That is why SPI is full duplex, and why reading always means writing something.

Modes: when to sample

Devices differ on the clock's idle level (CPOL) and on which edge data is sampled (CPHA), giving modes 0–3. Mode 0 (clock idles low, sample on the rising edge) is the most common. Controller and peripheral must use the same mode, or every bit is read half a clock off. The datasheet always states it; check it before blaming the wiring.

Choosing a bus

UART: 2 wires, point to point, no clock; 115200 baud 8N1 is about 11.5 kB/s. I²C: 2 wires shared by many addressed devices, acknowledged; 400 kHz gives at most about 44 kB/s before addressing overhead. SPI: 3 wires plus a CS per device, no addressing or acknowledge; 10 MHz moves 1.25 MB/s. Choose by data rate, number of devices and spare pins.

Explain it another way

Intuitive

Picture two people passing notes along a looped conveyor belt. Each time the belt clicks forward, one letter leaves each person and one arrives. After eight clicks they have swapped whole words, even if one of them only wanted to listen.

Technical

SPI is synchronous and full duplex with no protocol layer: framing is defined by CS, word length by the device (8, 16 or more bits), and there is no acknowledge, so a missing device reads as all 1s or all 0s rather than an error. Speed is limited by trace length, the peripheral's maximum SCLK and MISO propagation delay.

Mathematical

One bit per clock: a burst of N bytes takes 8N/f. At f = 10 MHz, 3 bytes = 24 clocks = 2.4 µs, and the data rate is f/8 = 1.25 MB/s. Compare I²C at 400 kHz: at best f/9 ≈ 44 400 bytes/s.

Applied

A 240 × 240 display at 16 bits per pixel holds 115 200 bytes per frame. At an 8 MHz SPI clock (1 MB/s) one full frame takes about 0.115 s, so roughly 8 frames per second; drawing only the part of the screen that changed is how small gadgets feel smooth.

Derivation: SPI burst time and data rate
  1. SPI moves one bit per clock edge pair and has no start, stop, parity or acknowledge bits.

  2. So N bytes need exactly 8N clocks (with CS held low and no gaps).

  3. One clock lasts 1/f, so the burst takes t = 8N/f. With f in MHz, t comes out in microseconds.

  4. The maximum data rate is N/t = f/8 bytes per second: 8 MHz → 1 000 000 bytes/s.

Worked example 1

How long does it take to send 64 bytes to an SD card at 4 MHz?

64 × 8 = 512 clocks. 512 / 4 MHz = 128 µs.

Worked example 2

A board has 3 SPI peripherals and 1 I²C sensor. How many microcontroller pins do the buses use?

SPI: SCLK, MOSI, MISO shared (3) plus one CS each (3) = 6. I²C: SDA and SCL = 2. Total 8 pins.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 2. Why does a controller send 0x00 on MOSI while reading a byte from an SPI sensor?

bytes/s

Practice 4. Which bus suits a colour display that needs about 1 MB/s, on a board with pins to spare?

µs

Apply it

Plan the buses for a rover with a GPS (UART, 9600 baud), an IMU and a barometer (I²C), and a 160 × 128 colour display at 16 bits per pixel. How long does one full display frame take over SPI at 8 MHz, and how many pins do all the buses use together?

Reflect

SPI is faster and simpler than I²C, so why does I²C still exist? Give two situations where you would choose it anyway.

Write your answer on paper or in your own notes. Lite never stores what you write.

← I²C: decoding a bus conversation