🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →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.
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.
| Controller | Base Address | Streams | Bus | Special Feature |
|---|---|---|---|---|
| DMA1 | 0x40026000 | Stream 0–7 | AHB1 | DAC, I2C, SPI2/3, USART2–5, TIM2–5/7, I2S |
| DMA2 | 0x40026400 | Stream 0–7 | AHB1 | ADC1–3, SPI1/4/5, USART1/6, TIM1/8, SDIO, Mem-to-Mem |
DMA Architecture
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):
02 DMA Register Map
Each DMA controller has:
- Two interrupt status registers:
LISR(streams 0–3) andHISR(streams 4–7) - Two interrupt flag clear registers:
LIFCRandHIFCR - Per-stream registers (SxCR, SxNDTR, SxPAR, SxM0AR, SxM1AR, SxFCR) — 6 registers × 8 streams
| Offset | Register | Description |
|---|---|---|
| 0x000 | DMA_LISR | Low interrupt status — TCIF/HTIF/TEIF/DMEIF/FEIF for streams 0–3 |
| 0x004 | DMA_HISR | High interrupt status — same flags for streams 4–7 |
| 0x008 | DMA_LIFCR | Low interrupt flag clear — write 1 to clear flags for streams 0–3 |
| 0x00C | DMA_HIFCR | High interrupt flag clear — write 1 to clear flags for streams 4–7 |
| 0x010+n×0x18 | DMA_SxCR | Stream n control register (n=0–7) |
| 0x014+n×0x18 | DMA_SxNDTR | Stream n number of data items to transfer (16-bit) |
| 0x018+n×0x18 | DMA_SxPAR | Stream n peripheral address register |
| 0x01C+n×0x18 | DMA_SxM0AR | Stream n memory 0 address register |
| 0x020+n×0x18 | DMA_SxM1AR | Stream n memory 1 address (double-buffer mode) |
| 0x024+n×0x18 | DMA_SxFCR | Stream 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.
Bit and field descriptions
| Bit / Field | Description |
|---|---|
| 31 res | Reserved |
| 27 CH2 | CHSEL[2:0] bits 27:25 — Channel selection: |
| 26 CH1 | CHSEL[1] |
| 25 CH0 | CHSEL[0] |
| 24 MB1 | MBURST[1:0] bits 24:23 — Memory burst: |
| 23 MB0 | MBURST[0] |
| 22 PB1 | PBURST[1:0] bits 22:21 — Peripheral burst |
| 21 PB0 | PBURST[0] |
| 20 res | Reserved |
| 19 CT | CT — Current Target (double-buffer): |
| 18 DBM | DBM — Double Buffer Mode: |
| 17 PL1 | PL[1:0] bits 17:16 — Priority Level: |
| 16 PL0 | PL[0] |
| 15 PCOS | PINCOS — Peripheral increment offset: |
| 14 MS1 | MSIZE[1:0] bits 14:13 — Memory data size: |
| 13 MS0 | MSIZE[0] |
| 12 PS1 | PSIZE[1:0] bits 12:11 — Peripheral data size: |
| 11 PS0 | PSIZE[0] |
| 10 MINC | MINC — Memory increment: |
| 9 PINC | PINC — Peripheral increment: |
| 8 CIRC | CIRC — Circular mode: |
| 7 DIR1 | DIR[1:0] bits 7:6 — Transfer direction: |
| 6 DIR0 | DIR[0] |
| 5 PFCT | PFCTRL — Peripheral flow controller: |
| 4 TCIE | TCIE — Transfer Complete interrupt enable: |
| 3 HTIE | HTIE — Half Transfer interrupt enable: |
| 2 TEIE | TEIE — Transfer Error interrupt enable |
| 1 DMIE | DMEIE — Direct Mode Error interrupt enable |
| 0 EN | EN — Stream enable: |
| Bits | Name | Description |
|---|---|---|
| 27:25 | CHSEL[2:0] | Channel selection: 0–7. Must match the peripheral’s DMA request channel from Table 42/43 |
| 24:23 | MBURST[1:0] | Memory burst: 00=single, 01=INCR4, 10=INCR8, 11=INCR16. Use 00 for normal transfers. |
| 22:21 | PBURST[1:0] | Peripheral burst: same encoding as MBURST |
| 19 | CT | Current target (double-buffer only): 0=M0AR is active, 1=M1AR is active. CPU changes inactive buffer while DMA uses active. |
| 18 | DBM | Double-buffer mode enable: 1=CT toggles automatically at end of each transfer |
| 17:16 | PL[1:0] | Priority level: 00=low, 01=medium, 10=high, 11=very high. Resolves conflicts when two streams want AHB bus. |
| 15 | PINCOS | Peripheral increment offset size: 0=PSIZE, 1=always 32-bit regardless of PSIZE |
| 14:13 | MSIZE[1:0] | Memory data size: 00=byte, 01=half-word (16-bit), 10=word (32-bit) |
| 12:11 | PSIZE[1:0] | Peripheral data size: same encoding as MSIZE |
| 10 | MINC | Memory increment mode: 1=auto-increment memory address after each transfer |
| 9 | PINC | Peripheral increment mode: usually 0 (peripheral has fixed data register) |
| 8 | CIRC | Circular mode: 1=NDTR automatically reloads to original value when it reaches 0. Transfer loops forever. |
| 7:6 | DIR[1:0] | Data transfer direction: 00=periph→memory (RX), 01=memory→periph (TX), 10=memory→memory |
| 5 | PFCTRL | Peripheral flow controller: 0=DMA controls number (use NDTR), 1=peripheral controls (NDTR ignored) |
| 4 | TCIE | Transfer complete interrupt enable |
| 3 | HTIE | Half-transfer interrupt enable: fires when NDTR reaches half. Used for double-buffer. |
| 2 | TEIE | Transfer error interrupt enable |
| 1 | DMEIE | Direct mode error interrupt enable |
| 0 | EN | Stream 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
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:
| Flag | Name | Meaning | Clear |
|---|---|---|---|
| TCIFx | Transfer Complete | NDTR reached 0 (full transfer done) | Write 1 to LIFCRx/HIFCRx |
| HTIFx | Half Transfer | NDTR reached half the original value | Write 1 to clear |
| TEIFx | Transfer Error | Bus error during DMA access | Write 1 to clear, stream auto-disabled |
| DMEIFx | Direct Mode Error | FIFO overrun/underrun in direct mode | Write 1 to clear |
| FEIFx | FIFO Error | FIFO overrun/underrun | Write 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 */
}
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.
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);
}
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 */
}
}
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=1to keep DMA requests active after last transfer - Set
ADC_CR1.SCAN=1andADC_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 */
}
07 DMA Interrupts
Each stream can generate interrupts on TC (transfer complete), HT (half transfer), TE (transfer error). IRQ numbers for STM32F407:
| Stream | DMA1 IRQn | DMA2 IRQn | Handler Name |
|---|---|---|---|
| Stream 0 | 11 | 56 | DMA1/2_Stream0_IRQHandler |
| Stream 1 | 12 | 57 | DMA1/2_Stream1_IRQHandler |
| Stream 2 | 13 | 58 | DMA1/2_Stream2_IRQHandler |
| Stream 3 | 14 | 59 | DMA1/2_Stream3_IRQHandler |
| Stream 4 | 15 | 60 | DMA1/2_Stream4_IRQHandler |
| Stream 5 | 16 | 68 | DMA1/2_Stream5_IRQHandler |
| Stream 6 | 17 | 69 | DMA1/2_Stream6_IRQHandler |
| Stream 7 | 47 | 70 | DMA1/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 */
}
08 Tips, Pitfalls & Best Practices
- 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.
- Enable DMA clock in RCC_AHB1ENR (DMA1EN bit 21, DMA2EN bit 22)
- Enable peripheral DMA request in peripheral’s control register (e.g., DMAT, DMA, DMAEN)
- Disable stream:
SxCR &= ~1, poll untilSxCR & 1 == 0 - Clear all interrupt flags in LIFCR/HIFCR for that stream
- Set SxPAR (peripheral address), SxM0AR (memory address)
- Set SxNDTR (count)
- Configure SxCR: CHSEL, DIR, MSIZE, PSIZE, MINC, PINC, CIRC, PL, interrupt enables
- Enable stream: set EN=1
- 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

2 Comments