UART misalign received data after previous receive timeout
Hello,
I'm having a problem making my UART communication robust on error. (two STM32F07xx custom boards communicationg through UART3, HAL + CubeMx). I have custom protocol (quite simple), fixed message length + acknowledge.
During normal runtime, I can correctly receive and transmitt data. If I intentionally (or unintentionally) insert some delay in code (elsewhere, not related to communication part of the project), thereceive part of the two boards should:
- get UART receive timeout,
- log an error into error log buffer,
- flush data register and
- continue to receive/send/communicate.
In reality it does everything except that any receivedata after first timeout is misaligned. For example,
during normal communication with no errors, this is received:0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF�?�?
On error, when data is not received and UART driver throws receive timeout (aka HAL_TIMEOUT), all the data is received corectly with additional byte at the beggining, causing data to be misaligned and therefore all wrong:
0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
What could cause this additional byte in reading data after previous rx timeout?
I receive data in blocking mode, using:
rx_status = HAL_UART_Receive(&huart3, data, MSG_SIZE, UART_BU_TO_CU_RX_TIMEOUT);
I tried to flush data register with all functions I could find (I think HAL documentation is a little bit unclear how to properly use this macros):
__HAL_UART_FLUSH_DRREGISTER(&huart3);
__HAL_UART_SEND_REQ(&huart3, UART_RXDATA_FLUSH_REQUEST); #flush #uart #timeour