Design Article

Use an MCU's low-power modes in foreground/background systems

Miro Samek

9/30/2007 2:13 PM EDT

The proper use of an embedded MCU's low-power/sleep modes is critical to a design's success.

In today's world of battery-operated devices, the proper use of the low-power/sleep modes provided in most embedded microcontrollers (MCUs) is critical. At the same time, most high-volume MCU applications, such as home appliances, vending machines, motor controllers, and electronic toys, are organized as foreground/background systems (super-loops or main + ISRs).

The foreground/background architecture consists of two main parts--the foreground comprises the interrupt service routines (ISRs) that handle asynchronous external events in a timely fashion, and the background is an infinite loop that uses all remaining CPU cycles to perform the less time-critical processing.

The foreground typically communicates with the background through shared memory. The background loop protects this memory from potential corruption by disabling interrupts when accessing the shared variables.

To employ a low-power MCU mode, the background loop must first determine that all external and internal events have been processed so that the CPU clock can be stopped until the next external event (an interrupt) will wake the CPU up. This situation is called the idle condition and is illustrated in Figure 1.

View the full-size image

Because the determination of the idle condition involves testing the variables shared with the foreground (ISRs), the background loop must disable interrupts before detecting the idle condition. Moreover, the idle condition remains valid only as long as interrupts remain disabled. If the interrupts were enabled after the background loop determines that all its work is done for now, but before actually switching to the low-power mode, an interrupt could preempt the background loop at this point and an ISR could produce new work for the background loop, thus invalidating the idle condition.


Next:




Andrew Geard

10/4/2007 3:59 PM EDT

The assumption that changes to the M16C interrupt flag are effective immediately is incorrect. Unless the flag is changed by an RETI instruction the changed status takes effect beginning with the next instruction i.e. it works the same as the AVR.

This information is contained in the M16C Software Manual. It is important that those writing code for the M16C also read through all the technical notes available on the Renesas website. These contain a lot of extremely important information especially about interrupts and power management.

Sign in to Reply



SRambo

10/25/2007 5:10 PM EDT

The following comment was submitted by Eric Weddington, Product Manager, Atmel:

Hello,

I recently saw the article, "Use an MCU's low-power modes..." by Miro Samek, in the October 2007 issue of Embedded Systems Design. I noticed, with interest, that the Atmel AVR microprocessor was reviewed and specifically that example code for the WinAVR toolchain (GNU) was given.

However, I was disappointed to see that the author seemed to know nothing about the APIs that are available from AVR-LibC, the C library for the WinAVR (GNU) toolchain. The example code given in the article was inline assembly statements, which are ultimately not needed. The AVR-LibC documentation can be found online at:

. AVR-LibC provides an Interrupt API, and a Sleep API which provides an excellent example of usage and description :

-------------------
#include
#include

...
cli();
if (some_condition) {
sleep_enable();
sei();
sleep_cpu();
sleep_disable();
}
sei();

"This sequence ensures an atomic test of some_condition with interrupts being disabled. If the condition is met, sleep mode will be prepared, and the SLEEP instruction will be scheduled immediately after an SEI instruction. As the instruction right after the SEI is guaranteed to be executed before an interrupt could trigger, it is sure the device will really be put to sleep."
-------------------

The Sleep API also provides a method to set the desired sleep mode that is portable between AVR devices, as just manipulating the SMCR register is not ideal as some AVR devices use a different register name. With the Sleep API, setting the sleep mode is as simple as writing,

set_sleep_mode(SLEEP_MODE_PWR_SAVE);

Additionaly, AVR-Libc provides a Power API , which provides further means of lowering power. The Power API provides convenience macros that manipulate the AVR Power Reduction Registers (PRR), and other macros that produce inline assembly to manipulate the Clock Prescaler Register (CLKPR) in a way that is portable across AVR devices (where these registers exist), and also that ensures that the correct timed assembly sequence is produced no matter what the optimization setting.

An article that just skims the surface of the issue of low-power settings in micros does not do justice to what hardware and software features are truly available to the end-user.

Eric Weddington
Product Manager, Atmel
Creator of WinAVR

Sign in to Reply



fudge

1/24/2008 8:55 AM EST

wide coverage and nice examples, i liked the article very much.Arm sleep mode is explained purty good.

cheers

Sign in to Reply



Please sign in to post comment

Navigate to related information

Datasheets.com Parts Search

185 million searchable parts
(please enter a part number or hit search to begin)

Feedback Form