Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is STM32 SPI?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what STM32 SPI is, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!

1. Introduction to SPI

SPI stipulates that the communication between two SPI devices must be controlled by the primary device (Master) and the secondary device (Slave). A Master device can control multiple Slave devices by providing Clock and Slave Select of Slave devices. SPI protocol also stipulates that the Clock of Slave devices is provided to Slave devices by Master devices through SCK pins, Slave devices themselves can not generate or control Clock, and Slave devices cannot work properly without Clock.

2. SPI features 2.1and SPI control mode.

The master-slave mode (Master-Slave) is adopted.

SPI stipulates that the communication between two SPI devices must be controlled by the primary device (Master) and the secondary device (Slave). A Master device can control multiple Slave devices by providing Clock and Slave Select of Slave devices. SPI protocol also stipulates that the Clock of Slave devices is provided to Slave devices by Master devices through SCK pins, Slave devices themselves can not generate or control Clock, and Slave devices cannot work properly without Clock.

2.2, SPI transmission mode

Use synchronous mode (Synchronous) to transmit data

Master devices will generate corresponding clock pulses (Clock Pulse) according to the data to be exchanged. Clock pulses form a clock signal (Clock Signal). The clock signal controls when data is exchanged and when to sample the received data between two SPI devices through clock polarity (CPOL) and clock phase (CPHA) to ensure that the data is transmitted synchronously between the two devices.

2.3.The SPI data exchange

Block diagram of SPI data exchange

The figure above is just a simple description of communication between SPI devices. Let's explain some of the components (Module) shown in the figure:

SSPBUF,Synchronous Serial Port Buffer, which generally refers to the internal buffer in a SPI device, is generally in the form of FIFO to store temporary data during transmission.

SSPSR, Synchronous Serial Port Register, generally refers to the shift register (Shift Regitser) in a SPI device, which is used to move data into or out of the SSPBUF according to the set data bit width (bit-width).

Controller, which generally refers to the control registers in SPI devices, can be configured to set the transmission mode of the SPI bus.

Data transmission between SPI devices is also called data exchange because the SPI protocol stipulates that a SPI device can not only act as a "Transmitter" or "Receiver" in the process of data communication. In each Clock cycle, the SPI device sends and receives a bit-sized data, which means that the device has a bit-sized data exchanged. In order to receive the control signal from Master, a Slave device must be able to be Access by the Master device before that. Therefore, Master devices must first select Slave devices through SS/CS pin and select the Slave devices they want to access. In the process of data transmission, each received data must be sampled before the next data transmission. If the previously received data is not read, the received data may be discarded, resulting in the final failure of the SPI physical module. Therefore, in the program, it is common to read the data in the SPI device after the SPI has transferred the data, even if the data (Dummy Data) is useless in our program.

2.4.The SPI transmission mode

Rising edge, falling edge, leading edge, trailing edge trigger. Of course, there are also MSB and LSB transmission methods.

3. Working mechanism 3.1, related abbreviations

The polarity Polarity and phase Phase of SPI are most commonly written in CPOL and CPHA, but there are some other ways to write them, which are briefly summarized as follows:

(1) CKPOL (Clock Polarity) = CPOL = POL = Polarity = (clock) polarity

CKPHA (Clock Phase) = CPHA = PHA = Phase = (clock) phase

(3) clock of SCK=SCLK=SPI

(4) Edge= edge, that is, the moment when the clock level changes, that is, the rising edge (rising edge) or the falling edge (falling edge)

For a clock cycle, there are two edge, called:

Leading edge= front edge = first edge, for the start voltage is 1, then 1 becomes 0, for the start voltage is 0, then 0 becomes 1

The last edge of the Trailing edge= = the second edge, for the starting voltage is 1, then 0 becomes 1 (that is, after the first 1 becomes 0, the latter 0 becomes 1), for the starting voltage is 0, then 1 becomes 0.

3.2Polarity of CPOL

First of all, what is the idle time of the SCLK clock, which is the state of the SCLK before and after sending 8 bit bits of data, and accordingly, when the SCLK is sending the data, it is the time of normal operation, the time of effective active.

First of all, in English, the concise interpretation is: Clock Polarity = IDLE state of SCK.

Then explain in detail in Chinese:

The CPOL of the SPI, indicating whether the level of the SCLK is low level 0 or high level 1 when the SCLK is idle idle:

CPOL=0, when the clock is idle idle, the level is low, so when the SCLK is effective, it is the high level, which is called active-high.

CPOL=1, when the clock is idle idle, the level is high, so when SCLK is effective, it is low level, which is called active-low.

3. 3. CPHA phase

First of all, to make it clear that capture strobe = latch = read = sample, all represent the time when the data is sampled and the data is valid. Phase, which corresponds to the edge on which the data is sampled, the first edge or the second edge, 0 corresponds to the first edge and 1 corresponds to the second edge.

For:

CPHA=0, which represents the first edge:

For CPOL=0,idle, it is a low level, and the first edge is from low to high, so it is the rising edge.

