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!

Figure-1: Dummy Task that is required to run after specific Time

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 NameDescription
Return Valuevoid

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.


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 
    printf("Task-%d Complete - Yielding Task.\n\n\n", *tParam);

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

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.


[1] – FreeRTOS Official Website

60 thoughts on “FreeRTOS – Task Yielding”

  • 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

  • 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!

  • 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

  • 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!

  • 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.

  • 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

  • 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

  • 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!

  • 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

  • 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.

  • 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

  • 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

Leave a Reply

Your email address will not be published. Required fields are marked *