SPI master

Clock a byte out to a peripheral, MSB-first, in SPI mode 0.

When to use it

SPI is the go-to for talking to sensors, ADCs, flash and displays. The master drives a clock (sclk) and chip-select (cs_n) and shifts data out on mosi while reading miso back, full duplex. This is mode 0 (CPOL=0, CPHA=0): sample on the rising edge, change on the falling edge.

How it works

Lower cs_n, then for each of 8 bits toggle sclk: present the next mosi bit while the clock is low, and sample miso on the rising edge. After 8 bits, raise cs_n. rx_data holds whatever the peripheral shifted back.

Gotchas

Try it

Runs on our simulator, edit the code and press Run to see the waveform.

Open & fork in the playground → Ship the verified version: lfpga add lfpga_spi_master

The runnable example above is a teaching version. The libfpga library ships a hardened, parameterised, CI-tested module (lfpga_spi_master) for production use.

Next recipe: CRC-8 (serial)