Author Topic: Writing to a single UART from multiple tasks  (Read 4838 times)

Offline FAQ

  • Newbie
  • *
  • Posts: 27
    • View Profile
Writing to a single UART from multiple tasks
« on: January 05, 2017, 09:18:48 PM »
If I have two tasks running and both need to access the same serial port, is there a mutex or semaphore method in uTasker?

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Writing to a single UART from multiple tasks
« Reply #1 on: January 05, 2017, 09:21:01 PM »
Hi

The UART has a queue which is protected so you can write to the same UART from different tasks and/or interrupts.

Since the OS is co-operative there is no need to protect tasks from other tasks.

The only way that UART output may be disturbed if you do

fnWrite(UART_ID, buffer1, len1); // write "MESSAGE1"
fnWrite(UART_ID, buffer2, len2); // write "MESSAGE2"


and an interrupt also write between the two (eg. writes "INT").

This would give

"MESSAGE1INTMESSAGE2"


The individual "MESSAGE1" and "MESSAGE2" messages would never get disturbed themselves due to queue protection.

If an interrupt were to share the UART (unlikely) and it is necessary to protect multiple writes you can still use

uDisable_Interrupt();
fnWrite(UART_ID, buffer1, len1); // write "MESSAGE1"
fnWrite(UART_ID, buffer2, len2); // write "MESSAGE2"
uEnable_Interrupt();


to ensure they can't get an int. message slipped in between them, but it would be rare to write to the same UART from both interrupts and also tasks.


Only one task should normally "receive" from a UART since it would otherwise be difficult to know which one actually get reception if both could read the input queue at any time....

Regards

Mark