2025-03-01 8:20 AM - edited 2025-03-01 8:21 AM
Hello,
I have just started with STM32 after some projects with AVR MCUs. I have recently noticed a strange behavior with "HAL_UART_Receive_IT" function. Everything works fine for me if the size argument is one, but for other values the overrun flag is set after few receptions.
If I for example give the size argument size=5 and send bytes in the chunks of three bytes like:
123<send> 123<send> 123<send> 123<send>
The overrun occurs somewhere during the fourth chunck. I dont understand why everything works perfectly and I never end up with ORE flag set if the size parameter is one. By looking at the HAL driver the interrupt routine should actually take slghtly longer when the size is one and with size=1 everything works always. Uart baudrate is only 9600. My debug code after initializations is just forever while loop with no content (while(1);) and my uart RX callback code is:
char rx_buff[100];
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1) // Ensure it's UART1
{
HAL_StatusTypeDef ret;
ret = HAL_UART_Receive_IT(&huart1, (uint8_t*)rx_buff, 5); //You need to toggle a breakpoint on this line!
}
}
Thank you and Best Regards
2025-03-01 9:11 AM
Probably, the problem is because your receiver is not ready when (part of) a message arrives.
So: what is your transmitter, and how do you ensure that your receiver is ready before your transmitter starts transmitting?
See this recent thread:
Unless you know for sure that the sender will only and always send a specific number of bytes, and your receiver will always be ready to catch the very first byte, it's generally better to just receive 1 byte at a time under interrupt, and collect received bytes into a ring buffer.
2025-03-01 9:34 AM
Use Live Expressions instead. You'll see that the data is placed in the buffer as it should. And there are no errors in the ErrorCode even after sending 123 many times