What is STM32F407 DMA Controller-Embedded Systems Course for Freshers in Hyderabad

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

Tutorial 18 — Direct Memory Access

STM32F407 DMA Controller

Master DMA1 and DMA2 — all stream/channel registers bit-by-bit, transfer modes (normal, circular, double-buffer), interrupt flags, and complete examples for memory copy, UART TX, and ADC scan.

2 DMA controllers16 Streams total8ch Per streamAHB Bus masteringM2M Mem-to-Mem
===== 1: OVERVIEW =====

01 DMA Overview

DMA (Direct Memory Access) transfers data between memory and peripherals — or between two memory regions — without CPU involvement. The CPU starts a transfer, then continues executing other code while DMA works in the background on the AHB bus.

DMA Controllers in STM32F407
ControllerBase AddressStreamsBusSpecial Feature
DMA10x40026000Stream 0–7AHB1DAC, I2C, SPI2/3, USART2–5, TIM2–5/7, I2S
DMA20x40026400Stream 0–7AHB1ADC1–3, SPI1/4/5, USART1/6, TIM1/8, SDIO, Mem-to-Mem
Memory-to-Memory: Only DMA2 supports memory-to-memory transfers. DMA1 can only transfer between a peripheral and memory.

DMA Architecture

STM32F407 DMA Architecture
DMA1 / DMA2
Stream 0
CH0..CH7
SxCR
SxNDTR
SxPAR
SxM0AR
SxM1AR
Stream 1
CH0..CH7
SxCR
SxNDTR
SxPAR
SxM0AR
SxM1AR
…
Stream 7
CH0..CH7
SxCR
SxNDTR
SxPAR
SxM0AR
SxM1AR
AHB bus master (bypasses CPU)
AHB1 Bus Matrix
Flash
(read)
SRAM
(read/write)
Peripherals
(USART, ADC, SPI…)

Each DMA controller has 8 streams. Each stream can be configured to one of 8 channels. A channel selects which peripheral is requesting the DMA. The channel selection is stored in the CHSEL[2:0] field of SxCR.

DMA Request Channel Mapping

The most commonly used DMA streams on STM32F407 (partial list from reference manual Table 42/43):

DMA1 S0 CH0
SPI3_RX, I2S3_RX
DMA1 S0 CH4
TIM4_CH1
DMA1 S1 CH0
SPI3_RX, I2S3_RX
DMA1 S2 CH0
SPI3_RX, I2S3_RX
DMA1 S3 CH4
TIM4_CH2
DMA1 S4 CH0
SPI2_RX, I2S2_RX
DMA1 S5 CH3
USART2_RX
DMA1 S5 CH7
DAC CH1
DMA1 S6 CH4
TIM4_UP
DMA1 S6 CH7
DAC CH2
DMA1 S6 CH3
USART2_TX
DMA1 S7 CH3
USART2_TX (alternate)
DMA2 S0 CH0
ADC1
DMA2 S0 CH3
SPI1_RX
DMA2 S1 CH3
SPI1_RX
DMA2 S2 CH3
SPI1_RX (alt)
DMA2 S3 CH3
SPI1_TX
DMA2 S4 CH0
ADC1 (alt)
DMA2 S5 CH3
SPI1_TX
DMA2 S2 CH4
USART1_RX
DMA2 S7 CH4
USART1_TX
DMA2 S0–7 CH0
M2M (any channel 0)
Always check reference manual Table 42/43 for the exact stream+channel for your peripheral. Multiple streams may support the same peripheral — pick the one not in use.
===== 2: REGISTERS =====

02 DMA Register Map

Each DMA controller has:

  • Two interrupt status registers: LISR (streams 0–3) and HISR (streams 4–7)
  • Two interrupt flag clear registers: LIFCR and HIFCR
  • Per-stream registers (SxCR, SxNDTR, SxPAR, SxM0AR, SxM1AR, SxFCR) — 6 registers × 8 streams
