当前位置: 首页 > news >正文

使用CubeMX新建DMA工程——存储器到外设模式

目录

1、新建板级支持包

1、usart.c:

2、修改的地方:在usart.c中添加了这些

3、usart.h:

4、在usart.h中添加了这些:

5、dma.c:

6、dma.h:

2、修改main.c文件

1、在main.c文件中添加头文件

2、添加外部变量声明

3、添加简单延时函数

4、添加应用代码

5、完整代码:

3、程序流程


新建工程的基本操作步骤参考这里:STM32CubeMX学习笔记(7)——DMA接口使用_cubemx dma-CSDN博客


1、新建板级支持包

LED板级支持包直接移植野火的,DMA板级支持包直接使用自动生成的代码在里面稍作修改

1、usart.c:

/* USER CODE BEGIN Header */
/********************************************************************************* @file    usart.c* @brief   This file provides code for the configuration*          of the USART instances.******************************************************************************* @attention** Copyright (c) 2025 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"/* USER CODE BEGIN 0 */
uint8_t SendBuff[SENDBUFF_SIZE];
/* USER CODE END 0 */UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_tx;/* USART1 init function */void MX_USART1_UART_Init(void)
{/* USER CODE BEGIN USART1_Init 0 *//* USER CODE END USART1_Init 0 *//* USER CODE BEGIN USART1_Init 1 *//* USER CODE END USART1_Init 1 */huart1.Instance = USART1;huart1.Init.BaudRate = 115200;huart1.Init.WordLength = UART_WORDLENGTH_8B;huart1.Init.StopBits = UART_STOPBITS_1;huart1.Init.Parity = UART_PARITY_NONE;huart1.Init.Mode = UART_MODE_TX_RX;huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;huart1.Init.OverSampling = UART_OVERSAMPLING_16;if (HAL_UART_Init(&huart1) != HAL_OK){Error_Handler();}/* USER CODE BEGIN USART1_Init 2 *//* USER CODE END USART1_Init 2 */}///重定向c库函数printf到串口DEBUG_USART,重定向后可使用printf函数
int fputc(int ch, FILE *f)
{/* 发送一个字节数据到串口DEBUG_USART */HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 1000);	return (ch);
}///重定向c库函数scanf到串口DEBUG_USART,重写向后可使用scanf、getchar等函数
int fgetc(FILE *f)
{int ch;HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, 1000);	return (ch);
}void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{GPIO_InitTypeDef GPIO_InitStruct = {0};if(uartHandle->Instance==USART1){/* USER CODE BEGIN USART1_MspInit 0 *//* USER CODE END USART1_MspInit 0 *//* USART1 clock enable */__HAL_RCC_USART1_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/**USART1 GPIO ConfigurationPA9     ------> USART1_TXPA10     ------> USART1_RX*/GPIO_InitStruct.Pin = GPIO_PIN_9;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);GPIO_InitStruct.Pin = GPIO_PIN_10;GPIO_InitStruct.Mode = GPIO_MODE_AF_INPUT;GPIO_InitStruct.Pull = GPIO_NOPULL;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* USART1 DMA Init *//* USART1_TX Init */hdma_usart1_tx.Instance = DMA1_Channel4;hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;hdma_usart1_tx.Init.Mode = DMA_NORMAL;hdma_usart1_tx.Init.Priority = DMA_PRIORITY_MEDIUM;if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK){Error_Handler();}__HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx);/* USER CODE BEGIN USART1_MspInit 1 *//* USER CODE END USART1_MspInit 1 */}
}void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{if(uartHandle->Instance==USART1){/* USER CODE BEGIN USART1_MspDeInit 0 *//* USER CODE END USART1_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_USART1_CLK_DISABLE();/**USART1 GPIO ConfigurationPA9     ------> USART1_TXPA10     ------> USART1_RX*/HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);/* USART1 DMA DeInit */HAL_DMA_DeInit(uartHandle->hdmatx);/* USER CODE BEGIN USART1_MspDeInit 1 *//* USER CODE END USART1_MspDeInit 1 */}
}/* USER CODE BEGIN 1 *//* USER CODE END 1 */

