Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 40

DMA Controller: Function & Benefits

Recommended resources

Hello! Welcome back to our module on Data Acquisition and DMA.

In our last lesson, you did an excellent job implementing a system to read the MCU's internal temperature. You saw firsthand that HAL_ADC_PollForConversion, while simple, brings the CPU to a complete halt while waiting for the ADC. We concluded by asking: how can we acquire data without freezing the entire system? This is a critical question in embedded systems, especially in automotive applications where you must manage engine sensors, communication buses, and user interfaces simultaneously.

Today, we will answer that question by introducing one of the most powerful peripherals in a modern microcontroller: the Direct Memory Access (DMA) controller. This lesson is foundational. We will focus on understanding what the DMA is, why it is so essential, and the core concepts behind its operation.

Your learning outcome for this lesson is to explain the function of a Direct Memory Access (DMA) controller and its benefits.

1. The Core Problem: The CPU as a Data Porter

To understand why DMA is a game-changer, we first need to understand the fundamental architecture of your microcontroller. The CPU (the ARM Cortex-M3 core) is the "brains" of the operation. It is a bus master, meaning it has the intelligence to initiate data transfers on the system bus—the highway connecting all components.

Peripherals like the ADC, UART, and SPI are generally bus slaves. They are highly specialized and efficient at their specific tasks (e.g., converting a voltage, transmitting a bit), but they lack the intelligence to move their own data to system memory (SRAM). They must rely on a bus master to act as a data porter.

In our previous labs, the CPU was that porter. It had to:

  1. Tell the ADC to start.
  2. Wait.
  3. Read the result from the ADC's data register.
  4. Write that result to a variable in RAM.

This process, repeated for every single piece of data, turns the powerful CPU into a simple copy-paste machine, preventing it from performing more complex tasks.

The following video provides an excellent explanation of this master-slave relationship and sets the stage for introducing the DMA controller.

DMA Introduction Master – Slave Communication

The video 'DMA Introduction Master – Slave Communication' from Fastbit Embedded Brain Academy clearly explains why peripherals can't move their own data and why this makes the CPU a bottleneck.

Please watch from the beginning to 08:36. Focus on the distinction between a 'master' and a 'slave' on the bus, and how the DMA controller is introduced as a second master specifically designed to offload the CPU.


2. The Solution: DMA, a Specialized Co-processor for Data

As you just saw, the DMA controller is a second bus master. Think of it as a specialized co-processor whose only job is to move data from point A to point B with maximum efficiency and zero CPU intervention.

Once the CPU configures a DMA transfer, it can go back to its primary duties. The DMA controller waits for a trigger from a peripheral (like "ADC conversion finished"), independently handles the entire data transfer, and only notifies the CPU when the whole block of data has been moved.

DMA: Direct Memory Access Comparison
This diagram visually contrasts a CPU-intensive transfer with a DMA-powered one. On the left, the CPU is tied up moving each byte. On the right, the DMA handles the entire transfer, freeing the CPU for other work and only interrupting it upon completion.

The following reading provides a great summary of this concept and introduces how it's implemented in STM32 devices.

STM32 DMA Tutorial - Using Direct Memory Access (DMA) In STM32

The article 'STM32 DMA Tutorial' from deepbluembedded uses clear diagrams to reinforce the concept of offloading the CPU.

Please read the introductory section titled 'What Is Direct Memory Access (DMA)?'. Pay close attention to the two diagrams comparing a system with and without a DMA controller.


3. How a DMA Transfer Works

