[STM32H5] 运行\DMA_FLASHToRAM例程,导致错误,怎么挽救

[复制链接]
2424|7
 楼主| yinxiangxv 发表于 2024-8-10 10:39 | 显示全部楼层 |阅读模式
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=288409]@file[/url]    DMA/DMA_FLASHToRAM/Src/main.c
  5.   * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  6.   * [url=home.php?mod=space&uid=247401]@brief[/url]   This example provides a description of how to use a DMA channel
  7.   *          to transfer a word data buffer from FLASH memory to embedded
  8.   *          SRAM memory through the STM32H5xx HAL API.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * Copyright (c) 2023 STMicroelectronics.
  13.   * All rights reserved.
  14.   *
  15.   * This software is licensed under terms that can be found in the LICENSE file
  16.   * in the root directory of this software component.
  17.   * If no LICENSE file comes with this software, it is provided AS-IS.
  18.   *
  19.   ******************************************************************************
  20.   */
  21. /* USER CODE END Header */
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "main.h"

  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include <string.h>
  27. /* USER CODE END Includes */

  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */

  30. /* USER CODE END PTD */

  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */

  33. /* USER CODE END PD */

  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */

  36. /* USER CODE END PM */

  37. /* Private variables ---------------------------------------------------------*/

  38. DMA_HandleTypeDef handle_GPDMA1_Channel0;

  39. /* USER CODE BEGIN PV */

  40. static const uint32_t aSRC_Const_Buffer[BUFFER_SIZE] =
  41. {
  42.   0x01020304, 0x05060708, 0x090A0B0C, 0x0D0E0F10,
  43.   0x11121314, 0x15161718, 0x191A1B1C, 0x1D1E1F20,
  44.   0x21222324, 0x25262728, 0x292A2B2C, 0x2D2E2F30,
  45.   0x31323334, 0x35363738, 0x393A3B3C, 0x3D3E3F40,
  46.   0x41424344, 0x45464748, 0x494A4B4C, 0x4D4E4F50,
  47.   0x51525354, 0x55565758, 0x595A5B5C, 0x5D5E5F60,
  48.   0x61626364, 0x65666768, 0x696A6B6C, 0x6D6E6F70,
  49.   0x71727374, 0x75767778, 0x797A7B7C, 0x7D7E7F80
  50. };

  51. static uint32_t aDST_Buffer[BUFFER_SIZE];

  52. static __IO uint32_t transferErrorDetected;    /* Set to 1 if an error transfer is detected */
  53. static __IO uint32_t transferCompleteDetected; /* Set to 1 if transfer is correctly completed */
  54. /* USER CODE END PV */

  55. /* Private function prototypes -----------------------------------------------*/
  56. void SystemClock_Config(void);
  57. static void MX_GPIO_Init(void);
  58. static void MX_GPDMA1_Init(void);
  59. static void MX_ICACHE_Init(void);
  60. /* USER CODE BEGIN PFP */
  61. /* Private function prototypes -----------------------------------------------*/
  62. static void TransferComplete(DMA_HandleTypeDef *handle_GPDMA1_Channel0);
  63. static void TransferError(DMA_HandleTypeDef *handle_GPDMA1_Channel0);
  64. static uint32_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
  65. /* USER CODE END PFP */

  66. /* Private user code ---------------------------------------------------------*/
  67. /* USER CODE BEGIN 0 */

  68. /* USER CODE END 0 */

  69. /**
  70.   * @brief  The application entry point.
  71.   * @retval int
  72.   */
  73. int main(void)
  74. {

  75.   /* USER CODE BEGIN 1 */
  76.   /* STM32H5xx HAL library initialization:
  77.        - Systick timer is configured by default as source of time base, but user
  78.              can eventually implement his proper time base source (a general purpose
  79.              timer for example or other time source), keeping in mind that Time base
  80.              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  81.              handled in milliseconds basis.
  82.        - Set NVIC Group Priority to 4
  83.        - Low Level Initialization
  84.      */
  85.   /* USER CODE END 1 */

  86.   /* MCU Configuration--------------------------------------------------------*/

  87.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  88.   HAL_Init();

  89.   /* USER CODE BEGIN Init */

  90.   /* USER CODE END Init */

  91.   /* Configure the system clock */
  92.   SystemClock_Config();

  93.   /* USER CODE BEGIN SysInit */

  94.   /* USER CODE END SysInit */

  95.   /* Initialize all configured peripherals */
  96.   MX_GPIO_Init();
  97.   MX_GPDMA1_Init();
  98.   MX_ICACHE_Init();
  99.   /* USER CODE BEGIN 2 */
  100.   /* Initialize LED2 */
  101.   BSP_LED_Init(LED2);

  102.   /* Reset transferErrorDetected to 0, it will be set to 1 if a transfer error is detected */
  103.   transferErrorDetected = 0;
  104.   /* Reset transferCompleteDetected to 0, it will be set to 1 if a transfer is correctly completed */
  105.   transferCompleteDetected = 0;

  106.   /* Select Callbacks functions called after Transfer complete and Transfer error */
  107.   HAL_DMA_RegisterCallback(&handle_GPDMA1_Channel0, HAL_DMA_XFER_CPLT_CB_ID, TransferComplete);
  108.   HAL_DMA_RegisterCallback(&handle_GPDMA1_Channel0, HAL_DMA_XFER_ERROR_CB_ID, TransferError);

  109.   /* Clean destination buffer */
  110.   memset((void *)aDST_Buffer, 0U, sizeof(aDST_Buffer) );

  111.   /* Configure the source, destination and buffer size DMA fields and Start DMA Channel/Stream transfer */
  112.   /* Enable All the DMA interrupts */
  113.   if (HAL_DMA_Start_IT(&handle_GPDMA1_Channel0, (uint32_t)&aSRC_Const_Buffer, (uint32_t)&aDST_Buffer, sizeof(aDST_Buffer)) != HAL_OK)
  114.   {
  115.     /* Transfer Error */
  116.     Error_Handler();
  117.   }

  118.   /* Wait for end of transmission or an error occurred */
  119.   while ((transferCompleteDetected == 0) && (transferErrorDetected == 0U));

  120.   /* Check DMA error */
  121.   if (transferErrorDetected == 1U)
  122.   {
  123.     Error_Handler();
  124.   }

  125.   /* Check destination aDST_Buffer */
  126.   if (Buffercmp((uint8_t *)aSRC_Const_Buffer, (uint8_t *)aDST_Buffer, (BUFFER_SIZE * 4U)) != 0U)
  127.   {
  128.     /* Transfer Error */
  129.     Error_Handler();
  130.   }
  131.   /* USER CODE END 2 */

  132.   /* Infinite loop */
  133.   /* USER CODE BEGIN WHILE */
  134.   while (1)
  135.   {
  136.     /* USER CODE END WHILE */

  137.     /* USER CODE BEGIN 3 */
  138.     /* Turn LED2 on*/
  139.     BSP_LED_On(LED2);
  140.   }
  141.   /* USER CODE END 3 */
  142. }

  143. /**
  144.   * @brief System Clock Configuration
  145.   * @retval None
  146.   */
  147. void SystemClock_Config(void)
  148. {
  149.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  150.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  151.   /** Configure the main internal regulator output voltage
  152.   */
  153.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);

  154.   while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

  155.   /** Initializes the RCC Oscillators according to the specified parameters
  156.   * in the RCC_OscInitTypeDef structure.
  157.   */
  158.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  159.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  160.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  161.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLL1_SOURCE_HSE;
  162.   RCC_OscInitStruct.PLL.PLLM = 12;
  163.   RCC_OscInitStruct.PLL.PLLN = 250;
  164.   RCC_OscInitStruct.PLL.PLLP = 2;
  165.   RCC_OscInitStruct.PLL.PLLQ = 2;
  166.   RCC_OscInitStruct.PLL.PLLR = 2;
  167.   RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1_VCIRANGE_1;
  168.   RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1_VCORANGE_WIDE;
  169.   RCC_OscInitStruct.PLL.PLLFRACN = 0;
  170.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  171.   {
  172.     Error_Handler();
  173.   }

  174.   /** Initializes the CPU, AHB and APB buses clocks
  175.   */
  176.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  177.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  178.                               |RCC_CLOCKTYPE_PCLK3;
  179.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  180.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  181.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  182.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  183.   RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  184.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  185.   {
  186.     Error_Handler();
  187.   }

  188.   /** Configure the programming delay
  189.   */
  190.   __HAL_FLASH_SET_PROGRAM_DELAY(FLASH_PROGRAMMING_DELAY_2);
  191. }

  192. /**
  193.   * @brief GPDMA1 Initialization Function
  194.   * @param None
  195.   * @retval None
  196.   */
  197. static void MX_GPDMA1_Init(void)
  198. {

  199.   /* USER CODE BEGIN GPDMA1_Init 0 */

  200.   /* USER CODE END GPDMA1_Init 0 */

  201.   /* Peripheral clock enable */
  202.   __HAL_RCC_GPDMA1_CLK_ENABLE();

  203.   /* GPDMA1 interrupt Init */
  204.     HAL_NVIC_SetPriority(GPDMA1_Channel0_IRQn, 0, 0);
  205.     HAL_NVIC_EnableIRQ(GPDMA1_Channel0_IRQn);

  206.   /* USER CODE BEGIN GPDMA1_Init 1 */

  207.   /* USER CODE END GPDMA1_Init 1 */
  208.   handle_GPDMA1_Channel0.Instance = GPDMA1_Channel0;
  209.   handle_GPDMA1_Channel0.Init.Request = DMA_REQUEST_SW;
  210.   handle_GPDMA1_Channel0.Init.BlkHWRequest = DMA_BREQ_SINGLE_BURST;
  211.   handle_GPDMA1_Channel0.Init.Direction = DMA_MEMORY_TO_MEMORY;
  212.   handle_GPDMA1_Channel0.Init.SrcInc = DMA_SINC_INCREMENTED;
  213.   handle_GPDMA1_Channel0.Init.DestInc = DMA_DINC_INCREMENTED;
  214.   handle_GPDMA1_Channel0.Init.SrcDataWidth = DMA_SRC_DATAWIDTH_WORD;
  215.   handle_GPDMA1_Channel0.Init.DestDataWidth = DMA_DEST_DATAWIDTH_WORD;
  216.   handle_GPDMA1_Channel0.Init.Priority = DMA_LOW_PRIORITY_HIGH_WEIGHT;
  217.   handle_GPDMA1_Channel0.Init.SrcBurstLength = 1;
  218.   handle_GPDMA1_Channel0.Init.DestBurstLength = 1;
  219.   handle_GPDMA1_Channel0.Init.TransferAllocatedPort = DMA_SRC_ALLOCATED_PORT0|DMA_DEST_ALLOCATED_PORT1;
  220.   handle_GPDMA1_Channel0.Init.TransferEventMode = DMA_TCEM_BLOCK_TRANSFER;
  221.   handle_GPDMA1_Channel0.Init.Mode = DMA_NORMAL;
  222.   if (HAL_DMA_Init(&handle_GPDMA1_Channel0) != HAL_OK)
  223.   {
  224.     Error_Handler();
  225.   }
  226.   if (HAL_DMA_ConfigChannelAttributes(&handle_GPDMA1_Channel0, DMA_CHANNEL_NPRIV) != HAL_OK)
  227.   {
  228.     Error_Handler();
  229.   }
  230.   /* USER CODE BEGIN GPDMA1_Init 2 */

  231.   /* USER CODE END GPDMA1_Init 2 */

  232. }

  233. /**
  234.   * @brief ICACHE Initialization Function
  235.   * @param None
  236.   * @retval None
  237.   */
  238. static void MX_ICACHE_Init(void)
  239. {

  240.   /* USER CODE BEGIN ICACHE_Init 0 */

  241.   /* USER CODE END ICACHE_Init 0 */

  242.   /* USER CODE BEGIN ICACHE_Init 1 */

  243.   /* USER CODE END ICACHE_Init 1 */

  244.   /** Enable instruction cache (default 2-ways set associative cache)
  245.   */
  246.   if (HAL_ICACHE_Enable() != HAL_OK)
  247.   {
  248.     Error_Handler();
  249.   }
  250.   /* USER CODE BEGIN ICACHE_Init 2 */

  251.   /* USER CODE END ICACHE_Init 2 */

  252. }

  253. /**
  254.   * @brief GPIO Initialization Function
  255.   * @param None
  256.   * @retval None
  257.   */
  258. static void MX_GPIO_Init(void)
  259. {
  260. /* USER CODE BEGIN MX_GPIO_Init_1 */
  261. /* USER CODE END MX_GPIO_Init_1 */

  262.   /* GPIO Ports Clock Enable */
  263.   __HAL_RCC_GPIOH_CLK_ENABLE();

  264. /* USER CODE BEGIN MX_GPIO_Init_2 */
  265. /* USER CODE END MX_GPIO_Init_2 */
  266. }

  267. /* USER CODE BEGIN 4 */
  268. /**
  269.   * @brief  DMA conversion complete callback
  270.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]   This function is executed when the transfer complete interrupt
  271.   *         is generated
  272.   * @retval None
  273.   */
  274. static void TransferComplete(DMA_HandleTypeDef *handle_GPDMA1_Channel0)
  275. {
  276.   transferCompleteDetected = 1;
  277. }

  278. /**
  279.   * @brief  DMA conversion error callback
  280.   * @note   This function is executed when the transfer error interrupt
  281.   *         is generated during DMA transfer
  282.   * @retval None
  283.   */
  284. static void TransferError(DMA_HandleTypeDef *handle_GPDMA1_Channel0)
  285. {
  286.   transferErrorDetected = 1;
  287. }

  288. /**
  289.   * @brief  Compares two buffers.
  290.   * @param  pBuffer1, pBuffer2: buffers to be compared.
  291.   * @param  BufferLength: buffer's length
  292.   * @retval 0   : pBuffer1 identical to pBuffer2
  293.   *         > 0 : pBuffer1 differs from pBuffer2
  294.   */
  295. static uint32_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
  296. {
  297.   while (BufferLength--)
  298.   {
  299.     if ((*pBuffer1) != *pBuffer2)
  300.     {
  301.       return BufferLength;
  302.     }

  303.     pBuffer1++;
  304.     pBuffer2++;
  305.   }

  306.   return 0;
  307. }
  308. /* USER CODE END 4 */

  309. /**
  310.   * @brief  This function is executed in case of error occurrence.
  311.   * @retval None
  312.   */
  313. void Error_Handler(void)
  314. {
  315.   /* USER CODE BEGIN Error_Handler_Debug */
  316.   while (1)
  317.   {
  318.     /* Toggle LED2 with a period of 1 s */
  319.     BSP_LED_Toggle(LED2);
  320.     HAL_Delay(1000);
  321.   }
  322.   /* USER CODE END Error_Handler_Debug */
  323. }

  324. #ifdef  USE_FULL_ASSERT
  325. /**
  326.   * @brief  Reports the name of the source file and the source line number
  327.   *         where the assert_param error has occurred.
  328.   * @param  file: pointer to the source file name
  329.   * @param  line: assert_param error line source number
  330.   * @retval None
  331.   */
  332. void assert_failed(uint8_t *file, uint32_t line)
  333. {
  334.   /* USER CODE BEGIN 6 */
  335.   /* User can add his own implementation to report the file name and line number,
  336.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  337.   /* Infinite loop */
  338.   while (1)
  339.   {
  340.   }
  341.   /* USER CODE END 6 */
  342. }
  343. #endif /* USE_FULL_ASSERT */
