In a beginner from the introduction to the mastery of the way, such as the discovery problem → input thinking → proposed the learning model is very effective.
1
Problems encountered
Through this period of coding practice, accumulated some coding experience, but also realized the previous code structure defects:
(1) Low development efficiency: each use of a piece of resources (such as timers, etc.), the author will go to inquire technical manuals, compare eggache~
(2) More code repetition: each experimental source, such as Xtal_init, led_init and other initialization functions to write every time
(3) Difficult to modify: the code in the business logic and SFR operations mixed together, the readability is poor, modified also laborious
2
Thoughts caused by the layering of websites
The author before the study embedded programming, has had the ASP.net website development experience, to its stratification theory also has the practice, the following simple mention:
General has a certain degree of complexity of the site can be divided into the following three layers:
(1) Data access Layer (DAL): Responsible for interacting with the database for the business logic layer to call
(2) Business Logic Layer (BLL): Invokes the data access layer for data acquisition and support for specific business requirements
(3) User interface layer (UIL): Responsible for rendering the final user interface
I believe many friends are very familiar with this, no longer repeat. In a word, after layering, it greatly improves the reusability and extensibility of the code.
Then in the embedded development, can also use the idea of layering, to improve the development efficiency, enhance its maintainability and scalability of it. Below, is some of the author's humble opinion after thinking.
3
Embedded projects also come in a layered
Of course, can not copy asp.net specific layered ideas, specific problems to be specific analysis ~
First of all, embedded development is the core of the chip, it provides fixed in-chip resources for the use of developers. And it has a very important feature is that it does not change with the requirements of the project. Therefore, it should be the lowest level, providing basic support for the upper layer. We named it the Hardware abstraction layer (hardware abstract Layer).
The chip has certainly not enough, usually we will expand some function modules outside the piece to satisfy the specific project request, for example: sensor, keyboard, LCD screen and so on. This layer is characterized by changes in the project with the module as the unit dynamic increase or decrease. This layer of operation requires the support of internal chip resources, so should be in the hardware abstraction layer, and call for the upper level. We name it the functional module layer (functional module Layer).
OK, now the raw materials are ready: Chip + expansion module, the next step is to start the real processing: we need to flexibly invoke the previous two levels of the interface provided to achieve specific project requirements. We name it the application layer (application Layer).
Graphic:
1. Hardware abstraction Layer (HAL)
Implement a generic configuration of the resources in the chip (such as timers, ADC, interrupts, I/O, etc.), hide specific SFR operation details, and provide a simple and clear call interface for the upper layer.
2. Functional module Layer (FML)
By invoking the HAL, we can realize the function modules involved in the project, hide specific module operation details, and provide a simple and clear calling interface for the upper layer.
3. Application Layer (APL)
The final application function is realized by invoking HAL and FML.
4
A small test of a sledgehammer
OK, let's give a concrete example to illustrate the application of layered thinking.
Before, the author needs to complete a slightly comprehensive small experiment "temperature monitoring system", the requirements analysis is as follows:
The cc2430 node realizes the timing collection of the temperature and can indicate the sampling frequency by the LED lamp.
• Node transmits data to the PC side
• Nodes can receive control instructions from the PC to adjust the sampling rate and power mode
• Automatic reset capability with downtime
• Can go to sleep state, and can be awakened by the key
From the above requirements we can see that the core of the experiment chip for the CC2430, the need for the external expansion module for LED lights and buttons, is expected to achieve specific project requirements that 5 points above.
Next, we take advantage of the layered theory mentioned above to plan the code structure of the "Temperature Monitoring System" experiment:
Application Layer (APL)
[MAIN.C] refers to Hal.h, ioCC2430.h and module.h to achieve the specific application requirements of temperature collection, communication with PC, downtime reduction, etc.
Functional module Layer (FML)
[Module.h] defines a series of external function modules (LEDs, keystrokes), and a list of related function declarations
[module.c] refers to the hal.h, the implementation of the module (LED, key) function
Hardware abstraction Layer (HAL)
[IoCC2430.h] (System self): Defines all SFR, interrupt vectors for CC2430
[hal.h] includes common type definitions, commonly used assignment macros, and configuration of resource on CC2430 (I/O, serial communication, ADC, timer, power management, etc.)
(Note: Because this experiment involves the outer module--led and the key--the use is extremely simple, therefore the author merges it into the single source file. If you encounter a more complex module, you can create a separate. h and. c files, such as LCD.h, LCD.C.
After this design, its advantages gradually surface:
• Efficient development rate: After the hal.h in the HAL layer, we can easily invoke, without having to repeatedly query the details of the SFR set
• Rapid expansion: If you need to strengthen the system function, just add the corresponding function module (i.e.. c file) in the FML layer, and call it in MAIN.C
• High code reusability: the SFR operations provided by the HAL layer are available for general use and can be used directly in new CC2430 projects with little modification
• Better maintainability: The project code structure is clear, HAL and FML almost no need to modify, just modify APL
5
Conclusion
Perhaps for the embedded programming master, the above theory may be completely nothing, even there are a lot of mistakes. However, in a beginner from the introduction to the mastery of the way, such as the discovery problem → put into thinking → proposed learning model, I believe it is worthwhile and very necessary. As many people say: The process is more important than the conclusion.
The 5th phase of the 1.2018-year "Single-chip computer and embedded system applications," the new electronic publication.
2. Why is the program on the microcontroller not using malloc, and PC on the regular use?
3. Chip Spring and Autumn · Arm biography
4.4 Months of programming language popularity and recruitment trends
5. Programmers are eager for a "code-free World" to come.
6. Why the chips are so hard to get.
Disclaimer: This article is a network reprint, the copyright belongs to the original author. If you are involved in copyright issues, please contact us and we will confirm the copyright and pay royalties or delete the content according to the copyright certificate you provided.