0
0
Embedded Cprogramming~10 mins

Circular buffer DMA mode in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Circular buffer DMA mode
Start DMA Transfer
Write data to buffer at write_index
Increment write_index
Check if write_index == buffer_size
Yes No
Reset write_index to 0
DMA continues writing in circular fashion
CPU reads data from buffer at read_index
Increment read_index
Check if read_index == buffer_size
Yes No
Reset read_index to 0
Repeat until stop
DMA writes data into a fixed-size buffer in a loop, wrapping around when reaching the end, while CPU reads data similarly, enabling continuous data flow without overflow.
Execution Sample
Embedded C
uint8_t buffer[4];
int write_index = 0;
int read_index = 0;

// DMA writes 6 bytes
for (int i = 0; i < 6; i++) {
  buffer[write_index++] = i + 1;
  if (write_index == 4) write_index = 0;
}
DMA writes 6 bytes into a 4-byte buffer, wrapping write_index to 0 after reaching buffer end.
Execution Table
Stepiwrite_index beforeActionwrite_index afterbuffer state
100Write 1 at buffer[0]1[1, _, _, _]
211Write 2 at buffer[1]2[1, 2, _, _]
322Write 3 at buffer[2]3[1, 2, 3, _]
433Write 4 at buffer[3]0[1, 2, 3, 4] (wrap)
540Write 5 at buffer[0]1[5, 2, 3, 4]
651Write 6 at buffer[1]2[5, 6, 3, 4]
💡 DMA stops after writing 6 bytes; write_index is 2, buffer wrapped twice.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
write_index01230122
buffer[_, _, _, _][1, _, _, _][1, 2, _, _][1, 2, 3, _][1, 2, 3, 4][5, 2, 3, 4][5, 6, 3, 4][5, 6, 3, 4]
Key Moments - 3 Insights
Why does write_index reset to 0 after reaching buffer size?
Because the buffer is circular, when write_index equals buffer size (4), it wraps to 0 to overwrite from the start, as shown in execution_table step 4.
Does the buffer lose old data when DMA writes more than buffer size?
Yes, older data is overwritten when write_index wraps around, like at step 5 where buffer[0] changes from 1 to 5.
Why is it safe for DMA to write continuously without stopping?
Because the circular buffer allows continuous writing by wrapping indices, preventing overflow if CPU reads data timely.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of write_index after step 4?
A4
B3
C0
D1
💡 Hint
Check the 'write_index after' column for step 4 in execution_table.
At which step does the buffer first overwrite old data?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look at buffer state changes in execution_table rows 4 and 5.
If buffer size was 6 instead of 4, how many times would write_index wrap in 6 writes?
A1 time
B0 times
C2 times
D3 times
💡 Hint
Compare buffer size with number of writes in execution_sample.
Concept Snapshot
Circular buffer DMA mode:
- DMA writes data into fixed-size buffer
- write_index increments each write
- When write_index == buffer_size, reset to 0 (wrap)
- Enables continuous data flow without overflow
- CPU reads similarly with read_index
- Overwrites old data if CPU is slow
Full Transcript
This visual execution shows how DMA writes data into a circular buffer. The write_index starts at 0 and increments after each byte written. When write_index reaches the buffer size, it resets to 0 to wrap around. This allows continuous writing without overflow. The buffer content updates accordingly, overwriting old data when wrapping. The variable tracker shows write_index and buffer state after each write. Key moments clarify why wrapping happens and how data can be overwritten. The quiz tests understanding of index wrapping and buffer overwriting.