FreeRTOS – Delaying Tasks
Keywords: Embedded systems, ARM, FreeRTOS, STM32F4, Tasks Delay, Tasks
Code Link: Tutorial Source Code Github (vTaskDelay)- Keil
Code Link: Tutorial Source Code Github (vTaskDelayUntil)- Keil
By default any FreeRTOS task should run indefinitely under normal operation. This means a task will always be scheduled to run independent of whether it is required or not as long as it remains in ready-state.
Consider a scenario in which a Microcontroller is connected to a sensor which is read continuously after specific time interval say 1-sec (e.g. a system tracking time in seconds from an RTS). If a FreeRTOS task is scheduled before 1-sec, then it will be useless consumption of valuable processor cycles as 1-measurement/sec is required. This can affect system real-time efficiency in which a task is only run when it is required to run.
In such cases a sensor reading task needs to be delayed/prevent from execution until time of 1-sec is elapsed even if the task is in ready state – Figure-1.

In order to meet such requirements, FreeRTOS provides two APIs that delay a task from required amount of time.
void vTaskDelay( const TickType_t xTicksToDelay )
void vTaskDelayUntil( TickType_t *pxPreviousWakeTime, const TickType_t xTimeIncrement )
1. vTaskDelay() :
vTaskDelay()
specify the amount of time in Tick Timer Ticks during which the task needs to be sent to block (not running) state. For example, specifying a block period of 100 ticks will cause the task to unblock 100 ticks after vTaskDelay()
is called!
Parameter Name | Description |
---|---|
xTicksToDelay | The amount of time, in Ticks, that the calling task should block. |
Return Value | void |
Note: vTaskDelay()
causes a task to be delayed for at least xTicksToDelay
Ticks. It doesn’t ensure that the task will be immediately scheduled/invoked after xTicksToDelay
ticks delay unless there is no other tasks (of same or higher priority) in system. The reason behind this behavior is that the task is delayed only after vTaskDelay()
is called. e.g. consider the following sample task using vTaskDelay()
API.
/* | Sample Task function | */ | void myTaskFtn (void * pvParam) { | | for (;;) { | | /* Task implementation */ | <------ Point-1 | ... | <-- [some other task preempt this task here] | vTaskDelay(50); | <------ Point-2 } | } |
The myTaskFtn implementation is expected be delayed repeatedly for 50 Ticks. But if by any chance another task preempted the myTaskFtn between Point-1 and Point-2 then the actual delay will be 50-Ticks + the execution time of that other task. More predictable delay can be generated via vTaskDelayUntil().
Note: INCLUDE_vTaskDelay must be defined as 1 in FreeRTOSConfig.h file for this function to be available.
2. vTaskDelayUntil() :
Unlike vTaskDelay() which produces relative time delay from the point onward the vTaskDelay() is called, the vTaskDelayUntil() produces more accurate delay using absolute time reference. The delay produces is independent of when vTaskDelayUntil() is called.
Parameter Name | Description |
---|---|
pxPreviousWakeTime | The starting point (time stamp) for the vTaskDelayUntil() to count onward. This variable is automatically updated within vTaskDelayUntil() which means it need to be initialized only once in code. |
xTimeIncrement | The amount of time, in Ticks, after the starting point i.e. pxPreviousWakeTime that the calling task will remain in block state. |
Return Value | void |
Question: Both vTaskDelay()
and vTaskDelayUntil()
have xTimeIncrement
parameter that specify the time in Ticks that the calling task will be delayed! The question is, Can we specify time in milliseconds
instead of Ticks?
Answer: Yes. FreeRTOS defines a MACRO portTICK_PERIOD_MS
in portmacro.h
that specify time in milliseconds between two consective Tick timer Ticks. So in order to covert any time to equivalent ticks use the following formula.

