Whether an embedded system runs on batteries or is connected to the grid, designing a system that is green and minimizes energy consumption is a common requirement for most systems. One of the biggest energy consumers in a modern embedded system is the microcontroller and understanding how to minimize its energy consumption is critical. Every processor architecture is going to have slightly different low power capabilities, even when you look at a single microcontroller family. In this post, we are going to look at the fundamental low power modes in the Arm Cortex-M processor core and how you can use them.
The fundamental low energy modes
Every microcontroller that is based on the Arm Cortex-M processor will have at least three power modes; Run, Sleep and Deep Sleep. Obviously, the run mode is where the processor is fully powered up and doing everything that the embedded system was designed to do. Sleep mode will halt the CPU clock but will leave the system clock, flash and peripheral clocks operational. Deep sleep mode will not just halt the CPU clock but also turn off the system clock, flash and the PLL.
The microcontroller vendor has the ability to fully customize their low power modules to offer varying degrees of low power. For example, the NXP Kinetis-L processors don’t just have the standard Cortex-M low power modes but also modes such as Low-Leakage sleep mode and Very Low Leakage Sleep mode which draw just micro or nanoamps of current.
It’s important to note that the deeper into sleep the microcontroller goes, the closer it is to being fully turned off. This is important to realize because as you use deeper and deeper sleep modes, the time required to power the microcontroller back up and start executing instructions can increase dramatically. Some processors that offer very deep power modes require the same amount of time as the processor boot sequence to become operational again. Depending on your application, this could have significant effects on the real-time performance of the system.
Entering low power mode
There are two different instructions that a developer can use to put their system into a low power state; Wait-For-Event (WFE) and Wait-For-Interrupt (WFI). When the WFE instruction is executed, it checks the value of the event bit in the System Control Block (SCB). If the bit is not set, the system will then be placed into sleep mode. Events can be generated by external sources or by using the Send Event (SEV) instruction. For the most part, 99% of the application I’ve seen or written have used the WFI instruction.
When the WFI instruction is encountered, the processor will immediately move into the low power sleep mode that is configured in the SCB unless there is a pending interrupt. Once the processor is asleep, it will wake-up when an interrupt is triggered. Some processors may even have an optional Wakeup-Interrupt-Controller which allows a developer to configure what sources are allowed to wake up the processor. This provides a developer with finer grain control over how long their system can remain in the lowest power sleep mode.
The C code to enter the desired sleep mode is very straight forward. All one needs to do in most development environments is write the following line of code:
If you are using CMSIS, you can also just make a call to the WFI function by writing:
Now you may be wondering how you can configure which low power mode you enter. Within the SCB register there is a SLEEPDEEP bit that if set to 1 will put the processor into a deep sleep mode. If the bit is cleared to 0, the processor will go into the sleep mode. As I mentioned earlier, the sleep modes are often enhanced by the silicon vendor so there will also be additional registers that need to be configured based on who designed your microcontroller.
Additional low power capabilities
In addition to being able to enter into a low power mode, there is also another interesting low power mode feature called Sleep-On-Exit. Sleep-On-Exit allows the processor to immediately go back to sleep once the interrupt that was executing completes. This allows a developer to save time, and time is energy in this case, by avoiding unnecessary context switching between the main application and the interrupt. The processor is allowed to wake-up in the interrupt, do the necessary work and then return to sleep.
The low power modes on the Cortex-M processors are one of the major keys to decreasing how much energy consumption an embedded system uses. As we have seen, developers can use the WFI instruction to place their system into their low power mode and the system will then wake-up once the previously configured wake-up event or interrupt has been triggered. Each microcontroller vendor is able to customize their power modes and we will see microcontrollers with as few as the three default power modes all the way through parts with a dozen power modes and complex state machines that facilitate how the processor moves from one mode to the next.
Jacob Beningo is an embedded software consultant, advisor and educator who currently works with clients in more than a dozen countries to dramatically transform their software, systems and processes. Feel free to contact him at firstname.lastname@example.org, at his website www.beningo.com, and sign-up for his monthly Embedded Bytes Newsletter.