Table of contents
1.
Introduction
2.
What is SPI?
3.
SPI Interface
3.1.
MOSI and MISO
4.
SPI Data Transmission
5.
Clock Polarity and Clock Phase
6.
Advantages and Disadvantages of using SPI
6.1.
Advantages of using SPI
6.2.
Disadvantages of using SPI
7.
Frequently Asked Questions
7.1.
What do you mean by SPI?
7.2.
How does the SPI interface work?
7.3.
What is the maximum speed of SPI?
7.4.
What is the use of SPI and I2C?
8.
Conclusion
Last Updated: Mar 27, 2024

Serial Peripheral Interface (SPI)

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! In this blog, we will learn about SPI, also known as Serial Peripheral Interface. SPI is one of the most extensively used interfaces between microcontrollers and peripheral ICs such as sensors, ADCs, DACs, shift registers, SRAM, and other devices.

What is SPI?

The Serial Peripheral Interface (SPI) is a synchronous serial communication interface specification used for short-distance communication, primarily in embedded systems. It is Motorola designed synchronous serial data protocol that serves as an interface bus that functions in full-duplex mode, allowing data to be delivered and received concurrently.

This article begins with a quick overview of the SPI interface, then moves on to an overview of Analog Devices' SPI enabled switches and muxes and how they might assist minimize the number of digital GPIOs on system boards.

SPI Interface

Source: Analog

Serial Peripheral Interface (SPI) is a synchronous data bus, which means it uses distinct lines for receiving and transmitting data and a clock to maintain both sides in perfect sync and an addressing line.

The main is the device that generates the clock signal. The data sent between the main and the subnode is synced with the main's clock. In comparison to I2C connections, SPI devices support substantially higher clock frequencies. The clock frequency specification of the SPI interface should be found on the product datasheet.

The SPI bus will have four lines in total, which will be used to communicate between the master and peripheral device:

  • MOSI – Master Output, Slave Input
     
  • MISO – Master input, Slave output
     
  • Clock (SPI CLK, SCLK)
     
  • Chip select (CS)

MOSI and MISO

MOSI and MISO's data lines are responsible for conveying data from the master to the slave, while MISO is responsible for transmitting data from the slave to the master. The data lines are MOSI and MISO. Data is sent from the main to the subnode via MOSI, and data is sent from the subnode to the main via MISO.

When the slave needs to respond to the master, the master will create a certain number of clock cycles before the slave sends data across the MISO line.

In SPI, unlike an asynchronous serial, where data is delivered in either direction at any rate, the master knows how much and when data is returned.

SPI Data Transmission

Source: Seeedstudio

An SPI communication will typically start with:

  • The master sends a clock signal through the SS line and selects a slave.
    • The master will transmit a logic 0 signal to pick a slave, as this line is on an active low signal.
    • If a waiting period is required (for example, analog to digital conversion), the master must wait for a certain amount of time before sending the clock signal.
       
  • SPI is a full-duplex interface, which means that both the master and slave can send data to each other at the same time via the MOSI and MISO lines, and data is delivered concurrently between the master and slave. The serial clock will keep everything in sync. For e.g., these two operations will happen at the same time:
    • On the MOSI connection, the master transfers data one bit at a time = Slave Reads it
    • On the MISO line, the slave sends the data one bit at a time to the master = Master reads it
       
  • SPI allows you to choose whether to sample or shift data on the rising or falling edge of the clock.
     
  • Please refer to your device datasheet for the number of data bits to be transmitted, as each device is different.

Also see, Difference Between Verilog and Vhdl

Clock Polarity and Clock Phase

  • There is no data exchange mechanism in SPI, which limits overhead and allows for high speed and data streaming.
     
  • The rising or falling edge of the data clock signal affects whether data is transferred in and out in each SPI mode (known as the clock phase), as well as whether the clock is idle at high or low frequencies (known as Clock polarity).
     
  • Switching the clock polarity (CPOL) and clock phase (CPHA) can form four unique modes to provide flexibility in communication between master and slave.

The four transmission modes are listed in the table below:

Advantages and Disadvantages of using SPI

Advantages of using SPI

  • Full-duplex communication is supported, which means data can be transmitted and received simultaneously.
     
  • Better signal integrity, supporting high-speed applications.
     
  • Only four signal lines are required for the hardware connection.
     
  • The transceiver is not required.
     
  • There is no need for the slave device to address.

Disadvantages of using SPI

  • Short transmission distance.
     
  • Unlike I2C, no flow control is provided, and no acknowledgment mechanism confirms data receipt.
     
  • The number of devices is approaching the practical limit of Pin ports.
     
  • Unlike UART, there is no error check (using parity bit).
     
  • Only additional signal lines can be used for interrupt operation.
     
  • Only one master.

Frequently Asked Questions

What do you mean by SPI?

A serial peripheral interface (SPI) is an interface that allows two devices, one termed as master and the other called a slave, to exchange data serially (one bit at a time). In full duplex mode, an SPI functions. This indicates that data can be sent in both directions at the same time.
 

How does the SPI interface work?

The MOSI and MISO lines allow both the main and subnode to transfer data simultaneously, making SPI a full-duplex interface. Data is concurrently broadcast (shifted serially onto the MOSI/SDO bus) and received (the data on the bus (MISO/SDI) is sampled or read in) during SPI communication.
 

What is the maximum speed of SPI?

The SPI bus can carry data at high speeds of up to 60 Mbps over short distances, such as between chips on a board.
 

What is the use of SPI and I2C?

I2C is a half-duplex communication, while SPI is a full-duplex communication. I2C supports multiple masters and slaves, but SPI only supports one master. SPI is a four-wire protocol, while I2C is a two-wire protocol. I2C supports clock stretching but not by SPI.

Conclusion

In this article, we have extensively discussed the Serial Peripheral Interface (SPI). This synchronous serial data protocol serves as an interface bus that functions in full-duplex mode, allowing data to be delivered and received concurrently.

We hope that this blog has helped you enhance your knowledge regarding Serial Peripheral Interface (SPI).

After reading about the Serial Peripheral Interface, are you not feeling excited to read/explore more articles on this topic? Don't worry; Coding Ninjas has you covered. To learn, see Operating SystemUnix File SystemFile System Routing, and File Input/Output.

Recommended Reading: 

What is arduino uno

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass