当前位置:网站首页>Detailed discussion on modular architecture design of MCU firmware
Detailed discussion on modular architecture design of MCU firmware
2022-06-23 03:22:00 【HQYJ_】
[ Reading guide ] General beginners of SCM students , Just started to do MCU development , Not yet involved in the use of RTOS, And just started directly RTOS It may be difficult . Some use relatively old MCU, and the resources are still limited , It's not suitable for running RTOS. Or use RTOS, I'm confused about the overall idea , I don't know where to start . therefore , Today, let's talk about some ideas and experiences of my overall framework design of single-chip microcomputer program .
Why discuss architecture ?
One of the goals of MCU system developers is to create firmware in the programming environment , To achieve a low-cost system 、 Software reliability and fast development iteration time . The best way to implement this programming environment is to use a unified firmware architecture , The architecture acts as a framework in the product development process and supports “ Firmware modularization ”, Or subsystem .
If a unified design architecture is not adopted , Then the coupling relationship between business requirements is complex , Design first is not used - Post development methodology , Where to think and write , The later maintenance of the program will become extremely difficult , And introduce potential bug/ The risk of defects will also increase greatly , And there is no possibility of multi person collaborative development .
Can be combined with firmware modularization 、 The right combination of testability and compatibility design architecture can be applied to any firmware development project , To maximize code reusability , Speed up firmware debugging and improve firmware portability .
Modular architecture design ?
Modular programming decomposes program functions into firmware modules / Subsystem , Each module performs a function , And contains all the source code and variables required to complete the function .
【 Article Welfare 】 Let's share some related tutorials , You can also add learning groups 780619431 Discuss with you :
FreeRTOS In depth analysis of kernel source code
Intelligent insertion project :FreeRTOS+LwIP
A lesson will get you started FreeRTOS
( STM32+LoRa make LPWA The Internet of things system )
From single chip microcomputer to embedded system linux What we need to do 
modularization / Subsystem helps to coordinate the parallel work of many people in the team , Manage the interdependencies between the various parts of the project , And make designers 、 System integrators can assemble complex systems in a reliable way . say concretely , It can help designers implement and manage complexity . As applications grow in size and functionality , It takes modularity to divide them into separate parts ( No matter what it is “ Components ”,“ modular ” still “ Subsystem ”). then , Each such separate part becomes an element of the modular architecture . such , Each component can be isolated and accessed using a well-defined interface . Besides , Modular programming can improve the readability of firmware , At the same time, it simplifies the debugging of firmware , Testing and maintenance .
Even if one person develops a project independently , This is still in the debugging of the code 、 Readability 、 Portability is the overall strategy of best practices . If the code is well designed , Can be easily applied to other projects . And the module has been tested and verified by the previous project , Applying it again in new projects will greatly reduce the risk of defects . So every project , With this strategy, we continue to accumulate modules " wheel " Components , As experience grows , Accumulated “ wheel ” More and more , It's getting better and better . So its advantages are obvious , Otherwise, every project , It all starts with the wheel , It takes a long time to develop , The development level has not been improved , Repetitive work is also boring . For example, the nonvolatile storage management subsystem mentioned above , If the design is good , It becomes a reliable and portable wheel . Please understand this passage deeply , And take it away. No thanks !
Firmware module principle
The basic concept of modular programming in firmware development is to create firmware modules . conceptually , Modules represent separation of concerns . In computer science , Separation of concerns is the process of decomposing a computer program into unique functions that rarely overlap . A concern is any concern or function of the program , And synonymous with function or behavior . The development of separation of concerns has traditionally been achieved through modularity and encapsulation , In fact, it is decoupling thought .
Firmware modules can be divided into several types :
The code related to many upper user modules is implemented as a separate firmware module . Common, such as the abstract implementation related to the underlying hardware . for example ,hal_adc.c yes ADC The firmware module of the user module , and hal_timer.c yes Timer The firmware module of the user module .
The code for a particular pure software algorithm is implemented as a separate firmware module . for example ,alg_filter.c Is to execute software filters ( For example, median filter , Mean filter or weighted mean filter 、IIR/FIR wave filtering ) Firmware module .
The application specific code is implemented as a separate firmware module . for example ,app_battery.c Is the firmware module for the battery charger application . The code of a specific tool is implemented as a separate firmware module . for example ,debug_print.c It is a firmware module used to realize the log printing function .
…
Some rules for implementing estimation modular design :
All functions related to the module shall be integrated into a single source file , This is the embodiment of high cohesion .
The module provides a header file , This file declares all the resources of the module ( Hardware dependence / macro / Constant / Variable / function ). As far as possible with struct The closely related variables are lumped and encapsulated .
Include the self-test code in the source file , To realize all self-test functions of the module .
The interface of firmware module shall be carefully designed and defined .
Because firmware depends on hardware , Therefore, it is necessary to explicitly mention the correlation of hardware in the source file header . For example, use macros to turn hardware dependencies into definitions , Or use functions to encapsulate basic operations . In the new architecture , Just migrate this part of the implementation to use .
Usually , The firmware module can be used by other team members in other projects . It may involve managing changes , Defect repair 、 The owner shall maintain the module . The source file header should contain “ author ” and “ edition ” Information .
The firmware depends to some extent on the compiler . The header of the source file should state which development environment has been verified , To specify the compiler or with IDE Relevant information .
It should be noted that , Modular design introduces some call overhead , It is also possible to increase the firmware size . In practice , Compromise consideration . Don't be overly modular , Therefore, it is recommended to use high cohesion 、 Implementation strategy of low coupling .
The ventilator mentioned in the previous article PB560 The design of the , Read its code , I was going to interpret its code design , But I read it and found , Its design is too modular , There is no idea of achieving high cohesion . Many of its source code source files only implement a function , Instead of concentrating a class of problems on abstract implementation , Then he gave up his code interpretation .
How to split modules ?
Do engineering development , It must be demand driven . The first thing is to have a clear understanding of needs , Then we can design a more reasonable framework . What we need to achieve ? The general overall design process strategy is shown in the figure below ( I prefer drawing , The picture will make people more intuitive ).
The first question to ask yourself is : What are the main functions of this project ? Where does this come from ? If it is actual product development , It may come from the demand of the market , If it's your own DIY project , It will YY Come up with a general idea ? In short, no matter where it comes from , The demand must be sorted out first . So what are the requirements in general ?
What are the hardware IO Interface requirements , For example, switching value input ,ADC sampling ,I2C/SPI Communications, etc
What are the business logic requirements , For example, to collect the data of a sensor , Control a heating device , So this is the demand for high cohesion .
What are the technical requirements related to the algorithm , For example, which signals in the product need filtering processing , What needs frequency domain analysis, etc .
Whether there are external communication protocol requirements .
Is there any business data that needs historical storage , Or the equipment parameters need to be saved after power failure
Whether there is a need for log printing .
…
To name but a few .
Combined with the principle of firmware module and relevant guidelines , Then the demand with high relevance , The abstract is implemented in a series of modules , This series of modules cooperate to realize a highly relevant business requirement , Further, these modules become a subsystem . Multiple subsystems in main.c Under the control of , Coordinate and complete the overall function of the product .
How to integrate scheduling ?
For some, do not use RTOS In terms of application , You can use the following framework to :

