STM32 Relearning - SPI in the Eyes of Engineers

Publisher:平和的心情Latest update time:2014-10-23 Source: 21icKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
A few days ago, a netizen talked about implementing SPI communication through FPGA. Through the replies to the post, I found that many netizens are still confused about SPI communication, so today I will take you to fully understand this complex yet simple communication protocol from the SPI standard protocol, SPI configuration on the STM32 microcontroller and the example of SPI communication on the 74HC595 logic chip.

SPI is the abbreviation of Serial Peripheral Interface, which literally means serial peripheral interface. SPI is a synchronous serial communication method launched by Motorola. It is a four-wire synchronous bus. Because of its powerful hardware functions, the software related to SPI is quite simple, which allows the MCU to have more time to handle other matters. It should be noted here that patents are still very important in the electronics industry. Therefore, some other manufacturers renamed the SPI communication protocol to avoid high patent fees, but the hardware processing method is the same, just with a different name, such as SSI communication in Texas Instruments microcontrollers.

The commonly used SPI communication method is the standard four-wire system, as shown in the following circuit diagram:

1.jpg

MISO: Master input/slave output pin. This pin sends data in slave mode and receives data in master mode.

MOSI: Master output/slave input pin. This pin sends data in master mode and receives data in slave mode.

SCK: serial port clock, output of the master device and input of the slave device

NSS: Slave Select. This is an optional pin used to select the master/slave device.

The MOSI pins are connected to each other, and the MISO pins are connected to each other. In this way, data is transferred serially between the master and the slave (MSB first). Communication is always initiated by the master device. The master device sends data to the slave device through the MOSI pin, and the slave device returns the data through the MISO pin. This means that the data output and data input of full-duplex communication are synchronized with the same clock signal; the clock signal is provided by the master device through the SCK pin.

The more complicated part is the slave select (NSS) pin, which has two modes: software NSS mode and hardware NSS mode.

Software NSS mode: In this mode, the pin is used as a normal GPIO. Its input/output function is the same as that of GPIO. We often use this mode when operating external devices through STM32.

In hardware NSS mode: This mode is divided into two cases: Case 1, NSS output is enabled: When STM32 works as the master SPI and NSS output is enabled, the NSS pin is pulled low, and all NSS pins are connected to the NSS pin of the master SPI and configured as hardware NSS SPI devices, which will automatically become slave SPI devices; Case 2, NSS output is turned off: allowing operation in a multi-master environment.

Now that we have finished talking about the hardware connection, let me introduce the clock line and signal line.

When learning digital logic circuits, we have all heard teachers talk about data latching methods, such as rising edge latching, etc. Our SPI communication method handles data latching methods very flexibly in hardware, and provides four different data transmission modes through the configuration of two parameters, as shown in the figure below:

2.jpg
3.jpg

From the above figure, we can see that when CPHA is set high, the data is locked at the second clock edge; when CPHA is cleared, the data is locked at the first clock edge. When the CPOL parameter is set high, the data is locked at the falling edge of the clock signal, and the clock line idle state is always high. Conversely, the data is locked at the rising edge of the clock signal, and the idle state is always low.

For the data transmission process, the frame format can also be modified. For example, you can choose the MSB mode (the most significant bit is sent first) or the LSB mode (the least significant bit is sent first). You can also choose to insert a CRC check, etc. These advanced applications will not be explained in detail here due to the limited space of this article.

Next, let's take a look at the SPI hardware standard through the STM32 microcontroller's initialization process for the SPI peripheral.

void SPI_init(void)

{

RCC_APB2PeriphClockCmd(sFLASH_CS_GPIO_CLK | sFLASH_SPI_MOSI_GPIO_CLK | sFLASH_SPI_MISO_GPIO_CLK |

sFLASH_SPI_SCK_GPIO_CLK, ENABLE);

/*!< Configure the SPI peripheral clock and enable it*/

RCC_APB2PeriphClockCmd(sFLASH_SPI_CLK, ENABLE);

/*!< Configure SCK pin*/

GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_SCK_PIN;

GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //This is set according to the specific application, for example, it can be configured as open-drain output

GPIO_Init(sFLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);

/*!< Configure MOSI pin*/

GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MOSI_PIN;

GPIO_Init(sFLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);

/*!< Configure MISO pin */

GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MISO_PIN;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(sFLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);

/*!< Configure NSS pin as GPIO output*/

GPIO_InitStructure.GPIO_Pin = sFLASH_CS_PIN;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(sFLASH_CS_GPIO_PORT, &GPIO_InitStructure);

/*!< SPI configuration*/

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //Two data lines, bidirectional full duplex and half duplex

SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //

SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //CPOL is set high, the clock line is always high when idle, and the falling edge latches data

SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //CPHA is set high, the second clock edge latches data

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //Slave pin is software configuration mode

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //SPI clock frequency is divided by 4

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //MSB highest bit is sent first

SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC check formula selects item 7

SPI_Init(sFLASH_SPI, &SPI_InitStructure);

/*!< Enable SPI */

SPI_Cmd(sFLASH_SPI, ENABLE);

}