OffsetRegisterDescription
0x000DMA_LISRLow interrupt status — TCIF/HTIF/TEIF/DMEIF/FEIF for streams 0–3
0x004DMA_HISRHigh interrupt status — same flags for streams 4–7
0x008DMA_LIFCRLow interrupt flag clear — write 1 to clear flags for streams 0–3
0x00CDMA_HIFCRHigh interrupt flag clear — write 1 to clear flags for streams 4–7
0x010+n×0x18DMA_SxCRStream n control register (n=0–7)
0x014+n×0x18DMA_SxNDTRStream n number of data items to transfer (16-bit)
0x018+n×0x18DMA_SxPARStream n peripheral address register
0x01C+n×0x18DMA_SxM0ARStream n memory 0 address register
0x020+n×0x18DMA_SxM1ARStream n memory 1 address (double-buffer mode)
0x024+n×0x18DMA_SxFCRStream n FIFO control register

C — Stream address macros

#define DMA1_BASE  0x40026000UL
#define DMA2_BASE  0x40026400UL

/* Stream n offset = 0x10 + n * 0x18 */
#define DMA_SxCR(dma, n)    (*(volatile uint32_t *)((dma) + 0x10 + (n)*0x18))
#define DMA_SxNDTR(dma, n)  (*(volatile uint32_t *)((dma) + 0x14 + (n)*0x18))
#define DMA_SxPAR(dma, n)   (*(volatile uint32_t *)((dma) + 0x18 + (n)*0x18))
#define DMA_SxM0AR(dma, n)  (*(volatile uint32_t *)((dma) + 0x1C + (n)*0x18))
#define DMA_SxM1AR(dma, n)  (*(volatile uint32_t *)((dma) + 0x20 + (n)*0x18))
#define DMA_SxFCR(dma, n)   (*(volatile uint32_t *)((dma) + 0x24 + (n)*0x18))

/* Interrupt status/clear registers */
#define DMA_LISR(dma)   (*(volatile uint32_t *)((dma) + 0x00))
#define DMA_HISR(dma)   (*(volatile uint32_t *)((dma) + 0x04))
#define DMA_LIFCR(dma)  (*(volatile uint32_t *)((dma) + 0x08))
#define DMA_HIFCR(dma)  (*(volatile uint32_t *)((dma) + 0x0C))

/* Usage: */
/* DMA_SxCR(DMA2_BASE, 0) = 0x12345678;  → DMA2 Stream0 CR */
/* DMA_SxNDTR(DMA1_BASE, 6) = 256;       → DMA1 Stream6 NDTR */

DMA_SxCR — Stream Control Register

This is the most important register — configures direction, data sizes, priorities, burst, circular mode, and enables the stream.

DMA_SxCR @ DMA_BASE + 0x10 + n×0x18
31res
30res
29res
28res
27CH2
26CH1
25CH0
24MB1
23MB0
22PB1
21PB0
20res
19CT
18DBM
17PL1
16PL0
15PCOS
14MS1
13MS0
12PS1
11PS0
10MINC
9PINC
8CIRC
7DIR1
6DIR0
5PFCT
4TCIE
3HTIE
2TEIE
1DMIE
0EN
Bit and field descriptions
Bit / FieldDescription
31 resReserved
27 CH2CHSEL[2:0] bits 27:25 — Channel selection:
26 CH1CHSEL[1]
25 CH0CHSEL[0]
24 MB1MBURST[1:0] bits 24:23 — Memory burst:
23 MB0MBURST[0]
22 PB1PBURST[1:0] bits 22:21 — Peripheral burst
21 PB0PBURST[0]
20 resReserved
19 CTCT — Current Target (double-buffer):
18 DBMDBM — Double Buffer Mode:
17 PL1PL[1:0] bits 17:16 — Priority Level:
16 PL0PL[0]
15 PCOSPINCOS — Peripheral increment offset:
14 MS1MSIZE[1:0] bits 14:13 — Memory data size:
13 MS0MSIZE[0]
12 PS1PSIZE[1:0] bits 12:11 — Peripheral data size:
11 PS0PSIZE[0]
10 MINCMINC — Memory increment:
9 PINCPINC — Peripheral increment:
8 CIRCCIRC — Circular mode:
7 DIR1DIR[1:0] bits 7:6 — Transfer direction:
6 DIR0DIR[0]
5 PFCTPFCTRL — Peripheral flow controller:
4 TCIETCIE — Transfer Complete interrupt enable:
3 HTIEHTIE — Half Transfer interrupt enable:
2 TEIETEIE — Transfer Error interrupt enable
1 DMIEDMEIE — Direct Mode Error interrupt enable
0 ENEN — Stream enable:
BitsNameDescription
27:25CHSEL[2:0]Channel selection: 0–7. Must match the peripheral’s DMA request channel from Table 42/43
24:23MBURST[1:0]Memory burst: 00=single, 01=INCR4, 10=INCR8, 11=INCR16. Use 00 for normal transfers.
22:21PBURST[1:0]Peripheral burst: same encoding as MBURST
19CTCurrent target (double-buffer only): 0=M0AR is active, 1=M1AR is active. CPU changes inactive buffer while DMA uses active.
18DBMDouble-buffer mode enable: 1=CT toggles automatically at end of each transfer
17:16PL[1:0]Priority level: 00=low, 01=medium, 10=high, 11=very high. Resolves conflicts when two streams want AHB bus.
15PINCOSPeripheral increment offset size: 0=PSIZE, 1=always 32-bit regardless of PSIZE
14:13MSIZE[1:0]Memory data size: 00=byte, 01=half-word (16-bit), 10=word (32-bit)
12:11PSIZE[1:0]Peripheral data size: same encoding as MSIZE
10MINCMemory increment mode: 1=auto-increment memory address after each transfer
9PINCPeripheral increment mode: usually 0 (peripheral has fixed data register)
8CIRCCircular mode: 1=NDTR automatically reloads to original value when it reaches 0. Transfer loops forever.
7:6DIR[1:0]Data transfer direction: 00=periph→memory (RX), 01=memory→periph (TX), 10=memory→memory
5PFCTRLPeripheral flow controller: 0=DMA controls number (use NDTR), 1=peripheral controls (NDTR ignored)
4TCIETransfer complete interrupt enable
3HTIEHalf-transfer interrupt enable: fires when NDTR reaches half. Used for double-buffer.
2TEIETransfer error interrupt enable
1DMEIEDirect mode error interrupt enable
0ENStream enable: Write 1 to start. Hardware clears this when transfer completes in normal mode.

