C language embedded system programming practices Software Architecture Article 2

Source: Internet
Author: User
Single taskProgramTypical Architecture

(1) starting from the specified address when the CPU is reset;

(2) Jump to assemblyCodeExecute at startup;

(3) Jump to the main program of the user owner and execute it in main:

A. testing various hardware devices;

B. initialize each software module;

C. Enter an infinite loop and call the processing functions of each module.

The user's main program and the processing functions of each module are completed in C language. The user's main program finally enters an endless loop. The preferred solution is:

While (1)
{
}

Some programmers write as follows:

For (;;)
{
}

This syntax does not exactly express the meaning of the code. We can't see anything from for (;). We can only understand what for (;) means unconditional loops in C.

Below are several "Famous" Endless loops:

(1) The operating system is an endless loop;

(2) Win32 programs are endless loops;

(3) embedded system software is an endless loop;

(4) The thread processing function of a multi-threaded Program is an endless loop.

You may argue, and say out loud: "Everything is not absolute, 2, 3, 4 can not be an endless loop ". Yes, you are right, but you cannot get flowers or applause. In fact, this is not very meaningful, because the world never needs a Win32 program to kill the OS after processing a few messages, you don't need an embedded system that just broke itself at the beginning of the run, and you don't need to start a thread for some reason. Sometimes, being too rigorous is not convenient but troublesome. The five-layer TCP/IP protocol stack surpassed the rigorous ISO/OSI Layer-7 protocol stack and became the de facto standard?

Some netizens often discuss:

Printf ("% d, % d", ++ I, I ++);/* What is the output? */
C = A ++ B;/* c =? */

. In the face of these problems, we can only express our sincere feelings: there are still many meaningful things in the world waiting for us to digest our food.

In fact, the embedded system will run to the end of the world.

Service Interruption

Interruption is an important part of embedded systems, but does not include interruption in Standard C. Many compilation developers have added support for interruptions on Standard C and provided new keywords for marking the interrupt service program (ISR), such as _ interrupt and # program interrupt. When a function is defined as ISR, the compiler automatically adds the on-site and out-of-stack Code required to interrupt the service program for the function.

The interrupted service program must meet the following requirements:

(1) the return value cannot be returned;

(2) parameters cannot be passed to ISR;

(3) ISR should be as short and precise as possible;

(4) printf (char * lpformatstring ,...) Functions may cause re-entry and performance problems and cannot be used in ISR.

In the development of a project, we designed a queue. In the interrupt service program, we only added the interrupt type to the queue, in the endless loop of the main program, the interruption queue is constantly scanned for interruptions. If yes, the first interruption type in the queue is taken out for corresponding processing.

/* Store the interrupted queue */
Typedef struct tagintqueue
{
Int inttype;/* interrupt type */
Struct tagintqueue * next;
} Intqueue;

Intqueue lpintqueuehead;

_ Interrupt isrexample ()
{
Int inttype;
Inttype = getsystemtype ();
Queueaddtail (lpintqueuehead, inttype);/* Add a new interrupt to the end of the queue */
}

Judge whether there is any interruption in the main program loop:

While (1)
{
If (! Isintqueueempty ())
{
Inttype = getfirstint ();
Switch (inttype)/* Is it like the message parsing function of Win32 program? */
{
/* Yes. Our interrupt type resolution is similar to message-driven */
Case XXX:/* do we call it "interrupt driver? */
...
Break;
Case XXX:
...
Break;
...
}
}
}

The interrupted service program designed according to the above method is very small, and the actual work is handed over to the main program.

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.