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