cancel
Showing results for 
Search instead for 
Did you mean: 

Using HAL UART interrupt RX for uncertain size data?

apullin
Associate II
Posted on April 02, 2017 at 20:38

Hi,

I am updating an old project that was built on the 'STM32 FWLib', so I am porting everything over to the new Stm32Cube HAL. The real motivation is to get the proper I2C driver in the HAL, but I am unsure how to handle one detail of the UART for use of either the interrupt or DMA drivers: what to choose for the size argument to HAL_UART_Receive_IT() for the case of receiving burst transmissions with large variations in length?

Presently, the code uses the UART interrupt to take incoming characters and immediately enqueue them into an RTOS queue, where they are consumed and run through the processing state machine at a non-interrupt priority in a dedicated thread.

In the application, the data coming over the UART line is mostly unstructured, and of variable length and frequency, so there is no prescribed size to any given transmission. Thus I am unsure how to match up that need to the way the HAL is implemented, without changing code in the HAL code in HAL_UART_IRQHandler().

One possible solution that comes to mind is that I believe could call

HAL_UART_Receive_IT

() with size =1, and then use HAL_UART_RxCpltCallback to do the enqueue, and then reload the driver with another size=1 call to 

HAL_UART_Receive_IT. 

Not optimal for throughput, but it should handle the general length case that I need.

That seems a bit round-about, so I am curious if there is something that already handles this case in the HAL that I am missing?

1 REPLY 1
toby2
Senior
Posted on April 03, 2017 at 11:04

I don't think you are missing anything, HAL doesn't deal with this situation well

:(

You can just ask for 1 byte at a time as you suggest but it is a horrible way of doing it.

Personally I would (and have...)not use HAL for this as it does not add any benefit. I use my own serial interrupt routines based on std_lib but you could also do direct register writes as std_lib does not do a lot.

btw, if using HAL I2C be careful, I found a bug.....

https://community.st.com/0D50X00009XkeYjSAJ