A DMA transfer isn't magic; it's a well-defined hardware process. The CPU's role shifts from being the porter to being the manager. It sets up the transfer by programming the DMA controller with a few key pieces of information:

  • Source Address: Where to get the data from (e.g., the ADC's data register, &ADC1->DR).
  • Destination Address: Where to put the data (e.g., the starting address of a buffer in RAM).
  • Transfer Count: The number of data items to move.
  • Data Size: The size of each item (e.g., 8-bit byte, 16-bit half-word).
  • Direction: Peripheral-to-memory, memory-to-peripheral, or memory-to-memory.
  • Mode: Normal (one-shot transfer) or Circular (repeats automatically, ideal for continuous streams).

Once configured and enabled, the process is as follows:

  1. A peripheral (e.g., ADC) finishes its task and raises a DMA request flag.
  2. The DMA controller sees the request and initiates a transfer.
  3. It reads the data from the source address.
  4. It writes the data to the destination address.
  5. It automatically increments the destination address pointer (to fill the next spot in our buffer).
  6. It decrements the transfer counter.
  7. This process repeats until the counter hits zero.
  8. Upon completion, the DMA controller can trigger a single interrupt to let the CPU know the entire block of data is ready for processing.

The following video walks through these configuration concepts in the context of an STM32 microcontroller.

STM32 DMA PT 1

The video 'STM32 DMA PT 1' by Eddie Amaya explains the practical concepts behind a DMA transfer, including transfer types, channels, priorities, and configuration settings.

This video is dense with information. Focus on these key concepts: 00:00 - 04:11: Understand the three main transfer types (peripheral-to-memory, memory-to-peripheral, memory-to-memory). 06:17 - 08:08: Grasp the idea of a peripheral 'triggering' a DMA request and the role of the transfer counter. 08:08 - 10:10: Understand the concept of DMA channels and the priority 'arbiter' that decides which channel goes first if multiple requests happen at once.

The DMA Arbiter

As mentioned in the video, your STM32F103 has two DMA controllers with a total of 12 channels. This means you could, for example, have the ADC filling a buffer on Channel 1 while you're transmitting data over UART on Channel 5.

But what if the ADC and UART both request a transfer at the exact same time? This is handled by the DMA Arbiter. It resolves conflicts based on a two-level priority system:

  1. Software Priority: You can assign each channel one of four priority levels (Low, Medium, High, Very High). The higher-priority channel wins.
  2. Hardware Priority: If two channels have the same software priority, the lower channel number wins (e.g., Channel 2 has priority over Channel 4).

This arbitration ensures that critical, high-speed data transfers are always serviced first.


4. The Benefits of DMA: More Than Just Speed

Now that we understand the function of a DMA controller, let's summarize its powerful benefits. The difference between using polling, interrupts, and DMA is a frequent topic in firmware engineering interviews.

STM32 UART DMA Receive Example | Normal & Circular Mode

This article from ControllersTech, while focused on UART, contains an excellent general-purpose summary and comparison table of the different data handling methods.

Read the sections 'Why Use DMA with UART in STM32?' and 'Blocking vs Interrupt vs DMA in STM32 UART'. The comparison table is particularly useful for summarizing the key differences.

As the article highlights, the main advantages are:

Feature Polling/Blocking Mode Interrupt Mode DMA Mode
CPU Usage Very high (CPU is 100% busy waiting) Medium (CPU is interrupted for every byte) Very low (CPU is only involved at the start and end)
Efficiency Very poor for continuous data Better, but overhead from frequent context switching can be high Excellent, ideal for high-speed, continuous data streams
Power Consumption High (CPU is always active) Lower Lowest (CPU can enter sleep modes during transfers)
Best Use Case Simple, one-off tasks Moderate data rates High-speed, large data blocks, and continuous streams

For an automotive system reading multiple sensors via ADC, handling CAN bus messages, and controlling actuators, the benefits are clear. DMA is not a luxury; it's a necessity for building a responsive, efficient, and reliable system.


Conclusion

In this lesson, we've pulled back the curtain on how data is moved efficiently inside your microcontroller. You now understand that the CPU isn't the only "master" in the system and that offloading data transfers to the specialized DMA controller is the key to unlocking high performance.

Key Takeaways:

  • A DMA controller is a hardware peripheral that acts as a second bus master, designed specifically for moving data.
  • Its primary function is to transfer data between peripherals and memory (or memory-to-memory) without any CPU intervention.
  • The CPU's role is to configure the DMA (source, destination, count) and then let it run independently.
  • The main benefits of using DMA are drastically reduced CPU load, the ability to handle high-throughput data streams, and lower power consumption.

Preview of the Next Lesson:
Theory is essential, but practice is where mastery is built. Now that you understand what DMA is and why it's so important, we will move on to our first hands-on lab. In the next lesson, you will configure a DMA channel for a basic memory-to-memory transfer. This will allow you to focus purely on the DMA configuration registers and process in a controlled way, building a solid foundation before we connect it to a peripheral like the ADC.