FreeRTOS – Task Yielding
Keywords: Embedded systems, ARM, FreeRTOS, STM32F4, Tasks, Tasks Yielding
Code Link: Tutorial Source Code Github – Keil
As we know from introductory tutorial that scheduler divides time line into small time slots based on tick timer. In each time slot only one task is selected and allowed to run. When the tick timer ticks again, another ready task is selected to run while the current task is sent to ready state.
Whats the problem with this approach?
As each task is allowed fixed time slot based on Tick timer ticking frequency, what if a task requires more time to complete than the alloted time slot? Similarly if a task completes its functionality before its time slot finishes than what should it do? For example a task dedicated to take some sensors data on regular bases – Figure-1. Lets say data acquisition requires 10% of time slot than what the task suppose to do in remaining 90% time?
In FreeRTOS a task runs indefinitely. It can’t return whether it finishes earlier or past the time slot. But FreeRTOS has a way around for tasks that completed their functionality earlier. The solution is called Task Yielding. In FreeRTOS a task can handover processor cycles (meant for it) any time it wants by calling FreeRTOS Task Yielding API. Using Task Yielding API a task can inform Scheduler to hand over processor cycles to another ready task as it has completed its functionality and can’t wait for tick timer based context switching!

The FreeRTOS Task Yielding API is given bellow.
void taskYIELD( void )
void taskYIELD(void):
taskYIELD()
is used to request a context switch to another task. Yielding occurs when a task volunteers to leave the Running state without being preempted and before its time slice has been fully used.taskYIELD()
must only be called from an executing task and therefore must not be called while the scheduler is in the Initialization state (before the scheduler is started).
When a task calls taskYIELD()
, the scheduler will select another Ready state task of equal priority to enter the Running state in its place. If there are no other Ready state tasks of equal priority, then the task that called taskYIELD()
will be transitioned into the Running state.
Parameter Name | Description |
---|---|
void | void |
Return Value | void |
In all our previous tutorials we put some delay in each task to ensure task consumes enough time to exceed time slot and same functionality is not executed multiple times with the same time slot. But real time Embedded Systems are quite different. They have deadlines to meet. They can’t wast time just to consume complete time slot. In real time systems, if a task completes, it must allow other tasks in queue to take consume processor cycles. The purpose of task yielding is simply to allow other tasks to execute once it completes its own functionality.
Tutorial Scenario:
In this tutorial we will create two FreeRTOS tasks. Each task when runs, it simply prints a message. After printing message it has nothing else to do, so instead of waiting for the context switch, it simply Yield to scheduler by calling FreeRTOS Scheduler API i.e. taskYIELD()
.
Following are the tutorial steps.
Steps:
1. The first step is to create two FreeRTOS Tasks i.e. “T1” and “T2” are created.
Note: If you don’t know how to create freeRTOS Tasks, refer to the following Tutorial.
const int task_1_Param = 1; const int task_2_Param = 2; xTaskCreate (vTask, "T1", 150, (void *)&task_1_Param, 1, NULL); xTaskCreate (vTask, "T2", 150, (void *)&task_2_Param, 1, NULL);
2. Next Step is to create a task function which will be called by FreeRTOS Tasks when scheduled.
void vTask(void * pvParams) { volatile unsigned int i = 0; const int * tParam = (const int *)pvParams; for (;;) { /* Do what every Task is meant to do */ printf("Task-%d Running.\n", *tParam); /* Now that task has completed its function, so let's yield so that other tasks are waiting to be scheduled are allowed to run. */ printf("Task-%d Complete - Yielding Task.\n\n\n", *tParam); taskYIELD(); } }
As can be seen from above task function implementation, the task prints the a message to debug console and once it prints the message it has noting more to do. So in order to handover processor to other task it simply calls the task Yield API taskYIELD(). Calling the taskYIELD() immediately calls initiate context switch and allow other task to run before the tick timer based context switch occurs.
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.
For complete source code refer to Github link given at the start of this tutorial.
Click the full screen button for more clear view.
I love your blog.. very nice colors & theme.
Did you create this website yourself or did you hire someone to do it for you?
Plz reply as I’m looking to construct my own blog and would like to find out where u got this from.
kudos asmr 0mniartist
Wonderful goods from you, man. I have bear in mind your stuff prior to and you’re
just too great. I actually like what you have acquired right here, really like
what you are saying and the way through which you assert
it. You’re making it enjoyable and you continue to take care
of to stay it wise. I can’t wait to read far more from you.
This is really a great website. asmr 0mniartist
Hi! I’m at work browsing your blog from my new
iphone! Just wanted to say I love reading through your blog and look forward to all your posts!
Keep up the great work! 0mniartist asmr
I’m really loving the theme/design of your website.
Do you ever run into any web browser compatibility
issues? A number of my blog audience have complained about
my blog not working correctly in Explorer but looks great in Chrome.
Do you have any advice to help fix this problem? 0mniartist asmr
Today, I went to the beachfront with my children. 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 totally off
topic but I had to tell someone! asmr 0mniartist
Wow that was strange. I just wrote an extremely long comment but after I clicked submit my comment didn’t show up.
Grrrr… well I’m not writing all that over again. Anyways, just
wanted to say great blog!
continuously i used to read smaller articles or reviews that also clear
their motive, and that is also happening with this paragraph which I am reading now.
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.
Hey there! I just wanted to ask if you ever have any issues with hackers?
My last blog (wordpress) was hacked and I ended up losing many months of hard
work due to no backup. Do you have any methods to protect against
hackers?
I do not know whether it’s just me or if perhaps everybody else experiencing issues with your website.
It appears like some of the text within your content are running off the screen. Can someone else
please provide feedback and let me know if this is happening to them too?
This might be a problem with my web browser because I’ve had
this happen previously. Cheers
What’s up to every body, it’s my first pay a visit of this blog;
this blog includes awesome and genuinely fine information for readers.
It is appropriate time to make some plans for the long run and it’s time to be
happy. I have read this submit and if I may I wish to counsel you few attention-grabbing
things or tips. Perhaps you can write subsequent
articles referring to this article. I want to learn more issues approximately it!
My brother recommended I would possibly like this website.
He was once totally right. This submit actually made my day.
You cann’t believe simply how so much time I had spent for this info!
Thank you!
What a data of un-ambiguity and preserveness of precious knowledge on the topic
of unexpected emotions.
scoliosis
Definitely believe that which you said. Your favorite justification seemed to be on the internet the
easiest thing to be aware of. I say to you, I definitely
get annoyed while people consider worries that
they just don’t know about. You managed to hit the nail upon the top
and also defined out the whole thing without having side-effects , people
could take a signal. Will likely be back to get more.
Thanks scoliosis
scoliosis
Howdy this is kinda of off topic but I was wanting
to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding skills so I wanted to get advice from someone with experience.
Any help would be enormously appreciated! scoliosis
scoliosis
Magnificent web site. A lot of helpful info here.
I am sending it to a few pals ans also sharing in delicious.
And naturally, thanks on your sweat! scoliosis
dating sites
I’m curious to find out what blog platform you have been using?
I’m having some small security problems with my latest blog and I
would like to find something more secure.
Do you have any solutions? dating sites https://785days.tumblr.com/
free dating sites
Wow, amazing blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your website is excellent, let alone the content!
free dating sites
What’s up, its good article regarding media print, we all be aware
of media is a enormous source of data.
Hi are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and create my own. Do you need any
html coding expertise to make your own blog? Any help would be greatly appreciated!
Good info. Lucky me I recently found your site by accident (stumbleupon).
I’ve bookmarked it for later!
Very good article. I certainly love this website.
Keep it up!
I always used to study paragraph in news papers but now as I am a user of net therefore from now I am using net for articles, thanks to web.
I’ve been browsing online more than three hours today, yet I never found
any interesting article like yours. It is pretty worth enough for me.
In my opinion, if all site owners and bloggers made good content as you did, the net will be a lot more
useful than ever before.
Howdy! Do you know if they make any plugins to safeguard
against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?
Fantastic blog post.Really thank you! Fantastic.
You made some decent points there. I checked on the internet to learn more about the issue and found most people
will go along with your views on this website.
Definitely consider that which you stated. Your favorite justification seemed to be on the web the
simplest thing to understand of. I say to you,
I certainly get annoyed at the same time as people think about worries
that they just don’t realize about. You controlled to hit
the nail upon the top and defined out the entire thing without having side-effects , folks
can take a signal. Will probably be again to get more. Thanks
Thankfulness to my father who told me about this
blog, this web site is truly awesome.
obviously like your web-site however you have to take a look at the spelling on several
of your posts. Several of them are rife with spelling problems and I to find it
very bothersome to tell the reality then again I’ll certainly
come again again.
Just wish to say your article is as astounding.
The clarity in your post is just great and i can assume you’re an expert on this subject.
Well with your permission let me to grab your RSS feed to
keep up to date with forthcoming post. Thanks a million and
please keep up the gratifying work.
Sweet blog! I found it while surfing around on Yahoo News.
Do you have any suggestions on how to get listed
in Yahoo News? I’ve been trying for a while
but I never seem to get there! Cheers
Hello, the whole thing is going well here and ofcourse every one is sharing facts, that’s really fine, keep up writing.
Have you ever thought about adding a little
bit more than just your articles? I mean, what you say is fundamental and all.
Nevertheless think about if you added some
great photos or video clips to give your posts more, “pop”!
Your content is excellent but with images and videos, this site could certainly be one of
the very best in its field. Superb blog!
Thanks very interesting blog!
I am really impressed with your writing skills as well as with the
layout on your blog. Is this a paid theme or did you customize
it yourself? Either way keep up the excellent quality
writing, it’s rare to see a nice blog like this one these days.
Heya fantastic blog! Does running a blog such as this require a
massive amount work? I’ve very little expertise in programming however I was hoping to start my own blog soon. Anyhow, should you have any ideas or techniques for new
blog owners please share. I know this is off subject nevertheless I
simply had to ask. Thanks a lot!
Fantastic beat ! I would like to apprentice at the same time as you amend your site, how can i subscribe
for a weblog web site? The account aided me a acceptable deal.
I had been a little bit acquainted of this your broadcast offered shiny
transparent idea
Wonderful site. A lot of useful information here. I am sending it to several pals ans also sharing in delicious.
And of course, thanks in your effort!
I blog frequently and I seriously thank you for your content.
This article has truly peaked my interest.
I am going to bookmark your site and keep checking for new information about once a week.
I opted in for your Feed as well.
all the time i used to read smaller articles or reviews that
as well clear their motive, and that is also happening with this paragraph which I am reading at this time.
What’s up to every one, the contents existing at this web
site are genuinely remarkable for people experience,
well, keep up the nice work fellows.
My family always say that I am wasting my time here at web, however I know I am getting familiarity every day
by reading such good content.
I every time emailed this weblog post page to all my contacts, since if like to read it after
that my contacts will too.
I’m not sure exactly why but this web site is loading very slow for
me. Is anyone else having this problem or is it a problem on my end?
I’ll check back later on and see if the problem still exists.
I’m not that much of a online reader to be honest but your sites really nice, keep it up!
I’ll go ahead and bookmark your site to come back in the future.
Many thanks
Wonderful work! This is the kind of information that should be shared around the web.
Disgrace on the seek engines for not positioning this
put up upper! Come on over and discuss with my website . Thanks
=) quest bars http://bitly.com/3C2tkMR quest bars
Do you mind if I quote a few of your articles as long as I provide credit and sources back to your blog?
My website is in the exact same niche as yours and my users would truly benefit from some of the information you
provide here. Please let me know if this ok with
you. Thanks a lot! asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr
Thanks for sharing your thoughts. I truly appreciate your efforts and
I will be waiting for your next post thank you
once again. cheap flights http://1704milesapart.tumblr.com/ cheap flights
Pretty! This was a really wonderful article.
Thanks for providing these details. scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery
Hello! I could have sworn I’ve been to this website before but after browsing through
many of the posts I realized it’s new to me. Regardless, I’m certainly delighted I discovered it and I’ll be book-marking it and
checking back regularly! scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
Hurrah, that’s what I was seeking for, what a stuff!
existing here at this webpage, thanks admin of this site.
quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
I every time used to study paragraph in news papers but now as I am a user of net thus from now I
am using net for posts, thanks to web. ps4 games https://bit.ly/3nkdKIi ps4 games
Tolorosa href [url=https://nameblogtopp.com/] -tlm urll [/url] -ca4c
When some one searches for his required thing, therefore he/she needs to be available that in detail,so that thing is maintained over here.
Really appreciate you sharing this blog article.Much thanks again. Fantastic.
I have been browsing online more than three hours lately, but I by no means discovered any interesting article like
yours. It is lovely worth sufficient for me. In my
opinion, if all website owners and bloggers made just right content material as you did, the internet can be much more helpful than ever before.
Pretty nice post. I just stumbled upon your blog and wanted
to say that I’ve truly enjoyed browsing your blog posts. After all I will
be subscribing to your feed and I hope you write again very
soon!
Hi there friends, how is the whole thing, and
what you want to say about this post, in my view its
in fact amazing for me.
My page :: tracfone special coupon 2022