In this tutorial, I will show you how to use DMA mode with UART on STM32 Microcontroller.
You will learn,
- How to Create project using STM32CubeMX.
- Import and Edit Project in STM32CubeIDE
- Enable and use DMA with UART on STM32 Microcontroller.
STM32F334 Features
- Core: Arm® Cortex®-M4 32-bit CPU with FPU (Floating Point Unit) up to 72 MHz.
- Memory: Up to 64 KB Flash, 12 KB SRAM.
- Analog: Fast 12-bit ADCs (5 Msps), Comparators, Op-Amp, DACs.
- Timers: High-resolution Timer (HRTIM), Motor Control Timer, General-purpose Timers.
- Communication: I2C, SPI, up to 3 USARTs, CAN.
- Power: Comprehensive power-saving modes.
- Core: Arm® Cortex®-M4 32-bit CPU with FPU (Floating Point Unit) up to 72 MHz.
- Memory: Up to 64 KB Flash, 12 KB SRAM.
- Analog: Fast 12-bit ADCs (5 Msps), Comparators, Op-Amp, DACs.
- Timers: High-resolution Timer (HRTIM), Motor Control Timer, General-purpose Timers.
- Communication: I2C, SPI, up to 3 USARTs, CAN.
- Power: Comprehensive power-saving modes.
What is Direct Memory Access (DMA)?
DMA is a technology that allows hardware devices to transfer data between themselves and memory without involving CPU. It increases CPU performance by offloading data transfer tasks from CPU, Enabling it to focus it on other critical operations.
DMA can be considered as traffic controller for data transfers in and out of memory. It efficiently manages these transfers freeing CPU for more complex tasks.
Advantages of DMA,
- Increases efficiency by reducing load on CPU,
- Real-Time responsiveness,
- Reduces power usages,
- High throughput and Speed,
- Non-Blocking operation,
- Power saving
Prerequisite
Before proceeding further make sure you have installed STM32CubeIDE and STM32CubeMX. Links to download both are as below,
Hardware Used
In this tutorial we are using below hardware,
Code
uint8_t txbuf[] = "Hi from EmbeTronics!!";
uint8_t rxbuf[256] = "";
HAL_UART_Transmit_DMA(&huart1, txbuf,strlen((char *)txbuf));
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(HAL_UARTEx_ReceiveToIdle_DMA(&huart1, rxbuf, 256) == HAL_OK)
{
HAL_UART_Transmit_DMA(&huart1, rxbuf,strlen((char *)rxbuf));
while(HAL_DMA_GetState(&hdma_usart1_tx) == HAL_DMA_STATE_BUSY);
memset(rxbuf,0,256);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
Explanation of Code
Let us understand blocks of code and see how it works.
First we have defined array variables for transmitting and receiving data from UART.
uint8_t txbuf[] = "Hi from EmbeTronics!!";
uint8_t rxbuf[256] = "";HAL_UART_Transmitt_DMA function transmits data from txbuf on UART using DMA Mode.
HAL_UART_Transmit_DMA(&huart1, txbuf,strlen((char *)txbuf));Below codes runs in infinite loop and waits for data to be received from UART in DMA mode and Transmits same data bak to host using DMA mode with UART.
if(HAL_UARTEx_ReceiveToIdle_DMA(&huart1, rxbuf, 256) == HAL_OK)
{
HAL_UART_Transmit_DMA(&huart1, rxbuf,strlen((char *)rxbuf));
while(HAL_DMA_GetState(&hdma_usart1_tx) == HAL_DMA_STATE_BUSY);
memset(rxbuf,0,256);
}Full Project Code
You can directly download and use whole project from below GitHub link.
GitHub Repo Link
Working Of Code
Below video shows working of this project. Code at boot up will send "Hi from EmbeTronics!!", then it will wait for data to be received from UART and will send back received data.
Wrapping Up
In todays tutorial, You have learned how to use DMA with UART. Using DMA with UART reduces load on CPU and increases performance.
Please leave a comment if you have a question or you found this tutorial helpful.
0 Comments