STM32F4 Study Notes 7——USART Part 2

Publisher:吾道明亮Latest update time:2017-09-20 Source: eefocusKeywords:STM32F4  USART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Hardware flow control 
uses nCTS input and nRTS output to control the serial data flow between two devices. The figure shows how to connect two devices in this mode: 
Hardware flow control between two USARTs 
Write 1 to the RTSE bit and CTSE bit in the USART_CR3 register to enable RTS and CTS flow control respectively.

RTS Flow Control 
If RTS flow control is enabled (RTSE=1), the USART receiver asserts nRTS (tied to low) whenever it is ready to receive new data. When the receive register is full, nRTS is deasserted, indicating that the transmission process will stop after the end of the current frame. The following figure shows an example of communication with RTS flow control enabled. 
RTS flow control

CTS Flow Control 
If CTS flow control is enabled (CTSE=1), the transmitter checks nCTS before sending the next frame. If nCTS is valid (tied to low), the next data is sent (assuming data is ready to send, that is, TXE=0); otherwise, no transmission is performed. If nCTS becomes invalid during transmission, the transmitter stops after the current transmission is completed. 
When CTSE=1, the CTSIF status bit is automatically set to 1 by hardware whenever nCTS changes. This indicates whether the receiver is ready for communication. If the CTSIE bit in the USART_CR3 register is set to 1, an interrupt is generated. The following figure shows an example of communication with CTS flow control enabled. 
CTS flow control 
Note: Special behavior of stop frame: When CTS flow is enabled, the transmitter will not check the nCTS input status when sending the stop signal.

USART Interrupts 
USART Interrupt Request 
The USART interrupt events are connected to the same interrupt vectors (see Figure 270). 
● During transmission: Transmit Complete, Clear to Send or Transmit Data Register Empty interrupts. 
● During reception: Idle Line Detect, Overrun Error, Receive Data Register Not Empty, Parity Error, LIN Break Detect, Noise Flag (Multi-buffer communication only) and Framing Error (Multi-buffer communication only) 
These events generate interrupts if the corresponding enable control bits are set. 
USART interrupt map

USART Configuration Mode 
USART Configuration Mode

Continuous Communication Using DMA 
Transmitting Using DMA 
Enable DMA mode for transmission by setting the DMAT bit in the USART_CR3 register. When the TXE bit is set, data can be loaded from the SRAM area (configured by DMA, see the DMA section) to the USART_DR register. To map a DMA channel for USART transmission, follow these steps (x represents the channel number): 1. Write the 
USART_DR register address in the DMA control register to configure it as the destination address of the transfer. After each TXE event, the data is moved from the memory to this address. 
2. Write the memory address in the DMA control register to configure it as the source address of the transfer. After each TXE event, the data is loaded from this memory area to the USART_DR register. 
3. Configure the total number of bytes to be transferred in the DMA control register. 4. Configure 
the channel priority in the DMA register 
5. Generate a DMA interrupt after half or all of the transfer is completed, depending on the application requirements. 
6. Write 0 to the TC bit in the SR register to clear it. 
7. Activate the channel in the DMA registers. 
When the amount of data transfer set in the DMA controller is reached, the DMA controller generates an interrupt on the interrupt vector of the DMA channel. 
In transmit mode, after the DMA has written all the data to be transmitted (the TCIF flag in the DMA_ISR register is set to 1), the TC flag can be monitored to ensure that the USART communication is complete. This step must be performed before disabling the USART or entering stop mode to avoid corruption of the last transmission. The software must wait until TC=1. The TC flag must remain clear during all data transmissions and then be set to 1 by hardware after the last frame transmission is completed. 
Sending using DMA

Reception using DMA 
Setting the DMAR bit in the USART_CR3 register enables DMA mode for reception. When a data byte is received, the data is loaded from the USART_DR register into the SRAM area (configured by DMA, see DMA specification). To map a DMA channel for USART reception, follow these steps: 
1. Write the USART_DR register address in the DMA control register to configure it as the source address of the transfer. After each RXNE event, the data is moved from this address to the memory. 
2. Write the memory address in the DMA control register to configure it as the destination address of the transfer. After each RXNE event, the data is loaded from the USART_DR register to this memory area. 
3. Configure the total number of bytes to be transferred in the DMA control register. 
4. Configure the channel priority in the DMA control register. 
5. Generate an interrupt after half or all of the transfer is completed, depending on the application requirements. 
6. Activate the channel in the DMA control register. 
When the amount of data transfer set in the DMA controller is reached, the DMA controller generates an interrupt on the interrupt vector of the DMA channel. In the interrupt subroutine, the DMAR bit in the USART_CR3 register should be cleared by software. 
Note: If DMA is used for reception, do not enable the RXNEIE bit. 
Receiving using DMA 
Error Flags and Interrupt Generation in Multi-Buffer Communication 
In multi-buffer communication, if any error occurs in the transaction, an error flag is set after the current byte. If the interrupt enable is set, an interrupt is generated. During single-byte reception, the framing error, overflow error, and noise flags that are set along with RXNE have a separate error flag interrupt enable bit (EIE bit in the USART_CR3 register); if this bit is set, an interrupt is generated after the current byte for any of these errors.

