cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Receive_IT problem with size argument other than one

keijonen123
Visitor

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

2 REPLIES 2

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:

https://community.st.com/t5/stm32-mcus-boards-and-hardware/extra-character-in-uart-rx-buffer/m-p/776891

 

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.

 

Karl Yamashita
Principal

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

KarlYamashita_0-1740850366258.png

 

Don't worry, I won't byte.
TimerCallback tutorial! | UART and DMA Idle tutorial!

If you find my solution useful, please click the Accept as Solution so others see the solution.