For CPOL=1,idle, it is high level, and the first edge is from high to low, so it is the falling edge.

CPHA=1, which represents the second edge:

For CPOL=0,idle, it is a low level, and the second edge is from high to low, so it is a falling edge.

3.4, polarity and phase diagram

Legend 1

Legend 2

3.5. Software sets polarity and phase

SPI is divided into master device and slave device, both of which communicate through SPI protocol.

The mode of setting SPI is the mode of the slave device, which determines the mode of the master device.

So it is necessary to find out what the SPI mode of the slave device is, and then set the SPI mode of the master device to the same mode as the slave device, and then communicate normally.

There are two modes for the SPI of the slave device:

1. Fixed, determined by SPI slave device hardware

SPI will find a description from the device and the specific mode in the relevant datasheet. You need to find the relevant description in the datasheet, that is:

With regard to the SPI slave device, whether it is high or low when idle, that is, it determines whether the CPOL is 0 or 1.

Then find out whether the device is desampling data on the rising edge or falling edge, that is, under the premise of setting the value of CPOL, it is possible to calculate whether the CPHA is 0 or 1.

two。 Configurable, set by the software itself

The slave device is also a SPI controller, and all four modes are supported, as long as you set it to a certain mode.

Then, after knowing the mode of the slave device, you can set the SPI master device mode to the same as the slave device mode.

As for how to configure CPOL and CPHA of SPI, most of them directly write the two bits of CPOL and CPHA in the corresponding register in the corresponding SPI controller, and write 0 or 1.

4. SPI control module of STM32

SPI is the abbreviation of English Serial Peripheral interface, as the name implies, is the serial peripheral interface. It was first defined by Motorola on its MC68HCXX family of processors. SPI interface is mainly used in EEPROM,FLASH, real-time clock, AD converter, digital signal processor and digital signal decoder. SPI is a high-speed, full-duplex, synchronous communication bus, and occupies only four lines on the pins of the chip, saving the pins of the chip, while saving space and providing convenience for the layout of PCB. Because of this easy-to-use feature, more and more chips integrate this communication protocol, and STM32 also has SPI interface.

SPI interfaces generally use four lines to communicate:

MISO master device data input, slave device data output.

MOSI master device data output, slave device data input.

SCLK clock signal, generated by the main device.

CS selects signals from the device chip and is controlled by the master device.

The main features of SPI are: it can send and receive serial data at the same time; it can work as a host or slave; provide frequency programmable clock; send end interrupt flag; write conflict protection; bus competition protection and so on.

STM32's SPI function is very powerful, SPI clock can be up to 18Mhz, support DMA, can be configured as SPI protocol or I2S protocol

About SPI, look up from the data manual

There are 3 SPI in STM32F207VCT6.

Find out the corresponding pins for each SPI from the following table

The definition of STM32 Standard Peripheral Library SPI_InitTypeDef

Typedef struct {uint16_t SPI_Direction; / / sets the communication mode of SPI. You can choose half-duplex, full-duplex, serial transmit and serial receive uint16_t SPI_Mode; / / set SPI master-slave mode uint16_t SPI_DataSize; / / 8-bit or 16-bit frame format option uint16_t SPI_CPOL; / / set clock polarity uint16_t SPI_CPHA / / set the clock phase uint16_t SPI_NSS; / / set the NSS signal by hardware (NSS pin) or software control uint16_t SPI_BaudRatePrescaler; / / set the SPI baud rate pre-division value uint16_t SPI_FirstBit; / / set the data transmission order is MSB bit before or LSB bit before uint16_t SPI_CRCPolynomial; / / set CRC check polynomial to improve communication reliability, greater than 1 can} SPI_InitTypeDef

The first parameter SPI_Direction is used to set the communication mode of SPI, which can be selected as half-duplex, full-duplex, serial transmit and serial receive. Here we choose full-duplex mode SPI_Direction_2Lines_FullDuplex.

The second parameter, SPI_Mode, is used to set the master-slave mode of SPI. Here we set it to host mode SPI_Mode_Master. Of course, you can also choose slave mode SPI_Mode_Slave if necessary.

The third parameter SPI_DataSiz is 8-bit or 16-bit frame format option, here we are 8-bit transmission, select SPI_DataSize_8b.

The fourth parameter, SPI_CPOL, is used to set the clock polarity. We set the idle state of the serial synchronous clock to high, so we choose SPI_CPOL_High.

The fifth parameter SPI_CPHA is used to set the clock phase, that is, the data is sampled at the jump edge (rising or falling) of the serial synchronous clock, which can be collected for the first or second edge. Here we choose the second jump edge, so select SPI_CPHA_2Edge.

The sixth parameter SPI_NSS sets whether the NSS signal is controlled by hardware (NSS pin) or software. Here we control the NSS key through software rather than hardware automatic control, so choose SPI_NSS_Soft.

The seventh parameter, SPI_BaudRatePrescaler, is very critical, that is, setting the pre-division value of SPI baud rate, that is, the parameters that determine the clock of SPI. There are 8 optional values for no sub-channel 256F. When initializing, we choose 256F value SPI_BaudRatePrescaler_256 and the transmission speed is 36M/256=140.625KHz.

