# Issue with xTaskDelayUntil API

**URL:** <https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868>\
**Category:** Kernel\
**Tags:** debug\
**Created:** [April 23, 2024, 2:06pm UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868 "2024-04-23T14:06:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![PSK](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@PSK](https://forums.freertos.org/u/PSK)\
**Post date:** [April 23, 2024, 2:06pm UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/1 "2024-04-23T14:06:42Z")

</div>

Hello All,

I am facing an issue with the **xTaskDelayUntil** API. My goal is to create tasks with execution frequencies of 5ms and 10ms using this API.

The problem I’m encountering is that when I run the code, the 5ms and 10ms counters increment properly. However, if I stop the code and rerun it, the counters no longer increment at all.

Even when I set a breakpoint inside the **vTask1\_handler** , the counters remain stagnant. It’s important to note that the program is in the run state and not halting anywhere.

Upon breaking the execution of the program, I’ve observed that it halts at the following API: **prvCheckTasksWaitingTermination()**.

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/4/4195265fc93c823b9a40654374b9d6986a7f042c.png)

For reference, the value of **pxDelayedTaskList** is 1, and **pxDelayedTaskList** = 2.

Below is the relevant code snippet:

```auto
xTaskCreate(vTask1_handler, "Task_1", configMINIMAL_STACK_SIZE, NULL, 2, &xTaskHandle1);

void vTask1_handler(void *params)
{
    TickType_t xLastWakeTime;
    const TickType_t xFrequency = pdMS_TO_TICKS(5);

    xLastWakeTime = xTaskGetTickCount();
    while (1)
    {
        // Wait for the next cycle.
        xWasDelayed = xTaskDelayUntil(&xLastWakeTime, xFrequency);

        FreeRtos_5ms_Counter++;

        taskYIELD();
    }
}

```

I appreciate any assistance in resolving this issue. Thank you!

---

<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:** [April 23, 2024, 4:20pm UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/2 "2024-04-23T16:20:33Z")

</div>

The point you are seeing is in the idle task, so the system thinks no tasks are ready. One question, what do you mean by “stop the code and rerun it”?

Your code should work, and you shouldn’t need the call to taskYIELD(), as xTaskDelayUntil should block the task until the next time point. The one provision is that the tick rate needs to be at least 200 Hz (so the tick period is no longer than 5 ms) of pdMS\_TO\_TICKS(5) would return 0, which is invalid.

One possible problem is that somewhere else in your code you are doing something which messes the scheduler. One common problem is an ISR with an incorrect priority.

Is configASSERT defined to catch problems?

---

<div class="post-metadata">

**Author:** ![PSK](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@PSK](https://forums.freertos.org/u/PSK)\
**Post date:** [April 23, 2024, 6:22pm UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/3 "2024-04-23T18:22:41Z")

</div>

Thank you for the response!  
What I mean by “stop the code and rerun it”? is that when I flash the code and run it those counter starts incrementing. There is no problem in that but when I pause the code using the debugger and once again resume it for the execution then those counters don’t increment for certain period of time. (like 50 sec to 1 min). After that they start to increment. If once again I pause the running code and rerun, again same thing will happen.

In the second scenario when I put a breakpoint here,

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/d/d73359454ceb926362181e9f8ecbe81d683a6f1c.png)  
that breakpoint gets hit. But after that when I run the code counters will not increment.

I have used a configASSERT over here (Not in entire program).

status1 = xTaskCreate(vTask1\_handler, “Task\_1”, configMINIMAL\_STACK\_SIZE, NULL,2, &xTaskHandle1 );

configASSERT(status1 == pdPASS);

status2 = xTaskCreate(vTask2\_handler, “Task\_2”, configMINIMAL\_STACK\_SIZE, NULL,2, &xTaskHandle2 );

configASSERT(status2 == pdPASS);

Both the status 1 and status 2 values are 1.

Now, in my program tick rate is defined as 1000 and frequency is 300 MHz.

#define configCPU\_CLOCK\_HZ ( ( unsigned long ) 300000000UL )  
#define configTICK\_RATE\_HZ ( ( TickType\_t ) 1000UL )

#define configMAX\_PRIORITIES ( 10 )  
#define configMINIMAL\_STACK\_SIZE ( ( unsigned short ) 256 )  
#define configTOTAL\_HEAP\_SIZE ( ( size\_t ) ( 32U \* 1024U ) )  
#define configMAX\_TASK\_NAME\_LEN ( 16 )

My understanding is, please correct me if I am wrong, when we use xTaskDelayUntil in the task, then for that specific period of time let’s assume 5 ms that particular stack will be moved to block/ready state and Freertos scheduler will run the idle task. Once that timer is elapsed once again schedular will run the original task and this goes on and on.

But I think in my case schedular is getting stuck in idle task not able to resume the original task as there are no tasks present in ready list as value of pxReadyTasksLists is 1.

if( listCURRENT\_LIST\_LENGTH( &( pxReadyTasksLists[tskIDLE\_PRIORITY] ) ) \> ( UBaseType\_t ) 1 )  
{  
taskYIELD();  
}  
else  
{  
mtCOVERAGE\_TEST\_MARKER();  
}

Please note that as I mentioned earlier the counter starts incrementing after 50s to 1 min (Not specific) time after I pause and run the code again.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [April 23, 2024, 7:40pm UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/4 "2024-04-23T19:40:51Z")

</div>

Or is there an issue with your systick timer rep. systick handler ?  
Can you put a breakpoint there before resuming execution to see that it’s hit right after resuming as it should ?

---

