An Inside view of FreeRTOS Scheduler – ARM Cortex-M Platform

Keywords: Embedded systems, ARM, FreeRTOS, STM32F4, Scheduler

Scheduler is the part of Operating System kernel used to schedule various tasks to run simultaneously (feels simultaneously) on a Processor Core thus achieving multitasking phenomena.

A single processor core can process only one task at a time. Most of real world embedded applications does more than a one job and usually runs on single core processor. For example a mobile phone performs multiple tasks at a time including battery Power monitoring, listening to incoming call and messages, communicating over Wifi/Data connection etc. In such scenarios it is required to efficiently manage tasks so that every task gets executed appropriately and not deprived of execution.

Scheduler Base:

A Scheduler itself is quite complex piece of code yet it can be traced back to two basic components. Generally, a Scheduler works based on the following two components – Figure-1.

  • A Timer – Ticking at constant rate.
  • A Task Switcher.
Figure-1: OS Scheduler Base

How Scheduler Works?

As mentioned earlier, a Scheduler is one of the most important part of OS Kernel. It’s the heart beat of any OS. Its one of the core module of OS Kernel. The inner working of scheduler depends on a Timer called Tick Timer ticking on OS Timer Line at predefined constant rate. The Scheduler tick is basically an interrupt that occurs continuously after predefine time interval (Tick Rate). The tick timer basically provides time slicing. In each time slice one process/task is allowed to run-Figure-2.

Figure-2: OS Task switching on Timer Tick

The tick from Timer (the interrupt) signals the task switcher which subsequently based on predefined algorithm (discussed later), selects one of the Ready Tasks in queue for execution -Figure-3.

Figure-3: OS Scheduler Gateway

NOTE: In Figure-2,3 tasks priority is not taken into consideration (priority based preemption). In real-time OS tasks are normally executed on priority based which means highest priority task is extended first.

When tick timer interrupt occurs, the task switcher is informed to switch to next task. The task switcher does the following things.

  • Stores the current task context (state, registers etc.) on stack.
  • Load the next chosen task context.
  • Let the processor execute process.

Scheduling Policies:

There are many algorithms on based of which next task to be executed is chosen. The most common ones are given bellow.

1. Pre-emptive Scheduling: In this algorithm each process/task is given equal time slot independent of importance/priority. Tasks are executed in round robin fashion i.e. one after other.

2. Priority based pre-emptive Scheduling: This is similar to pre-emptive scheduling except tasks with highest priority, amongst many ready tasks, are executed first. If all the ready tasks have same priority level, then this algorithm behaves exactly like pre-emptive scheduling i.e. tasks are executed in round robin fashion.

3. Cooperative Scheduling: In this algorithm, tasks themselves are written in such a way that they “cooperate” with each other, yielding processor to other tasks when necessary.

FreeRTOS Scheduler Implementation:

FreeRTOS allows to select either cooperative scheduling or priority based pre-emptive scheduling (and subsequently simple pre-emptive scheduling which is when required, can be implemented by simply assigning same priorities to each task). The selection can be made via the following macro in FreeRTOSConfig.h.

    Set to 1 to use the preemptive RTOS scheduler, or 0 to use the cooperative RTOS scheduler.

#define configUSE_PREEMPTION     1

NOTE: For the purpose of demonstration, we will assume ARM based SOC/platform especially Cortex-M profile and priority based pre-emptive scheduling plus dynamic memory allocation .

For more details on static and dynamic memory allocation, refer to the following tutorial.

NOTE: Only major steps within FreeRTOS scheduler will be discussed (assuming ARM Cortex-M Platform). For more in-dept details, refer to freeRTOS source code/documentations.

ARM SoCs are quite OS friendly. ARM Cortex-M profile provides on-core 24-bits down counter timer to support OS scheduler. On ARM Cortex-M platform, FreeRTOS utilizes two system features/resources i.e. Systick timer as Tick Timer and PendSV exception as Task Switcher to implement two layers of scheduler shown in Figure-3.

