Serial Peripheral Interface (SPI)

Pros: Easy to set up, fast, supports multiple devices on a single bus, allows two-way communication

Cons: When SPI is sending data to a device, no other device can receive or send data. When communicating with a slow device (one with low read capability), like an SD card for example, then the speed of the communication can slow down considerably.

General Information: SPI

SPI allows data to be exchanged one byte at a time between the microcontroller and a module via two communication lines. This is known as a Master/Slave protocol, which means that there is always a single master device which controls the flow of communication with one or more slave devices. Think of the master as a traffic cop. It directs all of the connected slave devices so they know when it's their turn to communicate.

The SPI protocol requires a minimum of three signal connections (at least three pins), and usually has four, in addition to the power connections. The following diagram shows the connections (arrows indicate the flow of data). The red lines constitute the shared bus connections used for talking to the slave devices. The green wire is the shared bus connection used by the slaves to talk to the master. The blue line is the chip selected for signaling each slave individually


SPI Signal Connections


SCK

This is the pin for the serial clock output, the clock signal that keeps the device and the module synchronized while transferring data. The two devices need to have a mutual understanding of how fast data is to be transferred between them. This is sometimes referred to as the baud or bitrate. The clock signal provides the reference signal the devices use when exchanging data.

Without a clock signal to synchronize the devices, the devices would have no way of interpreting the signal on the data lines.

One bit of data is transferred with each clock cycle (see the diagram below).

MOSI

MOSI stands for Master Out, Slave In. MOSI is the connection used to send data to a slave device.

MISO

MISO stands for Master In, Slave Out. This is the connection used by the selected slave device to send data to the master device.

CS or SS

This line, normally referred to as the Chip Select (CS) or Slave Select (SS) line, is used by the master device to notify a specific Slave device that it is about to send data. We normally call it CS, but you may see it either way in datasheets and other references.

The following diagram shows how the various pins in the SPI protocol are toggled to create meaningful data. In this case, the master sends the ASCII character 'S', and the slave responds with 'F'.


Timing diagram of SPI data exchange. Modified image from Sparkfun, used under the Creative Commons license (CC BY-NC-SA 3.0).


Remember that the master initiates all communication. When it is ready, the first thing it will do is pull the CS/SS pin low to let the slave device know that a data transmission is about to begin. The master holds this pin low for the duration of the data exchange, as seen in the diagram above.

With the CS/SS pin low, the master will start to toggle the clock pin (SCK) while simultaneously controlling the MOSI to represent the bits of information it wishes to send to the slave. The numbers in green on the diagram above delineate each bit in the byte being transferred.


Last modified: Tuesday, September 27, 2022, 3:06 PM