[STM32H7] 【STM32H7S78-DK测评】-6- SD读写测试

[复制链接]
977|2
 楼主| 南来之风 发表于 2024-9-29 19:56 | 显示全部楼层 |阅读模式
本帖最后由 南来之风 于 2024-9-8 21:53 编辑

开发板上有MicroSD卡接口:
2990466ddab251184a.png

根据原理图初始化相应的PIN与时钟:
  1. /**
  2. * [url=home.php?mod=space&uid=247401]@brief[/url] SD MSP Initialization
  3. * This function configures the hardware resources used in this example
  4. * @param hsd: SD handle pointer
  5. * @retval None
  6. */
  7. void HAL_SD_MspInit(SD_HandleTypeDef* hsd)
  8. {
  9.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  10.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  11.   if(hsd->Instance==SDMMC1)
  12.   {
  13.   /* USER CODE BEGIN SDMMC1_MspInit 0 */

  14.   /* USER CODE END SDMMC1_MspInit 0 */

  15.   /** Initializes the peripherals clock
  16.   */
  17.     PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SDMMC12;
  18.     PeriphClkInit.Sdmmc12ClockSelection = RCC_SDMMC12CLKSOURCE_PLL2S;
  19.     if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  20.     {
  21.       Error_Handler();
  22.     }

  23.     /* Peripheral clock enable */
  24.     __HAL_RCC_SDMMC1_CLK_ENABLE();

  25.     __HAL_RCC_GPIOD_CLK_ENABLE();
  26.     __HAL_RCC_GPIOC_CLK_ENABLE();
  27.     /**SDMMC1 GPIO Configuration
  28.     PD2     ------> SDMMC1_CMD
  29.     PC10     ------> SDMMC1_D2
  30.     PC11     ------> SDMMC1_D3
  31.     PC12     ------> SDMMC1_CK
  32.     PC8     ------> SDMMC1_D0
  33.     PC9     ------> SDMMC1_D1
  34.     */
  35.     GPIO_InitStruct.Pin = GPIO_PIN_2;
  36.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  37.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  38.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  39.     GPIO_InitStruct.Alternate = GPIO_AF11_SDMMC1;
  40.     HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  41.     GPIO_InitStruct.Pin = GPIO_PIN_10;
  42.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  43.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  44.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  45.     GPIO_InitStruct.Alternate = GPIO_AF12_SDMMC1;
  46.     HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  47.     GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_8|GPIO_PIN_9;
  48.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  49.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  50.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  51.     GPIO_InitStruct.Alternate = GPIO_AF11_SDMMC1;
  52.     HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  53.     /* SDMMC1 interrupt Init */
  54.     HAL_NVIC_SetPriority(SDMMC1_IRQn, 0, 0);
  55.   /* USER CODE BEGIN SDMMC1_MspInit 1 */
  56.   /*  SDMMC Clk on PLL1S: (64 / 4 * 25 / 2) = 200MHz */

  57.   /* Activate PLL1 with HSI as source (HSI is ON at reset) */

  58.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};

  59.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  60.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  61.   RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  62.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  63.   RCC_OscInitStruct.PLL1.PLLState = RCC_PLL_ON;
  64.   RCC_OscInitStruct.PLL1.PLLSource = RCC_PLLSOURCE_HSI;
  65.   RCC_OscInitStruct.PLL1.PLLM = 16;
  66.   RCC_OscInitStruct.PLL1.PLLN = 275;
  67.   RCC_OscInitStruct.PLL1.PLLP = 2;
  68.   RCC_OscInitStruct.PLL1.PLLQ = 2;
  69.   RCC_OscInitStruct.PLL1.PLLR = 2;
  70.   RCC_OscInitStruct.PLL1.PLLS = 2;
  71.   RCC_OscInitStruct.PLL1.PLLT = 2;
  72.   RCC_OscInitStruct.PLL1.PLLFractional = 0;
  73.   RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
  74.   RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
  75.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  76.   {
  77.     /* Initialization error */
  78.     while(1);
  79.   }

  80.   /*##-1- Enable peripherals and GPIO Clocks #################################*/

  81.   __HAL_RCC_SDMMC1_FORCE_RESET();
  82.   __HAL_RCC_SDMMC1_RELEASE_RESET();

  83.   /* NVIC configuration for SDMMC interrupts */
  84.   HAL_NVIC_EnableIRQ(SDMMC1_IRQn);
  85.   /* USER CODE END SDMMC1_MspInit 1 */

  86.   }

  87. }