Let’s get back to inner implementation of FreeRTOS Scheduler. The following flowchart shows a broad overview of FreeRTOS Scheduler running on ARM Cortex-M Cores.

Figure-4: FreeRTOS Scheduler for ARM Cortex-M Cores/Ports


1. vTaskStartScheduler(): This is the main entrance to FreeRTOS Kernel Scheduler. No task can run / is scheduled before calling this function. After calling vTaskStartScheduler() FreeRTOS kernel has complete control over tasks execution. vTaskStartScheduler() never returns unless there is insufficient RAM.

2. Idle Task: This is the only task that FreeRTOS creates explicitly inside vTaskStartScheduler() function. Idle Task performs various duties like Garbage collection/reclaiming RAM memory of deleted tasks etc. This task is created by freeRTOS Kernel with fixed priority value ‘0’ and runs only when there is no other task running.

3. Tick Timer Setup: As mentioned in Figure-3, the scheduler requires a tick timer for time reference. Based on tick timer various tasks are scheduled. Once the idle task is successfully created, the vTaskStartScheduler() function calls the void vPortSetupTimerInterrupt (void) to configure an on-chip timer for desired tick frequency as defined by configTICK_RATE_HZ MACRO in FreeRTOSConfig.h header file.
For ARM Cortex-M ports, Systick timer (mentioned above) is used as tick timer. User can change this timer to any other desired timer by providing appropriate implementation of void vPortSetupTimerInterrupt (void) function. We also have a dedicated tutorial on how to change FreeRTOS default tick timer. The tutorial can be accessed here.

4. Triggering SVC Exception: Once Tick timer is configured and started, the ARM Supervisory Call Exception i.e. SVC is triggered with Supervisory Number 0 (SVC 0).

5. SVC Exception Handler: The purpose of the SVC exception is choose (based on scheduler algorithm) and allow first task to execute. Once the First Task starts execution, the SVC Exception is never called again.
Note: Only newer versions of FreeRTOS uses SVC to schedule First Task. In older versions PendSv Exception was used for the same purpose.

6. First Task Execution: Once a task is chosen to run first in SVC Exception Handler, the processor starts executing the first task. The first task execute indefinitely unless preempted by scheduler.

7. Tick Timer Interrupt: The processor keeps executing the task being chosen for execution (may or may not be the first task) unless tick timer interrupt (configured before) gets hold of the processor. The tick timer generates interrupt at constant rate as defined by configTICK_RATE_HZ MACRO in FreeRTOSConfig.h header file.

8. Context Switch: Inside the Tick Timer interrupt handler it is checked whether a context switch is required or not. Context switch means whether its time to switch to next ready task in queue. As shown in Figure-3, each task in the system is assigned a slice of time in which that task executes. In each Tick Timer Interrupt its checked whether the time of current executing task is overred or not. If the time slice is finished the PendSV interrupt is pended (triggered) otherwise internal tick counter is incremented and control is returned back to the task being executed before the Tick Timer Interrupt occurs.

9. PendSV Exception: FreeRTOS uses PendSV exception for context switching. There is a reason why PendSV is chosen for context switching. We will explain it very briefly. The aim is to assign high priority to Tick Timer Interrupt so that we have more accurate time measurement and may not be delayed by other interrupt. The PendSV is kept at lowest possible priority in system which means PendSV is triggered when all other interrupts are served. System Interrupts are given priorities than Context Switching (partially configurable in FreeRTOS as explained here). This approach implies that a system interrupt can delay context switch (PendSV is served last) but never disturb reference clock time measurement (Tick Timer).

10. PendSV Exception Handler: PendSV Exception Handler is used to actually chose next ready task in queue to be assigned cpu cycles. Remember from previous steps, the PendSV is pended in Tick Timer Interrupt Handler and is triggered once all the pending interrupts and exceptions are served. Inside PendSV Exception Handler the current task context (registers values etc.) is saved on stack and next potential task is selected based on the chosen Scheduler Policy mentioned earlier – see Scheduler Policies. Before switching to the selected task, its context (registers values etc.) is restored from stack.

