DMA Controllers, Transfers, and Circular Buffers
What You'll Learn
- 1Describe DMA channel and stream configuration
- 2Set up memory to peripheral and memory to memory transfers
- 3Implement circular and double buffered modes
- 4Handle DMA completion and error interrupts
The direct memory access controller moves data between peripherals and memory without the CPU on every byte, which frees the processor for control math. A transfer is described by a source address, a destination address, a count, and a set of increment and width flags, all held in channel registers. Circular mode wraps the count back to zero and keeps running, which is exactly what continuous ADC scans and audio streams need. Done well, DMA removes both the data movement overhead and the overrun bugs that polling creates.
Channels, Requests, and Transfer Descriptors
Some parts give the DMA a fixed set of channels mapped to specific peripherals, while others use a request multiplexer that routes any peripheral request to any stream. A descriptor sets the source and destination addresses, whether each address increments, the data width of each beat, and the total number of beats. Alignment must be respected, because a byte transfer to a halfword aligned address is a fault on many parts.
Priorities decide which stream wins arbitration when several request at once, and the priority should reflect the cost of a delayed beat rather than the peripheral importance. Memory to memory transfers run at full bus bandwidth and are useful for moving frames, relocating tables, or clearing buffers in the background.
Circular Mode and Double Buffering
Circular mode reloads the transfer count automatically when it reaches zero, so the stream runs forever writing into a ring of memory. The half transfer interrupt marks the midpoint, which lets the CPU process the first half of a buffer while DMA fills the second. This ping pong pattern is the standard way to stream audio, serial, or sensor frames without gaps.
Double buffered mode swaps between two target addresses on each transfer complete, giving the CPU a whole buffer at a time and removing the race where DMA overwrites data the CPU has not read yet. The cost is an extra interrupt and careful ownership rules, but the result is a clean producer consumer boundary.
Integration With Peripherals and the CPU
For ADC scans, DMA is triggered by the end of conversion and writes each result into an array, which eliminates overrun completely. For transmit paths such as UART or SPI, DMA refills the peripheral data register from a buffer and pends an interrupt only when the whole frame is out. Coherency matters when the CPU and DMA share cacheable memory, because one side can observe stale data unless caches are cleaned and invalidated around the exchange.
Debugging DMA means checking the enable bit, the remaining count, the channel interrupt flags, and whether the peripheral clock and request are actually asserted. A stream that never starts is almost always a missing request or a misaligned descriptor rather than a silicon fault.
DMA Configuration Fields
| Field | Purpose | Common Choice |
|---|---|---|
| Source address | Where data starts | Peripheral data register or RAM buffer |
| Destination address | Where data lands | RAM buffer or peripheral register |
| Increment flags | Whether addresses advance | On for buffers, off for registers |
| Data width | Byte, halfword, or word per beat | Match the peripheral register size |
| Transfer count | Number of beats | Reload for circular, stop for one shot |
| Priority | Arbitration between streams | Give urgent streams higher priority |
Real-World Examples
- ADC scan into a buffer â Enable DMA request from the ADC, point it at a result array, and run in circular mode with a half transfer interrupt.
- UART transmit without blocking â Start a DMA send of the frame, then handle the complete interrupt to chain the next frame.
- Move a lookup table â Use a memory to memory transfer to copy a table into SRAM once at boot.
- Audio double buffering â Swap buffer targets on transfer complete so the CPU always has a stable block to filter.
đĄWatch the Remaining Count
Common Mistakes
Mismatching data width and register size
Fix: Byte DMA into a word only register leaves stale bytes in the upper positions.
Sharing cacheable memory without coherency
Fix: Clean the CPU cache before DMA reads and invalidate it before the CPU reads, or data will be stale.
Forgetting to clear status flags first
Fix: Leftover transfer complete flags can pend an interrupt immediately after enabling the stream.
Key Takeaways
- DMA descriptors define addresses, widths, increments, and beat count.
- Circular and double buffered modes build gap free data streams.
- DMA removes overrun in ADC scans and blocking in serial transmit.
- Cache coherency must be handled when CPU and DMA share memory.
Frequently Asked Questions
Does DMA need interrupts?
No, it can run purely polled, but interrupts are how you know a block is ready without wasting cycles polling.
What is the difference between a channel and a stream?
Terminology varies by vendor; a stream is a DMA path that can be routed to a request, while a channel is a fixed peripheral binding.
How much CPU time does DMA save?
It removes per byte interrupt overhead entirely, leaving only per block processing, which typically cuts interrupt load by an order of magnitude.
Interview Questions
Q1: Describe a circular DMA receive scheme.
DMA writes into a ring buffer with a reload count, pends an interrupt at the half and full points, and the CPU consumes whichever half is not being written.
Q2: How do you debug a DMA transfer that never starts?
Check the peripheral clock, the request enable, the stream enable, address alignment, remaining count, and any pending error flags in order.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions