I. Problems Encountered
In the "ZigBee Journey" series of blog posts, the author will write a small experiment to explain each article. Through this period of practice, I have accumulated some coding experience, but I also realized the defects of the previous code structure:
(1) low development efficiency: each time a single resource (such as a timer) in the tablet is used, the author will query the cc2430 Chinese manual to compare eggache ~
(2) code is too repetitive: in the source code of each experiment, suchXtal_init,Led_initWait until the initialization function is written every time
(3) Not easy to modify: the business logic in the Code is mixed with the SFR operation, with poor readability and effort to modify it
Because of the above problems, the author decided to suspend the continuous writing of this series of blog posts, and took time to think about the solution.
Ii. Thoughts Caused by website layering
Before learning embedded programming, I have experienced ASP. Net website development and have some practices on its layering theory. The following is a brief introduction:
Generally, websites with certain complexity can be divided into the following three layers:
(1) data access layer (DAL ):Interacts with the database for calling at the business logic layer.
(2) Business logic layer (BLL ):Call the data access layer to obtain data and provide support for specific business needs.
(3) User Interface Layer (UIL): Displays the final user interface
I believe most of my friends in the blog are familiar with this and I will not go into details here. In short, code reusability and scalability are greatly improved after the hierarchy.
In embedded development, can we also improve development efficiency and enhance maintainability and scalability by using the layered approach? Below are some of my thoughts.
3. Embedded projects are also divided into layers
Of course, you cannot copy the specific layering idea of ASP. NET. Do you have to analyze the specific issues ~
First, the core of embedded development is the chip, which provides fixed in-chip resources for developers to use. In addition, it has a very important feature that it does not change with the needs of the project. Therefore, it should be used as the bottom layer to provide basic support for the upper layer. We name itHardware Abstraction Layer(Hardware abstract layer ).
The chip is not enough. We usually expand some functional modules out of the chip to meet specific project requirements, such as sensors, keyboards, and LCD screens. This layer is characterized by dynamic increase or decrease of modules as the project changes. The operation of this layer requires the support of the internal resources of the chip. Therefore, it should be placed on the hardware abstraction layer and called for the upper layer. We name itFunction Module layer(Functional Module layer ).
Okay, now all the raw materials are ready: Chip + expansion module. Now we need to start the real processing: We need to flexibly call the interfaces provided by the previous two layers to meet specific project requirements. We name itApplication Layer(Application Layer ).
Text:
(1) Hardware Abstraction Layer (HAL)
Provides general configurations for On-Chip resources (such as timer, ADC, interrupt, and I/O), hides detailed SFr operations, and provides simple and clear interfaces for the upper layer.
(2) Functional Module layer (FML)
By callingHalTo implement the off-board function modules involved in the project, hide the operation details of specific modules, and provide simple and clear calling interfaces for the upper layer.
(3) Application Layer (APL)
By callingHalAndFMLTo implement the final application function.
4. Test the tool
Okay. Let's take a specific example to illustrate the application of stratified thinking.
When writing a blog article in the "ZigBee Journey" series, I need to complete a slightly comprehensive small experiment "Temperature Monitoring System". The requirement analysis is as follows:
•The cc2430 node periodically collects the temperature and uses LED lights to indicate the sampling frequency.
•Node transfers data to PC
•The node can receive control commands from the PC to adjust the sampling rate and power mode.
•Automatic Reset upon Shutdown
•Can enter sleep status, and can be awakened by pressing the button
From the above requirements, we can see that the core chip of this experiment is cc2430, and the external expansion module is LED lights and buttons, which are expected to meet the requirements of specific projects, that is, the above five points.
Next, we will use the layer theory mentioned above to test the code structure of the "Temperature Monitoring System" experiment:
(1) Application Layer (APL)
[Main. c] Reference Hal. H, iocc2430.h, and module. h to achieve specific application requirements, such as temperature collection, intercommunication with PCs, and shutdown reset.
(2) Functional Module layer (FML)
[Module. h] Defines a series of off-chip function modules (LED, buttons), and a series of related function declarations
[Module. c] Reference Hal. h to implement the functions of each off-Chip Module (LED, button)
(3) Hardware Abstraction Layer (HAL)
[Iocc2430.h] (Built-in system):Defines all SFR and interrupt vectors of cc2430
[Hal. h] Includes definition of common types, assignment macros, and configuration of cc2430 on-chip resources (I/O, serial communication, ADC, timer, power management, etc)
(Note: the use of the On-Chip Module-led and button-involved in this experiment is extremely simple, so the author combines it into a single source file. If you encounter complicated modules, you can create a new. h and. c file separately, such as LCD. h and LCD. C)
With this design, itsAdvantagesGradually surfaced:
• Efficient development speed: Complete the compilation
Hal. hThen, we can easily call it without having to repeatedly query the detailed settings of SFr.
• Quick Scaling: To enhance system functions, you only need to add the corresponding function module (namely, the. c file) to the FML layer and call it in Main. C.
• High code reusability: SFr operations provided by the Hal layer are available for general use, and the layer can be directly used in the new cc2430 project without any modifications.
• Good maintainability: The project code structure is clear. Hal and FML do not need to be modified. You only need to modify APL. Conclusion 5:
For embedded programming experts, the above theory may be totally irrelevant, and there may even be a lot of errors. But on the way from getting started to being proficientDiscovering problems → investing in thinking → proposing solutionsI believe it is worthwhile and necessary. As many people have said: the process is more important than the conclusion.
Next, I will devote most of my energy to the final work of the first phase of the "ZigBee Journey. I hope that after learning the C51 coding specification and thinking about code layering, I can compile a small but dirty project code.
Coming soon: ZigBee Tour (10): exploratory small comprehensive experiment-Temperature Monitoring System Based on cc2430 (unfinished)