<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:** [April 24, 2024, 8:48am UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/5 "2024-04-24T08:48:16Z")

</div>

What is your debugger? We may be looking at a tool problem here instead of a problem related to FreeRTOS.

---

<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:** [April 24, 2024, 7:38pm UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/6 "2024-04-24T19:38:30Z")

</div>

What is configUSE\_TICKLESS\_IDLE set to? If it is undefined in FreeRTOSConfig.h then it will default to 0.

---

<div class="post-metadata">

**Author:** ![danielglasser](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@danielglasser](https://forums.freertos.org/u/danielglasser)\
**Post date:** [April 24, 2024, 9:44pm UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/7 "2024-04-24T21:44:00Z")

</div>

> [@PSK](#):
>
> Please note that as I mentioned earlier the counter starts incrementing after 50s to 1 min (Not specific) time after I pause and run the code again.

Is there a chance that, while the processor is halted, the interrupt from the periodic timer that the scheduler uses is missed but the counter it’s using continues to advance, so the counter must wrap around back to the value the scheduler expects for the deadline to be detected?

---

<div class="post-metadata">

**Author:** ![PSK](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@PSK](https://forums.freertos.org/u/PSK)\
**Post date:** [April 25, 2024, 6:44am UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/8 "2024-04-25T06:44:02Z")

</div>

Hello All,  
Thank you for your responses.

Here, We are using **Lauterbach debugger**.  
Value of **configUSE\_TICKLESS\_IDLE** is not defined.

I want to address one point here. Right now, I flashed the one Freertos demo code **iLLD\_TC375\_ADS\_FreeRTOS\_Basic** present in aurix development studio.  
And I observed that my counters which are defined in the task are incrementing properly without using any delay function.

Code :–

void vTask1\_handler(void \* params)  
{  
while(1)  
{

```
    FreeRtos_Task1_Counter++;

}

```

}  
void vTask2\_handler(void \* params)  
{

```
while(1)
{
    FreeRtos_Task2_Counter++;

}

```

}

void vApplicationTickHook()  
{

```
TickHookCounter++;

```

}  
void vApplicationIdleHook( void )  
{

```
IDLEHookCounter++;

```

}

Values after starting the code :–

FreeRtos\_Task2\_Counter = 32662586  
FreeRtos\_Task1\_Counter = 32683002  
IDLEHookCounter = 0  
TickHookCounter = 1963  
xTickCount = 1963

But the problem is that when I stop the execution program and rerun it again  
my task counters start incrementing as soon as I run the code but x **TickCount** and **TickHookCounter** doesn’t increment for certain period of time.

**Scenario 1 -** Following are the values when I stop the code :–

FreeRtos\_Task2\_Counter = 111578373  
FreeRtos\_Task1\_Counter = 111595092  
IDLEHookCounter = 0  
TickHookCounter = 6705  
xTickCount = 6705

**Scenario 2** - Following are the values when I run the code again :–

FreeRtos\_Task2\_Counter = 111578373  
FreeRtos\_Task1\_Counter = 364759087  
IDLEHookCounter = 0  
TickHookCounter = 6706  
xTickCount = 6706

**Scenario 3 -** Following are the values after some time when the code is running :–

FreeRtos\_Task2\_Counter = 1188549687  
FreeRtos\_Task1\_Counter = 1508043176  
IDLEHookCounter = 0  
TickHookCounter = 71423  
xTickCount = 71424

**In scenario 2 task counters increments but xTickCount wont increment.**  
**In scenario 3 every counter increments.**

For your reference after scenario 1 which is after I stopped the code, I check the STM timer, and I got to know that even I have stooped the code STM timer is running.  
 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/b/b9f4834ae1ada6b7481496111ba4e9e1a67d932b.png)

Can anyone please help me here?  
I am stuck here for last couple of days.

Also, can someone explain me timer interrupt works in freertos?

One more thing, Also I don’t think that timer interrupt is generating at 1ms even my **configTICK\_RATE\_HZ** is 1000.

---

<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:** [April 25, 2024, 7:06am UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/9 "2024-04-25T07:06:11Z")

</div>

If xTickCount does not increment, your interrupts may be turned off. Check yur processor registers.

Again, I believe that you are dealing with an issue on the debugger side, not FreeRTOS operation.

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [April 25, 2024, 1:33pm UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/10 "2024-04-25T13:33:34Z")

</div>

Looks like if you (or the debugger) can configure the TC375 to halt the STM0 timer when execution is stopped in the debugger, then this problem will go away. Here is what seems to be the issue. The timer keeps counting while execution stops, so it pends a tick interrupt while execution is stopped. When execution resumes, the tick ISR finally executes. That tick ISR advances the compare register (STM0\_CMP0), but by that time the counter has already passed the new compare value. So the counter must count all the way around until it reaches the compare value again.

---

<div class="post-metadata">

**Author:** ![PSK](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@PSK](https://forums.freertos.org/u/PSK)\
**Post date:** [May 19, 2024, 2:53pm UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/11 "2024-05-19T14:53:20Z")

</div>

Hello,

My issue has been resolved. I changed the debugger, and my tasks were running properly.

Thank you everyone for your support.

Note : The code also works on Lauterbach debugger if we set the SUS register bit of STM as “Set Suspended”.

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/2/28a65bc6679d277af8c97b66f0f3ea97160b8109.png)

---

<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:** [May 20, 2024, 5:50am UTC](https://forums.freertos.org/t/issue-with-xtaskdelayuntil-api/19868/12 "2024-05-20T05:50:07Z")

</div>

Thank you for sharing your solution!