STM32F4 library 
serial port configuration process 
Enable USART clock 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USARTx, ENABLE); //1 and 6 USART 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE); //2, 3, 4, 5 USART 
Enable GPIO 
RCC_AHB1PeriphClockCmd(); //IO can be TX, RX, CTS and SCLK 
Set the multiplexing function of the peripheral 
1. GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); //Connect the pin to the corresponding multiplexing function 
2. Configure the pin to the corresponding multiplexing function through GPIO_InitStruct->GPIO_Mode_AF 
3. Select the IO pull-up and pull-down, IO type and IO speed through GPIO_PuPd, GPIO_OType and GPIO_Speed 
​​4. (Asynchronous) Configure Baud Rate, Word Length, Stop Bit, Parity, hardware data, mode (send and receive) through USART_Init(). 
USART_InitStructure.USART_BaudRate = 115200; /* Baud rate*/ 
USART_InitStructure.USART_WordLength = USART_WordLength_8b; 
USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity 
= USART_Parity_No; 
USART_InitStructure.USART_Har dwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1,&USART_InitStructure); 
5. For synchronous mode, enable clock and polarity, phase and last bit 
void USART_StructInit(USART_InitTypeDef* US ART_InitStruct); 
void 6. If interrupts need to be enabled,  enable 
NVIC and enable the corresponding  interrupt bits  through  the USART_ITConfig function  7.  When using DMA mode  - configure DMA through the DMA_Init() function  ——Activate the required channels through the function USART_Cmd()  8. Enable USART through USART_Cmd()  9. When using DMA, enable DMA through DMA_Cmd()  10. For details about Multi-Processor, LIN, half-duplex, Smartcard, IrDA, please refer to the M4 Authoritative Guide










In order to obtain a higher baud rate, the function USART_OverSampling8Cmd() can be used to set 8 times oversampling (the default is 16 times oversampling). This function needs to be called after the clock RCC_APBxPeriphClockCmd() and before USART_Init(). 
void USART_DeInit(USART_TypeDef* USARTx) is implemented by resetting the clock

The data transmission function 
uses the function USART_ReceiveData() to read the value of the register USART_DR. This data comes from the RDR buffer. The function USART_SendData() is used to write to the register USART_DR. The data will be stored in the TDR buffer. 
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data) 

/* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_USART_DATA(Data));

/* Transmit Data */ 
USARTx->DR = (Data & (uint16_t)0x01FF); 
}

uint16_t USART_ReceiveData(USART_TypeDef* USARTx) 

/* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx));

/* Receive Data */

return (uint16_t)(USARTx->DR & (uint16_t)0x01FF); 
}

DMA transfer and management functions 
The function USART_DMACmd is used to enable DMA send and receive requests 
void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState) 

/* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_USART_DMAREQ(USART_DMAReq)); assert_param(IS_FUNCTIONAL_STATE(NewState));

if (NewState != DISABLE) 

/* Enable the DMA transfer for selected requests by setting the DMAT and/or DMAR bits in the USART CR3 register */ 
USARTx->CR3 |= USART_DMAReq; 

else 

/* Disable the DMA transfer for selected requests by clearing the DMAT and/or DMAR bits in the USART CR3 register */ 
USARTx->CR3 &= (uint16_t)~USART_DMAReq; 

}