Based on RTOS An example of integration implementation of :


It's different RTOS, The function names are different , But the general idea is generally the same .
To sum up
This paper starts with why the overall architecture of modular design is needed , To the benefits of doing so , And some specific guidelines , And then how to realize , How to achieve high cohesion and low coupling , It provides some personal experience and ideas . meanwhile , For bare metal program overall framework 、 be based on RTOS The integration framework does two demo, It can basically solve most of the framework thinking problems . Some of the principles advocated by individuals in the previous article , In bold summary :
All functions related to the module shall be integrated into a single source file , This is the embodiment of high cohesion .
The module provides a header file , This file declares all the resources of the module ( Hardware dependence / macro / Constant / Variable / function ). As far as possible with struct The closely related variables are lumped and encapsulated .
Include the self-test code in the source file , To realize all self-test functions of the module .
The interface of firmware module shall be carefully designed and defined .
Because firmware depends on hardware , Therefore, it is necessary to explicitly mention the correlation of hardware in the source file header . For example, use macros to turn hardware dependencies into definitions , Or use functions to encapsulate basic operations . In the new architecture , Just migrate this part of the implementation to use .
Usually , The firmware module can be used by other team members in other projects . It may involve managing changes , Defect repair 、 The owner shall maintain the module . The source file header should contain “ author ” and “ edition ” Information .
The firmware depends to some extent on the compiler . The header of the source file should state which development environment has been verified , To specify the compiler or with IDE Relevant information .
It is strongly recommended to use design first - Post development mode , More taboo debug, Where to think and write . Of course, for novice learning , The latter pattern , You can iterate step by step , You can also grow experience faster . Of course, how to choose , It's all personal .
I believe if you read deeply , To realize , We should get some understanding from the design idea , Improved . If it can help you , Then my heart is very relieved , It's worth the hard work of coding so many words . Of course, if you think the article is valuable , Please help me to see , Or forward and share . Let more friends see , Of course, for the great God of MCU development , The views in the article are quite superficial . As for appreciation , Then you can .
边栏推荐
- Application of map function in JS
- What are the advantages and difficulties of introducing AI into ISP Technology
- Flink practice tutorial: advanced 7- basic operation and maintenance
- QUIC or TCP
- Chapter IV open source projects and deployment
- Implementing StdevP function of Excel with PHP
- Learning record -- superficial understanding of unity decoupling
- Autowired usage
- Best practices for building multi architecture mirrors
- Troubleshooting and solution of error 400 in easygbs video platform
猜你喜欢
![Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]](/img/ca/672bfe49c8123da8679b2abeb43a2e.jpg)
Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]

Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]

Analysis on the development of China's graphene industry chain in 2021: with the support of energy conservation and environmental protection policies, the scale of graphene industry will continue to e

Encryption related to returnee of national market supervision public service platform
![Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]](/img/84/192d152ceb760264b6b555b321f129.jpg)
Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]
![Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]](/img/81/4d3cb059f700dd9243645e64023be7.jpg)
Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]
![Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]](/img/de/d73805aaf4345ca3d2a7baf85aab8d.jpg)
Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]
![Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]](/img/4b/dd272f98b89a157180bf68570d2763.jpg)
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
![[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]](/img/f1/972a760459a6d599b5681aa634df09.jpg)
[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]

Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo
随机推荐
Best practices for building multi architecture mirrors
Reading redis source code (IV) command request processing flow
How to install redis version 5.0.8 on the pagoda panel
Pre and post processing of pytest
Detailed explanation of label smoothing and implementation of pytorch tenorflow
Email authentication bypass
Ultra detailed Apache and PHP installation tutorial windows (2022.1)
JSON. Function of the stringify() optional parameter
Eight models of data analysis: detailed explanation of RFM model
How to share small programs released by wechat
Chapter IV open source projects and deployment
Autowired usage
An implementation of universal interface caching Middleware
Communication between containers flannel and calico comparison
Implementation process of the new electronic amplification function of easycvr video fusion cloud platform
Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo
How to print multiple barcode labels on one sheet of paper
Concept and function of ES6 symbol
The difference between the use of return, break and continue in the if statement in JS
How to gracefully solve the problem of platform font adaptation