2021-04-15 02:39 AM
HAL_UART_Receive and HAL_UART_Receive_IT contain a bug. To reproduce (I use STM32L412KB nucleo 32 for this):
If one runs the code, there seems to be no issue.
Put a breakpoint in the function that stores the UART data.
Type 1 char in the terminal: the debugger kicks in. The debugger stops the core but not the UART. Type a number of chars in the terminal, enough to cause UART overrun condition.
Resume the software from the breakpoint. Whatever you type from now in the terminal emulator, HAL will never report UART data available.
The easiest solution is to put a receiver timeout and when it happens call
__HAL_UART_CLEAR_OREFLAG(&huart2);
but this isn't a clean way. HAL implemenation should clear the overrun bit when set and return an error condition.
The same behavior occurs in real-time, if an UART overrun event triggers.
The correct behavior would be HAL to report (overrun) error while this bit is set in the UART status and clear it afterwards.