Using the DMA Controller on STM32 Devices

This tutorial shows how to use the DMA controller on the STM32 devices, letting it perform background memory operations without consuming any CPU cycles. We will show how to use DMA to copy data between different buffers in RAM and also between RAM and the peripherals.

Before you begin, install VisualGDB 5.4 or later.

  1. Start Visual Studio and open the VisualGDB Embedded Project Wizard:
  2. On the first page of the wizard select “Embedded Binary -> MSBuild” and press “Next” to proceed to the next page:
  3. On the Device Selection page pick the ARM toolchain and select your device from the list. In this tutorial we will use the Nucleo-F410RB board, so we select the STM32F410RB device, however the steps shown in this tutorial will work for most of the other STM32 devices as well:
  4. In this tutorial we will add the DMA-related code from scratch, so select the most simple LEDBlink (HAL) tutorial on the next page:
  5. Finally, choose the debug settings that will work with your board. For most of the STM32 Discovery and Nucleo boards, simply connect the board to a USB port and VisualGDB will detect the settings automatically. Once the debug settings are configured, press “Finish” to generate a basic project.
  6. Once the project has been created, ensure you can debug and and take a note of the SystemCoreClock variable after the call to HAL_Init() returns:
  7. One last step before we begin experimenting with various DMA modes would be to enable the Chronometer on the Embedded Debug Tweaking page of VisualGDB Project Properties. It will automatically record the CPU cycles elapsed between different debug events, making it easier to understand the timings:Warning: Ensure you enable the Chronometer for both Debug and Release configurations, as we will be using both of them in this tutorial.
  8. Now we will create a basic program that will run 3 memory-intense operations:
    • Fill a memory buffer with a sequence of numbers: Ni = i * 3.
    • Copy the contents of the buffer to another buffer.
    • Fill the third buffer with the first 1024 Fibonacci numbers.

    Then we will and will then show how using the DMA improves its performance. Replace the main() function in your project with the following code:

  9. To minimize the impact of unoptimized code on the measured numbers, switch the active configuration to Release, then set a breakpoint at the call to FillMemory() and start debugging:
  10. Once the breakpoint hits, step over the FillMemory(), CopyMemory() and CalculateFibonacci() calls and check the Chronometer window for the timings:In this example, filling the first and the third buffer took the exactly the same time, while copying the first buffer to the second one took slightly less time:
  11. While the DMA cannot be used to compute Fibonacci numbers, or initialize arrays with non-constant values, it can be used for copying data between 2 memory locations. We will now demonstrate this by replacing the CopyMemory() function with the following one:

    The function above enables the DMA controller #2 (according to the STM32F410RB documentation, DMA#1 cannot be used for memory-to-memory transfers), and performs a single memory-to-memory transfer operation between s_Buffer1 and s_Buffer2.

  12. Run the new program and step over the function calls in main() to obtain the updated timings:Using DMA instead actually required more time than calling memcpy(), due to the initial setup (20 uS extra), however the CPU spent most of that time looping insideĀ HAL_DMA_PollForTransfer() waiting for the DMA transfer to finish.
  13. As the DMA transfers do not actually involve the CPU, we can easily change the program to compute the Fibonacci numbers in parallel with the DMA transfer:

     

  14. Run the modified version of the program and observe the new timings:
  15. Now the DMA operation ran in parallel with the CalculateFibonacci() function, reducing the overall program time by 21%:
  16. The DMA can be especially useful to optimize data transfers between the memory and various on-chip peripherals. E.g. we can modify the example above to output the contents of s_Buffer1 via the UART interface. Before we can do that, add the following function to your program to initialize the on-board UART peripheral:

    Then call it from main() and try outputting a test string by calling HAL_UART_Transmit():

    Note that if you are using a different board than Nucleo-STM32F410RB, you may need to use a different UART (the one that is actually connected to the on-board ST-Link’s COM port) and different GPIO pins. You can find out the UART/GPIO configuration for your board by cloning one of the ST’s UART examples via the VisualGDB Embedded Project Wizard.

  17. Verify that the “test” output is printed to the COM port:
  18. If you step into the HAL_UART_Transmit() function, you will see that the data is sent to the UART peripheral by writing it byte-by-byte into the UART2->DR register:
  19. The DMA could do exactly that – copy a given buffer to the UART1->DR register byte-by-byte, although there would be several important differences compared to the memory-to-memory operation:
    1. We cannot use any arbitrary DMA controller. Instead, we need to pick the controller, stream and channel that are connected to the UART2 TX function. This will ensure that the DMA controller will not start transferring another byte until the UART controller is ready to accept it (i.e. has finished physically transmitting the previous one).
    2. The DMA mode will need to be changed from DMA_MEMORY_TO_MEMORY to DMA_MEMORY_TO_PERIPH.
    3. Unlike the memory-to-memory transfer, where we need to move the write pointer after each transferred word (to avoid overwriting the previous one), the memory-to-UART transfers should always end up at the same address (address of UART1->DR). This is achieved by changing DMA_PINC_ENABLE to DMA_PINC_DISABLE.
    4. Because the UART transfers one byte at a time, both peripheral and memory data alignment need to be set at BYTE instead of WORD.

    You can find out the DMA stream and channel connected to the UART peripheral in the DMAx request mapping section of your STM32 device’s reference manual (not datasheet):

    The updated CopyMemoryWithDMA() function outputing the buffer contents to UART will look as follows:

  20. Run the new code and verify that the data transferred to the COM port matches the contents of s_Buffer1:
  21. We can also replace the call to HAL_DMA_Start() that manually specifies the address of USART2->DR and the manual update of the USART2->CR3 register with a higher-level call to HAL_UART_Transmit_DMA() that will do the necessary setup automatically:
  22. Finally, we will show how to use the DMA in a scenario that is often impossible with CPU-driven data transfers – generating (or capturing) uninterrupted streams of data. We will update our example to continuously output the stream of Fibonacci numbers to UART without making any breaks by handling the DMA half-transfer interrupts. The DMA controller will be used to repeatedly transfer the same buffer to the UART peripheral over and over, while the CPU will be computing the next batch of values and placing them into the part of the buffer that has already been transferred:
  23. To do this, change the DMA mode assigned to s_DMAHandle.Init.Mode from DMA_NORMAL to DMA_CIRCULAR, enable the interrupt for the DMA channel you are using (in this example, by calling HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn)) and add the following handlers to your main source file:
  24. Set a breakpoint in HAL_UART_TxHalfCpltCallback(), let it trigger and check the call stack:As the call stack shows, the DMA controller has raised an IRQ that invoked the DMA1_Stream6_IRQHandler() we created. In turn, our IRQ handler invoked the standard HAL_DMA_IRQHandler() function that sorted it out as a “Half of DMA buffer transferred” event, invoking the corresponding handler in the UART driver that finally called our HAL_UART_TxHalfCpltCallback() function.
  25. We can now modify the HAL_UART_TxHalfCpltCallback() and HAL_UART_TxCpltCallback() to fill the second and first half of s_Buffer1 respectively with the next batch of Fibonacci numbers. First of all, add an UpdateFibonacci() function shown below:

    Then, update the UART callbacks as follows:
  26. Now you can run the final version of the project and observe an continuous stream of numbers being sent to the COM port: