Serial Peripheral Interface-SPI Protocol

Serial Peripheral Interface-SPI Protocol


The Serial Peripheral Interface-SPI Protocol was developed by Motorola which is used to communicate between microcontrollers and peripheral devices such as SD cards, Serial LCDs, ADC/DAC ICs, shift registers, etc. Basic connection diagram of SPI is seen in below figure which shows master-slave communication with 4 lines named as MISO(Master-In Slave-Out), MOSI(Master-Out Slave-In), SCLK(Serial clock) and CS(Chip select) .


Purpose of these lines are as follow :

MISO – Master receives the data sent by slave on this line.

MOSI – Master send data to slave by using this line.

SCLK – Provide serial clock to synchronize the master and slave communication.

CS – Chip select line used to choose the device you wish to talk to.

Why SPI Protocol ?

Comparing SPI with UART

UART interface diagram

  • In UART, separate clock line is not used with data transmission so both device should agree on same Baud-rate. If differences occur between Baud-rate on either end will cause garbled data while in SPI separate clock line(SCLK) is used.
  • UART uses hardware overheads like start bit, stop bit, parity bit or CRC bit in each frame of transmission which will lead to increase in transmission time while SPI not use any overheads.
  • Another drawback of UART is that they are suited to communications between two, and only two, devices while SPI can used with multiple slave devices.
  • Data transfer speed is also an issue for UART, they are limited to some extent and maximum baud-rate is around 230400 bps.

Comparing SPI with I2C 

I2C interface diagram

  • SPI have higher data rates than I2C protocol so its suitable where mass data transfer is take place like accessing SD card, flash memory, etc.
  • SPI uses full duplex communication so that master-slave can transmit and receive data on separate lines on the same time.
  • SPI tends to be simpler and more efficient than I2C in point-to-point (single master, single slave) applications for the very same reason; the lack of device addressing means less overhead.

How SPI Protocol Works ?

To understand the working of SPI Protocol, Let we take a practical example

Single Byte Transmit and Receive

SPI-interface-single byte trasnfer

Idle condition

In SPI protocol, communication is always initiated by master by generating clock signal (usually called CLK or SCLK) and pulling the corresponding Chip Select(CS) line to Low for respective slave selection as you can see on above pic. There is always only one master (which is almost always your microcontroller), but there can be multiple slaves.

Send Byte

As we know that SPI uses separate lines for data and a clock that keeps both sides in perfect synchronization. The clock is an oscillating signal that tells the receiver exactly when to sample the bits on the data line. This could be the rising (Low to High) or falling (High to Low) edge of the clock signal depending upon datasheet. Here, we use rising edge. 

               Master puts data on MOSI(Master Out / Slave In) line with clock(SCLK), When the receiver detects the rising edge, it will immediately look at the data line to read the next bit (see the arrows in the above pic). 

Receive Byte

When data is sent from the master to a slave, it’s sent on a data line called MOSI(Master Out / Slave In). If the slave needs to send a response back to the master,  slave will put the data onto a third data line called MISO(Master In / Slave Out) and master continue to generate clock. And same principle is used to detect the data bits as we studied above(on rising edge).

Speed of SPI Protocol

If data must be transferred at ‘high speed’, SPI is clearly the protocol of choice, over I²C.  SPI does not define any speed limit; implementations often go over 10 Mbps. 

Multiple Slave Connection Configuration

There are two ways of connecting multiple slave devices with SPI protocol:

Each slave has separate Chip Select(CS) line

Multiple slave with each CS separate

  • In this configuration, each slave will have a separate CS line. To talk to a particular slave, you’ll make that slave’s CS line low and keep the rest of them high. Drawback of this kind of configuration is lots of slaves will require lots of SS lines, there are binary decoder chips that can use to multiply your CS lines.
  • Practical use of this kind of configuration is when there is less number of slave to talk with.

Daisy-chained slave configuration

Daisy chain slave configuration

  • In this configuration, MISO (output) of one slave going to the MOSI (input) of the next slave as you can see. In this case, a single CS line goes to all the slaves. Once all the data is sent, the CS line is raised, which causes all the slaves to be activated simultaneously.
  • This kind of configuration mostly used when there is output-only situations, such as driving LEDs or shift registers where you don’t need to receive any data back. We can also receive data back in this configuration but that would be worst case scenario. 

 Suggested Reading

  1. I2C Protocol
  2. Difference between UART and USART
  3. Difference between RS-232, RS-422 and RS-485