0
0
Embedded Cprogramming~10 mins

DMA controller concept in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - DMA controller concept
CPU Initiates DMA
DMA Controller Setup
DMA Transfers Data
DMA Signals Completion
CPU Resumes Normal Operation
The CPU sets up the DMA controller, which then transfers data directly between memory and peripherals, freeing the CPU until the transfer completes.
Execution Sample
Embedded C
void dma_transfer(void *src, void *dst, size_t size) {
    DMA->SRC = src;
    DMA->DST = dst;
    DMA->SIZE = size;
    DMA->CTRL |= START;
    while (!(DMA->STATUS & DONE)) {}
}
This code sets up and starts a DMA transfer, then waits until the transfer is done.
Execution Table
StepActionDMA RegistersCPU StatusResult
1CPU sets DMA->SRC to source addressSRC=src_addrCPU idleSource set
2CPU sets DMA->DST to destination addressDST=dst_addrCPU idleDestination set
3CPU sets DMA->SIZE to transfer sizeSIZE=sizeCPU idleSize set
4CPU sets DMA->CTRL START bitCTRL=STARTCPU idleTransfer started
5DMA controller transfers dataSTATUS=busyCPU waitingData moving
6DMA controller sets STATUS DONE bitSTATUS=DONECPU waitingTransfer complete
7CPU detects DMA done, resumes workCTRL=idleCPU activeCPU resumes
💡 DMA STATUS DONE bit set, CPU resumes normal operation
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 6Final
DMA->SRCundefinedsrc_addrsrc_addrsrc_addrsrc_addrsrc_addrsrc_addr
DMA->DSTundefinedundefineddst_addrdst_addrdst_addrdst_addrdst_addr
DMA->SIZE000sizesizesizesize
DMA->CTRL0000STARTSTARTidle
DMA->STATUS0000busyDONEDONE
CPU Statusactiveidleidleidleidlewaitingactive
Key Moments - 3 Insights
Why does the CPU wait after starting the DMA transfer?
The CPU waits because the DMA controller is busy transferring data (see execution_table step 5), so the CPU pauses until the DMA signals completion.
Does the CPU move the data during DMA transfer?
No, the DMA controller moves data directly between memory and peripherals, freeing the CPU (execution_table step 5 shows DMA doing the transfer).
What signals the CPU that the DMA transfer is complete?
The DMA controller sets the STATUS DONE bit (step 6), which the CPU checks to know it can resume work.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of DMA->CTRL after step 4?
ASTART
Bidle
CDONE
D0
💡 Hint
Check the DMA->CTRL column in execution_table row for step 4
At which step does the CPU resume normal operation?
AStep 5
BStep 6
CStep 7
DStep 4
💡 Hint
Look at the CPU Status and Result columns in execution_table
If DMA->STATUS never sets DONE, what happens to the CPU?
ACPU continues working normally
BCPU waits forever in the while loop
CCPU restarts the DMA transfer
DCPU ignores DMA and proceeds
💡 Hint
Refer to the while loop waiting for DMA->STATUS DONE bit in the code and execution_table step 5
Concept Snapshot
DMA Controller Concept:
- CPU sets source, destination, size in DMA registers
- CPU starts DMA transfer by setting START bit
- DMA controller moves data independently
- CPU waits until DMA signals DONE
- CPU resumes work after transfer completes
Full Transcript
This visual execution trace shows how a DMA controller works in embedded C. The CPU first sets the DMA source, destination, and size registers. Then it starts the transfer by setting the START bit in the control register. The DMA controller takes over and moves data directly between memory and peripherals, freeing the CPU. The CPU waits in a loop checking the DMA status. When the DMA sets the DONE bit, the CPU detects it and resumes normal operation. Variables like DMA->SRC, DMA->DST, DMA->SIZE, DMA->CTRL, and DMA->STATUS change step-by-step as shown. Key moments clarify why the CPU waits and how the DMA signals completion. The quiz tests understanding of register values and CPU behavior during DMA transfer.