FreeRTOS – Changing Tasks Priorities
Keywords: Embedded systems, ARM, FreeRTOS, STM32F4, Tasks, Task Priorities
Code Link: Tutorial Source Code Github – Keil
In the previous tutorials we showed how to create FreeRTOS Tasks and pass parameters to it. In this tutorial we will demonstrate two things.
- Effect of Task Priority on Task Execution
- Changing Task priority on fly
Note: The tutorial assumes Priority based Preemptive Scheduling (The default scheduling algorithm of FreeRTOS) according to which Tasks are executed based on Task priorities. Higher the priority, the first it will be executed.
As we mentioned earlier that scheduler always run the highest priority Ready task (in Priority based Preemptive Scheduling algorithm). In this tutorial we will create two FreeRTOS Tasks (Task-1, Task-2) with equal priorities. After 10 Task Switching iterations, we will rise Task Task-1 priority such that:
Priority of Task-1 > Priority of Task-2
Now that the Task-1 has the higher priority so as per scheduler policy it will keep running forever. This is because each time the scheduler tick interrupt occurs (to switch task), it finds Task-1 as the highest priority Ready task in queue.
Task-1 will keep running until another higher priority task gets ready or the priority of Task-1 is made lower/equal to Task-2. We will let Task-1 keep running for 10 task switching iteration, after that we will lower the Task-1 priority to be equal to Task-2 and vise versa. The process is summarized in Figure-1.