当编译运行后,不能再次下载其他程序。
并且报告下面的错误?
6305466b6d264c6fc5.png
6002766b6d26a9a7f2.png
2443466b6d26fa3d1a.png
这是怎么个情况,该怎么做呢?
用相关工具也没有检测到stlink?
怎么办呢?
elephant00 发表于 2024-8-10 16:42 | 显示全部楼层
检查硬件连接
两只袜子 发表于 2024-8-11 16:00 | 显示全部楼层
验证时钟配置
xionghaoyun 发表于 2024-8-12 08:48 | 显示全部楼层
这些可以百度(结果你发一串代码???)
例程说明不是代码问题 是你烧录器都没选好
可怜的小弗朗士 发表于 2024-8-12 13:58 | 显示全部楼层
是不是操作的地址越界了
jcky001 发表于 2024-8-12 16:00 | 显示全部楼层
仔细检查DMA的配置参数,包括传输方向(内存到内存、内存到外设等)、传输宽度、源和目标地址、传输长度等。
cr315 发表于 2024-8-12 19:00 | 显示全部楼层
检查FLASH和RAM地址
铁血丹心LLLL 发表于 2024-8-28 00:43 | 显示全部楼层
DMA配置需与硬件及其时钟配置相匹配。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

50

主题

348

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部