Saturday, January 1, 2011

VxWorks several common delay method

Embedded system, a task often need specific delay after the execution of a specified action such as waiting for peripherals to ensure that data is reliable, control speaker time and serial communication timeout resend.

This requires use of timer mechanism to measure a specific length of time. VnWorks as real-time embedded systems, provides a variety of timing interface function. The following are some common timing, and describes its considerations.

1 taskDelav

TaskDelay (n) to call the function of task delay n tick (kernel clock cycle).

The tasks within the specified time the initiative to give up CPU, besides taskDelay (0) dedicated to the task scheduling (will give the same priority of CPU for other tasks), the task delay also commonly used to wait for an external event, as a scheduled/delay mechanism. No interrupt is triggered, taskDelay makes it easy for you to implement and does not affect the overall performance of the system. For example, write data to EEPROM, EEPROM requires an internal erase time (maximum erase time for lOms). Following a tick mentioned are assumed to be 16.67 ms (1/60 s). You can simply call taskDelay (2) to ensure complete data erased. Arguably taskDelay (1) is sufficient to guarantee, why need taskDelay (2)?

This is a flaw used taskDelay, needs attention.

TaskDelay (n) represents the task delay to the nth system clock arrival time, as shown in Figure 1. If A time called taskDelay (1) only the delayed 5 ms, B moment taskDelay (1) just a tick cycle. Visible needs 10 ms delay must call taskDelay (2). TaskDelay 15 1 tick of error exists, taskDelay (n) actually delay (n-1) tick tick ~ n. Delay precision l/n, delay 1s is taskDelay (60) error limit was 1.6% taskDelay (1) error limit is 100 per cent.

Use taskDelay note another point is: even after n tick, call delay task that returns the execution status, may have higher or the same priority task consumes CPU.

2 WatchDog

VxWorks provides a common watchdog timer mechanism.

Use the supplied function, any task you can create a watch dog timer, after the specified delay time, the implementation of the system clock ISR running in the context of the specified program. Note that the watchdog timer is triggered by the program is executed on the interrupt level, rather than the task's context. Therefore, watch dog timer attached programming has certain limitations, the limitations and constraints of the interrupt service program is the same. For example, you cannot use Gets the semaphore's statement, and like the printf () 's I/O system functions.

By wdCreate () can create a watchdog timer.

Call wdStart () to start the timer, delay parameter to tick as with taskDelay as units, and shall specify the timing after the completion of the program you want to call. If your application also needs more watchdog function, you should use wdCreate () produces multiple independent watchdog ID. Because for a given ID watchdog, only by wdStart () associate a watchdog function. In the tick count is specified, to cancel prior to the arrival of a watchdog timer, you can call wdCancel (). Each call wdStart (), the watchdog timer is only executed once, so for some requirements for periodic execution of applications, to get the effect, then the timer function itself must pass a recursive call wdStart () to restart the timer.

If you use the watchdog timer delay, as existence and taskDelay deficiencies of precision to tick as benchmarks, and watchdog functions associated constraints is large, this is one aspect of using the inconvenience.

However starting watchdog mandate will not be disrupted because wdStart () call returns immediately and continue execution.

3 sleep/nanosleep

Sleep () and VxWorks nanosleep () is provided by the delay function interfaces.

Sleep to s, nanosleep provides more accurate delay; pass parameter is the clock of the structure, parameters can be accurate to ns, but actually can be greater than or equal to this time. Because the skep or nanosleep function delay time base remains a tick, calling this function of the tasks in the task delay State, which is consistent with taskDelay (). Different places that are used for taskDelay () task scheduling, taskDelay (O) has its own meaning, and sleep (O) are meaningless. Previously mentioned, taskDelay (n) the delay time (n-1) tick ~ ntick, sleep/nanosleep you guarantee that the actual delay time is greater than or equal to the time parameter. This is by writing a test program to test. Code is as follows:

  

4 high-accuracy clock sysTimeStamp

SysTimeStamp () also known as the "time stamp".

Is implemented by the system clock. Just starting to think it costsSolutions, the system clock timer cycle is tick, how to achieve high accuracy clock? by reading BSP underlying code found, sysTimeStamp is actually read by the current count value of timer to get high precision timing. By sysTimestampFreq () function to get the system time stamp of frequency, it tends to reflect the frequency of the benchmarks CPU timer. Of course, such a high resolution can only be an ideal value, different systems is not always possible. After all, the time stamp for a fatal weakness: the way through the query. The system clock timer interrupt is ticb: units to further increase the resolution reads timer count value (CPU a special function register), only the query. The code example is as follows:

  

This timing mode consume system resources, and only applies to short time, but for convenience.

To ensure regular and accurate, you should lock the interrupt call sysTimestamp; otherwise, you should consider using sysTimes-tampLock function.

5 secondary clock

Secondary clock is the use of target Board CPU another timer (apart from the system clock) interrupt.

It can be flexibly configured for high resolution timing, and easy to implement ms level even μ s-level timing. VxWorks provides a range of system clock the same operation interface, users can easily hook own interrupt handler, clock resolution height depends on the hardware timer accuracy and the length of a user interrupt functions. To secondary clock as accurate delay mechanism (such as ms-delay), can in this way. Initialize the program first calls SysAuxClkRateSet () function to set a secondary clock interrupt cycle for 1ms (generally contig.h file AUX_CLK_RATE_MIN and AUX_CLK_RATE_MAX, had to interrupt frequency limit, if you need to modify the macro definitions), and then through ysAuxClkConneet () will connect the user handler function secondary clock interrupt, the user handler function can SemGive (semTimer) release a synchronization signal. Write a msDelay (intms) as the other tasks call interface, function code is as follows:

  

This approach enables very precise timing, call delay tasks in a task blocked State.

But there is a bug with still cannot implement multiple tasks at the same time calls, and need a clock of the CPU resources, if there is no extra clock, so this method will not be able to achieve.

Also note that the Tornado of debugging tools Browser a > SpyChart implementation principle is to use auxiliary timer generates interrupts, and records the current task is interrupted, by sampling the data reflect the task CPU utilization.

So if the debugger using the auxiliary timer, then use Spy when scheduled processing function Chart will be back on the hook, the original scheduled hook procedure is not carried out. Conversely, if you run after hook Spy Chart support scheduled processing function, the run will Spy Chart. Experiment found that running Spy Chart after hook auxiliary timer handler, Spy Chart even if you selected the automatic refresh, the task status will not be updated, as shown in Figure 2.

  

VxWorks provides timing interface (not necessarily designed to be used regularly, can also be indirect implementation) is far from just these.

Ways in which, on the basis of their accuracy, resource status and priority of the request.

No comments:

Post a Comment