ENع
←DMA by hand
Lesson 36 · Registers and the memory map

Configuring DMA: moving data without the CPU

12 min10 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 36 · Interactive
01 / 10

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 waiting
Loading figure…
or ← →
Read the lesson step by step
  1. 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 waiting
  2. DMA: 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 end
  3. One 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)
  4. 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 0x400260A8
  5. M0AR: 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     = 0x20000000
  6. NDTR: 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 complete
  7. CR: 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                      0x08000450
  8. Let 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 4
  9. Enable: 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 time
  10. Done: 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

Quiz Unlocks after the last step.