Skip to content
systemcoding.dev
Embedded

Route DMA Requests With DMAMUX on STM32G4 and H7

Use the DMAMUX request multiplexer on STM32G4 and H7 to connect any peripheral request to any DMA channel, plus request generators and synchronization.

7 min read
Summarize in AI
Flat illustration of a browser and a server trading a request and a response, with the Arm, C and STMicroelectronics logos.

On an STM32F4 the question "which DMA stream can serve my ADC" is answered by a table in the reference manual, and two peripherals that want the same stream cannot both have it. Newer STM32 families remove that constraint with a small block between the peripherals and the DMA controllers, the DMA request multiplexer, DMAMUX. It connects any peripheral request to any channel, and it adds two features the older parts lack: a request generator that turns an external signal into DMA requests, and a synchronization input that gates a channel's requests. This post covers all three on the STM32G4 with STM32CubeG4 firmware V1.6 and notes where the H7 differs. The block is described in the DMAMUX chapter of the STM32G4 reference manual RM0440.

The basics of a DMA controller, including the request signal, are in the DMA guide.

The problem it solves

On the F4, each peripheral request is hard-wired to one or two streams, and each stream accepts requests from one peripheral at a time, chosen by its channel number from the table. USART2 receive can only use DMA1 stream 5, and the DAC channel 1 also only uses DMA1 stream 5. A design that needs both cannot have them, and the fix is a different peripheral choice, which can mean a different pin and a board change.

DMAMUX changes the wiring. Each DMA channel has a multiplexer in front of it with an input list that includes every peripheral request, and a register field selects which one it listens to. Selecting the request is configuration, not wiring, and no two peripherals compete for a channel unless you assign them to the same one.

The structure on the G4:

  • DMA1 and DMA2 each have eight channels, 16 in total.

  • DMAMUX1 has one request line multiplexer per DMA channel, so 16 channels, plus 4 request generators.

  • Each multiplexer channel is permanently tied to one DMA channel: DMAMUX channels 0 to 7 feed DMA1 channels 1 to 8, and DMAMUX channels 8 to 15 feed DMA2 channels 1 to 8.

The H7 follows the same idea with the controllers laid out differently. DMA1 and DMA2 have eight streams each and share DMAMUX1, while the BDMA in the third power domain has its own multiplexer, DMAMUX2, with its own set of request numbers. The HAL hides most of that behind the same Request field.

Selecting a request in the HAL

The HAL's DMA initialization has a field that the F4 version does not: Init.Request. It names the peripheral request to connect, with constants such as DMA_REQUEST_ADC1 and DMA_REQUEST_USART2_RX. The channel instance is any free one.

dma_adc_g4.cc
#include "stm32g4xx_hal.h"

DMA_HandleTypeDef hdma_adc1;

void adc_dma_channel_init(ADC_HandleTypeDef *hadc) {
    __HAL_RCC_DMAMUX1_CLK_ENABLE();
    __HAL_RCC_DMA1_CLK_ENABLE();

    hdma_adc1.Instance                 = DMA1_Channel1;
    hdma_adc1.Init.Request             = DMA_REQUEST_ADC1;
    hdma_adc1.Init.Direction           = DMA_PERIPH_TO_MEMORY;
    hdma_adc1.Init.PeriphInc           = DMA_PINC_DISABLE;
    hdma_adc1.Init.MemInc              = DMA_MINC_ENABLE;
    hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
    hdma_adc1.Init.MemDataAlignment    = DMA_MDATAALIGN_HALFWORD;
    hdma_adc1.Init.Mode                = DMA_CIRCULAR;
    hdma_adc1.Init.Priority            = DMA_PRIORITY_HIGH;
    HAL_DMA_Init(&hdma_adc1);

    __HAL_LINKDMA(hadc, DMA_Handle, hdma_adc1);

    HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
}

Compare it with the F4 version of the same job. There is no stream number tied to the ADC, no Channel field to match against a table, and no FIFO configuration, since the G4 controller has none. The line that does the routing is Init.Request. Moving the ADC to DMA1 channel 5 instead is a change of one identifier, DMA1_Channel5, and its interrupt vector, with no other change in the request setup.

The DMAMUX clock is enabled separately from the DMA clock. Forgetting __HAL_RCC_DMAMUX1_CLK_ENABLE is the G4 and H7 version of the classic nothing-happens bug: the request never reaches the channel and no flag is set.

Two peripherals that conflicted on the F4 now coexist:

