SPI communication protocol basics

Publisher:创意航海Latest update time:2023-05-16 Source: zhihuKeywords:SPI Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

introduction

When you connect a microcontroller to a sensor, display, or other module, have you considered how the two devices communicate with each other? What are they talking about? How do they understand each other?

Communication between electronic devices is like communication between humans. Both parties need to speak the same language. In electronics, these languages ​​are called communication protocols. Luckily for us, we only need to know a few communication protocols when building most DIY electronics projects.

First, we'll start with some basic concepts about electronic communications, then detail how SPI works.

SPI, I2C, and UART are much slower than protocols like USB, Ethernet, Bluetooth, and WiFi, but they are simpler and use fewer hardware and system resources. SPI, I2C and UART are ideal for communication between microcontrollers and between microcontrollers and sensors that do not require the transmission of large amounts of high-speed data.

text

Serial and parallel communications

Electronic devices communicate with each other by sending bits of data through wires that are physically connected between the devices. A bit like the letters in a word, except instead of 26 letters (the letters in the English alphabet), the bits are binary and can only be 1 or 0. Bits are transferred from one device to another through rapid changes in voltage. In a system operating at 5 V, bit 0 communicates as a short pulse of 0 V, while bit 1 communicates as a short pulse of 5 V.

Data bits can be transmitted in parallel or serial form. In parallel communication, data bits are sent all at the same time over a single wire. The image below shows the parallel transmission of the letter "C" in binary (01000011):



In serial communication, bits are sent one by one over a single wire. The image below shows the serial transmission of the letter "C" in binary (01000011):



Introduction to SPI communication

SPI is a common communication protocol used by many different devices. For example, SD card modules, RFID card reader modules, and 2.4 GHz wireless transmitter/receivers all use SPI to communicate with microcontrollers.

A unique advantage of SPI is that it can transfer data without interruption. Any number of bits can be sent or received in a continuous stream. Using I2C and UART, data is sent in packets, limited to a specific number of bits. Start and stop conditions define the beginning and end of each packet, so data is interrupted during transmission.

Devices communicating via SPI are in a master-slave relationship. The master device is the controlling device (usually a microcontroller), while the slave devices (usually sensors, displays, or memory chips) get instructions from the master device. The simplest configuration of SPI is a single master, one slave system, but one master can control multiple slaves (more on this below).



  • MOSI (Master Out/Slave In) – The line on which the master sends data to the slave.

  • MISO (Master In/Slave Out) – The line on which the slave sends data to the master.

  • SCLK (clock) – clock signal line.

  • SS/CS (Slave Select/Chip Select) – The line used by the master to select the slave to which it sends data.



In practice, the number of slaves is limited by the system load capacitance, which reduces the master's ability to accurately switch between voltage levels.

How does SPI work?

clock

The clock signal synchronizes the master's data bit output to the slave's bit sampling. One bit of data is transferred per clock cycle, so the speed of data transfer depends on the frequency of the clock signal. SPI communication is always initiated by the host since the host configures and generates the clock signal.

Any communication protocol in which devices share a clock signal is called synchronization. SPI is a synchronous communication protocol. There are also asynchronous methods that do not use clock signals. For example, in UART communication, both sides are set to a preconfigured baud rate, which determines the speed and timing of data transfer.

Clock signals in SPI can be modified using the properties of clock polarity and clock phase. Together, these two properties define when a bit is output and when it is sampled. The host can set the clock polarity to allow bits to be output and sampled on the rising or falling edge of the clock cycle. The clock phase can be set so that the output and sample occur on the first or second edge of the clock cycle, regardless of whether it is rising or falling.

Slave selection

The master can select a slave to communicate with by setting the slave's CS/SS lines to a low voltage level. In the idle, non-transmitting state, the slave select line remains at a high voltage level. There may be multiple CS/SS pins on the master, which allows multiple slaves to be connected in parallel. If only one CS/SS pin is present, multiple slaves can be daisy chained to the master.

multiple slaves

The SPI can be set up to run with a single master and a single slave, and can be set up with multiple slaves controlled by a single master. There are two ways to connect multiple slaves to the master. If the master has multiple slave select pins, the slaves can be connected in parallel as follows:



If only one slave select pin is available, slaves can be daisy-chained via:



MOSI and MISO

