Skip to main content

SPI Bus

SPI (Serial Peripheral Interface) is the high-speed four-wire bus that connects a microcontroller to fast peripherals — displays (ILI9341, SSD1306 in SPI mode), SD cards, radios (nRF24, RFM9X), high-rate ADCs, motor drivers and so on. The bus is full-duplex, can run from tens of kHz to tens of MHz, and is point-to-point per chip-select line.

Wiring and roles​

Four signals form the bus:

SignalDirectionRole
SCKmaster → slavesClock the master drives.
MOSImaster → slaves"Master Out, Slave In" — data from master to slave.
MISOslaves → master"Master In, Slave Out" — data from slave to master.
CS / SSmaster → one slave"Chip Select" / "Slave Select" — pulls low to talk to that one slave. One CS line per slave.

All slaves share SCK / MOSI / MISO; each one gets its own CS GPIO. The master de-asserts every CS except the slave it currently wants to address.

How NodeBlue exposes SPI​

Two patterns coexist:

  1. Module-managed — most peripherals that use SPI internally (radios, screens, SD cards) configure the bus in their own edit dialog. You set the SPI port and CS pin on the peripheral module itself; you do not add a separate SPI module.
  2. Direct — when you want raw SPI transactions from your flow (a custom sensor with no dedicated NodeBlue module, a one-off DAC, an experimental chip), drop in an nSPIMaster (or nSPISlave) module and wire bytes into / out of it.

nSPIMaster — direct master​

Use this when no dedicated module exists for the peripheral you're talking to. Its inputs accept a buffer to send; its outputs deliver the bytes received in the same transaction.

ParameterDescription
SPI PortWhich hardware SPI bus (SPI, SPI1, SPI2 …).
MISO / MOSI / SCKThe pins for the chosen port. Default values match the board's primary SPI.
CS pinThe chip-select line for the target slave.
Bit RateBus clock in Hz (default 1 MHz — push higher only if the peripheral supports it).
Data ModeSPI_MODE0..3 — combination of clock polarity (CPOL) and phase (CPHA). The peripheral's datasheet says which one. MODE0 is the most common default.
Bit OrderMSBFIRST (almost always) or LSBFIRST (rare; some specific peripherals).

The Examples/04-Communication/SPIMasterTest design shows a minimal master-side transaction.

nSPISlave — direct slave​

Less common, but supported: your board can also be an SPI slave responding to a master. The parameters mirror nSPIMaster (port, MISO/MOSI/SCK pins, data mode, bit order) but with SS pin in place of CS, and a Frame Length that fixes how many bytes per transaction the slave expects.

Module-managed SPI peripherals​

Every SPI-using module exposes the same parameter family (SPI Port, MISO, MOSI, SCK, CS, sometimes IRQ or Reset). For example:

  • The NRF24 radio module takes its own SPI port, CE pin, CS pin.
  • Display modules (SSD1306 in SPI mode, ILI9341) configure their own CS, plus a DC (data/command) and Reset pin specific to displays.

Picking the same SPI port on several modules is normal — they share the bus and the master only talks to one at a time via the chosen CS lines.

Tips for sharing a bus​

  • One CS per slave. Never short two CS lines together; if two slaves answer at once, MISO becomes a contested line and data is garbage.
  • Voltage levels. A 3.3 V MCU talking to a 5 V SPI slave usually needs a level shifter on MOSI/SCK; MISO from a 5 V slave to a 3.3 V MCU may also need one if the slave's VOH exceeds the MCU's tolerated level.
  • Bit Rate. When chaining several peripherals on the same bus, set each module's bit rate to what it can safely handle — the bus only runs as fast as the slowest active transaction.
  • Don't overlap port pins. Each hardware SPI port has fixed default pins; if two modules pick SPI but with different MISO/MOSI/SCK assignments, you'll get a pin-conflict warning in the IDE.

Now the other workhorse bus, complementary to SPI: I²C.