FreeRTOS
By Dorin Teodorov
Date: 23-10-2022
ES Semester 3 Course
Introduction:
This assignment we have learned about FreeRTOS, which is a small
capacity focused operating system for the STM32 board. With
FreeRTOS we can make multiple threads so code can run at the same
time, and a lot more.
Part 1 (Threads):
In the first part of this assignment we had to create the threads that
make a system of 3 scenes and 5 sprinklers work at the same time.
Process:
I used 5 LEDs as simulation and firstly I initialized them as output so
they can work.
Afterwards I made functions that turn them on and off:
I made different functions for faster work flow, but I am fully aware I
could have made only one function and give as parameter the
sprinkler number and then with a switch case handle each number.
I am storing each scene in a char array because it is easy to work with
it afterwards, modify it, etc. Each instruction is one element and then
the executeScene function identifies what to do with it, by using the
other functions above mentioned:
Testing:
For testing the program, I used the default thread to read the buffer
and I used a serial monitor to print values and tested if the leds
would open in the expected order. Everything worked perfectly(as no
thread was using the same led at the same time but different ones.
Part 2 (Mutexes):
For part 2 I was required to use Mutexes so no thread can use the
same led at the same time as other, but will wait for it to first turn off
the led and then use it too. Furthermore, I was required to use
mutexes for the scenes as well so one scene can not run at the same
time as itself, but run consequently right afterwards.
Process:
Firstly, I modified the third scene so it uses one of the LEDs as scene
2. I also added a forth scene for additional testing.
After creating the mutexes, for each critical part of the code (like
using a led or running a scene), I have added the required Mutex
condition:
Testing:
The same testing was done as in part 1, but this time I would run
scenes that I know share the same led and see if they wait for each
other and they did. Furthermore, I ran one scene 2 or more times in a
row and I noticed that, as expected, it ran consequently after each
termination of itself.
Part 3 (Queue):
For the third part, I had to change the program so in each scene
thread, the executeScene function would not read the scene
instructions directly from array, but form a message queue, for better
thread safety.
Process:
After created the queue, everytime we receive an input from the
terminal and we have to run a scene, we put the scene instructions in
the queue, and everytime we have to execute the scene we execute
it with the message from the queue. The implementation will be
shown below:
To answer the second step questions: yes the program is now thread
safety as it is not using a volatile variable to communicate between
its threads, but a queue and because threads have mutexes to
prevent safe functions and sprinkles to be run at the same time. Even
if a Scene is redefined, the executeScene function will know what to
do and the information will be safely transmitted between threads
(even if for this assignment I haven’t implemented Scene
redefinition). The program will function correctly everytime.
Testing:
The program is successful if the testing works exactly the same as in
the second part.
Conclusion:
FreeRTOS is an easy-to-use OS that helps with Threads and other
functionalities that can make a great program work as expected. It
has a lot of versatility for such a small OS. I will definitely use it more
in the future and I have learned its basics.
References:
Canvas presentation:
https://git.fhict.nl/technology/t-sem3-cb/-/blob/master/es/
FreeRTOS/RTOS.pptx
CMSIS RTOS website:
https://www.keil.com/pack/doc/CMSIS/RTOS/html/index.html