Introduction to FreeRTOS Queues
Keywords: Embedded systems, ARM, FreeRTOS, STM32F4, Tasks, Queues
“A Queue is a Data Structure that holds a finite number of fixed-size data items and follows FIFO (First in First Out) access mechanism”.
Most of the low power low end embedded systems follows legacy hardware architecture of shared memory where any part of volatile memory can be read/modified/written by any part of embedded software running on the hardware. This is not a big issue until the software gets complex and runs multiple units of independent software tasks accessing shared memory simultaneously e.g. System is performing multi-tasking under an RTOS.
In such systems one of the major problem is memory protection and inter-tasks communication. Though some part of shared memory can be reserved (via some global variables/arrays) that can be written by one task and read by another task when required; The problem in such systems arises when it comes the memory operation Atomicity.
It is quite probable that one task is in the process of writing data to the shared memory and has partially completed the write operation (like writing to a large array) when a context switch occures and another task reads it assuming its the correct data. A more worse situation occurs when multiple tasks are trying to write data to the same shared memory simultaneously.
As an example, the following video shows two freeRTOS tasks trying to write string to same serial Port. As can be seen while the one task has not yet finished writing to serial port, the context switch preempt the first task and runs the second task resulting into mixing of two strings. The exact same things may happen in case of shared memory.
* Task1 Prints: “Message From Task-1: FreeRTOS Task-1 Running...
“
* Task2 Prints: “Message From Task-2: FreeRTOS Task-2 Running...
“
In order to tackle such problem, queues are used that ensures the data can’t be written to queue if its full (prevent replacing data) and can’t be read if its empty (prevents reading garbage values).
Queues are used to provide communication channels between various independent pieces of code like task-to-task, task-to-ISR and ISRs-to-task communication.
Generally in Queue, data is written to the back; called tail of the queue and removed from the front; called head of the queue – Figure-1 [1].

