Bird
Raised Fist0
Embedded Cprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main advantage of using circular buffer mode in DMA?

easy
A. It reduces the size of the buffer needed.
B. It increases the CPU speed automatically.
C. It disables interrupts during data transfer.
D. It allows continuous data transfer without stopping when the buffer is full.

Solution

  1. Step 1: Understand circular buffer mode behavior

    Circular buffer mode lets DMA wrap around to the start of the buffer after reaching the end, enabling continuous data transfer.
  2. Step 2: Identify the advantage

    This means data can be streamed continuously without stopping or manual buffer reset.
  3. Final Answer:

    It allows continuous data transfer without stopping when the buffer is full. -> Option D
  4. Quick Check:

    Circular buffer = continuous transfer [OK]
Hint: Circular buffer means wrap-around for continuous data [OK]
Common Mistakes:
  • Thinking CPU speed changes with DMA mode
  • Confusing interrupt behavior with buffer mode
  • Assuming buffer size is reduced automatically
2.

Which of the following is the correct way to enable circular mode in a DMA configuration struct in embedded C?

DMA_InitTypeDef DMA_InitStructure;
// ... other initializations
DMA_InitStructure.Mode = ?;
easy
A. DMA_Mode_Circular
B. DMA_Mode_Interrupt
C. DMA_Mode_Normal
D. DMA_Mode_OneShot

Solution

  1. Step 1: Recall DMA mode options

    DMA_Mode_Circular is the standard mode to enable circular buffer operation in DMA configurations.
  2. Step 2: Match the correct mode

    DMA_Mode_Normal is single transfer, others like Interrupt or OneShot are not standard DMA modes for circular buffer.
  3. Final Answer:

    DMA_Mode_Circular -> Option A
  4. Quick Check:

    Enable circular mode = DMA_Mode_Circular [OK]
Hint: Look for 'Circular' keyword in DMA mode options [OK]
Common Mistakes:
  • Using DMA_Mode_Normal instead of circular
  • Confusing interrupt mode with circular mode
  • Using non-existent DMA modes
3.

Given the following code snippet configuring DMA in circular mode, what will be the value of buffer[0] after 5 DMA transfers if the peripheral sends values 10, 20, 30, 40, 50 continuously?

#define BUFFER_SIZE 4
uint8_t buffer[BUFFER_SIZE] = {0};
// DMA configured in circular mode with buffer size 4
// Peripheral sends: 10, 20, 30, 40, 50
// DMA writes each value to buffer sequentially
// After 5 transfers:
medium
A. 10
B. 50
C. 20
D. 40

Solution

  1. Step 1: Understand circular buffer write pattern

    Buffer size is 4, so DMA writes values to buffer[0..3] in order, then wraps around to buffer[0].
  2. Step 2: Track each transfer

    Transfers: 1->buffer[0]=10, 2->buffer[1]=20, 3->buffer[2]=30, 4->buffer[3]=40, 5->buffer[0]=50 (overwrite).
  3. Final Answer:

    50 -> Option B
  4. Quick Check:

    5th write wraps to buffer[0] = 50 [OK]
Hint: Count writes modulo buffer size for circular buffers [OK]
Common Mistakes:
  • Assuming buffer[0] keeps first value
  • Ignoring wrap-around overwrite
  • Confusing peripheral data order
4.

Identify the error in this DMA circular buffer setup snippet:

DMA_InitTypeDef DMA_InitStructure;
uint8_t buffer[8];
DMA_InitStructure.Mode = DMA_Mode_Circular;
DMA_InitStructure.BufferSize = 10;
DMA_InitStructure.MemBaseAddr = (uint32_t)buffer;
// Start DMA
medium
A. Memory address is not cast to correct type.
B. DMA_Mode_Circular is not set correctly.
C. Buffer size is larger than actual buffer, causing overflow.
D. Buffer should be declared as volatile.

Solution

  1. Step 1: Compare buffer size and BufferSize parameter

    Buffer array has 8 elements, but BufferSize is set to 10, which is larger than buffer length.
  2. Step 2: Understand consequences

    This mismatch can cause DMA to write beyond buffer bounds, leading to overflow and undefined behavior.
  3. Final Answer:

    Buffer size is larger than actual buffer, causing overflow. -> Option C
  4. Quick Check:

    BufferSize must not exceed buffer length [OK]
Hint: BufferSize must match actual buffer length [OK]
Common Mistakes:
  • Setting BufferSize larger than buffer array
  • Ignoring buffer overflow risks
  • Assuming casting memory address fixes size mismatch
5.

You want to use DMA in circular mode to continuously read sensor data into a buffer of size 16. However, you notice data loss when the CPU reads the buffer slower than DMA writes. What is the best way to avoid data loss?

hard
A. Increase buffer size and use double buffering technique.
B. Disable circular mode and use normal mode.
C. Reduce DMA transfer speed to match CPU speed.
D. Use interrupts to stop DMA when CPU is busy.

Solution

  1. Step 1: Understand cause of data loss

    If CPU reads slower than DMA writes, new data overwrites unread data in circular buffer causing loss.
  2. Step 2: Apply solution with double buffering

    Increasing buffer size and using double buffering lets CPU read one buffer while DMA writes to another, preventing overwrite.
  3. Final Answer:

    Increase buffer size and use double buffering technique. -> Option A
  4. Quick Check:

    Double buffering prevents overwrite in circular DMA [OK]
Hint: Use double buffering to separate read/write buffers [OK]
Common Mistakes:
  • Disabling circular mode loses continuous transfer benefit
  • Trying to slow DMA speed which may not be possible
  • Stopping DMA interrupts causes data gaps
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard