Methods for RTOS Multi-Task Access to the Same UART

Methods for RTOS Multi-Task Access to the Same UART

Author | strongerHuang

WeChat Official Account | Embedded Column

In RTOS multi-task programming, it is common for multiple tasks to access the same hardware (such as UART, I2C, etc.). If not handled properly, it can lead to a “chaotic” situation.
There are many methods to handle the “chaotic” situation. Below, based on FreeRTOS, I will explain two common methods: mutual exclusion and queues, using UART as an example.

Embedded Column

1

Mutual Exclusion Access Method
Mutex: A variable that can be in one of two states: unlocked and locked.
Principle: Create a mutex. Task A occupies the resource (using UART to send data) when needed. At this point, Task B and other tasks cannot occupy the resource. When Task A finishes using the resource (after sending data), it releases the resource, allowing other tasks to seize the resource.
Create a mutex
Task A occupies the resource
Use the resource (send data)
Task A releases the resource
Higher priority Task B occupies the resource
Use the resource
Task B releases the resource
In turn, higher priority tasks occupy the resource
·
·
·

Code:

// Create mutex resourceSemaphoreHandle_t xSemaphore = NULL;xSemaphore = xSemaphoreCreateMutex();
void TaskA(void *pvParameters){  for(;;)  {    // Occupy resource    if(xSemaphoreTake(xSemaphore, 10 ) == pdTRUE)    {      // Use resource (send data)      USART_SendNByte();      // Release resource      xSemaphoreGive(xSemaphore);    }  }}
Difference between Semaphore and Mutex:
Semaphore: Multiple tasks synchronize to use a resource;
One task signals others after completing an action, allowing them to execute certain actions;
Mutex: Multi-task mutual exclusion for certain resources;
When one task occupies a resource, other tasks cannot access it until that task releases it.

Embedded Column

2

Queue Operation Method
The queue operation method is FIFO, first in first out principle. For example: Task A wants to use UART to send a string of data, adding it to the queue; then Task B also wants to use UART to send a string of data.
Then, Task A adds this string of data to the queue, and Task B adds the data it wants to send to the queue.
In another UART sending task, read the data from the queue in FIFO order and send it out.
Create a queue (send data queue)
Create a task (UART send data task)
Task A adds to the queue
Task B adds to the queue
·
·
·
On the other side, tasks read queue data in order and use UART to send it out.

Code:

QueueHandle_t xQueue;xQueue = xQueueCreate(QUEUE_LENGTH, QUEUE_ITEM_SIZE);
xTaskCreate(UART_Send_Task, "UART_Send", STACK_SIZE, NULL, TASK_PRIORITY, NULL);
void TaskA(void *pvParameters){  for(;;)  {    // Task related operations
    // Add to queue    xQueueSend(xQueue, &TaskA_Buf, 10)  }}
void TaskB(void *pvParameters){  for(;;)  {    // Task related operations
    // Add to queue    xQueueSend(xQueue, &TaskB_Buf, 10)  }}
void UART_Send_Task(void *pvParameters){  for(;;)  {    // Loop read queue BUF    if(xQueueReceive(xQueue, &Buf, 10) == pdTRUE)    {      USART_SendNByte(&Buf);    }  }}

The above two methods are commonly used and relatively simple. I hope they are helpful to everyone.

Note:The code is for learning and understanding principles only. In projects, code should be added, deleted, or modified according to actual situations.

———— END ————

Reply “STM32” or “FreeRTOS” or “Embedded Software Design and Development” to read more related articles.

Welcome to follow my official account, reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Welcome to follow my video account:

Methods for RTOS Multi-Task Access to the Same UART

Click “Read Original” to see more shares. Please feel free to share, collect, like, and view.

Leave a Comment

×