中断标志管理函数 
查询模式 
(#) USART_FLAG_TXE : to indicate the status of the transmit buffer register 
(#) USART_FLAG_RXNE : to indicate the status of the receive buffer register 
(#) USART_FLAG_TC : to indicate the status of the transmit operation (#) USART_FLAG_IDLE : to indicate the status of the Idle Line 
(#) USART_FLAG_CTS : to indicate the status of the nCTS input 
(#) USART_FLAG_LBD : to indicate the status of the LIN break detection (#) USART_FLAG_NE : to indicate if a noise error occur 
(#) USART_FLAG_FE : to indicate if a frame error occur 
(#) USART_FLAG_PE : to indicate if a parity error occur 
(#) USART_FLAG_ORE : to indicate if an Overrun error occur 
在查询模式,建议使用下面两个函数: 
(+) FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG); 
(+) void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);

中断模式 
挂起标志Pending Bits: 
(##) USART_IT_TXE : to indicate the status of the transmit buffer register 
(##) USART_IT_RXNE : to indicate the status of the receive buffer register 
(##) USART_IT_TC : to indicate the status of the transmit operation (##) USART_IT_IDLE : to indicate the status of the Idle Line 
(##) USART_IT_LBD : to indicate the status of the LIN break detection (##) USART_IT_NE : to indicate if a noise error occur 
(##) USART_IT_FE : to indicate if a frame error occur 
(##) USART_IT_PE : to indicate if a parity error occur 
(##) USART_IT_ORE : to indicate if an Overrun error occur 
中断源Interrupt Source: 
(##) USART_IT_TXE : specifies the interrupt source for the Tx buffer empty interrupt. 
(##) USART_IT_RXNE : specifies the interrupt source for the Rx buffer not empty interrupt. 
(##) USART_IT_TC : specifies the interrupt source for the Transmit complete interrupt. 
(##) USART_IT_IDLE : specifies the interrupt source for the Idle Line interrupt. 
(##) USART_IT_CTS : specifies the interrupt source for the CTS interrupt. 
(##) USART_IT_LBD : specifies the interrupt source for the LIN break detection interrupt. 
(##) USART_IT_PE : specifies the interrupt source for the parity error interrupt. 
(##) USART_IT_ERR : specifies the interrupt source for the errors interrupt. 
上面的标志和中断源可以在函数组合使用 
在这个模式下,建议使用下面三个函数 
(+) void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState); 
(+) ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT); 
(+) void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);

DMA mode 
USART has two requests to manage in DMA mode, one is the send request and the other is the receive request 
(#) USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request (#) USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request 
It is recommended to use the surface function in this mode 
(+) void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);

The void USART_ITConfig() 
function is used to enable the 8 related interrupt sources, which are the 8 introduced earlier.

FlagStatus USART_GetFlagStatus()

void USART_ClearFlag() 
According to the previous introduction, these two are used together, mainly in the query mode for interrupts. There are two other functions 
ITStatus USART_GetITStatus() 
void USART_ClearITPendingBit()


Keywords:STM32F4  USART Reference address:STM32F4 Study Notes 7——USART Part 2

Previous article:STM32F4 study notes 8——NIVC vector interrupt
Next article:STM32F4 Study Notes 6——USART Part 1

Recommended ReadingLatest update time:2024-11-16 16:34

STM32F4 ADC internal temperature sensor
Used to measure the ambient temperature around the chip. The voltage output by this temperature sensor is proportional to the temperature. To obtain the temperature, the ADC is used to measure this voltage. Inside the chip, the temperature sensor is connected to CH16 of ADC1. When the sensor is not in use, it can be s
[Microcontroller]
STM32 serial USART communication
1. USART and UART USART (Universal Synchronous Asynchronous Receiver and Transmitter) is a universal synchronous asynchronous receiver and transmitter. It is a serial communication device that can flexibly exchange full-duplex data with external devices. Before this, we often used UART (Universal Asynchronous Receiv
[Microcontroller]
STM32 serial USART communication
STM32F429 timer
F429 has 14 timers in total 2 advanced TIM1, TIM8 10 general purpose TIM2~TIM5 TIM9~TIM14 2 basic TM6, TIM7 only TIM_Prescaler and TIM_Period 1. Configuration priority     NVIC_InitTypeDef NVIC_InitStructure;      NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); The interrupt group is 0     NVIC_InitStructure
[Microcontroller]
STM32--Analysis of printf redirection in Atomic Brother USART experiment
There is a part of the USART code that I don't quite understand. #if 1 #pragma import(__use_no_semihosting)              //Support functions required by the standard library                  struct __FILE  {      int handle;      /* Whatever you require here. If the only file you are using is */      /* standard out
[Microcontroller]
CubeMX clock settings STM32F407VET
A tutorial on how to use CubeMX is available at:  http://blog.csdn.net/u013429988/article/details/54566969 The first time I used the F4 series chip, I planned to use an 8M passive crystal as an external crystal. Connect it between RCC_OSC_IN and RCC_OSC_OUT as shown in the figure below. Configure the pins. The two g
[Microcontroller]
CubeMX clock settings STM32F407VET
STM32F407 ADC DMA multi-channel + temperature
Here is the temperature added in the previous chapter The above figure is the temperature calculation formula: where Vsense is the ADC value collected from the temperature channel. The stm32f407 temperature channel is channel 16 of ADC1. Avg_Slope is usually set to 0.0025 Compared with the previous one, the foll
[Microcontroller]
STM32F407 ADC DMA multi-channel + temperature
Playing Control with STM32F407 - Smith Predictive Compensation Control
The principle of Smith predictive compensation control is shown in Figure 1. The transfer function Ksgs(s) in Figure 1 is called a predictive compensator (this figure comes from the Internet, and the literature can be found in Jin Yihui's "Process Control"). In principle, Ksgs(s)=Kpgp(s)(1-exp(-τd*s)), so that the clo
[Microcontroller]
Playing Control with STM32F407 - Smith Predictive Compensation Control
DMA implementation of USART in STM32
For those who have never played with DMA, here is a brief introduction to DMA. Let me explain it in my own words. That is, data is transferred from a certain location to a certain location. If DMA is not used, the CPU must be involved in the operation, and it is moved byte by byte. If it is more efficient , it is mov
[Microcontroller]
DMA implementation of USART in STM32
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号