
Click on the blue text above to follow us.
If you are interested, please check out the detailed FreeRTOS series column articles on CSDN.
Among the current RTOS, there are mainly two popular programming methods. Next, I will explain these two programming methods through pseudo-code.
1
Initialize hardware and RTOS in the main function, create all tasks
This method initializes the hardware in the main function, initializes the RTOS system, creates all tasks, and finally starts the RTOS scheduler to begin multi-task scheduling.
The pseudo-code is as follows:
Program description is as follows:
(1) Hardware initialization. This step belongs to the realm of bare metal, where we can initialize and test all the hardware we need to ensure correctness.
(2) RTOS system initialization. For example, initializing global variables in the RTOS, creating idle tasks, etc. Different RTOS have slight differences in their initialization.
(3) Create various tasks. Here, all tasks to be used are created, but scheduling does not occur yet, because the RTOS scheduler has not been started at this time.
(4) Start the RTOS scheduler to begin task scheduling. At this point, the scheduler selects the highest priority task from the tasks just created to start running.
(5)(6) Task entities are usually a C function without a return value in an infinite loop, and the function body must have blocking situations; otherwise, the task (if its priority is the highest) will continue to execute in the while loop, preventing other tasks from having the opportunity to execute.
2
Initialize hardware and RTOS in the main function, create a start task
This method initializes the hardware and RTOS system in the main function, then creates a start task and starts the task scheduler. The start task creates various application tasks, and after all tasks are successfully created, the start task deletes itself.
The pseudo-code is as follows:
Program description is as follows:
(1) Hardware initialization. This step also belongs to the realm of bare metal, where we can initialize and test all the hardware we need to ensure correctness.
(2) RTOS system initialization. For example, initializing global variables in the RTOS, creating idle tasks, etc. Different RTOS have slight differences in their initialization.
(3) Create a start task. Then create various application tasks within this initial task.
(4) Start the RTOS scheduler to begin task scheduling. At this point, the scheduler executes the start task just created.
(5) We usually say that a task is a C function without a return value in an infinite loop. However, due to the special nature of the start task, it cannot be infinite; it executes only once and then closes. In the start task, we create all the tasks we need.
(6) Create tasks. Each time a task is created, it enters the ready state, and the system will perform a scheduling. If the priority of the newly created task is higher than that of the start task, it will execute the newly created task. When the new task blocks, it returns to the point where the start task was interrupted and continues execution. Conversely, it continues to create new tasks until all tasks are created.
(7) After all application tasks are created, the start task closes itself, completing its mission.
(8)(9) Task entities are usually a C function without a return value in an infinite loop, and the function body must have blocking situations; otherwise, the task (if its priority is the highest) will continue to execute in the while loop, preventing other tasks from having the opportunity to execute.
So, which of these two methods is better? I haven’t found out yet. LiteOS and ucos can use both methods, left to user choice, while RT-Thread and FreeRTOS default to the second method.

-
A Comprehensive Guide to FreeRTOS: Embedded Software System Architecture
-
A Comprehensive Guide to FreeRTOS: Multi-task Mechanism of Embedded Multi-task System
-
A High-Speed ADC and DAC Conversion Circuit Sharing
-
How many frames can be sent per second in a CAN FD network?