DMA_SxNDTR — Number of Data Items

A 16-bit register (bits 15:0) that holds the number of data items to transfer. Each “item” is the size defined by MSIZE/PSIZE. This register decrements by 1 for each completed transfer. When it reaches 0:

  • Normal mode: transfer stops, EN is cleared by hardware
  • Circular mode: NDTR reloads to its original value, transfer restarts
Write NDTR only when stream is disabled (EN=0). Wait until EN reads back as 0 before reconfiguring.

DMA_SxPAR — Peripheral Address Register

The 32-bit address of the peripheral data register. For example, for USART2 TX: 0x40004404 (USART2_DR). This address does NOT increment (PINC=0 in most cases since the peripheral has one fixed data register).

DMA_SxM0AR — Memory 0 Address Register

The 32-bit base address of the memory buffer. This increments by MSIZE bytes after each transfer if MINC=1. In double-buffer mode, M0AR is the primary buffer and M1AR is the secondary.

LISR/HISR — Interrupt Status Flags

Each stream has 5 flags packed into the 32-bit LISR/HISR registers. For stream N, the bit positions within LISR (streams 0–3) or HISR (streams 4–7) are:

FlagNameMeaningClear
TCIFxTransfer CompleteNDTR reached 0 (full transfer done)Write 1 to LIFCRx/HIFCRx
HTIFxHalf TransferNDTR reached half the original valueWrite 1 to clear
TEIFxTransfer ErrorBus error during DMA accessWrite 1 to clear, stream auto-disabled
DMEIFxDirect Mode ErrorFIFO overrun/underrun in direct modeWrite 1 to clear
FEIFxFIFO ErrorFIFO overrun/underrunWrite 1 to clear

C — Flag bit positions per stream in LISR/HISR

/* LISR covers streams 0–3, HISR covers streams 4–7.
   Within each register, the flag bit offsets for stream N (0..3 in LISR, 4..7 in HISR):
   Stream 0/4: bits 5,4,3,2,0
   Stream 1/5: bits 11,10,9,8,6
   Stream 2/6: bits 21,20,19,18,16
   Stream 3/7: bits 27,26,25,24,22

   Bit meaning within each group:
   +5: TCIF, +4: HTIF, +3: TEIF, +2: DMEIF, +0: FEIF
   (skipping bit +1 and +6/12/22 which are reserved)

   Example: TCIF for DMA2 Stream 0 is in LISR bit 5 */