The eighth parameter, SPI_FirstBit, sets whether the data transmission order is MSB bit or LSB bit, here we choose the SPI_FirstBit_MSB high bit first.

The ninth parameter SPI_CRCPolynomial is used to set the CRC check polynomial to improve the reliability of communication, as long as it is greater than 1.

Sample code:

Void SPIInit (void) {SPI_InitTypeDef SPI_InitStructure; FLASH_GPIO_Init (); / *!

< Deselect the FLASH: Chip Select high */ GPIO_SetBits( GPIOA, GPIO_Pin_4 ); /*!< SPI configuration */ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; /* 双线双向全双工 */ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; /* 主 SPI */ SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; /* SPI 发送接收 8 位帧结构 */ SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; /* 串行同步时钟的空闲状态为高电平 */ SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; /* 第二个跳变沿数据被采样 */ SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; /* NSS 信号由软件控制 */ SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; /* 预分频 16 */ SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; /* 数据传输从 MSB 位开始 */ SPI_InitStructure.SPI_CRCPolynomial = 7; /* CRC 值计算的多项式 */ SPI_Init( SPI1, &SPI_InitStructure ); /*!< Enable the sFLASH_SPI */ SPI_Cmd( SPI1, ENABLE );} 看到这里,可能觉的前面讲原理并没有太大的用处,因为STM32集成了SPI控制器,配置一下即可。 一方面我们学习原理是为了更好的理解SPI,用于对接不同的SPI设备,像norflash的spi驱动网上有大量的例子,不容易出错。但并不是特别常见的,spi驱动SD卡,SPI驱动网络PHY,SPI驱动ESP8266,甚至在设计两个IC通信时,由于没有过多GPIO,又觉的IIC通信速度慢的话,可以设计两个IC之间使用SPI通信,显然这些场景就需要了解SPI的原理 另外一方面,实际应用中,有可能因为芯片其他管脚用于特殊功能,留下的管脚没有硬件SPI功能,只能模拟实现,这个时候学习SPI原理就很有必要了。 5、SPI的应用 SPI的常用应用NorFlash 从数据手册上看到,SPI传输:CKPOL=1 , CKPHA=1

So the configuration of STM32's SPI read NorFlash is as follows

The waveform of the crawl is as follows

6 、 code

Read norflash

Use the STM32F207 hardware SPI module

/ * * @ brief Initializes the peripherals used by the SPI FLASH driver. * @ param None * @ retval None * / void FLASH_GPIO_Init (void) {GPIO_InitTypeDef GPIO_InitStructure; / *! < Enable the SPI clock * / RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE); / *! < Enable GPIO clocks * / RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA, ENABLE); RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOB, ENABLE); RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOE, ENABLE) / *! < SPI pins configuration * * / / *! < Connect SPI pins to AF5 * / GPIO_PinAFConfig (GPIOA, 5, GPIO_AF_SPI1); GPIO_PinAFConfig (GPIOA, 6, GPIO_AF_SPI1); GPIO_PinAFConfig (GPIOB, 5, GPIO_AF_SPI1) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//GPIO_PuPd_DOWN; / *! < SPI SCK pin configuration * / GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_Init (GPIOA, & GPIO_InitStructure) / *! < SPI MISO pin configuration * / GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_Init (GPIOA, & GPIO_InitStructure); / *! < SPI MOSI pin configuration * / GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_Init (GPIOB, & GPIO_InitStructure); / *! < Configure sFLASH Card CS pin in output pushpull mode * / GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init (GPIOE, & GPIO_InitStructure);} / * @ brief Initializes the peripherals used by the SPI FLASH driver. * @ param None * @ retval None * / void FLASH_SPIInit (void) {SPI_InitTypeDef SPI_InitStructure; FLASH_GPIO_Init (); / *! < Deselect the FLASH: Chip Select high * / GPIO_SetBits (GPIOE,GPIO_Pin_12); / *! < SPI configuration * / SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;// two-way full duplex SPI_InitStructure.SPI_Mode = SPI_Mode_Master / / main SPI SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;// SPI send and receive 8-bit frame structure SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;// the idle state of the serial synchronous clock is high SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;// the second jump edge data is sampled SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//NSS signal is controlled by software SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16 / / pre-division 16 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;// data transmission starts from the MSB bit SPI_InitStructure.SPI_CRCPolynomial = 7 SPI_Init (SPI1, & SPI_InitStructure) calculated by the raceme; / *! < Enable the sFLASH_SPI * / SPI_Cmd (SPI1, ENABLE);}

Software simulates SPI protocol

/ * @ brief Sends a byte through the SPI interface and return the byte received * from the SPI bus. * @ param byte: byte to send. * @ retval The value of the received byte. * / uint8_t SPI_ReadWriteByte (uint8_t data) {uint8_t iMagna datateller read = 0; if (dataholism 0xA5) {for (iMagnesia)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report