Follow+Star Public Account Number, don’t miss wonderful content
WeChat Public Account | Embedded Column
In embedded software development, including microcontroller development, software architecture is a crucial issue that developers must carefully consider. Software architecture is very important for the overall stability and reliability of the system; a suitable software architecture is not only clear in structure but also facilitates development and maintenance.
I believe that most developers adopt a simple sequential execution architecture in the early stages of embedded or microcontroller software development (I did the same). In embedded software development, program architecture can be mainly divided into three types, and this article will provide a detailed explanation of these three program architectures.
The Significance of Software Architecture
A good program architecture can be a dividing line between an experienced engineer and a beginner. Software architecture is user-friendly for developers; you can decide which tasks to execute first and which to execute later, or which tasks to execute at a certain time and which to execute next, or which events will synchronize with a certain task, etc. The specific methods for solving these issues vary under different software architectures.
The greatest help that software architecture provides to developers is: it helps developers grasp the framework of the entire project. Once you are proficient in using a certain program architecture, you will definitely be able to quickly locate and solve bugs that arise in the system. Of course, I recommend choosing a suitable software architecture for development based on needs; the specific reasons will be introduced later in the article.
Introducing Three Different Program Architectures
The three commonly used software architectures are:
-
Sequential Execution Front and Back System
-
Time-Slice Polling System
-
Multitasking Operating System
To give everyone a clearer understanding, I will introduce an example using these three software architectures. This example has four tasks: key scanning, sound and light alarm, screen refreshing, and ultrasonic ranging. The specific function of this example is to set the measurement distance threshold through the keys. When the measured distance is lower than the set threshold, it triggers the sound and light alarm and displays the measured distance in real-time on the screen (this application is a specific representation of a car reversing radar).
1. Sequential Execution Front and Back System
In the sequential execution front and back system, I will place the keyboard scanning in a querying manner within while(1), while the screen refresh and ultrasonic ranging use interrupts. After obtaining the measurement distance in the interrupt service function, it is displayed, and key detection is performed in the main function loop, with sound and light processing also placed in the main loop.
Thus, the entire program executes in a synchronized manner with variable flags between the main loop and background interrupts, as shown in the corresponding program code:

Main function of the sequential execution front and back system

Interrupt service function of the sequential execution front and back system
The advantage of this architecture is its simplicity and ease of understanding, while the disadvantage is that if each task occupies too much CPU time, it will lead to poor real-time performance of the program, such as in key detection.
2. Time-Slice Polling System and Multitasking Operating System
The time-slice polling method usually appears in operating systems, meaning it belongs to operating systems. However, what is mentioned here is time-slice polling based on the front and back system. The essence of the time-slice polling method is to select a timer, and every time a timer interrupt occurs, the count value is incremented. In the main loop, tasks are executed based on this count value, which is the time slice for task polling.
In this example, if a time-slice polling system is used, we first select any timer of the main control chip, and the timer timing cycle is determined by us. To ensure real-time performance and running efficiency, this value is usually set to 10ms, 30ms, 50ms, etc. I would set the key scanning polling value to 20ms since the key bounce duration is generally 20ms. This way, it achieves the purpose of debouncing without missing key detection; the screen refresh is set to 30ms, and if you feel the refresh response is slow, you can modify this polling value for improvement; the ultrasonic ranging polling value is set to 100ms, meaning it triggers ranging once every 100ms, which can meet most situations.
The program code is as follows:

Main function of the time-slice polling system

Timer interrupt function of the time-slice polling system
It can be seen that the time-slice polling method has significant advantages compared to sequential execution, combining the benefits of sequential execution with some advantages of operating systems.
3. Multitasking Operating System
The operating system itself is a relatively complex entity, and the management and scheduling of tasks at the bottom level are quite complicated and difficult. However, we generally treat the operating system itself as a tool or a platform; our goal is to use its functions rather than develop an operating system.
I have used small real-time operating systems like ucos and freertos, as well as large operating systems like Linux. With an operating system, both the stability of the program and the efficiency of development improve significantly. When using an operating system, we need to learn and understand its scheduling and communication methods. In fact, not many people can truly use an operating system; instead, most run on bare metal, which is also related to the specific requirements of the product. Many simple systems only require bare metal.
I won’t go into too much detail about the operating system itself here because it is indeed quite complex. The code in the following illustration shows the program structure for controlling an LED’s on and off using a key in freertos, which you can compare:

Main function in freertos multitasking system

Task callback function in freertos multitasking operating system
How to Choose the Right Software Architecture
I have used various MCUs for project development, such as: STM32, STC15, Nuvoton, etc., and have encountered complex design requirements such as: in-vehicle intelligent systems and smart home applications, and have worked with operating systems like ucos, freertos, and Linux.
When returning to bare metal development, one inevitably thinks about the design issues of the complete system’s software architecture. I believe most readers are also engaged in bare metal development.
I believe there is no best software (program) architecture, only the most suitable software architecture. Because different application scenarios require different program designs, simply comparing which program architecture is the best has no practical significance.
Next, let’s analyze specific application scenarios. In some logically clear and functionally simple systems, it is very suitable to choose a sequential execution front and back architecture. This software architecture can often meet most of our needs, such as rice cookers, induction cookers, and voice-controlled light bulbs; while in some microcontrollers with limited resources and high reliability requirements, this method is very suitable because it consumes relatively little system resources, sacrificing only one timer; however, choosing this program architecture requires us to carefully consider the division of time slices.
Finally, in some complex functional systems where logical control is difficult, it is suitable to choose a multitasking operating system, such as video surveillance systems, drones, and other application scenarios.
As an embedded software engineer, mastering these three software architectures is essential. They allow us more choices and considerations when designing programs, and each different program architecture has its own advantages and disadvantages, which requires us to practice diligently to appreciate its intricacies.
Statement: This article is sourced from the internet, and the copyright belongs to the original author. If there are any copyright issues, please contact me for deletion.
———— END ————

● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorial”
● Selected Tutorials from Embedded Column
Follow the public account reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.

Click “Read Original” to see more shares.