11. Task Exception: The Task selected in previous step is allowed to run and consume processor cycles. The processor keeps executing the Task until another Tick Timer Interrupt preempt the current task and select next task for execution.

From Now onward the process/loop (steps 7-11) repeats continuously until there is a potential problem that causes the scheduler loop to unlock and return – Figure-5.

Figure-5: FreeRTOS Scheduling Process

For more detail information on FreeRTOS kernel, refer to the FreeRTOS Official website [1].


[1] – FreeRTOS Official Website

97 thoughts on “An Inside view of FreeRTOS Scheduler – ARM Cortex-M Platform”

  • Howdy outstanding website! Does running a blog such as
    this take a great deal of work? I’ve virtually no expertise in programming however I had been hoping to start my own blog
    soon. Anyways, if you have any suggestions or techniques
    for new blog owners please share. I understand this is off subject but I
    just needed to ask. Thanks! asmr 0mniartist

  • Oh my goodness! Incredible article dude! Thank you, However I am
    going through problems with your RSS. I don’t understand the reason why I cannot subscribe to it.

    Is there anyone else having identical RSS problems?
    Anyone that knows the answer will you kindly respond?
    Thanks!! 0mniartist asmr

  • Oh my goodness! Awesome article dude! Thank you so much,
    However I am having problems with your RSS. I don’t know the reason why
    I can’t subscribe to it. Is there anyone else having identical RSS
    problems? Anybody who knows the solution will you kindly respond?
    Thanks!! asmr 0mniartist

  • My partner and I absolutely love your blog and find nearly all of
    your post’s to be exactly I’m looking for. Do you offer guest writers to write content for
    you personally? I wouldn’t mind producing a post or elaborating on a number of the subjects you write
    regarding here. Again, awesome site! 0mniartist asmr

  • Hi! I’ve been following your site for a while now and finally got the courage to go ahead and give you
    a shout out from Houston Tx! Just wanted to say keep up the good job!
    asmr 0mniartist

  • Hi, Neat post. There is an issue together with your site in web explorer, may
    test this? IE still is the marketplace leader and a huge component to other people will miss your fantastic writing due to this problem.
    0mniartist asmr

  • It’s a pity you don’t have a donate button! I’d most certainly donate to this superb blog!
    I suppose for now i’ll settle for book-marking and adding your RSS feed
    to my Google account. I look forward to brand new updates and will share
    this site with my Facebook group. Chat soon! 0mniartist asmr

  • This is really interesting, You are a very skilled blogger.
    I’ve joined your feed and look forward to seeking more of your excellent post.
    Also, I’ve shared your web site in my social networks!

    asmr 0mniartist

  • Hey! This is kind of off topic but I need some guidance from an established blog.
    Is it very hard to set up your own blog? I’m not very techincal but
    I can figure things out pretty quick. I’m thinking about making my own but I’m not sure
    where to begin. Do you have any points or suggestions?
    With thanks 0mniartist asmr

  • 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 would you mind letting me know which hosting company you’re utilizing?

    I’ve loaded your blog in 3 different browsers and I must
    say this blog loads a lot quicker then most. Can you recommend a good hosting
    provider at a reasonable price? Many thanks, I appreciate it!

  • Hello there, I found your website by means of Google while looking for a similar matter,
    your site came up, it appears good. I have bookmarked it
    in my google bookmarks.
    Hello there, just changed into alert to your blog thru Google, and located that it is really
    informative. I’m going to be careful for brussels. I’ll appreciate if you
    happen to continue this in future. A lot of other people might be benefited
    out of your writing. Cheers!

  • Thanks for your personal marvelous posting! I truly enjoyed reading it, you could be
    a great author.I will always bookmark your blog and may come back
    in the foreseeable future. I want to encourage one
    to continue your great work, have a nice morning!

  • When someone writes an post he/she keeps the idea of a user in his/her
    brain that how a user can know it. Thus that’s why this post is amazing.

  • scoliosis
    Howdy outstanding blog! Does running a blog similar to this
    take a large amount of work? I have absolutely no knowledge of computer programming
    however I was hoping to start my own blog in the near future.

    Anyhow, should you have any ideas or techniques for new blog
    owners please share. I know this is off subject nevertheless I
    just wanted to ask. Thanks a lot! scoliosis

  • You’re so cool! I don’t suppose I’ve read through anything like this before.

    So nice to discover someone with some unique thoughts on this subject.
    Seriously.. thanks for starting this up. This website
    is something that is needed on the internet, someone
    with a little originality!

  • Magnificent goods from you, man. I’ve understand your stuff previous to
    and you are just extremely magnificent. I actually like what you have
    acquired here, really like what you’re saying and the way in which
    you say it. You make it enjoyable and you still take care of to keep it
    sensible. I can’t wait to read far more from you. This is really
    a terrific site.

  • Hello there, I do think your web site may be having internet
    browser compatibility problems. When I look at your website in Safari, it
    looks fine however, when opening in Internet Explorer, it’s got some overlapping issues.
    I just wanted to provide you with a quick heads up! Aside from that, fantastic website!

  • Hi there this is kind of 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 guidance from someone
    with experience. Any help would be enormously appreciated!

  • I do trust all of the concepts you’ve offered to
    your post. They are very convincing and can definitely work.

    Still, the posts are too short for newbies.
    May just you please extend them a little from subsequent
    time? Thank you for the post.

  • Howdy would you mind sharing which blog platform you’re using?

    I’m planning to start my own blog in the near future
    but I’m having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and I’m looking for something
    completely unique. P.S My apologies
    for getting off-topic but I had to ask!

  • Eu realmente amo seu site … Ótimas cores e tema. Você fez este
    incrível site você mesmo? . Estou olhando criar meu próprio pessoal
    site e gostaria de aprender onde você baixo este tema ou exatamente qual tema.

    Abraços !

  • Aw, this was an extremely nice post. Finding the
    time and actual effort to create a great article… but what
    can I say… I put things off a whole lot and don’t manage to get anything

  • Hey! Someone in my Facebook group shared this website with us so I came to give
    it a look. I’m definitely enjoying the information. I’m book-marking and
    will be tweeting this to my followers! Fantastic blog and great design.

  • Have you ever considered publishing an e-book or guest authoring
    on other sites? I have a blog centered on the same ideas
    you discuss and would love to have you share some stories/information. I know my visitors would enjoy your work.
    If you’re even remotely interested, feel free to shoot me an email.

  • Attractive section of content. I just stumbled upon your blog and
    in accession capital to assert that I get in fact enjoyed account your blog posts.
    Any way I’ll be subscribing to your feeds and even I achievement you
    access consistently quickly.

  • you are actually a good webmaster. The website loading velocity is amazing.
    It kind of feels that you’re doing any distinctive trick.
    In addition, The contents are masterwork. you’ve
    performed a fantastic activity in this subject!

  • I absolutely love your website.. Pleasant colors & theme.
    Did you create this web site yourself? Please reply back as I’m attempting to create my very
    own website and would love to learn where you got this from or what the theme is named.
    Many thanks!

  • Hello! I know this is somewhat 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 difficulty finding one?
    Thanks a lot!

  • Just want to say your article is as amazing. The clearness for your put up is simply spectacular and that i could suppose you’re an expert in this subject.
    Fine with your permission let me to snatch your feed
    to stay up to date with imminent post. Thank you one
    million and please carry on the rewarding work.

  • you’re in point of fact a good webmaster. The site loading velocity is incredible.
    It seems that you are doing any unique trick. Furthermore, The contents are masterwork.

    you’ve performed a wonderful activity in this topic!

  • Undeniably believe that which you said. Your favorite reason appeared to be on the net the simplest thing to be aware of.
    I say to you, I certainly get annoyed while people think about worries that they just don’t know about.
    You managed to hit the nail upon the top as well as defined out the whole thing without having side effect , people can take a signal.
    Will probably be back to get more. Thanks

  • Admiring the time and energy you put into your website and in depth information you offer.
    It’s awesome to come across a blog every once in a while that
    isn’t the same out of date rehashed information. Fantastic
    read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

  • I was recommended this website by my cousin. I’m not sure whether this
    post is written by him as nobody else know such detailed about my trouble.
    You’re wonderful! Thanks!

  • Good day! I know this is kinda off topic nevertheless I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa?
    My website discusses a lot of the same topics as yours and I feel we could greatly benefit from each
    other. If you are interested feel free to send me an email.
    I look forward to hearing from you! Great blog by the way!

  • Write more, thats all I have to say. Literally, it seems as though you
    relied on the video to make your point. You definitely know what youre talking about, why throw away your intelligence on just posting videos to your site when you could be giving us something informative to

  • I am really loving the theme/design of your site. Do you ever run into any browser compatibility issues?
    A handful of my blog visitors have complained about my website not working correctly in Explorer but
    looks great in Chrome. Do you have any tips to help fix this problem?

  • Greetings from Idaho! I’m bored to tears at work so I decided to
    check out your website on my iphone during lunch break. I
    love the knowledge you present here and can’t wait to take a look when I get home.
    I’m amazed at how fast your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyhow, great

  • Hello! I could have sworn I’ve been to this website before but after reading through some of the post I realized
    it’s new to me. Nonetheless, I’m definitely glad I found it and
    I’ll be bookmarking and checking back frequently!

  • Howdy just wanted to give you a brief heads up and let you
    know a few of the images aren’t loading properly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different web browsers and both show the same outcome.

  • Hello, Neat post. There is an issue together with your web
    site in web explorer, could check this? IE nonetheless is the marketplace
    chief and a good section of folks will miss your great
    writing due to this problem.

  • Hey there this is kind of of off topic but I was wondering if blogs use WYSIWYG editors
    or if you have to manually code with HTML. I’m starting
    a blog soon but have no coding know-how so I wanted to get guidance from
    someone with experience. Any help would be greatly appreciated!

  • Hey I am so delighted I found your site, I really found you
    by accident, while I was searching on Digg for something else,
    Nonetheless I am here now and would just like to say cheers
    for a incredible post and a all round exciting blog (I also love the theme/design), I don’t have time to browse it all at
    the minute but I have saved it and also added
    your RSS feeds, so when I have time I will be back to read much more, Please do keep up
    the excellent job.

  • I will immediately seize your rss as I can not to find your email subscription hyperlink or newsletter service.
    Do you have any? Please let me understand so that I could subscribe.

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

  • Today, I went to the beachfront 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 totally off topic but I had to tell someone!

  • You can definitely see your expertise within the work you write. The sector hopes for more passionate writers like you who are not afraid to mention how they believe. Always go after your heart.

  • You are so cool! I do not think I’ve truly read a single thing
    like this before. So good to discover another person with genuine thoughts on this issue.

    Seriously.. thank you for starting this up. This web site is one
    thing that is needed on the web, someone with a little originality!

  • Wow that was strange. I just wrote an incredibly long comment but after I
    clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over
    again. Anyhow, just wanted to say fantastic

  • Hey there I am so happy I found your blog page, I really found you by error,
    while I was researching on Yahoo for something else, Anyways I am here now
    and would just like to say kudos for a remarkable post and a all round enjoyable blog (I also love the theme/design),
    I don’t have time to read through it all at the moment
    but I have book-marked it and also included your RSS feeds,
    so when I have time I will be back to read more, Please do
    keep up the awesome job. quest bars quest bars

  • Do you have a spam problem on this blog; I also am a blogger, and I was wanting to know your situation; we have created some nice methods and we are looking
    to exchange solutions with others, please shoot me an email if interested.
    scoliosis surgery scoliosis surgery

  • certainly like your web site however you need to test the spelling
    on several of your posts. Several of them are rife with spelling issues and I find it very bothersome to tell the reality then again I will surely come again again.

Leave a Reply

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