The source code above is a demo example of ST company operating SPI flash. We will use the hardware operation of 74HC595 chip to configure and initialize the SPI peripheral.

Let's first look at the hardware operation timing diagram of 74HC595:

4.jpg

From the above figure, we can see that the clock line (SH_CP) is always low in the idle state, and the data is latched on the rising edge of the first clock edge. Therefore, we need to modify the two parameters of the above configuration initialization to the following:

SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //CPOL is set high, the clock line is always low when idle, and the rising and falling edges latch data

SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //CPHA is cleared, the first clock edge latches data

The other parameters do not need to be modified. The above source code has been tested by connecting STM32F103 and 8 74HC595 chips in series. The complete engineering source code of the example can be found and downloaded from the Electronic Products World Forum.

The standard four-wire SPI communication not only saves us precious microcontroller pins, but its standardized hardware protocol also provides great convenience for our embedded software programming. Rich peripheral device support, such as SPI flash storage, SPI interface SD card reader, SPI interface network communication module are already very popular. It can be seen that the application of peripheral SPI communication has become one of the necessary skills for an engineer.

Keywords:STM32 Reference address:STM32 Relearning - SPI in the Eyes of Engineers

Previous article:12-bit 4-20mA loop-powered thermocouple measurement system using Cortex-M3
Next article:Remote testing system of oil well liquid level based on embedded cloud technology

Recommended ReadingLatest update time:2024-11-23 11:02

DMA implementation in STM32
  The STM32 series microcontrollers all contain DMA and universal clock TIMx modules. The low-end models only include DMA1, which supports 7 channels; the high-end models also include DMA2, which supports 5 channels. Each of its channels can specify any working mode, such as memory to memory, memory to peripherals, or
[Microcontroller]
Stm32 IAP programming and user programming
Stm32f10x series MCU  Bootloader  process Chip: stm32f103ze Required software: SecureCRT (used to send Application files using the Ymode protocol). In fact, we should write a host computer ourselves. Here, SecureCRT is used to act as our own application (used to verify whether the Bootloader is successful). Keil ver
[Microcontroller]
Stm32 IAP programming and user programming
Freescale synchronous serial transmission SPI optimization design
Most of the Freescale series MCUs have an SPI module, which is a synchronous serial peripheral interface that allows the MCU to communicate with various peripheral devices in a serial manner.  At present, most Freescale series microcontroller buses cannot be expanded externally. When the on-chip I/O or memory cannot
[Microcontroller]
Freescale synchronous serial transmission SPI optimization design
STM32 - interrupt priority
   Interrupts are an important type of system resource, which can play an indispensable role in the operation of hardware. Basically, every hardware will have corresponding interrupts, but it is just a matter of the type and number of interrupts. For example, the hardware of the 51 single-chip microcomputer has relati
[Microcontroller]
stm32-Serial port uses IDLE interrupt to accept variable length data method
Method 1: The serial port receives data, and the timer is used to determine whether the data reception is completed after the timeout. Method 2: DMA accept + IDLE interrupt Implementation idea: Use the serial port 1 of STM32F103, configure it to the idle interrupt IDLE mode and enable DMA reception, and set the re
[Microcontroller]
stm32-Serial port uses IDLE interrupt to accept variable length data method
Solution to stm32 serial port download error
Since my J-LINK was broken and the new one hadn't arrived yet, I used the serial port tool to download. The information prompt box indicates that the download content is not within the range of 0x08000000 and 0x20000000, that is, it is not on the program FLASH or SRAM. However, there is no problem in reading devic
[Microcontroller]
Solution to stm32 serial port download error
Using STM32's timer to output PWM
Chip used: STM32F103ZET6 Purpose: Use timer 3 of stm32 to output PWM Registers used and corresponding bits (refer to the STM32 reference manual): (1) Control register 1 (TIM1_CR1) Bit 0 CEN: counter enable bit; implemented by function void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState). The fourth bit DIR: co
[Microcontroller]
SPI interface design of serial display driver PS7219 and microcontroller
   0 Introduction   In the microcontroller application system, in order to facilitate people to observe and monitor the operation of the microcontroller, it is often necessary to use a monitor to display the intermediate results and status of the operation, etc. Therefore, the display is often one of the indispensabl
[Power Management]
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号