Advertisement

Decreasing energy consumption using direct memory access

January 23, 2018

Jacob Beningo-January 23, 2018

The energy consumed by an embedded system, especially a battery-operated device such as an IoT sensor node, can be the difference between whether the device successfully fulfills its purpose or falls into obscurity. There are many low power best practices that developers can utilize such as:

  • Using interrupts and avoiding polling
  • Utilizing sleep-on-exit
  • Turning off unused peripherals and clocks
  • Throttling the system clock
  • Employing autonomous peripherals

Perhaps the greatest tool available to embedded software developers is direct memory access (DMA).

DMA provides the embedded developer with an efficient way to transfer data around the microcontroller without the CPU intervening. In resource constrained applications, DMA can allow the CPU to be handling an important task while the DMA is perhaps reading in data bytes from a UART and placing them memory for later processing. For a developer interested in minimizing energy consumption, the CPU could instead be put into a low power mode, allowing DMA to transfer data at a much lower energy level! The CPU is after all one of the most energy hungry devices on a microcontroller.

Let’s consider a simple example, where an IoT sensor node is going to be periodically sampling analog sensors using an analog to digital converter (ADC). There are several different ways that a developer could acquire the sensor data, each which will affect the energy consumed by the system in a different manner. First, an implementation that I still see quite frequently, is to simply poll and wait for the conversion complete flag. This code looks very similar to the code below:

void SampleSensors(void)
{
   for(Sensor = 0; Sensor < MAX_SENSORS; Sensor++)
   {
      ADC_CHANNEL_CONVERT(Sensor);
      while(!ADC_CONVERSION_COMPLETE);
      Sensor_Data[Sensor] = ADC_GET_RESULT(Sensor);
   }
}


This function can certainly be called at a periodic rate and will sample the various analog sensors, but it is incredibly inefficient. The while statement is basically waiting for an ADC conversion complete flag and the CPU is constantly checking to see if it can move on. Even on a little 48 MHz microcontroller, this software architecture can burn a lot of cycles.

The next step up from the polled approach is to use ADC interrupts. The developers kick off an ADC sample using the CPU and then either goes off to execute other code or enters a sleep state while it waits for the ADC to complete. Once the ADC conversion is complete, the interrupt wakes up the microcontroller and the samples are stored in the data buffer.

void AdcInterrupt_handler(void)
{
   Sensor_Data[Sensor] = ADC_GET_RESULTS(Sensor);
}


This approach is far more efficient than polling but to really minimize energy usage, a developer needs to use the DMA.

The approach that I’ve found to be the most energy efficient is to utilize a DMA channel to transfer the resulting ADC conversions into SRAM. First, a developer sets the DMA controller source to the ADC conversion data buffer. The number of bytes to transfer must be set and is often a multiple of two bytes. Next, a developer must can set the address for the SRAM location that stores the ADC sample data for processing. Finally, the DMA can be configured to transfer the data when the ADC conversion flag complete is set (usually when the interrupt fires).

In most instances, a developer can use a low power timer to start the ADC conversion while the CPU is sleeping. With the CPU asleep, the entire process of sensor sampling and data storage is done behind the scenes in a low energy state! The CPU only wakes up when enough data has been entered into the buffer and is ready for processing.


Figure. Using a low-power timer and direct memory access (DMA), developers can perform sensor sampling and data storage while the processor remains in a low-power state. (Source: Author)

The first time that a developer uses a DMA controller, it can seem a bit intimidating. It seems complex, but use of DMA can dramatically improve not just energy efficiency but also data throughput. Any developer interested in developing a low-power device or a battery-operated device should become familiar with their particular MCU’s DMA controller.


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 jacob@beningo.com, at his website www.beningo.com, and sign-up for his monthly Embedded Bytes Newsletter.

 

Loading comments...