Application of uCOS-II in mobile GPS mobile terminal

1. General overview

At present, commercial embedded system products on the market, such as Vxwork, PSOS and Windows CE, are very mature, providing powerful development and debugging tools, but the development cost is expensive, and uCOS-II is a multi-task real-time operating system, kernel source The code is open, short and capable, and has strong portability. It is very suitable for the development of some small systems. This system describes how to transplant and apply uCOS-II to MCS51 series single-chip microcomputers, and discusses how to apply it to the "embedded equipment-vehicle GPS" system.

1. Introduction to uCOS-II

The real-time embedded operating system uCOS-II is a priority-based preemptive real-time multitasking operating system, including real-time kernel, task management, time management, communication synchronization between tasks (semaphore, mailbox, message queue) and memory management. . Most of the code is written in C language, and the hardware-related parts are written in assembly language, and its source code is open and free.

uCOS-II is for small and medium-sized embedded systems. The core containing all functional modules is about 10K. If only the core code is retained after reduction, it can be compressed to about 3K. The occupancy of RAM is related to the number of tasks in the system. The stack of the task occupies a large amount of RAM space. The size of the stack depends on the local variables of the task, the size of the buffer and the number of possible interrupt nesting levels. The time accuracy of the application program is determined by the system clock tick. UCOS-II requires the user to provide a periodic clock signal source for time delay and confirmation timeout. The general clock tick should be between 10 and 100 Hz (the maximum accuracy is 10 ms) Because uCOS-II checks whether every higher-priority task is waiting to be executed at every beat, if there is, it is necessary to switch tasks, so the higher the clock tick rate, the heavier the system's additional load.

2. The design goals of the system

This vehicle-mounted mobile terminal mainly completes the following control functions:

(1) Location and related information transmission, including real-time request transmission, equal time interval transmission, equidistant transmission, transmission methods include GPRS method and short message method, because GPRS method for data communication is charged according to the flow, every 1K bytes 2- 3 cents, the cost is much lower than SMS, so this system uses GPRS as the main communication method, with SMS as the supplement.

(2) Alarm function, divided into the following parts:

A. Specific area alarm function: After setting a specific alarm area (such as the driving task area specified by the control center), when the vehicle drives out of the set area, the monitoring center will alert the vehicle-mounted unit and record the real-time location information of the vehicle in time.

B. Emergency alarm function: When the vehicle encounters emergencies such as robbery and traffic accidents, the driver can send a distress signal to the control center through the emergency distress button and upload the vehicle positioning data.

C. Anti-theft alarm function: When the vehicle is set to the anti-theft state, any illegal movement of the vehicle, the vehicle-mounted unit will automatically alarm and upload vehicle positioning data.

D. Power failure alarm function: When the main power of the vehicle-mounted unit is powered off (or artificially cut off), the vehicle-mounted unit will automatically alarm and upload vehicle positioning data.

E. It can combine automatic alarm with manual alarm: the system supports manual single-key alarm and automatic alarm function generated by intelligent equipment. The single-key manual alarm requires the driver to perform fast concealed single-key operation to quickly alarm. Automatic alarms such as intelligent illegal mobile alarms, the system automatically generates alarm information and sends it to the monitoring center, and saves the alarm data, and the personnel of the monitoring center can take measures as needed.

(3) Power supply monitoring function, real-time monitoring of the backup power supply, if the power is found to be insufficient, it will automatically switch to the charging mode until the power is sufficient to automatically cut off the charging mode.

3. Function blocks of the system

The system structure is shown in Figure 1. There are three layers from outside to inside: hardware circuit layer, task layer, and operating system layer.

Figure 1 System structure diagram

Second, the hardware circuit layer design

The mobile terminal of this system mainly includes the following four parts: GPS module, GPRS module, handle, single-chip microcomputer control module, and its general functions are described as follows:

(1) GPS module-used for the collection of satellite positioning data, the collection time interval can be set, and the minimum interval is one collection per second

(2) GPRS communication module-used to realize GPRS data sending and receiving, short message sending and receiving and voice call function.