The master sends data to the slave serially over the MOSI line. The slave receives the data sent by the master through the MOSI pin. Data sent from the master to the slave is usually sent most significant bit first.

The slave can also send data back to the master serially over the MISO line. Data sent back to the master from the slave is usually sent least significant bit first.

SPI data transfer steps

  1. Host output clock signal:


  1. The master switches the SS/CS pin to a low voltage state, thus activating the slave:



  1. The master sends data to the slave one at a time along the MOSI line. The slave reads the received bits:



  1. If a response is required, the slave returns data one bit at a time along the MISO line to the master. The host reads the received bits:



Advantages and Disadvantages of SPI

There are some advantages and disadvantages to using SPI, and if you are choosing between different communication protocols, you should know when to use SPI based on your project requirements:

advantage

  • There are no start and stop bits, so data can stream continuously without interruption

  • No complex 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 simultaneously

shortcoming

  • Use four wires (I2C and UART use two wires)

  • No acknowledgment that data has been received successfully (I2C has this feature)

  • Doesn't have any form of error checking like parity bits in UART

  • Only one host allowed


Keywords:SPI Reference address:SPI communication protocol basics

Previous article:What is a microcontroller? What is a microcontroller?
Next article:How does MCU run programs on extended SDRAM?

Recommended ReadingLatest update time:2024-11-15 07:21

51 MCU - SPI, DS1302 clock C language programming
SPI: Write timing:         At the rising edge of the next SCLK clock after the control instruction word is input, the data is written into the DS1302, and the data input starts from the low bit (bit 0). (Write the low bit first) Read timing:         The data of DS1302 is read out at the falling edge of the next SCLK
[Microcontroller]
51 MCU - SPI, DS1302 clock C language programming
SPI_FLASH timing description and driver programming
Ⅰ. Write in front The previous article talked about SPI drivers (hardware SPI and software simulated SPI). This article follows that article to talk about in SPI applications.   There are two purposes for writing this article: 1. To let everyone know the importance of SPI in practical application development; 2. To l
[Microcontroller]
SPI_FLASH timing description and driver programming
SPI clock of STM32
1) fPCLK is not the CPU frequency, but the frequency of the peripheral bus. 2) The fastest SPI clock of STM32 is 18MHz, which is determined when the chip is designed. 3) STM32's SPI1 is on APB2, SPI2 and SPI3 are on APB1, the highest frequency of APB1 is 36MHz, and the highest frequency of APB2 is 72MHz; therefor
[Microcontroller]
ARM7 Getting Started 12, SPI Communication
The digital tube displays 1-F. Main program: /******************************************************************************* *File: Main.c *Function: The LED digital tube displays characters 0-F, and controls 4 LEDs to display the corresponding hexadecimal ****************************************************
[Microcontroller]
ARM7 Getting Started 12, SPI Communication
STM8S hardware SPI driver 74HC595
Introduction: I have always been interested in the hardware SPI of STM8S, but I have never really used it. In the past, I used IO port simulation. This time, there happened to be a board with two 595-driven 8-bit LED digital tubes, so I tried it on it and recorded the process. Hardware wiring diagram: 595 is a dat
[Microcontroller]
STM8S hardware SPI driver 74HC595
STM32 as SPI slave usage routine
The SPI of STM32 can be used as a master or a slave. There are relatively few routines for using it as a slave. After several hours of tossing today, I finally got the data out, and it was verified to be consistent with the metadata. The source code is now posted here for reference by those who need it.   Hardware Con
[Microcontroller]
SPI device connection under ARM platform embedded Linux
Introduction: On embedded ARM platform devices, there is often a need to mount one or more SPI devices on a SPI bus. Since mounting one device is relatively simple, this article mainly explains how to mount multiple SPI devices in an embedded Linux environment. The hardware used in this article is the Toradex Colibr
[Microcontroller]
STM32 SPI slave DMA routine
#include "stm32f10x.h" /*RCC clock configuration*/ void RCC_config(void) {  ErrorStatus HSEStartUpStatus; /* RCC registers are set to default configuration */ RCC_DeInit(); /* Turn on external high speed clock */ RCC_HSEConfig(RCC_HSE_ON); /* Wait for the external high-speed clock to stabilize*/ HSEStartUpStatus
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号