Hello everyone, In this article we will talk about USART communication on STM32 with CubeMx . First we need to have a STM32Fx series microprocessor or a developer board that has a STM32Fx microprocessor.

First we need to start a CubeMx project and select our board. And configure our board for USART communicating with DMA.

Click New > Project and from new screen we need to select our microprocessor. I have STM32F051 Discovery board and I selected it.

First, I have selected Connectivity section on the left panel and and activated USART as Asyncronus . And then I have checked the pins and they are were not using with anything else. The PA10 pin is addicted to USART1_RX and PA9 pin addicted to USART1_TX. This means our microprocessor will use PA10 pin to Receive USART data; PA9 pin to Transmit USART data.

After selecting the pins now we will configure USART DMA settings.

Click on DMA Settings. Add USART1_RX and USART1_TX DMA channels. Select both of this DMA objects and set the Mode as Circular, Data Width as Byte. Now we have setted DMA will send and receive datas byte by byte. Also after completing the data transmission it will start again due to Circular Mode.

Pay Attention !!!

Also make sure not to activate USART1 interrupt. Cubemx will be already activated DMA. If both interrupt and DMA will be activated our program will not be working as we want.

After making DMA configuration now we will make our USART configuration. Below picture has my project configuration and you can set your own configuration values for your project.

Now we are ready to generate our code. I have generated for uVision Keil.

Let’s take a look at codes now:

Normally Cube API has HAL_UART_DMA_Stop function but it stops receiving and transmiting together. In here we will just need to stop Transmitting process. Because sometimes maybe when we are transmitting datas we can receive another data. For keeping these datas we have just created the same function of HAL_UART_DMA_Stop and removed Receiving stop section. In program we will use this function. There are also two different functions. These functions will call when Transmitting and receiving process when held.

After configuration functions we need to start DMA receiving and wait for the data coming.

In while section we can build our own program has receiving and transmiting processes.

Now we are ready to make use USART DMA in STM32 CubeMx.

I have builded this project using stm32f051 but in CubeMx Api library methods are the same. So we can use this in STM32F4 series or what serias what we want. Do as I do and have enjoy with USART DMA.

That all for this article. Thank you for reading me.

Good coding for everyone.

Burak Hamdi TUFAN.


Please enter your comment!
Please enter your name here