2、修改的地方:在usart.c中添加了这些

uint8_t SendBuff[SENDBUFF_SIZE];///重定向c库函数printf到串口DEBUG_USART,重定向后可使用printf函数
int fputc(int ch, FILE *f)
{/* 发送一个字节数据到串口DEBUG_USART */HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 1000);	return (ch);
}///重定向c库函数scanf到串口DEBUG_USART,重写向后可使用scanf、getchar等函数
int fgetc(FILE *f)
{int ch;HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, 1000);	return (ch);
}

3、usart.h:

/* USER CODE BEGIN Header */
/********************************************************************************* @file    usart.h* @brief   This file contains all the function prototypes for*          the usart.c file******************************************************************************* @attention** Copyright (c) 2025 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USART_H__
#define __USART_H__#ifdef __cplusplus
extern "C" {
#endif/* Includes ------------------------------------------------------------------*/
#include "main.h"/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes */extern UART_HandleTypeDef huart1;/* USER CODE BEGIN Private defines */
#define SENDBUFF_SIZE                     		1000//发送的数据量/* USER CODE END Private defines */void MX_USART1_UART_Init(void);/* USER CODE BEGIN Prototypes *//* USER CODE END Prototypes */#ifdef __cplusplus
}
#endif#endif /* __USART_H__ */

4、在usart.h中添加了这些:

#include <stdio.h>extern UART_HandleTypeDef huart1;#define SENDBUFF_SIZE                     		1000//发送的数据量

5、dma.c:

/* USER CODE BEGIN Header */
/********************************************************************************* @file    dma.c* @brief   This file provides code for the configuration*          of all the requested memory to memory DMA transfers.******************************************************************************* @attention** Copyright (c) 2025 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header *//* Includes ------------------------------------------------------------------*/
#include "dma.h"/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*----------------------------------------------------------------------------*/
/* Configure DMA                                                              */
/*----------------------------------------------------------------------------*//* USER CODE BEGIN 1 *//* USER CODE END 1 *//*** Enable DMA controller clock*/
void MX_DMA_Init(void)
{/* DMA controller clock enable */__HAL_RCC_DMA1_CLK_ENABLE();/* DMA interrupt init *//* DMA1_Channel4_IRQn interrupt configuration */HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);}/* USER CODE BEGIN 2 *//* USER CODE END 2 */

6、dma.h:

/* USER CODE BEGIN Header */
/********************************************************************************* @file    dma.h* @brief   This file contains all the function prototypes for*          the dma.c file******************************************************************************* @attention** Copyright (c) 2025 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __DMA_H__
#define __DMA_H__#ifdef __cplusplus
extern "C" {
#endif/* Includes ------------------------------------------------------------------*/
#include "main.h"/* DMA memory to memory transfer handles -------------------------------------*//* USER CODE BEGIN Includes *//* USER CODE END Includes *//* USER CODE BEGIN Private defines *//* USER CODE END Private defines */void MX_DMA_Init(void);/* USER CODE BEGIN Prototypes *//* USER CODE END Prototypes */#ifdef __cplusplus
}
#endif#endif /* __DMA_H__ */

2、修改main.c文件

1、在main.c文件中添加头文件

#include "stm32f1xx.h"
#include "./led/bsp_led.h"

2、添加外部变量声明

extern UART_HandleTypeDef huart1;
extern DMA_HandleTypeDef hdma_usart1_tx;
extern uint8_t SendBuff[SENDBUFF_SIZE];

3、添加简单延时函数

static void Delay(__IO uint32_t nCount)	 //简单的延时函数
{for(; nCount != 0; nCount--);
}

4、添加应用代码

