STM32H735xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32h7xx_hal_sd.c 00004 * @author MCD Application Team 00005 * @brief SD card HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Secure Digital (SD) peripheral: 00008 * + Initialization and de-initialization functions 00009 * + IO operation functions 00010 * + Peripheral Control functions 00011 * + Peripheral State functions 00012 * 00013 ****************************************************************************** 00014 * @attention 00015 * 00016 * Copyright (c) 2017 STMicroelectronics. 00017 * All rights reserved. 00018 * 00019 * This software is licensed under terms that can be found in the LICENSE file 00020 * in the root directory of this software component. 00021 * If no LICENSE file comes with this software, it is provided AS-IS. 00022 * 00023 ****************************************************************************** 00024 @verbatim 00025 ============================================================================== 00026 ##### How to use this driver ##### 00027 ============================================================================== 00028 [..] 00029 This driver implements a high level communication layer for read and write from/to 00030 this memory. The needed STM32 hardware resources (SDMMC and GPIO) are performed by 00031 the user in HAL_SD_MspInit() function (MSP layer). 00032 Basically, the MSP layer configuration should be the same as we provide in the 00033 examples. 00034 You can easily tailor this configuration according to hardware resources. 00035 00036 [..] 00037 This driver is a generic layered driver for SDMMC memories which uses the HAL 00038 SDMMC driver functions to interface with SD and uSD cards devices. 00039 It is used as follows: 00040 00041 (#)Initialize the SDMMC low level resources by implementing the HAL_SD_MspInit() API: 00042 (##) Enable the SDMMC interface clock using __HAL_RCC_SDMMC_CLK_ENABLE(); 00043 (##) SDMMC pins configuration for SD card 00044 (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE(); 00045 (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init() 00046 and according to your pin assignment; 00047 (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT() 00048 and HAL_SD_WriteBlocks_IT() APIs). 00049 (+++) Configure the SDMMC interrupt priorities using function HAL_NVIC_SetPriority(); 00050 (+++) Enable the NVIC SDMMC IRQs using function HAL_NVIC_EnableIRQ() 00051 (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT() 00052 and __HAL_SD_DISABLE_IT() inside the communication process. 00053 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT() 00054 and __HAL_SD_CLEAR_IT() 00055 (##) No general propose DMA Configuration is needed, an Internal DMA for SDMMC Peripheral are used. 00056 00057 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization 00058 00059 00060 *** SD Card Initialization and configuration *** 00061 ================================================ 00062 [..] 00063 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes 00064 SDMMC Peripheral(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer). 00065 This function provide the following operations: 00066 00067 (#) Apply the SD Card initialization process at 400KHz and check the SD Card 00068 type (Standard Capacity or High Capacity). You can change or adapt this 00069 frequency by adjusting the "ClockDiv" field. 00070 The SD Card frequency (SDMMC_CK) is computed as follows: 00071 00072 SDMMC_CK = SDMMCCLK / (2 * ClockDiv) 00073 00074 In initialization mode and according to the SD Card standard, 00075 make sure that the SDMMC_CK frequency doesn't exceed 400KHz. 00076 00077 This phase of initialization is done through SDMMC_Init() and 00078 SDMMC_PowerState_ON() SDMMC low level APIs. 00079 00080 (#) Initialize the SD card. The API used is HAL_SD_InitCard(). 00081 This phase allows the card initialization and identification 00082 and check the SD Card type (Standard Capacity or High Capacity) 00083 The initialization flow is compatible with SD standard. 00084 00085 This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case 00086 of plug-off plug-in. 00087 00088 (#) Configure the SD Card Data transfer frequency. You can change or adapt this 00089 frequency by adjusting the "ClockDiv" field. 00090 In transfer mode and according to the SD Card standard, make sure that the 00091 SDMMC_CK frequency doesn't exceed 25MHz and 100MHz in High-speed mode switch. 00092 00093 (#) Select the corresponding SD Card according to the address read with the step 2. 00094 00095 (#) Configure the SD Card in wide bus mode: 4-bits data. 00096 00097 *** SD Card Read operation *** 00098 ============================== 00099 [..] 00100 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks(). 00101 This function support only 512-bytes block length (the block size should be 00102 chosen as 512 bytes). 00103 You can choose either one block read operation or multiple block read operation 00104 by adjusting the "NumberOfBlocks" parameter. 00105 After this, you have to ensure that the transfer is done correctly. The check is done 00106 through HAL_SD_GetCardState() function for SD card state. 00107 00108 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA(). 00109 This function support only 512-bytes block length (the block size should be 00110 chosen as 512 bytes). 00111 You can choose either one block read operation or multiple block read operation 00112 by adjusting the "NumberOfBlocks" parameter. 00113 After this, you have to ensure that the transfer is done correctly. The check is done 00114 through HAL_SD_GetCardState() function for SD card state. 00115 You could also check the DMA transfer process through the SD Rx interrupt event. 00116 00117 (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT(). 00118 This function support only 512-bytes block length (the block size should be 00119 chosen as 512 bytes). 00120 You can choose either one block read operation or multiple block read operation 00121 by adjusting the "NumberOfBlocks" parameter. 00122 After this, you have to ensure that the transfer is done correctly. The check is done 00123 through HAL_SD_GetCardState() function for SD card state. 00124 You could also check the IT transfer process through the SD Rx interrupt event. 00125 00126 *** SD Card Write operation *** 00127 =============================== 00128 [..] 00129 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks(). 00130 This function support only 512-bytes block length (the block size should be 00131 chosen as 512 bytes). 00132 You can choose either one block read operation or multiple block read operation 00133 by adjusting the "NumberOfBlocks" parameter. 00134 After this, you have to ensure that the transfer is done correctly. The check is done 00135 through HAL_SD_GetCardState() function for SD card state. 00136 00137 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA(). 00138 This function support only 512-bytes block length (the block size should be 00139 chosen as 512 bytes). 00140 You can choose either one block read operation or multiple block read operation 00141 by adjusting the "NumberOfBlocks" parameter. 00142 After this, you have to ensure that the transfer is done correctly. The check is done 00143 through HAL_SD_GetCardState() function for SD card state. 00144 You could also check the DMA transfer process through the SD Tx interrupt event. 00145 00146 (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT(). 00147 This function support only 512-bytes block length (the block size should be 00148 chosen as 512 bytes). 00149 You can choose either one block read operation or multiple block read operation 00150 by adjusting the "NumberOfBlocks" parameter. 00151 After this, you have to ensure that the transfer is done correctly. The check is done 00152 through HAL_SD_GetCardState() function for SD card state. 00153 You could also check the IT transfer process through the SD Tx interrupt event. 00154 00155 *** SD card status *** 00156 ====================== 00157 [..] 00158 (+) The SD Status contains status bits that are related to the SD Memory 00159 Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus(). 00160 00161 *** SD card information *** 00162 =========================== 00163 [..] 00164 (+) To get SD card information, you can use the function HAL_SD_GetCardInfo(). 00165 It returns useful information about the SD card such as block size, card type, 00166 block number ... 00167 00168 *** SD card CSD register *** 00169 ============================ 00170 (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register. 00171 Some of the CSD parameters are useful for card initialization and identification. 00172 00173 *** SD card CID register *** 00174 ============================ 00175 (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register. 00176 Some of the CSD parameters are useful for card initialization and identification. 00177 00178 *** SD HAL driver macros list *** 00179 ================================== 00180 [..] 00181 Below the list of most used macros in SD HAL driver. 00182 00183 (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt 00184 (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt 00185 (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not 00186 (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags 00187 00188 (@) You can refer to the SD HAL driver header file for more useful macros 00189 00190 *** Callback registration *** 00191 ============================================= 00192 [..] 00193 The compilation define USE_HAL_SD_REGISTER_CALLBACKS when set to 1 00194 allows the user to configure dynamically the driver callbacks. 00195 00196 Use Functions HAL_SD_RegisterCallback() to register a user callback, 00197 it allows to register following callbacks: 00198 (+) TxCpltCallback : callback when a transmission transfer is completed. 00199 (+) RxCpltCallback : callback when a reception transfer is completed. 00200 (+) ErrorCallback : callback when error occurs. 00201 (+) AbortCpltCallback : callback when abort is completed. 00202 (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed. 00203 (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed. 00204 (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed. 00205 (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed. 00206 (+) MspInitCallback : SD MspInit. 00207 (+) MspDeInitCallback : SD MspDeInit. 00208 This function takes as parameters the HAL peripheral handle, the Callback ID 00209 and a pointer to the user callback function. 00210 For specific callbacks TransceiverCallback use dedicated register callbacks: 00211 respectively HAL_SD_RegisterTransceiverCallback(). 00212 00213 Use function HAL_SD_UnRegisterCallback() to reset a callback to the default 00214 weak (surcharged) function. It allows to reset following callbacks: 00215 (+) TxCpltCallback : callback when a transmission transfer is completed. 00216 (+) RxCpltCallback : callback when a reception transfer is completed. 00217 (+) ErrorCallback : callback when error occurs. 00218 (+) AbortCpltCallback : callback when abort is completed. 00219 (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed. 00220 (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed. 00221 (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed. 00222 (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed. 00223 (+) MspInitCallback : SD MspInit. 00224 (+) MspDeInitCallback : SD MspDeInit. 00225 This function) takes as parameters the HAL peripheral handle and the Callback ID. 00226 For specific callbacks TransceiverCallback use dedicated unregister callbacks: 00227 respectively HAL_SD_UnRegisterTransceiverCallback(). 00228 00229 By default, after the HAL_SD_Init and if the state is HAL_SD_STATE_RESET 00230 all callbacks are reset to the corresponding legacy weak (surcharged) functions. 00231 Exception done for MspInit and MspDeInit callbacks that are respectively 00232 reset to the legacy weak (surcharged) functions in the HAL_SD_Init 00233 and HAL_SD_DeInit only when these callbacks are null (not registered beforehand). 00234 If not, MspInit or MspDeInit are not null, the HAL_SD_Init and HAL_SD_DeInit 00235 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) 00236 00237 Callbacks can be registered/unregistered in READY state only. 00238 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered 00239 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used 00240 during the Init/DeInit. 00241 In that case first register the MspInit/MspDeInit user callbacks 00242 using HAL_SD_RegisterCallback before calling HAL_SD_DeInit 00243 or HAL_SD_Init function. 00244 00245 When The compilation define USE_HAL_SD_REGISTER_CALLBACKS is set to 0 or 00246 not defined, the callback registering feature is not available 00247 and weak (surcharged) callbacks are used. 00248 00249 @endverbatim 00250 ****************************************************************************** 00251 */ 00252 00253 /* Includes ------------------------------------------------------------------*/ 00254 #include "stm32h7xx_hal.h" 00255 00256 /** @addtogroup STM32H7xx_HAL_Driver 00257 * @{ 00258 */ 00259 00260 /** @addtogroup SD 00261 * @{ 00262 */ 00263 00264 #ifdef HAL_SD_MODULE_ENABLED 00265 00266 /* Private typedef -----------------------------------------------------------*/ 00267 /* Private define ------------------------------------------------------------*/ 00268 /** @addtogroup SD_Private_Defines 00269 * @{ 00270 */ 00271 /* Frequencies used in the driver for clock divider calculation */ 00272 #define SD_INIT_FREQ 400000U /* Initialization phase : 400 kHz max */ 00273 #define SD_NORMAL_SPEED_FREQ 25000000U /* Normal speed phase : 25 MHz max */ 00274 #define SD_HIGH_SPEED_FREQ 50000000U /* High speed phase : 50 MHz max */ 00275 /* Private macro -------------------------------------------------------------*/ 00276 #if defined (DLYB_SDMMC1) && defined (DLYB_SDMMC2) 00277 #define SD_GET_DLYB_INSTANCE(SDMMC_INSTANCE) (((SDMMC_INSTANCE) == SDMMC1)? \ 00278 DLYB_SDMMC1 : DLYB_SDMMC2 ) 00279 #elif defined (DLYB_SDMMC1) 00280 #define SD_GET_DLYB_INSTANCE(SDMMC_INSTANCE) ( DLYB_SDMMC1 ) 00281 #endif /* (DLYB_SDMMC1) && defined (DLYB_SDMMC2) */ 00282 00283 /** 00284 * @} 00285 */ 00286 00287 /* Private variables ---------------------------------------------------------*/ 00288 /* Private function prototypes -----------------------------------------------*/ 00289 /* Private functions ---------------------------------------------------------*/ 00290 /** @defgroup SD_Private_Functions SD Private Functions 00291 * @{ 00292 */ 00293 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd); 00294 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd); 00295 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus); 00296 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus); 00297 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd); 00298 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd); 00299 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR); 00300 static void SD_PowerOFF(SD_HandleTypeDef *hsd); 00301 static void SD_Write_IT(SD_HandleTypeDef *hsd); 00302 static void SD_Read_IT(SD_HandleTypeDef *hsd); 00303 static uint32_t SD_HighSpeed(SD_HandleTypeDef *hsd); 00304 #if (USE_SD_TRANSCEIVER != 0U) 00305 static uint32_t SD_UltraHighSpeed(SD_HandleTypeDef *hsd); 00306 static uint32_t SD_DDR_Mode(SD_HandleTypeDef *hsd); 00307 #endif /* USE_SD_TRANSCEIVER */ 00308 /** 00309 * @} 00310 */ 00311 00312 /* Exported functions --------------------------------------------------------*/ 00313 /** @addtogroup SD_Exported_Functions 00314 * @{ 00315 */ 00316 00317 /** @addtogroup SD_Exported_Functions_Group1 00318 * @brief Initialization and de-initialization functions 00319 * 00320 @verbatim 00321 ============================================================================== 00322 ##### Initialization and de-initialization functions ##### 00323 ============================================================================== 00324 [..] 00325 This section provides functions allowing to initialize/de-initialize the SD 00326 card device to be ready for use. 00327 00328 @endverbatim 00329 * @{ 00330 */ 00331 00332 /** 00333 * @brief Initializes the SD according to the specified parameters in the 00334 SD_HandleTypeDef and create the associated handle. 00335 * @param hsd: Pointer to the SD handle 00336 * @retval HAL status 00337 */ 00338 HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd) 00339 { 00340 HAL_SD_CardStatusTypeDef CardStatus; 00341 uint32_t speedgrade; 00342 uint32_t unitsize; 00343 uint32_t tickstart; 00344 00345 /* Check the SD handle allocation */ 00346 if (hsd == NULL) 00347 { 00348 return HAL_ERROR; 00349 } 00350 00351 /* Check the parameters */ 00352 assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance)); 00353 assert_param(IS_SDMMC_CLOCK_EDGE(hsd->Init.ClockEdge)); 00354 assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave)); 00355 assert_param(IS_SDMMC_BUS_WIDE(hsd->Init.BusWide)); 00356 assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl)); 00357 assert_param(IS_SDMMC_CLKDIV(hsd->Init.ClockDiv)); 00358 00359 if (hsd->State == HAL_SD_STATE_RESET) 00360 { 00361 /* Allocate lock resource and initialize it */ 00362 hsd->Lock = HAL_UNLOCKED; 00363 00364 #if (USE_SD_TRANSCEIVER != 0U) 00365 /* Force SDMMC_TRANSCEIVER_PRESENT for Legacy usage */ 00366 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_UNKNOWN) 00367 { 00368 hsd->Init.TranceiverPresent = SDMMC_TRANSCEIVER_PRESENT; 00369 } 00370 #endif /*USE_SD_TRANSCEIVER */ 00371 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 00372 /* Reset Callback pointers in HAL_SD_STATE_RESET only */ 00373 hsd->TxCpltCallback = HAL_SD_TxCpltCallback; 00374 hsd->RxCpltCallback = HAL_SD_RxCpltCallback; 00375 hsd->ErrorCallback = HAL_SD_ErrorCallback; 00376 hsd->AbortCpltCallback = HAL_SD_AbortCallback; 00377 hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuf0CpltCallback; 00378 hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuf1CpltCallback; 00379 hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuf0CpltCallback; 00380 hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuf1CpltCallback; 00381 #if (USE_SD_TRANSCEIVER != 0U) 00382 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT) 00383 { 00384 hsd->DriveTransceiver_1_8V_Callback = HAL_SD_DriveTransceiver_1_8V_Callback; 00385 } 00386 #endif /* USE_SD_TRANSCEIVER */ 00387 00388 if (hsd->MspInitCallback == NULL) 00389 { 00390 hsd->MspInitCallback = HAL_SD_MspInit; 00391 } 00392 00393 /* Init the low level hardware */ 00394 hsd->MspInitCallback(hsd); 00395 #else 00396 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */ 00397 HAL_SD_MspInit(hsd); 00398 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 00399 } 00400 00401 hsd->State = HAL_SD_STATE_PROGRAMMING; 00402 00403 /* Initialize the Card parameters */ 00404 if (HAL_SD_InitCard(hsd) != HAL_OK) 00405 { 00406 return HAL_ERROR; 00407 } 00408 00409 if (HAL_SD_GetCardStatus(hsd, &CardStatus) != HAL_OK) 00410 { 00411 return HAL_ERROR; 00412 } 00413 /* Get Initial Card Speed from Card Status*/ 00414 speedgrade = CardStatus.UhsSpeedGrade; 00415 unitsize = CardStatus.UhsAllocationUnitSize; 00416 if ((hsd->SdCard.CardType == CARD_SDHC_SDXC) && ((speedgrade != 0U) || (unitsize != 0U))) 00417 { 00418 hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED; 00419 } 00420 else 00421 { 00422 if (hsd->SdCard.CardType == CARD_SDHC_SDXC) 00423 { 00424 hsd->SdCard.CardSpeed = CARD_HIGH_SPEED; 00425 } 00426 else 00427 { 00428 hsd->SdCard.CardSpeed = CARD_NORMAL_SPEED; 00429 } 00430 00431 } 00432 /* Configure the bus wide */ 00433 if (HAL_SD_ConfigWideBusOperation(hsd, hsd->Init.BusWide) != HAL_OK) 00434 { 00435 return HAL_ERROR; 00436 } 00437 00438 /* Verify that SD card is ready to use after Initialization */ 00439 tickstart = HAL_GetTick(); 00440 while ((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER)) 00441 { 00442 if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 00443 { 00444 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT; 00445 hsd->State = HAL_SD_STATE_READY; 00446 return HAL_TIMEOUT; 00447 } 00448 } 00449 00450 /* Initialize the error code */ 00451 hsd->ErrorCode = HAL_SD_ERROR_NONE; 00452 00453 /* Initialize the SD operation */ 00454 hsd->Context = SD_CONTEXT_NONE; 00455 00456 /* Initialize the SD state */ 00457 hsd->State = HAL_SD_STATE_READY; 00458 00459 return HAL_OK; 00460 } 00461 00462 /** 00463 * @brief Initializes the SD Card. 00464 * @param hsd: Pointer to SD handle 00465 * @note This function initializes the SD card. It could be used when a card 00466 re-initialization is needed. 00467 * @retval HAL status 00468 */ 00469 HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd) 00470 { 00471 uint32_t errorstate; 00472 SD_InitTypeDef Init; 00473 uint32_t sdmmc_clk = 0U; 00474 00475 /* Default SDMMC peripheral configuration for SD card initialization */ 00476 Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING; 00477 Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE; 00478 Init.BusWide = SDMMC_BUS_WIDE_1B; 00479 Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE; 00480 00481 /* Init Clock should be less or equal to 400Khz*/ 00482 sdmmc_clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SDMMC); 00483 if (sdmmc_clk == 0U) 00484 { 00485 hsd->State = HAL_SD_STATE_READY; 00486 hsd->ErrorCode = SDMMC_ERROR_INVALID_PARAMETER; 00487 return HAL_ERROR; 00488 } 00489 Init.ClockDiv = sdmmc_clk / (2U * SD_INIT_FREQ); 00490 00491 #if (USE_SD_TRANSCEIVER != 0U) 00492 Init.TranceiverPresent = hsd->Init.TranceiverPresent; 00493 00494 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT) 00495 { 00496 /* Set Transceiver polarity */ 00497 hsd->Instance->POWER |= SDMMC_POWER_DIRPOL; 00498 } 00499 #elif defined (USE_SD_DIRPOL) 00500 /* Set Transceiver polarity */ 00501 hsd->Instance->POWER |= SDMMC_POWER_DIRPOL; 00502 #endif /* USE_SD_TRANSCEIVER */ 00503 00504 /* Initialize SDMMC peripheral interface with default configuration */ 00505 (void)SDMMC_Init(hsd->Instance, Init); 00506 00507 /* Set Power State to ON */ 00508 (void)SDMMC_PowerState_ON(hsd->Instance); 00509 00510 /* wait 74 Cycles: required power up waiting time before starting 00511 the SD initialization sequence */ 00512 sdmmc_clk = sdmmc_clk / (2U * Init.ClockDiv); 00513 HAL_Delay(1U + (74U * 1000U / (sdmmc_clk))); 00514 00515 /* Identify card operating voltage */ 00516 errorstate = SD_PowerON(hsd); 00517 if (errorstate != HAL_SD_ERROR_NONE) 00518 { 00519 hsd->State = HAL_SD_STATE_READY; 00520 hsd->ErrorCode |= errorstate; 00521 return HAL_ERROR; 00522 } 00523 00524 /* Card initialization */ 00525 errorstate = SD_InitCard(hsd); 00526 if (errorstate != HAL_SD_ERROR_NONE) 00527 { 00528 hsd->State = HAL_SD_STATE_READY; 00529 hsd->ErrorCode |= errorstate; 00530 return HAL_ERROR; 00531 } 00532 00533 /* Set Block Size for Card */ 00534 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 00535 if (errorstate != HAL_SD_ERROR_NONE) 00536 { 00537 /* Clear all the static flags */ 00538 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00539 hsd->ErrorCode |= errorstate; 00540 hsd->State = HAL_SD_STATE_READY; 00541 return HAL_ERROR; 00542 } 00543 00544 return HAL_OK; 00545 } 00546 00547 /** 00548 * @brief De-Initializes the SD card. 00549 * @param hsd: Pointer to SD handle 00550 * @retval HAL status 00551 */ 00552 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd) 00553 { 00554 /* Check the SD handle allocation */ 00555 if (hsd == NULL) 00556 { 00557 return HAL_ERROR; 00558 } 00559 00560 /* Check the parameters */ 00561 assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance)); 00562 00563 hsd->State = HAL_SD_STATE_BUSY; 00564 00565 #if (USE_SD_TRANSCEIVER != 0U) 00566 /* Deactivate the 1.8V Mode */ 00567 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT) 00568 { 00569 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 00570 if (hsd->DriveTransceiver_1_8V_Callback == NULL) 00571 { 00572 hsd->DriveTransceiver_1_8V_Callback = HAL_SD_DriveTransceiver_1_8V_Callback; 00573 } 00574 hsd->DriveTransceiver_1_8V_Callback(RESET); 00575 #else 00576 HAL_SD_DriveTransceiver_1_8V_Callback(RESET); 00577 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 00578 } 00579 #endif /* USE_SD_TRANSCEIVER */ 00580 00581 /* Set SD power state to off */ 00582 SD_PowerOFF(hsd); 00583 00584 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 00585 if (hsd->MspDeInitCallback == NULL) 00586 { 00587 hsd->MspDeInitCallback = HAL_SD_MspDeInit; 00588 } 00589 00590 /* DeInit the low level hardware */ 00591 hsd->MspDeInitCallback(hsd); 00592 #else 00593 /* De-Initialize the MSP layer */ 00594 HAL_SD_MspDeInit(hsd); 00595 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 00596 00597 hsd->ErrorCode = HAL_SD_ERROR_NONE; 00598 hsd->State = HAL_SD_STATE_RESET; 00599 00600 return HAL_OK; 00601 } 00602 00603 00604 /** 00605 * @brief Initializes the SD MSP. 00606 * @param hsd: Pointer to SD handle 00607 * @retval None 00608 */ 00609 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd) 00610 { 00611 /* Prevent unused argument(s) compilation warning */ 00612 UNUSED(hsd); 00613 00614 /* NOTE : This function should not be modified, when the callback is needed, 00615 the HAL_SD_MspInit could be implemented in the user file 00616 */ 00617 } 00618 00619 /** 00620 * @brief De-Initialize SD MSP. 00621 * @param hsd: Pointer to SD handle 00622 * @retval None 00623 */ 00624 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) 00625 { 00626 /* Prevent unused argument(s) compilation warning */ 00627 UNUSED(hsd); 00628 00629 /* NOTE : This function should not be modified, when the callback is needed, 00630 the HAL_SD_MspDeInit could be implemented in the user file 00631 */ 00632 } 00633 00634 /** 00635 * @} 00636 */ 00637 00638 /** @addtogroup SD_Exported_Functions_Group2 00639 * @brief Data transfer functions 00640 * 00641 @verbatim 00642 ============================================================================== 00643 ##### IO operation functions ##### 00644 ============================================================================== 00645 [..] 00646 This subsection provides a set of functions allowing to manage the data 00647 transfer from/to SD card. 00648 00649 @endverbatim 00650 * @{ 00651 */ 00652 00653 /** 00654 * @brief Reads block(s) from a specified address in a card. The Data transfer 00655 * is managed by polling mode. 00656 * @note This API should be followed by a check on the card state through 00657 * HAL_SD_GetCardState(). 00658 * @param hsd: Pointer to SD handle 00659 * @param pData: pointer to the buffer that will contain the received data 00660 * @param BlockAdd: Block Address from where data is to be read 00661 * @param NumberOfBlocks: Number of SD blocks to read 00662 * @param Timeout: Specify timeout value 00663 * @retval HAL status 00664 */ 00665 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, 00666 uint32_t Timeout) 00667 { 00668 SDMMC_DataInitTypeDef config; 00669 uint32_t errorstate; 00670 uint32_t tickstart = HAL_GetTick(); 00671 uint32_t count; 00672 uint32_t data; 00673 uint32_t dataremaining; 00674 uint32_t add = BlockAdd; 00675 uint8_t *tempbuff = pData; 00676 00677 if (NULL == pData) 00678 { 00679 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 00680 return HAL_ERROR; 00681 } 00682 00683 if (hsd->State == HAL_SD_STATE_READY) 00684 { 00685 hsd->ErrorCode = HAL_SD_ERROR_NONE; 00686 00687 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 00688 { 00689 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 00690 return HAL_ERROR; 00691 } 00692 00693 hsd->State = HAL_SD_STATE_BUSY; 00694 00695 /* Initialize data control register */ 00696 hsd->Instance->DCTRL = 0U; 00697 00698 if (hsd->SdCard.CardType != CARD_SDHC_SDXC) 00699 { 00700 add *= 512U; 00701 } 00702 00703 /* Configure the SD DPSM (Data Path State Machine) */ 00704 config.DataTimeOut = SDMMC_DATATIMEOUT; 00705 config.DataLength = NumberOfBlocks * BLOCKSIZE; 00706 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B; 00707 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 00708 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 00709 config.DPSM = SDMMC_DPSM_DISABLE; 00710 (void)SDMMC_ConfigData(hsd->Instance, &config); 00711 __SDMMC_CMDTRANS_ENABLE(hsd->Instance); 00712 00713 /* Read block(s) in polling mode */ 00714 if (NumberOfBlocks > 1U) 00715 { 00716 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK; 00717 00718 /* Read Multi Block command */ 00719 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add); 00720 } 00721 else 00722 { 00723 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK; 00724 00725 /* Read Single Block command */ 00726 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add); 00727 } 00728 if (errorstate != HAL_SD_ERROR_NONE) 00729 { 00730 /* Clear all the static flags */ 00731 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00732 hsd->ErrorCode |= errorstate; 00733 hsd->State = HAL_SD_STATE_READY; 00734 hsd->Context = SD_CONTEXT_NONE; 00735 return HAL_ERROR; 00736 } 00737 00738 /* Poll on SDMMC flags */ 00739 dataremaining = config.DataLength; 00740 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND)) 00741 { 00742 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) && (dataremaining >= 32U)) 00743 { 00744 /* Read data from SDMMC Rx FIFO */ 00745 for (count = 0U; count < 8U; count++) 00746 { 00747 data = SDMMC_ReadFIFO(hsd->Instance); 00748 *tempbuff = (uint8_t)(data & 0xFFU); 00749 tempbuff++; 00750 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU); 00751 tempbuff++; 00752 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU); 00753 tempbuff++; 00754 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU); 00755 tempbuff++; 00756 } 00757 dataremaining -= 32U; 00758 } 00759 00760 if (((HAL_GetTick() - tickstart) >= Timeout) || (Timeout == 0U)) 00761 { 00762 /* Clear all the static flags */ 00763 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00764 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT; 00765 hsd->State = HAL_SD_STATE_READY; 00766 hsd->Context = SD_CONTEXT_NONE; 00767 return HAL_TIMEOUT; 00768 } 00769 } 00770 __SDMMC_CMDTRANS_DISABLE(hsd->Instance); 00771 00772 /* Send stop transmission command in case of multiblock read */ 00773 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U)) 00774 { 00775 if (hsd->SdCard.CardType != CARD_SECURED) 00776 { 00777 /* Send stop transmission command */ 00778 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 00779 if (errorstate != HAL_SD_ERROR_NONE) 00780 { 00781 /* Clear all the static flags */ 00782 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00783 hsd->ErrorCode |= errorstate; 00784 hsd->State = HAL_SD_STATE_READY; 00785 hsd->Context = SD_CONTEXT_NONE; 00786 return HAL_ERROR; 00787 } 00788 } 00789 } 00790 00791 /* Get error state */ 00792 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 00793 { 00794 /* Clear all the static flags */ 00795 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00796 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; 00797 hsd->State = HAL_SD_STATE_READY; 00798 hsd->Context = SD_CONTEXT_NONE; 00799 return HAL_ERROR; 00800 } 00801 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 00802 { 00803 /* Clear all the static flags */ 00804 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00805 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; 00806 hsd->State = HAL_SD_STATE_READY; 00807 hsd->Context = SD_CONTEXT_NONE; 00808 return HAL_ERROR; 00809 } 00810 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR)) 00811 { 00812 /* Clear all the static flags */ 00813 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00814 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN; 00815 hsd->State = HAL_SD_STATE_READY; 00816 hsd->Context = SD_CONTEXT_NONE; 00817 return HAL_ERROR; 00818 } 00819 else 00820 { 00821 /* Nothing to do */ 00822 } 00823 00824 /* Clear all the static flags */ 00825 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS); 00826 00827 hsd->State = HAL_SD_STATE_READY; 00828 00829 return HAL_OK; 00830 } 00831 else 00832 { 00833 hsd->ErrorCode |= HAL_SD_ERROR_BUSY; 00834 return HAL_ERROR; 00835 } 00836 } 00837 00838 /** 00839 * @brief Allows to write block(s) to a specified address in a card. The Data 00840 * transfer is managed by polling mode. 00841 * @note This API should be followed by a check on the card state through 00842 * HAL_SD_GetCardState(). 00843 * @param hsd: Pointer to SD handle 00844 * @param pData: pointer to the buffer that will contain the data to transmit 00845 * @param BlockAdd: Block Address where data will be written 00846 * @param NumberOfBlocks: Number of SD blocks to write 00847 * @param Timeout: Specify timeout value 00848 * @retval HAL status 00849 */ 00850 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, 00851 uint32_t Timeout) 00852 { 00853 SDMMC_DataInitTypeDef config; 00854 uint32_t errorstate; 00855 uint32_t tickstart = HAL_GetTick(); 00856 uint32_t count; 00857 uint32_t data; 00858 uint32_t dataremaining; 00859 uint32_t add = BlockAdd; 00860 uint8_t *tempbuff = pData; 00861 00862 if (NULL == pData) 00863 { 00864 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 00865 return HAL_ERROR; 00866 } 00867 00868 if (hsd->State == HAL_SD_STATE_READY) 00869 { 00870 hsd->ErrorCode = HAL_SD_ERROR_NONE; 00871 00872 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 00873 { 00874 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 00875 return HAL_ERROR; 00876 } 00877 00878 hsd->State = HAL_SD_STATE_BUSY; 00879 00880 /* Initialize data control register */ 00881 hsd->Instance->DCTRL = 0U; 00882 00883 if (hsd->SdCard.CardType != CARD_SDHC_SDXC) 00884 { 00885 add *= 512U; 00886 } 00887 00888 /* Configure the SD DPSM (Data Path State Machine) */ 00889 config.DataTimeOut = SDMMC_DATATIMEOUT; 00890 config.DataLength = NumberOfBlocks * BLOCKSIZE; 00891 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B; 00892 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD; 00893 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 00894 config.DPSM = SDMMC_DPSM_DISABLE; 00895 (void)SDMMC_ConfigData(hsd->Instance, &config); 00896 __SDMMC_CMDTRANS_ENABLE(hsd->Instance); 00897 00898 /* Write Blocks in Polling mode */ 00899 if (NumberOfBlocks > 1U) 00900 { 00901 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK; 00902 00903 /* Write Multi Block command */ 00904 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add); 00905 } 00906 else 00907 { 00908 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK; 00909 00910 /* Write Single Block command */ 00911 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add); 00912 } 00913 if (errorstate != HAL_SD_ERROR_NONE) 00914 { 00915 /* Clear all the static flags */ 00916 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00917 hsd->ErrorCode |= errorstate; 00918 hsd->State = HAL_SD_STATE_READY; 00919 hsd->Context = SD_CONTEXT_NONE; 00920 return HAL_ERROR; 00921 } 00922 00923 /* Write block(s) in polling mode */ 00924 dataremaining = config.DataLength; 00925 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | 00926 SDMMC_FLAG_DATAEND)) 00927 { 00928 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) && (dataremaining >= 32U)) 00929 { 00930 /* Write data to SDMMC Tx FIFO */ 00931 for (count = 0U; count < 8U; count++) 00932 { 00933 data = (uint32_t)(*tempbuff); 00934 tempbuff++; 00935 data |= ((uint32_t)(*tempbuff) << 8U); 00936 tempbuff++; 00937 data |= ((uint32_t)(*tempbuff) << 16U); 00938 tempbuff++; 00939 data |= ((uint32_t)(*tempbuff) << 24U); 00940 tempbuff++; 00941 (void)SDMMC_WriteFIFO(hsd->Instance, &data); 00942 } 00943 dataremaining -= 32U; 00944 } 00945 00946 if (((HAL_GetTick() - tickstart) >= Timeout) || (Timeout == 0U)) 00947 { 00948 /* Clear all the static flags */ 00949 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00950 hsd->ErrorCode |= errorstate; 00951 hsd->State = HAL_SD_STATE_READY; 00952 hsd->Context = SD_CONTEXT_NONE; 00953 return HAL_TIMEOUT; 00954 } 00955 } 00956 __SDMMC_CMDTRANS_DISABLE(hsd->Instance); 00957 00958 /* Send stop transmission command in case of multiblock write */ 00959 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U)) 00960 { 00961 if (hsd->SdCard.CardType != CARD_SECURED) 00962 { 00963 /* Send stop transmission command */ 00964 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 00965 if (errorstate != HAL_SD_ERROR_NONE) 00966 { 00967 /* Clear all the static flags */ 00968 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00969 hsd->ErrorCode |= errorstate; 00970 hsd->State = HAL_SD_STATE_READY; 00971 hsd->Context = SD_CONTEXT_NONE; 00972 return HAL_ERROR; 00973 } 00974 } 00975 } 00976 00977 /* Get error state */ 00978 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 00979 { 00980 /* Clear all the static flags */ 00981 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00982 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; 00983 hsd->State = HAL_SD_STATE_READY; 00984 hsd->Context = SD_CONTEXT_NONE; 00985 return HAL_ERROR; 00986 } 00987 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 00988 { 00989 /* Clear all the static flags */ 00990 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 00991 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; 00992 hsd->State = HAL_SD_STATE_READY; 00993 hsd->Context = SD_CONTEXT_NONE; 00994 return HAL_ERROR; 00995 } 00996 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR)) 00997 { 00998 /* Clear all the static flags */ 00999 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01000 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN; 01001 hsd->State = HAL_SD_STATE_READY; 01002 hsd->Context = SD_CONTEXT_NONE; 01003 return HAL_ERROR; 01004 } 01005 else 01006 { 01007 /* Nothing to do */ 01008 } 01009 01010 /* Clear all the static flags */ 01011 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS); 01012 01013 hsd->State = HAL_SD_STATE_READY; 01014 01015 return HAL_OK; 01016 } 01017 else 01018 { 01019 hsd->ErrorCode |= HAL_SD_ERROR_BUSY; 01020 return HAL_ERROR; 01021 } 01022 } 01023 01024 /** 01025 * @brief Reads block(s) from a specified address in a card. The Data transfer 01026 * is managed in interrupt mode. 01027 * @note This API should be followed by a check on the card state through 01028 * HAL_SD_GetCardState(). 01029 * @note You could also check the IT transfer process through the SD Rx 01030 * interrupt event. 01031 * @param hsd: Pointer to SD handle 01032 * @param pData: Pointer to the buffer that will contain the received data 01033 * @param BlockAdd: Block Address from where data is to be read 01034 * @param NumberOfBlocks: Number of blocks to read. 01035 * @retval HAL status 01036 */ 01037 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, 01038 uint32_t NumberOfBlocks) 01039 { 01040 SDMMC_DataInitTypeDef config; 01041 uint32_t errorstate; 01042 uint32_t add = BlockAdd; 01043 01044 if (NULL == pData) 01045 { 01046 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01047 return HAL_ERROR; 01048 } 01049 01050 if (hsd->State == HAL_SD_STATE_READY) 01051 { 01052 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01053 01054 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 01055 { 01056 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01057 return HAL_ERROR; 01058 } 01059 01060 hsd->State = HAL_SD_STATE_BUSY; 01061 01062 /* Initialize data control register */ 01063 hsd->Instance->DCTRL = 0U; 01064 01065 hsd->pRxBuffPtr = pData; 01066 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks; 01067 01068 if (hsd->SdCard.CardType != CARD_SDHC_SDXC) 01069 { 01070 add *= 512U; 01071 } 01072 01073 /* Configure the SD DPSM (Data Path State Machine) */ 01074 config.DataTimeOut = SDMMC_DATATIMEOUT; 01075 config.DataLength = BLOCKSIZE * NumberOfBlocks; 01076 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B; 01077 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 01078 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 01079 config.DPSM = SDMMC_DPSM_DISABLE; 01080 (void)SDMMC_ConfigData(hsd->Instance, &config); 01081 __SDMMC_CMDTRANS_ENABLE(hsd->Instance); 01082 01083 /* Read Blocks in IT mode */ 01084 if (NumberOfBlocks > 1U) 01085 { 01086 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT); 01087 01088 /* Read Multi Block command */ 01089 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add); 01090 } 01091 else 01092 { 01093 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT); 01094 01095 /* Read Single Block command */ 01096 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add); 01097 } 01098 if (errorstate != HAL_SD_ERROR_NONE) 01099 { 01100 /* Clear all the static flags */ 01101 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01102 hsd->ErrorCode |= errorstate; 01103 hsd->State = HAL_SD_STATE_READY; 01104 hsd->Context = SD_CONTEXT_NONE; 01105 return HAL_ERROR; 01106 } 01107 01108 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | 01109 SDMMC_FLAG_RXFIFOHF)); 01110 01111 return HAL_OK; 01112 } 01113 else 01114 { 01115 return HAL_BUSY; 01116 } 01117 } 01118 01119 /** 01120 * @brief Writes block(s) to a specified address in a card. The Data transfer 01121 * is managed in interrupt mode. 01122 * @note This API should be followed by a check on the card state through 01123 * HAL_SD_GetCardState(). 01124 * @note You could also check the IT transfer process through the SD Tx 01125 * interrupt event. 01126 * @param hsd: Pointer to SD handle 01127 * @param pData: Pointer to the buffer that will contain the data to transmit 01128 * @param BlockAdd: Block Address where data will be written 01129 * @param NumberOfBlocks: Number of blocks to write 01130 * @retval HAL status 01131 */ 01132 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, 01133 uint32_t NumberOfBlocks) 01134 { 01135 SDMMC_DataInitTypeDef config; 01136 uint32_t errorstate; 01137 uint32_t add = BlockAdd; 01138 01139 if (NULL == pData) 01140 { 01141 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01142 return HAL_ERROR; 01143 } 01144 01145 if (hsd->State == HAL_SD_STATE_READY) 01146 { 01147 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01148 01149 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 01150 { 01151 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01152 return HAL_ERROR; 01153 } 01154 01155 hsd->State = HAL_SD_STATE_BUSY; 01156 01157 /* Initialize data control register */ 01158 hsd->Instance->DCTRL = 0U; 01159 01160 hsd->pTxBuffPtr = pData; 01161 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks; 01162 01163 if (hsd->SdCard.CardType != CARD_SDHC_SDXC) 01164 { 01165 add *= 512U; 01166 } 01167 01168 /* Configure the SD DPSM (Data Path State Machine) */ 01169 config.DataTimeOut = SDMMC_DATATIMEOUT; 01170 config.DataLength = BLOCKSIZE * NumberOfBlocks; 01171 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B; 01172 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD; 01173 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 01174 config.DPSM = SDMMC_DPSM_DISABLE; 01175 (void)SDMMC_ConfigData(hsd->Instance, &config); 01176 01177 __SDMMC_CMDTRANS_ENABLE(hsd->Instance); 01178 01179 /* Write Blocks in Polling mode */ 01180 if (NumberOfBlocks > 1U) 01181 { 01182 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_IT); 01183 01184 /* Write Multi Block command */ 01185 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add); 01186 } 01187 else 01188 { 01189 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT); 01190 01191 /* Write Single Block command */ 01192 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add); 01193 } 01194 if (errorstate != HAL_SD_ERROR_NONE) 01195 { 01196 /* Clear all the static flags */ 01197 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01198 hsd->ErrorCode |= errorstate; 01199 hsd->State = HAL_SD_STATE_READY; 01200 hsd->Context = SD_CONTEXT_NONE; 01201 return HAL_ERROR; 01202 } 01203 01204 /* Enable transfer interrupts */ 01205 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | 01206 SDMMC_FLAG_TXFIFOHE)); 01207 01208 return HAL_OK; 01209 } 01210 else 01211 { 01212 return HAL_BUSY; 01213 } 01214 } 01215 01216 /** 01217 * @brief Reads block(s) from a specified address in a card. The Data transfer 01218 * is managed by DMA mode. 01219 * @note This API should be followed by a check on the card state through 01220 * HAL_SD_GetCardState(). 01221 * @note You could also check the DMA transfer process through the SD Rx 01222 * interrupt event. 01223 * @param hsd: Pointer SD handle 01224 * @param pData: Pointer to the buffer that will contain the received data 01225 * @param BlockAdd: Block Address from where data is to be read 01226 * @param NumberOfBlocks: Number of blocks to read. 01227 * @retval HAL status 01228 */ 01229 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, 01230 uint32_t NumberOfBlocks) 01231 { 01232 SDMMC_DataInitTypeDef config; 01233 uint32_t errorstate; 01234 uint32_t add = BlockAdd; 01235 01236 if (NULL == pData) 01237 { 01238 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01239 return HAL_ERROR; 01240 } 01241 01242 if (hsd->State == HAL_SD_STATE_READY) 01243 { 01244 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01245 01246 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 01247 { 01248 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01249 return HAL_ERROR; 01250 } 01251 01252 hsd->State = HAL_SD_STATE_BUSY; 01253 01254 /* Initialize data control register */ 01255 hsd->Instance->DCTRL = 0U; 01256 01257 hsd->pRxBuffPtr = pData; 01258 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks; 01259 01260 if (hsd->SdCard.CardType != CARD_SDHC_SDXC) 01261 { 01262 add *= 512U; 01263 } 01264 01265 /* Configure the SD DPSM (Data Path State Machine) */ 01266 config.DataTimeOut = SDMMC_DATATIMEOUT; 01267 config.DataLength = BLOCKSIZE * NumberOfBlocks; 01268 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B; 01269 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 01270 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 01271 config.DPSM = SDMMC_DPSM_DISABLE; 01272 (void)SDMMC_ConfigData(hsd->Instance, &config); 01273 01274 __SDMMC_CMDTRANS_ENABLE(hsd->Instance); 01275 hsd->Instance->IDMABASE0 = (uint32_t) pData ; 01276 hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF; 01277 01278 /* Read Blocks in DMA mode */ 01279 if (NumberOfBlocks > 1U) 01280 { 01281 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA); 01282 01283 /* Read Multi Block command */ 01284 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add); 01285 } 01286 else 01287 { 01288 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA); 01289 01290 /* Read Single Block command */ 01291 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add); 01292 } 01293 if (errorstate != HAL_SD_ERROR_NONE) 01294 { 01295 /* Clear all the static flags */ 01296 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01297 hsd->ErrorCode |= errorstate; 01298 hsd->State = HAL_SD_STATE_READY; 01299 hsd->Context = SD_CONTEXT_NONE; 01300 return HAL_ERROR; 01301 } 01302 01303 /* Enable transfer interrupts */ 01304 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND)); 01305 01306 01307 return HAL_OK; 01308 } 01309 else 01310 { 01311 return HAL_BUSY; 01312 } 01313 } 01314 01315 /** 01316 * @brief Writes block(s) to a specified address in a card. The Data transfer 01317 * is managed by DMA mode. 01318 * @note This API should be followed by a check on the card state through 01319 * HAL_SD_GetCardState(). 01320 * @note You could also check the DMA transfer process through the SD Tx 01321 * interrupt event. 01322 * @param hsd: Pointer to SD handle 01323 * @param pData: Pointer to the buffer that will contain the data to transmit 01324 * @param BlockAdd: Block Address where data will be written 01325 * @param NumberOfBlocks: Number of blocks to write 01326 * @retval HAL status 01327 */ 01328 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, 01329 uint32_t NumberOfBlocks) 01330 { 01331 SDMMC_DataInitTypeDef config; 01332 uint32_t errorstate; 01333 uint32_t add = BlockAdd; 01334 01335 if (NULL == pData) 01336 { 01337 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01338 return HAL_ERROR; 01339 } 01340 01341 if (hsd->State == HAL_SD_STATE_READY) 01342 { 01343 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01344 01345 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 01346 { 01347 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01348 return HAL_ERROR; 01349 } 01350 01351 hsd->State = HAL_SD_STATE_BUSY; 01352 01353 /* Initialize data control register */ 01354 hsd->Instance->DCTRL = 0U; 01355 01356 hsd->pTxBuffPtr = pData; 01357 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks; 01358 01359 if (hsd->SdCard.CardType != CARD_SDHC_SDXC) 01360 { 01361 add *= 512U; 01362 } 01363 01364 /* Configure the SD DPSM (Data Path State Machine) */ 01365 config.DataTimeOut = SDMMC_DATATIMEOUT; 01366 config.DataLength = BLOCKSIZE * NumberOfBlocks; 01367 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B; 01368 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD; 01369 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 01370 config.DPSM = SDMMC_DPSM_DISABLE; 01371 (void)SDMMC_ConfigData(hsd->Instance, &config); 01372 01373 01374 __SDMMC_CMDTRANS_ENABLE(hsd->Instance); 01375 01376 hsd->Instance->IDMABASE0 = (uint32_t) pData ; 01377 hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF; 01378 01379 /* Write Blocks in Polling mode */ 01380 if (NumberOfBlocks > 1U) 01381 { 01382 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA); 01383 01384 /* Write Multi Block command */ 01385 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add); 01386 } 01387 else 01388 { 01389 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA); 01390 01391 /* Write Single Block command */ 01392 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add); 01393 } 01394 if (errorstate != HAL_SD_ERROR_NONE) 01395 { 01396 /* Clear all the static flags */ 01397 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01398 hsd->ErrorCode |= errorstate; 01399 hsd->State = HAL_SD_STATE_READY; 01400 hsd->Context = SD_CONTEXT_NONE; 01401 return HAL_ERROR; 01402 } 01403 01404 /* Enable transfer interrupts */ 01405 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND)); 01406 01407 return HAL_OK; 01408 } 01409 else 01410 { 01411 return HAL_BUSY; 01412 } 01413 } 01414 01415 /** 01416 * @brief Erases the specified memory area of the given SD card. 01417 * @note This API should be followed by a check on the card state through 01418 * HAL_SD_GetCardState(). 01419 * @param hsd: Pointer to SD handle 01420 * @param BlockStartAdd: Start Block address 01421 * @param BlockEndAdd: End Block address 01422 * @retval HAL status 01423 */ 01424 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd) 01425 { 01426 uint32_t errorstate; 01427 uint32_t start_add = BlockStartAdd; 01428 uint32_t end_add = BlockEndAdd; 01429 01430 if (hsd->State == HAL_SD_STATE_READY) 01431 { 01432 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01433 01434 if (end_add < start_add) 01435 { 01436 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01437 return HAL_ERROR; 01438 } 01439 01440 if (end_add > (hsd->SdCard.LogBlockNbr)) 01441 { 01442 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01443 return HAL_ERROR; 01444 } 01445 01446 hsd->State = HAL_SD_STATE_BUSY; 01447 01448 /* Check if the card command class supports erase command */ 01449 if (((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U) 01450 { 01451 /* Clear all the static flags */ 01452 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01453 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 01454 hsd->State = HAL_SD_STATE_READY; 01455 return HAL_ERROR; 01456 } 01457 01458 if ((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 01459 { 01460 /* Clear all the static flags */ 01461 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01462 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 01463 hsd->State = HAL_SD_STATE_READY; 01464 return HAL_ERROR; 01465 } 01466 01467 /* Get start and end block for high capacity cards */ 01468 if (hsd->SdCard.CardType != CARD_SDHC_SDXC) 01469 { 01470 start_add *= 512U; 01471 end_add *= 512U; 01472 } 01473 01474 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */ 01475 if (hsd->SdCard.CardType != CARD_SECURED) 01476 { 01477 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */ 01478 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add); 01479 if (errorstate != HAL_SD_ERROR_NONE) 01480 { 01481 /* Clear all the static flags */ 01482 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01483 hsd->ErrorCode |= errorstate; 01484 hsd->State = HAL_SD_STATE_READY; 01485 return HAL_ERROR; 01486 } 01487 01488 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */ 01489 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add); 01490 if (errorstate != HAL_SD_ERROR_NONE) 01491 { 01492 /* Clear all the static flags */ 01493 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01494 hsd->ErrorCode |= errorstate; 01495 hsd->State = HAL_SD_STATE_READY; 01496 return HAL_ERROR; 01497 } 01498 } 01499 01500 /* Send CMD38 ERASE */ 01501 errorstate = SDMMC_CmdErase(hsd->Instance, 0UL); 01502 if (errorstate != HAL_SD_ERROR_NONE) 01503 { 01504 /* Clear all the static flags */ 01505 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 01506 hsd->ErrorCode |= errorstate; 01507 hsd->State = HAL_SD_STATE_READY; 01508 return HAL_ERROR; 01509 } 01510 01511 hsd->State = HAL_SD_STATE_READY; 01512 01513 return HAL_OK; 01514 } 01515 else 01516 { 01517 return HAL_BUSY; 01518 } 01519 } 01520 01521 /** 01522 * @brief This function handles SD card interrupt request. 01523 * @param hsd: Pointer to SD handle 01524 * @retval None 01525 */ 01526 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd) 01527 { 01528 uint32_t errorstate; 01529 uint32_t context = hsd->Context; 01530 01531 /* Check for SDMMC interrupt flags */ 01532 if ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U)) 01533 { 01534 SD_Read_IT(hsd); 01535 } 01536 01537 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) != RESET) 01538 { 01539 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND); 01540 01541 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | \ 01542 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE | \ 01543 SDMMC_IT_RXFIFOHF); 01544 01545 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC); 01546 __SDMMC_CMDTRANS_DISABLE(hsd->Instance); 01547 01548 if ((context & SD_CONTEXT_IT) != 0U) 01549 { 01550 if (((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)) 01551 { 01552 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 01553 if (errorstate != HAL_SD_ERROR_NONE) 01554 { 01555 hsd->ErrorCode |= errorstate; 01556 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01557 hsd->ErrorCallback(hsd); 01558 #else 01559 HAL_SD_ErrorCallback(hsd); 01560 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01561 } 01562 } 01563 01564 /* Clear all the static flags */ 01565 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS); 01566 01567 hsd->State = HAL_SD_STATE_READY; 01568 hsd->Context = SD_CONTEXT_NONE; 01569 if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U)) 01570 { 01571 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01572 hsd->RxCpltCallback(hsd); 01573 #else 01574 HAL_SD_RxCpltCallback(hsd); 01575 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01576 } 01577 else 01578 { 01579 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01580 hsd->TxCpltCallback(hsd); 01581 #else 01582 HAL_SD_TxCpltCallback(hsd); 01583 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01584 } 01585 } 01586 else if ((context & SD_CONTEXT_DMA) != 0U) 01587 { 01588 hsd->Instance->DLEN = 0; 01589 hsd->Instance->DCTRL = 0; 01590 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA; 01591 01592 /* Stop Transfer for Write Multi blocks or Read Multi blocks */ 01593 if (((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)) 01594 { 01595 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 01596 if (errorstate != HAL_SD_ERROR_NONE) 01597 { 01598 hsd->ErrorCode |= errorstate; 01599 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01600 hsd->ErrorCallback(hsd); 01601 #else 01602 HAL_SD_ErrorCallback(hsd); 01603 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01604 } 01605 } 01606 01607 hsd->State = HAL_SD_STATE_READY; 01608 hsd->Context = SD_CONTEXT_NONE; 01609 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)) 01610 { 01611 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01612 hsd->TxCpltCallback(hsd); 01613 #else 01614 HAL_SD_TxCpltCallback(hsd); 01615 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01616 } 01617 if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U)) 01618 { 01619 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01620 hsd->RxCpltCallback(hsd); 01621 #else 01622 HAL_SD_RxCpltCallback(hsd); 01623 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01624 } 01625 } 01626 else 01627 { 01628 /* Nothing to do */ 01629 } 01630 } 01631 01632 else if ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U)) 01633 { 01634 SD_Write_IT(hsd); 01635 } 01636 01637 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR | 01638 SDMMC_FLAG_TXUNDERR) != RESET) 01639 { 01640 /* Set Error code */ 01641 if (__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL) != RESET) 01642 { 01643 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; 01644 } 01645 if (__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT) != RESET) 01646 { 01647 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; 01648 } 01649 if (__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXOVERR) != RESET) 01650 { 01651 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN; 01652 } 01653 if (__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXUNDERR) != RESET) 01654 { 01655 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN; 01656 } 01657 01658 /* Clear All flags */ 01659 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS); 01660 01661 /* Disable all interrupts */ 01662 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | \ 01663 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR); 01664 01665 __SDMMC_CMDTRANS_DISABLE(hsd->Instance); 01666 hsd->Instance->DCTRL |= SDMMC_DCTRL_FIFORST; 01667 hsd->Instance->CMD |= SDMMC_CMD_CMDSTOP; 01668 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance); 01669 hsd->Instance->CMD &= ~(SDMMC_CMD_CMDSTOP); 01670 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DABORT); 01671 01672 if ((context & SD_CONTEXT_IT) != 0U) 01673 { 01674 /* Set the SD state to ready to be able to start again the process */ 01675 hsd->State = HAL_SD_STATE_READY; 01676 hsd->Context = SD_CONTEXT_NONE; 01677 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01678 hsd->ErrorCallback(hsd); 01679 #else 01680 HAL_SD_ErrorCallback(hsd); 01681 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01682 } 01683 else if ((context & SD_CONTEXT_DMA) != 0U) 01684 { 01685 if (hsd->ErrorCode != HAL_SD_ERROR_NONE) 01686 { 01687 /* Disable Internal DMA */ 01688 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC); 01689 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA; 01690 01691 /* Set the SD state to ready to be able to start again the process */ 01692 hsd->State = HAL_SD_STATE_READY; 01693 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01694 hsd->ErrorCallback(hsd); 01695 #else 01696 HAL_SD_ErrorCallback(hsd); 01697 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01698 } 01699 } 01700 else 01701 { 01702 /* Nothing to do */ 01703 } 01704 } 01705 01706 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_IDMABTC) != RESET) 01707 { 01708 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_IDMABTC); 01709 if (READ_BIT(hsd->Instance->IDMACTRL, SDMMC_IDMA_IDMABACT) == 0U) 01710 { 01711 /* Current buffer is buffer0, Transfer complete for buffer1 */ 01712 if ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U) 01713 { 01714 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01715 hsd->Write_DMADblBuf1CpltCallback(hsd); 01716 #else 01717 HAL_SDEx_Write_DMADoubleBuf1CpltCallback(hsd); 01718 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01719 } 01720 else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */ 01721 { 01722 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01723 hsd->Read_DMADblBuf1CpltCallback(hsd); 01724 #else 01725 HAL_SDEx_Read_DMADoubleBuf1CpltCallback(hsd); 01726 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01727 } 01728 } 01729 else /* SD_DMA_BUFFER1 */ 01730 { 01731 /* Current buffer is buffer1, Transfer complete for buffer0 */ 01732 if ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U) 01733 { 01734 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01735 hsd->Write_DMADblBuf0CpltCallback(hsd); 01736 #else 01737 HAL_SDEx_Write_DMADoubleBuf0CpltCallback(hsd); 01738 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01739 } 01740 else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */ 01741 { 01742 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01743 hsd->Read_DMADblBuf0CpltCallback(hsd); 01744 #else 01745 HAL_SDEx_Read_DMADoubleBuf0CpltCallback(hsd); 01746 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01747 } 01748 } 01749 } 01750 else 01751 { 01752 /* Nothing to do */ 01753 } 01754 } 01755 01756 /** 01757 * @brief return the SD state 01758 * @param hsd: Pointer to sd handle 01759 * @retval HAL state 01760 */ 01761 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd) 01762 { 01763 return hsd->State; 01764 } 01765 01766 /** 01767 * @brief Return the SD error code 01768 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains 01769 * the configuration information. 01770 * @retval SD Error Code 01771 */ 01772 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd) 01773 { 01774 return hsd->ErrorCode; 01775 } 01776 01777 /** 01778 * @brief Tx Transfer completed callbacks 01779 * @param hsd: Pointer to SD handle 01780 * @retval None 01781 */ 01782 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd) 01783 { 01784 /* Prevent unused argument(s) compilation warning */ 01785 UNUSED(hsd); 01786 01787 /* NOTE : This function should not be modified, when the callback is needed, 01788 the HAL_SD_TxCpltCallback can be implemented in the user file 01789 */ 01790 } 01791 01792 /** 01793 * @brief Rx Transfer completed callbacks 01794 * @param hsd: Pointer SD handle 01795 * @retval None 01796 */ 01797 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd) 01798 { 01799 /* Prevent unused argument(s) compilation warning */ 01800 UNUSED(hsd); 01801 01802 /* NOTE : This function should not be modified, when the callback is needed, 01803 the HAL_SD_RxCpltCallback can be implemented in the user file 01804 */ 01805 } 01806 01807 /** 01808 * @brief SD error callbacks 01809 * @param hsd: Pointer SD handle 01810 * @retval None 01811 */ 01812 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd) 01813 { 01814 /* Prevent unused argument(s) compilation warning */ 01815 UNUSED(hsd); 01816 01817 /* NOTE : This function should not be modified, when the callback is needed, 01818 the HAL_SD_ErrorCallback can be implemented in the user file 01819 */ 01820 } 01821 01822 /** 01823 * @brief SD Abort callbacks 01824 * @param hsd: Pointer SD handle 01825 * @retval None 01826 */ 01827 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd) 01828 { 01829 /* Prevent unused argument(s) compilation warning */ 01830 UNUSED(hsd); 01831 01832 /* NOTE : This function should not be modified, when the callback is needed, 01833 the HAL_SD_AbortCallback can be implemented in the user file 01834 */ 01835 } 01836 01837 #if (USE_SD_TRANSCEIVER != 0U) 01838 /** 01839 * @brief Enable/Disable the SD Transceiver 1.8V Mode Callback. 01840 * @param status: Voltage Switch State 01841 * @retval None 01842 */ 01843 __weak void HAL_SD_DriveTransceiver_1_8V_Callback(FlagStatus status) 01844 { 01845 /* Prevent unused argument(s) compilation warning */ 01846 UNUSED(status); 01847 /* NOTE : This function should not be modified, when the callback is needed, 01848 the HAL_SD_EnableTransceiver could be implemented in the user file 01849 */ 01850 } 01851 #endif /* USE_SD_TRANSCEIVER */ 01852 01853 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01854 /** 01855 * @brief Register a User SD Callback 01856 * To be used instead of the weak (surcharged) predefined callback 01857 * @param hsd : SD handle 01858 * @param CallbackID : ID of the callback to be registered 01859 * This parameter can be one of the following values: 01860 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID 01861 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID 01862 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID 01863 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID 01864 * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID 01865 * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID 01866 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID 01867 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID 01868 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID 01869 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID 01870 * @param pCallback : pointer to the Callback function 01871 * @retval status 01872 */ 01873 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, 01874 pSD_CallbackTypeDef pCallback) 01875 { 01876 HAL_StatusTypeDef status = HAL_OK; 01877 01878 if (pCallback == NULL) 01879 { 01880 /* Update the error code */ 01881 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 01882 return HAL_ERROR; 01883 } 01884 01885 /* Process locked */ 01886 __HAL_LOCK(hsd); 01887 01888 if (hsd->State == HAL_SD_STATE_READY) 01889 { 01890 switch (CallbackID) 01891 { 01892 case HAL_SD_TX_CPLT_CB_ID : 01893 hsd->TxCpltCallback = pCallback; 01894 break; 01895 case HAL_SD_RX_CPLT_CB_ID : 01896 hsd->RxCpltCallback = pCallback; 01897 break; 01898 case HAL_SD_ERROR_CB_ID : 01899 hsd->ErrorCallback = pCallback; 01900 break; 01901 case HAL_SD_ABORT_CB_ID : 01902 hsd->AbortCpltCallback = pCallback; 01903 break; 01904 case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID : 01905 hsd->Read_DMADblBuf0CpltCallback = pCallback; 01906 break; 01907 case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID : 01908 hsd->Read_DMADblBuf1CpltCallback = pCallback; 01909 break; 01910 case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID : 01911 hsd->Write_DMADblBuf0CpltCallback = pCallback; 01912 break; 01913 case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID : 01914 hsd->Write_DMADblBuf1CpltCallback = pCallback; 01915 break; 01916 case HAL_SD_MSP_INIT_CB_ID : 01917 hsd->MspInitCallback = pCallback; 01918 break; 01919 case HAL_SD_MSP_DEINIT_CB_ID : 01920 hsd->MspDeInitCallback = pCallback; 01921 break; 01922 default : 01923 /* Update the error code */ 01924 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 01925 /* update return status */ 01926 status = HAL_ERROR; 01927 break; 01928 } 01929 } 01930 else if (hsd->State == HAL_SD_STATE_RESET) 01931 { 01932 switch (CallbackID) 01933 { 01934 case HAL_SD_MSP_INIT_CB_ID : 01935 hsd->MspInitCallback = pCallback; 01936 break; 01937 case HAL_SD_MSP_DEINIT_CB_ID : 01938 hsd->MspDeInitCallback = pCallback; 01939 break; 01940 default : 01941 /* Update the error code */ 01942 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 01943 /* update return status */ 01944 status = HAL_ERROR; 01945 break; 01946 } 01947 } 01948 else 01949 { 01950 /* Update the error code */ 01951 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 01952 /* update return status */ 01953 status = HAL_ERROR; 01954 } 01955 01956 /* Release Lock */ 01957 __HAL_UNLOCK(hsd); 01958 return status; 01959 } 01960 01961 /** 01962 * @brief Unregister a User SD Callback 01963 * SD Callback is redirected to the weak (surcharged) predefined callback 01964 * @param hsd : SD handle 01965 * @param CallbackID : ID of the callback to be unregistered 01966 * This parameter can be one of the following values: 01967 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID 01968 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID 01969 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID 01970 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID 01971 * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID 01972 * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID 01973 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID 01974 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID 01975 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID 01976 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID 01977 * @retval status 01978 */ 01979 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID) 01980 { 01981 HAL_StatusTypeDef status = HAL_OK; 01982 01983 /* Process locked */ 01984 __HAL_LOCK(hsd); 01985 01986 if (hsd->State == HAL_SD_STATE_READY) 01987 { 01988 switch (CallbackID) 01989 { 01990 case HAL_SD_TX_CPLT_CB_ID : 01991 hsd->TxCpltCallback = HAL_SD_TxCpltCallback; 01992 break; 01993 case HAL_SD_RX_CPLT_CB_ID : 01994 hsd->RxCpltCallback = HAL_SD_RxCpltCallback; 01995 break; 01996 case HAL_SD_ERROR_CB_ID : 01997 hsd->ErrorCallback = HAL_SD_ErrorCallback; 01998 break; 01999 case HAL_SD_ABORT_CB_ID : 02000 hsd->AbortCpltCallback = HAL_SD_AbortCallback; 02001 break; 02002 case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID : 02003 hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuf0CpltCallback; 02004 break; 02005 case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID : 02006 hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuf1CpltCallback; 02007 break; 02008 case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID : 02009 hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuf0CpltCallback; 02010 break; 02011 case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID : 02012 hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuf1CpltCallback; 02013 break; 02014 case HAL_SD_MSP_INIT_CB_ID : 02015 hsd->MspInitCallback = HAL_SD_MspInit; 02016 break; 02017 case HAL_SD_MSP_DEINIT_CB_ID : 02018 hsd->MspDeInitCallback = HAL_SD_MspDeInit; 02019 break; 02020 default : 02021 /* Update the error code */ 02022 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 02023 /* update return status */ 02024 status = HAL_ERROR; 02025 break; 02026 } 02027 } 02028 else if (hsd->State == HAL_SD_STATE_RESET) 02029 { 02030 switch (CallbackID) 02031 { 02032 case HAL_SD_MSP_INIT_CB_ID : 02033 hsd->MspInitCallback = HAL_SD_MspInit; 02034 break; 02035 case HAL_SD_MSP_DEINIT_CB_ID : 02036 hsd->MspDeInitCallback = HAL_SD_MspDeInit; 02037 break; 02038 default : 02039 /* Update the error code */ 02040 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 02041 /* update return status */ 02042 status = HAL_ERROR; 02043 break; 02044 } 02045 } 02046 else 02047 { 02048 /* Update the error code */ 02049 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 02050 /* update return status */ 02051 status = HAL_ERROR; 02052 } 02053 02054 /* Release Lock */ 02055 __HAL_UNLOCK(hsd); 02056 return status; 02057 } 02058 02059 #if (USE_SD_TRANSCEIVER != 0U) 02060 /** 02061 * @brief Register a User SD Transceiver Callback 02062 * To be used instead of the weak (surcharged) predefined callback 02063 * @param hsd : SD handle 02064 * @param pCallback : pointer to the Callback function 02065 * @retval status 02066 */ 02067 HAL_StatusTypeDef HAL_SD_RegisterTransceiverCallback(SD_HandleTypeDef *hsd, pSD_TransceiverCallbackTypeDef pCallback) 02068 { 02069 HAL_StatusTypeDef status = HAL_OK; 02070 02071 if (pCallback == NULL) 02072 { 02073 /* Update the error code */ 02074 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 02075 return HAL_ERROR; 02076 } 02077 02078 /* Process locked */ 02079 __HAL_LOCK(hsd); 02080 02081 if (hsd->State == HAL_SD_STATE_READY) 02082 { 02083 hsd->DriveTransceiver_1_8V_Callback = pCallback; 02084 } 02085 else 02086 { 02087 /* Update the error code */ 02088 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 02089 /* update return status */ 02090 status = HAL_ERROR; 02091 } 02092 02093 /* Release Lock */ 02094 __HAL_UNLOCK(hsd); 02095 return status; 02096 } 02097 02098 /** 02099 * @brief Unregister a User SD Transceiver Callback 02100 * SD Callback is redirected to the weak (surcharged) predefined callback 02101 * @param hsd : SD handle 02102 * @retval status 02103 */ 02104 HAL_StatusTypeDef HAL_SD_UnRegisterTransceiverCallback(SD_HandleTypeDef *hsd) 02105 { 02106 HAL_StatusTypeDef status = HAL_OK; 02107 02108 /* Process locked */ 02109 __HAL_LOCK(hsd); 02110 02111 if (hsd->State == HAL_SD_STATE_READY) 02112 { 02113 hsd->DriveTransceiver_1_8V_Callback = HAL_SD_DriveTransceiver_1_8V_Callback; 02114 } 02115 else 02116 { 02117 /* Update the error code */ 02118 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 02119 /* update return status */ 02120 status = HAL_ERROR; 02121 } 02122 02123 /* Release Lock */ 02124 __HAL_UNLOCK(hsd); 02125 return status; 02126 } 02127 #endif /* USE_SD_TRANSCEIVER */ 02128 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 02129 02130 /** 02131 * @} 02132 */ 02133 02134 /** @addtogroup SD_Exported_Functions_Group3 02135 * @brief management functions 02136 * 02137 @verbatim 02138 ============================================================================== 02139 ##### Peripheral Control functions ##### 02140 ============================================================================== 02141 [..] 02142 This subsection provides a set of functions allowing to control the SD card 02143 operations and get the related information 02144 02145 @endverbatim 02146 * @{ 02147 */ 02148 02149 /** 02150 * @brief Returns information the information of the card which are stored on 02151 * the CID register. 02152 * @param hsd: Pointer to SD handle 02153 * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that 02154 * contains all CID register parameters 02155 * @retval HAL status 02156 */ 02157 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID) 02158 { 02159 pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U); 02160 02161 pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U); 02162 02163 pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U)); 02164 02165 pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU); 02166 02167 pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U); 02168 02169 pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U)); 02170 02171 pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U); 02172 02173 pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U); 02174 02175 pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U); 02176 02177 pCID->Reserved2 = 1U; 02178 02179 return HAL_OK; 02180 } 02181 02182 /** 02183 * @brief Returns information the information of the card which are stored on 02184 * the CSD register. 02185 * @param hsd: Pointer to SD handle 02186 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that 02187 * contains all CSD register parameters 02188 * @retval HAL status 02189 */ 02190 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD) 02191 { 02192 pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U); 02193 02194 pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U); 02195 02196 pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U); 02197 02198 pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U); 02199 02200 pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U); 02201 02202 pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU); 02203 02204 pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U); 02205 02206 pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U); 02207 02208 pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U); 02209 02210 pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U); 02211 02212 pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U); 02213 02214 pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U); 02215 02216 pCSD->Reserved2 = 0U; /*!< Reserved */ 02217 02218 if (hsd->SdCard.CardType == CARD_SDSC) 02219 { 02220 pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U)); 02221 02222 pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U); 02223 02224 pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U); 02225 02226 pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U); 02227 02228 pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U); 02229 02230 pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U); 02231 02232 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ; 02233 hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U)); 02234 hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU)); 02235 02236 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U); 02237 hsd->SdCard.LogBlockSize = 512U; 02238 } 02239 else if (hsd->SdCard.CardType == CARD_SDHC_SDXC) 02240 { 02241 /* Byte 7 */ 02242 pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U)); 02243 02244 hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U); 02245 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr; 02246 hsd->SdCard.BlockSize = 512U; 02247 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize; 02248 } 02249 else 02250 { 02251 /* Clear all the static flags */ 02252 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02253 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02254 hsd->State = HAL_SD_STATE_READY; 02255 return HAL_ERROR; 02256 } 02257 02258 pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U); 02259 02260 pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U); 02261 02262 pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU); 02263 02264 pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U); 02265 02266 pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U); 02267 02268 pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U); 02269 02270 pCSD->MaxWrBlockLen = (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U); 02271 02272 pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U); 02273 02274 pCSD->Reserved3 = 0; 02275 02276 pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U); 02277 02278 pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U); 02279 02280 pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U); 02281 02282 pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U); 02283 02284 pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U); 02285 02286 pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U); 02287 02288 pCSD->ECC = (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U); 02289 02290 pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U); 02291 02292 pCSD->Reserved4 = 1; 02293 02294 return HAL_OK; 02295 } 02296 02297 /** 02298 * @brief Gets the SD status info.( shall be called if there is no SD transaction ongoing ) 02299 * @param hsd: Pointer to SD handle 02300 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that 02301 * will contain the SD card status information 02302 * @retval HAL status 02303 */ 02304 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus) 02305 { 02306 uint32_t sd_status[16]; 02307 uint32_t errorstate; 02308 HAL_StatusTypeDef status = HAL_OK; 02309 02310 if (hsd->State == HAL_SD_STATE_BUSY) 02311 { 02312 return HAL_ERROR; 02313 } 02314 02315 errorstate = SD_SendSDStatus(hsd, sd_status); 02316 if (errorstate != HAL_SD_ERROR_NONE) 02317 { 02318 /* Clear all the static flags */ 02319 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02320 hsd->ErrorCode |= errorstate; 02321 hsd->State = HAL_SD_STATE_READY; 02322 status = HAL_ERROR; 02323 } 02324 else 02325 { 02326 pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U); 02327 02328 pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U); 02329 02330 pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U)); 02331 02332 pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) | 02333 ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U)); 02334 02335 pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU); 02336 02337 pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U); 02338 02339 pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U); 02340 02341 pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU)); 02342 02343 pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U); 02344 02345 pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U); 02346 02347 pStatus->UhsSpeedGrade = (uint8_t)((sd_status[3] & 0x00F0U) >> 4U); 02348 pStatus->UhsAllocationUnitSize = (uint8_t)(sd_status[3] & 0x000FU) ; 02349 pStatus->VideoSpeedClass = (uint8_t)((sd_status[4] & 0xFF000000U) >> 24U); 02350 } 02351 02352 /* Set Block Size for Card */ 02353 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 02354 if (errorstate != HAL_SD_ERROR_NONE) 02355 { 02356 /* Clear all the static flags */ 02357 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02358 hsd->ErrorCode = errorstate; 02359 hsd->State = HAL_SD_STATE_READY; 02360 status = HAL_ERROR; 02361 } 02362 02363 02364 return status; 02365 } 02366 02367 /** 02368 * @brief Gets the SD card info. 02369 * @param hsd: Pointer to SD handle 02370 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that 02371 * will contain the SD card status information 02372 * @retval HAL status 02373 */ 02374 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo) 02375 { 02376 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType); 02377 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion); 02378 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class); 02379 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd); 02380 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr); 02381 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize); 02382 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr); 02383 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize); 02384 02385 return HAL_OK; 02386 } 02387 02388 /** 02389 * @brief Enables wide bus operation for the requested card if supported by 02390 * card. 02391 * @param hsd: Pointer to SD handle 02392 * @param WideMode: Specifies the SD card wide bus mode 02393 * This parameter can be one of the following values: 02394 * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer 02395 * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer 02396 * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer 02397 * @retval HAL status 02398 */ 02399 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode) 02400 { 02401 SDMMC_InitTypeDef Init; 02402 uint32_t errorstate; 02403 uint32_t sdmmc_clk = 0U; 02404 HAL_StatusTypeDef status = HAL_OK; 02405 02406 /* Check the parameters */ 02407 assert_param(IS_SDMMC_BUS_WIDE(WideMode)); 02408 02409 /* Change State */ 02410 hsd->State = HAL_SD_STATE_BUSY; 02411 02412 if (hsd->SdCard.CardType != CARD_SECURED) 02413 { 02414 if (WideMode == SDMMC_BUS_WIDE_8B) 02415 { 02416 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02417 } 02418 else if (WideMode == SDMMC_BUS_WIDE_4B) 02419 { 02420 errorstate = SD_WideBus_Enable(hsd); 02421 02422 hsd->ErrorCode |= errorstate; 02423 } 02424 else if (WideMode == SDMMC_BUS_WIDE_1B) 02425 { 02426 errorstate = SD_WideBus_Disable(hsd); 02427 02428 hsd->ErrorCode |= errorstate; 02429 } 02430 else 02431 { 02432 /* WideMode is not a valid argument*/ 02433 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 02434 } 02435 } 02436 else 02437 { 02438 /* MMC Card does not support this feature */ 02439 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02440 } 02441 02442 if (hsd->ErrorCode != HAL_SD_ERROR_NONE) 02443 { 02444 /* Clear all the static flags */ 02445 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02446 status = HAL_ERROR; 02447 } 02448 else 02449 { 02450 sdmmc_clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SDMMC); 02451 if (sdmmc_clk != 0U) 02452 { 02453 /* Configure the SDMMC peripheral */ 02454 Init.ClockEdge = hsd->Init.ClockEdge; 02455 Init.ClockPowerSave = hsd->Init.ClockPowerSave; 02456 Init.BusWide = WideMode; 02457 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl; 02458 02459 /* Check if user Clock div < Normal speed 25Mhz, no change in Clockdiv */ 02460 if (hsd->Init.ClockDiv >= (sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ))) 02461 { 02462 Init.ClockDiv = hsd->Init.ClockDiv; 02463 } 02464 else if (hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) 02465 { 02466 /* UltraHigh speed SD card,user Clock div */ 02467 Init.ClockDiv = hsd->Init.ClockDiv; 02468 } 02469 else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) 02470 { 02471 /* High speed SD card, Max Frequency = 50Mhz */ 02472 if (hsd->Init.ClockDiv == 0U) 02473 { 02474 if (sdmmc_clk > SD_HIGH_SPEED_FREQ) 02475 { 02476 Init.ClockDiv = sdmmc_clk / (2U * SD_HIGH_SPEED_FREQ); 02477 } 02478 else 02479 { 02480 Init.ClockDiv = hsd->Init.ClockDiv; 02481 } 02482 } 02483 else 02484 { 02485 if ((sdmmc_clk / (2U * hsd->Init.ClockDiv)) > SD_HIGH_SPEED_FREQ) 02486 { 02487 Init.ClockDiv = sdmmc_clk / (2U * SD_HIGH_SPEED_FREQ); 02488 } 02489 else 02490 { 02491 Init.ClockDiv = hsd->Init.ClockDiv; 02492 } 02493 } 02494 } 02495 else 02496 { 02497 /* No High speed SD card, Max Frequency = 25Mhz */ 02498 if (hsd->Init.ClockDiv == 0U) 02499 { 02500 if (sdmmc_clk > SD_NORMAL_SPEED_FREQ) 02501 { 02502 Init.ClockDiv = sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ); 02503 } 02504 else 02505 { 02506 Init.ClockDiv = hsd->Init.ClockDiv; 02507 } 02508 } 02509 else 02510 { 02511 if ((sdmmc_clk / (2U * hsd->Init.ClockDiv)) > SD_NORMAL_SPEED_FREQ) 02512 { 02513 Init.ClockDiv = sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ); 02514 } 02515 else 02516 { 02517 Init.ClockDiv = hsd->Init.ClockDiv; 02518 } 02519 } 02520 } 02521 02522 #if (USE_SD_TRANSCEIVER != 0U) 02523 Init.TranceiverPresent = hsd->Init.TranceiverPresent; 02524 #endif /* USE_SD_TRANSCEIVER */ 02525 02526 (void)SDMMC_Init(hsd->Instance, Init); 02527 } 02528 else 02529 { 02530 hsd->ErrorCode |= SDMMC_ERROR_INVALID_PARAMETER; 02531 status = HAL_ERROR; 02532 } 02533 } 02534 02535 /* Set Block Size for Card */ 02536 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 02537 if (errorstate != HAL_SD_ERROR_NONE) 02538 { 02539 /* Clear all the static flags */ 02540 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02541 hsd->ErrorCode |= errorstate; 02542 status = HAL_ERROR; 02543 } 02544 02545 /* Change State */ 02546 hsd->State = HAL_SD_STATE_READY; 02547 02548 return status; 02549 } 02550 02551 /** 02552 * @brief Configure the speed bus mode 02553 * @param hsd: Pointer to the SD handle 02554 * @param SpeedMode: Specifies the SD card speed bus mode 02555 * This parameter can be one of the following values: 02556 * @arg SDMMC_SPEED_MODE_AUTO: Max speed mode supported by the card 02557 * @arg SDMMC_SPEED_MODE_DEFAULT: Default Speed/SDR12 mode 02558 * @arg SDMMC_SPEED_MODE_HIGH: High Speed/SDR25 mode 02559 * @arg SDMMC_SPEED_MODE_ULTRA: Ultra high speed mode 02560 * @retval HAL status 02561 */ 02562 02563 HAL_StatusTypeDef HAL_SD_ConfigSpeedBusOperation(SD_HandleTypeDef *hsd, uint32_t SpeedMode) 02564 { 02565 uint32_t tickstart; 02566 uint32_t errorstate; 02567 HAL_StatusTypeDef status = HAL_OK; 02568 02569 /* Check the parameters */ 02570 assert_param(IS_SDMMC_SPEED_MODE(SpeedMode)); 02571 /* Change State */ 02572 hsd->State = HAL_SD_STATE_BUSY; 02573 02574 #if (USE_SD_TRANSCEIVER != 0U) 02575 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT) 02576 { 02577 switch (SpeedMode) 02578 { 02579 case SDMMC_SPEED_MODE_AUTO: 02580 { 02581 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) || 02582 (hsd->SdCard.CardType == CARD_SDHC_SDXC)) 02583 { 02584 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED; 02585 /* Enable Ultra High Speed */ 02586 if (SD_UltraHighSpeed(hsd) != HAL_SD_ERROR_NONE) 02587 { 02588 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE) 02589 { 02590 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02591 status = HAL_ERROR; 02592 } 02593 } 02594 } 02595 else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) 02596 { 02597 /* Enable High Speed */ 02598 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE) 02599 { 02600 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02601 status = HAL_ERROR; 02602 } 02603 } 02604 else 02605 { 02606 /*Nothing to do, Use defaultSpeed */ 02607 } 02608 break; 02609 } 02610 case SDMMC_SPEED_MODE_ULTRA: 02611 { 02612 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) || 02613 (hsd->SdCard.CardType == CARD_SDHC_SDXC)) 02614 { 02615 /* Enable UltraHigh Speed */ 02616 if (SD_UltraHighSpeed(hsd) != HAL_SD_ERROR_NONE) 02617 { 02618 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02619 status = HAL_ERROR; 02620 } 02621 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED; 02622 } 02623 else 02624 { 02625 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02626 status = HAL_ERROR; 02627 } 02628 break; 02629 } 02630 case SDMMC_SPEED_MODE_DDR: 02631 { 02632 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) || 02633 (hsd->SdCard.CardType == CARD_SDHC_SDXC)) 02634 { 02635 /* Enable DDR Mode*/ 02636 if (SD_DDR_Mode(hsd) != HAL_SD_ERROR_NONE) 02637 { 02638 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02639 status = HAL_ERROR; 02640 } 02641 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED | SDMMC_CLKCR_DDR; 02642 } 02643 else 02644 { 02645 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02646 status = HAL_ERROR; 02647 } 02648 break; 02649 } 02650 case SDMMC_SPEED_MODE_HIGH: 02651 { 02652 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) || 02653 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) || 02654 (hsd->SdCard.CardType == CARD_SDHC_SDXC)) 02655 { 02656 /* Enable High Speed */ 02657 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE) 02658 { 02659 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02660 status = HAL_ERROR; 02661 } 02662 } 02663 else 02664 { 02665 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02666 status = HAL_ERROR; 02667 } 02668 break; 02669 } 02670 case SDMMC_SPEED_MODE_DEFAULT: 02671 break; 02672 default: 02673 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 02674 status = HAL_ERROR; 02675 break; 02676 } 02677 } 02678 else 02679 { 02680 switch (SpeedMode) 02681 { 02682 case SDMMC_SPEED_MODE_AUTO: 02683 { 02684 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) || 02685 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) || 02686 (hsd->SdCard.CardType == CARD_SDHC_SDXC)) 02687 { 02688 /* Enable High Speed */ 02689 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE) 02690 { 02691 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02692 status = HAL_ERROR; 02693 } 02694 } 02695 else 02696 { 02697 /*Nothing to do, Use defaultSpeed */ 02698 } 02699 break; 02700 } 02701 case SDMMC_SPEED_MODE_HIGH: 02702 { 02703 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) || 02704 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) || 02705 (hsd->SdCard.CardType == CARD_SDHC_SDXC)) 02706 { 02707 /* Enable High Speed */ 02708 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE) 02709 { 02710 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02711 status = HAL_ERROR; 02712 } 02713 } 02714 else 02715 { 02716 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02717 status = HAL_ERROR; 02718 } 02719 break; 02720 } 02721 case SDMMC_SPEED_MODE_DEFAULT: 02722 break; 02723 case SDMMC_SPEED_MODE_ULTRA: /*not valid without transceiver*/ 02724 default: 02725 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 02726 status = HAL_ERROR; 02727 break; 02728 } 02729 } 02730 #else 02731 switch (SpeedMode) 02732 { 02733 case SDMMC_SPEED_MODE_AUTO: 02734 { 02735 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) || 02736 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) || 02737 (hsd->SdCard.CardType == CARD_SDHC_SDXC)) 02738 { 02739 /* Enable High Speed */ 02740 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE) 02741 { 02742 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02743 status = HAL_ERROR; 02744 } 02745 } 02746 else 02747 { 02748 /*Nothing to do, Use defaultSpeed */ 02749 } 02750 break; 02751 } 02752 case SDMMC_SPEED_MODE_HIGH: 02753 { 02754 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) || 02755 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) || 02756 (hsd->SdCard.CardType == CARD_SDHC_SDXC)) 02757 { 02758 /* Enable High Speed */ 02759 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE) 02760 { 02761 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02762 status = HAL_ERROR; 02763 } 02764 } 02765 else 02766 { 02767 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02768 status = HAL_ERROR; 02769 } 02770 break; 02771 } 02772 case SDMMC_SPEED_MODE_DEFAULT: 02773 break; 02774 case SDMMC_SPEED_MODE_ULTRA: /*not valid without transceiver*/ 02775 default: 02776 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 02777 status = HAL_ERROR; 02778 break; 02779 } 02780 #endif /* USE_SD_TRANSCEIVER */ 02781 02782 /* Verify that SD card is ready to use after Speed mode switch*/ 02783 tickstart = HAL_GetTick(); 02784 while ((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER)) 02785 { 02786 if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 02787 { 02788 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT; 02789 hsd->State = HAL_SD_STATE_READY; 02790 return HAL_TIMEOUT; 02791 } 02792 } 02793 02794 /* Set Block Size for Card */ 02795 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 02796 if (errorstate != HAL_SD_ERROR_NONE) 02797 { 02798 /* Clear all the static flags */ 02799 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS); 02800 hsd->ErrorCode |= errorstate; 02801 status = HAL_ERROR; 02802 } 02803 02804 /* Change State */ 02805 hsd->State = HAL_SD_STATE_READY; 02806 return status; 02807 } 02808 02809 /** 02810 * @brief Gets the current sd card data state. 02811 * @param hsd: pointer to SD handle 02812 * @retval Card state 02813 */ 02814 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd) 02815 { 02816 uint32_t cardstate; 02817 uint32_t errorstate; 02818 uint32_t resp1 = 0; 02819 02820 errorstate = SD_SendStatus(hsd, &resp1); 02821 if (errorstate != HAL_SD_ERROR_NONE) 02822 { 02823 hsd->ErrorCode |= errorstate; 02824 } 02825 02826 cardstate = ((resp1 >> 9U) & 0x0FU); 02827 02828 return (HAL_SD_CardStateTypeDef)cardstate; 02829 } 02830 02831 /** 02832 * @brief Abort the current transfer and disable the SD. 02833 * @param hsd: pointer to a SD_HandleTypeDef structure that contains 02834 * the configuration information for SD module. 02835 * @retval HAL status 02836 */ 02837 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd) 02838 { 02839 HAL_SD_CardStateTypeDef CardState; 02840 02841 /* DIsable All interrupts */ 02842 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | \ 02843 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR); 02844 02845 /* Clear All flags */ 02846 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS); 02847 02848 /* If IDMA Context, disable Internal DMA */ 02849 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA; 02850 02851 hsd->State = HAL_SD_STATE_READY; 02852 02853 /* Initialize the SD operation */ 02854 hsd->Context = SD_CONTEXT_NONE; 02855 02856 CardState = HAL_SD_GetCardState(hsd); 02857 if ((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02858 { 02859 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance); 02860 } 02861 if (hsd->ErrorCode != HAL_SD_ERROR_NONE) 02862 { 02863 return HAL_ERROR; 02864 } 02865 return HAL_OK; 02866 } 02867 02868 /** 02869 * @brief Abort the current transfer and disable the SD (IT mode). 02870 * @param hsd: pointer to a SD_HandleTypeDef structure that contains 02871 * the configuration information for SD module. 02872 * @retval HAL status 02873 */ 02874 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd) 02875 { 02876 HAL_SD_CardStateTypeDef CardState; 02877 02878 /* Disable All interrupts */ 02879 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | \ 02880 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR); 02881 02882 /* If IDMA Context, disable Internal DMA */ 02883 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA; 02884 02885 /* Clear All flags */ 02886 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS); 02887 02888 CardState = HAL_SD_GetCardState(hsd); 02889 hsd->State = HAL_SD_STATE_READY; 02890 02891 if ((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02892 { 02893 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance); 02894 } 02895 02896 if (hsd->ErrorCode != HAL_SD_ERROR_NONE) 02897 { 02898 return HAL_ERROR; 02899 } 02900 else 02901 { 02902 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 02903 hsd->AbortCpltCallback(hsd); 02904 #else 02905 HAL_SD_AbortCallback(hsd); 02906 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 02907 } 02908 02909 return HAL_OK; 02910 } 02911 02912 /** 02913 * @} 02914 */ 02915 02916 /** 02917 * @} 02918 */ 02919 02920 /* Private function ----------------------------------------------------------*/ 02921 /** @addtogroup SD_Private_Functions 02922 * @{ 02923 */ 02924 02925 02926 /** 02927 * @brief Initializes the sd card. 02928 * @param hsd: Pointer to SD handle 02929 * @retval SD Card error state 02930 */ 02931 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd) 02932 { 02933 HAL_SD_CardCSDTypeDef CSD; 02934 uint32_t errorstate; 02935 uint16_t sd_rca = 0U; 02936 uint32_t tickstart = HAL_GetTick(); 02937 02938 /* Check the power State */ 02939 if (SDMMC_GetPowerState(hsd->Instance) == 0U) 02940 { 02941 /* Power off */ 02942 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 02943 } 02944 02945 if (hsd->SdCard.CardType != CARD_SECURED) 02946 { 02947 /* Send CMD2 ALL_SEND_CID */ 02948 errorstate = SDMMC_CmdSendCID(hsd->Instance); 02949 if (errorstate != HAL_SD_ERROR_NONE) 02950 { 02951 return errorstate; 02952 } 02953 else 02954 { 02955 /* Get Card identification number data */ 02956 hsd->CID[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1); 02957 hsd->CID[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2); 02958 hsd->CID[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3); 02959 hsd->CID[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4); 02960 } 02961 } 02962 02963 if (hsd->SdCard.CardType != CARD_SECURED) 02964 { 02965 /* Send CMD3 SET_REL_ADDR with argument 0 */ 02966 /* SD Card publishes its RCA. */ 02967 while (sd_rca == 0U) 02968 { 02969 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca); 02970 if (errorstate != HAL_SD_ERROR_NONE) 02971 { 02972 return errorstate; 02973 } 02974 if ((HAL_GetTick() - tickstart) >= SDMMC_CMDTIMEOUT) 02975 { 02976 return HAL_SD_ERROR_TIMEOUT; 02977 } 02978 } 02979 } 02980 if (hsd->SdCard.CardType != CARD_SECURED) 02981 { 02982 /* Get the SD card RCA */ 02983 hsd->SdCard.RelCardAdd = sd_rca; 02984 02985 /* Send CMD9 SEND_CSD with argument as card's RCA */ 02986 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 02987 if (errorstate != HAL_SD_ERROR_NONE) 02988 { 02989 return errorstate; 02990 } 02991 else 02992 { 02993 /* Get Card Specific Data */ 02994 hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1); 02995 hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2); 02996 hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3); 02997 hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4); 02998 } 02999 } 03000 03001 /* Get the Card Class */ 03002 hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20U); 03003 03004 /* Get CSD parameters */ 03005 if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK) 03006 { 03007 return HAL_SD_ERROR_UNSUPPORTED_FEATURE; 03008 } 03009 03010 /* Select the Card */ 03011 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U)); 03012 if (errorstate != HAL_SD_ERROR_NONE) 03013 { 03014 return errorstate; 03015 } 03016 03017 /* All cards are initialized */ 03018 return HAL_SD_ERROR_NONE; 03019 } 03020 03021 /** 03022 * @brief Enquires cards about their operating voltage and configures clock 03023 * controls and stores SD information that will be needed in future 03024 * in the SD handle. 03025 * @param hsd: Pointer to SD handle 03026 * @retval error state 03027 */ 03028 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd) 03029 { 03030 __IO uint32_t count = 0U; 03031 uint32_t response = 0U; 03032 uint32_t validvoltage = 0U; 03033 uint32_t errorstate; 03034 #if (USE_SD_TRANSCEIVER != 0U) 03035 uint32_t tickstart = HAL_GetTick(); 03036 #endif /* USE_SD_TRANSCEIVER */ 03037 03038 /* CMD0: GO_IDLE_STATE */ 03039 errorstate = SDMMC_CmdGoIdleState(hsd->Instance); 03040 if (errorstate != HAL_SD_ERROR_NONE) 03041 { 03042 return errorstate; 03043 } 03044 03045 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */ 03046 errorstate = SDMMC_CmdOperCond(hsd->Instance); 03047 if (errorstate == SDMMC_ERROR_TIMEOUT) /* No response to CMD8 */ 03048 { 03049 hsd->SdCard.CardVersion = CARD_V1_X; 03050 /* CMD0: GO_IDLE_STATE */ 03051 errorstate = SDMMC_CmdGoIdleState(hsd->Instance); 03052 if (errorstate != HAL_SD_ERROR_NONE) 03053 { 03054 return errorstate; 03055 } 03056 03057 } 03058 else 03059 { 03060 hsd->SdCard.CardVersion = CARD_V2_X; 03061 } 03062 03063 if (hsd->SdCard.CardVersion == CARD_V2_X) 03064 { 03065 /* SEND CMD55 APP_CMD with RCA as 0 */ 03066 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0); 03067 if (errorstate != HAL_SD_ERROR_NONE) 03068 { 03069 return HAL_SD_ERROR_UNSUPPORTED_FEATURE; 03070 } 03071 } 03072 /* SD CARD */ 03073 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */ 03074 while ((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U)) 03075 { 03076 /* SEND CMD55 APP_CMD with RCA as 0 */ 03077 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0); 03078 if (errorstate != HAL_SD_ERROR_NONE) 03079 { 03080 return errorstate; 03081 } 03082 03083 /* Send CMD41 */ 03084 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | 03085 SD_SWITCH_1_8V_CAPACITY); 03086 if (errorstate != HAL_SD_ERROR_NONE) 03087 { 03088 return HAL_SD_ERROR_UNSUPPORTED_FEATURE; 03089 } 03090 03091 /* Get command response */ 03092 response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1); 03093 03094 /* Get operating voltage*/ 03095 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U); 03096 03097 count++; 03098 } 03099 03100 if (count >= SDMMC_MAX_VOLT_TRIAL) 03101 { 03102 return HAL_SD_ERROR_INVALID_VOLTRANGE; 03103 } 03104 03105 /* Set default card type */ 03106 hsd->SdCard.CardType = CARD_SDSC; 03107 03108 if ((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) 03109 { 03110 hsd->SdCard.CardType = CARD_SDHC_SDXC; 03111 #if (USE_SD_TRANSCEIVER != 0U) 03112 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT) 03113 { 03114 if ((response & SD_SWITCH_1_8V_CAPACITY) == SD_SWITCH_1_8V_CAPACITY) 03115 { 03116 hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED; 03117 03118 /* Start switching procedue */ 03119 hsd->Instance->POWER |= SDMMC_POWER_VSWITCHEN; 03120 03121 /* Send CMD11 to switch 1.8V mode */ 03122 errorstate = SDMMC_CmdVoltageSwitch(hsd->Instance); 03123 if (errorstate != HAL_SD_ERROR_NONE) 03124 { 03125 return errorstate; 03126 } 03127 03128 /* Check to CKSTOP */ 03129 while ((hsd->Instance->STA & SDMMC_FLAG_CKSTOP) != SDMMC_FLAG_CKSTOP) 03130 { 03131 if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 03132 { 03133 return HAL_SD_ERROR_TIMEOUT; 03134 } 03135 } 03136 03137 /* Clear CKSTOP Flag */ 03138 hsd->Instance->ICR = SDMMC_FLAG_CKSTOP; 03139 03140 /* Check to BusyD0 */ 03141 if ((hsd->Instance->STA & SDMMC_FLAG_BUSYD0) != SDMMC_FLAG_BUSYD0) 03142 { 03143 /* Error when activate Voltage Switch in SDMMC Peripheral */ 03144 return SDMMC_ERROR_UNSUPPORTED_FEATURE; 03145 } 03146 else 03147 { 03148 /* Enable Transceiver Switch PIN */ 03149 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 03150 hsd->DriveTransceiver_1_8V_Callback(SET); 03151 #else 03152 HAL_SD_DriveTransceiver_1_8V_Callback(SET); 03153 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 03154 03155 /* Switch ready */ 03156 hsd->Instance->POWER |= SDMMC_POWER_VSWITCH; 03157 03158 /* Check VSWEND Flag */ 03159 while ((hsd->Instance->STA & SDMMC_FLAG_VSWEND) != SDMMC_FLAG_VSWEND) 03160 { 03161 if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 03162 { 03163 return HAL_SD_ERROR_TIMEOUT; 03164 } 03165 } 03166 03167 /* Clear VSWEND Flag */ 03168 hsd->Instance->ICR = SDMMC_FLAG_VSWEND; 03169 03170 /* Check BusyD0 status */ 03171 if ((hsd->Instance->STA & SDMMC_FLAG_BUSYD0) == SDMMC_FLAG_BUSYD0) 03172 { 03173 /* Error when enabling 1.8V mode */ 03174 return HAL_SD_ERROR_INVALID_VOLTRANGE; 03175 } 03176 /* Switch to 1.8V OK */ 03177 03178 /* Disable VSWITCH FLAG from SDMMC Peripheral */ 03179 hsd->Instance->POWER = 0x13U; 03180 03181 /* Clean Status flags */ 03182 hsd->Instance->ICR = 0xFFFFFFFFU; 03183 } 03184 } 03185 } 03186 #endif /* USE_SD_TRANSCEIVER */ 03187 } 03188 03189 return HAL_SD_ERROR_NONE; 03190 } 03191 03192 /** 03193 * @brief Turns the SDMMC output signals off. 03194 * @param hsd: Pointer to SD handle 03195 * @retval None 03196 */ 03197 static void SD_PowerOFF(SD_HandleTypeDef *hsd) 03198 { 03199 /* Set Power State to OFF */ 03200 (void)SDMMC_PowerState_OFF(hsd->Instance); 03201 } 03202 03203 /** 03204 * @brief Send Status info command. 03205 * @param hsd: pointer to SD handle 03206 * @param pSDstatus: Pointer to the buffer that will contain the SD card status 03207 * SD Status register) 03208 * @retval error state 03209 */ 03210 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus) 03211 { 03212 SDMMC_DataInitTypeDef config; 03213 uint32_t errorstate; 03214 uint32_t tickstart = HAL_GetTick(); 03215 uint32_t count; 03216 uint32_t *pData = pSDstatus; 03217 03218 /* Check SD response */ 03219 if ((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 03220 { 03221 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 03222 } 03223 03224 /* Set block size for card if it is not equal to current block size for card */ 03225 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U); 03226 if (errorstate != HAL_SD_ERROR_NONE) 03227 { 03228 hsd->ErrorCode |= HAL_SD_ERROR_NONE; 03229 return errorstate; 03230 } 03231 03232 /* Send CMD55 */ 03233 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 03234 if (errorstate != HAL_SD_ERROR_NONE) 03235 { 03236 hsd->ErrorCode |= HAL_SD_ERROR_NONE; 03237 return errorstate; 03238 } 03239 03240 /* Configure the SD DPSM (Data Path State Machine) */ 03241 config.DataTimeOut = SDMMC_DATATIMEOUT; 03242 config.DataLength = 64U; 03243 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B; 03244 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 03245 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 03246 config.DPSM = SDMMC_DPSM_ENABLE; 03247 (void)SDMMC_ConfigData(hsd->Instance, &config); 03248 03249 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */ 03250 errorstate = SDMMC_CmdStatusRegister(hsd->Instance); 03251 if (errorstate != HAL_SD_ERROR_NONE) 03252 { 03253 hsd->ErrorCode |= HAL_SD_ERROR_NONE; 03254 return errorstate; 03255 } 03256 03257 /* Get status data */ 03258 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND)) 03259 { 03260 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF)) 03261 { 03262 for (count = 0U; count < 8U; count++) 03263 { 03264 *pData = SDMMC_ReadFIFO(hsd->Instance); 03265 pData++; 03266 } 03267 } 03268 03269 if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 03270 { 03271 return HAL_SD_ERROR_TIMEOUT; 03272 } 03273 } 03274 03275 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 03276 { 03277 return HAL_SD_ERROR_DATA_TIMEOUT; 03278 } 03279 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 03280 { 03281 return HAL_SD_ERROR_DATA_CRC_FAIL; 03282 } 03283 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR)) 03284 { 03285 return HAL_SD_ERROR_RX_OVERRUN; 03286 } 03287 else 03288 { 03289 /* Nothing to do */ 03290 } 03291 03292 while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DPSMACT))) 03293 { 03294 *pData = SDMMC_ReadFIFO(hsd->Instance); 03295 pData++; 03296 03297 if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 03298 { 03299 return HAL_SD_ERROR_TIMEOUT; 03300 } 03301 } 03302 03303 /* Clear all the static status flags*/ 03304 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS); 03305 03306 return HAL_SD_ERROR_NONE; 03307 } 03308 03309 /** 03310 * @brief Returns the current card's status. 03311 * @param hsd: Pointer to SD handle 03312 * @param pCardStatus: pointer to the buffer that will contain the SD card 03313 * status (Card Status register) 03314 * @retval error state 03315 */ 03316 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus) 03317 { 03318 uint32_t errorstate; 03319 03320 if (pCardStatus == NULL) 03321 { 03322 return HAL_SD_ERROR_PARAM; 03323 } 03324 03325 /* Send Status command */ 03326 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 03327 if (errorstate != HAL_SD_ERROR_NONE) 03328 { 03329 return errorstate; 03330 } 03331 03332 /* Get SD card status */ 03333 *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1); 03334 03335 return HAL_SD_ERROR_NONE; 03336 } 03337 03338 /** 03339 * @brief Enables the SDMMC wide bus mode. 03340 * @param hsd: pointer to SD handle 03341 * @retval error state 03342 */ 03343 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd) 03344 { 03345 uint32_t scr[2U] = {0UL, 0UL}; 03346 uint32_t errorstate; 03347 03348 if ((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 03349 { 03350 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 03351 } 03352 03353 /* Get SCR Register */ 03354 errorstate = SD_FindSCR(hsd, scr); 03355 if (errorstate != HAL_SD_ERROR_NONE) 03356 { 03357 return errorstate; 03358 } 03359 03360 /* If requested card supports wide bus operation */ 03361 if ((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO) 03362 { 03363 /* Send CMD55 APP_CMD with argument as card's RCA.*/ 03364 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 03365 if (errorstate != HAL_SD_ERROR_NONE) 03366 { 03367 return errorstate; 03368 } 03369 03370 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */ 03371 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U); 03372 if (errorstate != HAL_SD_ERROR_NONE) 03373 { 03374 return errorstate; 03375 } 03376 03377 return HAL_SD_ERROR_NONE; 03378 } 03379 else 03380 { 03381 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 03382 } 03383 } 03384 03385 /** 03386 * @brief Disables the SDMMC wide bus mode. 03387 * @param hsd: Pointer to SD handle 03388 * @retval error state 03389 */ 03390 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd) 03391 { 03392 uint32_t scr[2U] = {0UL, 0UL}; 03393 uint32_t errorstate; 03394 03395 if ((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 03396 { 03397 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 03398 } 03399 03400 /* Get SCR Register */ 03401 errorstate = SD_FindSCR(hsd, scr); 03402 if (errorstate != HAL_SD_ERROR_NONE) 03403 { 03404 return errorstate; 03405 } 03406 03407 /* If requested card supports 1 bit mode operation */ 03408 if ((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO) 03409 { 03410 /* Send CMD55 APP_CMD with argument as card's RCA */ 03411 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 03412 if (errorstate != HAL_SD_ERROR_NONE) 03413 { 03414 return errorstate; 03415 } 03416 03417 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */ 03418 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U); 03419 if (errorstate != HAL_SD_ERROR_NONE) 03420 { 03421 return errorstate; 03422 } 03423 03424 return HAL_SD_ERROR_NONE; 03425 } 03426 else 03427 { 03428 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 03429 } 03430 } 03431 03432 03433 /** 03434 * @brief Finds the SD card SCR register value. 03435 * @param hsd: Pointer to SD handle 03436 * @param pSCR: pointer to the buffer that will contain the SCR value 03437 * @retval error state 03438 */ 03439 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR) 03440 { 03441 SDMMC_DataInitTypeDef config; 03442 uint32_t errorstate; 03443 uint32_t tickstart = HAL_GetTick(); 03444 uint32_t index = 0U; 03445 uint32_t tempscr[2U] = {0UL, 0UL}; 03446 uint32_t *scr = pSCR; 03447 03448 /* Set Block Size To 8 Bytes */ 03449 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U); 03450 if (errorstate != HAL_SD_ERROR_NONE) 03451 { 03452 return errorstate; 03453 } 03454 03455 /* Send CMD55 APP_CMD with argument as card's RCA */ 03456 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U)); 03457 if (errorstate != HAL_SD_ERROR_NONE) 03458 { 03459 return errorstate; 03460 } 03461 03462 config.DataTimeOut = SDMMC_DATATIMEOUT; 03463 config.DataLength = 8U; 03464 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B; 03465 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 03466 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 03467 config.DPSM = SDMMC_DPSM_ENABLE; 03468 (void)SDMMC_ConfigData(hsd->Instance, &config); 03469 03470 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */ 03471 errorstate = SDMMC_CmdSendSCR(hsd->Instance); 03472 if (errorstate != HAL_SD_ERROR_NONE) 03473 { 03474 return errorstate; 03475 } 03476 03477 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND | 03478 SDMMC_FLAG_DATAEND)) 03479 { 03480 if ((!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOE)) && (index == 0U)) 03481 { 03482 tempscr[0] = SDMMC_ReadFIFO(hsd->Instance); 03483 tempscr[1] = SDMMC_ReadFIFO(hsd->Instance); 03484 index++; 03485 } 03486 03487 03488 if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 03489 { 03490 return HAL_SD_ERROR_TIMEOUT; 03491 } 03492 } 03493 03494 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 03495 { 03496 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT); 03497 03498 return HAL_SD_ERROR_DATA_TIMEOUT; 03499 } 03500 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 03501 { 03502 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL); 03503 03504 return HAL_SD_ERROR_DATA_CRC_FAIL; 03505 } 03506 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR)) 03507 { 03508 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR); 03509 03510 return HAL_SD_ERROR_RX_OVERRUN; 03511 } 03512 else 03513 { 03514 /* No error flag set */ 03515 /* Clear all the static flags */ 03516 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS); 03517 03518 *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) | \ 03519 ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24)); 03520 scr++; 03521 *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) | \ 03522 ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24)); 03523 03524 } 03525 03526 return HAL_SD_ERROR_NONE; 03527 } 03528 03529 /** 03530 * @brief Wrap up reading in non-blocking mode. 03531 * @param hsd: pointer to a SD_HandleTypeDef structure that contains 03532 * the configuration information. 03533 * @retval None 03534 */ 03535 static void SD_Read_IT(SD_HandleTypeDef *hsd) 03536 { 03537 uint32_t count; 03538 uint32_t data; 03539 uint8_t *tmp; 03540 03541 tmp = hsd->pRxBuffPtr; 03542 03543 if (hsd->RxXferSize >= 32U) 03544 { 03545 /* Read data from SDMMC Rx FIFO */ 03546 for (count = 0U; count < 8U; count++) 03547 { 03548 data = SDMMC_ReadFIFO(hsd->Instance); 03549 *tmp = (uint8_t)(data & 0xFFU); 03550 tmp++; 03551 *tmp = (uint8_t)((data >> 8U) & 0xFFU); 03552 tmp++; 03553 *tmp = (uint8_t)((data >> 16U) & 0xFFU); 03554 tmp++; 03555 *tmp = (uint8_t)((data >> 24U) & 0xFFU); 03556 tmp++; 03557 } 03558 03559 hsd->pRxBuffPtr = tmp; 03560 hsd->RxXferSize -= 32U; 03561 } 03562 } 03563 03564 /** 03565 * @brief Wrap up writing in non-blocking mode. 03566 * @param hsd: pointer to a SD_HandleTypeDef structure that contains 03567 * the configuration information. 03568 * @retval None 03569 */ 03570 static void SD_Write_IT(SD_HandleTypeDef *hsd) 03571 { 03572 uint32_t count; 03573 uint32_t data; 03574 uint8_t *tmp; 03575 03576 tmp = hsd->pTxBuffPtr; 03577 03578 if (hsd->TxXferSize >= 32U) 03579 { 03580 /* Write data to SDMMC Tx FIFO */ 03581 for (count = 0U; count < 8U; count++) 03582 { 03583 data = (uint32_t)(*tmp); 03584 tmp++; 03585 data |= ((uint32_t)(*tmp) << 8U); 03586 tmp++; 03587 data |= ((uint32_t)(*tmp) << 16U); 03588 tmp++; 03589 data |= ((uint32_t)(*tmp) << 24U); 03590 tmp++; 03591 (void)SDMMC_WriteFIFO(hsd->Instance, &data); 03592 } 03593 03594 hsd->pTxBuffPtr = tmp; 03595 hsd->TxXferSize -= 32U; 03596 } 03597 } 03598 03599 /** 03600 * @brief Switches the SD card to High Speed mode. 03601 * This API must be used after "Transfer State" 03602 * @note This operation should be followed by the configuration 03603 * of PLL to have SDMMCCK clock between 50 and 120 MHz 03604 * @param hsd: SD handle 03605 * @retval SD Card error state 03606 */ 03607 uint32_t SD_HighSpeed(SD_HandleTypeDef *hsd) 03608 { 03609 uint32_t errorstate = HAL_SD_ERROR_NONE; 03610 SDMMC_DataInitTypeDef sdmmc_datainitstructure; 03611 uint32_t SD_hs[16] = {0}; 03612 uint32_t count; 03613 uint32_t loop = 0 ; 03614 uint32_t Timeout = HAL_GetTick(); 03615 03616 if (hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED) 03617 { 03618 /* Standard Speed Card <= 12.5Mhz */ 03619 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 03620 } 03621 03622 if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) 03623 { 03624 /* Initialize the Data control register */ 03625 hsd->Instance->DCTRL = 0; 03626 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U); 03627 03628 if (errorstate != HAL_SD_ERROR_NONE) 03629 { 03630 return errorstate; 03631 } 03632 03633 /* Configure the SD DPSM (Data Path State Machine) */ 03634 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT; 03635 sdmmc_datainitstructure.DataLength = 64U; 03636 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ; 03637 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 03638 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 03639 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE; 03640 03641 (void)SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure); 03642 03643 03644 errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_SDR25_SWITCH_PATTERN); 03645 if (errorstate != HAL_SD_ERROR_NONE) 03646 { 03647 return errorstate; 03648 } 03649 03650 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND | 03651 SDMMC_FLAG_DATAEND)) 03652 { 03653 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF)) 03654 { 03655 for (count = 0U; count < 8U; count++) 03656 { 03657 SD_hs[(8U * loop) + count] = SDMMC_ReadFIFO(hsd->Instance); 03658 } 03659 loop ++; 03660 } 03661 03662 if ((HAL_GetTick() - Timeout) >= SDMMC_DATATIMEOUT) 03663 { 03664 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT; 03665 hsd->State = HAL_SD_STATE_READY; 03666 return HAL_SD_ERROR_TIMEOUT; 03667 } 03668 } 03669 03670 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 03671 { 03672 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT); 03673 03674 return errorstate; 03675 } 03676 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 03677 { 03678 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL); 03679 03680 errorstate = SDMMC_ERROR_DATA_CRC_FAIL; 03681 03682 return errorstate; 03683 } 03684 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR)) 03685 { 03686 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR); 03687 03688 errorstate = SDMMC_ERROR_RX_OVERRUN; 03689 03690 return errorstate; 03691 } 03692 else 03693 { 03694 /* No error flag set */ 03695 } 03696 03697 /* Clear all the static flags */ 03698 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS); 03699 03700 /* Test if the switch mode HS is ok */ 03701 if ((((uint8_t *)SD_hs)[13] & 2U) != 2U) 03702 { 03703 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE; 03704 } 03705 03706 } 03707 03708 return errorstate; 03709 } 03710 03711 #if (USE_SD_TRANSCEIVER != 0U) 03712 /** 03713 * @brief Switches the SD card to Ultra High Speed mode. 03714 * This API must be used after "Transfer State" 03715 * @note This operation should be followed by the configuration 03716 * of PLL to have SDMMCCK clock between 50 and 120 MHz 03717 * @param hsd: SD handle 03718 * @retval SD Card error state 03719 */ 03720 static uint32_t SD_UltraHighSpeed(SD_HandleTypeDef *hsd) 03721 { 03722 uint32_t errorstate = HAL_SD_ERROR_NONE; 03723 SDMMC_DataInitTypeDef sdmmc_datainitstructure; 03724 uint32_t SD_hs[16] = {0}; 03725 uint32_t count; 03726 uint32_t loop = 0 ; 03727 uint32_t Timeout = HAL_GetTick(); 03728 03729 if (hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED) 03730 { 03731 /* Standard Speed Card <= 12.5Mhz */ 03732 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 03733 } 03734 03735 if (hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) 03736 { 03737 /* Initialize the Data control register */ 03738 hsd->Instance->DCTRL = 0; 03739 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U); 03740 03741 if (errorstate != HAL_SD_ERROR_NONE) 03742 { 03743 return errorstate; 03744 } 03745 03746 /* Configure the SD DPSM (Data Path State Machine) */ 03747 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT; 03748 sdmmc_datainitstructure.DataLength = 64U; 03749 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ; 03750 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 03751 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 03752 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE; 03753 03754 if (SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK) 03755 { 03756 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR); 03757 } 03758 03759 errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_SDR104_SWITCH_PATTERN); 03760 if (errorstate != HAL_SD_ERROR_NONE) 03761 { 03762 return errorstate; 03763 } 03764 03765 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND | 03766 SDMMC_FLAG_DATAEND)) 03767 { 03768 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF)) 03769 { 03770 for (count = 0U; count < 8U; count++) 03771 { 03772 SD_hs[(8U * loop) + count] = SDMMC_ReadFIFO(hsd->Instance); 03773 } 03774 loop ++; 03775 } 03776 03777 if ((HAL_GetTick() - Timeout) >= SDMMC_DATATIMEOUT) 03778 { 03779 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT; 03780 hsd->State = HAL_SD_STATE_READY; 03781 return HAL_SD_ERROR_TIMEOUT; 03782 } 03783 } 03784 03785 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 03786 { 03787 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT); 03788 03789 return errorstate; 03790 } 03791 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 03792 { 03793 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL); 03794 03795 errorstate = SDMMC_ERROR_DATA_CRC_FAIL; 03796 03797 return errorstate; 03798 } 03799 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR)) 03800 { 03801 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR); 03802 03803 errorstate = SDMMC_ERROR_RX_OVERRUN; 03804 03805 return errorstate; 03806 } 03807 else 03808 { 03809 /* No error flag set */ 03810 } 03811 03812 /* Clear all the static flags */ 03813 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS); 03814 03815 /* Test if the switch mode HS is ok */ 03816 if ((((uint8_t *)SD_hs)[13] & 2U) != 2U) 03817 { 03818 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE; 03819 } 03820 else 03821 { 03822 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 03823 hsd->DriveTransceiver_1_8V_Callback(SET); 03824 #else 03825 HAL_SD_DriveTransceiver_1_8V_Callback(SET); 03826 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 03827 #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2) 03828 /* Enable DelayBlock Peripheral */ 03829 /* SDMMC_FB_CLK tuned feedback clock selected as receive clock, for SDR104 */ 03830 MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX, SDMMC_CLKCR_SELCLKRX_1); 03831 if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK) 03832 { 03833 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR); 03834 } 03835 #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */ 03836 } 03837 } 03838 03839 return errorstate; 03840 } 03841 03842 /** 03843 * @brief Switches the SD card to Double Data Rate (DDR) mode. 03844 * This API must be used after "Transfer State" 03845 * @note This operation should be followed by the configuration 03846 * of PLL to have SDMMCCK clock less than 50MHz 03847 * @param hsd: SD handle 03848 * @retval SD Card error state 03849 */ 03850 static uint32_t SD_DDR_Mode(SD_HandleTypeDef *hsd) 03851 { 03852 uint32_t errorstate = HAL_SD_ERROR_NONE; 03853 SDMMC_DataInitTypeDef sdmmc_datainitstructure; 03854 uint32_t SD_hs[16] = {0}; 03855 uint32_t count; 03856 uint32_t loop = 0 ; 03857 uint32_t Timeout = HAL_GetTick(); 03858 03859 if (hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED) 03860 { 03861 /* Standard Speed Card <= 12.5Mhz */ 03862 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 03863 } 03864 03865 if (hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) 03866 { 03867 /* Initialize the Data control register */ 03868 hsd->Instance->DCTRL = 0; 03869 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U); 03870 03871 if (errorstate != HAL_SD_ERROR_NONE) 03872 { 03873 return errorstate; 03874 } 03875 03876 /* Configure the SD DPSM (Data Path State Machine) */ 03877 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT; 03878 sdmmc_datainitstructure.DataLength = 64U; 03879 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ; 03880 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC; 03881 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK; 03882 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE; 03883 03884 if (SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK) 03885 { 03886 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR); 03887 } 03888 03889 errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_DDR50_SWITCH_PATTERN); 03890 if (errorstate != HAL_SD_ERROR_NONE) 03891 { 03892 return errorstate; 03893 } 03894 03895 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND | 03896 SDMMC_FLAG_DATAEND)) 03897 { 03898 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF)) 03899 { 03900 for (count = 0U; count < 8U; count++) 03901 { 03902 SD_hs[(8U * loop) + count] = SDMMC_ReadFIFO(hsd->Instance); 03903 } 03904 loop ++; 03905 } 03906 03907 if ((HAL_GetTick() - Timeout) >= SDMMC_DATATIMEOUT) 03908 { 03909 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT; 03910 hsd->State = HAL_SD_STATE_READY; 03911 return HAL_SD_ERROR_TIMEOUT; 03912 } 03913 } 03914 03915 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT)) 03916 { 03917 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT); 03918 03919 return errorstate; 03920 } 03921 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL)) 03922 { 03923 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL); 03924 03925 errorstate = SDMMC_ERROR_DATA_CRC_FAIL; 03926 03927 return errorstate; 03928 } 03929 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR)) 03930 { 03931 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR); 03932 03933 errorstate = SDMMC_ERROR_RX_OVERRUN; 03934 03935 return errorstate; 03936 } 03937 else 03938 { 03939 /* No error flag set */ 03940 } 03941 03942 /* Clear all the static flags */ 03943 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS); 03944 03945 /* Test if the switch mode is ok */ 03946 if ((((uint8_t *)SD_hs)[13] & 2U) != 2U) 03947 { 03948 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE; 03949 } 03950 else 03951 { 03952 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 03953 hsd->DriveTransceiver_1_8V_Callback(SET); 03954 #else 03955 HAL_SD_DriveTransceiver_1_8V_Callback(SET); 03956 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 03957 #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2) 03958 /* Enable DelayBlock Peripheral */ 03959 /* SDMMC_CKin feedback clock selected as receive clock, for DDR50 */ 03960 MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX, SDMMC_CLKCR_SELCLKRX_0); 03961 if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK) 03962 { 03963 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR); 03964 } 03965 #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */ 03966 } 03967 } 03968 03969 return errorstate; 03970 } 03971 03972 #endif /* USE_SD_TRANSCEIVER */ 03973 03974 /** 03975 * @brief Read DMA Buffer 0 Transfer completed callbacks 03976 * @param hsd: SD handle 03977 * @retval None 03978 */ 03979 __weak void HAL_SDEx_Read_DMADoubleBuf0CpltCallback(SD_HandleTypeDef *hsd) 03980 { 03981 /* Prevent unused argument(s) compilation warning */ 03982 UNUSED(hsd); 03983 03984 /* NOTE : This function should not be modified, when the callback is needed, 03985 the HAL_SDEx_Read_DMADoubleBuf0CpltCallback can be implemented in the user file 03986 */ 03987 } 03988 03989 /** 03990 * @brief Read DMA Buffer 1 Transfer completed callbacks 03991 * @param hsd: SD handle 03992 * @retval None 03993 */ 03994 __weak void HAL_SDEx_Read_DMADoubleBuf1CpltCallback(SD_HandleTypeDef *hsd) 03995 { 03996 /* Prevent unused argument(s) compilation warning */ 03997 UNUSED(hsd); 03998 03999 /* NOTE : This function should not be modified, when the callback is needed, 04000 the HAL_SDEx_Read_DMADoubleBuf1CpltCallback can be implemented in the user file 04001 */ 04002 } 04003 04004 /** 04005 * @brief Write DMA Buffer 0 Transfer completed callbacks 04006 * @param hsd: SD handle 04007 * @retval None 04008 */ 04009 __weak void HAL_SDEx_Write_DMADoubleBuf0CpltCallback(SD_HandleTypeDef *hsd) 04010 { 04011 /* Prevent unused argument(s) compilation warning */ 04012 UNUSED(hsd); 04013 04014 /* NOTE : This function should not be modified, when the callback is needed, 04015 the HAL_SDEx_Write_DMADoubleBuf0CpltCallback can be implemented in the user file 04016 */ 04017 } 04018 04019 /** 04020 * @brief Write DMA Buffer 1 Transfer completed callbacks 04021 * @param hsd: SD handle 04022 * @retval None 04023 */ 04024 __weak void HAL_SDEx_Write_DMADoubleBuf1CpltCallback(SD_HandleTypeDef *hsd) 04025 { 04026 /* Prevent unused argument(s) compilation warning */ 04027 UNUSED(hsd); 04028 04029 /* NOTE : This function should not be modified, when the callback is needed, 04030 the HAL_SDEx_Write_DMADoubleBuf1CpltCallback can be implemented in the user file 04031 */ 04032 } 04033 04034 04035 /** 04036 * @} 04037 */ 04038 04039 #endif /* HAL_SD_MODULE_ENABLED */ 04040 04041 /** 04042 * @} 04043 */ 04044 04045 /** 04046 * @} 04047 */