In my last post, “How Low (Power) Can You Go?”, we examined several different low power modes that are available in microcontrollers and how those low power modes affect energy consumption and wake-up times. Low power modes are one of the main techniques to decrease the energy that a microcontroller uses, but it’s not the only technique available to developers. In this post, we will examine five of my favorite techniques for lowering microcontroller energy consumption.
Technique #1 – Utilize the DMA Controller
Within a microcontroller, the CPU is typically the power-hungry device that uses the most current. Peripherals certainly contribute to the energy consumption especially if there is a USB or Ethernet controller, but one of the first steps to optimizing a device for energy is to keep the CPU in its lowest state as long as possible. Applications will periodically need to wake-up though to move data around such as receiving bytes from a UART and storing them in a buffer. A way to avoid constantly waking the CPU up to handle moving data around is to utilize the direct memory access (DMA) controller.
DMA allows a developer to keep their CPU asleep and instead use a lower power peripheral to do what the CPU would have done otherwise, move data around the microcontroller. The DMA controller can be used for several purposes such as:
- Moving data from a peripheral to memory
- Moving data from memory into another memory location
- Moving data in memory to a peripheral
The specifics on how to setup DMA will vary from one microcontroller to the next, but the benefit is that the CPU can be kept in a low power state up until it needs to actually wake-up and process the data that has been transferred by DMA.
Technique #2 – Use Clock Throttling
You might recall from a microcontroller course or from reading online that the energy consumption of a microcontroller is dependent upon the clock frequency. The higher the frequency, the higher the energy consumption. An application doesn’t necessarily always need to be running full tilt. In fact, throttling the clock based on what needs to be done can be an effective technique for minimizing the energy that will be consumed.
Take for example Figure 1 below which shows the Run Mode Current versus the Core Frequency in an NXP Kinetis-L microcontroller. From this figure, the reader can see that throttling back the clock frequency will decrease energy consumption. It can also be seen how disabling unused peripherals can make a difference in how much energy the microcontroller consumes as well.
click for larger image
Figure 1 – Microcontroller current consumption as a function of core clock frequency. (Source: NXP Kinetis-L Datasheet)
Exactly when and how to throttle the clock frequency is completely application dependent. Some applications may still want to run full tilt all the time. In others, it may be okay to cut the frequency in half from the maximum and run the part that way. In still others, it may make sense to dynamically change the clock on the fly based on what the application is doing. Just beware that if you decide to use clock throttling dynamically, you may also need to dynamically change peripheral register settings to compensate for the changes.
Technique #3 – Utilize Compiler Optimizations
Modern compilers provide embedded with optimization settings that can improve the execution time for a given piece of code. For example, most compilers provide developers with optimization settings to perform loop unrolling and inline functions. While these are just two examples, developers can use these techniques to generate code that executes faster. Code that executes faster, even if it does use more code space, will give the opportunity to spend more time in a low power mode which in turn will decrease the energy consumed by the application.
I recommend that you schedule some time soon to review your compilers manual and see what features it has built-in to help you execute your code faster. While it may seem like saving a few dozen clock cycles isn’t a big deal. Those clock cycles can add up very quickly over the course of weeks or even months.
Technique #4 – Utilize Low-Power (LP) Peripherals
If you carefully read a microcontrollers datasheet, you’ll notice that microcontrollers designed for low power operations will often include low-power peripherals that are separate from the “standard” peripherals. For example, the STM32 microcontrollers include a LP timer. This is a timer peripheral that doesn’t include all the extra bells and whistles but is designed to be operated in a low-power environment. When optimizing, or even starting a low-power design, watching for these low-power peripherals and utilizing them can be a great way to decrease the products energy consumption without a lot of extra effort.
Technique #5 – Think Outside the Box
Sometimes you may find that you need to really think outside the box to optimize for energy. Several years ago, I was working on a medical product that was battery operated and had to last on the battery for 12 – 14 hours between charges. We selected the battery we needed and then moved on with the design. Well, the industrial designers decided that the battery was too big and without telling the electronics or software team, they decided to shrink the battery considerably so that they could create a smaller, sexier device. The first test of the device revealed that the battery was only lasting about 4 – 5 hours!
The industrial team had of course gone to production tooling and there was no going back to a larger battery. The only option was to optimize like crazy. Despite using every possible optimization technique in the book, I could only get the device to last for about 8 hours. So, what could be done? It turned out that the product had a USB host on board that had a custom memory device connected as an MSD/CDC device. USB uses a lot of energy and if the USB could be shut down when it wasn’t needed, quite a bit of energy could be saved.
We had to think outside the box and after starting the device up, we would disable the USB device, power down the USB host and when the USB device was needed, we bit-banged a message over the USB D+ and D- ports that signals the USB host was going to power back up. It was crazy, ugly, a work-around, but it successfully got the battery life to over 14 hours. Sometimes you just have to think outside the box to get the energy efficiency you need.
In this post we have examined a few additional techniques that developers can leverage to minimize the energy consumption of their microcontroller. Using these techniques along with low-power modes can dramatically lower device energy consumption. As you work on optimizing your microcontroller application for energy, don’t forget to select the techniques that will provide the greatest bang for your time. Energy optimization can often become a black hole where the time investment has diminishing returns with each iteration.
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.