Reduce resource usage of an Idle task
I find that in uC/OS-II RTOS开发者_运维百科, there is an idle task that gets executed when no other task is ready to run. If an idle task can consume resources, how can we reduce it ?
Usually the idle task is where the processor is put into low power (sleep) mode, if it's a low-power system and the processor has such a mode. This is usually a specific assembly instruction, for example on the ARM Cortex M3 you'd execute the "WFI" instruction. On other chips, there might be a specific register outside the core that manages power (as opposed to an instruction).
Note that there are often conditions (requirements that must be met) before entering low power mode. Sometimes you need to have interrupts locked, sometimes unlocked, before going to sleep; check your chip's datasheet.
Usually before entering low-power mode, you'll power down any peripherals you don't need. Again, check your chip's datasheet. Also if you're going to use an interrupt to wake back up, make sure that the peripheral isn't powered down, and that the interrupt is enabled, otherwise, you won't wake up.
Last point: often when debugging (e.g. under control of a JTAG device), weird things happen when entering low power mode, so you want to disable "sleeping" in the idle task when debugging, and only do this when running without the debugger. Usually this is a compile-time decision (#ifdef ...)
A typical idle loop is likely to exercise very few processor gates and therefore consume very little power in the core, but if it is critical you can invoke a sleep mode in the idle loop, so that no code at all is executed - the core just stops. However it is likely that to maintain real-time response peripheral devices will remain powered and able to generate interrupts, so in practice the benefit may be minimal.
In terms of other resources the idle loop will typically be simply a branch to the current instruction; one instruction, no data; it cannot get much smaller than that. The source code for uC/OS-II is provided, so you could just take a look!
Of course if you have extended the idle task by modifying it directly or implementing a hook, that resource usage is entirely within your control, but you cannot get something for nothing.
精彩评论