Configuring DMA: moving data without the CPU
About this lesson
DMA, direct memory access, is a second engine on the chip that copies data between memory and a peripheral by itself. On an STM32F407 (the STM32F4DISCOVERY board) we send "Hello" out of USART2, first by polling, which keeps the CPU waiting 875,000 clock ticks, then by setting up DMA1 stream 6 by hand: four registers (PAR, M0AR, NDTR, CR), one bit in USART2, and EN. The lessons “The memory map: flash, RAM and peripherals” and “Peripheral registers and bit masks” explain the addresses and masks used here, and the Hardware lesson “Interrupts and I/O” compares polling, interrupts and DMA in general.
Without DMA, the CPU waits
To send "Hello", the CPU puts one byte in USART2's data register, then checks again and again until it has gone: polling. At 9600 baud a byte takes about 1.04 ms, so five bytes keep a 168 MHz CPU waiting about 875,000 clock ticks.
polling: the CPU copies every byte itself
for each of the 5 bytes of "Hello":
wait until DR is empty (TXE = 1)
USART2->DR = byte;
one byte 10 bits at 9600 baud = 1.04 ms
5 bytes 5.21 ms
CPU 168 MHz x 5.21 ms = 875,000 ticks spent waitingRead the lesson step by step
Without DMA, the CPU waits
To send "Hello", the CPU puts one byte in USART2's data register, then checks again and again until it has gone: polling. At 9600 baud a byte takes about 1.04 ms, so five bytes keep a 168 MHz CPU waiting about 875,000 clock ticks.
polling: the CPU copies every byte itself for each of the 5 bytes of "Hello": wait until DR is empty (TXE = 1) USART2->DR = byte; one byte 10 bits at 9600 baud = 1.04 ms 5 bytes 5.21 ms CPU 168 MHz x 5.21 ms = 875,000 ticks spent waitingDMA: a copier beside the CPU
DMA, direct memory access, is a small engine on the chip that copies data by itself. The CPU tells it where the data is, where it goes and how many items. Then the CPU is free until DMA raises an interrupt.
DMA controller: copies data without the CPU the CPU tells it, once: where the data is (a memory address) where it goes (a peripheral address) how many items (a count) then DMA moves them and raises an interrupt at the endOne stream, one channel
The STM32F407 has 2 DMA controllers with 8 streams each. A fixed table wires each request, a peripheral's call for data, to a stream and channel: USART2 transmit is DMA1, stream 6, channel 4. Its registers are memory addresses, starting at 0x400260A0.
STM32F407: DMA1 and DMA2, 8 streams each USART2_TX -> DMA1, stream 6, channel 4 DMA1_Stream6 registers (base 0x400260A0): CR 0x400260A0 NDTR 0x400260A4 PAR 0x400260A8 M0AR 0x400260AC in C: #define S DMA1_Stream6 (a pointer to them)PAR: where bytes go
Line 1 writes PAR, the peripheral address register. It holds the address of USART2's data register, 0x40004404. Every byte will be written there. S is our short name for DMA1_Stream6, a pointer to the stream's registers.
line 1 S->PAR = (uint32_t)&USART2->DR; USART2 base 0x40004400, DR at +4 PAR = 0x40004404 (fixed: every byte goes here) written at 0x400260A8M0AR: where bytes come from
Line 2 writes M0AR, the memory address register: where msg starts in SRAM. Say the linker put it at 0x20000000. DMA reads the first byte, H, from there.
line 2 S->M0AR = (uint32_t)msg; msg "Hello" in SRAM, say at 0x20000000 0x20000000 H 0x20000001 e 0x20000002 l 0x20000003 l 0x20000004 o M0AR = 0x20000000NDTR: how many
Line 3 writes NDTR, the number of data items: 5, one for each letter of Hello. NDTR counts down by one after every byte moved. At 0 the stream stops by itself.
line 3 S->NDTR = 5; NDTR = 5 items (bytes here) counts 5, 4, 3, 2, 1, 0 at 0 the transfer is completeCR: how to copy
Line 4 sets the control register in one go: 0x08000450. Bits 27 to 25 hold 4: channel 4. MINC, bit 10, steps through memory. DIR = 01, bits 7 to 6, means memory to peripheral. TCIE, bit 4, asks for an interrupt.
line 4 S->CR = 0x08000450; CHSEL bits 27..25 = 4 0x08000000 channel 4 MINC bit 10 = 1 0x00000400 next byte, next address DIR bits 7..6 = 01 0x00000040 memory -> peripheral TCIE bit 4 = 1 0x00000010 interrupt when done sizes bits 14..11 = 0 bytes EN bit 0 = 0 not started yet sum 0x08000450Let USART2 ask for bytes
Line 5 sets DMAT, bit 7 of USART2's CR3 register. Now, whenever its data register is empty, USART2 sends a request to DMA, instead of waiting for the CPU to notice.
line 5 USART2->CR3 |= USART_CR3_DMAT; DMAT bit 7 of CR3 = 1 DR empty -> request to DMA1 stream 6, channel 4Enable: the bytes move alone
Line 6 sets EN, bit 0 of CR, and the stream starts. Each request moves one byte from SRAM into the data register, and NDTR counts 5, 4, 3, 2, 1, 0. Meanwhile the CPU is free to run other code.
line 6 S->CR |= DMA_SxCR_EN; CR = 0x08000451 each request from USART2: 0x20000000 'H' -> DR NDTR 4 0x20000001 'e' -> DR NDTR 3 0x20000002 'l' -> DR NDTR 2 0x20000003 'l' -> DR NDTR 1 0x20000004 'o' -> DR NDTR 0 CPU: free the whole timeDone: one interrupt
At NDTR = 0, DMA sets the TCIF6 flag and interrupt 17 runs DMA1_Stream6_IRQHandler. The handler clears the flag through HIFCR. The CPU wrote 6 registers instead of waiting about 875,000 ticks.
NDTR = 0 -> the stream stops, EN reads 0 again -> TCIF6 (bit 21 of DMA1->HISR) = 1 TCIE = 1 -> interrupt 17: DMA1_Stream6_IRQHandler handler DMA1->HIFCR = DMA_HIFCR_CTCIF6; (bit 21) cost 6 register writes + one short handler vs 875,000 ticks of polling
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Sending "Hello" at 9600 baud by polling, about how long does the CPU spend waiting?
Each byte is 10 bits on the wire, so it takes 10 / 9600 s ≈ 1.04 ms. Five bytes take about 5.2 ms: 875,000 ticks of a 168 MHz clock.
02In a memory-to-peripheral transfer, which register holds the address the bytes are written to?
PAR, the peripheral address register, holds the destination: USART2's data register at 0x40004404. M0AR holds the source in memory and NDTR the count.
03NDTR was set to 5. What does it read after three bytes have been moved?
NDTR counts the items still to move. It goes down by one after every byte: 5, 4, 3, then 2 after the third. At 0 the transfer is complete.
04Why does the control word 0x08000450 set MINC (bit 10) but not PINC?
MINC moves the memory address on after each byte, so DMA walks through H, e, l, l, o. The peripheral address must stay fixed: every byte goes into the same USART2 data register.
05Once EN is set, what does the CPU do while the five bytes are sent?
USART2 asks DMA for each byte (the DMAT bit) and DMA moves it. The CPU only hears about it once, when TCIF6 fires the DMA1_Stream6 interrupt.
Glossary
- DMA (direct memory access)
- A controller on the chip that copies data between memory and peripherals by itself, once the CPU has told it where from, where to and how many.
- Polling
- Checking a status flag again and again until a device is ready. Simple, but the CPU can do nothing else meanwhile.
- Stream and channel
- On the STM32F407 each DMA controller has 8 streams; a channel number picks which peripheral request a stream answers. USART2 transmit is DMA1 stream 6, channel 4.
- PAR, M0AR, NDTR
- A stream's peripheral address, memory address and number of data items still to move.
- Control register (CR)
- The stream's settings in one 32-bit word: channel, direction, address increments, sizes, interrupts, and EN to start it.
- DMA request
- A signal from a peripheral asking DMA for the next item; USART2 sends one when its data register is empty and DMAT is set.
- Transfer-complete interrupt
- The interrupt DMA raises when NDTR reaches 0 (flag TCIF), so the CPU learns the job is done.
Notes and sources
- The card shows the six set-up lines. The whole function also turns on the DMA1 clock (bit 21 of RCC->AHB1ENR), clears USART2's TC flag by writing 0 to it, and enables interrupt 17 in the NVIC. USART2 itself is set up as in ST's STM32F4-Discovery example: 9600 baud, 8 data bits, no parity, one stop bit.
- 0x20000000 is the start of SRAM and only an example: the linker chooses where msg goes. msg is not const, so it sits in SRAM.
- 875,000 = 168 MHz x 5 bytes x 10 bits / 9600 baud. Each byte is 10 bits on the wire (start bit, 8 data bits, stop bit). The clock is the 168 MHz that ST's Discovery example sets up.
- Transfer complete means DMA has handed the last byte to the data register, not that it has left the pin: it still needs about 1.04 ms on the wire. Wait for USART2's own TC flag before turning the port off.
- The stream registers can only be changed while EN is 0. To send again: wait for EN to read 0, clear the flags, write NDTR again (it is now 0) and set EN. Bytes and direct mode (no FIFO) are the reset settings, so line 4 leaves them at 0.
- ST RM0090 Reference manual, STM32F405/415, STM32F407/417, STM32F427/437, STM32F429/439 (DMA chapter, Table 42: DMA1 request mapping)
- ST CMSIS device header stm32f407xx.h (addresses and bit positions)
- ST STM32CubeF4, STM32F4-Discovery UART_TwoBoards_ComDMA example: main.h (USART2 TX = DMA1 stream 6, channel 4)
- ST STM32CubeF4, same example: main.c (168 MHz clock, 9600 baud 8N1)
- ST STM32F4 HAL DMA driver header (directions, channels, NDTR)
- ST STM32F4 HAL DMA driver source (PAR, M0AR, NDTR set-up and flag clearing)
- ST STM32F4 HAL UART driver source (HAL_UART_Transmit_DMA: DR, TC, DMAT)
- J. W. Valvano, Embedded Systems: Shape the World, ch. 11: Serial interfacing (UART frames, busy-wait output)
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 36: I/O Devices (DMA)
Quiz Unlocks after the last step.