Is it safe to read and write UART from different tasks?

maldus
Posts: 83
Joined: Wed Jun 06, 2018 1:41 pm

Is it safe to read and write UART from different tasks?

Postby maldus » Wed Mar 27, 2019 9:02 am

Just a small doubt that hit me recently.
I have an ESP32 project where I answer to a custom communication protocol on UART0. I have a task receiving interrupts when there is data to read that elaborates the requests and in turn sends (through a message queue) valid messages to a second task. The second task then answer those requests, eventually by writing to the UART in turn.

Is is safe to have a task reading the uart while another one writes it (concurrently)? Rx and Tx lines are separated, and up until now there was no problem, but you never know. Should I use a semaphore or have both communication directions in a single thread?

fivdiAtESP32
Posts: 47
Joined: Thu Dec 20, 2018 9:47 am

Re: Is it safe to read and write UART from different tasks?

Postby fivdiAtESP32 » Sun Mar 31, 2019 12:27 am

There's an esp-idf example called "UART Asynchronous Example with Separate Receive and Transfer Tasks" at https://github.com/espressif/esp-idf/tr ... _rxtxtasks so it looks like it can be done.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: Is it safe to read and write UART from different tasks?

Postby urbanze » Wed Apr 24, 2019 8:41 pm

Looking just at uart.c, apparently YES, but anyone can really confirm if all uart tx are safe?

Who is online

Users browsing this forum: No registered users and 173 guests