/* Helper macro: get LISR/HISR register and bit offset for stream N */
static inline uint8_t dma_tcif_bit(uint8_t stream) {
    /* bit offsets: stream%4 maps to [5, 11, 21, 27] */
    const uint8_t offsets[] = {5, 11, 21, 27};
    return offsets[stream & 3];
}

/* Wait for DMA2 Stream 0 transfer complete */
void DMA2_S0_WaitTC(void) {
    volatile uint32_t *LISR  = (volatile uint32_t *)(0x40026400);
    volatile uint32_t *LIFCR = (volatile uint32_t *)(0x40026408);
    while (!(*LISR & (1<<5)));  /* wait TCIF0 */
    *LIFCR = (1<<5);            /* clear CTCIF0 */
}
===== 3: TRANSFER MODES =====

03 Transfer Modes

Normal Mode

The DMA transfers exactly NDTR items, then stops. The EN bit is cleared by hardware. You must re-configure and re-enable the stream for the next transfer. Used for one-shot transfers like: memcpy, single SPI transaction.

Circular Mode (CIRC=1)

When NDTR reaches 0, it automatically reloads to its original value and the transfer restarts from the beginning. Ideal for audio playback, ADC continuous scan, or continuous data streams. The address pointers also reset to M0AR.

Double Buffer Mode (DBM=1)

Two memory addresses (M0AR and M1AR) are used alternately. When DMA finishes filling M0AR, it switches to M1AR and sets the CT (Current Target) flag. Meanwhile, the CPU can safely process M0AR. Then when M1AR is full, DMA switches back to M0AR. This enables zero-copy real-time streaming.

Double Buffer mode timeline
Double Buffer mode timeline:
Time
DMA:
fills M0AR
fills M1AR
fills M0AR
CPU:
process M0AR
process M1AR
NDTR:
N → 0 (reload) → N → 0 (reload) → N
CT:
0
1
0
===== 4: MEMORY-TO-MEMORY =====

04 Memory-to-Memory Transfer

DMA2 can copy data between two memory regions without any peripheral trigger. Set DIR=10 and PINC=1, MINC=1. Both PAR (source) and M0AR (destination) will increment. No circular mode available in M2M.

C — Fast memcpy using DMA2

#include <stdint.h>
#include <stddef.h>

#define RCC_AHB1ENR  (*(volatile uint32_t *)0x40023830)

/* DMA2 Stream 0 registers */
#define DMA2_S0CR    (*(volatile uint32_t *)0x40026410)
#define DMA2_S0NDTR  (*(volatile uint32_t *)0x40026414)
#define DMA2_S0PAR   (*(volatile uint32_t *)0x40026418)
#define DMA2_S0M0AR  (*(volatile uint32_t *)0x4002641C)
#define DMA2_LISR    (*(volatile uint32_t *)0x40026400)
#define DMA2_LIFCR   (*(volatile uint32_t *)0x40026408)

/* DMA2 memory-to-memory copy (word-aligned, len in words) */
void DMA2_Memcpy32(const uint32_t *src, uint32_t *dst, uint16_t len_words) {
    /* Enable DMA2 clock */
    RCC_AHB1ENR |= (1 << 22);  /* DMA2EN bit 22 */

    /* 1. Disable stream first */
    DMA2_S0CR = 0;
    while (DMA2_S0CR & 1);  /* wait until EN=0 */

    /* 2. Clear any pending flags for stream 0 */
    DMA2_LIFCR = (0x3D);  /* clear all stream-0 flags: TCIF|HTIF|TEIF|DMEIF|FEIF */

    /* 3. Configure addresses and count */
    DMA2_S0PAR  = (uint32_t)src;    /* source address (as "peripheral") */
    DMA2_S0M0AR = (uint32_t)dst;    /* destination address */
    DMA2_S0NDTR = len_words;

    /* 4. Configure SxCR:
       CHSEL=0 (channel 0 for M2M)
       DIR=10 (M→M)
       PINC=1, MINC=1 (both increment)
       PSIZE=MSIZE=10 (32-bit words)
       CIRC=0, PFCTRL=0, EN=1 */
    DMA2_S0CR =
        (0  << 25) |  /* CHSEL = 0 */
        (2  << 13) |  /* MSIZE = 10 (32-bit) */
        (2  << 11) |  /* PSIZE = 10 (32-bit) */
        (1  << 10) |  /* MINC = 1 */
        (1  << 9)  |  /* PINC = 1 */
        (0  << 8)  |  /* CIRC = 0 */
        (2  << 6)  |  /* DIR = 10 (M→M) */
        (1  << 0);    /* EN = 1 */
}

