Friday, May 28, 2010

Running Timer's of uCOS-II to Measure Cycle Average Execution Time

Software Timers

Embedded applications need to schedule future events. This can be accomplished using timers and timers' services.

Timers are an integral part of any real-time system. A timer can be seen as an event scheduling according to a predefined time value in the future, exactly like setting an alarm clock.

Embedded systems that have time-sensitive activities use 2 types of timers; hard-timers and soft-timers. Hard timers are based on physical timers on the chip that directly interrupt the CPU. A hard-timer is a must in case of an application demanding high precision and predication. On the other hand soft-timers are scheduled through a software facility that enables soft-timer's maintenance, installation, and removal.

But if hard-timers are more accurate than their soft counterparts, why are they used? The answer is in their nature. The can be programmed with timeouts of course granularity. In addition, the high-precision is not always needed in most of embedded systems. Another prevailing reason is reducing system interrupt overhead. Facilities of soft-timers are built using only 1 hardware-timer.

Software Timers in uCOS-II

Starting from uCOS-II V2.8x, uCOS-II added the soft-timers feature. uCOS-II timers have 2 modes of operations; periodic and 1-shot modes. Whenever a timer timeouts, a callback function is executed. This service is implemented as a uCOS-II task. This task is assigned a stack size and priority defined the macros OS_TASK_TMR_STK_SIZE and OS_TASK_TMR_PRIO respectively. This task is signaled from the tick ISR at a constant rate defined by the macro OS_TMR_CFG_TICKS_PER_SEC. Whenever signaled, it updates the soft timers created by the user and make calls to callback functions if they expire. The design structure used for timers implementation is wheel spoke. Like any other uCOS-II service, this service is highly configurable. In order to use it you have to:

1. Define OS_TMR_EN to a value greater than zero


3. Define frequency of timers update through OS_TMR_CFG_TICKS_PER_SEC

4. Enable hooks

5. Modify OSTimeTickHook to signals the timers manager task as explained here

6. Use any of timers management APIs in your application

Measuring Execution Time

To test the execution time of a cyclic task, you should use the OSTimeGet API at its start and end to measure the execution time. You should accumulate the total execution time and the number of cycles executed in order to calculate average cycle execution time. The soft-timer will be used to calculate this average at periodic rates. A code example is can be found here.

Fig 1 shows the result of running that code.

Figure 1: Timer Measuring Execution Time