Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 401 of 9365. Communication Protocols and Data TransferFree lesson

Executing Scatter-Gather DMA Operations

Chain multiple non-contiguous memory buffers into a single DMA transaction to improve efficiency in complex data processing tasks.

In this lesson, you will learn how to use Scatter-Gather DMA to bridge the gap between non-contiguous memory locations and continuous data streams.

The Problem: Memory Fragmentation

In previous lessons, we learned that standard DMA transfers require a single, contiguous block of source memory. However, in real-world systems, data is often "scattered"—for example, network packets might be split across several non-contiguous buffers in RAM. Manually copying these to a single contiguous buffer (a "gather" operation) before sending them is a CPU-intensive task that wastes precious cycles.

The Solution: The Descriptor Chain

Scatter-Gather DMA solves this by allowing the DMA controller to follow a "linked list" of instructions. Instead of pointing to one static memory address, you configure the DMA channel to read a series of Descriptors. Each descriptor contains:

  1. A pointer to the current data segment.
  2. The length of that segment.
  3. A pointer to the next descriptor in the chain.

When the DMA controller finishes transferring the data segment defined by the first descriptor, it automatically loads the next descriptor from memory. It then resumes the transfer from the new address without any CPU intervention.

Practical Implementation

Imagine you need to send a header, a payload, and a footer stored at different memory addresses. You would set up a structure like this:

struct DMA_Descriptor {
    uint32_t source_addr;
    uint32_t dest_addr;
    uint32_t length;
    struct DMA_Descriptor* next_descriptor;
};

By linking these structures together (desc1->next = &desc2), the DMA engine acts like a state machine that traverses your memory map. Once you enable the DMA, the controller fetches the segments one by one, effectively "gathering" them into a single continuous stream on your target bus (like an I2C port or CAN peripheral), and "scattering" them out to the destination. This maximizes throughput by ensuring the DMA controller never idles while waiting for the CPU to handle the next segment.

Key Takeaway: Scatter-Gather DMA uses linked descriptors to automate the transfer of multiple, non-contiguous data buffers, eliminating the need for CPU-intensive memory copying.