# Problems with vTaskDelayUntil on ATmega323

**URL:** <https://forums.freertos.org/t/problems-with-vtaskdelayuntil-on-atmega323/5655>\
**Category:** Kernel\
**Created:** [February 23, 2005, 10:45am UTC](https://forums.freertos.org/t/problems-with-vtaskdelayuntil-on-atmega323/5655 "2005-02-23T10:45:11Z")\
**Posts on this page:** 6\
**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 23, 2005, 10:45am UTC](https://forums.freertos.org/t/problems-with-vtaskdelayuntil-on-atmega323/5655/1 "2005-02-23T10:45:11Z")

</div>

**[rapurvey](https://sourceforge.net/u/rapurvey/)** wrote on [Wednesday, February 23, 2005](https://sourceforge.net/p/freertos/discussion/382005/thread/fc0d5a3c/#1fa0):

I have currently written a test application with just 2 tasks using preemptive scheduling.&nbsp; Each task toggles a bit on a port and then delays using vTaskDelayUntil for a period of time.

The problem is that I am getting strange results as one task will run for say 100 iterations and then the other task will keep running not letting the first task get any processing time.&nbsp; This all seems to work ok though if I use vTaskDelay instead.

Im running this in simulation mode in AVRStudio as I havnt got any hardware yet.

Does anybody have any ideas?

---

<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:** [February 23, 2005, 10:57am UTC](https://forums.freertos.org/t/problems-with-vtaskdelayuntil-on-atmega323/5655/2 "2005-02-23T10:57:24Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, February 23, 2005](https://sourceforge.net/p/freertos/discussion/382005/thread/fc0d5a3c/#1fa0/2ebc):

Hi,

Just to clarify - are you seeing that the application executes correctly with both tasks executing as expected until a certain time - after which the problem occurs?

I presume you are using WinAVR (or some other verison of GCC).&nbsp; Could you send me your project so I can try it?&nbsp; If so zip up the entire directory (including source and demo) and mail to the address from the Contacts page on the WEB site.&nbsp; Can you also include a few lines on how the project is built if this is different from the standard buildcoff or buildhex batch files.

Regards,  
Richard.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 23, 2005, 11:16am UTC](https://forums.freertos.org/t/problems-with-vtaskdelayuntil-on-atmega323/5655/3 "2005-02-23T11:16:30Z")

</div>

**[rapurvey](https://sourceforge.net/u/rapurvey/)** wrote on [Wednesday, February 23, 2005](https://sourceforge.net/p/freertos/discussion/382005/thread/fc0d5a3c/#1fa0/2ebc/755b):

The application works as expected using vTaskDelay but using vTaskDelayUntil causes the application perform inconsistantly depending on the priority and delays of the tasks.

Im using WinAVR (the latest).&nbsp; Ill send the project to you straight away.

---

<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:** [February 23, 2005, 12:00pm UTC](https://forums.freertos.org/t/problems-with-vtaskdelayuntil-on-atmega323/5655/4 "2005-02-23T12:00:24Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, February 23, 2005](https://sourceforge.net/p/freertos/discussion/382005/thread/fc0d5a3c/#1fa0/2ebc/755b/1890):

Hi,

The first parameter to vTaskDelayUntil() is the last execution time - which gets updated by vTaskDelayUntil() so it is correct for the next call.

In the code you sent the variable mxLastWakeTime is global - but used by two different tasks.&nbsp; This means that one task will corrupt the value used by the other.

Try placing mxLastWakeTime on the stack for each task (and remove the global variable), for example:

void vTask1( void \* pv )  
{  
…// Declare it here  
…portTickType mxLastWakeTime;

…// Init it here  
…mxLastWakeTime = xTaskGetTickCount();

…while(1)  
…{  
…// Your code task 1 code …

…vTaskDelayUntil( &mxLastWakeTime, 5 );  
…}  
}

void vTask2( void \* pv )  
{  
…// Declare it here  
…portTickType mxLastWakeTime;

…// Init it here  
…mxLastWakeTime = xTaskGetTickCount();

…while(1)  
…{  
…// Your task 2 code …

…vTaskDelayUntil( &mxLastWakeTime, 10 );  
…}  
}

This way each task has it’s own mxLastWakeTime value.

One other small thing.&nbsp; mxLastWakeTime should not be initialised until the scheduler has been started.&nbsp; This is because the tick count variable is not initialised until this time.&nbsp; In this case this does not really matter as the tick count should be set to 0 by the C startup code anyway, and moving the variable to the stack means the scheduler must be started for the code to run.

---

<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:** [February 23, 2005, 12:01pm UTC](https://forums.freertos.org/t/problems-with-vtaskdelayuntil-on-atmega323/5655/5 "2005-02-23T12:01:34Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, February 23, 2005](https://sourceforge.net/p/freertos/discussion/382005/thread/fc0d5a3c/#1fa0/dec9):

Forgot to say - post again if this doesn’t solve the problem.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 23, 2005, 12:08pm UTC](https://forums.freertos.org/t/problems-with-vtaskdelayuntil-on-atmega323/5655/6 "2005-02-23T12:08:18Z")

</div>

**[rapurvey](https://sourceforge.net/u/rapurvey/)** wrote on [Wednesday, February 23, 2005](https://sourceforge.net/p/freertos/discussion/382005/thread/fc0d5a3c/#1fa0/2ebc/755b/1890/02bc):

Thanks Richard, that works now. 🙂
