Offloading ADC Data with DMA in Embedded C: A Practical Guide
Learn how to use DMA in Embedded C to move ADC data without CPU load, see a concrete STM32F4 example, and avoid common pitfalls.
04 Apr 2026, 19:11 UTC

Why the CPU Gets Stuck Moving ADC Samples
In many embedded applications the analog‑to‑digital converter (ADC) produces a steady stream of samples. If the CPU copies each sample from the ADC data register into a software buffer, the processor spends a large fraction of its time on simple memory moves. This wastes cycles, raises power consumption, and can miss real‑time deadlines. The practical takeaway is that Direct Memory Access (DMA) lets the ADC write directly into a buffer, freeing the core for computation or other peripherals.
What DMA Actually Does
DMA is a hardware subsystem that can take control of a bus transaction and move data between a peripheral and memory without involving the CPU. The peripheral drives the address bus, the DMA controller handles the timing, and the memory controller writes the data into the designated buffer. Because the transfer runs in parallel with CPU execution, latency drops and the processor stays available for other tasks.
Most modern MCUs – for example the STM32F4 series – integrate a DMA controller with several independent channels. Each channel can be linked to a specific peripheral (ADC, UART, SPI, etc.) and to a memory region. The configuration is done in C using the vendor’s HAL or standard peripheral library, and the transfer is started by setting a few registers and enabling the channel.
Concrete STM32F4 Example
The following code shows a minimal DMA setup to transfer a 256‑byte buffer from ADC1 channel 0 into memory using DMA1 Channel 1.
/* Include required headers */
#include "stm32f4xx_hal.h"
/* DMA handle */
DMA_HandleTypeDef hdma_adc1_channel1;
/* Buffer that will receive the ADC data */
uint32_t adc_buffer[256];
/* ADC handle (assumed already configured elsewhere) */
ADC_HandleTypeDef hadc1;
/* DMA initialization function */
void MX_DMA_Init(void)
{
/* Enable DMA clock – RCC configuration depends on the device */
__HAL_RCC_DMA1_CLK_ENABLE();
/* Configure DMA1 Channel 1 */
hdma_adc1_channel1.Instance = DMA1_Stream0; /* Stream0 is Channel 1 on STM32F4 */
hdma_adc1_channel1.Init.Channel = DMA_CHANNEL_0; /* ADC1_IN1 channel mapping */
hdma_adc1_channel1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc1_channel1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc1_channel1.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc1_channel1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_adc1_channel1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_adc1_channel1.Init.Mode = DMA_NORMAL; /* Stop after one transfer */
hdma_adc1_channel1.Init.Priority = DMA_PRIORITY_HIGH;
hdma_adc1_channel1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
/* Link the DMA handle to the ADC peripheral */
__HAL_LINKDMA(&hadc1, DMA_HandleAttr, hdma_adc1_channel1);
/* NVIC configuration – enable interrupt and set priority */
HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
/* Initialise the DMA */
HAL_DMA_Init(&hdma_adc1_channel1);
}
/* Start a transfer of 256 words (512 bytes) */
void Start_Adc_Dma_Transfer(void)
{
/* Ensure the ADC is ready */
HAL_ADC_Start(&hadc1);
/* Configure the DMA transfer size and addresses */
hdma_adc1_channel1.Instance->M0AR = (uint32_t)&ADC1->DR; /* peripheral address */
hdma_adc1_channel1.Instance->M0BR = (uint32_t)adc_buffer; /* memory address */
hdma_adc1_channel1.Instance->NDTR = 256; /* transfer count */
/* Enable the transfer */
HAL_DMA_Start(&hdma_adc1_channel1);
}
/* DMA interrupt handler – called when transfer completes */
void DMA1_Stream0_IRQHandler(void)
{
if (__HAL_DMA_GET_FLAG(&hdma_adc1_channel1, DMA_FLAG_TCIF0_4)) {
HAL_DMA_Stop(&hdma_adc1_channel1);
HAL_NVIC_DisableIRQ(DMA1_Stream0_IRQn);
/* Transfer complete flag can be cleared here if needed */
}
}
Where to run the code: place the MX_DMA_Init call early in main() (after HAL initialization) and invoke Start_Adc_Dma_Transfer when you want the ADC to fill the buffer. The example assumes the HAL library is already set up and that the ADC peripheral clock is enabled.
Required permissions: the DMA clock must be enabled via the RCC register; the NVIC priority must be set high enough to pre‑empt other interrupts but not so high that it starves the system. The code uses DMA_PRIORITY_HIGH as a safe default.
Expected checks: after HAL_DMA_Start the DMA interrupt flag DMA_FLAG_TCIF0_4 (transfer‑complete) should become set. You can verify this by reading the status register or by attaching a debugger and watching the DMA1->ISR register.
Risks: mis‑aligning the peripheral or memory addresses, using a channel that is not dedicated to the ADC, or forgetting to enable the DMA clock can cause the peripheral to lock up or write to unintended memory locations. Always consult the STM32F4 reference manual for the exact channel‑to‑peripheral mapping.
Trade‑offs and Limitations
While DMA dramatically reduces CPU load, it introduces a few practical constraints:
- Alignment requirements: the peripheral and memory addresses must be word‑aligned for the chosen data width. Mis‑alignment can lead to bus faults on some MCUs.
- Interrupt handling: the DMA completion interrupt must be serviced promptly. If the interrupt priority is too low, the flag may stay set, causing the transfer to appear stuck.
- Data consistency: the CPU must not modify the DMA buffer while the transfer is active, otherwise the data may become corrupted. In multi‑threaded or RTOS environments, protect the buffer with a critical section or use a double‑buffer scheme.
- Memory region accessibility: the DMA controller can only access memory that is not protected by the MPU (Memory Protection Unit). If the buffer resides in a non‑coherent region, the transferred data may not be visible to the CPU until the transfer finishes.
These trade‑offs are acceptable when the data rate exceeds a few kilobytes per second or when the CPU must stay free for real‑time tasks. For low‑rate sensors, polling the ADC register may be simpler and avoid the extra code complexity.
How to Verify the Transfer Works
1. Compile and flash: use the vendor’s HAL or standard peripheral library, then program the target board. No special permissions beyond those described above are needed.
2. Monitor the DMA flag: after Start_Adc_Dma_Transfer, read DMA1->ISR & DMA_FLAG_TCIF0_4. The flag should be set within a few microseconds of the transfer start. If it never sets, check the channel configuration and the ADC start command.
3. Visual verification: a logic analyzer or on‑chip debugger can capture the address and data buses. Confirm that the ADC data appears on the bus and that the DMA controller drives the correct memory addresses.
4. Functional check: after the transfer completes, inspect adc_buffer[0] through adc_buffer[255] in the debugger. The values should match what the ADC read during the transfer.
These steps give you confidence that the DMA is correctly offloading the ADC without unintended side effects.
Closing Action
If your application moves more than a few kilobytes per second from a peripheral, enable DMA to keep the CPU free for real‑time processing. Start by configuring a single DMA channel for the most demanding peripheral, verify the flag, and then expand to additional channels as needed. Remember to respect alignment, interrupt priority, and memory protection rules to avoid silent failures.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.