# FreeRTOS : Issue with switching between 2 Tasks and usage of vTaskDelay().

**URL:** <https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266>\
**Category:** Kernel\
**Created:** [November 15, 2019, 10:31am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266 "2019-11-15T10:31:06Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 15, 2019, 10:31am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/1 "2019-11-15T10:31:06Z")

</div>

**[mansoor](https://sourceforge.net/u/mansoor/)** wrote on [Friday, November 15, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/96c1704a80/#c61a):

Hi,

**CASE 1:**

i am using vTaskDelay() to put the Task in Blocked state for some Time.  
while using vTaskDelay() its entering into the infinite delay state.  
The control is not coming out of the delay statement.

_following is the code flow, where vTaskDelay() is been used in both CADENCE and SYNOPSYS._  
int Count;  
int main()  
{  
xTaskCreate (test\_delay ); /_create a task ( test\_delay)_/  
vTaskStartScheduler();  
}

void test\_delay()  
{  
while(1)  
{  
Count++;  
printf(“Task running iter %d\n”,Count);  
vTaskDelay(1);  
}  
}

• Here the expected result is the printf() should print the statement infinte times  
but it is printing only once in both Cadence and synopsys.  
• In cadence it is showing as Stack Overflow stopping…

**CASE 2:**  
i am trying to communicate betweeen the 2 Tasks,  
if Task1 is sending Task2 should receive.  
Task2 again will send the data to Task1 and Task1 should Receive Data.

following is the code flow which i have tested in cadence but it is not working in the synopsys.

int main()  
{

```
 xTaskCreate (send_Task); /*created a send task */ 
 xTaskCreate(Receive_Task);/*created a receive task */ 
 vTaskStartScheduler();

```

}

void send\_Task()  
{  
while(1)  
{  
xMessageBufferSend(Data\_1);  
vTaskDelay(1);  
//taskYIELD();  
xMessageBufferReceive(Data\_2);  
}

}  
void Receive\_Task()  
{  
while(1)  
{  
xMessageBufferReceive(Data\_1);  
xMessageBufferSend(Data\_2);  
}  
}

• Here i am expecting after executing Task2 should switch back to Task1 which is not happenning.  
• i tried increasing the priority of the send\_Task also but it is not working.  
• i used taskYIELD() function, it is executing the next task2 of same priority but after executing it is not Resuming back to the task.  
• if i remove the vTaskDelay() only Task1 will execute continously.

```
      Is there any property settings that has to be changed based on the platform we are using to get this working.
      presently we are working on synopsys. 

```

Thanks and Regards,  
Mansoor Basha

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 15, 2019, 3:04pm UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/2 "2019-11-15T15:04:47Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, November 15, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/96c1704a80/#c61a/3290):

> • In cadence it is showing as Stack Overflow stopping…

Did you try increasing the stack size? The stack allocated to a task is  
one of the parameters passed into the xTaskCreate() function  
[This page describes the RTOS xTaskCreate() FreeRTOS API function which is part of the RTOS task control API. FreeRTOS is a professional grade, small footprint, open source RTOS for microcontrollers.](https://www.freertos.org/a00125.html) . There are also some utility  
functions you can use to monitor the task’s stack usage so you know if  
you allocate too much or too little.

> _CASE 2:_  
> i am trying to communicate betweeen the 2 Tasks,  
> if Task1 is sending Task2 should receive.  
> Task2 again will send the data to Task1 and Task1 should Receive Data.
> 
> following is the code flow which i have tested in cadence but it is not  
> working in the synopsys.
> 
> int main()  
> {
> 
> xTaskCreate (send\_Task); /\*created a send task \*/  
> xTaskCreate(Receive\_Task);/\*created a receive task \*/  
> vTaskStartScheduler();
> 
> }
> 
> void send\_Task()  
> {  
> while(1)  
> {  
> xMessageBufferSend(Data\_1);  
> vTaskDelay(1);  
> //taskYIELD();  
> xMessageBufferReceive(Data\_2);  
> }
> 
> }  
> void Receive\_Task()  
> {  
> while(1)  
> {  
> xMessageBufferReceive(Data\_1);  
> xMessageBufferSend(Data\_2);  
> }  
> }
> 
> • Here i am expecting after executing Task2 should switch back to Task1  
> which is not happenning.

Please post the complete function calls as without being able to see the  
priorities you are using or the block times you are using I don’t know  
the expected behaviour.

---

<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:** [November 15, 2019, 3:05pm UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/3 "2019-11-15T15:05:24Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Friday, November 15, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/96c1704a80/#e7f5):

My first guess is that your Tick Timer isn’t working, and the system isn’t seeing time move forward, so the delays are never satisifed.

It would be helpful to know what processor you are building this on.

---

<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:** [November 15, 2019, 3:05pm UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/4 "2019-11-15T15:05:26Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Friday, November 15, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/96c1704a80/#e660):

My first guess is that your Tick Timer isn’t working, and the system isn’t seeing time move forward, so the delays are never satisifed.

It would be helpful to know what processor you are building this on.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2019, 7:30am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/5 "2019-11-18T07:30:21Z")

</div>

**[mansoor](https://sourceforge.net/u/mansoor/)** wrote on [Monday, November 18, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/96c1704a80/#e7f5/b7ce):

ARCEM9D

Thanks for the Reply.  
Mansoor Basha

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2019, 10:31am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/6 "2019-11-18T10:31:40Z")

</div>

**[mansoor](https://sourceforge.net/u/mansoor/)** wrote on [Monday, November 18, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/96c1704a80/#c61a/3290/c0c8):

Project file and screenshots are attached here. one screenshot is the Result in cadence and the same result is expected in synopsys also but it is not switching back to the task1 to get the correct Result.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 20, 2019, 4:17am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/7 "2019-11-20T04:17:38Z")

</div>

**[mansoor](https://sourceforge.net/u/mansoor/)** wrote on [Wednesday, November 20, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/96c1704a80/#2f89):

Hi,

```
  [SYNOPSYS SWITCHING RELATED ISSUE SOLVED].
         if i increase the Stack Size of the parameter passing to xTaskCreate the switching of the Tasks is working fine now. 
   
  [SYNOPSYS vTaskDelay() ISSUE NOT SOLVED].
           but without vTaskDelay() it is working, if i include vTaskDelay() it is not working bcz the functionality of vTaskDelay() may be enetering into infinite condition.
```

---

<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:** [November 20, 2019, 4:34am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/8 "2019-11-20T04:34:11Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Wednesday, November 20, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/96c1704a80/#b0d0):

If increasing the stack size fixes things, then likely your issue is memory corruption causes by over running the stack. If that is happening, basically don’t try to figure out what is happening, but just fix the stack issue.

You probably should also enable stack checking, at least during early debugging to confirm that stacks are big enough (it may slow down things enough that you don’t want to use it in production, but it is good to check things at first.)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 21, 2019, 6:32am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/9 "2019-11-21T06:32:52Z")

</div>

**[mansoor](https://sourceforge.net/u/mansoor/)** wrote on [Thursday, November 21, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/96c1704a80/#299b):

Hi,

```
  As i am trying to make vTaskDelay() functionality working. i am not able to understand what is the problem in this case. as i got reply that Tick Timer isn't working, how to make this Tick Timer working. in SYNOPSYS (ARCEM9D)
  Screenshot (62)= Result with vTaskDelay(1)
  Screenshot (63)= Result without vTaskDelay(1)

```

CASE 1:

i am using vTaskDelay() to put the Task in Blocked state for some Time.  
while using vTaskDelay() its entering into the infinite delay state.  
The control is not coming out of the delay statement.

following is the code flow, where vTaskDelay() is been used in both CADENCE and SYNOPSYS.  
int Count;  
int main()  
{  
xTaskCreate (test\_delay ); //create a task ( test\_delay)//  
vTaskStartScheduler();  
}

void test\_delay()  
{  
while(1)  
{  
Count++;  
printf(“Task running iter %d\n”,Count);  
vTaskDelay(1);  
}  
}

• Here the expected result is the printf() should print the statement infinte times  
but it is printing only once in synopsys.

---

<div class="post-metadata">

**Author:** ![pointnotfoe](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/pointnotfoe/32/4796_2.png) [@pointnotfoe](https://forums.freertos.org/u/pointnotfoe)\
**Post date:** [July 25, 2024, 7:22pm UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/10 "2024-07-25T19:22:40Z")

</div>

Hi not sure if anyone is watching this.  
I am also using synosys’ arc, and running into the same issue. I can get two tasks to switch if I uses vTaskDelay(0) in at least one of them. If i use a non zero value in both they stop working and seem to get stuck. I have checked and the timer interrupt seems to be working.

---

<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 26, 2024, 5:26am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/11 "2024-07-26T05:26:38Z")

</div>

Which FreeRTOS version and which FreeRTOS port are you using?

> [@pointnotfoe](#):
>
> I have checked and the timer interrupt seems to be working.

How did you check that? Did you also check if [xTickCount](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L487) is incrementing?

---

<div class="post-metadata">

**Author:** ![pointnotfoe](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/pointnotfoe/32/4796_2.png) [@pointnotfoe](https://forums.freertos.org/u/pointnotfoe)\
**Post date:** [July 26, 2024, 10:00pm UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/12 "2024-07-26T22:00:01Z")

</div>

> Which FreeRTOS version and which FreeRTOS port are you using?

FreeRTOS 202210.01 LTS. Port: portable/ThirdParty/GCC/ARC\_EM\_HS. Support files from [GitHub - foss-for-synopsys-dwc-arc-processors/embarc\_osp: embARC Open Software Platform (OSP) - An embedded software distribution for IoT and other embedded applications for ARC](https://github.com/foss-for-synopsys-dwc-arc-processors/embarc_osp/tree/master)

> How did you check that? Did you also check if [xTickCount](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L487) is incrementing?

I am incrementing a variable every time interrupt is fired and reading that. Yes, xTickCount also increments.

---

<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 28, 2024, 7:31am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/13 "2024-07-28T07:31:30Z")

</div>

What are the value of `configUSE_PREEMPTION` and `configUSE_TIME_SLICING`?

> [@pointnotfoe](#):
>
> If i use a non zero value in both they stop working and seem to get stuck.

If you put a `vTaskDelay(1)`, the task should unblock on the next tick. Can you step though the [xTaskIncrementTick](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L4674) and see what it returns?

---

<div class="post-metadata">

**Author:** ![pointnotfoe](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/pointnotfoe/32/4796_2.png) [@pointnotfoe](https://forums.freertos.org/u/pointnotfoe)\
**Post date:** [July 29, 2024, 3:58pm UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/14 "2024-07-29T15:58:47Z")

</div>

`configUSE_PREEMPTION 1 configUSE_TIME_SLICING 1`  
I dont have debugger to step through, I will try to see way of getting that status.

---

<div class="post-metadata">

**Author:** ![pointnotfoe](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/pointnotfoe/32/4796_2.png) [@pointnotfoe](https://forums.freertos.org/u/pointnotfoe)\
**Post date:** [August 8, 2024, 7:29pm UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/15 "2024-08-08T19:29:46Z")

</div>

Sorry about the delay, the increment is returning 1. I believe I have figured out the missing piece for the task not switching.  
The switch was not happening due to me calling the kernel tick ISR directly instead of using the ARC interrupt entry function, `exc_entry_int`, that is supposed to wrap the kernel tick and yield from ISR.  
But… With this change all tasks seem to be stuck and I dont see any activity. Trying to understand the implementation of exc\_entry\_int in arc\_support.s to see what might be missing.

Any hints are welcome

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [August 12, 2024, 5:17am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/16 "2024-08-12T05:17:40Z")

</div>

> [@pointnotfoe](#):
>
> With this change all tasks seem to be stuck and I dont see any activity

Can you share the task code, what activity do you expect?

---

<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:** [August 12, 2024, 7:01am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/17 "2024-08-12T07:01:56Z")

</div>

> [@pointnotfoe](#):
>
> Trying to understand the implementation of exc\_entry\_int in arc\_support.s to see what might be missing.

These functions are likely from the vendor SDK and you should try reaching out to them also.

---

<div class="post-metadata">

**Author:** ![pointnotfoe](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/pointnotfoe/32/4796_2.png) [@pointnotfoe](https://forums.freertos.org/u/pointnotfoe)\
**Post date:** [August 25, 2024, 3:58am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/18 "2024-08-25T03:58:39Z")

</div>

Just as an update, figured out the issue. Interrupt prologue and epilogue wasn’t exactly compatible with my arc processor, I had to change that from handwritten implementation in arc\_support.s to compiler generated isr wrapper provided by the arc tool chain. From there just call the rest of the arc\_support code.

---

<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:** [August 26, 2024, 6:13am UTC](https://forums.freertos.org/t/freertos-issue-with-switching-between-2-tasks-and-usage-of-vtaskdelay/8266/19 "2024-08-26T06:13:47Z")

</div>

Thank you for sharing!
