当前位置:   article > 正文

STM32cubeMX开发 NANDflash 之H27U4G8F2E 问题记录

h27u4g8f2e

stm32cubemx配置

时钟配置

 代码部分

  1. /**
  2. ******************************************************************************
  3. * File Name : FMC.c
  4. * Description : This file provides code for the configuration
  5. * of the FMC peripheral.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "fmc.h"
  21. /* USER CODE BEGIN 0 */
  22. /* USER CODE END 0 */
  23. NAND_HandleTypeDef hnand1;
  24. /* FMC initialization function */
  25. void MX_FMC_Init(void)
  26. {
  27. FMC_NAND_PCC_TimingTypeDef ComSpaceTiming = {0};
  28. FMC_NAND_PCC_TimingTypeDef AttSpaceTiming = {0};
  29. /** Perform the NAND1 memory initialization sequence
  30. */
  31. hnand1.Instance = FMC_NAND_DEVICE;
  32. /* hnand1.Init */
  33. hnand1.Init.NandBank = FMC_NAND_BANK3;
  34. hnand1.Init.Waitfeature = FMC_NAND_PCC_WAIT_FEATURE_ENABLE;
  35. hnand1.Init.MemoryDataWidth = FMC_NAND_PCC_MEM_BUS_WIDTH_8;
  36. hnand1.Init.EccComputation = FMC_NAND_ECC_DISABLE;
  37. hnand1.Init.ECCPageSize = FMC_NAND_ECC_PAGE_SIZE_2048BYTE;
  38. hnand1.Init.TCLRSetupTime = 0;
  39. hnand1.Init.TARSetupTime = 0;
  40. /* hnand1.Config */
  41. hnand1.Config.PageSize = 2048;
  42. hnand1.Config.SpareAreaSize = 64;
  43. hnand1.Config.BlockSize = 64;
  44. hnand1.Config.BlockNbr = 4096;
  45. hnand1.Config.PlaneNbr = 2;
  46. hnand1.Config.PlaneSize = 276824064;
  47. hnand1.Config.ExtraCommandEnable = DISABLE;
  48. /* ComSpaceTiming */
  49. ComSpaceTiming.SetupTime = 0;
  50. ComSpaceTiming.WaitSetupTime = 1;
  51. ComSpaceTiming.HoldSetupTime = 3;
  52. ComSpaceTiming.HiZSetupTime = 1;
  53. /* AttSpaceTiming */
  54. AttSpaceTiming.SetupTime = 0;
  55. AttSpaceTiming.WaitSetupTime = 1;
  56. AttSpaceTiming.HoldSetupTime = 3;
  57. AttSpaceTiming.HiZSetupTime = 1;
  58. if (HAL_NAND_Init(&hnand1, &ComSpaceTiming, &AttSpaceTiming) != HAL_OK)
  59. {
  60. Error_Handler( );
  61. }
  62. }
  63. static uint32_t FMC_Initialized = 0;
  64. static void HAL_FMC_MspInit(void){
  65. /* USER CODE BEGIN FMC_MspInit 0 */
  66. /* USER CODE END FMC_MspInit 0 */
  67. GPIO_InitTypeDef GPIO_InitStruct = {0};
  68. if (FMC_Initialized) {
  69. return;
  70. }
  71. FMC_Initialized = 1;
  72. /* Peripheral clock enable */
  73. __HAL_RCC_FMC_CLK_ENABLE();
  74. /** FMC GPIO Configuration
  75. PE7 ------> FMC_D4
  76. PE8 ------> FMC_D5
  77. PE9 ------> FMC_D6
  78. PE10 ------> FMC_D7
  79. PD11 ------> FMC_CLE
  80. PD12 ------> FMC_ALE
  81. PD14 ------> FMC_D0
  82. PD15 ------> FMC_D1
  83. PD0 ------> FMC_D2
  84. PD1 ------> FMC_D3
  85. PD4 ------> FMC_NOE
  86. PD5 ------> FMC_NWE
  87. PD6 ------> FMC_NWAIT
  88. PG9 ------> FMC_NCE3
  89. */
  90. /* GPIO_InitStruct */
  91. GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
  92. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  93. GPIO_InitStruct.Pull = GPIO_NOPULL;
  94. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  95. GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
  96. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  97. /* GPIO_InitStruct */
  98. GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_14|GPIO_PIN_15
  99. |GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5
  100. |GPIO_PIN_6;
  101. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  102. GPIO_InitStruct.Pull = GPIO_NOPULL;
  103. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  104. GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
  105. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  106. /* GPIO_InitStruct */
  107. GPIO_InitStruct.Pin = GPIO_PIN_9;
  108. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  109. GPIO_InitStruct.Pull = GPIO_NOPULL;
  110. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  111. GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
  112. HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
  113. /* USER CODE BEGIN FMC_MspInit 1 */
  114. /* USER CODE END FMC_MspInit 1 */
  115. }
  116. void HAL_NAND_MspInit(NAND_HandleTypeDef* nandHandle){
  117. /* USER CODE BEGIN NAND_MspInit 0 */
  118. /* USER CODE END NAND_MspInit 0 */
  119. HAL_FMC_MspInit();
  120. /* USER CODE BEGIN NAND_MspInit 1 */
  121. /* USER CODE END NAND_MspInit 1 */
  122. }
  123. static uint32_t FMC_DeInitialized = 0;
  124. static void HAL_FMC_MspDeInit(void){
  125. /* USER CODE BEGIN FMC_MspDeInit 0 */
  126. /* USER CODE END FMC_MspDeInit 0 */
  127. if (FMC_DeInitialized) {
  128. return;
  129. }
  130. FMC_DeInitialized = 1;
  131. /* Peripheral clock enable */
  132. __HAL_RCC_FMC_CLK_DISABLE();
  133. /** FMC GPIO Configuration
  134. PE7 ------> FMC_D4
  135. PE8 ------> FMC_D5
  136. PE9 ------> FMC_D6
  137. PE10 ------> FMC_D7
  138. PD11 ------> FMC_CLE
  139. PD12 ------> FMC_ALE
  140. PD14 ------> FMC_D0
  141. PD15 ------> FMC_D1
  142. PD0 ------> FMC_D2
  143. PD1 ------> FMC_D3
  144. PD4 ------> FMC_NOE
  145. PD5 ------> FMC_NWE
  146. PD6 ------> FMC_NWAIT
  147. PG9 ------> FMC_NCE3
  148. */
  149. HAL_GPIO_DeInit(GPIOE, GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10);
  150. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_14|GPIO_PIN_15
  151. |GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5
  152. |GPIO_PIN_6);
  153. HAL_GPIO_DeInit(GPIOG, GPIO_PIN_9);
  154. /* USER CODE BEGIN FMC_MspDeInit 1 */
  155. /* USER CODE END FMC_MspDeInit 1 */
  156. }
  157. void HAL_NAND_MspDeInit(NAND_HandleTypeDef* nandHandle){
  158. /* USER CODE BEGIN NAND_MspDeInit 0 */
  159. /* USER CODE END NAND_MspDeInit 0 */
  160. HAL_FMC_MspDeInit();
  161. /* USER CODE BEGIN NAND_MspDeInit 1 */
  162. /* USER CODE END NAND_MspDeInit 1 */
  163. }
  164. /**
  165. * @}
  166. */
  167. /**
  168. * @}
  169. */
  170. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. #include "fmc.h"
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "nandflash.h"
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31. /* USER CODE END PTD */
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. /* USER CODE END PD */
  35. /* Private macro -------------------------------------------------------------*/
  36. /* USER CODE BEGIN PM */
  37. /* USER CODE END PM */
  38. /* Private variables ---------------------------------------------------------*/
  39. /* USER CODE BEGIN PV */
  40. /* USER CODE END PV */
  41. /* Private function prototypes -----------------------------------------------*/
  42. void SystemClock_Config(void);
  43. /* USER CODE BEGIN PFP */
  44. /* USER CODE END PFP */
  45. /* Private user code ---------------------------------------------------------*/
  46. /* USER CODE BEGIN 0 */
  47. /* USER CODE END 0 */
  48. /**
  49. * @brief The application entry point.
  50. * @retval int
  51. */
  52. int main(void)
  53. {
  54. /* USER CODE BEGIN 1 */
  55. /* USER CODE END 1 */
  56. /* MCU Configuration--------------------------------------------------------*/
  57. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  58. HAL_Init();
  59. /* USER CODE BEGIN Init */
  60. /* USER CODE END Init */
  61. /* Configure the system clock */
  62. SystemClock_Config();
  63. /* USER CODE BEGIN SysInit */
  64. /* USER CODE END SysInit */
  65. /* Initialize all configured peripherals */
  66. MX_GPIO_Init();
  67. MX_FMC_Init();
  68. MX_USART1_UART_Init();
  69. /* USER CODE BEGIN 2 */
  70. HAL_Delay (500);
  71. if(nandflash())
  72. HAL_UART_Transmit(&huart1,"read nandflash failed",sizeof("read nandflash failed"),100);
  73. /* USER CODE END 2 */
  74. /* Infinite loop */
  75. /* USER CODE BEGIN WHILE */
  76. while (1)
  77. {
  78. /* USER CODE END WHILE */
  79. /* USER CODE BEGIN 3 */
  80. }
  81. /* USER CODE END 3 */
  82. }
  83. /**
  84. * @brief System Clock Configuration
  85. * @retval None
  86. */
  87. void SystemClock_Config(void)
  88. {
  89. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  90. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  91. /** Configure the main internal regulator output voltage
  92. */
  93. __HAL_RCC_PWR_CLK_ENABLE();
  94. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  95. /** Initializes the CPU, AHB and APB busses clocks
  96. */
  97. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  98. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  99. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  100. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  101. RCC_OscInitStruct.PLL.PLLM = 25;
  102. RCC_OscInitStruct.PLL.PLLN = 192;
  103. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  104. RCC_OscInitStruct.PLL.PLLQ = 4;
  105. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  106. {
  107. Error_Handler();
  108. }
  109. /** Initializes the CPU, AHB and APB busses clocks
  110. */
  111. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  112. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  113. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  114. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  115. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  116. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  117. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  118. {
  119. Error_Handler();
  120. }
  121. }
  122. /* USER CODE BEGIN 4 */
  123. /* USER CODE END 4 */
  124. /**
  125. * @brief This function is executed in case of error occurrence.
  126. * @retval None
  127. */
  128. void Error_Handler(void)
  129. {
  130. /* USER CODE BEGIN Error_Handler_Debug */
  131. /* User can add his own implementation to report the HAL error return state */
  132. /* USER CODE END Error_Handler_Debug */
  133. }
  134. #ifdef USE_FULL_ASSERT
  135. /**
  136. * @brief Reports the name of the source file and the source line number
  137. * where the assert_param error has occurred.
  138. * @param file: pointer to the source file name
  139. * @param line: assert_param error line source number
  140. * @retval None
  141. */
  142. void assert_failed(uint8_t *file, uint32_t line)
  143. {
  144. /* USER CODE BEGIN 6 */
  145. /* User can add his own implementation to report the file name and line number,
  146. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  147. /* USER CODE END 6 */
  148. }
  149. #endif /* USE_FULL_ASSERT */
  150. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  1. #include "fmc.h"
  2. #include "usart.h"
  3. NAND_IDTypeDef nand_ID;
  4. NAND_AddressTypeDef NAND_Address;
  5. void nand_init(uint8_t nPlane,uint16_t nBlock,uint8_t nPage)
  6. {
  7. assert_param(IS_nPLANE_OK(nPlane));
  8. assert_param(IS_nBLOCK_OK(nBlock));
  9. assert_param(IS_nPAGE_OK(nPage));
  10. NAND_Address.Plane = nPlane;
  11. NAND_Address.Block = nBlock;
  12. NAND_Address.Page = nPage;
  13. }
  14. uint8_t nandflash()
  15. {
  16. HAL_StatusTypeDef HAL_status;
  17. uint8_t read_result[2048] = "0";
  18. HAL_NAND_Reset(&hnand1);
  19. HAL_Delay (500);
  20. HAL_status = HAL_NAND_Read_ID(&hnand1,&nand_ID);
  21. if(HAL_status != HAL_OK)
  22. return 1;
  23. HAL_UART_Transmit(&huart1,&nand_ID.Device_Id,sizeof(uint8_t),100);
  24. HAL_UART_Transmit(&huart1,&nand_ID.Fourth_Id,sizeof(uint8_t),100);
  25. HAL_UART_Transmit(&huart1,&nand_ID.Maker_Id,sizeof(uint8_t),100);
  26. HAL_UART_Transmit(&huart1,&nand_ID.Third_Id,sizeof(uint8_t),100);
  27. nand_init(1,1,1);
  28. HAL_status = HAL_NAND_Read_Page_8b(&hnand1,&NAND_Address,read_result,1);
  29. if(HAL_status != HAL_OK)
  30. return 3;
  31. HAL_UART_Transmit(&huart1,read_result,sizeof(read_result),3000);
  32. if(HAL_status != HAL_OK)
  33. return 4;
  34. return 0;
  35. }

问题:调试时总是卡在

    HAL_status = HAL_NAND_Read_ID(&hnand1,&nand_ID);这个库函数中

  1. data = *(__IO uint32_t *)deviceaddress;
  2. /* Return the data read */

一执行这一步,就报错

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/404185
推荐阅读
相关标签
  

闽ICP备14008679号