On one hand we have FreeRTOS, an awesome tiny operating system you can run on almost any microcontroller in order to run concurrent processes. It is also great to learn how more complex operating systems, say Linux, work under the hood. And on the other hand we have Arduino, a small, low-cost and user-friendly microcontroller development board.
So, why not combine both? Let's have a look at it.
Getting Arduino ready
Assuming you have your Arduino at hand, let's get started. First and foremost Configure your PC to program Arduino with Atmel Studio.
Once you have done that, you could proceed to download FreeRTOS from its website. However, the FreeRTOS project is huge, as it includes examples for many microcontrollers and ARMs, despite that the FreeRTOS source code istelf is relatively compact. Furthermore, you'll find that none of the examples works directly on the ATmega328P, and that you will have to modify some files.
Specifically, you will have to modify how FreeRTOS accesses one of the ATmega328P's timers, as it is needed by the operating system to control how much time to allocate to each task.
But don't worry, you have to do nothing of that. Below is a link from which to download a ported Atmel Studio project for the ATmega328P.
Get FreeRTOS for Arduino UNO (ATmega328P)
The below project was created by [Cipriano], a collegue of mine at work. But truth be said, he told be that he took a great portion of the port from another blog or forum, but he can't remember from where exactly. As soon as we find out, I'll modify this post to add the credits.
Without further babbling, download the FreeRTOS project from this link:
[edit 2023: sadly, I'm unable to share this code anymore].
Test FreeRTOS
After downloading and unzipping the above project, you should be able to open it with Atmel Studio. Either from within Atmel Studio (File>>Open>>Project) or directly by double clicking on ArduinoFreeRTOS.atsln (the top-nmost project file contained in the zip).
Try to compile the solution and send it to Arduino as seen here. The result should be a blinking LED. If you want to go the next step, see this post on how to run several task concurrently on FreeRTOS.
If you have any questions or comments, please leave a comment below. I'll try to answer in the briefest possible time.
Hi,
ReplyDeleteThank you providing this blog. I was able to run the blinky example. I would like to know the changes you did in the FreeRtos if you can provide.
Thanks
I'm glad the example code was useful :)
DeleteAs for your question, can you be more precise about what you want to know?
In the example code (following the download link in the post, in main.c) I created a simple "blinkLED" function that I then added to the FreeRTOS scheduler using its "xTaskCreate" function. This ads "blinkLED" to the list of functions that FreeRTOS needs to call/schedule. Because there is only one function (aside from FreeRTOS built in idle task), FreeRTOS will check if there is any function to call, it will then call "blinkLED" (the LED will blink once), then the function exits, and FreeRTOS checks again if there are any other functions to call; because there are not, it will start all over again. The end result is that the LED should blink at a constant rate with a period close to (slightly above) 500 ms.