cancel
Showing results for 
Search instead for 
Did you mean: 

Handling UART errors with HAL

bgorsline
Associate II
Posted on November 16, 2015 at 02:29

Looking for guidance on how to robustly handle UART errors using HAL. Got a two-board app communicating based on the UART_TwoBoards_ComIT example and various forum messages, but any sort of error throws it into an endless interrupt loop.

The basic approach is what's in UART_TwoBoards_ComIT – setting global variables to lock the RX and TX when busy, unlocking in the *CpltCallback routines, and setting flags so the main thread notices. But it's unclear from the docs I've found what do about errors. First, I'm not sure that HAL_UART_ErrorCallback can even tell whether the error was for Rx or Tx. Found some macros, but not convinced they work in all scenarios. This is how I'm detecting and handling a receive failure:

if ((__HAL_UART_GET_IT(UartHandle, UART_IT_RXNE) != RESET) &&
(__HAL_UART_GET_IT_SOURCE(UartHandle, UART_IT_RXNE) != RESET)) {
__HAL_UART_SEND_REQ(UartHandle, UART_RXDATA_FLUSH_REQUEST);
__HAL_UART_CLEAR_IT(UartHandle, UART_CLEAR_OREF);
}

Not doing anything for transmit failure at this point. While I think this is crux of the question, attached is the 'minimal' test program that shows the endless loop behavior – to get errors quickly, I just put one board at 9600 baud and the other at 57600. As soon as the receive comes in with the wrong baud rate, even the heartbeat transmit stops - debugger shows it looping in interrupts. What should happen is that the receives fail gracefully and the transmits keep going. (Note that the full app uses more than port, hence the generalized routines and use of UART3.) thanks in advance Bret #uart-hal
1 REPLY 1
bgorsline
Associate II
Posted on November 22, 2015 at 04:45

Anyone have tips about handling UART errors even if not using HAL?