two_streams.cc
// USART2 receive and DAC output, which both needed DMA1 stream 5 on the F4
hdma_usart2_rx.Instance     = DMA1_Channel2;
hdma_usart2_rx.Init.Request = DMA_REQUEST_USART2_RX;

hdma_dac1.Instance          = DMA1_Channel3;
hdma_dac1.Init.Request      = DMA_REQUEST_DAC1_CHANNEL1;

The only constraint left is the one every design has, that two simultaneous transfers need two channels. With 16 channels on the G4 that is rarely a limit.

The request generator

The request generators are the first feature the F4 lacks. A generator turns a signal into DMA requests, in place of a peripheral. The trigger can be an EXTI line, the output of a low-power timer or a timer's trigger event, and each trigger edge produces a programmed number of requests. A DMA channel then treats the generator as its request source, just as it would a peripheral.

The use is anywhere an event should cause a data transfer with no CPU involvement. A button edge that copies a block of configuration words to a peripheral, a pulse from an external sensor that triggers a register read, a timer event that steps a memory-to-peripheral output. Without the generator, each of those needs an interrupt that starts a transfer.

reqgen.cc
HAL_DMA_MuxRequestGeneratorConfigTypeDef gen = {0};

void dma_trigger_on_edge(DMA_HandleTypeDef *hdma) {
    gen.SignalID      = HAL_DMAMUX1_REQ_GEN_EXTI0;       // external line 0
    gen.Polarity      = HAL_DMAMUX_REQ_GEN_RISING;
    gen.RequestNumber = 1;                               // one request per edge
    HAL_DMAEx_ConfigMuxRequestGenerator(hdma, &gen);
    HAL_DMAEx_EnableMuxRequestGenerator(hdma);
}

For the generator to feed the channel, the channel's Init.Request is set to DMA_REQUEST_GENERATOR0, which selects the first generator as the request source, before HAL_DMA_Init. With one request per edge and a transfer of one unit per request, each rising edge on the line moves one word. After three edges, a counter of completed transfers would read three.

Text
edge 1: 1 unit transferred
edge 2: 2 units transferred
edge 3: 3 units transferred

The signal IDs and the polarity constants are named in the HAL's DMA extension header for your family. Check the exact constant names there before copying, since they differ between G4, L4+ and H7 releases.

Synchronization

The second feature is synchronization. A DMA channel's requests can be gated by a sync signal, so that requests pass only while the signal is active, or only start after an edge. The sync input is chosen from the same kind of list: EXTI lines, a low-power timer output, a timer event.

A concrete use is aligning a transfer with a timer. Suppose a DAC channel is fed by DMA from a waveform table, paced by a timer, and you want the output to start on an external trigger rather than at an arbitrary moment. Enable the sync input with the trigger as its signal and the channel waits for the edge before honoring the first request.

sync.cc
HAL_DMA_MuxSyncConfigTypeDef sync = {0};

void dma_gate_on_edge(DMA_HandleTypeDef *hdma) {
    sync.SyncSignalID  = HAL_DMAMUX1_SYNC_EXTI0;
    sync.SyncPolarity  = HAL_DMAMUX_SYNC_RISING;
    sync.SyncEnable    = ENABLE;
    sync.EventEnable   = DISABLE;
    sync.RequestNumber = 8;       // requests to pass per sync event
    HAL_DMAEx_ConfigMuxSync(hdma, &sync);
}

Here each rising edge on the sync line lets 8 requests through, and the channel waits for the next edge before passing more. The EventEnable field would additionally generate a DMAMUX output event after the set number of requests, which can in turn trigger another peripheral or the generator of another channel. That cascade is how you chain a sequence of paced transfers without CPU code.

Overrun detection

Because DMAMUX can queue a limited number of requests, a fast peripheral that raises more requests than the channel can serve produces a synchronization overrun or a request overrun, reported in the DMAMUX status. The HAL surfaces it as a DMA error callback when you enable the corresponding interrupt. As with any DMA design, enable the error path in code you intend to ship.

What does not change

Everything else about DMA stays as it is on the F4: the memory-reachability rules, alignment, buffer lifetime, cache coherency on the H7, and the half-transfer and complete events. DMAMUX removes the wiring constraint and adds the generator and sync inputs, and it does not change the model of a controller that moves units between two addresses on a request. A reader who has configured DMA on an F4 loses nothing by moving to a G4; they only gain a field.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.