Library 63- DMA for STM32F4xx

Having each stream on separate bits for checking flags has really annoying for me! I had to made a library for DMA which will handle all that stuff any maybe a little more for me because it is really stressfull, when I have to change DMA stream. Then you have to change interrupt handler function name, all flag bit locations and so on. Too much work for easily fail and bug is here!

To enable/disable interrupts, get/clear flags, you have to pass only which stream and which flag for transfer complete, error, half complete….



  • Easy checking and clearing DMA interrupt flags
  • Easy interrupt handling for DMA stream
  • Enable/Disable interrupts


    • STM32F4xx
    • STM32F4xx DMA
    • STM32F4xx MISC
  • TM
    • defines.h
    • attributes.h

Interrupt handling

Library has implemented all DMA stream interrupt handlers for easy interrupt handling for user. The problem happen when some libraries from me also uses interrupt handler internally.

One example is FATFS library. DMA for SDIO is DMA2_Stream3 and function DMA2_Stream3_IRQHandler() is also implemented in my DMA and FATFS library. If you use both libs, error will happen. To prevent this errors I’ve added support to disable interrupt handler inside DMA library from me. To disable it, open defines.h file and add defines:

Functions and enumerations


DMA result with IRQ

DMA result with IRQ

Project is available on my Github, download library below.

TM STM32F4 DMA Library


Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Thanut Tosanguan

    Thank you for a great site to learn coding for STM32.

    I am new to ARM and STM32F4, so I am wondering if you will be doing a tutorial on how to use DMA for motor control or modulate PWM. I have been looking at the example code for STM32F1xx (, but could not get it to work so far.

    I need to generate a fixed digital signal with 61 bits at a rate of 1-5 Mbits/sec to control an operation of another IC. I would really appreciate any help or suggestions you can give. Thank you.

    • pwm is not ok for you?
      I think you don’t need DMA here, but I may be wrong.

      • Thanut Tosanguan

        It is something that I have used on PIC32MX where the DMA is used to update the pulse widths of PWM which are stored in an array. I saw something similar in the motor control example for STM32 that I mentioned.

        I would prefer it this way because I will have a lot of things going on at the same time. But I guess it is possible to use a timer handler to update the pulse width each time. Which would you suggest?

        Also, I find ARM’s DMA structure quite complicated with the different Stream/Channel for different Timer. Compared to PIC32, I would have found ARM programming to be very tedious if not for your libraries.

        • Well, there is no ARM structure. It’s structure for DMA from ST and this structure is really strait-forward one 🙂
          So, what exactly are your problems here with this structure?
          For detailed things, you can write to me to my email.

          • Thanut Tosanguan

            Tried e-mailing but failed so I sent a message through the About page.

  • Kyaw Kyaw Aung

    Hi, TM. Is there a tutorial on how to use DMA for ADC and DAC in STM32F4 Discovery?? Your tutorials and libraries are good to follow. But I can’t figure out how to use DMA with your ADC, DAC libraries. I’m fairly new to this so I’d appreciate your help so much. Thanks again and have a nice day, sir!

    • Hi,

      For ADC I dont have it, but for dac, you can take a look at my signal library with DAC.

      • Kyaw Kyaw Aung

        Alright. I’ll definitely check it out. thanks for the reply, sir. 🙂

  • Anna Leshinsky

    Hi, do you have an example of DMA for SDIO with FatFs?

  • kk

    Hi Majerle, I tried to use your precompiler directives
    in defines.h and
    right at the beginning of main.cpp to define my own DMA handler but it just ain’t working. I receive errors stating “multiple definition of DMA2_Stream4_IRQHandler … tm_stm32f4_dma.c:296: first defined here”
    Is there any special order to include your files (especially in CooCox IDE)?

    • Good question. Code seems to work ok. Make sure proper defines.h file is included in case you have them multiple.