9 » IC Electronic information » Category: C

IC Electronic information

C / OSII advantages and disadvantages of the software timer and Improvement

In Electronic Infomation Category: C | on April 23,2011

C / OSII a small, stable, open source and D5C060-55 datasheet and many other advantages, and D5C060-55 price and C / OSII most of the ANSI C language using the system very easy to transplant. In C / OSII I2.81 and D5C060-55 suppliers and later [2], added to the software timer support, the C / OSII operating system more perfect.

C / OSII is a preemptive priority-based operating system, real-time strong. The system software timer is not priority, order of implementation of the callback function, thus reducing the systems real-time. Therefore, this paper improved the software timer, the timer to add priority by priority the implementation of the callback function to improve the system in real time.

1 introduces the software timer

C / OSII system time delay and task management features including software timers, and the software timer is the main role is to perform the function of the regular periodic or one-time use of the software timer control block and the "Timer round of "management software timer. Timer control block structure as the task control block, create a timer, the timer control block from the free list to get a free control block, and its assignment.

Software timer also needs a beat-driven clock, and this driver is generally implemented in hardware, general use of C / OSII operating system, beat the clock delay in the mission to drive the software timer. Each tick OSTmrCtr (global variables, the initial value of 0) by 1, when the OSTmrCtr value equal to the OS_TICKS_PER_SEC / OS_TMR_CFG_TICKS_PER_SEC (This software is both to decide the frequency of the timer), the calling function OSTmrSignal (), this function Send semaphore OSTmrSemSignal (initial value 0, the decision task OSTmr_Task scanning software running timer). In other words, the handling of the timer tick interrupt function is not carried out, but the amount of signal to activate the task OSTmr_Task (high priority). The timer task OSTmr_Task detection processing, including timer time judging completed, the callback function is executed.

C / OSII 2.86 associated with the timer function of the software include:

static functions within the software timer. Timer control block to obtain and release function OSTmr_Alloc (), OSTmr_Free (); Dingshi Qi into the appropriate "time wheel" group function OSTmr_Link (); from the corresponding "time wheel" group to remove the timer function OSTmr_Unlink (); Software Timer task initialization function OSTmr_InitTask (); Ding Shiqi scanning task OSTmr_Task; Ding Shiqi lock and unlock function OSTmr_Lock () and OSTmr_Unlock () (in C / OSII 2.91, this function is scheduling two locking and unlocking functions instead) .

timer external interface functions. Ding Shiqi create and delete functions OSTmrCreate (), OSTmrDel (); timer start and stop function OSTmrStart (), OSTmrStop (); Ding Shiqi remaining time with the current status query function OSTmrRemainGet (), OSTmrStateGet (); software first timer the beginning of OSTmr_Init (); send semaphore OSTmrSemSignal function OSTmrSignal (); timer name query Function OSTmrNameGet ().

Due to a software implementation of the timer callback function is executed in the task OSTmr_Task, multiple timers at the same time if done regularly, then the timer task to execute multiple timer callback function, so the task Ding Shiqi the execution time of uncertainty. And the timer callback function is executed in order, if a timer callback function needs as soon as possible to achieve precise timing would be difficult to achieve. Because each timer is not priority, so the impact of the system in real time.

2 improvements to the software timer

Software timer callback function to enhance the implementation of real-time, for each timer is given a priority. When the timer is complete, and the timer callback function is not empty, put the priority of the timer is ready to write the software timer table. Task OSTmr_Task the corresponding "time wheel" After the inspection, if the scanning software timers in place of each table before the timer to zero and the scan is not zero, then the amount of activation of a callback function to send the signal the task OSTmr_TaskCallback. In this task, software timer callback function according to the priority of the table ready to perform the appropriate callback function, thus improving the system in real time.

2.1 of the software timer-related data structures to improve

defined structure OS_TMR_CALL, store the timer callback function, the function of the parameters, the timer pointer, in the form below:

