BSP files for STM32H747I-Discovery Copy from ST Cube delivery
Dependents: DISCO_H747I_LCD_demo DISCO_H747I_AUDIO_demo
stm32h747i_discovery_sd.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32h747i_discovery_sd.c 00004 * @author MCD Application Team 00005 * @brief This file includes the uSD card driver mounted on STM32H747I-DISCOVERY 00006 * board. 00007 ****************************************************************************** 00008 * @attention 00009 * 00010 * <h2><center>© Copyright (c) 2019 STMicroelectronics. 00011 * All rights reserved.</center></h2> 00012 * 00013 * This software component is licensed by ST under BSD 3-Clause license, 00014 * the "License"; You may not use this file except in compliance with the 00015 * License. You may obtain a copy of the License at: 00016 * opensource.org/licenses/BSD-3-Clause 00017 * 00018 ****************************************************************************** 00019 */ 00020 00021 /* File Info : ----------------------------------------------------------------- 00022 User NOTES 00023 1. How To use this driver: 00024 -------------------------- 00025 - This driver is used to drive the micro SD external card mounted on STM32412G-DISCOVERY 00026 board. 00027 - This driver does not need a specific component driver for the micro SD device 00028 to be included with. 00029 00030 2. Driver description: 00031 --------------------- 00032 + Initialization steps: 00033 o Initialize the micro SD card using the BSP_SD_Init() function. This 00034 function includes the MSP layer hardware resources initialization and the 00035 SDIO interface configuration to interface with the external micro SD. It 00036 also includes the micro SD initialization sequence. 00037 o To check the SD card presence you can use the function BSP_SD_IsDetected() which 00038 returns the detection status 00039 o If SD presence detection interrupt mode is desired, you must configure the 00040 SD detection interrupt mode by calling the function BSP_SD_ITConfig(). The interrupt 00041 is generated as an external interrupt whenever the micro SD card is 00042 plugged/unplugged in/from the board. 00043 o The function BSP_SD_GetCardInfo() is used to get the micro SD card information 00044 which is stored in the structure "HAL_SD_CardInfoTypedef". 00045 00046 + Micro SD card operations 00047 o The micro SD card can be accessed with read/write block(s) operations once 00048 it is ready for access. The access can be performed whether using the polling 00049 mode by calling the functions BSP_SD_ReadBlocks()/BSP_SD_WriteBlocks(), or by DMA 00050 transfer using the functions BSP_SD_ReadBlocks_DMA()/BSP_SD_WriteBlocks_DMA() 00051 o The DMA transfer complete is used with interrupt mode. Once the SD transfer 00052 is complete, the SD interrupt is handled using the function BSP_SD_IRQHandler(), 00053 the DMA Tx/Rx transfer complete are handled using the functions 00054 SD_DMA_Tx_IRQHandler()/SD_DMA_Rx_IRQHandler() that should be defined by user. 00055 The corresponding user callbacks are implemented by the user at application level. 00056 o The SD erase block(s) is performed using the function BSP_SD_Erase() with specifying 00057 the number of blocks to erase. 00058 o The SD runtime status is returned when calling the function BSP_SD_GetStatus(). 00059 00060 ------------------------------------------------------------------------------*/ 00061 00062 /* Includes ------------------------------------------------------------------*/ 00063 #include "stm32h747i_discovery_sd.h" 00064 00065 /** @addtogroup BSP 00066 * @{ 00067 */ 00068 00069 /** @addtogroup STM32H747I_DISCOVERY 00070 * @{ 00071 */ 00072 00073 /** @defgroup STM32H747I_DISCOVERY_SD STM32H747I_DISCOVERY_SD 00074 * @{ 00075 */ 00076 00077 /** @defgroup STM32H747I_DISCOVERY_SD_Exported_Variables Exported Variables 00078 * @{ 00079 */ 00080 SD_HandleTypeDef uSdHandle; 00081 /** 00082 * @} 00083 */ 00084 00085 /** @defgroup STM32H747I_DISCOVERY_SD_Exported_Functions Exported Functions 00086 * @{ 00087 */ 00088 00089 /** 00090 * @brief Initializes the SD card device. 00091 * @retval SD status 00092 */ 00093 uint8_t BSP_SD_Init(void) 00094 { 00095 uint8_t sd_state = MSD_OK; 00096 00097 /* uSD device interface configuration */ 00098 uSdHandle.Instance = SDMMC1; 00099 00100 /* if CLKDIV = 0 then SDMMC Clock frequency = SDMMC Kernel Clock 00101 else SDMMC Clock frequency = SDMMC Kernel Clock / [2 * CLKDIV]. 00102 */ 00103 #if ! defined (BSP_SD_HIGH_PERFORMANCE_CONFIG) 00104 uSdHandle.Init.ClockDiv = 4; 00105 #else 00106 /* Code for high performance */ 00107 uSdHandle.Init.ClockDiv = 2; 00108 #endif /* BSP_SD_HIGH_PERFORMANCE_CONFIG */ 00109 uSdHandle.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE; 00110 uSdHandle.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING; 00111 uSdHandle.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE; 00112 uSdHandle.Init.BusWide = SDMMC_BUS_WIDE_4B; 00113 00114 /* Msp SD initialization */ 00115 BSP_SD_MspInit(&uSdHandle, NULL); 00116 00117 /* Check if SD card is present */ 00118 if(BSP_SD_IsDetected() != SD_PRESENT) 00119 { 00120 BSP_SD_MspDeInit(&uSdHandle, NULL); 00121 return MSD_ERROR_SD_NOT_PRESENT; 00122 } 00123 00124 /* HAL SD initialization */ 00125 if(HAL_SD_Init(&uSdHandle) != HAL_OK) 00126 { 00127 sd_state = MSD_ERROR; 00128 } 00129 00130 return sd_state; 00131 } 00132 00133 /** 00134 * @brief DeInitializes the SD card device. 00135 * @retval SD status 00136 */ 00137 uint8_t BSP_SD_DeInit(void) 00138 { 00139 uint8_t sd_state = MSD_OK; 00140 00141 uSdHandle.Instance = SDMMC1; 00142 00143 /* HAL SD deinitialization */ 00144 if(HAL_SD_DeInit(&uSdHandle) != HAL_OK) 00145 { 00146 sd_state = MSD_ERROR; 00147 } 00148 00149 /* Msp SD deinitialization */ 00150 uSdHandle.Instance = SDMMC1; 00151 BSP_SD_MspDeInit(&uSdHandle, NULL); 00152 00153 return sd_state; 00154 } 00155 00156 /** 00157 * @brief Configures Interrupt mode for SD1 detection pin. 00158 * @retval Returns 0 00159 */ 00160 uint8_t BSP_SD_ITConfig(void) 00161 { 00162 GPIO_InitTypeDef gpio_init_structure; 00163 00164 /* Configure Interrupt mode for SD detection pin */ 00165 gpio_init_structure.Pin = SD_DETECT_PIN; 00166 gpio_init_structure.Pull = GPIO_PULLUP; 00167 gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH; 00168 gpio_init_structure.Mode = GPIO_MODE_IT_RISING_FALLING; 00169 HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &gpio_init_structure); 00170 00171 /* Enable and set SD detect EXTI Interrupt to the lowest priority */ 00172 HAL_NVIC_SetPriority((IRQn_Type)(SD_DETECT_EXTI_IRQn), 0x0F, 0x00); 00173 HAL_NVIC_EnableIRQ((IRQn_Type)(SD_DETECT_EXTI_IRQn)); 00174 00175 return MSD_OK; 00176 } 00177 00178 /** 00179 * @brief Detects if SD card is correctly plugged in the memory slot or not. 00180 * @retval Returns if SD is detected or not 00181 */ 00182 uint8_t BSP_SD_IsDetected(void) 00183 { 00184 __IO uint8_t status = SD_PRESENT; 00185 00186 00187 /* Check SD card detect pin */ 00188 if (HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) == GPIO_PIN_SET) 00189 { 00190 status = SD_NOT_PRESENT; 00191 } 00192 00193 return status; 00194 } 00195 00196 /** 00197 * @brief Reads block(s) from a specified address in an SD card, in polling mode. 00198 * @param pData: Pointer to the buffer that will contain the data to transmit 00199 * @param ReadAddr: Address from where data is to be read 00200 * @param NumOfBlocks: Number of SD blocks to read 00201 * @param Timeout: Timeout for read operation 00202 * @retval SD status 00203 */ 00204 uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout) 00205 { 00206 00207 if( HAL_SD_ReadBlocks(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout) == HAL_OK) 00208 { 00209 return MSD_OK; 00210 } 00211 else 00212 { 00213 return MSD_ERROR; 00214 } 00215 00216 } 00217 00218 /** 00219 * @brief Writes block(s) to a specified address in an SD card, in polling mode. 00220 * @param pData: Pointer to the buffer that will contain the data to transmit 00221 * @param WriteAddr: Address from where data is to be written 00222 * @param NumOfBlocks: Number of SD blocks to write 00223 * @param Timeout: Timeout for write operation 00224 * @retval SD status 00225 */ 00226 uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout) 00227 { 00228 00229 if( HAL_SD_WriteBlocks(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout) == HAL_OK) 00230 { 00231 return MSD_OK; 00232 } 00233 else 00234 { 00235 return MSD_ERROR; 00236 } 00237 } 00238 00239 /** 00240 * @brief Reads block(s) from a specified address in an SD card, in DMA mode. 00241 * @param pData: Pointer to the buffer that will contain the data to transmit 00242 * @param ReadAddr: Address from where data is to be read 00243 * @param NumOfBlocks: Number of SD blocks to read 00244 * @retval SD status 00245 */ 00246 uint8_t BSP_SD_ReadBlocks_DMA(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks) 00247 { 00248 00249 if( HAL_SD_ReadBlocks_DMA(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks) == HAL_OK) 00250 { 00251 return MSD_OK; 00252 } 00253 else 00254 { 00255 return MSD_ERROR; 00256 } 00257 } 00258 00259 /** 00260 * @brief Writes block(s) to a specified address in an SD card, in DMA mode. 00261 * @param pData: Pointer to the buffer that will contain the data to transmit 00262 * @param WriteAddr: Address from where data is to be written 00263 * @param NumOfBlocks: Number of SD blocks to write 00264 * @retval SD status 00265 */ 00266 uint8_t BSP_SD_WriteBlocks_DMA(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks) 00267 { 00268 00269 if( HAL_SD_WriteBlocks_DMA(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks) == HAL_OK) 00270 { 00271 return MSD_OK; 00272 } 00273 else 00274 { 00275 return MSD_ERROR; 00276 } 00277 00278 } 00279 00280 /** 00281 * @brief Erases the specified memory area of the given SD card. 00282 * @param StartAddr: Start byte address 00283 * @param EndAddr: End byte address 00284 * @retval SD status 00285 */ 00286 uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr) 00287 { 00288 00289 if( HAL_SD_Erase(&uSdHandle, StartAddr, EndAddr) == HAL_OK) 00290 { 00291 return MSD_OK; 00292 } 00293 else 00294 { 00295 return MSD_ERROR; 00296 } 00297 } 00298 00299 /** 00300 * @brief Initializes the SD MSP. 00301 * @param hsd SD handle 00302 * @param Params User parameters 00303 * @retval None 00304 */ 00305 __weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params) 00306 { 00307 /* __weak function can be modified by the application */ 00308 00309 GPIO_InitTypeDef gpio_init_structure; 00310 00311 /* SD pins are in conflict with Camera pins on the Disco board 00312 therefore Camera must be power down before using the BSP SD 00313 To power down the camera , Set GPIOJ pin 14 to high 00314 */ 00315 00316 /* Enable GPIO J clock */ 00317 __HAL_RCC_GPIOJ_CLK_ENABLE(); 00318 00319 gpio_init_structure.Pin = GPIO_PIN_14; 00320 gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP; 00321 gpio_init_structure.Pull = GPIO_NOPULL; 00322 gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 00323 HAL_GPIO_Init(GPIOJ, &gpio_init_structure); 00324 00325 /* Set the camera POWER_DOWN pin (active high) */ 00326 HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_14, GPIO_PIN_SET); 00327 00328 /* Enable SDIO clock */ 00329 __HAL_RCC_SDMMC1_CLK_ENABLE(); 00330 00331 00332 /* Enable GPIOs clock */ 00333 __HAL_RCC_GPIOB_CLK_ENABLE(); 00334 __HAL_RCC_GPIOC_CLK_ENABLE(); 00335 __HAL_RCC_GPIOD_CLK_ENABLE(); 00336 00337 00338 /* Common GPIO configuration */ 00339 gpio_init_structure.Mode = GPIO_MODE_AF_PP; 00340 gpio_init_structure.Pull = GPIO_PULLUP; 00341 gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 00342 gpio_init_structure.Alternate = GPIO_AF12_SDIO1; 00343 00344 /* SDMMC GPIO CLKIN PB8, D0 PC8, D1 PC9, D2 PC10, D3 PC11, CK PC12, CMD PD2 */ 00345 /* GPIOC configuration */ 00346 gpio_init_structure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12; 00347 00348 HAL_GPIO_Init(GPIOC, &gpio_init_structure); 00349 00350 /* GPIOD configuration */ 00351 gpio_init_structure.Pin = GPIO_PIN_2; 00352 HAL_GPIO_Init(GPIOD, &gpio_init_structure); 00353 00354 /* Configure Input mode for SD detection pin */ 00355 SD_DETECT_GPIO_CLK_ENABLE(); 00356 gpio_init_structure.Pin = SD_DETECT_PIN; 00357 gpio_init_structure.Pull = GPIO_PULLUP; 00358 gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH; 00359 gpio_init_structure.Mode = GPIO_MODE_INPUT; 00360 HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &gpio_init_structure); 00361 00362 /* NVIC configuration for SDIO interrupts */ 00363 HAL_NVIC_SetPriority(SDMMC1_IRQn, 5, 0); 00364 HAL_NVIC_EnableIRQ(SDMMC1_IRQn); 00365 00366 } 00367 00368 /** 00369 * @brief DeInitializes the SD MSP. 00370 * @param hsd SD handle 00371 * @param Params User parameters 00372 * @retval None 00373 */ 00374 __weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params) 00375 { 00376 /* Disable NVIC for SDIO interrupts */ 00377 HAL_NVIC_DisableIRQ(SDMMC1_IRQn); 00378 00379 /* DeInit GPIO pins can be done in the application 00380 (by surcharging this __weak function) */ 00381 00382 /* Disable SDMMC1 clock */ 00383 __HAL_RCC_SDMMC1_CLK_DISABLE(); 00384 00385 /* GPIO pins clock and DMA clocks can be shut down in the application 00386 by surcharging this __weak function */ 00387 } 00388 00389 /** 00390 * @brief Handles SD card interrupt request. 00391 * @retval None 00392 */ 00393 void BSP_SD_IRQHandler(void) 00394 { 00395 HAL_SD_IRQHandler(&uSdHandle); 00396 } 00397 00398 /** 00399 * @brief Gets the current SD card data status. 00400 * @retval Data transfer state. 00401 * This value can be one of the following values: 00402 * @arg SD_TRANSFER_OK: No data transfer is acting 00403 * @arg SD_TRANSFER_BUSY: Data transfer is acting 00404 * @arg SD_TRANSFER_ERROR: Data transfer error 00405 */ 00406 uint8_t BSP_SD_GetCardState(void) 00407 { 00408 return((HAL_SD_GetCardState(&uSdHandle) == HAL_SD_CARD_TRANSFER ) ? SD_TRANSFER_OK : SD_TRANSFER_BUSY); 00409 } 00410 00411 /** 00412 * @brief Get SD information about specific SD card. 00413 * @param CardInfo: Pointer to HAL_SD_CardInfoTypedef structure 00414 * @retval None 00415 */ 00416 void BSP_SD_GetCardInfo(BSP_SD_CardInfo *CardInfo) 00417 { 00418 HAL_SD_GetCardInfo(&uSdHandle, CardInfo); 00419 } 00420 00421 /** 00422 * @brief SD Abort callbacks 00423 * @param hsd SD handle 00424 * @retval None 00425 */ 00426 void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd) 00427 { 00428 BSP_SD_AbortCallback(); 00429 } 00430 00431 00432 /** 00433 * @brief Tx Transfer completed callbacks 00434 * @param hsd SD handle 00435 * @retval None 00436 */ 00437 void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd) 00438 { 00439 BSP_SD_WriteCpltCallback(); 00440 } 00441 00442 /** 00443 * @brief Rx Transfer completed callbacks 00444 * @param hsd SD handle 00445 * @retval None 00446 */ 00447 void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd) 00448 { 00449 BSP_SD_ReadCpltCallback(); 00450 } 00451 00452 /** 00453 * @brief BSP SD Abort callbacks 00454 * @retval None 00455 */ 00456 __weak void BSP_SD_AbortCallback(void) 00457 { 00458 00459 } 00460 00461 /** 00462 * @brief BSP Tx Transfer completed callbacks 00463 * @retval None 00464 */ 00465 __weak void BSP_SD_WriteCpltCallback(void) 00466 { 00467 00468 } 00469 00470 /** 00471 * @brief BSP Rx Transfer completed callbacks 00472 * @retval None 00473 */ 00474 __weak void BSP_SD_ReadCpltCallback(void) 00475 { 00476 00477 } 00478 /** 00479 * @} 00480 */ 00481 00482 /** 00483 * @} 00484 */ 00485 00486 /** 00487 * @} 00488 */ 00489 00490 /** 00491 * @} 00492 */ 00493 00494 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 18:45:41 by 1.7.2