Introduction of SPI Communication Protocol
SPI is a very popular serial bus communication protocol. SPI stands for the serial peripheral interface. It is a Synchronous serial communication protocol that is based on master and slave. In the SPI protocol, communication is always started by the master to put the slave-select line low.
The SPI Interface was developed by Motorola in late 1980 and it is the most popular serial synchronous bus protocol for short-distance communication.
Sometimes, SPI is called a four-wire serial bus and each bus has a specific role and importance. SPI works in full-duplex mode, which means it can receive and send data at a time.
What is SPI?
SPI is a serial communication protocol. It is a master and slave base protocol and communication is always started by the master. It is a Synchronous communication protocol because the clock is shared by the master and slave and clock is provided by the master.
The SPI can be multi-slave but it cannot be multi-master that means in SPI there must be only one master which control the all communication event. In the case of the multi-slave, the master selects the slave using the slave select line to putting the line low.
The serial peripheral interface is four wire-based protocol these wire generally known as MOSI (master out slave in), MISO (master in slave out), SCL (serial clock which produces by the master) and SS (slave select line which use to select specific slave during the communication).
In the serial peripheral interface, data is shifted out from the master and shifted into the master through the shift register and the SPI device communicates in the full-duplex mode so data send-receive at a time.
Why use SPI?
Unlike the serial com port, SPI is the synchronous communication protocol. In SPI master and slave both shared the same clock and clock is produced by the master. In the case of asynchronous serial communication, every byte consists of a start and stop bits which create extra overhead on communication.
Another disadvantage of serial com port communication is that it needs to maintain clock frequency of the transmitter and receiver ( both not shared the clock). If the frequency of the transmitter and receiver does not match then the receiver will not receive the data transmitted by the transmitter.
SPI (Serial peripheral interface) Protocol
SPI is a single master full-duplex communication protocol that means communication always starts by the master. In SPI communication multi-slave can be attached with a single master and slave cannot change its role to master.
Each slave has its own slave select pin which is controlled by the master. In the case of multi-slave, the master selects the slave by pulling down its slave select line (ss). There is four-wire is used in communication these are MOSI, MISO, SCLK, and SS.
SCLK: Serial Clock (It is produced by the master to start the communication)
MOSI: Master Out, Slave In (This line is used to carry data from the master to the slave)
MISO: Master in, Slave out (This line is used to carry the data from the slave to the master)
SS: Slave Select (This line is used to select the slave in case of the multi-slave communication)
Following steps for the SPI communication
Step1: Master pulling down the slave select line of a slave that it wants to communicate.
Step2: After selecting the slave master start to generates the clock signal which is shared by the slave. The clock configuration (polarity and phase) of the master and slave should be the same.
Step3: Now master generates information on the MOSI line and samples the received data at the MISO line at the same time.
The SPI bus is a de facto standard. However, the lack of a formal standard is reflected in a wide variety of protocol options. Every device defines its own protocol, including whether it supports commands at all. Some devices are transmit-only or others are receive-only.
How SPI Works?
Each time the master drives a pulse on the clock line and one bit is transferred in each direction. The MOSI line sends out a bit, while the MISO line receives a bit. While this means that the amount of data sent and the amount of data received must be equal, it’s trivial to provide dummy data when you don’t have anything interesting to send.
In SPI protocol one rule should be common for the slave and master. The data line should be sync to each other for example if MOSI line sample at the rising edge so MISO line should be a sample at the rising edge.
SPI communication data is driven in 4 modes and the mode decided by the combination of clock polarity (CPOL) and clock phase (CPHA).
Mode | CPOL | CPHA |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
2 | 1 | 0 |
3 | 1 | 1 |
Pros and cons:
Advantages:
- There is no start and stop bits, so the data can be streamed continuously without interruption.
- It supports full-duplex.
- No need for precision oscillators in slave devices as it uses a master’s clock.
- No complicated slave addressing system like I2C.
- Higher data transfer rate than I2C (almost twice as fast).
- Separate MISO and MOSI lines, so data can be sent and received at the same time.
- Simple software implementation.
Disadvantages:
- If there is more than one slave in communication then the wiring will be complex.
- Uses four wires (I2C and UARTs use two).
- No acknowledgment that the data has been successfully received (I2C has this).
- No form of error checking like the parity bit in UART.
- It only allows for a single master.
Recommended Post
- I2C vs SPI.
- Can protocol interview Questions.
- 100 C interview Questions.
- I2C interview Questions
- Interview questions on bitwise operators in C
- C++ Interview Questions.
- 10 questions about dynamic memory allocation.
- File handling in C.
- Pointer in C.
- C format specifiers.