Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 218 of 9363. Microcontroller Fundamentals and Bare-Metal CFree lesson

Implementing Circular Buffers with DMA Controllers

Configure DMA auto-reload features to create continuous data streaming buffers for real-time signal processing applications.

In this lesson, you will learn how to configure your DMA controller’s auto-reload functionality to create a continuous stream of data without manual software intervention.

The Problem with Linear Transfers

Previously, you learned how to use DMA to move a block of data from a peripheral to memory. Typically, once the DMA reaches the end of the buffer, it stops and fires a "Transfer Complete" interrupt. If you want to keep receiving data, your software must manually re-enable the DMA channel and provide a new memory address. This "re-arming" process introduces latency and risks missing new incoming data if your interrupt handler is delayed.

Enabling Circular Mode

Most DMA controllers feature a "Circular Mode" (or "Auto-Reload") bit. When this mode is enabled, the DMA controller does not stop when the transfer counter hits zero. Instead, it automatically reloads the initial buffer address and the original number of data elements.

This creates a self-sustaining cycle: as the peripheral (like an ADC or UART) receives data, the DMA continuously pushes that data into your memory buffer. Once the pointer reaches the end of the buffer, it seamlessly wraps back to the start and begins overwriting the oldest data.

Implementation Concept

To implement this, you configure the DMA controller as follows:

// Conceptual configuration for Circular DMA
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; 
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_BufferSize = BUFFER_SIZE;
DMA_Init(DMA1_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA1_Stream0, ENABLE);

Managing the Stream

Because the DMA is constantly overwriting the buffer, your mainline application must process the data "behind" the DMA pointer. You can use the "Transfer Half-Complete" interrupt to process the first half of the buffer while the DMA is busy filling the second half, and vice versa. This technique is known as double-buffering within a circular array, ensuring your real-time signal processing always has fresh data to work on while the hardware handles the heavy lifting of data movement.

Key Takeaway: By enabling Circular Mode, the DMA controller creates a continuous hardware loop that eliminates the need for software to manually re-trigger transfers, allowing for jitter-free, high-speed data streaming.