cancel
Showing results for 
Search instead for 
Did you mean: 

UART receive interrupt issue HAL_UART_Receive_IT remains BUSY

SA.17
Associate III

Hi ,

I am using

HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)

HAL api for uart receive interrupt

i am calling it at end of HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

it works for some time

but after some time HAL_UART_Receive_IT return value is Busy

once its busy ,its always busy , i tried below options

  1. if it's busy ,try again. But still remains busy
if((HAL_UART_Receive_IT(&UartHandle, (uint8_t *)RxBuffer, 1)) != HAL_OK)
                   {
                   HAL_UART_Receive_IT(huart, (uint8_t *)RxBuffer, 1) ;
                 
                   }

2 . I used while loop ,still remains busy forever

if((HAL_UART_Receive_IT(&UartHandle, (uint8_t *)RxBuffer, 1)) != HAL_OK)
                   {
                     while(HAL_UART_Receive_IT(huart, (uint8_t *)RxBuffer, 1)==HAL_BUSY);
                     }

3 . I used timer to ensure a delay after which i try to enable receive interrupt again after 5 sec ,

still remain busy .

4 . I tried running this operation in between __disable_irq() and __enable_irq,still problem remains same.

How to resolve the issue ?

9 REPLIES 9
TDK
Guru

The transfer needs to complete in order for it to go from busy to ready. Ensure data is being sent to it. Ensure you're not within an ISR which is blocking it from completing.

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

I used __disable_irq() and __enable_irq also but result remains same

Why would those help?
If you feel a post has answered your question, please click "Accept as Solution".
Rmalani
Associate

Check if you are using HAL_UART_Transmit() in polling and HAL_UART_Receive_IT() in inteerupt mode. Use bot Tx and Rx in Interrupt mode. Older ST Driver has shared variables between Tx and Rx.

I am using Transmit only ,i will try this

How to get newer drivers ?

Karl Yamashita
Lead III

Sounds like a buffer overrun issue most likely due to you being in another interrupt or you're still in the HAL_UART_RxCpltCallback() when another character is received.

Someone had posted in the forums saying after a while when reading a text file that gets updated every so often, that person was looking for a key word. Well that code would not always find the key word. It turns out that person was processing the received data within the interrupt instead of saving the data to a buffer and processing the data from a polling routine outside of the interrupt.

If you are indeed processing the received data within the interrupt, then you should make a ring buffer to save the data and then parse the data outside the interrupt.

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

I am buffering characters till i receive a '\r' inside HAL_UART_RxCpltCallback()  function .

an i am calling parser function from inside of HAL_UART_RxCpltCallback()  function . Is it OK ?

That is why you're having issues. When you're calling another function from within the HAL_UART_RxCpltCallback(), you're no longer receiving interrupts when characters are filling the FIFO, thus you're getting a buffer overrun. You should set a flag that indicates your buffer is ready to be parsed. Then poll outside of any interrupts for that flag and then parse the buffer.

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.