Serial Peripheral Interface (SPI)

The Serial Peripheral Interface (SPI) is a synchronous serial communication interface used for short-distance communication, primarily in embedded systems. It was originally developed by Motorola.

What is SPI?

The Serial Peripheral Interface (SPI) is a synchronous, full-duplex, master-slave communication protocol used for short-distance communication between microcontrollers and peripherals (like Sensors, Displays, and Flash memory).

It is known as a 4-wire interface because it typically uses four distinctive lines: Clock (SCLK), Master Out (MOSI), Master In (MISO), and Chip Select (CS).

Why SPI? - History & Simplicity

SPI was developed by Motorola in the mid-1980s. Before SPI, connecting a simple sensor to a microcontroller often required parallel buses (8+ wires) or complex protocols.

The Need for Speed

  • Simplicity: Motorola wanted a protocol that was essentially a "circular shift register." Data goes out, data comes in. Minimal logic required.
  • Speed: Unlike I2C (which is limited by pull-up resistors), SPI uses push-pull drivers, allowing it to run at very high speeds (tens of MHz).
  • No Addressing Overhead: By using a Chip Select (CS) line, SPI avoids the need to send address bits, making transactions instant.

It is strictly a "Master-Slave" protocol where the master initiates communication. Its simplicity makes it the de-facto standard for SD Cards, LCD displays, and Flash memory.

Signal Description

4-Wire Interface

SPI is often referred to as a "4-wire" bus, though "3-wire" variants exist.

The standard four signals are:

  • SCLK (Serial Clock): Generated by the Master. Synchronizes data transmission.
  • MOSI (Master Out Slave In): Data line from Master to Slave.
  • MISO (Master In Slave Out): Data line from Slave to Master.
  • SS/CS (Slave Select / Chip Select): Active Low signal ensuring only one slave is active at a time.

Typical Connection Diagram

SPI Master-Slave Connection

      MASTER                     SLAVE
  +-----------------------------+             +-----------------------------+
  |            |             |            |
  |       SCLK +----------------------------->+ SCLK       |
  |            |             |            |
  |       MOSI +----------------------------->+ MOSI       |
  |            |             |            |
  |       MISO +<-----------------------------+ MISO       |
  |            |             |            |
  |         CS +----------------------------->+ CS         |
  +-----------------------------+             +-----------------------------+
                            

Pros & Cons

  • Advantages:
    • Full-duplex communication (send and receive at same time).
    • Higher throughput than I2C or UART.
    • Simple hardware implementation (shift registers).
    • No complex protocol overhead (start/stop bits, addressing).
  • ❌ Disadvantages:
    • Requires more pins (4 wires) than I2C (2 wires).
    • No built-in acknowledgement (Master doesn't know if Slave exists).
    • No standardized error checking.
    • Only one master allowed.