int main(void)
{/* USER CODE BEGIN 1 */uint16_t i;/* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_DMA_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 *//* 配置RGB彩色灯 */LED_GPIO_Config();printf("\r\n USART1 DMA TX 测试 \r\n");/*填充将要发送的数据*/for(i=0;i<SENDBUFF_SIZE;i++){SendBuff[i]	 = 'A';}/* USART1 向 DMA发出TX请求 */HAL_UART_Transmit_DMA(&huart1, (uint8_t *)SendBuff ,SENDBUFF_SIZE);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* 此时CPU是空闲的,可以干其他的事情 */  //例如同时控制LEDLED1_TOGGLEDelay(0xFFFFF);/* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}

5、完整代码:

/* USER CODE BEGIN Header */
/********************************************************************************* @file           : main.c* @brief          : Main program body******************************************************************************* @attention** Copyright (c) 2025 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stm32f1xx.h"
#include "./led/bsp_led.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
extern UART_HandleTypeDef huart1;
extern DMA_HandleTypeDef hdma_usart1_tx;
extern uint8_t SendBuff[SENDBUFF_SIZE];
static void Delay(__IO uint32_t nCount); /* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*** @brief  The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 */uint16_t i;/* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_DMA_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 *//* 配置RGB彩色灯 */LED_GPIO_Config();printf("\r\n USART1 DMA TX 测试 \r\n");/*填充将要发送的数据*/for(i=0;i<SENDBUFF_SIZE;i++){SendBuff[i]	 = 'A';}/* USART1 向 DMA发出TX请求 */HAL_UART_Transmit_DMA(&huart1, (uint8_t *)SendBuff ,SENDBUFF_SIZE);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* 此时CPU是空闲的,可以干其他的事情 */  //例如同时控制LEDLED1_TOGGLEDelay(0xFFFFF);/* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}static void Delay(__IO uint32_t nCount)	 //简单的延时函数
{for(; nCount != 0; nCount--);
}/*** @brief System Clock Configuration* @retval None*/
void SystemClock_Config(void)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//*** @brief  This function is executed in case of error occurrence.* @retval None*/
void Error_Handler(void)
{/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */__disable_irq();while (1){}/* USER CODE END Error_Handler_Debug */
}#ifdef  USE_FULL_ASSERT
/*** @brief  Reports the name of the source file and the source line number*         where the assert_param error has occurred.* @param  file: pointer to the source file name* @param  line: assert_param error line source number* @retval None*/
void assert_failed(uint8_t *file, uint32_t line)
{/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

3、程序流程

1、HAL库函数初始化

2、系统时钟初始化

3、GPIO  DMA  USART  LED  初始化

4、打印提示信息

5、填充将要发送的数据

6、USART1向DMA发出TX请求,开始DMA数据传输

7、同时CPU控制LED翻转

相关文章:

  • 梳理顶会论文相关的经验贴
  • L3-040 人生就像一场旅行
  • 目标文件的段结构及核心组件详解
  • C++/SDL 进阶游戏开发 —— 双人塔防(代号:村庄保卫战 17)
  • 【Shell 脚本编程】详细指南:第三章 - 运算符与条件判断
  • PostgreSQL:pgAdmin 4 使用教程
  • tiktok web X-Bogus X-Gnarly 分析
  • 【dify—8】Agent实战——占星师
  • L3-041 影响力
  • 艺华直播 5.0 |专注于提供港澳台及央视频道的电视直播应用,加载快,播放流畅
  • PMP-第九章 项目资源管理(一)
  • 嵌入式产品运行中数据丢失怎么办?
  • ES6异步编程中Promise与Proxy对象
  • centos7 离线安装python3 保留python2
  • 使用Set和Map解题思路
  • 25.4.30数据结构|并查集 路径压缩
  • 企业经营系统分类及功能详解
  • Java状态机实战:打造高扩展性的订单流程引擎(含源码详解与快照设计)
  • Memory Bank 不够用?Cline 全新 CRCT:省 token,依赖关系自行追踪
  • OpenGL-ES 学习(13) ---- Shader 编译和程序对象
  • 思政课也精彩,“少年修齐讲堂”开讲《我的中国“芯”》
  • 中国武术协会原主席张耀庭逝世,曾促成电影《少林寺》拍摄
  • 欢迎回家!神十九返回舱成功着陆
  • 美国参议院投票通过戴维·珀杜出任美国驻华大使
  • 国务院安委会对辽宁辽阳一饭店重大火灾事故查处挂牌督办
  • 发挥全国劳模示范引领作用,加速汽车产业电智化转型