cancel
Showing results for 
Search instead for 
Did you mean: 

HAL UART Overrun causes it to never receive data again in normal and interrupt modes

EGi
Associate III

HAL_UART_Receive and HAL_UART_Receive_IT contain a bug. To reproduce (I use STM32L412KB nucleo 32 for this):

  • Connect a terminal emulator to the ST-LINK as serial port.
  • Configure that serial port in CubeIDE if default not chosen.
  • Use for example the following code:
  • HAL_StatusTypeDef uhst = HAL_UART_Receive (&huart2, uart_buffer, 1, 1);
  • if (uhst == HAL_OK) { /* store the single byte in a larger buffer */ }

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.

0 REPLIES 0