cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Receive_IT - how to clean before starting the function

PDogu.1
Associate

Dear Mrs, Mr,

I'm working with a Riverdi discovery board with a STM32. I'm developing a serial protocol for which I'm using interrupt for reception.

  • I enabled the IT in CubeMx
  • Using HAL_UART_Receive_IT(), it is working fine for the first time and also if all characters received are read
  • If I relaunch HAL_UART_Receive_IT() and characters were sent to the uart RX buffer without being read, it fails to work
  • The only way I made it working was to add a HAL_UART_Init function before it and it is working fine.

But I guess this is not the best way to clean buffer, state and pending interrupts. Can you advise of the best way to do it ?

Thank you in advance,

Pascal

1 ACCEPTED SOLUTION

Accepted Solutions
Guenael Cadier
ST Employee

Hi Pascal,

You could check the ISR (or SR register, depending on the STM32 serie you are using) of UART instance when you execute the 2nd reception (the failing one). Maybe some errors are already present in register, preventing the reception to be properly handled. I'm thinking about the Overrun flag (indicating that some data where received but not read on time from RDR). This could happen if additional data have been received after 1st reception is complete. When this flag is set (some data have been lost), but reception is frozen under the error is cleared.

If this happens in your case, you could clear the flag by resetting flag in ICR (or CR register) prior restarting 2nd reception, for instance using the dedicated macro :

__HAL_UART_CLEAR_FLAG(__HANDLE__, __FLAG__) with UART_CLEAR_OREF as flag.

Regards

Guenael

View solution in original post

1 REPLY 1
Guenael Cadier
ST Employee

Hi Pascal,

You could check the ISR (or SR register, depending on the STM32 serie you are using) of UART instance when you execute the 2nd reception (the failing one). Maybe some errors are already present in register, preventing the reception to be properly handled. I'm thinking about the Overrun flag (indicating that some data where received but not read on time from RDR). This could happen if additional data have been received after 1st reception is complete. When this flag is set (some data have been lost), but reception is frozen under the error is cleared.

If this happens in your case, you could clear the flag by resetting flag in ICR (or CR register) prior restarting 2nd reception, for instance using the dedicated macro :

__HAL_UART_CLEAR_FLAG(__HANDLE__, __FLAG__) with UART_CLEAR_OREF as flag.

Regards

Guenael