...
The important portions of the code for the idle task are shown below (refer to os_core.c
for the complete code). These portions are still executed even if the idle task is disabled in os_cfg.h
.
Anchor | ||||
---|---|---|---|---|
|
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
void OS_IdleTask (void *p_arg)
{
while (DEF_ON) { (1)
CPU_CRITICAL_ENTER();
OSIdleTaskCtr++; (2)
OSStatTaskCtr++;
CPU_CRITICAL_EXIT();
OSIdleTaskHook(); (3)
}
} |
Panel | ||
---|---|---|
| ||
(1) The idle task is a “true” infinite loop that never calls functions to “wait for an event”. This is because, on most processors, when there is “nothing to do,” the processor still executes instructions. When µC/OS-III determines that there is no other higher-priority task to run, µC/OS-III “parks” the CPU in the idle task. However, instead of having an empty “infinite loop” doing nothing, µC/OS-III uses this “idle” time to do something useful. (2) Two counters are incremented whenever the idle task runs.
(3) Every time through the loop,
Typically, most processors exit low-power mode when an interrupt occurs. Depending on the processor, however, the Interrupt Service Routine (ISR) may have to write to “special” registers to return the CPU to its full or desired speed. If the ISR wakes up a high-priority task (every task is higher in priority than the idle task) then the ISR will not immediately return to the interrupted idle task, but instead switch to the higher-priority task. When the higher-priority task completes its work and waits for its event to occur, µC/OS-III causes a context switch to return to |