SPI stands for Serial Peripheral Interface, and it’s considered a 4-wire interface; this is misleading, though. In reality, it’s 3 wires, plus one per peripheral. It’s a hierarchical protocol, using a controller/peripheral regime (master/slave). The necessary wires are:

CIPO (MISO) - Controller in, peripheral out. This line carries messages sent from the controller, to a peripheral COPI (MOSI) - Controller out, peripheral in. This line carries messages sent from a peripheral, to the controller SCK - Serial clock. This line carries a clock signal. nCS - Chip select (active low). This line determines which peripheral should read a message being sent on the CIPO line.

A typical SPI message, from the controller to a peripheral, might look like this: center

Each peripheral may have different settings for SPI, and should be consulted accordingly. That said, most SPI messages take this two-word structure (a word can be any number of bytes, in this case one):

Address Byteb7b6b5b4b3b2b1b0
read/writeaddress bit 6address bit 5address bit 4address bit 3address bit 2address bit 1address bit 0
Data Byteb7b6b5b4b3b2b1b0
data bit 7data bit 6data bit 5data bit 4data bit 3data bit 2data bit 1data bit 0

There are a number of different configurations of SPI, which will be determined in the datasheet. It’s crucial to have your SPI driver configured the same as the device you’re trying to send to, otherwise unpredictable behavior will occur. Often, this means reconfiguring the driver depending on which device you’re sending a message to.

SPI devices are connected as follows. Pullup resistors are needed only on the chip select lines.

SPI wiring

⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’

Excalidraw Data

Text Elements

Controller

COPI

CIPO

Peripheral

COPI

SCK

GPIO Out 1

GPIO Out 2

CIPO

SCK

nCS

Peripheral

COPI

CIPO

SCK

nCS

Link to original