Library 55- Extend USART with TX DMA

Here is a great feature for USART. I’ve made an extension library for USART (and for SPI will come soon) to use DMA for TX data using USART. This can be very handy to use if you have a lot of work and not so many time using your STM32F4 device.

I was thinking first about adding DMA RX functionality for USART too. But then I realize that my USART library by default uses RXNE (RX Not Empty) interrupt and that data is transfered to internal USART’s library buffers. Also, some additional logic and interrupt handlers would be required and this will make library very unefficient.



  • Extension library for TM USART library
  • Supports DMA TX functionality
  • Works with any USART from USART1 to UART8
  • Supports changeable stream and channel settings


    • STM32F4xx
    • STM32F4xx DMA
  • TM
    • TM USART
    • TM DMA
    • defines.h

Stream and channel settings

STM32F4xx devices have 2 DMA controllers. Each DMA controller has 8 DMA streams where each stream has 8 DMA channels for different peripherals available.

To get all available DMA peripherals, you should take a STM32F4xx Reference manual (1700+ pages) and take a look at DMA section. There are all available streams and channels for different peripheral.

This library uses only USART TX DMA. Default DMA streams and channels are in table below:

USART DMA DMA Stream  DMA Channel
USART1 DMA2 DMA Stream 7 DMA Channel 4
USART2 DMA1 DMA Stream 6 DMA Channel 4
USART3 DMA1 DMA Stream 3 DMA Channel 4
UART4 DMA1 DMA Stream 4 DMA Channel 4
UART5 DMA1 DMA Stream 7 DMA Channel 4
USART6 DMA2 DMA Stream 6 DMA Channel 5
UART7 DMA1 DMA Stream 1 DMA Channel 5
UART8 DMA1 DMA Stream 0 DMA Channel 5

Some USARTs allows different streams and channels. This can be handy if you have 2 peripherals on the same stream and DMA and you want to enable DMA for both. You can’t do that because only one channel on specific stream can be used at a time. For that purpose, someking of “remapping” was enabled which allows you to select custom Stream and Channel for specific USART if it is available. Always look for STM32F4xx Reference manual for that settings.



Below example works similar than example for my USART library except it uses DMA to return received string from terminal back to user.

STM32F4xx USART DMA TX functionality

STM32F4xx USART DMA TX functionality

Project is available on my Github account, download library below.



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!
  • Pingback: All STM32F4 libraries - STM32F4 Discovery()

  • Bublo

    Hi Tilen, in your last release of USART_DMA library on line 126 is called TM_DMA_ClearFlags function, which has no implementation in your code. Previously was there DMA_DeInit call. It is only written mistake?

    • You need TM DMA library.
      It is in dependencies section.

      • Bublo

        Ahhh, ok, I am sorry, mea culpa 🙂

  • Fabián

    What is the deal with using DMA for transmission if we should wait untill it ends?
    Wouldn’t be a better aproach to use interrupts?

    • Line 44 says. And yes, lib supports interrupts also.

  • kralizec

    Hi, I am trying to use your usart_dma library with HAL drivers and failing… do you plan to publish a lib that uses HAL drivers?

    • Im not planning this,since enabling dma on hal is pretty straight forward.

      • kralizec

        Yeah… I must be really bad in embedded programming then… do you suggest to start from your hal based usart library or ditch that also?

        • Download cube examples. There is at least 10 examples using dma and uart. Check it.

          • kralizec

            forgot to reply, I followed your advise and did it, thank you very much 😀