5.5.8Embedded Systems & Real-Time Software

DMA — memory-to-memory, peripheral-to-memory without CPU

2,216 words10 min readdifficulty · medium2 backlinks

WHAT is DMA?

The three canonical transfer directions:

Direction Source Destination Typical use
Mem-to-Mem RAM RAM memcpy acceleration, framebuffer copy
Periph-to-Mem UART/ADC/SPI data register RAM buffer logging sensor samples
Mem-to-Periph RAM buffer DAC/SPI/UART data register streaming audio out

WHY does DMA exist? (the core motivation)

The deep win is parallelism: while DMA owns the bus moving data, the CPU runs from cache/instruction memory.


HOW does DMA work? (derive the timing from first principles)

A transfer is fully described by 4 things you must set:

  1. Source address SS
  2. Destination address DD
  3. Count NN (number of items)
  4. Item width ww (bytes per item: 1, 2, or 4)

Total bytes moved: B=NwB = N \cdot w.

Deriving transfer time

Each DMA "beat" moves one item and consumes some bus cycles. Let:

  • ff = bus clock frequency (Hz)
  • cc = bus cycles per beat (typically 1 for fast SRAM, more if a peripheral inserts wait states)

Time per beat: tbeat=cft_{\text{beat}} = \frac{c}{f}

Total transfer time (ignoring setup): tDMA=Ntbeat=Ncf\boxed{t_{\text{DMA}} = N \cdot t_{\text{beat}} = \frac{N\,c}{f}}

Address generation

Per beat, the controller updates addresses: Si+1=Si+ΔS,Di+1=Di+ΔDS_{i+1} = S_i + \Delta_S, \qquad D_{i+1} = D_i + \Delta_D

  • Mem-to-mem: ΔS=ΔD=w\Delta_S = \Delta_D = w (both increment).
  • Periph-to-mem: ΔS=0\Delta_S = 0 (fixed peripheral register), ΔD=w\Delta_D = w (buffer fills).
  • Mem-to-periph: ΔS=w\Delta_S = w, ΔD=0\Delta_D = 0.
Figure — DMA — memory-to-memory, peripheral-to-memory without CPU

Triggering & arbitration

  • Mem-to-mem is usually software-triggered: program it, set the enable bit, it runs full speed.
  • Periph transfers are hardware-triggered: the peripheral asserts a DMA request line each time it has/needs one item (e.g., "ADC conversion ready"). DMA performs one beat per request → naturally rate-matched.

Worked Examples


Common Mistakes (Steel-manned)


Recall Feynman: explain to a 12-year-old

Your computer's brain is busy thinking. Moving a giant pile of papers from one desk to another is boring busy-work. So the brain hires a robot helper (DMA): "Take papers from desk A, put them on desk B, 1000 of them, then tap me on the shoulder when you're done." The brain keeps thinking while the robot carries papers. The robot is fast and never gets bored — but both share one hallway (the bus), so sometimes the brain has to wait a moment for the robot to pass.


Flashcards

What does DMA stand for and what does it do?
Direct Memory Access — a hardware controller that transfers data between memory/peripherals without the CPU handling each word.
Why use DMA instead of a CPU copy loop?
Frees the CPU for compute (parallelism), gives deterministic low-latency capture, and uses dedicated hardware to move data in fewer cycles.
Formula for DMA transfer time?
t=Nc/ft = Nc/f where N=item count, c=bus cycles per beat, f=bus clock.
Formula for DMA throughput, and why doesn't N appear?
Throughput=wf/c\text{Throughput}=wf/c; N cancels because both bytes and time scale with N, so rate depends only on width, clock, cycles/beat.
In periph-to-mem mode, what are the source and destination address increments?
Source (peripheral register) Δ=0 (fixed); destination (RAM buffer) Δ=w (increments).
Why is the peripheral address fixed during transfer?
A peripheral's data register is a single hardware address; new bytes appear at the same location each time it's ready.
What is cycle stealing?
DMA grabs the bus for one beat then releases it, so the CPU isn't starved — trading throughput for lower CPU latency.
What must you do before READING a DMA-filled buffer on a cached CPU?
Invalidate the data cache so the CPU sees fresh RAM contents, not stale cached values.
Why is DMA NOT truly "free"?
It consumes bus bandwidth/cycles; while DMA holds the bus the CPU may stall on memory accesses.
When can you safely use DMA result data?
Only after the transfer-complete interrupt/flag fires (DMA is asynchronous).
How does wider item width affect transfer time for fixed total bytes?
Wider items → fewer beats → proportionally faster (4-byte ≈ 4× faster than 1-byte).
What triggers a peripheral-to-memory DMA beat?
A hardware DMA request line asserted by the peripheral when it has data ready (rate-matched).

Connections

  • Interrupts and ISRs — DMA signals completion via an interrupt.
  • Memory-Mapped IO — peripheral data registers are fixed addresses DMA reads/writes.
  • Cache Coherency — the classic DMA stale-data trap.
  • Bus Arbitration — how CPU and DMA share one memory bus.
  • Circular and Double Buffering — DMA modes for continuous streaming.
  • ADC and DAC — common DMA-driven peripherals.
  • memcpy and Block Copy — mem-to-mem DMA accelerates this.

Concept Map

programs S D N w

moves each word

signals when done

notifies

frees CPU for

time = N c / f

B / t

wider items

CPU

DMA controller

Data transfer

Interrupt

Parallelism / compute

Mem-to-Mem

Periph-to-Mem

Mem-to-Periph

Transfer time

Throughput = w f / c

4x with w=4

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho CPU ek manager hai jo important sochne ka kaam kar raha hai. Ab agar 1000 boxes (data) ek desk se doosre desk pe le jaane hain, to manager khud ek-ek box uthayega to uska saara time waste ho jayega. Isiliye hum ek DMA controller rakhte hain — ek dedicated hardware robot jo data ko directly source se destination tak copy karta hai, bina CPU ko har word touch kiye. CPU sirf 4 cheezein set karta hai (Source, Count, Address-increment, Mode), enable dabata hai, aur phir apna asli kaam karta rehta hai. Jab transfer complete hota hai, DMA ek interrupt maar ke bolta hai "ho gaya boss".

Teen main modes hain: mem-to-mem (RAM se RAM, jaise fast memcpy — dono addresses badhte hain), periph-to-mem (jaise ADC ke samples RAM buffer me — peripheral ka address fixed rehta hai kyunki naya data hamesha same register pe aata hai), aur mem-to-periph (buffer se UART/DAC ko data bhejna). Time ka formula simple hai: t=Nc/ft = Nc/f, aur throughput =wf/c= wf/c — yahan dhyaan do ki throughput me NN cancel ho jaata hai, matlab wider items (4-byte vs 1-byte) use karo to 4x faster transfer milta hai.

Do bade traps yaad rakhna. Pehla: DMA free nahi hai — wo bus use karta hai, aur jab DMA bus pakadta hai to CPU memory access pe ruk sakta hai (cycle stealing isi liye hota hai). Doosra aur zyada dangerous: cache coherency. Agar CPU me data cache hai, to DMA RAM me likhta hai par CPU purana cached value padh sakta hai — isiliye DMA buffer padhne se pehle cache invalidate karo, aur DMA ko data dene se pehle cache clean/flush karo. Aur sabse important: transfer complete hone ka interrupt aane se pehle buffer mat padho, warna adha-adhura data milega.

Go deeper — visual, from zero

Test yourself — Embedded Systems & Real-Time Software

Connections