UART2 RX interrupt how to do it?

ob_seven
Posts: 12
Joined: Tue Sep 12, 2023 12:29 pm

Re: UART2 RX interrupt how to do it?

Postby ob_seven » Mon Mar 03, 2025 5:09 pm

Hi,

There is something I do not get in all the above examples. You either have a while (1) loop to wait for an event to occur or a for (;;) which is for me the same.
The whole point of the interrupt is precisely to not have such types of infinite loops that do eat cpu time.

After looking all over internet it seems that the only way to get a true interrupt mechanism, firing an interrupt handler to recover the bytes from the uart needs to be written from scratch right? In the isr handler beside clearing the interrupts we also need to give a semaphore to the actual uart_data_process task.

The queue management waiting for an event to happen does not look to me any better than just waiting for bytes to appear at the uart with the proper flags being raised…..

Am I right or I miss something fundamental ?

Thanks

Sprite
Espressif staff
Espressif staff
Posts: 10596
Joined: Thu Nov 26, 2015 4:08 am

Re: UART2 RX interrupt how to do it?

Postby Sprite » Mon Mar 03, 2025 11:41 pm

You're missing the fact that you're running on a RTOS, and functions like uart_read_bytes are so-called blocking functions (recognizable by the fact that they tend to have a timeout argument). What happens is that the UART driver sets up an interrupt when installed, and when you call uart_read_bytes, it will tell the RTOS to suspend the task until that interrupt wakes it again. A suspended task simply gets zero CPU time (it's not executed), so while uart_read_bytes is waiting for data, it uses zero CPU. When the interrupt happens, the RTOS starts executing the task again, uart_read_bytes returns, and you can read your data.

ob_seven
Posts: 12
Joined: Tue Sep 12, 2023 12:29 pm

Re: UART2 RX interrupt how to do it?

Postby ob_seven » Tue Mar 04, 2025 8:51 am

Thank you!!!

It indeed now makes a lot more sense!

Gosh a few simple things like that are not trivial to catch.

Now I fully get it .

Thanks again

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: UART2 RX interrupt how to do it?

Postby MicroController » Tue Mar 04, 2025 8:55 am

In the isr handler beside clearing the interrupts we also need to give a semaphore to the actual uart_data_process task.
That's actually how the UART driver works. (The ISR puts data into a ringbuffer, and the ringbuffer gives a semaphore when it has data available to be read.)

ob_seven
Posts: 12
Joined: Tue Sep 12, 2023 12:29 pm

Re: UART2 RX interrupt how to do it?

Postby ob_seven » Tue Mar 04, 2025 9:22 am

Ok!

Then it means I have a bad parameter in the uart driver installation then because the esp keeps looping at hi frequency on the below.

NoBytesRead = uart_read_bytes(UART_NUM_2, serBufIn, NoBytesRead, 200 / portTICK_PERIOD_MS)

With what you explained above I will check for my mistake and revert

Thanks

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: UART2 RX interrupt how to do it?

Postby MicroController » Tue Mar 04, 2025 11:14 am

Code: Select all

... uart_read_bytes(UART_NUM_2, serBufIn, NoBytesRead, 200 / portTICK_PERIOD_MS)
The NoBytesRead argument is not what you need there. In its place, you pass in the maximum number of bytes you want the driver to put into your buffer. If this argument is 0, uart_read_bytes() will not wait/block for any data but return straight away.

ob_seven
Posts: 12
Joined: Tue Sep 12, 2023 12:29 pm

Re: UART2 RX interrupt how to do it?

Postby ob_seven » Tue Mar 04, 2025 12:26 pm

Gosh,

I made so many checks and so many tries for such a stupid mistake

Thanks a lot!

Who is online

Users browsing this forum: Bing [Bot] and 5 guests