Does embedded development need architecture design?

[What can you learn by reading this article]

1. I have been engaged in embedded development for 12 years, and my understanding of architecture design;

2. To deliberately train the architecture design in the embedded system;

3. Some tips in the process of embedded system development;

4. A Demo for smart home projects, which can be directly compiled and executed;

 

[My understanding of architecture design]

1. Understanding of architecture design concepts

    I believe that most of the students who read this article are engaged in embedded development, and everyone must have such an impression: some of the architecture design positions on the recruitment website are for the Web direction, but rarely see recruitment The position of the system architect in the embedded position.

    

    My understanding is that there are probably the following 2 reasons:

(1) Web development: a hundred schools of thought contend, there is no unified standard and boss

    In recent years, thanks to the development of the mobile Internet, the demand for front-end and back-end development positions has greatly increased, and various frameworks have emerged one after another.

    How to use these frameworks to provide users with high-performance services does not have a unified standard, so a hundred schools of thought contend, and corresponding designer positions are emerging in endlessly.

 

(2) Embedded development: Linux will give it away

    In the development of embedded systems, there is hardly much room for the choice of operating system, most of which are the combination of ARM+Linux.

    At the Linux operating system level: Those great gods have designed the kernel and driver layer to perfection, and developers rarely need to make a lot of changes.

    At the application level: if the developer has nothing to pursue, only to achieve the functions defined in the specification.

    As for the boss, he only pays attention to whether the product function can be realized normally. As for the portability, scalability, execution efficiency, etc., he would not think of this level.

    Even if the product needs to be updated, let the developers re-implement it, anyway, only the function is OK.

 

2. The importance of embedded system architecture design

    Tell a little story.

    A colleague wrote a program for a single-chip product for a customer, and then the code was handed over to me after the colleague left.

    This product has a small feature that needs to be modified. It happened that I was working on another project, so with the permission of the boss, I sent the source code to the customer and asked them to modify it by themselves.

    Customers must be very happy because they have obtained the source code, because as long as they understand the code, other similar devices can be developed by themselves.

    After a while, I asked the customer: How was the function modification of that product last time?

    He said: I haven't got it done yet. I lost the code you gave last time. I will look people down. Now I am rewriting the code from scratch.    

 

    The story is true.

 

    Codes are composed of characters, some codes are pleasing to the eye, and some codes seem suspicious of life.

 

    Code without architectural design guidance has these shortcomings:

(1) The code cannot be reused and transplantation is troublesome.

(2) When the requirements change, the code cannot be adjusted quickly.

(3) For the existing code: Don't dare to change, don't want to change, it will affect the whole body.

(4) Debugging bugs is a headache.

    

    On the contrary, if the architecture is designed well, it is good for all aspects:

 

    For the project:

(1) Controllable project cycle

(2) Good code readability

(3) Function can be expanded

(4) Modifying a single module will not affect other functions

(5) Parallel development

(6) Convenient unit testing

 

    For developers

(1) Save development time

(2) A global perspective to improve the ability to develop large-scale projects

(3) Debug is easy and fast 

 

[How to design the architecture]

1. Design documents

    As long as you enter the field of programming, everyone knows that high cohesion, low coupling, sub-module and layered design are required.

    But what exactly needs to be done?

    How to do things well within the prescribed project cycle and make yourself less tired?

    How to pave the way for later maintenance?

    。。。

    These problems may be well planned at the beginning of the project.

    But in the execution process, it will become more and more lazy and deviate more and more from the predefined direction.

 

    my suggestion is:

    No matter the size of the project, no matter the length of the project cycle, there must be a design document, and the level of detail of the design document needs to be flexibly grasped according to the actual situation of the project.

    In the design document, it is necessary to reflect the design of the architecture. In the process of implementation, strictly follow the requirements in the document.

Take the above, get the inside; get the middle, get the bottom.

 

2. The physical model of the program file

(1) Layered design

    Business Layer

    Function module layer

    Drive layer

(2) Sub-module design

    Divide modules according to function

    Data interaction between modules through API interface functions

    Design flexible API interface functions 

 

