5.5.8 · HinglishEmbedded Systems & Real-Time Software

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

2,033 words9 min readRead in English

5.5.8 · Coding › Embedded Systems & Real-Time Software


WHAT is DMA?

Teeno 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 sensor samples logging karna
Mem-to-Periph RAM buffer DAC/SPI/UART data register audio streaming out

WHY does DMA exist? (core motivation)

Deeper win hai parallelism: jab DMA bus own karke data move karta hai, CPU cache/instruction memory se run karta hai.


HOW does DMA work? (transfer timing first principles se derive karo)

Ek transfer completely 4 cheezein describe karti hain jo tumhe set karni hoti hain:

  1. Source address
  2. Destination address
  3. Count (items ki sankhya)
  4. Item width (bytes per item: 1, 2, ya 4)

Total bytes moved: .

Transfer time derive karna

Har DMA "beat" ek item move karta hai aur kuch bus cycles consume karta hai. Maano:

  • = bus clock frequency (Hz)
  • = bus cycles per beat (typically 1 fast SRAM ke liye, zyada agar peripheral wait states insert kare)

Time per beat:

Total transfer time (setup ignore karke):

Address generation

Har beat mein, controller addresses update karta hai:

  • Mem-to-mem: (dono increment hote hain).
  • Periph-to-mem: (fixed peripheral register), (buffer fill hota hai).
  • Mem-to-periph: , .
Figure — DMA — memory-to-memory, peripheral-to-memory without CPU

Triggering & arbitration

  • Mem-to-mem usually software-triggered hota hai: program karo, enable bit set karo, full speed chalega.
  • Periph transfers hardware-triggered hote hain: peripheral ek DMA request line assert karta hai jab bhi uske paas ek item hota hai/chahiye (jaise "ADC conversion ready"). DMA har request par ek beat perform karta hai → naturally rate-matched.

Worked Examples


Common Mistakes (Steel-manned)


Recall Feynman: ek 12-saal ke bacche ko explain karo

Computer ka dimag busy sochne mein laga hai. Ek bade pile of papers ko ek desk se doosri desk par uthana boring busy-work hai. Toh dimag ek robot helper (DMA) hire karta hai: "Desk A se papers lo, desk B par rakh do, 1000 papers, phir kaam hone par mujhe tap karo." Dimag sochta rehta hai jabki robot papers uthata rehta hai. Robot fast hai aur kabhi bore nahi hota — lekin dono ek hallway (the bus) share karte hain, toh kabhi-kabhi dimag ko robot ke guzarne ka thoda wait karna padta hai.


Flashcards

DMA ka full form kya hai aur yeh kya karta hai?
Direct Memory Access — ek hardware controller jo memory/peripherals ke beech data transfer karta hai bina CPU ke har word ko handle kiye.
CPU copy loop ki jagah DMA kyun use karein?
CPU ko compute ke liye free karta hai (parallelism), deterministic low-latency capture deta hai, aur dedicated hardware se data kam cycles mein move karta hai.
DMA transfer time ka formula?
jahan N=item count, c=bus cycles per beat, f=bus clock.
DMA throughput ka formula, aur N kyun appear nahi karta?
; N cancel ho jaata hai kyunki bytes aur time dono N ke saath scale hote hain, toh rate sirf width, clock, cycles/beat par depend karta hai.
Periph-to-mem mode mein source aur destination address increments kya hote hain?
Source (peripheral register) Δ=0 (fixed); destination (RAM buffer) Δ=w (increment hota hai).
Transfer ke dauran peripheral address fixed kyun hota hai?
Peripheral ka data register ek single hardware address hai; naye bytes har baar ready hone par usi location par appear hote hain.
Cycle stealing kya hai?
DMA bus ko ek beat ke liye grab karta hai phir release karta hai, taaki CPU starve na ho — throughput ki jagah lower CPU latency ke liye trade-off.
Cached CPU par DMA-filled buffer READ karne se pehle kya karna chahiye?
Data cache invalidate karo taaki CPU fresh RAM contents dekhe, stale cached values nahi.
DMA truly "free" kyun nahi hai?
Yeh bus bandwidth/cycles consume karta hai; jab DMA bus hold karta hai CPU memory accesses par stall ho sakta hai.
DMA result data kab safely use kar sakte ho?
Sirf tab jab transfer-complete interrupt/flag fire ho jaaye (DMA asynchronous hai).
Fixed total bytes ke liye wider item width transfer time ko kaise affect karta hai?
Wider items → kam beats → proportionally faster (4-byte ≈ 4× faster than 1-byte).
Peripheral-to-memory DMA beat ko kya trigger karta hai?
Peripheral ki taraf se ek hardware DMA request line assert hoti hai jab uske paas data ready hota hai (rate-matched).

Connections

  • Interrupts and ISRs — DMA completion ek interrupt ke through signal karta hai.
  • Memory-Mapped IO — peripheral data registers fixed addresses hain jo DMA read/write karta hai.
  • Cache Coherency — classic DMA stale-data trap.
  • Bus Arbitration — CPU aur DMA ek memory bus kaise share karte hain.
  • Circular and Double Buffering — continuous streaming ke liye DMA modes.
  • ADC and DAC — common DMA-driven peripherals.
  • memcpy and Block Copy — mem-to-mem DMA ise accelerate karta hai.

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