Let’s say we want to delay a task for 50msec. The easy way to use the Delay APIs for the delay in milliseconds is:
void vTaskDelay( 50 / portTICK_PERIOD_MS ) void vTaskDelayUntil( pxPreviousWakeTime , 50 / portTICK_PERIOD_MS )
Tutorial Scenario:
This tutorial consists of two parts. Each part implements a single FreeRTOS Task that is responsible for counting the number of elapsed time in seconds. In the first part we will use vTaskDelay()
Delay API while in the second part we will use vTaskDelayUntil()
API to generate 1-sec delay.
Let’s get back to programming. Following are the steps.
Steps:
1. The first step is to create a FreeRTOS Task via xTaskCreate()
API which will be scheduled and will run after each 1-sec delay.
/* Simple FreeRTOS Task */ xTaskCreate (vTask, "T1", 150, NULL, 1, NULL);
Note: If you don’t know how to create freeRTOS Tasks, refer to the following Tutorial.
The subsequent steps are to implement first part of tutorial i.e. using xTaskCreate() API to generate 1-second delay.
2. The next step is to implement vTask function given as an argument to xTaskCreate()
API in the step-1. The vTask function for vTaskDelay()
API (first part of tutorial) is given bellow.
/* Task function for vTaskDelay() API demonstration */ void vTask(void * pvParams) { printf("Clock Task Start Running...\n"); for (;;) { printf("Seconds Count: %d\n", counter++); /* create 1-second (1000-msec) delay. */ vTaskDelay(1000/ portTICK_PERIOD_MS); } }
As can be seen from the above function implementation, when the Task is executed for the first time, it prints message Clock Task Start Running… as an indication of start seconds counting clock/task. It then prints the value of a global variable counter which holds the number of seconds elapsed (see source code from github link).
Once the task prints the seconds counts message, it sends the task to block state for 1-second by calling FreeRTOS delay API vTaskDelay()
discussed before. Once 1-second is elapsed, it calls back prints the seconds being counted and the cycle repeats.
The problem with this approach is that to delay API is called after the print message i.e. printf("Seconds Count: %d\n", counter++);.
which means the total time taken will be 1-second (generated by vTaskDelay()
) plus the time taken to by the printf("Seconds Count: %d\n", counter++);
statement. Thats the reason why vTaskDelay()
API is not suitable for accurate time delay with reference to scheduler time line.
3. The last step is to start the scheduler to allow created tasks to run.
/* Start the Scheduler */ vTaskStartScheduler();
Note: We have routed printf messages to ST-Link debugger via ARM-ITM. There is a dedicated tutorial on how to redirect printf messages to debugger. Link to the tutorial is given bellow.
Video demonstration:
For complete source code refer to Github link given at the start of this tutorial.
Click the full screen button for more clear view.
Now let’s move to second part to implement vTaskDelayUntil()
API to generate 1-second delay. We will just repeat 2,3 parts of previous first part.
2. Following is the vTask
function implementation for vTaskDelayUntil()
API.
/* Task function for vTaskDelayUntil() API demonstration */ void vTask(void * pvParams) { TickType_t xLastWakeTime; /* 1000-msec in terms of tick */ const TickType_t xFrequency = 1000/ portTICK_PERIOD_MS; /* stamp current time... It is only required once. it will be updated automatically with every call to vTaskDelayUntil() API. */ xLastWakeTime = xTaskGetTickCount(); printf("Clock Task Start Running...\n"); for (;;) { printf("Seconds Count: %d\n", counter++); /* create 1-second (1000-msec) delay. */ vTaskDelayUntil(&xLastWakeTime, xFrequency); } }
The code is quite self explanatory. For any doubt refer to the vTaskDelayUntil() API explanation, discussed earlier.
3. The last step is to start the scheduler to allow created tasks to run.
/* Start the Scheduler */ vTaskStartScheduler();
Note: We have routed printf messages to ST-Link debugger via ARM-ITM. There is a dedicated tutorial on how to redirect printf messages to debugger. Link to the tutorial is given bellow.
Video demonstration:
For complete source code refer to Github link given at the start of this tutorial.
Click the full screen button for more clear view.
Undeniably believe that which you said. Your favourite reason seemed to be
at the internet the simplest thing to have in mind of. I say to you, I definitely get annoyed whilst other
people think about concerns that they plainly do not understand about.
You managed to hit the nail upon the top and defined out the entire thing without having side effect ,
other people could take a signal. Will probably be again to get more.
Thank you asmr 0mniartist
Good article! We will be linking to this particularly great article on our website.
Keep up the good writing. 0mniartist asmr
Right here is the perfect web site for everyone who really wants to understand this topic.
You understand so much its almost tough to argue with you (not
that I personally would want to…HaHa). You definitely put a new spin on a
topic that’s been written about for decades. Great stuff, just excellent!
asmr 0mniartist
I know this if off topic but I’m looking into starting my own blog and was curious
what all is required to get setup? I’m assuming having a blog like yours would cost a pretty penny?
I’m not very internet savvy so I’m not 100%
sure. Any tips or advice would be greatly appreciated.
Thank you 0mniartist asmr
each time i used to read smaller articles that also clear their motive, and that is
also happening with this article which I am reading now.
asmr 0mniartist
I really like what you guys are up too. Such clever work and exposure!
Keep up the amazing works guys I’ve included you guys to my
blogroll.
Every weekend i used to visit this site,
as i wish for enjoyment, for the reason that this
this site conations genuinely nice funny information too.
These are truly great ideas in concerning blogging. You have touched some pleasant things here.
Any way keep up wrinting.
You ought to be a part of a contest for one of the best sites
on the internet. I will recommend this site!
Wow! This blog looks just like my old one! It’s on a
totally different topic but it has pretty much the same page layout and design. Wonderful choice of colors!
Today, I went to the beach with my kids. I found a sea
shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell
to her ear and screamed. There was a hermit crab
inside and it pinched her ear. She never wants to go back!
LoL I know this is entirely off topic but I had to tell someone!
Wonderful post! We will be linking to this great content on our site.
Keep up the good writing.
Hi there, I want to subscribe for this blog to get latest updates,
therefore where can i do it please help out.
I got this web page from my buddy who shared with me on the topic
of this site and now this time I am visiting this site and reading very informative articles here.
scoliosis
I’ve learn some excellent stuff here. Certainly value bookmarking for revisiting.
I surprise how so much effort you put to make one of these fantastic informative web
site. scoliosis
scoliosis
I used to be able to find good info from your blog posts.
scoliosis
scoliosis
For most up-to-date information you have to go to
see web and on internet I found this site as a finest site for
newest updates. scoliosis
free dating sites
You actually make it appear so easy together with your presentation however I
in finding this topic to be actually something that I think
I might never understand. It sort of feels too complicated and extremely vast for
me. I’m having a look forward in your next submit, I will try
to get the dangle of it! https://785days.tumblr.com/ free dating sites
dating sites
I will immediately snatch your rss as I can’t find your e-mail subscription hyperlink or newsletter service.
Do you’ve any? Kindly let me realize so that
I may just subscribe. Thanks. free dating sites
It’s the best time to make some plans for the longer term and it’s time to be happy.
I have read this post and if I may I desire to
recommend you some fascinating things or advice.
Maybe you could write subsequent articles regarding this article.
I want to learn even more issues approximately it!
Article writing is also a excitement, if you know afterward
you can write if not it is difficult to write.
Hello, I enjoy reading through your article post.
I wanted to write a little comment to support you.
Actually no matter if someone doesn’t be aware of then its up to other viewers
that they will help, so here it takes place.
Having read this I believed it was very enlightening. I appreciate you finding the time and energy to put this article
together. I once again find myself personally spending a lot of time both reading and commenting.
But so what, it was still worthwhile!
Your means of telling the whole thing in this paragraph is in fact pleasant, every one be capable of simply know it, Thanks
a lot.
An intriguing discussion is definitely worth comment.
I do think that you need to write more about this
issue, it might not be a taboo subject but typically people do not speak about such topics.
To the next! Kind regards!!
It’s hard to come by knowledgeable people in this
particular topic, but you seem like you know what you’re talking
about! Thanks
Great blog article.Really looking forward to read more. Much obliged.
Very nice post. I just stumbled upon your weblog and wanted to say that I’ve
really enjoyed browsing your blog posts. After all I’ll be subscribing to your rss feed
and I hope you write again very soon!
What i don’t understood is actually how you are not actually much more well-preferred than you
may be now. You’re so intelligent. You recognize therefore considerably in terms of this matter, produced me individually believe it from
so many numerous angles. Its like women and men don’t seem to be interested unless it is
something to accomplish with Woman gaga! Your individual
stuffs nice. Always handle it up!
Quality articles is the main to invite the visitors to pay a quick visit
the site, that’s what this web page is providing.
Hi my loved one! I wish to say that this post is amazing, great written and
come with almost all significant infos. I’d like to peer more posts
like this .
Hey, I think your website might be having browser compatibility
issues. When I look at your blog in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up! Other
then that, very good blog!
You’ve made some decent points there. I checked on the net for more info
about the issue and found most individuals will go along
with your views on this site.
An outstanding share! I have just forwarded this onto a colleague who
has been doing a little homework on this.
And he actually ordered me lunch because I discovered it for
him… lol. So let me reword this…. Thanks
for the meal!! But yeah, thanks for spending some time to talk about this subject here
on your internet site.
It’s actually a nice and useful piece of info. I’m
glad that you just shared this helpful information with us.
Please keep us informed like this. Thanks for sharing.
Highly descriptive post, I loved that a lot. Will
there be a part 2?
Great post. I was checking continuously this weblog
and I’m inspired! Extremely useful information specifically the ultimate phase 🙂 I care for such info a lot.
I was looking for this certain information for a long time.
Thanks and good luck.
Hey there just wanted to give you a quick heads up.
The words in your article seem to be running off the screen in Chrome.
I’m not sure if this is a format issue or something to
do with internet browser compatibility but I figured I’d post to let you know.
The style and design look great though! Hope you get the issue fixed soon. Cheers
Hello! I just would like to offer you a huge thumbs up for the great info you have right
here on this post. I’ll be returning to your website for more soon.
Hi exceptional blog! Does running a blog similar to this require a large amount of work?
I have no expertise in coding however I had been hoping to start
my own blog soon. Anyhow, if you have any ideas or techniques for new blog owners please
share. I understand this is off topic nevertheless I just wanted
to ask. Thank you!
Every weekend i used to pay a visit this web site, because
i wish for enjoyment, for the reason that this this web
site conations really good funny stuff too.
Appreciate this post. Let me try it out.
Oh my goodness! Awesome article dude! Thank you so much, However I am having issues with your RSS.
I don’t understand the reason why I can’t join it. Is there anybody having the same RSS problems?
Anyone who knows the answer will you kindly respond?
Thanx!!
Magnificent beat ! I wish to apprentice while you amend your web site, how could i
subscribe for a blog web site? The account helped me
a acceptable deal. I had been a little bit acquainted of
this your broadcast offered bright clear idea
It’s perfect time to make some plans for the future and it’s time to
be happy. I’ve read this post and if I could I want to suggest you few interesting things or tips.
Perhaps you can write next articles referring to this article.
I desire to read even more things about
it!
Simply desire to say your article is as astonishing.
The clarity in your post is just excellent and i can assume you’re knowledgeable on this subject.
Fine along with your permission let me to snatch your RSS feed to keep up to date with
drawing close post. Thanks 1,000,000 and please continue the gratifying work.
Hmm is anyone else having problems with the images
on this blog loading? I’m trying to find out if its a problem on my
end or if it’s the blog. Any suggestions would be greatly appreciated.
I like what you guys are usually up too. This sort of clever work and exposure!
Keep up the fantastic works guys I’ve included you guys to blogroll.
I really like your blog.. very nice colors & theme.
Did you make this website yourself or did you hire someone to do it for you?
Plz respond as I’m looking to construct my own blog and would like to find out
where u got this from. thanks quest bars http://j.mp/3jZgEA2 quest bars
It’s very effortless to find out any matter on net as compared to textbooks, as I found this article at this web site.
asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr
I used to be able to find good advice from your blog posts.
cheap flights http://1704milesapart.tumblr.com/ cheap flights
Good website! I truly love how it is simple on my eyes and the data are well written. I am wondering how I could be notified when a new post has been made. I have subscribed to your RSS which must do the trick! Have a nice day!
I truly wanted to jot down a small note in order to say thanks to you for all the splendid suggestions you are giving out here. My long internet look up has at the end been paid with wonderful strategies to write about with my great friends. I ‘d repeat that we readers are really blessed to exist in a useful site with so many lovely people with very helpful pointers. I feel somewhat privileged to have used your website page and look forward to tons of more entertaining times reading here. Thank you once more for all the details.
Hey very nice blog! ps4 games https://bitly.com/3nkdKIi ps4
games
Hi my loved one! I want to say that this post is awesome, nice written and include
approximately all important infos. I’d like to look more posts like this .
scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery
I have to thank you for the efforts you have put in penning
this site. I am hoping to view the same high-grade content from you later on as well.
In truth, your creative writing abilities has inspired
me to get my own site now 😉 quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
Quality articles is the key to attract the people to visit the web site, that’s what this web site is providing.
scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
I found your weblog web site on google and test a couple of of your early posts. Proceed to keep up the excellent operate. I just further up your RSS feed to my MSN News Reader. Searching for ahead to reading more from you afterward!…
When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!
This web site is really a walk-through for all of the info you wanted about this and didn?t know who to ask. Glimpse here, and you?ll definitely discover it.
I?d have to check with you here. Which is not something I usually do! I enjoy reading a post that will make people think. Also, thanks for allowing me to comment!
I discovered your blog site on google and check a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!?
The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.
Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.
After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.
Oh my goodness! an amazing article dude. Thank you However I am experiencing issue with ur rss . Don?t know why Unable to subscribe to it. Is there anyone getting identical rss problem? Anyone who knows kindly respond. Thnkx
There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game. Both boys and girls feel the impact of just a moment?s pleasure, for the rest of their lives.
When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!
Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.
This is the right blog for anyone who wants to find out about this topic. You realize so much its almost hard to argue with you (not that I actually would want?HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, just great!
There is noticeably a bundle to know about this. I assume you made certain nice points in features also.
There are some interesting points in time in this article but I don?t know if I see all of them center to heart. There is some validity but I will take hold opinion until I look into it further. Good article , thanks and we want more! Added to FeedBurner as well
Your place is valueble for me. Thanks!?
WONDERFUL Post.thanks for share..more wait .. 😉 ?
An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers
After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.
I was recommended this web site by my cousin. I’m
not sure whether this post is written by him as no one else know such detailed
about my problem. You’re wonderful! Thanks! https://parttimejobshiredin30minutes.wildapricot.org/ part time jobs hired in 30 minutes
There’s certainly a great deal to know about this issue.
I love all the points you made.
I do trust all of the ideas you’ve presented for your post. They are very convincing and can definitely work. Nonetheless, the posts are too quick for starters. May just you please prolong them a little from subsequent time? Thank you for the post.
I appreciate you sharing this blog article.Really thank you! Great.
If some one desires expert view concerning blogging and site-building afterward i advise him/her to visit this website, Keep up the good work.
Your style is very unique in comparison to other folks I have read
stuff from. I appreciate you for posting when you
have the opportunity, Guess I’ll just bookmark this blog.
Hello, i think that i saw you visited my site thus i came to “return the favor”.I’m trying to find things to improve my web
site!I suppose its ok to use some of your ideas!!
You made some decent points there. I checked on the internet for more
information about the issue and found most people will go along with your
views on this website.
Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some time and
was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy reading your blog and I
look forward to your new updates.
I’m amazed, I have to admit. Seldom do I come across a blog
that’s equally educative and engaging, and let me tell you,
you have hit the nail on the head. The problem is something that too few people are speaking
intelligently about. I’m very happy I came across this during my
search for something relating to this.
I love your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz respond as I’m looking to construct my own blog and would like to know where u got this from. kudos
Thank you for sharing superb informations. Your website is so cool. I am impressed by the details that you’ve on this blog. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found simply the information I already searched everywhere and simply couldn’t come across. What an ideal web site.
I was just searching for this information for some time. After six hours of continuous Googleing, at last I got it in your site. I wonder what is the lack of Google strategy that don’t rank this type of informative web sites in top of the list. Normally the top websites are full of garbage.
It’s really a great and useful piece of information. I am glad that you shared this useful info with us. Please keep us informed like this. Thanks for sharing.
Hey there! I know this is kind of off topic but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having trouble finding one? Thanks a lot!
It is really a nice and helpful piece of info. I’m glad that you shared this useful information with us. Please keep us informed like this. Thank you for sharing.