</>EmbeddedInterview

How do you connect multiple SPI devices?

EasyCommunication Protocols

Short answer: Usually with independent chip selects: all devices share SCLK, MOSI and MISO, and each has its own CS line; only the selected device drives MISO, the others keep it high-impedance. Alternatively, daisy-chain devices that support it: each device's data output feeds the next device's input and they share one CS, so data shifts through the chain like one long shift register.

Independent chip selects               Daisy chain
MCU SCLK ─┬─────┬─────┬                MCU MOSI → [Dev1] → [Dev2] → [Dev3] → MCU MISO
MCU MOSI ─┼──┬──┼──┬──┼──┬             shared SCLK and one CS
MCU MISO ─┼──┼──┼──┼──┼──┼
CS0 ──────┘  │  │  │  │  │   Dev0
CS1 ─────────┼──┘  │  │  │   Dev1
CS2 ─────────┼─────┘  ...

Independent CS details

  • MISO must be tri-stated by every deselected device; some devices do not do this properly (check the datasheet or add a buffer).
  • Different devices may need different SPI modes and speeds: reconfigure the peripheral before each transaction, and on a shared bus with an RTOS, protect it with a mutex.
  • Put pull-ups on CS lines so devices stay deselected while the MCU is in reset.
  • Running out of CS pins: use a GPIO expander, a 3-to-8 decoder (74HC138), or a demultiplexer.

Daisy chain details

  • Common with shift registers (74HC595), LED drivers and some ADCs/DACs.
  • To update N devices you clock out N frames, last device first, then raise CS to latch all of them together.

On Linux: each device on the bus has its own reg (chip select number) under the SPI controller in the device tree; the SPI core handles CS and mode per device.