cancel
Showing results for 
Search instead for 
Did you mean: 

How can I process incoming UART communications when the code cycle is lengthy?

sandtime
Associate II
Posted on November 07, 2014 at 08:54

I'm working with an STM32F4 Discovery board, and I've been experimenting with the CubeF4 UART examples (HAL).

Everything I've seen suggests that receiving takes a manual command.  

Example:

HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 50);

Even the interrupt based examples look similar.  Playing around with the polling version, I discovered that if each code cycle takes too long (can be simulated by adding a 100 ms delay), then the majority of incoming communications are lost.  Increasing the timeout on the receive command can help, but then it slows down the code execution.

How can I have incoming communications processed immediately, with good success, without slowing down the rest of the code execution, and have the receive buffer act as a true buffer - holding the last communication even if the code cycle is lengthy?

4 REPLIES 4
ivani
Associate II
Posted on November 08, 2014 at 18:46

The HAL drivers are a jungle of code but hardly useful...

A typical way to organize the communication is using of circular buffers for received (and transmitted) characters. The management of the buffers (i.e. receiving of incoming characters) could be done either via ISR, or by DMA. These buffers are separated from the application level by function calls, which return the requested number of characters (either blocking, or non-blocking), or fill the transmission buffers with characters to send.

Of course, the buffers (especially the Rx one) should be made big enough to fit the incoming characters for the maximal interval of inactivity between two servicing calls.
sandtime
Associate II
Posted on November 09, 2014 at 07:52

I'm not thrilled with the HAL code either.  But that's what the example projects are written in, and there isn't a good alternative, that I've found.

Do you know if there's any example code out there for the type of communication management you're describing?
frankmeyer9
Associate II
Posted on November 09, 2014 at 19:15

Do you know if there's any example code out there for the type of communication management you're describing?

 

For example in the

http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/LN1734/PF257901

, containing several UART examples.

They don't come with a ''covers-all'' RX/TX buffer management, but avoid the glutted Cube stuff ...

ivani
Associate II
Posted on November 10, 2014 at 08:29

Regarding an example, you may search for ''circular buffer'' implementation. Some basis, although not specifically for UART, is given here:

http://stackoverflow.com/questions/827691/how-do-you-implement-a-circular-buffer-in-c

Edit: This one is even better:

http://embedjournal.com/implementing-circular-buffer-embedded-c/