Embedded Linux C language (11)--c Language Modular programming

Source: Internet
Author: User

embedded Linux C Language (11)--C language modular programming Modular programming of C language

The so-called modular programming means that a program contains multiple source files (. c files and. h files), and each module is a combination of a. c file and a. h file, and the header file (. h) is a declaration of the interface for that module. The unspoken rules for. C,. h files in C-language modular programming:

1,. c files are mainly responsible for implementation, that is, define functions;. h files are primarily responsible for declarations, such as function declarations, macro definitions, and so on, the definition of structures, custom data types are generally also placed in header files, and variables cannot be defined in. h files. Write the code of a functional module separately into a. c file, and then place the module's interface functions in the. h file.

The difference between defining a variable and declaring a variable is that defining an operation that produces memory allocations is the concept of the assembly phase, whereas a declaration simply tells the module that contains the declaration to look for external functions and variables from other modules during the connection phase.

2, introduce the compiler comes with the header file (including standard header file) with angle brackets, introduce a custom header file with double quotation marks, for example:

#include <stdio.h>

#include "myFile.h"

The header file should be idempotent. That is, the effect of including the header file multiple times is exactly the same as the one-time effect.

#ifndef _stdio_h

#define _stdio_h

/* DECLARE part * /

#endif

3, the module provides to other the external functions and data of the block call are declared with the extern keyword in the. h file, and the functions and global variables within the module are declared with the static keyword at the beginning of the. c file.

II. Modular Programming in the development of embedded systems

Embedded systems typically consist of two types of modules:

(1) hardware driver module, a specific hardware corresponding to a module .

(2) software function module, its module division should meet the low coupling, high cohesion's requirements. The tighter the connection between the modules, the stronger the coupling, the less the independence of the module, and the more cohesive the elements within the module (between the names of the words and the program segments), the higher the cohesion.

1. single-tasking system

The so-called "single-tasking system" means that the system cannot support multitasking and perform a task in a macro-serial manner. Multi-tasking systems can perform multiple tasks "simultaneously" in macro parallel (possibly serially).

Typical architecture of a single-tasking program

(1) specified address from CPU reset   Start implementation;

(2) jump to the assembly code startup execution;

(3) jump to the user main program main execution, in main completed:

  A. Preliminary examination of the hardware equipment;

  B. initialize each software module;

  c. Enter the dead Loop (infinite loop) and call the handler function of each module

  The user main program and the processing functions of each module are completed in C language. The user main program finally enters a dead loop, the preferred scheme is:

while (1)
{
}

2. Multi-tasking system

concurrent execution of multitasking often relies on a multitasking operating system (OS), the core of the multitasking OS is the system scheduler, which uses task control blocks (TCB) to manage task scheduling functions. The TCB includes information such as the current state of the task, the priority, the event or resource to wait for, the starting address of the task code, the initial stack pointer, and so on. This information is used by the scheduler when the task is activated. In addition, the TCB is used to hold the "context" of the task. The context of a task is all the information that you want to save when an executing task is stopped. Typically, 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 the TCB and the context of the task to be executed is removed from its TCB and placed in each register.

Whether the choice of multi-tasking or single-tasking, depends on the software system is large. For example, the vast majority of mobile phone programs are multitasking, but also some of the PHS protocol stack is a single task, no operating system, their main program in turn calls the various software module handlers, simulation multi-tasking environment.

3. Interrupt Service Program

Interrupts are an important part of an embedded system, but no interrupts are included in standard C. Many compiler developers have added support for interrupts on standard C, providing new keywords for marking interrupt service programs (ISR), similar to __interrupt, #program interrupt, and so on. When a function is defined as an ISR, the compiler automatically increments the interrupt field into the stack and out-of-stack code required by the interrupt service program for that function.

The interrupt service program needs to meet the following requirements:

  (1) cannot return a value;

  (2) not to ISR passing parameters;

  (3) The ISR should be as concise as possible;

  (4) printf (char * lpformatstring,...) functions can introduce re-entry and performance issues and cannot be ISR used in the.

4. Hardware Driver Module

  A hardware driver module should typically include the following functions:

(1) Interrupt Service program ISR

(2) initialization of hardware

  A, modify the register, set the hardware parameters (such as the UART should set its baud rate, Ad/da device should set its sampling rate, etc.);

b ,

/* set interrupt vector table */
M_myptr = Make_far_pointer (0l);/* return void far : uart Interrupt service program */
/* offset from the first address of the interrupt vector table */
*m_ Myptr = &uart _isr; /* UART _isr uart */

(3) set the CPU control line for the hardware

  A, if the control line can be used as PIO (programmable I/O) and control signal, then set the internal CPU corresponding register as the control signal;

B, set the interrupt screen bit for the device within the CPU, set the interrupt mode (level trigger or edge trigger).

(4) provides a series of operating interface functions for the device. For example, for LCD, its driver module should provide the drawing pixel, draw line, draw matrix, display character lattice and other functions, and for real-time clock, its driver module needs to provide the acquisition time, set time and other functions.


This paper introduces the main ideas of C language module programming, such as module division, multi-task or single task selection, typical architecture of single task program, interrupt service program and hardware driver module design.

This article is to learn the Web blog and summarized by their own thinking, the source of the blog:

     C Language Chinese web, C language Modular programming WNHB embedded c language --- modular programming

Because of the complexity of the network blog, can not identify the original source, the listed sources for my study when the information.


This article from "Endless life, Struggle not only" blog, please be sure to keep this source http://9291927.blog.51cto.com/9281927/1790678

Embedded Linux C language (11)--c Language Modular programming

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.