The understanding of SPI

Craig Shaw
4 min readMar 31, 2019

The Serial Peripheral Interface is also known as SPI, which is a synchronous serial communication interface specification, primarily used in embedded systems. First developed by Motorola in the mid-1980s, and gradually become a de facto standard.

If you don’t familiar with the concept of serial communication. Please refer to another post of mine help you quickly to understand serial communication.

To get back to the point, the first thing you need to know with SPI is the role setting of communication. Two devices involved in communication, one set to master and one set to slave.

As the name implies, the two parties involved in SPI communication are not equal, only the SPI master device can initiate communication. The SPI master device informs the slave device that communication is about to begin by controlling the level of the chip select pin. (aka cs or ss)

The next important step is how to synchronize the data. (For serial communication data synchronization, please refer to another post of mine) SPI adopts synchronous communication mode, that is, the communication parties share one clock signal line and exchange data according to the same signal frequency.

It should be noted that the clock signal is provided by the SPI master device and is not output until the slice selection signal has been output for a period of time.

Finally, let’s talk about how the SPI protocol interacts with data. We will discuss this issue from two perspectives.

If you are using a high-level SPI driver APIs, you need to know that either read or write is initiated by the SPI master device. If you need to control the master device, you have to invoke the write function manually to send data, invoke the read function to get the data from the slave device. On the other hand, if you control the slave, you could receive the data sent by the master. If you need to send data to the master, you need to put the sent packet into the specific memory buffer or some register, waiting for the master to fetch.

If your work is very close to the underlying hardware, then you will access to the key parts of SPI data communication, ‘swap’.

Logically, when the master device sends data to the slave, it also receives the data sent from the slave, exchanging 1 bit of data at the same time.

From the hardware point of view, there are two data lines between the master and slave devices, one of which is from the master to the slave, and the other is the opposite. The data exchange is performed simultaneously on the two wires.

That is the main content of SPI communication, then let’s talk about some points in the practice.

  • First, the SPI provides two free options, clock polarity and clock phase. In a word, the master and the slave are configured to be the same.
  • Second. in order to control multiple slaves from one master, you need to assign a chip select line to each slave.

--

--