This article is very useful! Newbies should not be self-righteous. Do you know how to remap the STM32 serial port pins?
The STM32F10x series of microcontrollers all include the USART module, which is a universal synchronous asynchronous receiver and transmitter. The universal synchronous asynchronous receiver and transmitter (USART) provides a flexible method for full-duplex data exchange with external devices using the industrial standard NRZ asynchronous serial data format. It supports synchronous one-way communication and half-duplex single-line communication, as well as LIN (local interconnect network), smart card protocol and IrDA (infrared data organization) SIR ENDEC specification, as well as modem (CTS/RTS) operation. It also allows multi-processor communication.
From the previous introduction, we know that the USART module is very powerful. Here I will just briefly talk about how to use the USART module to implement standard EIA-232 serial communication.
Anyone who has used a single-chip microcomputer must have come into contact with a serial port. Setting a serial port is nothing more than setting the baud rate, data bit, stop bit, and parity bit. There are three basic ways to send and receive: polling, interrupt, and DMA. The USART module of STM32F10x is no different. So I will focus on the various mistakes I made when debugging the code, and I will not explain in detail those codes that are easy to get.
First, let me talk about my hardware environment. I still use the Shenzhou 4 development board, and use serial port 2, which corresponds to USART2. By default, USART2 is connected to IO port A, but I need to redirect the USART pins to IO port D. The specific relationship between the pins is shown in the table below. This table is copied from the STM32 reference manual.
The code to initialize USART is very simple. USART2 is connected to APB1 bus. First, turn on the clock of USART2, and then set parameters such as baud rate.
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure );
This setting is not usable yet. This is because we redirected USART2. The redirection operation requires writing the multiplexing remapping and debugging I/O configuration register (AFIO_MAPR). GPIO_PinRemapConfig() can complete this task.
[cpp] view plain copy
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
This is not enough. The STM32 reference manual says:
Before reading or writing registers AFIO_EVCR, AFIO_MAPR and AFIO_EXTICRX, the AFIO clock should be turned on first. Refer to Section 6.3.7 APB2 Peripheral Clock Enable Register (RCC_APB2ENR).
So you need to turn on the AFIO clock first. Therefore, redirection of USART2 requires two steps:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
I thought it would work, but it still didn't output anything. I had no choice but to continue researching. When I was reading the chapter on GPIO, I saw the following picture, which made me suddenly realize it.
The input and output of USART2 are all borrowed from the PD port, but the clock of the PD port has not been given. The corresponding input and output states of the several IO ports used have not been set. When reading the section 8.1.9 Multiplexing Function Configuration, I found the following table.
According to the configuration given above, write the program:
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOD, &GPIO_InitStructure);
Tested again, everything is normal.
A function that sends a character can be written like this:
void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET ) {};
USART_SendData (USARTx, Data);
}
This function can be optimized manually, and the two function calls in it can be removed, or even implemented in assembly or written as an inline function. However, this is just a sample code and does not take these into consideration.
The function that sends the string is as follows:
void UART_PutStr (USART_TypeDef* USARTx, uint8_t *str)
{
while (0 != *str)
{
UART_PutChar(USARTx, *str);
str++;
}
}
The above serial port initialization code can be put into a function:
void USART2_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure );
USART_Cmd(USART2, ENABLE);
}
That's all for today. The function for receiving characters is similar to the function for sending characters, but this polling method is very inefficient and is not recommended. Next time I will write an article about how to send and receive serial port data using interrupts, which is much more efficient. If I have time, I will write another article about sending and receiving data using DMA.
Previous article:STM32 UART/USART initialization clock enable
Next article:STM32 485 serial data transmission and reception
Recommended ReadingLatest update time:2024-11-16 12:59
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Could you please recommend a mature domestic high-power step-down DCDC?
- Design of analog signal source for remote sensing images with wide spectrum coverage
- Small Cell Networks and the Evolution of 5G
- C Language Core Technology (Original Book 2nd Edition)
- ADS1.2 Issues
- Limited-time free download | NI white paper: "Three Visions of the O-RAN Alliance"
- Which domestic motor drive company do you think is the most promising?
- What should we pay attention to when developing headphone speakers?
- Analysis of Optimized Programming of TMS320C6000 Embedded System
- 【Qinheng RISC-V core CH582】 3 Light routines and button acquisition initial test