The critical region is where only one task will run on FreeRTOS and that task will continue to run until it exits the critical region. There will not be any other task running during that phase. For the older AEMB2 with only one active thread, the FreeRTOS will disable the context switching of the thread by disabling the interrupts. Since the interrupts are disabled for the active thread, no task can be switched in and the current running task will enter a critical region. As it exits the critical region, the interrupt are then re-enabled. For the newer AEMB2, there are two active threads running together. Both threads will run two different tasks or an infinite loop and a task. To make the critical region work for the new AEMB2, whenever one thread enters critical region, the other thread should not run any task. One possibility to implement the mechanism is to let the other thread run the infinite loop which does nothing.

When one thread enters the critical region, it needs to identify that the other thread runs the infinite loop before it can proceed inside the critical region. One way is to use the hardware mutex bit in the infinite loop. The infinite loop will control the mutex bit while the critical region function will check for the mutex bit. The modification are as shown.



The modification is tested with two critical region tasks with priority 1 and prints their task name. The output of the test shows the task 1 do not run anymore after a few cycles.


interrupt happens before disable

The GTKWave shows that an interrupt happened on the other thread before the critical region code disables the interrupt on the current thread. This forces the other thread to run the ISR and then task 2 while the first thread is still running task 1. This means that there are no critical region but both task are running side by side. This modification needs to be changed so that the disabling of the interrupt should occur only when no threads are interrupted.


The interrupt disabling macro is modified to check if there are any interrupts before proceeding to the next line of code. Every time the interrupts are disabled, the previous interrupt bit is stored in a register for checking. The output however shows another problem.



The simulator stuck at the infinite loop of the interrupt disabling macro. The FreeRTOS itself enters a critical region during the setup of tasks before the scheduler starts. As no interrupts are enable at the start, the macro will not proceed to the next line of codes but stuck at an infinite loop. This modification is not applicable and another way of controlling the critical regions might be needed.



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.