cancel
Showing results for 
Search instead for 
Did you mean: 

UART blocking transmition/reception and HAL driver: would need some clarification :)

Yves Bernard
Associate II
Posted on December 05, 2017 at 18:14

Hi guys,

I'm fairly new to the world of STM32 and embedded C, and i've been playing with a STM32F401 recently. 

I'm using the HAL UART driver to set a serial communication with my computer through the port COM3, and I have a few questions :

1. It took me a good while before transmitting data correctly. I believe my configuration was good but for some reason, the terminal was displaying only the fist character from my chain. I managed to sort it out by inserting a 1ms delay in the HAL_UART_Transmit driver,each time we have receive one byte. I assume it's not necessary since it wasn't in the original driver but would anyone have an idea of what is hapenning, and what i misunderstand ?

2. When trying to receive data using HAL_UART_Receive, I manage to reveive data byte by byte, but if I entry more than one character (so a chain), the function return only the first character and then go to timeout. I have a feeling there is some timing issue there but I'd like to understand what I am doing wrong and what I misunderstand.

I can add code if you think there is value, but I'd say it's more a theoretical issue.

Also, i'm now gonna look at implementing this with interupt since I beleive it will be more adequate.

Thanks a lot,

Yves 

#uart #hal_uart_transmit #hal_uart_receive #hal-uart
1 REPLY 1
AvaTar
Lead
Posted on December 06, 2017 at 09:43

I managed to sort it out by inserting a 1ms delay in the HAL_UART_Transmit driver,each time we have receive one byte.

This is a sub-optimal solution, to say the least.

I suspect you manage your reception in a synchronous, blocking manner. The Cube generator spits out some framework, but you need to think about proper mechanism (algorithm) before, especially when asynchronous events like serial transmission is involved.

I can add code if you think there is value, but I'd say it's more a theoretical issue.

Presenting the code will definitely be of value.

I do not use Cube/HAL, and I don't comment on it, but there are several others here.

Also, i'm now gonna look at implementing this with interupt since I beleive it will be more adequate.

Yes, definitely.

AFAIK, Cube generates a callback function for your code, so you don't need to touch the interrupt handler itself.

Be aware interrupt priorities and runtime issues - take care to spend as few as possible time in the interrupt context, else you will block other interrupts, including follow-ups of the same interrupt.