(3) Handle-used for voice calls.

(4) Microcontroller control module-used to control data reception, transmission, voice call control, short message sending and receiving, power supply monitoring management and oil and electricity control of GPS and GPRS modules.

3. Design of the task layer

1. System task layer composition and priority setting

The system task layer has the following six tasks in parallel: monitoring task, key processing task, off-hook task, GPRS task, short message task, serial port receiving task. Each task consists of the following three parts: application, task stack and task control block. Among them, only the application program is burned into the ROM, and the task itself is placed in the RAM, and then established when the system is running. The task stack is used to store CPU register contents. When a task changes from a running state to another state, the contents of the CPU register are pushed into the corresponding task stack, otherwise, the contents of the corresponding task stack are placed into the CPU register. As a data structure defined in the system, the content of the task control block includes the address of the task stack, the current state of the task, and the task priority. The operating system manages tasks by querying the content of the task control block.

The setting of priority is determined by the execution order of each task and the size of the impact on system security. The priority from high to low is: monitoring task, key processing task, off-hook task, GPRS task, short message task, serial port reception task. The system adopts static priority setting, that is, the task priority does not change during operation.

2. The status of the task

There are 4 kinds of state of each task in this system: wait state, ready state, running state and interrupt state. The state transition relationship is shown in Figure 2. When a task occupies the CPU, the task is in the running state, and its priority must be higher than that of all ready tasks. If the system operation causes the priority of a task in the ready state to be higher than the priority of the running state task, the scheduling function is called, the running state task will lose the CPU occupation and become the ready state, and the highest priority ready state task will become Running state. Only one task can be running at a time. The transition of the task between the ready state and the running state is called task switching. When the task in the running state expects a certain message (that is, the data transfer between the task and the task), the task will lose the right to occupy the CPU and switch to the waiting state. The waiting time can be set by the system. If the task receives a message within the waiting time, the task will be changed to the ready state, otherwise it will be forced to be ready by the time management function. When the interrupt occurs, the task in the running state will be put into the interrupt state, and the occupancy right of the CPU will be lost. Because there may be a message sent during the interruption, the task in the waiting state is transferred to the ready state, so after the interruption returns, the task scheduling function will be run first to determine the task state.

4. Software layer design

This system selects μC / OS-II operating system and transplants it to MCS51 series single-chip microcomputer. The clock tick adopted by the system is TIck = 50 times / second (that is, 0.02 seconds / time), and all tasks and semaphores, message tanks, message queues, etc. are created in main.

void main (void)

OSInit ();

3. Off-hook task

When the handset is lifted or lowered, an interruption occurs. In the interrupt, OSSemPost (HookSem) is called to wake up the off-hook task and clear the interrupt flag. The off-hook task calls OSSemPend (HookSem, 0, & err) to obtain the semaphore. After the semaphore is obtained, the off-hook status flag is used to determine whether it is off-hook or on-hook. When on-hook, if the phone was off-hook before ringing, the off-hook task will treat it as a received call; if it is not off-hook when ringing, then off-hook task will be off when on-hook It is treated as a dialed call.

4. GPRS task

When the serial port reading task receives GPRS data, call the OSQPost (GprsQ, (void *) & Gprs_Buf [0]) function to always wake up the GPRS task. The GPRS task continuously calls gprs_msg = OSQPend (GprsQ, 50, & err) to obtain the serial port reading task According to the current status of the GPRS data sent in, it is decided whether to send positioning data and related information to the control center.

5. Short message processing task

When the GPRS network is unavailable, the system starts the short message task to communicate data. When the read serial port task receives the short message, it calls OSQPost (SMsgQ, (void *) & SMsg_Buf [0]) to send a message to the short message task. The SMS task continuously calls sm_msg = OSQPend (SMsgQ, 100, & err) to get the SMS, and then sends and receives the corresponding SMS.

6. Read serial port task

In the serial port reading task, read the character string sent from the GPRS communication module and GPS module from the receiving buffer, and analyze the received character string to process and send messages to the GPRS task and short message task.


