cancel
Showing results for 
Search instead for 
Did you mean: 

USART - reading 1 byte at a time

Rami Rosenbaum (old)
Associate II
Posted on February 19, 2017 at 13:39

Hi,

I need to parse incoming data from UART, with unknown length.

The packet length is sent within the packet, so I need to parse the packet as it arrives.

So - I need to read 1 byte at a time, and parse the packet while receiving.

I'm using CubeMX generated code and HAL libraries, and will probably work with interrupts.

Should I be calling HAL_UART_Receive_IT(&huart1, &nextByte, 1) within my HAL_UART_TxCpltCallback() method, and move the received byte to the foreground thread' for parse?

#hal #usart
10 REPLIES 10
Alan Chambers
Associate II
Posted on February 20, 2017 at 17:02

I can't really comment on the correct HAL approach to this but, if it helps:

I use DMA with a circular buffer for RX. I don't bother with DMA interrupts but poll NDTR on a timer to see if any new data has arrived. I do this because the packets are variable length and might be received intermittently: I don't want to hang around mid-packet waiting for more data to arrive to trigger a DMA interrupt. The buffer need not be very large: a bit over twice the maximum possible bytes per timer interval should do it. I usually opt for every 1ms, but this is arbitrary. I typically run the UART at 115200 or 230400 baud. New data is copied into a buffer and processed through a packet finding FSM (actually it's copied into a small structure which is appended to a queue for handling in application land). I would much rather do this than interrupt for every byte at 20kHz or whatever.

I have no idea whether something like this is possible using HAL. I assume so.