What is DMA and How it works ?
The everyday picture is an
How DMA Works:
1. DMA Controller:
Role: Managed by a dedicated hardware component known as the DMA controller, which handles multiple DMA channels, each capable of managing a separate data transfer task.Setup: The CPU initializes a DMA transfer by specifying the source and destination addresses, the amount of data to transfer, and the direction of the transfer (read or write). The CPU then allows the DMA controller to manage the direct transfer.
2. Data Transfer:
Direct Transfer: The DMA controller takes over bus control from the CPU and conducts the data transfer directly between the I/O device and system memory, optimizing efficiency and speed.Modes of Operation: DMA can operate in different modes, such as burst mode (completing the entire data transfer at once) or cycle stealing mode (intermittently accessing the bus, allowing the CPU to access memory between bursts).
3. Interrupts and Completion:
Interrupts: Upon completion of the data transfer, the DMA controller sends an interrupt to the CPU, indicating that the transfer is complete and the device is ready for more data or processing.CPU Involvement: The CPU is involved only at the initiation and completion stages of the DMA transfer, which frees up CPU resources and enhances overall system performance.
Advantages of Using DMA:
Efficiency: Offloading data transfer duties from the CPU to the DMA controller allows the system to perform other tasks simultaneously, improving operational efficiency.Speed: DMA enables faster data transfer rates by minimizing the number of data copies and CPU interruptions.Reduced CPU Burden: As the CPU is not directly involved in the data transfer during DMA operation, it can continue running application processes or managing other tasks, optimizing the utilization of system resources.
Applications of DMA:
Multimedia Applications: Handles large data transfers for audio and video streaming efficiently.Network Communications: Manages data packets directly between network interfaces and memory.Data Acquisition Systems: Enables fast data transfer from sensors or measurement devices directly to memory for immediate processing.
Practical Notes and Common Pitfalls
The real benefit is fewer interrupts, not just less copying: without DMA a fast peripheral interrupts the CPU per byte/word — thousands of times a second — shredding real-time timing. DMA gathers a whole buffer and interrupts once. That jitter reduction is why DMA is a key real-time technique, not merely a speed trick.Cache coherency is the notorious bug: DMA writes straight to RAM, bypassing the CPU cache. If the CPU then reads stale cached data (or DMA reads data still dirty in cache), you get silent corruption. You must invalidate/flush the cache around DMA buffers — a classic "works sometimes" failure (ties to caches).Bus contention is the cost: CPU and DMA share the memory bus, so heavy DMA "steals" cycles and can stall the CPU. Burst mode is fast but hogs the bus; cycle-stealing mode is gentler but slower — the page lists both; the trade is CPU responsiveness vs transfer speed.Ping-pong (double) buffering is the standard pattern: for continuous streams (audio, radio samples), DMA fills buffer A while the CPU processes buffer B, then they swap — so data flows without gaps. This is how DSP/comms systems stream non-stop; the page implies but doesn't name it.It's central to this site's topic: streaming samples from an ADC/radio to memory, or assembling network packets, is textbook DMA — the CPU runs the algorithm while DMA moves the data. "Scatter-gather" DMA extends this to many non-contiguous regions in one setup (essential for networking).
Quick Recap
- DMA lets hardware transfer data
directly to/from memory without the CPU , freeing the processor for real work. - Flow:
CPU sets up the transfer → DMA controller takes the bus → moves data → interrupts once on completion . - Biggest real-time win:
collapsing per-byte interrupts into one , cutting CPU load and jitter. - Watch
cache coherency andbus contention ; useping-pong buffering for continuous streams (audio, network, sensors).