Introduction

The Serial Peripheral Interface (SPI), often pronounced “spy”, is a commonly used serial communication protocol. Serial communication protocols are ones that send bits of data one after another over a single data line, as opposed to using several parallel data lines to send, say, 8 bits at a time.

SPI consists of a master device - an MCU (Micro Controller Unit), for instance - and several slave devices, such as sensors. The master device initiates communication, terminates communication, and sets the data rate for communication.

The SPI Bus

The SPI bus, however, has four lines associated with it.

Untitled

The Clock

The first is a clock line, often denoted with “CLK”, or “SCLK”. Over this line, the “master device” on the SPI bus produces a regular on/off pulse at a set frequency, that looks something like this:

Untitled

This clock signal from the master device connects to all slave devices on the SPI bus, and sets the rate at which bits are sent between the master and slaves. Depending on how the bus is set up, the transmitting device sends a bit every clock period/pulse, and the receiving device may sample (read) that data on every rising edge of the clock pulse, or every falling edge, or while the clock is in the 0 state, or while its in the 1 state.

The Data Lines

Data is sent over two lines. MISO, “Master In, Slave Out” (sometimes referred to as Controller In, Peripheral Out - CIPO), and MOSI, “Master Out, Slave In” (sometimes referred to as Controller Out, Peripheral In - COPI). These terms are pretty self explanatory. MISO/CIPO is the line where the slave device can send information to the master, and MOSI/COPI is the line where the master device can send information to the slave. On the slave device, MOSI is often called SDI (Serial Data In), and MISO is often called SDO (Serial Data Out). Data is sent bit by bit, at a rate set by the clock signal.

The SPI protocol does not define whether data should be sent with the leftmost bit first (Most Significant bit, MSb), or the rightmost bit first (Least Significant bit, LSb). MSb or LSb-first can generally be configured by the devices on the bus, but all slaves and the master must agree on how this communication is performed (obviously, if your slave device expects an LSb and you give it an MSb, you’ll have problems).

A term you may hear commonly is “little endian” and “big endian”. This terminology is simply a reference to the way series of bytes are transmitted. For example, if you have to send a 32-bit number over SPI, you generally need to break it down into 4 bytes. Whether you send the rightmost byte first (Least Significant Byte - LSB), or the leftmost byte first (Most Significant Byte - MSB), is something that your devices also need to agree on. Little endian is a term used to refer to LSB communication, while big endian is a term used for MSB communication.

Chip Select

SPI is a bit unique out of the serial communication protocols in that it is full duplex, meaning the master can both send and receive data at the same time. A lot of other protocols, like I2C, are half duplex, meaning devices on the bus can only either send or receive data at any given moment.

In addition to producing the clock signal, the master device’s job is also to choose which device to talk to. To simplify the electrical interfacing between devices, a SPI bus consists of shared SCLK, MOSI, and MISO lines (imagine if you had 100 devices on your SPI bus - you would need a whole lot of wires if those lines weren’t shared!). This however, comes at the cost of the master device only being able to talk to one device at a time.

In order for the master to indicate which slave device it wants to talk to, a Chip Select (CS) line is used. The bar symbol seen over top of the chip select line means it is “active low”. This means that the CS line is generally always in the HIGH, or “1” state, and is “pulled low” in order to start communication. Chip Select is sometimes also called Slave Select (SS).

The master needs to have one CS line per slave. In order to begin talking to a slave device, the master pulls CS low. Only one CS line can generally be pulled low at a time.

Untitled