A Detailed Explanation of Three Program Architectures in Embedded Development

Click ▲ to follow “IT168 Enterprise” for top updatesMore exciting content delivered to you firstIntroductionIn embedded software development, including microcontroller development, software architecture is a critical consideration for developers. The software architecture is vital for the overall stability and reliability of the system; a suitable software architecture is not only clearly structured but also facilitates development. I believe that most developers in the early stages of embedded or microcontroller software development adopt a simple sequential execution architecture (I did the same). In embedded software development, program architectures can be mainly divided into three types, and this article will provide a detailed explanation of these three program architectures.

▍1. The Significance of Software Architecture

A good program architecture can be seen as a dividing line between an experienced engineer and a beginner. Software architecture is friendly to developers; you can specify which tasks to execute first, which tasks to execute next, or which tasks to execute at a certain time point, and what events will synchronize with a certain task, etc. The specific methods to solve these problems differ 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 a particular program architecture, you will be able to quickly locate and resolve bugs that arise in the system. Of course, I recommend choosing a suitable software architecture for development based on needs, and the specific reasons will be discussed later in the article.

▍2. In-Depth Introduction to Three Different Program Architectures

The three commonly used software architectures are: sequential execution front-and-back system, time-slice polling system, and multitasking operating system. To provide a clearer understanding, I will introduce an example using these three software architectures. This example includes four tasks: key scanning, sound and light alarm, display refresh, and ultrasonic distance measurement. The specific function of this example is to set the threshold for measuring distance through a key. When the measured distance is below the set threshold, it triggers a sound and light alarm and displays the measured distance in real-time on the screen (this application is a specific embodiment of a car reversing radar).

  • 2.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 display refresh and ultrasonic distance measurement use interrupts. The measured distance is displayed after being obtained in the interrupt service function, and key detection and sound and light processing are also placed in the main loop. Thus, the entire program executes in a synchronized manner using variable flags between the main loop and background interrupts, as shown in the corresponding program code:

A Detailed Explanation of Three Program Architectures in Embedded Development

Main function of the sequential execution front-and-back system

A Detailed Explanation of Three Program Architectures in Embedded Development

Interrupt service function of the sequential execution front-and-back systemThe 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 can lead to poor real-time performance, such as in key detection.

  • 2.2 Time-Slice Polling System and Multitasking Operating System

The time-slice polling method typically appears in operating systems, meaning it belongs to the operating system. However, what is discussed here is the time-slice polling based on the front-and-back system. The essence of the time-slice polling method is to select a timer, and each time a timer interrupt occurs, the count value is incremented. In the main loop, tasks are executed based on this count value, which represents the time slice for task polling. In this example, if a time-slice polling system is used, we first select any timer from the main control chip, and the timer’s timing period is determined by us. To ensure real-time performance and operational efficiency, this value is usually set to 10ms, 30ms, 50ms, etc. I would set the key scanning polling value to 20ms, as the key bounce duration is generally around 20ms. This approach achieves debouncing while ensuring key detection is not missed; the display refresh is set to 30ms, and if you feel the refresh response is slow, you can modify this polling value for improvement; the polling value for ultrasonic distance measurement is set to 100ms, meaning distance measurement is triggered every 100ms, which can meet most situations.The program code is as follows:

A Detailed Explanation of Three Program Architectures in Embedded Development

Main function of the time-slice polling system

A Detailed Explanation of Three Program Architectures in Embedded Development

Timer interrupt function of the time-slice polling systemIt can be seen that the time-slice polling method has significant advantages over sequential execution, combining the benefits of both sequential execution and some advantages of operating systems.

  • 2.3 Multitasking Operating System

The operating system itself is a relatively complex entity, and the management and scheduling of tasks at the underlying level is quite complicated and challenging. However, we generally regard the operating system itself as a tool or platform; our goal is to utilize 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 program stability and development efficiency improve significantly. When using an operating system, we need to learn and understand its scheduling and communication methods. In fact, very few people can truly utilize an operating system; instead, most run bare-metal systems, which also relates to specific product requirements, as many simple systems only require bare-metal to meet their needs.I will not delve too deeply into the operating system itself here, as it is indeed quite complex. The code in the diagram below shows the structure of a program that controls an LED’s on/off state using a key in FreeRTOS, which you can compare:

A Detailed Explanation of Three Program Architectures in Embedded Development

Main function in the FreeRTOS multitasking system

A Detailed Explanation of Three Program Architectures in Embedded Development

Task callback function in the FreeRTOS multitasking operating system

▍3. 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 homes. I have worked with operating systems like uCOS, FreeRTOS, and Linux, and when returning to bare-metal development, I inevitably think about the design issues of the complete system’s software architecture. I believe that there is no best software architecture (program architecture), only the most suitable one. Different application scenarios require different program designs, and simply comparing which program architecture is the best is not practically meaningful. 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 often meets most of our needs, such as in rice cookers, induction cookers, and voice-controlled light bulbs; in cases where resources are limited in microcontrollers and high reliability is required, this method is very suitable because it consumes relatively little system resources, sacrificing only one timer. However, choosing this program architecture requires careful consideration of time slice allocation. Finally, in systems with complex functions and difficult logical control, a multitasking operating system is suitable, such as in video surveillance systems, drones, and other application scenarios.As an embedded software engineer, mastering these three software architectures is essential. They provide us with more choices and considerations when designing programs, and each different program architecture has its own advantages and disadvantages, which we need to practice diligently to appreciate their intricacies.Source: Embedded on the Left, C Language on the RightLink:https://www.toutiao.com/i6804446207872598539/

A Detailed Explanation of Three Program Architectures in Embedded Development

IT168 Enterprise

Let some people see the future of enterprise IT first

WeChat Public Account ID: IT168qiye

Leave a Comment