sharing UART for multiple tasks.

fanmen1
Posts: 24
Joined: Thu Mar 21, 2024 1:30 pm

sharing UART for multiple tasks.

Postby fanmen1 » Wed Jul 31, 2024 8:26 am

Hi there,
I'm developing a device for data logging purpose. It mostly performs 2 FreeRTOS tasks. Firstly, forward all the packets received at UART2 to UART1 and vice versa (High priority). Secondly, send requests of its own to gather data for logging register values via UART1 (lower priority). As you can see, UART1 must be shared for both of these tasks. Moreover, maintain a robust concurrency. I have tried Mutexes, Semaphores, Queue, sometime with ISR (most of these introduces latency in data logging task). Semaphores seems to achieve this but is not flawless. Please suggest a best approach for achieving this.

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

Re: sharing UART for multiple tasks.

Postby MicroController » Wed Jul 31, 2024 12:59 pm

forward all the packets received at UART2
How do you determine what constitutes a 'packet' and when it is ok to insert a 'request' into the stream?
Semaphores seems to achieve this but is not flawless.
What flaws do you see?

fanmen1
Posts: 24
Joined: Thu Mar 21, 2024 1:30 pm

Re: sharing UART for multiple tasks.

Postby fanmen1 » Wed Jul 31, 2024 1:53 pm

Any Modbus request with the correct checksum that is received in UART port 2 constitutes a packet.

At the moment, given that data logging task is initialised with lower priority, it misses a few logging cycles whenever UART1 is occupied. Moreover, the data logging contains time stamp, therefore using a queue/buffer would just delay the time stamp logs too. So I'd rather not even register that data.

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

Re: sharing UART for multiple tasks.

Postby MicroController » Wed Jul 31, 2024 8:17 pm

Task priorities shouldn't matter in this case.
Use an approach which leverages blocking operations to read from each UART; this is easily done via two tasks, one per UART.
Also, because the UART hardware and driver may buffer both TX and RX data, you may need to first wait for the TX (and RX) buffer to be empty to be able to 'synchronize' data sent to data subsequently received.

Who is online

Users browsing this forum: MicroController and 206 guests