/* Blocking wait for completion */
void DMA2_Memcpy32_Wait(void) {
    while (!(DMA2_LISR & (1<<5)));  /* wait TCIF0 */
    DMA2_LIFCR = (1<<5);            /* clear TCIF0 */
}

/* Example: copy 1024-word block */
uint32_t src_buf[1024];
uint32_t dst_buf[1024];

int main(void) {
    /* Fill src */
    for (int i=0; i<1024; i++) src_buf[i] = i;

    DMA2_Memcpy32(src_buf, dst_buf, 1024);
    DMA2_Memcpy32_Wait();
    /* dst_buf now = src_buf */

    while(1);
}
===== 5: UART DMA TX =====

05 UART TX via DMA

Instead of sending bytes one-by-one in polling mode, use DMA to stream a buffer to USART_DR automatically. USART2 TX uses DMA1 Stream 6 Channel 4. The CPU just starts the DMA and continues — the data transmits in the background.

C — USART2 TX via DMA1 Stream 6

#include <stdint.h>
#include <string.h>

/* ── Peripheral registers ── */
#define RCC_AHB1ENR   (*(volatile uint32_t *)0x40023830)
#define RCC_APB1ENR   (*(volatile uint32_t *)0x40023840)
#define GPIOA_MODER   (*(volatile uint32_t *)0x40020000)
#define GPIOA_AFRL    (*(volatile uint32_t *)0x40020020)

#define USART2_SR     (*(volatile uint32_t *)0x40004400)
#define USART2_DR     (*(volatile uint32_t *)0x40004404)
#define USART2_BRR    (*(volatile uint32_t *)0x40004408)
#define USART2_CR1    (*(volatile uint32_t *)0x4000440C)
#define USART2_CR3    (*(volatile uint32_t *)0x40004414)

/* DMA1 Stream 6 registers */
#define DMA1_S6CR    (*(volatile uint32_t *)0x400260A0)
#define DMA1_S6NDTR  (*(volatile uint32_t *)0x400260A4)
#define DMA1_S6PAR   (*(volatile uint32_t *)0x400260A8)
#define DMA1_S6M0AR  (*(volatile uint32_t *)0x400260AC)
#define DMA1_HISR    (*(volatile uint32_t *)0x40026004)
#define DMA1_HIFCR   (*(volatile uint32_t *)0x4002600C)

volatile uint8_t dma_tx_busy = 0;

void USART2_DMA_Init(void) {
    /* Clocks: GPIOA, DMA1, USART2 */
    RCC_AHB1ENR |= (1<<0)|(1<<21); /* GPIOAEN | DMA1EN */
    RCC_APB1ENR |= (1<<17);         /* USART2EN */

    /* PA2 = TX (AF7), PA3 = RX (AF7) */
    GPIOA_MODER &= ~((3<<4)|(3<<6));
    GPIOA_MODER |=  ((2<<4)|(2<<6));
    GPIOA_AFRL  &= ~((0xF<<8)|(0xF<<12));
    GPIOA_AFRL  |=  ((7<<8)|(7<<12));

    /* USART2: 115200, 8N1, APB1=42MHz, BRR=364 */
    USART2_BRR = 364;
    USART2_CR1 = (1<<13)|(1<<3)|(1<<2); /* UE|TE|RE */
    USART2_CR3 |= (1<<7);                    /* DMAT: DMA TX enable */

    /* DMA1 Stream6 base config (fixed peripheral address) */
    DMA1_S6CR  = 0;
    DMA1_S6PAR = 0x40004404;  /* USART2_DR */
}