Typedefstructos_tmr_call {

OS_TMR_CALLBACKOSTmrCallback; / * callback function * /

Void * OSTmrCallbackArg; / * callback function pointer * /

OS_TMR * OSTmr; / * timer pointer * /


Ucos_ii.h in the header file, define OSTmrCallbackTbl [OS_TMR_CFG_MAX], OS_TMR_CFG_MAX configured in the system that the number of software timers.

timer control block in the software to add a member variable OSTmrPrio (timer interrupt priority), deleting the variable OSTmrCallback (callback function), OSTmrCallbackArg (callback function parameters), in order to test the convenience, it can be temporarily removed two variables.

defined timer ready table:


INT8UOSTmrRdyTbl [OS_TMR_CFG_MAX / 8 + 1];

Time when the timer is complete, the timer is ready to write the priority list, according to the priority task of the callback function, the implementation of the callback function.

defined semaphore OSTmrSemCallback (initial value 0), when the time is completed, the amount of sending this signal to activate the task callback function to execute a callback function.

2.2 timer related functions and software functions and tasks to improve

2.2.1 software timer creation function OSTmrCreate

Created by adding the parameters of the function OSTmrCreate priority parameter prio. Create a function called when the timer control block in the member variable assignment, and corresponding to the callback function array cell assignment, the following form:

OSTmrCallbackTbl [prio]. OSTmrCallback = callback;

OSTmrCallbackTbl [prio]. OSTmrCallbackArg = callback_arg;

OSTmrCallbackTbl [prio]. OSTmr = ptmr;

2.2.2 Improvement of the timer task OSTmr_Task

Timer for when the time to complete, the timer interrupt priority to write software timers in place in the table, and according to the values ??before and after the table ready to send signals to determine the amount of time OSTmrSemSignal, to activate the callback function tasks. Task OSTmr_Task process shown in Figure 1.

Figure 1 OSTmr_Task process

The timer into the timer ready priority code table is as follows:

If (OSTmrTime == ptmr > OSTmrMatch) {

Prio = ptmr > OSTmrPrio;

Pfnct = OSTmrCall [prio]. OSTmrCallback;

If (pfnct! = (OS_TMR_CALLBACK) 0) {/ * ready to join the timer callback function table * /

OSTmrRdyGrp | = (INT8U) (1 "(INT8U) (prio" 0x03 ));

OSTmrRdyTbl [prio>> 0x03] | = (INT8U) (1 "(INT8U) (prio & 0x07 ));



2.2.3 to stop the timer function OSTmrStop () changes

Function OSTmrStop simply modify and implement the relevant part of the callback function can, for example, case OS_TMR_OPT_CALLBACK_ARG: part of the code is as follows:


Prio = ptmr > OSTmrPrio;

Pfnct = OSTmrCall [prio]. OSTmrCallback;

If (pfnct! = (OS_TMR_CALLBACK) 0) {

... ... / * Prio timer is ready to join the table * /

OSTmrCall [prio]. OSTmrCallbackArg = (void *) callback_arg;

OSSemPost (OSTmrSemCallback); / * send a callback function to perform a semaphore * /

} Else {



The case OS_TMR_OPT_CALLBACK: part of the code above, only the parameters of the callback function does not need to re-assignment.

2.2.4 callback task OSTmr_TaskCallback ()

Tmr.c in the source file callback function to add the task OSTmr_TaskCallback (), according to Ding Shiqi ready implementation of the priority table, the appropriate callback function, the structure of the task callback function is as follows:

Static voidOSTmr_TaskCallback (void * p_arg) {... ... / * variable definitions * /

For (;;){// request semaphore OSTmrSemCallback

OSSemPend (OSTmrSemCallback, 0, & err);

OSTmr_Lock ();/* timer lock * /

While (OSTmrRdyGrp) {

... ... / * Get the table from the timer in place the highest priority of the timer callback function * /

... ... / * Delete the possession of the table ready bit * /

OSTmr_Unlock (); / * Ding Shi Qi locked * /

Pfnct = OSTmrCall [prio]. OSTmrCallback;

(* Pfnct) ((void *) (OSTmrCall [prio]. OSTmr), OSTmrCall [prio]. OSTmrCallbackArg); / * execute a callback function * /

OSTmr_Lock (); / * timer lock * /


OSTmr_Unlock ();/* timer unlock * /



We can see from the above code, ready access to the timer table lock, the execution of the timer callback function is unlocked. If the callback function performs a long time, beat the arrival of the next software timer, the timer can be scanned and timely implementation of the task, after the completion of the current callback function can be ready in time have to implement the highest priority in the table back to the timer transfer function. It can be seen, high-priority timer callback function is the timely implementation of the systems real-time increase.

Experimental test showed that the task OSTmr_TaskCallback in the callback function, use the task scheduler lock and unlock the lock and unlock than using the timer (ie, the request semaphore) faster execution speed. After all, the priority task of the callback function is high (usually after the task OSTmr_Task Ding Shiqi priority scan), so use the task scheduler lock lock a little better than Ding Shiqi. Of course, you can also use the switch to interrupt the way of ready access to the table, you can choose according to which way the actual situation.

3 experimental test

This experiment using the software development environment, IAR 5.30, kernel-based CortexM3 Land Rover LPC1768 development board as a hardware test platform [6], real-time operating system C / OSII 2.86 for improvement.

The improved operating system for testing, in the main function to create a startup task, the task starts to create four periodic timer (system "time wheel" set number 4), given different priorities and timing value of each timer to control a LED blinking, start the four timers. In the start function to create four tasks, each task is control of a flashing LED lights (using the task delay), then start the task to hang. Using C / OSII CSPY plug observed the operation of the timer shown in Figure 2.

Figure 2 software timer running interface

The experimental test, the system is operating normally, the timer callback function to receive timely implementation, the system has been greatly improved real-time.

4 Conclusion

Improved software timers, the timer task to determine the execution time, only to be completed while the timer time related to the number of timers in the ready list by priority the implementation of the callback function, so that high priority Ding Shiqi The timely implementation of the callback function, to improve the system in real time.

D5C060-55 datasheetD5C060-55 suppliersD5C060-55 Price

All right © 2010-2016 Certificate