# vTaskDelayUntil doesn't wait ?

**URL:** <https://forums.freertos.org/t/vtaskdelayuntil-doesnt-wait/3352>\
**Category:** Kernel\
**Created:** [February 8, 2008, 10:52pm UTC](https://forums.freertos.org/t/vtaskdelayuntil-doesnt-wait/3352 "2008-02-08T22:52:52Z")\
**Posts on this page:** 3\
**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:** [February 8, 2008, 10:52pm UTC](https://forums.freertos.org/t/vtaskdelayuntil-doesnt-wait/3352/1 "2008-02-08T22:52:52Z")

</div>

**[martin\_cibulski](https://sourceforge.net/u/martin_cibulski/)** wrote on [Friday, February 08, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/91aa9cb5/#d07d):

Hello,

I’m programming a motor controller in a task.  
To have a constant schedule for my controller I use the vTaskDelayUntil function.  
But I found that my controller worked much too fast.

So I wrote some test code into my task to find out how long vTaskDelayUntil actually waits.  
(I don’t show all code to keep my posting small)  
- - - - -

#define MOT\_TASK\_PERIOD&nbsp;&nbsp;&nbsp; ((portTickType)100)  
.  
.  
.  
portTASK\_FUNCTION (task\_mot, pParameters \_\_attribute\_\_ ((unused)))  
{  
&nbsp;&nbsp;&nbsp; static&nbsp;&nbsp;&nbsp; portTickType&nbsp;&nbsp;&nbsp; xLastWakeTime = 500;

&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nMot;  
&nbsp;&nbsp;&nbsp; MOTOR\*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pMot;  
&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nSpeedStep;  
&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nTick1, nTick2;  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; DEBUG\_LINE ("task\_mot(.)");  
&nbsp;&nbsp;&nbsp; i2c\_init (MOT\_I2C\_BUS\_NO, MOT\_I2C\_KHZ);  
&nbsp;&nbsp;&nbsp; mot\_init ();

&nbsp;&nbsp;&nbsp; while (1) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nTick1 = xTaskGetTickCount();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vTaskDelayUntil (&xLastWakeTime, MOT\_TASK\_PERIOD);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nTick2 = xTaskGetTickCount();  
.  
.  
.  
- - - - -

When I debug the task and stop behind these statements, the variables  
have the following values:  
nTick1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp; (OK, only 3 milliseconds since system start)  
xLastWakeTime&nbsp; 600&nbsp; (which is 500 + MOT\_TASK\_PERIOD)  
nTick2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp; (should be at least 600)

Why is nTick2 not 600 or more ?  
vTaskDelayUntil should wait until 600 Ticks have been counted, right ?

I use FreeRTOS 4.7.1 on an LPC2148 with GCC.  
The same happened with an earlier release (4.5 ?).

What did I do wrong here ?

Thanks for help,  
Martin

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 9, 2008, 12:08pm UTC](https://forums.freertos.org/t/vtaskdelayuntil-doesnt-wait/3352/2 "2008-02-09T12:08:21Z")

</div>

**[woops\_](https://sourceforge.net/u/woops_/)** wrote on [Saturday, February 09, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/91aa9cb5/#d07d/0d70):

You initialized xLastWakeTime to an arbitrary value and this probably makes the scheduler think the actual time has overflowed since vTaskDelayUntil was called. You can step into vTaskDelayUntil to check this. Try initializing the xLastWakeTime to the actual time as shown in the API doc example. If this does not fix your problem check the tick is actually running.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 9, 2008, 5:02pm UTC](https://forums.freertos.org/t/vtaskdelayuntil-doesnt-wait/3352/3 "2008-02-09T17:02:10Z")

</div>

**[martin\_cibulski](https://sourceforge.net/u/martin_cibulski/)** wrote on [Saturday, February 09, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/91aa9cb5/#d07d/99eb):

Yes, initializing the xLastWakeTime to the actual time solved my problem.  
Now my task runs every 100 ms as exspected.

Thank you,  
Martin
