In the previous post, I wrote about creating a simple FreeRTOS task. However, to demonstrate the FreeRTOS cooperative kernel, more than one task is needed. Basically when starting the FreeRTOS scheduler, an Idle task is automatically created which is an infinite loop so when the kernel starts running, it will never return back the control to the main program.
In cooperative multitasking, it is the responsibility of the currently running task to give up the processor resources to allow other tasks to run and that is the reason for calling it cooperative (tasks must cooperate for the OS to run). Consequently, FreeRTOS kernel performs this by calling a function to yield from task to another. When this function is called it performs a context-switch process. Context switching in the other hand consists of three main steps:
- Saving the context of the currently running task (in the portable layer)
- Selecting the next task to run (kernel job)
- Restoring the context of the next task (in the portable layer)
The first step is simply a reverse operation of the third one and they are performed in the portable layer of FreeRTOS. While the second step is the kernel responsibility where it is being decided which task should run next.
So to demonstrate the cooperative kernel, two tasks are created, which print a string each. Since in this project
printf is used to display the output, the stack used for the tasks had to be relatively large compared to the original minimal stack depth.
Within each task, the yielding function is called to cause the the above mentioned steps. The following figure shows the demonstration of this project phase.
Successfully demonstrating cooperative multitasking is considered a milestone in my project progress therefore in my git repository I created a release branch to represent this increment.
This is all for the moment, and when I come back I will write a post about the most complex and important phase in the project which is introducing the interrupt and the interrupt handling.
Till then I wish you the best of luck.