Home |
RTKernel-32 Programming Manual Multitasking, Real-Time, and RTKernel-32 Alternate APIs for RTKernel-32 Preemptive or Cooperative Multitasking? Cyclic Tasks (Timer) Starting Objects' Methods as Tasks Performance and Interrupt Response Times Task Switches in Cooperative Scheduling Using the FPU in Interrupt Handlers |
Cyclic Tasks (Timer)Tasks that must run in a fixed time frame are frequently encountered in real-time applications. The structure of such a task could be as follows: void RTKAPI CyclicTask(void * p) { #define Cycle 5 RTKTime NextActivation; NextActivation = RTKGetTime(); while (1) { NextActivation += Cycle; RTKDelayUntil(NextActivation); /* the task's job is done here */ } } This task would run exactly once every 5 timer ticks, provided the task's job takes no longer than 5 timer ticks. The actual cycle time can be adjusted using constant Cycle and the timer interrupt interval. If the cycle time of a task is not an exact multiple of the timer interrupt interval, the following task can be used: #include <timer.h> void CyclicTask(void) { #define Cycle 0.7 /* seconds */ TISeconds NextActivation; NextActivation = TITicksToSeconds(RTKGetTime()); while (True) { NextActivation += Cycle; RTKDelayUntil(TISecondsToTicks(NextActivation)); /* the task's job is done here */ } } Since RTKernel-32 can only activate tasks waiting in an RTKDelay or RTKDelayUntil through a timer interrupt, these tasks would, on the average, conform to their cycle exactly. The start of a cycle can deviate from the desired point in time up to one timer tick. This round-off error arises from the conversion of a float point number to an integer in function TISecondsToTicks of module Timer. However, the error does not accumulate; it is the same in each cycle. It must be noted that rounding errors can occur when floats are used to store large numbers representing RTKernel-32 times. Unlike the first example, an overflow of RTKernel-32's clock must also be considered (see section Module RTKernel-32, Time).
|