School/○ Innovator/Elective · ECE-310
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
1.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.
- ◇ Name the four SPI signals and explain the shift-register exchange on every clock.
- ◇ Calculate SPI transfer time and data rate from the clock frequency.
- ◇ Choose between UART, I²C and SPI for a given device, data rate and pin budget.
2.Predict
A controller wants to read one byte from an SPI sensor. What must it do on MOSI while the byte arrives on MISO?
Guess first. Being wrong here is how the surprise works.
3.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?
Loading the interactive…
Can't use the interactive? Read a description instead
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.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
In an 8-bit SPI transfer, how many clock cycles does it take for the controller's and peripheral's bytes to swap completely?
5.Watch
→ 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.
32 min 59 s · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
6.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
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.
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.
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.
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
SPI moves one bit per clock edge pair and has no start, stop, parity or acknowledge bits.
So N bytes need exactly 8N clocks (with CS held low and no gaps).
One clock lasts 1/f, so the burst takes t = 8N/f. With f in MHz, t comes out in microseconds.
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
- You can read from SPI without writing.
✓ Every clock shifts one bit each way; reading N bytes means sending N bytes (often dummies). - SPI devices have addresses like I²C devices.
✓ SPI has none: the chip-select line picks the device, at the cost of one pin each. - If the wiring is right, any SPI mode will work.
✓ Controller and peripheral must agree on CPOL and CPHA; the wrong mode shifts every bit by half a clock.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
Why does a controller send 0x00 on MOSI while reading a byte from an SPI sensor?
Practice 3
What is the maximum SPI data rate at an 8 MHz clock, in bytes per second?
Practice 4
Which bus suits a colour display that needs about 1 MB/s, on a board with pins to spare?
Practice 5
Four SPI peripherals share SCLK, MOSI and MISO, each with its own chip select. How many microcontroller pins does the SPI bus use?
Practice 6 · a fresh version every time
A microcontroller clocks 43 bytes out of an SPI port at 10 MHz with chip select held low throughout and no gaps between bytes. How long does the burst take, in microseconds? (2 decimal places)
8.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?
9.Reflect
Concepts: Serial communication: UART, I2C and SPI