3. Selection of Process and Thread

    In an embedded system, the realization of product functions can be accomplished through the cooperation of multiple processes, or multithreading. There is no fixed standard for this choice, and it depends on the specific circumstances of the project.

 

    My general approach is:

    If the product function is not complicated, try to use multiple threads to achieve;

    If the product is designed with more functions, put the strongly related modules in an independent process.

 

(1) Use process

    Each module is compiled independently and will not affect each other.

    When a problem similar to SegmentFault occurs, it is easy to locate the perpetrator.

    Facilitate distributed deployment.

    Code security: In addition to integrators, everyone else only needs to clone the code of the module they are responsible for, and has no permission or access to the code of others.

 

    But: need to consider the communication issues between processes, such as: IPC call, socket communication, bus. (I generally use a MQTT bus to connect all communication modules in the local system)

 

(2) Use threads

    The cost of creating threads is low.

    Sharing global variables between threads (from another perspective, this is also a disadvantage).

    It is convenient to call between modules because the function address is directly visible. 

 

4. API design

    A module can be regarded as a black box, given an input, it will return a certain result or perform a certain function.

    Only need to define this API interface function between modules.

    As for how the module is implemented internally, everyone has their own capabilities.

 

    In addition, if you are an API designer, you must pay attention to making it comfortable for the caller. Just like when you hand a pair of scissors to someone, you must give the other person your hand.

 

 

    Another experience is that in the early stage of project design, try not to design API functions too rigidly, which is easy to set yourself.

    E.g:

(1) Variables with char * can be designed, and json formatted strings can be used to transfer data of any length and type.

(2) Variables with void * can be designed to transfer addresses of any data type. This function is used in many projects superbly, such as the BREW platform of Qualcomm mobile phones long ago, and the ZWave platform in smart homes. 

 

5. Design of file directory

    This part is easy to understand. Files with different responsibilities should be stored in the corresponding directories: header files, library files, executable files, and related documents. If this part of the organization is not good enough, when you hand over the project to other colleagues, you will definitely be recited a thousand times in your heart: FUCK YOU!

 

6. Design of compilation script (build tool)

        When we receive an embedded project, after determining the plan, the platform on which the program runs is determined. In most cases, it is embedded Linux, or some variants.

 

    In the development stage, I have seen some developers cross-compile and put the code after debugging a function point, and then remotely mount it through NFS, or remotely copy it with scp, and execute it on a real device. I look tired.

 

    In fact, you can compile a version for different platforms in the compilation script.

    For example, if you use the Ubuntu system to develop products, as long as the x86 platform can simulate product functions, you can directly compile the x86 version.

 

    When all the function points are tested on the x86 platform and OK, then they are unified in the real embedded system for joint debugging, which can save a lot of time.

 

[Demo description]

1 Introduction

    This Demo is extracted from a smart home project. It only reflects the design of each functional module. There is no function inside the function, and it is only used to show the design process.

 

2. Code acquisition

https://pan.baidu.com/s/1B3F9byydXeNWdtgYEEQNLg  

Password: 3a9p

    Under Ubuntu 16.04 system, you can directly compile and execute.

 

3. System architecture diagram

 

4. Directory structure

    Makefile: compile script

    application: business layer

    module: functional module layer

    driver: hardware driver layer 

 

5. Perform sequence demonstration

    The orange arrow in the figure represents a control command sent from the cloud.

    After receiving the instruction, the business layer parses the instruction and sends it to the Control module.

    The Control module parses the specific command again, sends it to the ZigBee device, and records it in the log at the same time.

 

【END】

1. This is an original article, please respect the copyright. If you need to reprint, please keep the entire content and indicate the source. If it is convenient, please contact me to confirm.

2. If there are errors in the article, or if you want to communicate or discuss related content, please feel free to contact me.

3. My email: [email protected]

4. WeChat public account: IOT Internet of Things Town 

 

Guess you like

Origin blog.csdn.net/u012296253/article/details/106740159