Clearing USART (UART) interrupt flags in an STM32?
Generally, you only need to handle the interrupt flags which you have specifically enabled with USART_ITConfig()
.
However, if you enable the RXNE interrupt (USART_ITConfig(USARTx, USART_IT_RXNE)
) then this also enables the Overrun interrupt! So you must handle both of those.
The USART flags can be confusing. There are separate status flags and interrupt flags and they share similar names. For example: USART_IT_RXNE
and USART_FLAG_RXNE
.
In addition, there are various methods to clear these flags. For example, the USART_ClearITPendingBit()
function only works for four (of the ten) possible flags.
Here is a summary of the interrupt flags and how to use them. These are specific for the STM32F105, but are representative:
USART_IT_TXE - "Transmit Data register empty"
- It is cleared automatically when calling
USART_SendData()
USART_IT_RXNE - "Receive Data register not empty"
It is cleared automatically when calling
USART_ReceiveData(USARTx)
It can be cleared manually by calling
USART_ClearITPendingBit(USARTx, USART_IT_RXNE)
USART_IT_TC - "Transmission complete"
It is cleared automatically by:
USART_GetITStatus(USARTx, USART_IT_TC)
followed byUSART_SendData()
It can be also be cleared manually by calling
USART_ClearITPendingBit(USARTx, USART_IT_TC)
USART_IT_CTS - "CTS change"
- Cleared by calling
USART_ClearITPendingBit(USARTx, USART_IT_CTS)
USART_IT_LBD - "LIN Break detected"
- Cleared by calling
USART_ClearITPendingBit(USARTx, USART_IT_LBD)
USART_IT_PE - "Parity error"
- Cleared by:
USART_GetITStatus(USARTx, USART_IT_PE)
followed byUSART_ReceiveData(USARTx)
USART_IT_NE - "Noise error"
- Cleared by:
USART_GetITStatus(USARTx, USART_IT_NE)
followed byUSART_ReceiveData(USARTx)
USART_IT_ORE - "Overrun error"
- Cleared by:
USART_GetITStatus(USARTx, USART_IT_ORE)
followed byUSART_ReceiveData(USARTx)()
USART_IT_IDLE - "Idle line detected"
- Cleared by:
USART_GetITStatus(USARTx, USART_IT_IDLE)
followed byUSART_ReceiveData(USARTx)()
Just want to add some my experience on this problem, I follow the instructions:
USART_IT_ORE - "Overrun error"
Cleared by: USART_GetITStatus(USARTx, USART_IT_ORE) followed by USART_ReceiveData(USARTx)()
Is seems not work, and the following command work for me instead:
USART_GetFlagStatus(USARTx, USART_IT_ORE) followed by USART_ReceiveData(USARTx)
If you look into the functions:
USART_GetFlagStatus() and USART_ReceiveData()
You will find what exactly Bitsmack wrote before... "First read the USARTx_SR register, then read the USARTx_DR register."
Hopefully it work for you and save lot more time on this issue.=)