cancel
Showing results for 
Search instead for 
Did you mean: 

Difference between blocking and non blocking HAL UART functions.

Arman Ilmak
Senior
Posted on June 23, 2018 at 12:23

Hi.

I'm trying to work with the stm32 uart functions with HAL and cubemx. When I was reading the functions I saw that there are 2 different ways to send and receive.One blocking mode (HAL_UART_Transmit() and HAL_UART_Receive()) and the other non blocking mode(HAL_UART_Transmit_IT() and HAL_UART_Receive_IT() ).

What is the diffence between these?

I was watching a video that used the interrupt and when a word was received and ready to read an interrupt occurred and used 

HAL_UART_Receive() in uart subroutine and get the word. So what is HAL_UART_Receive_IT()?

6 REPLIES 6
LMI2
Lead
Posted on June 23, 2018 at 14:04

There could also be a third method using DMA. UART sends or receives a byte 8 bits at a time, not words 16 bits or more. There are examples of those methods, not perfect because they not made with Cube but something at least. And you can Google functions like HAL_UART_Transmit. You'll find plenty of hits.

T J
Lead
Posted on June 23, 2018 at 14:33

Strange thing that you need to enable the UART interrupt before the DMA works perfectly...(oddity in STM-UART implementation, I think)

anyhow, your question.

Blocking code without RxDMA and interrrupts, you have to catch every byte before you lose it.

if your data is coming in at 9600 baud then you have 1mS roughly between bytes.

what are you doing while you wait ?

you only have 1 mS before you must read that byte,

after another mS, a new byte will overwrite the last byte in the UART Rx Register

with the nonBlocking RxDMA running on a circular buffer of 1024bytes, you have 1000x more time to do other stuff like an OS type arrangement of parallel state machines.

Posted on June 23, 2018 at 17:44

>>

So what is HAL_UART_Receive_IT()?

It is a way of initiating reception that will call-back your code at some later point when the buffer you requested is full. The way this is architected often isn't helpful, so you get back to requesting one byte at a time, for which an interrupt occurs for anyway, and processing it then.

One could argue a case for discarding most of the HAL USART/UART routines.

The difference between the blocking and non-blocking is to break the user of the serialized/linear flow model, data from the USART really should be collected under interrupt, or via a DMA FIFO arrangement, and then processed by a worker thread *when* data is available or complete. The polling/blocking methods are good for really simple examples, but is wastefully of computational resources in more practical/commercial applications.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Arman Ilmak
Senior
Posted on June 23, 2018 at 18:32

I haven't ever worked with DMA in any MCUs and I'm not familiar with(but as soon as I can I will search what it is and how to use it).

I saw an example that the uart interrupt was enabled and after the interrupt occured it used blocking mode to recieve the byte,Is this the correct form using interrupt?

Posted on June 23, 2018 at 20:38

>>I saw an example that the uart interrupt was enabled and after the interrupt occured it used blocking mode to recieve the byte,Is this the correct form using interrupt?

One could perhaps do that, but I wouldn't recommend it.

You might want to post a link or code fragment, it makes it easier to see what they were trying to do.

I've seen ST do blocking code in the interrupts of the LoRa examples, it results in data-loss elsewhere.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
T J
Lead
Posted on June 24, 2018 at 03:02

Take an example in real life.

Non-blocking:

I am working away at my desk, processing all different kinds of requests ( thought bubbles, work for stuff)

and my wife asks from the other room, ' Can you make coffee husband ?'

so firstly, my ears are interrupting my thoughts...

my brain quickly dismisses any kind of urgency, but we will finish what we are doing first.

anyhow, all these thoughts/threads are told to suspend, they all tie up to a stable point, and I get up to make the coffee.

So in the Operating system, it is constantly processing 'worker threads'.

Generally several are processing at the same time but also, there are many interrupts set up waiting, like traps ready to spring,

My ears are primed, my eyes, my nose, my sense of touch, all waiting to run like hell if I have to.

Blocking:

I sit by the door, do nothing but wait for the wife to ask for coffee. such a waste of time.