What DMA Does and Why It Frees the CPU
Understand what a DMA controller does, how it moves data as a bus master, and why one interrupt per buffer instead of per byte frees the CPU.

Direct memory access, DMA, is a small piece of hardware inside a microcontroller whose only job is to copy data from one address to another. The CPU tells it where to read, where to write and how many units to move, and from then on the CPU does not touch the data at all. This post explains what that means in practice: what the controller does on each bus cycle, why that frees the processor, how the saving can be reasoned about without a stopwatch, and what DMA cannot do. No vendor library is needed to follow it. A tiny simulation in C at the end shows the whole mechanism in thirty lines. If you want working STM32 code next, the memory-to-memory post is the first step, and the DMA guide covers the full picture.
Everything is an address
A microcontroller's CPU talks to memory and peripherals the same way: by reading and writing addresses. The flash, the RAM and every peripheral register sit in one address space. A UART's received byte is a register at some fixed address, and reading that address returns the byte. An ADC result is a register as well. A buffer in RAM is a run of consecutive addresses.
Seen that way, moving a byte from the UART into a buffer is a read from one address and a write to another. A CPU does it with a load instruction followed by a store instruction. A DMA controller does the same read and write, with no program: it holds a source address, a destination address and a count in registers, and steps through them on its own.
That is the whole idea. Everything else, channels, requests, circular mode, is a refinement of "read from here, write to there, this many times".
The controller as a second bus master
Inside the chip, the CPU and the DMA controller both connect to the same bus, with an arbiter deciding who goes on it each cycle. Both are bus masters, units that can start a read or a write. Memories and peripherals are bus slaves, which only respond.
A transfer of one unit is two bus operations:
The controller reads the unit from the source address.
The controller writes it to the destination address.
Then it advances whichever addresses are set to advance and decrements the count. When the count reaches zero it sets a flag and optionally raises an interrupt. While this happens, the CPU is free to run its own instructions, with one catch. The CPU and the controller share the bus, so on any cycle where both want it, one waits. For most programs the cost is small, because the CPU rarely uses the bus on every cycle, since instructions come from a fetch buffer and many run entirely from registers.
The simulation
The following program models a DMA controller in software. It has the registers every real controller has, and a step function that does one unit of work, as the hardware would on one request. It runs two transfers: a memory-to-memory copy with both addresses advancing, and a peripheral-to-memory transfer, where the source address stays fixed because it stands for a data register.
#include <stdio.h>
#include <stdint.h>
typedef struct {
const uint32_t *src;
uint32_t *dst;
uint32_t count;
int src_inc;
int dst_inc;
} dma_t;
static int dma_step(dma_t *d) {
if (d->count == 0) return 0;
*d->dst = *d->src; // one read, one write
if (d->src_inc) d->src++;
if (d->dst_inc) d->dst++;
d->count--;
return 1;
}
int main(void) {
uint32_t a[4] = {10, 20, 30, 40};
uint32_t b[4] = {0, 0, 0, 0};
dma_t copy = { a, b, 4, 1, 1 };
while (dma_step(©)) { }
printf("memory to memory: %u %u %u %u\n", b[0], b[1], b[2], b[3]);
uint32_t adc_dr = 0; // stands for a peripheral data register
uint32_t samples[4] = {0, 0, 0, 0};
dma_t rx = { &adc_dr, samples, 4, 0, 1 };
for (uint32_t t = 0; t < 4; t++) {
adc_dr = 100 + t; // the peripheral produces a new value
dma_step(&rx); // the DMA collects it on its request
}
printf("peripheral to memory: %u %u %u %u\n",
samples[0], samples[1], samples[2], samples[3]);
return 0;
}memory to memory: 10 20 30 40
peripheral to memory: 100 101 102 103Look at what the second transfer shows. The source never advances, because it is one register that holds a new value each time. The destination advances so each sample lands in the next slot. That pairing of increments is why a DMA configuration always asks, for each side, whether the address increments. Forgetting to turn on destination increment in the second case would leave samples[0] holding the last value and the other slots empty, a common first bug.
In the model, dma_step runs in a loop for the copy and once per tick for the peripheral. In real hardware the same distinction exists. A memory-to-memory transfer runs as fast as the bus allows, one unit after another. A peripheral transfer is paced by the peripheral's request signal: the peripheral raises it when it has data, and the controller does one unit per request. The pacing is what keeps the controller from reading a data register that holds nothing new.
Why the CPU is freed
Think about what the CPU would do instead. To read each sample of an ADC without DMA, the CPU needs to learn that a sample is ready, which means polling a flag in a loop or taking an interrupt. Polling spends the CPU waiting. An interrupt makes it stop what it was doing, save its registers, run a handler of a few instructions and restore, for each sample.
With DMA the controller collects the samples. The CPU is only told when a whole buffer is full. So the number of interrupts falls by the buffer length. The total work to move the data is the same hardware activity, a read and a write per unit, but it no longer interrupts anything.
The saving is easy to reason about without measuring cycles, because it is a ratio. Say a UART delivers B bytes per second and the DMA buffer holds N bytes. Interrupt-per-byte handling takes B interrupts per second. DMA takes B / N. This program prints that for a few baud rates, with 10 bits on the wire per byte (start, eight data, stop) and a 256-byte block:
#include <stdio.h>
int main(void) {
const double baud[] = {9600.0, 115200.0, 921600.0, 3000000.0};
const double block = 256.0;
for (int i = 0; i < 4; i++) {
double bytes_per_s = baud[i] / 10.0;
printf("%8.0f baud: %9.0f irq/s per byte, %7.1f irq/s with DMA\n",
baud[i], bytes_per_s, bytes_per_s / block);
}
return 0;
} 9600 baud: 960 irq/s per byte, 3.8 irq/s with DMA
115200 baud: 11520 irq/s per byte, 45.0 irq/s with DMA
921600 baud: 92160 irq/s per byte, 360.0 irq/s with DMA
3000000 baud: 300000 irq/s per byte, 1171.9 irq/s with DMAThe CPU load from interrupts is the interrupt rate multiplied by the cost of one interrupt in cycles, divided by the clock frequency. The cost depends on your core, your handler and your flash wait states, so measure it on your hardware, for instance with the cycle counter. The ratio between the two columns does not depend on the cost, however: it is always N. At 3 Mbaud, the per-byte approach takes 300,000 interrupts a second, which on most small cores leaves little time for anything else, and the DMA approach takes about 1,172.
The remaining work for the CPU
The CPU keeps three jobs. It sets the transfer up, which is a handful of register writes. It handles the end-of-transfer interrupt, or polls the flag. And it deals with the data itself, because DMA moves bytes and does not process them: parsing a received line, filtering samples or building the next buffer all remain CPU work. DMA reduces the cost of transport, not of computation.
It also adds a responsibility that a plain loop does not have: the memory involved belongs to the controller for the duration of the transfer. If the CPU modifies a transmit buffer while the DMA reads it, the wire carries a mixture. If the CPU reads a receive buffer while the DMA writes it, the result is half old and half new. This is why streaming designs split a buffer in two, so the CPU and the controller always work on different halves.
What DMA cannot do
DMA moves data and does nothing else. It cannot compare, convert, filter or decode. It cannot move data between places it has no bus connection to: on many parts some memories, such as tightly coupled RAM, are visible only to the CPU. It cannot start itself on an event the hardware does not route to it; the request source has to be one the controller supports. And the setup costs something, so for a transfer of a few bytes, the CPU's own loop is faster and simpler.
A last limit is worth stating plainly. DMA does not make data arrive faster. If a peripheral delivers 11,520 bytes per second, DMA delivers 11,520 bytes per second into memory. It makes the delivery cheap for the processor, and it makes it more regular, since the controller's response to a request does not wait behind other interrupts.


