C language embedded system programming practices Software Architecture Article 1

Source: Internet
Author: User
Module division

The "planning" of module division refers to planning, which means how to reasonably divide a large software into a series of functional independent parts to fulfill the system requirements. C language, as a structured programming language, is mainly based on functions in module division (Division by function becomes an error in object-oriented design, and Newton's Law encounters relativity ), modular C LanguageProgramThe design should understand the following concepts:

(1) A module is the combination of a. c file and A. H file. The header file (. h) declares the interface of this module;

(2) external functions and data provided by a module to other modules must be declared with the extern keyword in the. h file;

(3) Functions and global variables in the module must be declared with the static keyword at the beginning of the. c file;

(4) never define variables in. H files! The difference between defining variables and declaring variables is that defining operations that will generate memory allocation is the concept of the assembly phase; the declaration only tells the module that contains the Declaration to find external functions and variables from other modules during the connection phase. For example:

/* Module1.h */
Int A = 5;/* define int A */In the. h file of Module 1 */

/* Module1. C */
# Include "module1.h"/* contains the. h file of Module 1 in Module 1 */

/* Module2. C */
# Include "module1.h"/* contains the. h file of Module 1 in Module 2 */

/* Module3. C */
# Include "module1.h"/* contains the. h file of Module 1 in Module 3 */

The result of the above program is that the integer variable A is defined in Modules 1, 2, and 3. A corresponds to different address units in different modules. In this world, such a program is never needed. The correct method is:

/* Module1.h */
Extern int A;/* declare int A */In the. h file of Module 1 */

/* Module1. C */
# Include "module1.h"/* contains the. h file of Module 1 in Module 1 */
Int A = 5;/* define int A */In the. c file of Module 1 */

/* Module2. C */
# Include "module1.h"/* contains the. h file of Module 1 in Module 2 */

/* Module3. C */
# Include "module1.h"/* contains the. h file of Module 1 in Module 3 */

In this way, if modules 1, 2, and 3 operate on a, the corresponding memory unit is the same.

An embedded system usually includes two types of modules:

(1) hardware driver module. A specific hardware corresponds to a module;

(2) The division of software functional modules should meet the requirements of low coupling and high cohesion.

Multi-task or single task

The so-called "single-task system" means that the system cannot support multi-task concurrent operations and execute a task in a macro-serial manner. Multi-task systems can execute multiple tasks in parallel (micro-level serial) at the same time.

Concurrent execution of multiple tasks usually depends on a multi-task operating system (OS). The core of the multi-task operating system is the system scheduler, which uses the task control block (TCB) to manage the task scheduling function. TCB includes the current status, priority, events or resources to wait, start address of the task code, and initial Stack pointer of the task. The scheduler uses this information when the task is activated. In addition, TCB is used to store the context of the task ). The context of a task is all information to be saved when a task is stopped. Generally, the context is the current state of the computer, that is, the content of each register. When a task switchover occurs, the context of the currently running task is stored in TCB, and the context of the task to be executed is retrieved from its TCB and put into various registers.

Typical examples of embedded Multitasking OS include VxWorks and uClinux. Embedded OS is not an out-of-the-box thing. We can use less than 1000 lines.CodeThe author is preparing to implement an OS kernel with the simplest features for the 80186 processor and hopes to contribute his experience to you.

Whether to choose multi-task or single-task mode depends on whether the software system is large. For example, the vast majority of Mobile Phone programs are multitasking, but some PHS protocol stacks are single-task and have no operating system. Their main programs call the processing programs of each software module in turn, simulate a multi-task environment.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.