0
0
Embedded Cprogramming~10 mins

Peripheral-to-memory transfer in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Peripheral-to-memory transfer
Peripheral has data ready
Check if DMA enabled?
NoWait or CPU handles
Yes
DMA controller reads data from peripheral
DMA controller writes data to memory
Update transfer count
Check if transfer complete?
NoRepeat DMA read/write
Yes
DMA signals transfer complete interrupt
CPU processes data in memory
This flow shows how data moves from a peripheral device to memory using DMA, reducing CPU load.
Execution Sample
Embedded C
volatile uint8_t *PERIPH_DATA = (uint8_t*)0x4000;
uint8_t buffer[4];
for(int i=0; i<4; i++) {
  buffer[i] = *PERIPH_DATA;
}
This code reads 4 bytes from a peripheral register into a memory buffer using CPU polling.
Execution Table
StepiActionPERIPH_DATA Readbuffer[i] Value
10Read from peripheral0x120x12
21Read from peripheral0x340x34
32Read from peripheral0x560x56
43Read from peripheral0x780x78
54Exit loop--
💡 i reaches 4, condition i<4 is false, loop ends
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
i012344
buffer[_,_,_,_][0x12,_,_,_][0x12,0x34,_,_][0x12,0x34,0x56,_][0x12,0x34,0x56,0x78][0x12,0x34,0x56,0x78]
Key Moments - 3 Insights
Why does the loop stop when i equals 4?
Because the loop condition i<4 becomes false at step 5 in the execution_table, so the loop exits.
Is the peripheral data read the same each time?
No, each read at steps 1 to 4 shows different values (0x12, 0x34, 0x56, 0x78) as shown in the execution_table.
Why do we use a buffer array here?
The buffer stores the data read from the peripheral so the CPU can use it later without reading the peripheral again.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of buffer[2] after step 3?
A0x34
B0x56
C0x78
D0x12
💡 Hint
Check the 'buffer[i] Value' column at step 3 where i=2.
At which step does the loop condition become false and exit?
AStep 5
BStep 4
CStep 3
DStep 2
💡 Hint
Look at the 'Action' column where it says 'Exit loop'.
If the buffer size was increased to 5, how would the variable 'i' change in variable_tracker?
AIt would start at 1
BIt would stay at 4
CIt would go up to 5 instead of 4
DIt would decrease
💡 Hint
The loop runs while i < buffer size, so increasing size increases max i.
Concept Snapshot
Peripheral-to-memory transfer:
- Data moves from peripheral to memory buffer
- CPU can read peripheral directly or use DMA
- Loop reads data step-by-step
- Loop ends when count reached
- Buffer stores data for CPU use
Full Transcript
This example shows how data is transferred from a peripheral device to memory using a simple loop in embedded C. The peripheral data register is read four times, each time storing the value into a buffer array. The loop variable i starts at 0 and increments until it reaches 4, at which point the loop stops. Each step reads a different byte from the peripheral, shown in the execution table. The buffer accumulates these bytes for later CPU processing. This method uses CPU polling, but in real embedded systems, DMA can automate this transfer to reduce CPU load.