As shown in the Figure-1, for the first 10 iterations both Task-1 and Task-2 runs alternatively as both have equal priority. At 10th iteration, the priority of Task-1 is increased. Now for next 10 iterations we kept Task-1 priority higher than Task-2 so only Task-1 will be allowed to run as per scheduling policy. After 10 iterations (at 20 iteration in total), we again lowered the Task-1 priority to be equal to Task-2. Now as both tasks again have equal priority so both will run alternatively and vise versa.
Task Priority APIs:
Two APIs are used to inquire freeRTOS Tasks Priorities.
UBaseType_t uxTaskPriorityGet( TaskHandle_t xTask );
void vTaskPrioritySet( TaskHandle_t xTask, UBaseType_t uxNewPriority );
uxTaskPriorityGet(…):
This API returns the task priority of handle xTask given as argument. Passing a NULL
handle results in the priority of the calling task being returned.
Note: In FreeRTOSConfig.h
header file, INCLUDE_vTaskPriorityGet
must be defined as 1 for this function to be available.
vTaskPrioritySet(…):
This API sets the task priority of handle xTask
given as argument to the new value given in parameter uxNewPriority
. Passing a NULL
handle results in setting the priority of the calling task.
Note: In FreeRTOSConfig.h
header file, INCLUDE_vTaskPrioritySet
must be defined as 1 for this function to be available.
Let’s jump to programming.
Steps
1. First of all declare TaskHandle_t
handler for Task-1 which will be used in Task Priority APIs.
TaskHandle_t hTask1;
2. Next create two FreeRTOS Tasks of equal priorities. As we will change Task-1 priority on fly so we need to refer to Task-1 later in code. For this purpose we need a task handler. we have already declared a global TaskHandle_t
handler in previous step. Here we will only pass its pointer to xTaskCreat(...)
API.
const int task_1_Param = 1; const int task_2_Param = 2; xTaskCreate (vTask, "T1", 150, (void *)&task_1_Param, 1, &hTask1); xTaskCreate (vTask, "T2", 150, (void *)&task_2_Param, 1, NULL);
3. Define a counter variable to be used to count number of times tasks being switched.
int counter = 0;
4. Upon 10th iteration, raise the priority of Task-1 by 1. At this point forward only Task-1 will run as it has the higher priority than Task-2.
if (counter == 10) { printf("Raising Task-1 Priority....\n"); vTaskPrioritySet(hTask1, uxTaskPriorityGet(hTask1)+1); }
5. Upon 20th iteration, lower the priority of Task-1 by 1. At this point forward both Tasks will run alternatively as both Tasks have equal priorities.
if (counter == 20) { printf("Lowering Task-1 Priority....\n"); vTaskPrioritySet(hTask1, uxTaskPriorityGet(hTask1)-1); counter = 0; }
The complete Task function code is given bellow.
void vTask(void * pvParams) { volatile unsigned int i = 0; const int * tParam = (const int *)pvParams; for (;;) { printf("Task-%d Running.\n", *tParam); counter++; if (counter == 10) { printf("Raising Task-1 Priority....\n"); vTaskPrioritySet(hTask1, uxTaskPriorityGet(hTask1)+1); }else if (counter == 20) { printf("Lowering Task-1 Priority....\n"); vTaskPrioritySet(hTask1, uxTaskPriorityGet(hTask1)-1); counter = 0; } /*Dummy Delay - Lazy work */ for (i =0; i < 500000; ++i); } }
5. Start the Scheduler.
vTaskStartScheduler();
6. Finally Compile source code, connect STM32F4-Discovery board to PC and enter debug mode. Open printf serial window via View -> Serial Windows -> Debug (printf) Viewer. Run the Program, You will see Task execution messages as shown in bellow Video.
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.
Tremendous issues here. I’m very satisfied to look your article.
Thanks so much and I’m looking ahead to touch you.
Will you kindly drop me a mail? asmr 0mniartist
Heya i’m for the first time here. I came across this board and I
find It really useful & it helped me out a lot.
I hope to give something back and aid others like you helped me.
0mniartist asmr
Very energetic post, I loved that a lot. Will there be a part 2?
asmr 0mniartist
I just couldn’t go away your website prior to suggesting that I extremely loved the
standard information an individual supply on your guests?
Is gonna be again ceaselessly to check out
new posts asmr 0mniartist
Hi there i am kavin, its my first occasion to commenting anywhere,
when i read this piece of writing i thought i could also make comment due to this brilliant paragraph.
asmr 0mniartist
My brother suggested I might like this blog. He was entirely right.
This post actually made my day. You cann’t imagine just how much time I had spent for this
info! Thanks! asmr 0mniartist
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 waste your intelligence on just posting videos to your
site when you could be giving us something enlightening to read?
asmr 0mniartist
Hi there to every one, the contents existing
at this site are genuinely awesome for people experience, well, keep up the good work fellows.
asmr 0mniartist
Hi, its pleasant paragraph regarding media print, we all understand media
is a impressive source of information. asmr 0mniartist
Thanks for any other magnificent article. The place else
could anyone get that kind of information in such an ideal means of writing?
I have a presentation next week, and I’m at the search for such info.
I visited various web sites except the audio quality for audio songs
present at this website is really superb.
I could not resist commenting. Perfectly written!
Very great post. I simply stumbled upon your blog and wanted to mention that I have truly enjoyed
browsing your blog posts. In any case I’ll be subscribing in your rss feed and
I hope you write once more soon!
Do you have a spam issue on this blog; I also am a blogger, and I
was wondering your situation; many of us have developed some nice practices and we
are looking to swap solutions with other folks, please shoot me an e-mail if interested.
What’s up every one, here every person is sharing such
experience, so it’s pleasant to read this blog,
and I used to go to see this webpage daily.
Superb, what a website it is! This weblog presents valuable data to us, keep it up.
I was wondering if you ever considered changing the layout of your website?
Its very well written; I love what youve got
to say. But maybe you could a little more in the way
of content so people could connect with it better.
Youve got an awful lot of text for only having one or 2 pictures.
Maybe you could space it out better?
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.
scoliosis
constantly i used to read smaller articles that also clear their motive,
and that is also happening with this article which I am reading at this place.
scoliosis
scoliosis
Wow, this article is pleasant, my sister is analyzing these kinds of
things, thus I am going to let know her. scoliosis
scoliosis
Because the admin of this site is working, no hesitation very rapidly it will
be well-known, due to its feature contents. scoliosis
free dating sites
I blog quite often and I really appreciate your
content. This great article has really peaked my interest.
I’m going to book mark your blog and keep checking for new details about once per week.
I subscribed to your Feed too. dating sites https://785days.tumblr.com/
dating sites
I am in fact thankful to the owner of this site who has shared
this impressive paragraph at at this place. free dating sites
It’s not my first time to pay a visit this site, i am visiting this web page dailly and obtain pleasant data from here everyday.
Wow that was strange. I just wrote an incredibly 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 wonderful blog!
It is appropriate 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 some interesting things or suggestions.
Maybe you could write next articles referring to this article.
I want to read more things about it!
Thanks to my father who told me about this weblog, this blog is truly amazing.
An interesting discussion is definitely worth comment.
I do believe that you should publish more on this topic, it might not be
a taboo subject but usually folks don’t discuss these subjects.
To the next! Best wishes!!
Hi it’s me, I am also visiting this web page regularly, this website
is genuinely good and the people are actually sharing fastidious thoughts.
If you would like to increase your familiarity simply keep visiting this web site and be updated with the latest news posted here.
Hmm it appears like your website ate my first comment (it
was super long) so I guess I’ll just sum it up what I submitted and
say, I’m thoroughly enjoying your blog. I too am
an aspiring blog writer but I’m still new to the whole thing.
Do you have any recommendations for rookie blog writers?
I’d really appreciate it.
A big thank you for your blog article.Thanks Again. Awesome.
Hey there, You’ve done a great job. I’ll definitely digg it and personally recommend to my friends.
I’m confident they’ll be benefited from this website.
This post will assist the internet people for building up new web site
or even a blog from start to end.
Hello very cool blog!! Guy .. Beautiful .. Amazing .. I will bookmark your website and take
the feeds also? I am satisfied to seek out a lot
of useful information right here within the submit, we need develop extra strategies on this regard, thank you for sharing.
. . . . .
Nice response in return of this question with genuine arguments
and telling all about that.
Everything published made a ton of sense. However, what about this?
suppose you wrote a catchier title? I am not suggesting your
content isn’t good, but what if you added a title that grabbed people’s
attention? I mean FreeRTOS – Changing Tasks Priorities – EcoderLenz is kinda boring.
You might peek at Yahoo’s home page and watch how they create article titles to get people to click.
You might add a video or a related picture or two to get people interested about everything’ve written. Just my opinion, it could bring
your posts a little livelier.
Pretty section of content. I just stumbled upon your website and in accession capital to assert that I
get actually enjoyed account your blog posts.
Any way I will be subscribing to your augment and even I achievement you access
consistently quickly.
fantastic issues altogether, you just gained a new reader.
What would you recommend in regards to your put up that you simply made some days ago?
Any positive?
Excellent website you have here but I was curious about if you knew of any discussion boards that cover the same topics discussed in this article?
I’d really like to be a part of group where I can get
feed-back from other experienced individuals that share the same interest.
If you have any suggestions, please let me know. Cheers!
I do not even know how I ended up here, but I thought this post was
good. I do not know who you are but definitely you’re going to a famous blogger if you are not already
😉 Cheers!
If you are going for most excellent contents like myself, simply pay a quick
visit this web page daily as it offers quality contents, thanks
Hey! This is my first visit to your blog! We are a team of volunteers and starting a new project
in a community in the same niche. Your blog provided
us valuable information to work on. You have done a extraordinary job!
WOW just what I was looking for. Came here by searching for what
We’re a gaggle of volunteers and starting a new scheme
in our community. Your site provided us with useful information to work
on. You have performed a formidable activity and
our whole neighborhood will likely be grateful to you.
Thanks , I have recently been searching for info approximately this topic for a long time and yours is the best I have came upon so far.
But, what concerning the conclusion? Are you sure about the source?
My brother suggested I might like this website.
He was totally right. This post actually made my day.
You can not imagine just how much time I had spent for this info!
Thanks!
Wonderful blog! Do you have any helpful hints for aspiring writers?
I’m hoping to start my own website soon but I’m a
little lost on everything. Would you advise starting with a free platform like WordPress or go for a paid
option? There are so many choices out there that
I’m totally overwhelmed .. Any recommendations? Cheers!
Hmm is anyone else experiencing problems with the pictures on this blog loading?
I’m trying to figure out if its a problem on my end or if it’s
the blog. Any feed-back would be greatly appreciated.
Someone necessarily lend a hand to make significantly articles I would state.
This is the very first time I frequented your website page and to this
point? I amazed with the analysis you made to make this
actual put up extraordinary. Wonderful process!
Thank you for every other informative site.
The place else may just I am getting that kind of information written in such an ideal approach?
I have a challenge that I am just now working on, and I’ve
been on the look out for such information.
Helpful info. Lucky me I discovered your website by chance, and
I am shocked why this accident didn’t happened in advance!
I bookmarked it.
You need to take part in a contest for one of the greatest blogs
on the internet. I am going to highly recommend
this website!
Quality articles is the key to interest the users to pay a
quick visit the web site, that’s what this site is providing.
If you are going for best contents like me, only go to see this site every day since it provides quality contents, thanks
Hi there to every one, it’s in fact a nice for me to go to see this site, it includes
valuable Information.
Yes! Finally someone writes about a.
Wonderful blog! I found it while searching on Yahoo News. Do you have
any tips on how to get listed in Yahoo News? I’ve been trying for a while but I
never seem to get there! Many thanks asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr
Attractive component to content. I simply stumbled upon your web
site and in accession capital to assert that I acquire in fact enjoyed account your
blog posts. Anyway I will be subscribing on your augment or even I achievement you get admission to constantly rapidly.
quest bars http://bit.ly/3jZgEA2 quest bars
Hello, yes this paragraph is truly good and I have
learned lot of things from it concerning blogging.
thanks. scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery
Amazing issues here. I am very glad to look your article.
Thanks a lot and I am looking forward to touch you.
Will you please drop me a mail? ps4 https://bitly.com/3nkdKIi ps4 games
I’m really impressed with your writing skills and also with the layout on your weblog.
Is this a paid theme or did you customize it
yourself? Either way keep up the excellent quality writing,
it is rare to see a great blog like this one nowadays. cheap
flights http://1704milesapart.tumblr.com/ cheap flights
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 design my own blog and would like to know where u got this from.
thanks a lot scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
Hey there! Someone in my Myspace group shared this site with us
so I came to give it a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my followers!
Fantastic blog and outstanding design. quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
This is a topic which is near to my heart…
Many thanks! Where are your contact details though? https://parttimejobshiredin30minutes.wildapricot.org/ part
time jobs hired in 30 minutes
Right now it looks like Expression Engine is the best blogging platform
out there right now. (from what I’ve read) Is that what
you are using on your blog?
slots games online slot games slots online
Very neat blog article.Thanks Again.
Great article, just what I wanted to find.