SRAM msg at 0x20000000stream 6 registersCPUwaiting...freein handlerDMA1stream 6[0]H[1]e[2]l[3]l[4]oUSART2 DRPAR0M0AR0NDTR0CR0terminal1S->PAR = (uint32_t)&USART2->DR;2S->M0AR = (uint32_t)msg;3S->NDTR = 5;4S->CR = 0x08000450;5USART2->CR3 |= USART_CR3_DMAT;6S->CR |= DMA_SxCR_EN;!Hello
Back: the STM32F407's CPU, DMA1 and its stream 6 registers, SRAM, USART2 and a terminalFront: the six set-up linesTokens: bytes, register values, the USART2 request and the interruptAn STM32F407 sends "Hello" out of USART2. Polling, the CPU copies every byte and waits 875,000 clock ticks. Six register writes set up DMA1 stream 6: PAR = 0x40004404 (where), M0AR = 0x20000000 (from), NDTR = 5 (how many), CR = 0x08000450 (how), DMAT in USART2, then EN. The bytes then move by themselves while NDTR counts down to 0, and one interrupt says it is done.

Why DMA

  1. 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.

Set it up by hand

Let it run

What happens

;; step 1/10 · poll
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 waiting

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. 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.