RTOS, known in Chinese as 实时操作系统 (Real-Time Operating System), is the main focus of this series, which aims to master FreeRTOS. Next, we will compare it with bare metal systems and understand the advantages of RTOS, allowing us to clearly recognize why we should learn RTOS.

Bare Metal System
In a bare metal system, the functionality we implement in the while loop of the main function is the way the logical system operates. This means executing each function in sequence and looping indefinitely.
The execution method described above, which involves an infinite loop executing fixed content, is also known as a polling system.
If an immediate response is required, we can interrupt the currently executing task to prioritize the interrupt, and then continue executing the unfinished task. Of course, interrupts can also be interrupted by higher-priority interrupts.
RTOS (Real-Time Operating System)
In an RTOS, the operating system’s scheduler determines which task to execute. Each task runs in an infinite loop, and tasks are executed based on priority. When a high-priority task is ready, it preempts the low-priority task. After the high-priority task is completed, the low-priority task is executed, and so on. This approach allows for the appearance of multiple tasks executing simultaneously on a macro level.
The FreeRTOS we will learn has the following characteristics:
1. FreeRTOS is an open-source and free RTOS, which is both free and open-source;
2. FreeRTOS is highly configurable, meaning we can trim it according to our needs to meet user requirements;
3. As a lightweight operating system, FreeRTOS has only about 9000 lines of source code, making it easy to deploy on low-resource hardware, further enhancing its portability.
4. FreeRTOS does not impose limits on the number of tasks or their priorities; multiple tasks can operate at the same priority level. It is important to note that while FreeRTOS itself does not limit the number of tasks or priorities, if hardware priority definitions are used, they must be defined according to the hardware’s support.
5. FreeRTOS supports preemptive, cooperative (currently suspended), and round-robin scheduling algorithms to manage task execution.
6. All tasks are in infinite loops, managed by the task scheduling algorithm to control each task’s execution.
7. When the current task enters a waiting state, the CPU is released, allowing it to execute other lower-priority tasks.
Example Analysis
Next, let’s simulate a scenario: in the morning, while eating breakfast, we chat with friends on our phone. Suddenly, we realize we are going to be late, so we immediately get up and leave the house to take a car to our destination.
We can simulate the above process using both a bare metal system and RTOS:
Bare Metal System
We are continuously executing the task of eating. Messages from friends act as interrupts, and being late is like a flag. When the condition for the flag is met, we leave to take a taxi and wait until we reach our destination, occasionally replying to friends’ messages.
RTOS
We define eating as one task, replying to friends as another task, with a higher priority for replying to ensure timely responses; leaving to take a taxi to reach the destination is also a task with the highest priority. While executing the task of leaving to take a taxi, we plan our subsequent itinerary during the waiting time to reach our destination.
Comparative Analysis
Because RTOS manages task scheduling, the efficiency of CPU work increases, which is directly reflected in being able to do other things while taking a taxi. In contrast, the bare metal system can only handle very brief tasks during interrupts, completing one task before moving on to the next, at most replying to a message.
Overall, the advantages of RTOS lie in its efficient use of CPU resources, real-time responsiveness, parallel multitasking, and optimized resource management.
In summary, bare metal systems are suitable for simple logical scenarios, while RTOS is our best choice in larger, more complex environments.