STM32 SPI usage and SPI initialization precautions

Publisher:NexusDreamLatest update time:2017-09-23 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the STM32F10x series chips, although the number of SPI synchronous serial ports is different, the initialization method is the same. When using SPI, we recall the use of GPIO and pay attention to 6 points. We will mainly explain one point here.

According to the system's requirements for different functions, it will be more complicated to initialize different functional peripherals, initialize GPIO, and initialize SPI synchronous serial port devices. All SIP synchronous serial ports of STM32F10x series chips are shared with GPIO. The initialization of SPI is divided into two major parts, namely the initialization of the I/O port used by SPI and the initialization of SPI functions. For the pins used by the SPI synchronous serial port, according to the direction of the data, set GPIO_Mode_IN_FLOATING or GPIO_Mode_AF_PP multiplexing push-free output. The other settings are the same as the GPIO pin settings.

The initialization method of the SPI synchronous serial port parameters will be shown in a later example.

When we use SPI, we must turn on the clock of the SPI synchronous serial port, so that the system can send and receive data from this opened synchronous serial port.

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);

Sending data in SPI is very simple. When the data byte to be sent is written into the transmit buffer, the transmission process begins. However, before trying to write to the transmit buffer, it is necessary to confirm whether the TXE flag is 1.

Similarly for the receiver, when the data transmission is completed, the data is also completed after the end. At the last sampling clock edge, the RXNE flag is set. The data word received in the shift register is transferred to the receive buffer. The RXNE bit is cleared when the SPI_DR register is set.

Therefore: when sending data, it is necessary to determine whether the previous data has been sent. Only when the data is sent completely can new data be sent.

For example: PA7 is used as the MOSI pin of SPI1 synchronous serial port, PA6 is used as the MISO pin of SPI1 synchronous serial port, PA5 is used as the SCK pin, and PA4 is used as the NSS chip select pin. Define the synchronous serial port SPI1 as the master device, send 8 data bits each time, the high bit is first, the SPI clock frequency is 9MHz, the data is captured at the second clock edge, the clock is low when idle, and the NSS pin is managed by software.

The complete code is as follows:

#include "stm32f10x_lib.h"

void SPI_Configuration(void)

{

// SPI_1 serial port parameter definition, SPI_1 parameters are clock is usually high, rising edge sampling

SPI_InitTypeDef SPI_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI2,ENABLE); //Enable SPI_2 clock

//Configure SPI1's MISO (PA6) as a floating input

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

GPIO_InitStructure.GPIO_MODE = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA,&GPIO_InitStructure);

//Configure SPI1's MOSI (PA7) and SPI1's CLK (PA5) as multiplexed push-to-disconnect outputs

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;

GPIO_InitSturcture.GPIO_Speed ​​= GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA,&GPIO_InitStructure);

//Configure SPI1's NSS (PA4) as push output

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

GPIO_InitSturcture.GPIO_Speed ​​= GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOA,&GPIO_InitSturcture);

//SPI1 synchronization parameter initialization definition

SPI_InitTypeDef SPI_InitStructure;

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_Fullduplex;

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_Init(SPI1,&SPI_InitStructure);

SPI_Cmd(SPI1,ENABLE);

}

//Send a data through SPI1 port and receive a data at the same time

unsigned char SPI_SendByte(unsigned char Byte)

{

//If the register data is not sent, wait in a loop

while(SPI_T1S_GetFlagStatus(SPI1,SPI_T1S_FLAG_TXE) == RESET);

//Write the data to be sent to the send register

SPI_T1S_SendData(SPI2,Byte);

//If the receiving register does not receive data, loop

while(SPI_T1S_GetFlagStatus(SPI1,SPI_T1S_FLAG_RXNE) == RESET);

return SPI_T1S_ReceiveData(SPI1);

}


Keywords:STM32 Reference address:STM32 SPI usage and SPI initialization precautions

Previous article:ARM9 2440 Hardware SPI Driver - NRF24L01
Next article:Basic introduction and points to note about bit operations in STM32

Recommended ReadingLatest update time:2024-11-25 05:16

Implementation of bit-band operation in STM32 using C language
 first:    #define BITBAND(addr, bitnum) ((addr & 0xF0000000) + 0x2000000 + ((addr & 0xFFFFF) 5) + (bitnum 2))    Explanation of the above program:   The mapping of bit-band addresses is expressed by macro definition. The function has two parameters, addr and bitnum, which are the original address and the
[Microcontroller]
STM32: Keil MDK (uVision v4.10) development environment construction
Integrated development environment (IDE): Keil's RealView MDK (uVision V4.10) Source file editor: EditPlus (what I use)/UltraEdit/SourceInsight/IDE's own editor  Serial port tool: SecureCRT (a professional hyperterminal tool that supports many interfaces (including serial ports) and protocols, recommended.)   RealView
[Microcontroller]
stm32 download method serial port ISP\swd (JLink)
1. First look at the stm32 startup mode  The boot mode of stm32 is determined by the boot0 and boot1 pins of the 32 chip. It is divided into embedded flash boot mode (normal boot mode), memory boot mode, and rom boot mode. The corresponding relationship between the boot mode and the pin high and low levels is as follo
[Microcontroller]
stm32 download method serial port ISP\swd (JLink)
STM32 USB routine modification steps
The following is a summary of what I learned when I modified ST's Custom_HID routine into a "custom USB device" routine. Because I have just started learning USB development, some misunderstandings are inevitable. Please correct me if I misunderstand them.       1. usb_desc.c file Modify it according to the
[Microcontroller]
stm32 system clock configuration problem
From power-on reset to 72MZ configuration, the clock is provided by the internal high-speed RC oscillator with a frequency of 8MZ. This is completed and guaranteed by the following part of the code in the front of the void SystemInit (void) function:        RCC- CR |= (uint32_t)0x00000001;   #ifndef STM32F10X_CL
[Microcontroller]
Understanding STM32 GPIO speed, mode, etc.
1. GPIO mode configuration 1. Input/output mode (refer to the STM32 manual) 2. In GPIO output mode, the difference between several speeds: (1). GPIO pin speed: GPIO_Speed_2MHz       (10MHz, 50MHz);       also known as the response speed of the output driver circuit: (The chip has arranged multiple output driver cir
[Microcontroller]
STM32 Series Chapter 9 - External Interrupts
Each IO of STM32 can be used as an external interrupt input.  The interrupt controller of STM32 supports 19 external interrupt/event requests: Lines 0~15: Corresponding to the input interrupt of the external IO port. Line 16: Connect to PVD output. Line 17: Connect to the RTC alarm event. Line 18: Connect to USB w
[Microcontroller]
STM32IO and timer mapping to address
Significance: Sometimes when we operate multiple STM32 IOs, the hardware design may not be regular, such as the output pins are: PB3, PC4, PC5, PD0, but the operations of these pins have commonalities, or we want to use for(it i = 0; i 4; i++) to operate these pins like an array, the program will become very concise,
[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号