cancel
Showing results for 
Search instead for 
Did you mean: 

Strange HAL_UART_Receive_IT() behavior

Chuev.Vladimir
Associate III

I have an RS485 device, the time for which it turns on is about 3-4 seconds.

In response to a request, it can send an indefinite amount of data, so I run HAL_UART_Receive_IT with a margin of a few bytes.

But I only receive 1 byte and then an OVR error occurs.

Sometimes 0 byte is accepted with an OVR error.

It is VERY rare that HAL accepts all the bytes sent by the device.

It should be noted that the time from the start of the request from the host to the response from the device ranges from 100ms to 3sec.

HAL_GPIO_WritePin(GPIOB, RS485_POWER_EN_Pin, GPIO_PIN_SET);
HAL_Delay(5000);
 
while(true) {
	HAL_UART_Transmit(&huart1, (uint8_t*)txData, 5, 100);
 
	HAL_UART_AbortReceive_IT(&huart1);
	HAL_UART_Receive_IT(&huart1, (uint8_t*)rxData, 50);
 
	HAL_Delay(4000);
}

But I noticed that if I delay after turning on not 5000, but 1000 (i.e. the device does not have time to turn on), then everything works well.

Yes, HAL_UART_Receive_IT won't receive anything the first time, but there won't be any problems later

Trying to understand the reasons, I came to the conclusion that this code works

HAL_GPIO_WritePin(GPIOB, RS485_POWER_EN_Pin, GPIO_PIN_SET);
HAL_UART_Receive_IT(&huart1, (uint8_t*)rxData, 50);
HAL_Delay(5000);
 
while(true) {
	HAL_UART_Transmit(&huart1, (uint8_t*)txData, 5, 100);
 
	HAL_UART_AbortReceive_IT(&huart1);
	HAL_UART_Receive_IT(&huart1, (uint8_t*)rxData, 50);
 
	HAL_Delay(4000);
}

For some reason, if you run HAL_UART_Receive_IT for the first time so that it does not receive anything, then everything will work

I have a headache from all this, what is the reason for this behavior?

I use STM32F4

3 REPLIES 3
TDK
Guru

Include your STM32 chip number.

When does OVR actually get set? Does it happen within the HAL_UART_Transmit routine?

Device is probably sending data while you're not actively reading it.

If you feel a post has answered your question, please click "Accept as Solution".

i use STM32F413VGT6

I'm not sure, but I think this happens after receiving the first byte HAL_UART_Receive_IT

Are you saying that having data on the UART RX line while HAL_UART_Receive_IT causes an OVR error?

Pavel A.
Evangelist III

If I remember correctly, ST library has issues with UART overrun handling. Better disable it.

-- pa