1. FreeRTOS Queues:
FreeRTOS implements an enhanced version of generic Queue (FIFO). A queue can only be written to back (tail) and be read in revered order (from front to tail) i.e. FIFO (First in First out). This means if a message is written last, it will be read last as well. Thus Queue messaging mechanism has no message priority associated with them. FreeRTOS provides a partially way around it by providing APIs that can write both to the front and back of Queue. Thus the message written to the front will be read first (sort of high priority). This makes freeRTOS Queues follow both FIFO and LIFO mechanism. In this way FreeRTOS queues get somehow closer to POSIX messaging mechanism. More details are covered in the APIs discussion.
2. FreeRTOS Queues APIs:
This following APIs are used for freeRTOS Queue main Operations.
2.1 xQueueCreate():
Before a queue can be used, it must be created explicitly with predefined length and item size it will hold. Queues are referenced by handles, which are variables of type QueueHandle_t
. The xQueueCreate()
API function creates a queue and returns a QueueHandle_t
as a references to the queue just created.
Parameter Name | Description |
---|---|
uxQueueLength | The maximum number of items that the queue being created can hold at any one time. |
uxItemSize | The size, in bytes, of each data item that can be stored in the queue. |
Return Value | If NULL is returned, then the queue cannot be created because there is insufficient heap memory available for FreeRTOS to allocate the queue data structures and storage area. If a non-NULL value is returned, the queue has been created successfully. The returned value should be stored as the handle to the created queue. |
2.2 xQueueSendToBack() and xQueueSendToFront():
BaseType_t xQueueSendToBack( QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait );
BaseType_t xQueueSendToFront( QueueHandle_t xQueue, const void * pvItemToQueue, TickType_t xTicksToWait );
xQueueSendToBack()
is used to send data to the back (tail) of a queue. xQueueSendToFront()
is used to send data to the front (head) of a queue.
Parameter Name | Description |
---|---|
xQueue | The handle of the queue to which the data is being sent (written). The queue handle will have been returned from the call to xQueueCreate() used to create the queue. |
pvItemToQueue | A pointer to the data to be copied into the queue. The size of each item that the queue can hold is set when the queue is created, so this many bytes will be copied from pvItemToQueue into the queue storage area. |
xTicksToWait | The maximum amount of time the task should remain in the Blocked state to wait for space to become available on the queue, should the queue already be full. Both xQueueSendToFront() and xQueueSendToBack() will return immediately if xTicksToWait is zero and the queue is already full. The block time is specified in tick periods, so the absolute time it represents is dependent on the tick frequency. The macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds into a time specified in ticks. Setting xTicksToWait to portMAX_DELAY will cause the task to wait indefinitely (without timing out), provided INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. |
Return Value | There are two possible return values: 1. pdPASS Returned only if data was successfully sent to the queue. If a block time was specified (xTicksToWait was not zero), then it is possible the calling task was placed into the Blocked state, to wait for space to become available in the queue, before the function returned, but data was successfully written to the queue before the block time expired. 2. errQUEUE_FULL Returned if data could not be written to the queue because the queue was already full. If a block time was specified (xTicksToWait was not zero) then the calling task will have been placed into the Blocked state to wait for another task or interrupt to make space in the queue, but the specified block time expired before that happened. |
Note: Do not call xQueueSendToFront()
or xQueueSendToBack()
from an interrupt service routine. Use the interrupt-safe versions, xQueueSendToFrontFromISR()
and xQueueSendToBackFromISR()
, instead.
2.3 xQueueReceive():
BaseType_t xQueueReceive( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait );
xQueueReceive() is used to receive (read) a message from a queue. The message that is received is removed from the queue.
Parameter Name/ Returned value | Description |
---|---|
xQueue | The handle of the queue to which the data is being sent (written). The queue handle will have been returned from the call to xQueueCreate() used to create the queue. |
pvBuffer | A pointer to the memory into which the received data will be copied. The size of each data item that the queue holds is set when the queue is created. The memory pointed to by pvBuffer must be at least large enough to hold that many bytes. |
xTicksToWait | The maximum amount of time the task should remain in the Blocked state to wait for data to become available on the queue, should the queue already be empty. If xTicksToWait is zero, then xQueueReceive() will return immediately if the queue is already empty. The block time is specified in tick periods, so the absolute time it represents depends on the tick frequency. The macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds into ticks. Setting xTicksToWait to portMAX_DELAY will cause the task to wait indefinitely (without timing out), provided INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. |
Return Value | There are two possible return values: 1. pdPASS Returned only if data was successfully read from the queue. If a block time was specified (xTicksToWait was not zero), then it is possible the calling task was placed into the Blocked state, to wait for data to become available on the queue, but data was successfully read from the queue before the block time expired. 2. errQUEUE_EMPTY Returned if data cannot be read from the queue because the queue is already empty. If a block time was specified (xTicksToWait was not zero,) then the calling task will have been placed into the Blocked state to wait for another task or interrupt to send data to the queue, but the block time expired before that happened. |
Note: Do not call xQueueReceive()
from an interrupt service routine. Use the interrupt-safe xQueueReceiveFromISR()
API function instead.
2.4 uxQueueMessagesWaiting():
UBaseType_t uxQueueMessagesWaiting( QueueHandle_t xQueue );
uxQueueMessagesWaiting()
is used to query the number of items that are currently in a queue.
Parameter Name | Description |
---|---|
xQueue | The handle of the queue being queried. The queue handle will have been returned from the call to xQueueCreate() used to create the queue. |
Return Value | The number of items that the queue being queried is currently holding. If zero is returned, then the queue is empty. |
Note: Do not call uxQueueMessagesWaiting() from an interrupt service routine. Use the interrupt-safe uxQueueMessagesWaitingFromISR() instead.
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 website to come back in the future.
Cheers 0mniartist asmr
Undeniably imagine that that you said. Your favourite reason seemed to be
on the web the simplest factor to take into account of.
I say to you, I definitely get annoyed even as folks think about issues that they plainly don’t recognize about.
You controlled to hit the nail upon the top as neatly as outlined out the whole thing without having side effect , other people can take a signal.
Will likely be back to get more. Thank you asmr
0mniartist
Hi there I am so thrilled I found your weblog, I really found
you by mistake, while I was browsing on Aol for something else, Nonetheless
I am here now and would just like to say many thanks for a fantastic post and a all round entertaining blog (I also
love the theme/design), I don’t have time to go through
it all at the moment but I have saved it and also included your RSS feeds, so when I have time I will be
back to read more, Please do keep up the superb work. 0mniartist asmr
I need to to thank you for this wonderful read!!
I certainly enjoyed every bit of it. I’ve got you saved as a favorite to check out new things you post… 0mniartist
asmr
I’m really loving the theme/design of your weblog. Do you
ever run into any web browser compatibility issues? A small number
of my blog visitors have complained about my blog not working
correctly in Explorer but looks great in Safari. Do you
have any advice to help fix this problem? asmr 0mniartist
Link exchange is nothing else except it is simply
placing the other person’s web site link on your page
at suitable place and other person will also do same in favor of you.
0mniartist asmr
There’s certainly a lot to learn about this issue. I love all
the points you made. asmr 0mniartist
It is actually a great and useful piece of information. I’m satisfied that you just
shared this helpful information with us. Please stay us up to date
like this. Thanks for sharing. asmr 0mniartist
Hi there to every one, it’s really a nice for me
to pay a quick visit this site, it consists of important Information.
What’s up, I wish for to subscribe for this weblog to take most recent updates, thus where can i
do it please help out.
Hey There. I discovered your blog using msn. This is an extremely smartly written article.
I’ll make sure to bookmark it and come back to learn more
of your useful information. Thank you for the post.
I will definitely comeback.
Hi, this weekend is pleasant for me, because this point in time i am
reading this wonderful informative paragraph here at my home.
An outstanding share! I’ve just forwarded this onto
a coworker who has been conducting a little
research on this. And he actually ordered me lunch
simply because I found it for him… lol. So allow me to reword this….
Thank YOU for the meal!! But yeah, thanks for spending time to discuss this subject here on your web site.
What’s up i am kavin, its my first occasion to commenting anyplace, when i read
this post i thought i could also make comment due to this sensible paragraph.
Hi there superb blog! Does running a blog similar to this take a massive amount work?
I’ve virtually no expertise in coding but I was hoping to start
my own blog in the near future. Anyway, if you have any ideas or tips for new blog owners please share.
I know this is off topic however I simply needed
to ask. Many thanks!
Do you have a spam problem on this website; I also am a blogger, and I was curious about your situation; we have created some nice procedures and
we are looking to swap strategies with others, be sure
to shoot me an e-mail if interested.
Have you ever thought about including a little bit more than just your articles?
I mean, what you say is valuable and all. But think about if you added some great visuals or video
clips to give your posts more, “pop”! Your content is
excellent but with pics and video clips, this site could certainly
be one of the greatest in its niche. Very good blog!
scoliosis
Heya superb blog! Does running a blog like this require a great deal
of work? I’ve absolutely no understanding of computer programming but I
was hoping to start my own blog soon. Anyway, should you have any ideas or tips for new blog owners please share.
I know this is off subject nevertheless I simply needed to ask.
Cheers! scoliosis
scoliosis
Truly when someone doesn’t be aware of after that its up to other visitors that they will help, so here it occurs.
scoliosis
scoliosis
If some one desires to be updated with most up-to-date technologies then he must be go
to see this site and be up to date every day. scoliosis
dating sites
Thank you for the auspicious writeup. It
in fact was a amusement account it. Look advanced to more added agreeable from you!
However, how can we communicate? dating sites https://785days.tumblr.com/
dating sites
My developer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the costs. But he’s tryiong
none the less. I’ve been using Movable-type on several websites for
about a year and am anxious about switching to another platform.
I have heard great things about blogengine.net. Is there a way I can import all my wordpress posts into it?
Any kind of help would be greatly appreciated! free dating
sites
Inspiring story there. What happened after? Good luck!
I’ll right away snatch your rss feed as I can’t in finding your e-mail subscription link or newsletter service.
Do you’ve any? Kindly allow me know in order that I may subscribe.
Thanks.
I needed to thank you for this very good read!!
I certainly enjoyed every bit of it. I have you book-marked to check out new stuff
you post…
Now I am ready to do my breakfast, after having my breakfast coming again to read further news.
Hey! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy reading
your posts. Can you suggest any other blogs/websites/forums that deal with the same
topics? Thanks a lot!
It is the best time to make some plans for the long run and it’s time to be happy.
I have read this post and if I could I want to counsel
you few interesting things or suggestions. Maybe you can write subsequent articles referring to this
article. I wish to read even more issues approximately it!
Howdy, i read your blog from time to time and i own a similar one and i was
just wondering if you get a lot of spam responses? If so how do you reduce it, any plugin or anything you can advise?
I get so much lately it’s driving me mad so any help is very
much appreciated.
As the admin of this site is working, no doubt very shortly it will
be famous, due to its quality contents.
At this time I am going to do my breakfast, afterward having my breakfast coming yet again to read other news.
I have to thank you for the efforts you’ve put in penning this site.
I am hoping to check out the same high-grade blog posts from you in the future as
well. In truth, your creative writing abilities has encouraged me to get my
very own website now 😉
This is the right blog for anyone who wishes to find out about this
topic. You know so much its almost tough to argue with you (not that I personally will need to…HaHa).
You certainly put a brand new spin on a topic that’s been discussed for a
long time. Great stuff, just wonderful!
This is the perfect blog for anybody who really wants to find out about this topic.
You realize so much its almost tough to argue with you
(not that I actually will need to…HaHa). You definitely put a
fresh spin on a subject which has been written about for years.
Great stuff, just great!
Hurrah! Finally I got a web site from where I can actually obtain valuable information concerning my study and knowledge.
Heya! I just wanted to ask if you ever have any issues with hackers?
My last blog (wordpress) was hacked and I ended up losing several weeks of hard work
due to no backup. Do you have any methods to stop hackers?
Hello there, I discovered your website by way of Google even as searching for a similar
matter, your web site got here up, it appears good. I have bookmarked it in my google bookmarks.
Hi there, just was alert to your blog thru Google, and
found that it’s really informative. I’m going to watch out for brussels.
I’ll be grateful in case you proceed this in future.
Many folks will be benefited out of your writing. Cheers!
Good day I am so thrilled I found your webpage, I really found you by mistake, while I was looking on Digg for something
else, Regardless I am here now and would just like to say many thanks for a
incredible post and a all round interesting blog (I also love
the theme/design), I don’t have time to browse it
all at the minute but I have bookmarked it and also added in your
RSS feeds, so when I have time I will be back to
read more, Please do keep up the excellent jo.
Hello, I log on to your blogs like every week. Your story-telling
style is witty, keep up the good work!
This text is worth everyone’s attention. Where can I find
out more?
Right here is the perfect website for anyone who wants to find out about this topic.
You know so much its almost hard to argue with you (not that I really will
need to…HaHa). You definitely put a new spin on a topic that’s been written about for many years.
Great stuff, just excellent!
This paragraph is genuinely a fastidious one it assists new web visitors, who are wishing for blogging.
Hello there! I know this is kinda off topic nevertheless I’d figured I’d
ask. Would you be interested in exchanging links or maybe guest authoring a blog post or
vice-versa? My blog addresses a lot of the same topics
as yours and I feel we could greatly benefit from each other.
If you might be interested feel free to send me an email.
I look forward to hearing from you! Wonderful blog by the way!
Why people still make use of to read news papers when in this technological globe all is accessible on net?
Somebody essentially help to make significantly articles I might state.
This is the first time I frequented your website page and thus far?
I amazed with the research you made to make this particular post incredible.
Fantastic job!
I’ll right away seize your rss feed as I can not to find your email subscription hyperlink or e-newsletter service.
Do you’ve any? Please permit me recognise so that
I could subscribe. Thanks.
Hi, every time i used to check website posts here early in the
break of day, since i like to find out more and more.
I for all time emailed this blog post page to all my associates, for
the reason that if like to read it then my friends will too.
Hi, I do believe this is an excellent website. I stumbledupon it ;
) I am going to revisit yet again since I saved as a
favorite it. Money and freedom is the best way to change, may you
be rich and continue to guide others.
Hi! I simply wish to give you a huge thumbs up for the excellent
info you’ve got here on this post. I’ll be returning to your blog for more
soon.
I’ve been exploring for a bit for any high-quality articles
or weblog posts in this sort of space . Exploring in Yahoo I finally stumbled upon this
site. Reading this information So i’m glad to show that I have an incredibly just right uncanny feeling I
discovered just what I needed. I such a lot surely will make sure to do not disregard this site and provides it a glance on a
continuing basis.
Thanks for sharing your thoughts on and. Regards
Why people still make use of to read news papers when in this technological world all is accessible
on web?
Awesome blog you have here but I was curious if you knew
of any forums that cover the same topics discussed here?
I’d really like to be a part of online community where I can get suggestions from other knowledgeable individuals that share the same
interest. If you have any recommendations, please let me know.
Appreciate it!
I love looking through a post that can make people think.
Also, thanks for allowing me to comment! quest bars http://tinyurl.com/49u8p8w7 quest
bars
We are a group of volunteers and opening a new scheme in our community.
Your website offered us with valuable information to work on. You’ve
done an impressive job and our whole community will be thankful to you.
asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr
I believe what you said made a bunch of sense.
However, think on this, suppose you added a little content?
I am not saying your content isn’t good., but suppose you added something that makes people
desire more? I mean Introduction to FreeRTOS Queues –
EcoderLenz is a little vanilla. You could glance at Yahoo’s home page and note how they
create news headlines to grab people to click.
You might add a video or a related pic or two to grab people excited about
what you’ve got to say. In my opinion, it would bring your website a little livelier.
cheap flights http://1704milesapart.tumblr.com/ cheap flights
Good post! We will be linking to this particularly
great post on our site. Keep up the great writing.
scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery
Enjoyed reading this, very good stuff, thanks. “Talk sense to a fool and he calls you foolish.” by Euripides.
Thank you for another informative website. The place else could I get that kind of information written in such a perfect method?
I have a project that I am simply now running on, and I have been on the look
out for such info. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
It’s amazing for me to have a site, which is useful in favor of my experience.
thanks admin quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
I think this is one of the such a lot significant information for me.
And i’m happy studying your article. However wanna statement on few common issues, The web site taste is perfect, the articles is really nice :
D. Just right job, cheers ps4 games https://tinyurl.com/45xtc52b ps4
I like this weblog very much so much good information.
Merely wanna tell that this is handy, Thanks for taking your time to write this.
Hi just wanted to give you a quick heads up and let you
know a few of the pictures aren’t loading properly.
I’m not sure why but I think its a linking issue. I’ve tried
it in two different browsers and both show the same results.
Good day very cool web site!! Man .. Beautiful ..
Superb .. I’ll bookmark your blog and take the
feeds additionally? I’m glad to search out so many useful info right here
in the submit, we need work out extra strategies on this regard, thanks for sharing.
. . . . .
Hi, I do believe this is a great site. I
stumbledupon it 😉 I’m going to come back once again since i have book marked it.
Money and freedom is the best way to change, may you be rich and continue to help others.
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 happy I found it and I’ll be bookmarking and checking back often!
I enjoy what you guys tend to be up too. Such clever work and exposure!
Keep up the amazing works guys I’ve added you guys to my own blogroll.
I am really enjoying the theme/design of your web site.
Do you ever run into any internet browser compatibility issues?
A few of my blog audience have complained about my website not working correctly in Explorer but looks great in Safari.
Do you have any suggestions to help fix this issue?
When I initially left a comment I appear to have clicked
the -Notify me when new comments are added- checkbox and now
every time a comment is added I get 4 emails with the exact same comment.
There has to be an easy method you are able to remove me from that service?
Thanks!
I really love your blog.. Great colors & theme. Did you make this site
yourself? Please reply back as I’m trying to create my very own site and would like to find out where you
got this from or just what the theme is named. Thank
you!
Hi there! 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 fast.
I’m thinking about setting up my own but I’m not sure where to begin. Do you have any tips or
suggestions? With thanks
Everyone loves what you guys are up too. This type of clever work and exposure!
Keep up the very good works guys I’ve incorporated you guys to my own blogroll.
Feel free to surf to my site – tracfone special coupon 2022
if you wanna make a fortume come check me out