# How to synchronize the two task in UART interrupt mode

**URL:** <https://forums.freertos.org/t/how-to-synchronize-the-two-task-in-uart-interrupt-mode/20975>\
**Category:** Kernel\
**Tags:** tutorial\
**Created:** [July 17, 2024, 7:08am UTC](https://forums.freertos.org/t/how-to-synchronize-the-two-task-in-uart-interrupt-mode/20975 "2024-07-17T07:08:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![manoj0306](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/manoj0306/32/4776_2.png) [@manoj0306](https://forums.freertos.org/u/manoj0306)\
**Post date:** [July 17, 2024, 7:08am UTC](https://forums.freertos.org/t/how-to-synchronize-the-two-task-in-uart-interrupt-mode/20975/1 "2024-07-17T07:08:46Z")

</div>

I am craeting two task and A ,B and i want to send data on same UART and i am using preemtive scheduling with 1ms systick below is my code i have written

```auto
uint8_t TX_Buffer_1[] = "MADY\n\r";
uint8_t TX_Buffer_2[] = "Hello_2";

void main()
{

    xTaskCreate( vUART_1_Task, ( signed portCHAR * ) "LED1", 100, NULL, 2, &UART_1_handle1 );

    xTaskCreate( vUART_2_Task, ( signed portCHAR * ) "LED2", 100, NULL, 2, &UART_2_handle2 );

    vTaskStartScheduler();
}

static void vUART_1_Task(void *pvParameters)
{
    BaseType_t status;

    for( ;; )
    {
       UART_Write((HalUartHandle_t *)&g_Uart_1, UART_1, (uint8_t *) TX_Buffer_1, sizeof(TX_Buffer_1));

       vTaskDelay( 10);
    }
}

static void vUART_2_Task(void *pvParameters)

{
    BaseType_t status;

    for( ;; )
    {
       UART_Write((HalUartHandle_t *)&g_Uart_1, UART_1, (uint8_t *) TX_Buffer_2, sizeof(TX_Buffer_2));

       vTaskDelay( 10);
    }
}

```

Output is as follows

```auto
MAHello_2

MAHello_2

```

continuesly print

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [July 17, 2024, 7:28am UTC](https://forums.freertos.org/t/how-to-synchronize-the-two-task-in-uart-interrupt-mode/20975/2 "2024-07-17T07:28:53Z")

</div>

we have had this several times before, for example here: [Synchronizing UART TX - Kernel - FreeRTOS Community Forums](https://forums.freertos.org/t/synchronizing-uart-tx/17758)

I generally recommend to employ the forum search function before posting questions.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [July 17, 2024, 11:37am UTC](https://forums.freertos.org/t/how-to-synchronize-the-two-task-in-uart-interrupt-mode/20975/3 "2024-07-17T11:37:31Z")

</div>

The first part of the answer is that very likely UART\_Write is NOT designed to be RTOS aware, and thus not suitable. It likely also “spin-waits” as it is sending, and thus isn’t really appropriate for use in an RTOS, even if you added the sycronization needed to make it uable in an RTOS.

Studying a bit to understand those questions will help you emensly to answer the question you are asking.

---

<div class="post-metadata">

**Author:** ![manoj0306](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/manoj0306/32/4776_2.png) [@manoj0306](https://forums.freertos.org/u/manoj0306)\
**Post date:** [July 17, 2024, 12:05pm UTC](https://forums.freertos.org/t/how-to-synchronize-the-two-task-in-uart-interrupt-mode/20975/4 "2024-07-17T12:05:25Z")

</div>

UART\_Write function write in UART Interrupt mode and both the task access that UART RX,TX functionality

---

<div class="post-metadata">

**Author:** ![manoj0306](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/manoj0306/32/4776_2.png) [@manoj0306](https://forums.freertos.org/u/manoj0306)\
**Post date:** [July 17, 2024, 12:12pm UTC](https://forums.freertos.org/t/how-to-synchronize-the-two-task-in-uart-interrupt-mode/20975/5 "2024-07-17T12:12:07Z")

</div>

but i am not able to synchronise the task in interrupt mode first task write after that my code stuck into the config\_assert of xSemaphoreGiveFromISR

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [July 17, 2024, 1:25pm UTC](https://forums.freertos.org/t/how-to-synchronize-the-two-task-in-uart-interrupt-mode/20975/6 "2024-07-17T13:25:49Z")

</div>

It would help to know which assert was triggered, but a common one is due to not assigning the interrupt a valid priority. They often default to a non-suitable priority.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [July 17, 2024, 2:54pm UTC](https://forums.freertos.org/t/how-to-synchronize-the-two-task-in-uart-interrupt-mode/20975/7 "2024-07-17T14:54:37Z")

</div>

> [@manoj0306](#):
>
> but i am not able to synchronise the task in interrupt mode

Which synchronization primitive are you using for synchronizing? Can you share code snippet?

> [@manoj0306](#):
>
> first task write after that my code stuck into the config\_assert of xSemaphoreGiveFromISR

Which assert is getting triggered?

---

<div class="post-metadata">

**Author:** ![rvt](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@rvt](https://forums.freertos.org/u/rvt)\
**Post date:** [July 20, 2024, 7:48pm UTC](https://forums.freertos.org/t/how-to-synchronize-the-two-task-in-uart-interrupt-mode/20975/8 "2024-07-20T19:48:22Z")

</div>

Why not create 3 tasks.  
TAsk A/B to write to a Queue  
Tasks 3 that reads the queue and sent to a UART…  
Unless you want to have some order, but then it sounds like you are doign something wrong…

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [July 20, 2024, 8:20pm UTC](https://forums.freertos.org/t/how-to-synchronize-the-two-task-in-uart-interrupt-mode/20975/9 "2024-07-20T20:20:42Z")

</div>

If the write to the parts is using an interrupt based driver, then you don’t really need a third task to do the operations, but they can be done at the task level.

There will need to be a Semaphore or Mutex to block access while the operation is in progress (it needs to be a semaphore if it will be cleared by an ISR).

If the ISR asserted in the xSemaphoreGiveFromISR, as reported, my guess is that the interrupt priority of the ISR was never set right (and on many machines it defaults to being just too high). That, or the file with the ISR isn’t getting the right handle for the Semaphore.

I have rarely found a uart driver from a vendor that worked the way I wanted, as they almost always take a buffer that needs to be maintained with the data until the sending is complete, and then you can give them another buffer. I tend to write my own that stores the data to send into a Queue or Streambuffer, and that is pulled off in an ISR. The sending end of the Queue/Streambuffer is guarded with a Mutex so tasks can fill the buffer with a full message, at which point they give it up.
