Always check UART_FLAG_FE flag when using USART on STM32

If you are using HAL libraries, you may be not aware that framing error will stop current receiving operation. I discovered this problem while performing ESD test (electrostatic discharge). At first I thought that ESD spike somehow corrupted USART registers, so USART stopped to receive any data from PC.  Later I found out that this problem is related to UART_FLAG_FE (framing error flag). Framing errors are easy to encounter when using half-duplex RS-485 IC chip,  or some type of interference (ESD, relay switching, etc.). It is important to monitor error flags, especially UART_FLAG_FE, because it stops USART RX transfer in case of circular DMA buffer transfer.  Temporarily I have solved this problem using this code:

if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_FE)) { … restart usart dma rx transfer again …}

Leave a Reply

Your email address will not be published. Required fields are marked *