By: Lotta Frimanson, IAR Systems
The basic mechanisms for power management are already in the RTOS. The lowest-priority task can put the CPU into a selected sleep mode to be reawakened by any interrupt in the role of Prince Charming.
Low power consumption is an essential requirement in many embedded applications. The growing importance of battery-powered applications for portable electronics, home automation and wireless communications—for example, in medical devices, personal navigation devices, sports watches and portable game consoles—means that the demand for improved battery life is growing dramatically. The number of low-power versions of microprocessors and other silicon devices that have power management capabilities is increasing steadily.
Many embedded applications spend most of their time waiting for something to happen: receiving data on a serial port, watching an I/O pin change state, or waiting for a time delay to expire. If the processor is still running at full speed when it is idle, battery life is being consumed while very little is being accomplished. So in many applications the microprocessor is only active during a very small amount of the total time, and by placing it in a low-power mode during the idle time, the battery life can be extended by orders of magnitude.
However, incorporating power management and using an RTOS may be just as important a way to save power as the silicon design itself. Test examples have shown that some microprocessors may be in idle mode for 60 - 80% of their operating time. Using an RTOS in combination with idle tasks may significantly reduce power consumption and the CPU overhead by ensuring that the CPU is in its lowest possible power mode whenever it is idle.
Reducing Power Consumption in an Embedded System
The key is to minimize the active time and therefore maximize the time when the processor can go to sleep. The active time can be reduced by using a compiler that is efficient at optimizing for speed. The faster a specific task can be executed, the less time the processor needs to run.
A further important consideration is to structure the code in a way that identifies when the system is not performing any tasks. A good approach is to have a task-oriented design and to use an RTOS: In a task-oriented design, a task can be defined with the lowest priority, and will then only run when there is no other task needing to run. We call this task the idle task, and it is the perfect place to implement power management. In practice, every time the idle task is activated, it puts the processor into a low-power mode.
An RTOS provides the developer with a framework on which to build and organize the features of the system. The toolbox that accompanies the RTOS should also provide services such as inter-task communication and time management. Even for systems that have no need of the real-time capability, the code can be cleaner and better organized if based on an RTOS, partly because it promotes code reuse. The integration of an RTOS solves a variety of problems that can occur in application code, since it provides multitasking capability and allows the application to be broken down into smaller pieces.
Each task is assigned its own priority based on its importance, and preemptive scheduling ensures that the microcontroller (MCU) runs the task that has the highest priority among those that are ready to run. In most cases, adding a lower-priority task will not affect the responsiveness of the system to high-priority tasks.
There are many advantages to implementing power management within the OS, as it allows developers to concentrate purely on application development. The application developers can then easily use this object-oriented approach to make the embedded system more power efficient and easier to maintain, and development times become faster.
Using Low-Power Modes
The use of low-power modes is also very beneficial. A low-power mode is simply a state that a microprocessor can be switched to in order to save power. Many microprocessors and other silicon devices have a number of different low-power modes, in which different parts of the processor can be turned off when they are not needed. The oscillator can be either turned off or switched to a lower frequency, peripheral units and timers can be turned off, and the CPU stops executing instructions. Several processors have different levels of low-power modes, each with a different power consumption based on which peripherals are left on.
The wake-up time from a low-power mode does of course differ depending on which microprocessor it is, and it also depends also on which parts of the processor have been turned off. For example, the MSP430 device in the 5xx family has a wake-up time from standby mode of less than 5 µs. MSP430X5xx devices support several power modes, so instead of all clocks being active the following modes may be applied:
Low-power mode 1 (LPM1)
• CPU is disabled.
• ACLK and SMCLK remain active. MCLK is disabled.
• DCO’s dc-generator is disabled if DCO not used in active mode.
Low-power mode 2 (LPM2)
• CPU is disabled.
• MCLK and SMCLK are disabled.
• DCO’s dc-generator remains enabled.
• ACLK remains active.
Low-power mode 3 (LPM3)
• CPU is disabled.
• MCLK and SMCLK are disabled.
• DCO’s dc-generator is disabled.
• ACLK remains active.
The low-power modes shown above are configured by means of the CPUOFF, OSCOFF, SCG0 and SCG1 bits in the status register. The advantage of including these particular mode-control bits in the status register is that the current operating mode is saved onto the stack during an interrupt service routine. The program flow returns to the previous operating mode if the saved SR value is not altered during the interrupt service routine.
Program flow can be returned to a different operating mode by manipulating the saved SR value on the stack within the interrupt service routine, and the mode-control bits and the stack can be accessed within any instruction. When setting any of the mode-control bits, the selected operating mode takes effect immediately. Peripherals operating with any disabled clock are themselves disabled until the clock becomes active again. The peripherals may also be disabled by means of their individual control register settings.
Example of Implementing Power Save
The perfect place to implement power management is in the idle task, since this is the code that will be executed as soon as there are no other tasks to perform. Depending on which resources are used, the idle task is an ideal means to trigger the CPU to enter the lowest possible power mode. Figures 1 and 2 show an example of implementing the power save functionality in the idle function, where we switch off the peripherals that are not used and then enter one of the low-power modes. It is noted that the RTOS always brings the CPU into lowest power mode possible whenever the CPU is idle and peripheral clock requirements are taken into account.
IAR PowerPac for MSP430 provides support for power management; its RTOS uses power management counters to keep track of the usage of shared resourced within the system. The following power management functions are provided:
OS_POWER_GetMask — Returns a mask that shows which power management counters are set.
OS_POWER_UsageDec — Decrements power management counters.
OS_POWER_UsageInc—- Increments power management counters.
All tasks that are using shared hardware resources need to make operating systems calls to let the system know when shared resources are or are not in use.
In the hardware initialization example in Figure 1, the function needs to use OS_POWER_UsageInc to indicate that the Sub-System Master Clock (SMCLK) is used by the system.
The idle task will use the function OS_POWER_GetMask to get information about which power management counters are set, as shown in Figure 2. This will make it possible for the idle task to find out which low-power mode it can enter. All peripheral units with a counter value of zero can be switched off. On an MSP430 processor, entering a low-power mode is done by a simple write to the status register.
The processor will now stay in the low-power mode until one of the tasks is ready to execute. Whenever there is a task that needs to be done, or the application needs to respond to an event, the system will be woken up through any enabled interrupt.
With only a very small amount of additional code, it is possible for an application to make significant improvements when it comes to saving power. Additionally, there is no need to increase the complexity of the system to implement power management, since the RTOS manages it with virtually no overhead. Using the RTOS to manage power in this way will ensure that the processor spends as much time as possible in the lowest possible low-power mode.
So what exactly do you as a developer need to consider in order to minimize the power consumption? Firstly, you need to select a microprocessor that can run at a low-power level, and which supports low-power modes. And you should select a professional build chain with a highly optimized compiler. Then it is necessary to use an efficient RTOS with a small footprint and optimized task switching. And finally, you should implement the power management functionality, and thus make the idle task responsible for bringing the processor into a low-power mode.
Foster City, CA.
© 2009 RTC Group, Inc., 905 Calle Amanecer, Suite 250, San Clemente, CA 92673