目錄
1、新建板級支持包
2、修改main.c
3、程序流程
4、問題
新建工程的基本操作步驟參考這里:
【【野火】STM32 HAL庫開發實戰指南 教學視頻 手把手教學STM32全系列 零基礎入門CubeMX+HAL庫,基于野火全系列STM32開發板】 https://www.bilibili.com/video/BV18X4y1M763/?p=37&share_source=copy_web&vd_source=6bf2ecf2bed1f4e4ccbce142a48bb318
1、新建板級支持包
LED的板級支持包直接移植,DMA外設就不新建板級支持包了,直接使用生成的代碼。
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 */
DMA_HandleTypeDef hdma_memtomem_dma1_channel1;/*** Enable DMA controller clock* Configure DMA for memory to memory transfers* hdma_memtomem_dma1_channel1*/
void MX_DMA_Init(void)
{/* DMA controller clock enable */__HAL_RCC_DMA1_CLK_ENABLE();/* Configure DMA request hdma_memtomem_dma1_channel1 on DMA1_Channel1 */hdma_memtomem_dma1_channel1.Instance = DMA1_Channel1;hdma_memtomem_dma1_channel1.Init.Direction = DMA_MEMORY_TO_MEMORY;hdma_memtomem_dma1_channel1.Init.PeriphInc = DMA_PINC_ENABLE;hdma_memtomem_dma1_channel1.Init.MemInc = DMA_MINC_ENABLE;hdma_memtomem_dma1_channel1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;hdma_memtomem_dma1_channel1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;hdma_memtomem_dma1_channel1.Init.Mode = DMA_NORMAL;hdma_memtomem_dma1_channel1.Init.Priority = DMA_PRIORITY_MEDIUM;if (HAL_DMA_Init(&hdma_memtomem_dma1_channel1) != HAL_OK){Error_Handler();}}/* USER CODE BEGIN 2 *//* USER CODE END 2 */
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 -------------------------------------*/
extern DMA_HandleTypeDef hdma_memtomem_dma1_channel1;/* 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、添加LED外設驅動頭文件
2、定義aSRC_Const_Buffer數組作為DMA傳輸數據源,?定義DMA傳輸目標存儲器aDST_Buffer
3、定義簡單延時函數Delay和比較函數Buffercmp
/* 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 "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 */
#define BUFFER_SIZE 32/* 定義aSRC_Const_Buffer數組作為DMA傳輸數據源const關鍵字將aSRC_Const_Buffer數組變量定義為常量類型 */
const uint32_t aSRC_Const_Buffer[BUFFER_SIZE]= {0x01020304,0x05060708,0x090A0B0C,0x0D0E0F10,0x11121314,0x15161718,0x191A1B1C,0x1D1E1F20,0x21222324,0x25262728,0x292A2B2C,0x2D2E2F30,0x31323334,0x35363738,0x393A3B3C,0x3D3E3F40,0x41424344,0x45464748,0x494A4B4C,0x4D4E4F50,0x51525354,0x55565758,0x595A5B5C,0x5D5E5F60,0x61626364,0x65666768,0x696A6B6C,0x6D6E6F70,0x71727374,0x75767778,0x797A7B7C,0x7D7E7F80};/* 定義DMA傳輸目標存儲器 */
uint32_t aDST_Buffer[BUFFER_SIZE];static void Delay(__IO uint32_t nCount);
uint8_t Buffercmp(const uint32_t* pBuffer, uint32_t* pBuffer1, uint16_t BufferLength); /* 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 *//* 定義存放比較結果變量 */uint8_t TransferStatus;/* 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 *//* LED 端口初始化 */LED_GPIO_Config(); /* 設置 RGB 彩色燈為紫色 */LED_PURPLE;/* 簡單延時函數 */Delay(0xFFFFFF);/* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_DMA_Init();/* USER CODE BEGIN 2 */HAL_DMA_Start(&hdma_memtomem_dma1_channel1,(uint32_t)aSRC_Const_Buffer,(uint32_t)aDST_Buffer,BUFFER_SIZE);/* 等待DMA傳輸完成 */while(__HAL_DMA_GET_FLAG(&hdma_memtomem_dma1_channel1,DMA_FLAG_TC1)==RESET){} /* 比較源數據與傳輸后數據 */TransferStatus=Buffercmp(aSRC_Const_Buffer, aDST_Buffer, BUFFER_SIZE);/* 判斷源數據與傳輸后數據比較結果*/if(TransferStatus==0) {/* 源數據與傳輸后數據不相等時RGB彩色燈顯示紅色 */LED_RED;}else{ /* 源數據與傳輸后數據相等時RGB彩色燈顯示藍色 */LED_BLUE;}/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}/* 簡單的延時函數 */
static void Delay(__IO uint32_t nCount)
{for(; nCount != 0; nCount--);
}
/*** 判斷指定長度的兩個數據源是否完全相等,* 如果完全相等返回1,只要其中一對數據不相等返回0*/
uint8_t Buffercmp(const uint32_t* pBuffer, uint32_t* pBuffer1, uint16_t BufferLength)
{/* 數據長度遞減 */while(BufferLength--){/* 判斷兩個數據源是否對應相等 */if(*pBuffer != *pBuffer1){/* 對應數據源不相等馬上退出函數,并返回0 */return 0;}/* 遞增兩個數據源的地址指針 */pBuffer++;pBuffer1++;}/* 完成判斷并且對應數據相等 */return 1;
}
/*** @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、定義存放比較結果的變量
2、HAL庫初始化,系統時鐘初始化,LED端口初始化、設置LED為紫色,DMA初始化
3、開啟DMA傳輸
4、等待DMA傳輸完成后,比較原數據與傳輸后的數據,如果不相同顯示紅色,否則藍色。
4、問題
1、移植野火的代碼到main.c文件時要注意將DMA句柄結構體實例化對象名稱修改為自動生成的名稱hdma_memtomem_dma1_channel1。
2、在配置DMA外設時注意
/* Configure DMA request hdma_memtomem_dma1_channel1 on DMA1_Channel1 */hdma_memtomem_dma1_channel1.Instance = DMA1_Channel1;hdma_memtomem_dma1_channel1.Init.Direction = DMA_MEMORY_TO_MEMORY;hdma_memtomem_dma1_channel1.Init.PeriphInc = DMA_PINC_ENABLE;hdma_memtomem_dma1_channel1.Init.MemInc = DMA_MINC_ENABLE;hdma_memtomem_dma1_channel1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;hdma_memtomem_dma1_channel1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;hdma_memtomem_dma1_channel1.Init.Mode = DMA_NORMAL;hdma_memtomem_dma1_channel1.Init.Priority = DMA_PRIORITY_MEDIUM;
3、? ? 要調用庫函數開啟DMA傳輸
HAL_DMA_Start(&hdma_memtomem_dma1_channel1,(uint32_t)aSRC_Const_Buffer,(uint32_t)aDST_Buffer,BUFFER_SIZE);
4、??
/* 等待DMA傳輸完成 */while(__HAL_DMA_GET_FLAG(&hdma_memtomem_dma1_channel1,DMA_FLAG_TC1)==RESET){}
中的DMA_FLAG_TC1要改為這個。