This article describes the implementation of uC / OS-II on the MCS51 hardware platform, and proposes a solution to the embedded system design based on uC / OS-II in view of the poor stability of traditional single-chip programming methods. However, using real-time kernel to manage these tasks will increase the system's memory capacity and CPU time consumption, and the advantages of task scheduling can not be well displayed, so the design has certain limitations. However, when the memory of the system is large enough and the CPU runs fast enough, using the real-time kernel uC / OS-II design can improve the reliability and stability of the system, which is conducive to the subsequent development of the system. This system uses CPU It is W78E516, external expansion 32K RAM, crystal frequency is 22.1184M, can well meet the requirements of the system.

// Create semaphore, message queue;

HookSem = OSSemCreate (0); // Wake up the off-hook task

GprsQ = OSQCreate (& GprsMsg [0], 10);

SMsgQ = OSQCreate (& SMsg [0], 5);

// Create a memory area;

Mem20 = OSMemCreate (Part1,20,50, & err);

Mem50 = OSMemCreate (Part2,100,10, & err);

// Task creation;

OSTaskCreate (WatchDogTask, (void *) 0, & WatchDogStk [0], 2); // Monitoring task

OSTaskCreate ((void *) KeyTask, (void *) 0, & KeyTaskStk [0], 3); // Key processing task

OSTaskCreate ((void *) WriteTask, (void *) 0, & WriteStk [0], 4); // Off-hook task

OSTaskCreate ((void *) GPRSTask, (void *) 0, & GPRSStk [0], 5); // GPRS task

OSTaskCreate ((void *) SMsgTask, (void *) 0, & SMsgStk [0], 6); // Short message task

OSTaskCreate (ReadTask, (void *) 0, & ReadStk [0], 7); // Read serial task

OSStart ();


1. Monitoring tasks

Because this system works in a car environment with strong interference, although a variety of hardware anti-jamming measures have been taken, such as adding a shield, reliable grounding, and setting software traps, it is still possible that the system will fall into chaos due to instantaneous disturbances, causing the system to run away. Can only rely on the watchdog reset to run again, so that the design goals cannot be achieved. To this end, the system uses monitoring tasks to supervise whether other tasks are running normally. If a task fails to run normally, corresponding measures are taken to minimize the number of watchdog resets.

The design idea of ​​the monitoring task is: when the monitored task is running normally, its execution time is predictable, and when the monitored task is about to finish running, it sends a message to the monitoring task indicating that it is running normally. When the monitored task is running, the monitoring task is in a waiting state, waiting for the monitored task to send a message to it, and the waiting time is set to the maximum time required for the task to run normally. If the monitoring task receives a message within the waiting time, the task sending the message is considered to be operating normally, and the next task starts to run according to the order of execution of each task, and the monitoring task waits for the message sent by the next task. If the waiting time has passed and the monitoring task has not received a message, the system's time management function will force the monitoring task to be in a ready state. Because the priority of the monitoring task is the highest, it will seize control of the CPU and adopt the corresponding error correction scheme.

2. Key processing tasks

The key processing tasks mainly deal with anti-theft alarms, car snatch alarms, and answering phone buttons. When the task cycle detects that a key is pressed, the key processing task sends the corresponding semaphore to the program that processes the corresponding key.

Traditional solar system includes so many parts: traditional lamp ( or LED lamp), Solar Panel, lead-acid battery, controller, solar panel brackets, battery cabinets. The volume is very large, therefore freight charge is very high, and labor cost of mounting is also very high. But Ruifeng`s Solar Smart integrated solar LED Street Lights solve all the above problems and troubles. Ruifeng has designed and developed a unique [SolarSmart"-all in one Integrated solar street lighting module, Fit and Forget built with intelligent Microcontroller charge controller system, comprising High Efficiency

12W Integrated Solar Street Lights

12W Integrated Solar Street Lights,12W Solar Street Lights,Solar Led Street Light,Solar Street Lamp

Yangzhou Bright Solar Solutions Co., Ltd. ,

This entry was posted in on