/* Start async DMA TX — returns immediately */
void USART2_DMA_Send(const uint8_t *data, uint16_t len) {
    while (dma_tx_busy);  /* wait for previous transfer */

    DMA1_S6CR = 0;              /* disable */
    while (DMA1_S6CR & 1);      /* wait EN=0 */

    /* Clear stream-6 flags in HISR (stream 6 → bits 27,26,25,24,22) */
    DMA1_HIFCR = (0x3D<<22);

    DMA1_S6M0AR = (uint32_t)data;
    DMA1_S6NDTR = len;

    dma_tx_busy = 1;
    DMA1_S6CR =
        (4<<25) |  /* CHSEL = 4 (USART2_TX) */
        (2<<16) |  /* PL = high priority */
        (0<<13) |  /* MSIZE = 00 (byte) */
        (0<<11) |  /* PSIZE = 00 (byte) */
        (1<<10) |  /* MINC = 1 (memory increments) */
        (0<<9)  |  /* PINC = 0 (USART_DR is fixed) */
        (0<<8)  |  /* CIRC = 0 (normal) */
        (1<<6)  |  /* DIR = 01 (mem→periph) */
        (1<<4)  |  /* TCIE = 1 (TC interrupt) */
        (1<<0);    /* EN = 1 */
}

/* DMA1 Stream6 IRQ handler — fires when TX buffer fully sent */
void DMA1_Stream6_IRQHandler(void) {
    if (DMA1_HISR & (1<<27)) {   /* TCIF6 in HISR bit 27 */
        DMA1_HIFCR = (1<<27);     /* clear TCIF6 */
        dma_tx_busy = 0;
        /* Optionally: wait for TC in USART_SR then disable TE */
    }
}

/* Enable DMA1_Stream6 IRQ (IRQ 17) in NVIC */
void NVIC_Enable_DMA1S6(void) {
    volatile uint32_t *NVIC_ISER0 = (volatile uint32_t *)0xE000E100;
    *NVIC_ISER0 |= (1<<17);  /* DMA1_Stream6_IRQn = 17 */
}

int main(void) {
    USART2_DMA_Init();
    NVIC_Enable_DMA1S6();

    const uint8_t msg[] = "Hello from DMA UART!\r\n";
    USART2_DMA_Send(msg, sizeof(msg)-1);

    while(1) {
        /* CPU free while DMA sends data */
    }
}
===== 6: ADC DMA =====

06 ADC Scan via DMA

ADC in scan+continuous mode with DMA is the standard pattern for reading multiple channels. Covered in detail in Tutorial 16 (ADC). Key points:

  • ADC1 uses DMA2 Stream 0 Channel 0 (or Stream 4 Channel 0)
  • Set MSIZE=PSIZE=01 (16-bit) to match ADC_DR data width
  • Enable CIRC=1 so buffer updates continuously
  • Set ADC_CR2.DDS=1 to keep DMA requests active after last transfer
  • Set ADC_CR1.SCAN=1 and ADC_CR2.CONT=1

C — ADC DMA configuration snippet

volatile uint16_t adc_buf[4];  /* 4-channel scan result */

/* DMA2 Stream 0 Channel 0 → ADC1 */
#define DMA2_S0CR    (*(volatile uint32_t *)0x40026410)
#define DMA2_S0NDTR  (*(volatile uint32_t *)0x40026414)
#define DMA2_S0PAR   (*(volatile uint32_t *)0x40026418)
#define DMA2_S0M0AR  (*(volatile uint32_t *)0x4002641C)

void DMA2_ADC_Init(void) {
    volatile uint32_t *rcc_ahb1 = (volatile uint32_t *)(0x40023830);
    *rcc_ahb1 |= (1<<22);  /* DMA2EN */

    DMA2_S0CR  = 0; while(DMA2_S0CR & 1);

    DMA2_S0PAR  = 0x4001204C;        /* ADC1_DR */
    DMA2_S0M0AR = (uint32_t)adc_buf;
    DMA2_S0NDTR = 4;
    DMA2_S0CR   =
        (0<<25) |  /* CHSEL=0 (ADC1) */
        (1<<13) |  /* MSIZE=16-bit */
        (1<<11) |  /* PSIZE=16-bit */
        (1<<10) |  /* MINC=1 */
        (0<<9)  |  /* PINC=0 */
        (1<<8)  |  /* CIRC=1 */
        (0<<6)  |  /* DIR=00 (periph→mem) */
        (1<<0);    /* EN=1 */
}
===== 7: INTERRUPTS =====

07 DMA Interrupts

Each stream can generate interrupts on TC (transfer complete), HT (half transfer), TE (transfer error). IRQ numbers for STM32F407:

StreamDMA1 IRQnDMA2 IRQnHandler Name
Stream 01156DMA1/2_Stream0_IRQHandler
Stream 11257DMA1/2_Stream1_IRQHandler
Stream 21358DMA1/2_Stream2_IRQHandler
Stream 31459DMA1/2_Stream3_IRQHandler
Stream 41560DMA1/2_Stream4_IRQHandler
Stream 51668DMA1/2_Stream5_IRQHandler
Stream 61769DMA1/2_Stream6_IRQHandler
Stream 74770DMA1/2_Stream7_IRQHandler

C — Generic DMA interrupt handler pattern

/* DMA2 Stream 0 interrupt handler */
void DMA2_Stream0_IRQHandler(void) {
    volatile uint32_t *LISR  = (volatile uint32_t *)(0x40026400);
    volatile uint32_t *LIFCR = (volatile uint32_t *)(0x40026408);

    /* Check which flag fired (stream 0: bits [5:0] in LISR) */
    uint32_t isr = *LISR;

    if (isr & (1<<5)) {  /* TCIF0 — Transfer Complete */
        *LIFCR = (1<<5);  /* clear CTCIF0 */
        /* transfer done — process data */
    }
    if (isr & (1<<4)) {  /* HTIF0 — Half Transfer */
        *LIFCR = (1<<4);
        /* half buffer ready — process first half */
    }
    if (isr & (1<<3)) {  /* TEIF0 — Transfer Error */
        *LIFCR = (1<<3);
        /* handle error — stream is automatically disabled */
    }
}

/* Enable DMA2 Stream0 interrupt in NVIC */
void NVIC_Enable_DMA2S0(void) {
    volatile uint32_t *NVIC_ISER1 = (volatile uint32_t *)0xE000E104;
    *NVIC_ISER1 |= (1<<(56-32));  /* IRQ 56 → bit 24 of ISER1 */
}
===== 8: TIPS =====

08 Tips, Pitfalls & Best Practices

Common Pitfalls
  • Not waiting for EN=0 before reconfiguring: Always disable the stream (SxCR &= ~1) and wait for EN to read back 0 before changing NDTR, PAR, or M0AR. The hardware may take a few cycles to actually disable.
  • Not clearing old flags: Before enabling a stream, write 1 to all flag bits in LIFCR/HIFCR for that stream. Old TEIF flags will prevent the stream from starting.
  • MSIZE/PSIZE mismatch: If peripheral data is 16-bit (e.g., ADC_DR) and you set MSIZE=8-bit, only the low byte is transferred. Match sizes carefully.
  • Non-volatile buffer: DMA-filled buffers must be declared volatile. The compiler may cache the value in a register otherwise, and you’ll read stale data.
  • Cache coherency: If you use the Cortex-M4’s cache (not enabled by default on STM32F407), you need cache invalidation before reading DMA-written memory.
  • M2M only on DMA2: A common mistake is trying to configure memory-to-memory on DMA1. It silently fails.
  • Forgetting DMAT in peripheral CR: For UART DMA TX, you must set USART_CR3.DMAT=1. For ADC, ADC_CR2.DMA=1. The DMA channel alone is not enough.
  • Priority conflicts: Two streams at the same priority arbitrate by stream number (lower wins). For real-time use (audio, UART), set higher priority via PL field.
DMA Stream Configuration Checklist
  1. Enable DMA clock in RCC_AHB1ENR (DMA1EN bit 21, DMA2EN bit 22)
  2. Enable peripheral DMA request in peripheral’s control register (e.g., DMAT, DMA, DMAEN)
  3. Disable stream: SxCR &= ~1, poll until SxCR & 1 == 0
  4. Clear all interrupt flags in LIFCR/HIFCR for that stream
  5. Set SxPAR (peripheral address), SxM0AR (memory address)
  6. Set SxNDTR (count)
  7. Configure SxCR: CHSEL, DIR, MSIZE, PSIZE, MINC, PINC, CIRC, PL, interrupt enables
  8. Enable stream: set EN=1
  9. If using interrupt: enable IRQ in NVIC ISER

STM32F407 Tutorial Series — Tutorial 18: DMA  | ← DAC | Memory Map →

Bare-metal register-level programming  ·  No HAL library  ·  STM32F407VGT6

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →