Microcontroller programs are familiar to everyone, but few truly consider the architecture used. As program development continues to increase, having a solid architecture becomes essential.
1. Time-Slice Polling Method
This is a program architecture design scheme that lies between sequential execution methods and operating systems. This design scheme aims to help embedded software developers elevate their skills. If the following points are encountered during embedded software development, this design scheme can be considered the optimal choice, especially for more complex embedded systems:
Current design requirements do not necessitate the use of an operating system.
Task functions do not need to be executed continuously; there are intervals (for example, button presses usually require software debouncing. Beginners often implement a delay of about 10ms before checking, but this significantly wastes CPU resources, as the CPU could handle many other tasks during this time).
There are certain real-time requirements.
This design scheme requires the use of a timer, typically set to 1ms (the timing can be adjusted, but if interrupts are too frequent, efficiency decreases; if interrupts are too long, real-time performance suffers). Therefore, the execution time of each task function must be considered, and it is recommended that it does not exceed 1ms (optimizing the program to shorten execution time is ideal; if optimization is not possible, the execution cycle of the task must be much greater than the time it takes to execute the task). Additionally, there should be no millisecond-level delays in the main loop or task functions.

Below are two different implementation schemes, one for those unfamiliar with function pointers and another for those who wish to learn further.
1. Design Method Without Function Pointers




2. Design Method With Function Pointers




2. Operating Systems
Embedded Operating Systems (EOS) are versatile system software that were primarily used in industrial control and defense systems in the past. For microcontrollers, commonly used options include UCOS, FreeRTOS, RT-Thread Nano, and RTX, among various preemptive operating systems (other operating systems like Linux are not suitable for microcontrollers).
In terms of task execution, operating systems do not impose strict time requirements on each task. Instead, they manage task execution through priority settings, where high-priority tasks can preempt lower-priority ones. Operating systems are relatively complex, so a detailed introduction is not provided here.
Regarding how to choose a suitable operating system (comparison of features among uCOS, FreeRTOS, RT-Thread, RTX, etc.):
-
uCOS: Rich online resources, very suitable for learning, but requires payment for product use.
-
FreeRTOS: Free to use, hence widely adopted in many products.
-
RT-Thread: A domestic IoT operating system with a wealth of components, also free. Documentation can be found at the RT-Thread documentation center.
-
RTX: A royalty-free, deterministic real-time operating system designed for ARM and Cortex-M devices.
Here is a comparison image from the internet:

3. Front-Back Sequential Execution Method
This is a commonly used program framework design scheme for beginners, requiring little consideration of various factors. The code is simple, and the overall real-time and concurrency requirements of the system are not high. After initialization, it continuously calls the functions written by the user through a while(1){} or for(;;){} loop, without much consideration for the time required for each function execution. In most cases, there are some millisecond-level delays in the functions.
Advantages: For beginners, this is the easiest and most intuitive program architecture, with simple and clear logic, suitable for software development with low complexity.
Disadvantages: Low real-time performance, as each function has some millisecond-level delays. Even a 1ms delay can cause different execution intervals for other functions. Although this can be mitigated through timer interrupts, the execution time of the interrupt function must be short. As program logic complexity increases, it can lead to confusion for later maintenance personnel, making it difficult to clarify the program’s operational state.




Source: https://blog.csdn.net/an520_/article/details/124877026
This article is sourced from the internet, freely conveying knowledge. Copyright belongs to the original author. If there are any copyright issues, please contact me for removal.

This small architecture is both beautiful and elegant
iPhone 15 Pro Max Teardown, with BOM list!
What are the C programming environments for microcontrollers?
A Huawei expert’s account: How to become an excellent engineer