相应的初始化函数:
  1. void MX_SDMMC1_SD_Init(void)
  2. {

  3.   /* USER CODE BEGIN SDMMC1_Init 0 */

  4.   /* USER CODE END SDMMC1_Init 0 */

  5.   /* USER CODE BEGIN SDMMC1_Init 1 */

  6.   /* USER CODE END SDMMC1_Init 1 */
  7.   hsd1.Instance = SDMMC1;
  8.   hsd1.Init.ClockEdge = SDMMC_CLOCK_EDGE_FALLING;
  9.   hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  10.   hsd1.Init.BusWide = SDMMC_BUS_WIDE_4B;
  11.   hsd1.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_ENABLE;
  12.   hsd1.Init.ClockDiv = 0x02;
  13.   if (HAL_SD_Init(&hsd1) != HAL_OK)
  14.   {
  15.     Error_Handler();
  16.   }
  17.   /* USER CODE BEGIN SDMMC1_Init 2 */

  18.   /* USER CODE END SDMMC1_Init 2 */

  19. }
接下来进行读写测试:
  1. static int32_t FS_FileOperations(void)
  2. {
  3.   FRESULT res; /* FatFs function common result code */
  4.   uint32_t byteswritten, bytesread; /* File write/read counts */
  5.   uint8_t rtext[100]; /* File read buffer */

  6.   /* Register the file system object to the FatFs module */
  7.   if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) == FR_OK)
  8.   {
  9.     /* Create and Open a new text file object with write access */
  10.     if(f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)
  11.     {

  12.       /* Write data to the text file */
  13.       res = f_write(&SDFile, (const void *)wtext, sizeof(wtext), (void *)&byteswritten);

  14.       if((byteswritten > 0) && (res == FR_OK))
  15.       {
  16.         /* Close the open text file */
  17.         f_close(&SDFile);

  18.         /* Open the text file object with read access */
  19.         if(f_open(&SDFile, "STM32.TXT", FA_READ) == FR_OK)
  20.         {
  21.           /* Read data from the text file */
  22.           res = f_read(&SDFile, ( void *)rtext, sizeof(rtext), (void *)&bytesread);
  23.           HAL_UART_Transmit(&huart4, "\r\n", sizeof("\r\n"), 0xffffffff);
  24.           HAL_UART_Transmit(&huart4, rtext, strlen(rtext), 0xffffffff);

  25.           if((bytesread > 0) && (res == FR_OK))
  26.           {
  27.             /* Close the open text file */
  28.             f_close(&SDFile);

  29.             /* Compare read data with the expected data */
  30.             if(bytesread == byteswritten)
  31.             {
  32.                     HAL_UART_Transmit(&huart4, "\r\nSuccess of the demo", sizeof("\r\n Success of the demo"), 0xffffffff);
  33.               /* Success of the demo: no error occurrence */
  34.               return 0;
  35.             }
  36.           }
  37.         }
  38.       }
  39.     }
  40.   }
  41.   /* Error */
  42.   return -1;
  43. }


实物展示:
2645066ddabe2cb7e5.png


Amazingxixixi 发表于 2024-10-31 16:01 | 显示全部楼层
测试速度没有?速度可以到多少M?
地瓜patch 发表于 2024-10-31 18:00 来自手机 | 显示全部楼层
sd卡的文件系统是用系统自带的么?还是调用库
您需要登录后才可以回帖 登录 | 注册

本版积分规则

69

主题

293

帖子

2

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