The software architecture of C language Embedded system programming

Source: Internet
Author: User

Module Division

Module Partition "Zoned" is the meaning of planning, meaning how reasonable to divide a large software into a series of functional independent part of the cooperation to complete the system needs. C language as a structured programming language, in the division of modules mainly based on functions (according to the function of the division in the object-oriented design to become a mistake, Newton's law encountered relativity), C-language modular programming needs to understand the following concepts:

(1) The module is a combination of a. c file and a. h file, and the header file (. h) is a declaration of the module interface;

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

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

(4) Never define a variable in the. h file! The difference between defining a variable and declaring a variable is that defining the operation that produces the memory allocation is the concept of the assembly phase, whereas the declaration simply tells the module containing the declaration to look for external functions and variables from other modules during the connection phase. Such as:

/*module1.h*/
int a = 5; /* 在模块1的.h文件中定义int a */
/*module1 .c*/
#include "module1.h" /* 在模块1中包含模块1的.h文件 */
/*module2 .c*/
#include "module1.h" /* 在模块2中包含模块1的.h文件 */
/*module3 .c*/
#include "module1.h" /* 在模块3中包含模块1的.h文件 */

The results of the above program are defined in Modules 1, 2, 3, the integer variable a,a in different modules corresponding to different address units, the world never need such a program. The correct approach is:

/*module1.h*/
extern int a; /* 在模块1的.h文件中声明int a */
/*module1 .c*/
#include "module1.h" /* 在模块1中包含模块1的.h文件 */
int a = 5; /* 在模块1的.c文件中定义int a */
/*module2 .c*/
#include "module1.h" /* 在模块2中包含模块1的.h文件 */
/*module3 .c*/
#include "module1.h" /* 在模块3中包含模块1的.h文件 */

So if the module 1, 2, 3 operation A, the corresponding is the same piece of internal deposit.

An embedded system usually includes two types of modules:

(1) Hardware driver module, a specific hardware corresponding to a module;

(2) Software function module, the partition of its module should meet the requirements of low coupling and high cohesion.

Multitasking or a single task

The so-called "single task system" means that the system can not support multi-task concurrent operation, macro serial execution of a task. Multi-task systems can perform multiple tasks at the same time in macro-parallel (microscopic, possibly serial).

Concurrent execution of multitasking often relies on a multitasking operating system (OS), and the core of the multitasking OS is the system scheduler, which uses task control blocks (TCB) to manage task scheduling functions. TCB includes information such as the current state of the task, priority, the event or resource to wait for, the start address of the task code, the initial stack pointer, and so on. The scheduler uses this information when the task is activated. In addition, TCB is also used to hold the context of the task. The context of a task is all the information that is to be saved when a task in execution is stopped. Usually, the context is the current state of the computer, which is the contents of each register. When a task switch occurs, the context of the currently running task is stored in TCB, and the context of the task being performed is removed from its TCB and placed in each register.

Typical examples of embedded multitasking OS are VxWorks, Uclinux, and so on. Embedded OS is not a remote shrine, we can use less than 1000 lines of code to achieve a 80186-processor function of the simplest OS kernel, the author is ready to do this work, hope to be able to contribute to the experience.

Whether the choice of multitasking or a single task, depending on the software system is large. For example, the vast majority of mobile phone programs are multitasking, but there are some PHS stack is a single task, there is no operating system, their main program in turn invoke each software module of the processing program, simulation of the multitasking 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.