STM32F103xB HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f1xx_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 @verbatim 00014 ============================================================================== 00015 ##### How to use this driver ##### 00016 ============================================================================== 00017 [..] 00018 This driver implements a high level communication layer for read and write from/to 00019 this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by 00020 the user in HAL_SD_MspInit() function (MSP layer). 00021 Basically, the MSP layer configuration should be the same as we provide in the 00022 examples. 00023 You can easily tailor this configuration according to hardware resources. 00024 00025 [..] 00026 This driver is a generic layered driver for SDIO memories which uses the HAL 00027 SDIO driver functions to interface with SD and uSD cards devices. 00028 It is used as follows: 00029 00030 (#)Initialize the SDIO low level resources by implementing the HAL_SD_MspInit() API: 00031 (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE(); 00032 (##) SDIO pins configuration for SD card 00033 (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE(); 00034 (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init() 00035 and according to your pin assignment; 00036 (##) DMA configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA() 00037 and HAL_SD_WriteBlocks_DMA() APIs). 00038 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE(); 00039 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled. 00040 (##) NVIC configuration if you need to use interrupt process when using DMA transfer. 00041 (+++) Configure the SDIO and DMA interrupt priorities using functions 00042 HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority 00043 (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ() 00044 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT() 00045 and __HAL_SD_DISABLE_IT() inside the communication process. 00046 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT() 00047 and __HAL_SD_CLEAR_IT() 00048 (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT() 00049 and HAL_SD_WriteBlocks_IT() APIs). 00050 (+++) Configure the SDIO interrupt priorities using function HAL_NVIC_SetPriority(); 00051 (+++) Enable the NVIC SDIO IRQs using function HAL_NVIC_EnableIRQ() 00052 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT() 00053 and __HAL_SD_DISABLE_IT() inside the communication process. 00054 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT() 00055 and __HAL_SD_CLEAR_IT() 00056 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization 00057 00058 00059 *** SD Card Initialization and configuration *** 00060 ================================================ 00061 [..] 00062 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes 00063 SDIO Peripheral(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer). 00064 This function provide the following operations: 00065 00066 (#) Apply the SD Card initialization process at 400KHz and check the SD Card 00067 type (Standard Capacity or High Capacity). You can change or adapt this 00068 frequency by adjusting the "ClockDiv" field. 00069 The SD Card frequency (SDIO_CK) is computed as follows: 00070 00071 SDIO_CK = SDIOCLK / (ClockDiv + 2) 00072 00073 In initialization mode and according to the SD Card standard, 00074 make sure that the SDIO_CK frequency doesn't exceed 400KHz. 00075 00076 This phase of initialization is done through SDIO_Init() and 00077 SDIO_PowerState_ON() SDIO low level APIs. 00078 00079 (#) Initialize the SD card. The API used is HAL_SD_InitCard(). 00080 This phase allows the card initialization and identification 00081 and check the SD Card type (Standard Capacity or High Capacity) 00082 The initialization flow is compatible with SD standard. 00083 00084 This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case 00085 of plug-off plug-in. 00086 00087 (#) Configure the SD Card Data transfer frequency. You can change or adapt this 00088 frequency by adjusting the "ClockDiv" field. 00089 In transfer mode and according to the SD Card standard, make sure that the 00090 SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch. 00091 To be able to use a frequency higher than 24MHz, you should use the SDIO 00092 peripheral in bypass mode. Refer to the corresponding reference manual 00093 for more details. 00094 00095 (#) Select the corresponding SD Card according to the address read with the step 2. 00096 00097 (#) Configure the SD Card in wide bus mode: 4-bits data. 00098 00099 *** SD Card Read operation *** 00100 ============================== 00101 [..] 00102 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks(). 00103 This function support only 512-bytes block length (the block size should be 00104 chosen as 512 bytes). 00105 You can choose either one block read operation or multiple block read operation 00106 by adjusting the "NumberOfBlocks" parameter. 00107 After this, you have to ensure that the transfer is done correctly. The check is done 00108 through HAL_SD_GetCardState() function for SD card state. 00109 00110 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA(). 00111 This function support only 512-bytes block length (the block size should be 00112 chosen as 512 bytes). 00113 You can choose either one block read operation or multiple block read operation 00114 by adjusting the "NumberOfBlocks" parameter. 00115 After this, you have to ensure that the transfer is done correctly. The check is done 00116 through HAL_SD_GetCardState() function for SD card state. 00117 You could also check the DMA transfer process through the SD Rx interrupt event. 00118 00119 (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT(). 00120 This function support only 512-bytes block length (the block size should be 00121 chosen as 512 bytes). 00122 You can choose either one block read operation or multiple block read operation 00123 by adjusting the "NumberOfBlocks" parameter. 00124 After this, you have to ensure that the transfer is done correctly. The check is done 00125 through HAL_SD_GetCardState() function for SD card state. 00126 You could also check the IT transfer process through the SD Rx interrupt event. 00127 00128 *** SD Card Write operation *** 00129 =============================== 00130 [..] 00131 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks(). 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 00139 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA(). 00140 This function support only 512-bytes block length (the block size should be 00141 chosen as 512 bytes). 00142 You can choose either one block read operation or multiple block read operation 00143 by adjusting the "NumberOfBlocks" parameter. 00144 After this, you have to ensure that the transfer is done correctly. The check is done 00145 through HAL_SD_GetCardState() function for SD card state. 00146 You could also check the DMA transfer process through the SD Tx interrupt event. 00147 00148 (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT(). 00149 This function support only 512-bytes block length (the block size should be 00150 chosen as 512 bytes). 00151 You can choose either one block read operation or multiple block read operation 00152 by adjusting the "NumberOfBlocks" parameter. 00153 After this, you have to ensure that the transfer is done correctly. The check is done 00154 through HAL_SD_GetCardState() function for SD card state. 00155 You could also check the IT transfer process through the SD Tx interrupt event. 00156 00157 *** SD card status *** 00158 ====================== 00159 [..] 00160 (+) The SD Status contains status bits that are related to the SD Memory 00161 Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus(). 00162 00163 *** SD card information *** 00164 =========================== 00165 [..] 00166 (+) To get SD card information, you can use the function HAL_SD_GetCardInfo(). 00167 It returns useful information about the SD card such as block size, card type, 00168 block number ... 00169 00170 *** SD card CSD register *** 00171 ============================ 00172 (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register. 00173 Some of the CSD parameters are useful for card initialization and identification. 00174 00175 *** SD card CID register *** 00176 ============================ 00177 (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register. 00178 Some of the CSD parameters are useful for card initialization and identification. 00179 00180 *** SD HAL driver macros list *** 00181 ================================== 00182 [..] 00183 Below the list of most used macros in SD HAL driver. 00184 00185 (+) __HAL_SD_ENABLE : Enable the SD device 00186 (+) __HAL_SD_DISABLE : Disable the SD device 00187 (+) __HAL_SD_DMA_ENABLE: Enable the SDIO DMA transfer 00188 (+) __HAL_SD_DMA_DISABLE: Disable the SDIO DMA transfer 00189 (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt 00190 (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt 00191 (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not 00192 (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags 00193 00194 (@) You can refer to the SD HAL driver header file for more useful macros 00195 00196 *** Callback registration *** 00197 ============================================= 00198 [..] 00199 The compilation define USE_HAL_SD_REGISTER_CALLBACKS when set to 1 00200 allows the user to configure dynamically the driver callbacks. 00201 00202 Use Functions @ref HAL_SD_RegisterCallback() to register a user callback, 00203 it allows to register following callbacks: 00204 (+) TxCpltCallback : callback when a transmission transfer is completed. 00205 (+) RxCpltCallback : callback when a reception transfer is completed. 00206 (+) ErrorCallback : callback when error occurs. 00207 (+) AbortCpltCallback : callback when abort is completed. 00208 (+) MspInitCallback : SD MspInit. 00209 (+) MspDeInitCallback : SD MspDeInit. 00210 This function takes as parameters the HAL peripheral handle, the Callback ID 00211 and a pointer to the user callback function. 00212 00213 Use function @ref HAL_SD_UnRegisterCallback() to reset a callback to the default 00214 weak (surcharged) function. It allows to reset following callbacks: 00215 (+) TxCpltCallback : callback when a transmission transfer is completed. 00216 (+) RxCpltCallback : callback when a reception transfer is completed. 00217 (+) ErrorCallback : callback when error occurs. 00218 (+) AbortCpltCallback : callback when abort is completed. 00219 (+) MspInitCallback : SD MspInit. 00220 (+) MspDeInitCallback : SD MspDeInit. 00221 This function) takes as parameters the HAL peripheral handle and the Callback ID. 00222 00223 By default, after the @ref HAL_SD_Init and if the state is HAL_SD_STATE_RESET 00224 all callbacks are reset to the corresponding legacy weak (surcharged) functions. 00225 Exception done for MspInit and MspDeInit callbacks that are respectively 00226 reset to the legacy weak (surcharged) functions in the @ref HAL_SD_Init 00227 and @ref HAL_SD_DeInit only when these callbacks are null (not registered beforehand). 00228 If not, MspInit or MspDeInit are not null, the @ref HAL_SD_Init and @ref HAL_SD_DeInit 00229 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) 00230 00231 Callbacks can be registered/unregistered in READY state only. 00232 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered 00233 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used 00234 during the Init/DeInit. 00235 In that case first register the MspInit/MspDeInit user callbacks 00236 using @ref HAL_SD_RegisterCallback before calling @ref HAL_SD_DeInit 00237 or @ref HAL_SD_Init function. 00238 00239 When The compilation define USE_HAL_SD_REGISTER_CALLBACKS is set to 0 or 00240 not defined, the callback registering feature is not available 00241 and weak (surcharged) callbacks are used. 00242 00243 @endverbatim 00244 ****************************************************************************** 00245 * @attention 00246 * 00247 * <h2><center>© Copyright (c) 2018 STMicroelectronics. 00248 * All rights reserved.</center></h2> 00249 * 00250 * This software component is licensed by ST under BSD 3-Clause license, 00251 * the "License"; You may not use this file except in compliance with the 00252 * License. You may obtain a copy of the License at: 00253 * opensource.org/licenses/BSD-3-Clause 00254 * 00255 ****************************************************************************** 00256 */ 00257 00258 /* Includes ------------------------------------------------------------------*/ 00259 #include "stm32f1xx_hal.h" 00260 00261 #if defined(SDIO) 00262 00263 /** @addtogroup STM32F1xx_HAL_Driver 00264 * @{ 00265 */ 00266 00267 /** @addtogroup SD 00268 * @{ 00269 */ 00270 00271 #ifdef HAL_SD_MODULE_ENABLED 00272 00273 /* Private typedef -----------------------------------------------------------*/ 00274 /* Private define ------------------------------------------------------------*/ 00275 /** @addtogroup SD_Private_Defines 00276 * @{ 00277 */ 00278 00279 /** 00280 * @} 00281 */ 00282 00283 /* Private macro -------------------------------------------------------------*/ 00284 /* Private variables ---------------------------------------------------------*/ 00285 /* Private function prototypes -----------------------------------------------*/ 00286 /* Private functions ---------------------------------------------------------*/ 00287 /** @defgroup SD_Private_Functions SD Private Functions 00288 * @{ 00289 */ 00290 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd); 00291 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd); 00292 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus); 00293 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus); 00294 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd); 00295 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd); 00296 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR); 00297 static void SD_PowerOFF(SD_HandleTypeDef *hsd); 00298 static void SD_Write_IT(SD_HandleTypeDef *hsd); 00299 static void SD_Read_IT(SD_HandleTypeDef *hsd); 00300 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma); 00301 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma); 00302 static void SD_DMAError(DMA_HandleTypeDef *hdma); 00303 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma); 00304 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma); 00305 /** 00306 * @} 00307 */ 00308 00309 /* Exported functions --------------------------------------------------------*/ 00310 /** @addtogroup SD_Exported_Functions 00311 * @{ 00312 */ 00313 00314 /** @addtogroup SD_Exported_Functions_Group1 00315 * @brief Initialization and de-initialization functions 00316 * 00317 @verbatim 00318 ============================================================================== 00319 ##### Initialization and de-initialization functions ##### 00320 ============================================================================== 00321 [..] 00322 This section provides functions allowing to initialize/de-initialize the SD 00323 card device to be ready for use. 00324 00325 @endverbatim 00326 * @{ 00327 */ 00328 00329 /** 00330 * @brief Initializes the SD according to the specified parameters in the 00331 SD_HandleTypeDef and create the associated handle. 00332 * @param hsd: Pointer to the SD handle 00333 * @retval HAL status 00334 */ 00335 HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd) 00336 { 00337 /* Check the SD handle allocation */ 00338 if(hsd == NULL) 00339 { 00340 return HAL_ERROR; 00341 } 00342 00343 /* Check the parameters */ 00344 assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance)); 00345 assert_param(IS_SDIO_CLOCK_EDGE(hsd->Init.ClockEdge)); 00346 assert_param(IS_SDIO_CLOCK_BYPASS(hsd->Init.ClockBypass)); 00347 assert_param(IS_SDIO_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave)); 00348 assert_param(IS_SDIO_BUS_WIDE(hsd->Init.BusWide)); 00349 assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl)); 00350 assert_param(IS_SDIO_CLKDIV(hsd->Init.ClockDiv)); 00351 00352 if(hsd->State == HAL_SD_STATE_RESET) 00353 { 00354 /* Allocate lock resource and initialize it */ 00355 hsd->Lock = HAL_UNLOCKED; 00356 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 00357 /* Reset Callback pointers in HAL_SD_STATE_RESET only */ 00358 hsd->TxCpltCallback = HAL_SD_TxCpltCallback; 00359 hsd->RxCpltCallback = HAL_SD_RxCpltCallback; 00360 hsd->ErrorCallback = HAL_SD_ErrorCallback; 00361 hsd->AbortCpltCallback = HAL_SD_AbortCallback; 00362 00363 if(hsd->MspInitCallback == NULL) 00364 { 00365 hsd->MspInitCallback = HAL_SD_MspInit; 00366 } 00367 00368 /* Init the low level hardware */ 00369 hsd->MspInitCallback(hsd); 00370 #else 00371 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */ 00372 HAL_SD_MspInit(hsd); 00373 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 00374 } 00375 00376 hsd->State = HAL_SD_STATE_BUSY; 00377 00378 /* Initialize the Card parameters */ 00379 if (HAL_SD_InitCard(hsd) != HAL_OK) 00380 { 00381 return HAL_ERROR; 00382 } 00383 00384 /* Initialize the error code */ 00385 hsd->ErrorCode = HAL_SD_ERROR_NONE; 00386 00387 /* Initialize the SD operation */ 00388 hsd->Context = SD_CONTEXT_NONE; 00389 00390 /* Initialize the SD state */ 00391 hsd->State = HAL_SD_STATE_READY; 00392 00393 return HAL_OK; 00394 } 00395 00396 /** 00397 * @brief Initializes the SD Card. 00398 * @param hsd: Pointer to SD handle 00399 * @note This function initializes the SD card. It could be used when a card 00400 re-initialization is needed. 00401 * @retval HAL status 00402 */ 00403 HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd) 00404 { 00405 uint32_t errorstate; 00406 HAL_StatusTypeDef status; 00407 SD_InitTypeDef Init; 00408 00409 /* Default SDIO peripheral configuration for SD card initialization */ 00410 Init.ClockEdge = SDIO_CLOCK_EDGE_RISING; 00411 Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE; 00412 Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE; 00413 Init.BusWide = SDIO_BUS_WIDE_1B; 00414 Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE; 00415 Init.ClockDiv = SDIO_INIT_CLK_DIV; 00416 00417 /* Initialize SDIO peripheral interface with default configuration */ 00418 status = SDIO_Init(hsd->Instance, Init); 00419 if(status != HAL_OK) 00420 { 00421 return HAL_ERROR; 00422 } 00423 00424 /* Disable SDIO Clock */ 00425 __HAL_SD_DISABLE(hsd); 00426 00427 /* Set Power State to ON */ 00428 (void)SDIO_PowerState_ON(hsd->Instance); 00429 00430 /* Enable SDIO Clock */ 00431 __HAL_SD_ENABLE(hsd); 00432 00433 /* Identify card operating voltage */ 00434 errorstate = SD_PowerON(hsd); 00435 if(errorstate != HAL_SD_ERROR_NONE) 00436 { 00437 hsd->State = HAL_SD_STATE_READY; 00438 hsd->ErrorCode |= errorstate; 00439 return HAL_ERROR; 00440 } 00441 00442 /* Card initialization */ 00443 errorstate = SD_InitCard(hsd); 00444 if(errorstate != HAL_SD_ERROR_NONE) 00445 { 00446 hsd->State = HAL_SD_STATE_READY; 00447 hsd->ErrorCode |= errorstate; 00448 return HAL_ERROR; 00449 } 00450 00451 /* Set Block Size for Card */ 00452 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 00453 if(errorstate != HAL_SD_ERROR_NONE) 00454 { 00455 /* Clear all the static flags */ 00456 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00457 hsd->ErrorCode |= errorstate; 00458 hsd->State = HAL_SD_STATE_READY; 00459 return HAL_ERROR; 00460 } 00461 00462 return HAL_OK; 00463 } 00464 00465 /** 00466 * @brief De-Initializes the SD card. 00467 * @param hsd: Pointer to SD handle 00468 * @retval HAL status 00469 */ 00470 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd) 00471 { 00472 /* Check the SD handle allocation */ 00473 if(hsd == NULL) 00474 { 00475 return HAL_ERROR; 00476 } 00477 00478 /* Check the parameters */ 00479 assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance)); 00480 00481 hsd->State = HAL_SD_STATE_BUSY; 00482 00483 /* Set SD power state to off */ 00484 SD_PowerOFF(hsd); 00485 00486 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 00487 if(hsd->MspDeInitCallback == NULL) 00488 { 00489 hsd->MspDeInitCallback = HAL_SD_MspDeInit; 00490 } 00491 00492 /* DeInit the low level hardware */ 00493 hsd->MspDeInitCallback(hsd); 00494 #else 00495 /* De-Initialize the MSP layer */ 00496 HAL_SD_MspDeInit(hsd); 00497 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 00498 00499 hsd->ErrorCode = HAL_SD_ERROR_NONE; 00500 hsd->State = HAL_SD_STATE_RESET; 00501 00502 return HAL_OK; 00503 } 00504 00505 00506 /** 00507 * @brief Initializes the SD MSP. 00508 * @param hsd: Pointer to SD handle 00509 * @retval None 00510 */ 00511 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd) 00512 { 00513 /* Prevent unused argument(s) compilation warning */ 00514 UNUSED(hsd); 00515 00516 /* NOTE : This function should not be modified, when the callback is needed, 00517 the HAL_SD_MspInit could be implemented in the user file 00518 */ 00519 } 00520 00521 /** 00522 * @brief De-Initialize SD MSP. 00523 * @param hsd: Pointer to SD handle 00524 * @retval None 00525 */ 00526 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) 00527 { 00528 /* Prevent unused argument(s) compilation warning */ 00529 UNUSED(hsd); 00530 00531 /* NOTE : This function should not be modified, when the callback is needed, 00532 the HAL_SD_MspDeInit could be implemented in the user file 00533 */ 00534 } 00535 00536 /** 00537 * @} 00538 */ 00539 00540 /** @addtogroup SD_Exported_Functions_Group2 00541 * @brief Data transfer functions 00542 * 00543 @verbatim 00544 ============================================================================== 00545 ##### IO operation functions ##### 00546 ============================================================================== 00547 [..] 00548 This subsection provides a set of functions allowing to manage the data 00549 transfer from/to SD card. 00550 00551 @endverbatim 00552 * @{ 00553 */ 00554 00555 /** 00556 * @brief Reads block(s) from a specified address in a card. The Data transfer 00557 * is managed by polling mode. 00558 * @note This API should be followed by a check on the card state through 00559 * HAL_SD_GetCardState(). 00560 * @param hsd: Pointer to SD handle 00561 * @param pData: pointer to the buffer that will contain the received data 00562 * @param BlockAdd: Block Address from where data is to be read 00563 * @param NumberOfBlocks: Number of SD blocks to read 00564 * @param Timeout: Specify timeout value 00565 * @retval HAL status 00566 */ 00567 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout) 00568 { 00569 SDIO_DataInitTypeDef config; 00570 uint32_t errorstate; 00571 uint32_t tickstart = HAL_GetTick(); 00572 uint32_t count, data, dataremaining; 00573 uint32_t add = BlockAdd; 00574 uint8_t *tempbuff = pData; 00575 00576 if(NULL == pData) 00577 { 00578 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 00579 return HAL_ERROR; 00580 } 00581 00582 if(hsd->State == HAL_SD_STATE_READY) 00583 { 00584 hsd->ErrorCode = HAL_SD_ERROR_NONE; 00585 00586 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 00587 { 00588 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 00589 return HAL_ERROR; 00590 } 00591 00592 hsd->State = HAL_SD_STATE_BUSY; 00593 00594 /* Initialize data control register */ 00595 hsd->Instance->DCTRL = 0U; 00596 00597 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 00598 { 00599 add *= 512U; 00600 } 00601 00602 /* Configure the SD DPSM (Data Path State Machine) */ 00603 config.DataTimeOut = SDMMC_DATATIMEOUT; 00604 config.DataLength = NumberOfBlocks * BLOCKSIZE; 00605 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 00606 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; 00607 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 00608 config.DPSM = SDIO_DPSM_ENABLE; 00609 (void)SDIO_ConfigData(hsd->Instance, &config); 00610 00611 /* Read block(s) in polling mode */ 00612 if(NumberOfBlocks > 1U) 00613 { 00614 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK; 00615 00616 /* Read Multi Block command */ 00617 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add); 00618 } 00619 else 00620 { 00621 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK; 00622 00623 /* Read Single Block command */ 00624 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add); 00625 } 00626 if(errorstate != HAL_SD_ERROR_NONE) 00627 { 00628 /* Clear all the static flags */ 00629 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00630 hsd->ErrorCode |= errorstate; 00631 hsd->State = HAL_SD_STATE_READY; 00632 hsd->Context = SD_CONTEXT_NONE; 00633 return HAL_ERROR; 00634 } 00635 00636 /* Poll on SDIO flags */ 00637 dataremaining = config.DataLength; 00638 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR)) 00639 { 00640 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) && (dataremaining > 0U)) 00641 { 00642 /* Read data from SDIO Rx FIFO */ 00643 for(count = 0U; count < 8U; count++) 00644 { 00645 data = SDIO_ReadFIFO(hsd->Instance); 00646 *tempbuff = (uint8_t)(data & 0xFFU); 00647 tempbuff++; 00648 dataremaining--; 00649 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU); 00650 tempbuff++; 00651 dataremaining--; 00652 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU); 00653 tempbuff++; 00654 dataremaining--; 00655 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU); 00656 tempbuff++; 00657 dataremaining--; 00658 } 00659 } 00660 00661 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U)) 00662 { 00663 /* Clear all the static flags */ 00664 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00665 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT; 00666 hsd->State= HAL_SD_STATE_READY; 00667 hsd->Context = SD_CONTEXT_NONE; 00668 return HAL_TIMEOUT; 00669 } 00670 } 00671 00672 /* Send stop transmission command in case of multiblock read */ 00673 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U)) 00674 { 00675 if(hsd->SdCard.CardType != CARD_SECURED) 00676 { 00677 /* Send stop transmission command */ 00678 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 00679 if(errorstate != HAL_SD_ERROR_NONE) 00680 { 00681 /* Clear all the static flags */ 00682 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00683 hsd->ErrorCode |= errorstate; 00684 hsd->State = HAL_SD_STATE_READY; 00685 hsd->Context = SD_CONTEXT_NONE; 00686 return HAL_ERROR; 00687 } 00688 } 00689 } 00690 00691 /* Get error state */ 00692 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) 00693 { 00694 /* Clear all the static flags */ 00695 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00696 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; 00697 hsd->State = HAL_SD_STATE_READY; 00698 hsd->Context = SD_CONTEXT_NONE; 00699 return HAL_ERROR; 00700 } 00701 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) 00702 { 00703 /* Clear all the static flags */ 00704 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00705 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; 00706 hsd->State = HAL_SD_STATE_READY; 00707 hsd->Context = SD_CONTEXT_NONE; 00708 return HAL_ERROR; 00709 } 00710 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) 00711 { 00712 /* Clear all the static flags */ 00713 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00714 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN; 00715 hsd->State = HAL_SD_STATE_READY; 00716 hsd->Context = SD_CONTEXT_NONE; 00717 return HAL_ERROR; 00718 } 00719 else 00720 { 00721 /* Nothing to do */ 00722 } 00723 00724 /* Empty FIFO if there is still any data */ 00725 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (dataremaining > 0U)) 00726 { 00727 data = SDIO_ReadFIFO(hsd->Instance); 00728 *tempbuff = (uint8_t)(data & 0xFFU); 00729 tempbuff++; 00730 dataremaining--; 00731 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU); 00732 tempbuff++; 00733 dataremaining--; 00734 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU); 00735 tempbuff++; 00736 dataremaining--; 00737 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU); 00738 tempbuff++; 00739 dataremaining--; 00740 00741 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U)) 00742 { 00743 /* Clear all the static flags */ 00744 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00745 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT; 00746 hsd->State= HAL_SD_STATE_READY; 00747 hsd->Context = SD_CONTEXT_NONE; 00748 return HAL_ERROR; 00749 } 00750 } 00751 00752 /* Clear all the static flags */ 00753 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS); 00754 00755 hsd->State = HAL_SD_STATE_READY; 00756 00757 return HAL_OK; 00758 } 00759 else 00760 { 00761 hsd->ErrorCode |= HAL_SD_ERROR_BUSY; 00762 return HAL_ERROR; 00763 } 00764 } 00765 00766 /** 00767 * @brief Allows to write block(s) to a specified address in a card. The Data 00768 * transfer is managed by polling mode. 00769 * @note This API should be followed by a check on the card state through 00770 * HAL_SD_GetCardState(). 00771 * @param hsd: Pointer to SD handle 00772 * @param pData: pointer to the buffer that will contain the data to transmit 00773 * @param BlockAdd: Block Address where data will be written 00774 * @param NumberOfBlocks: Number of SD blocks to write 00775 * @param Timeout: Specify timeout value 00776 * @retval HAL status 00777 */ 00778 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout) 00779 { 00780 SDIO_DataInitTypeDef config; 00781 uint32_t errorstate; 00782 uint32_t tickstart = HAL_GetTick(); 00783 uint32_t count, data, dataremaining; 00784 uint32_t add = BlockAdd; 00785 uint8_t *tempbuff = pData; 00786 00787 if(NULL == pData) 00788 { 00789 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 00790 return HAL_ERROR; 00791 } 00792 00793 if(hsd->State == HAL_SD_STATE_READY) 00794 { 00795 hsd->ErrorCode = HAL_SD_ERROR_NONE; 00796 00797 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 00798 { 00799 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 00800 return HAL_ERROR; 00801 } 00802 00803 hsd->State = HAL_SD_STATE_BUSY; 00804 00805 /* Initialize data control register */ 00806 hsd->Instance->DCTRL = 0U; 00807 00808 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 00809 { 00810 add *= 512U; 00811 } 00812 00813 /* Configure the SD DPSM (Data Path State Machine) */ 00814 config.DataTimeOut = SDMMC_DATATIMEOUT; 00815 config.DataLength = NumberOfBlocks * BLOCKSIZE; 00816 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 00817 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD; 00818 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 00819 config.DPSM = SDIO_DPSM_ENABLE; 00820 (void)SDIO_ConfigData(hsd->Instance, &config); 00821 00822 /* Write Blocks in Polling mode */ 00823 if(NumberOfBlocks > 1U) 00824 { 00825 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK; 00826 00827 /* Write Multi Block command */ 00828 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add); 00829 } 00830 else 00831 { 00832 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK; 00833 00834 /* Write Single Block command */ 00835 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add); 00836 } 00837 if(errorstate != HAL_SD_ERROR_NONE) 00838 { 00839 /* Clear all the static flags */ 00840 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00841 hsd->ErrorCode |= errorstate; 00842 hsd->State = HAL_SD_STATE_READY; 00843 hsd->Context = SD_CONTEXT_NONE; 00844 return HAL_ERROR; 00845 } 00846 00847 /* Write block(s) in polling mode */ 00848 dataremaining = config.DataLength; 00849 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR)) 00850 { 00851 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) && (dataremaining > 0U)) 00852 { 00853 /* Write data to SDIO Tx FIFO */ 00854 for(count = 0U; count < 8U; count++) 00855 { 00856 data = (uint32_t)(*tempbuff); 00857 tempbuff++; 00858 dataremaining--; 00859 data |= ((uint32_t)(*tempbuff) << 8U); 00860 tempbuff++; 00861 dataremaining--; 00862 data |= ((uint32_t)(*tempbuff) << 16U); 00863 tempbuff++; 00864 dataremaining--; 00865 data |= ((uint32_t)(*tempbuff) << 24U); 00866 tempbuff++; 00867 dataremaining--; 00868 (void)SDIO_WriteFIFO(hsd->Instance, &data); 00869 } 00870 } 00871 00872 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U)) 00873 { 00874 /* Clear all the static flags */ 00875 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00876 hsd->ErrorCode |= errorstate; 00877 hsd->State = HAL_SD_STATE_READY; 00878 hsd->Context = SD_CONTEXT_NONE; 00879 return HAL_TIMEOUT; 00880 } 00881 } 00882 00883 /* Send stop transmission command in case of multiblock write */ 00884 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U)) 00885 { 00886 if(hsd->SdCard.CardType != CARD_SECURED) 00887 { 00888 /* Send stop transmission command */ 00889 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 00890 if(errorstate != HAL_SD_ERROR_NONE) 00891 { 00892 /* Clear all the static flags */ 00893 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00894 hsd->ErrorCode |= errorstate; 00895 hsd->State = HAL_SD_STATE_READY; 00896 hsd->Context = SD_CONTEXT_NONE; 00897 return HAL_ERROR; 00898 } 00899 } 00900 } 00901 00902 /* Get error state */ 00903 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) 00904 { 00905 /* Clear all the static flags */ 00906 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00907 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; 00908 hsd->State = HAL_SD_STATE_READY; 00909 hsd->Context = SD_CONTEXT_NONE; 00910 return HAL_ERROR; 00911 } 00912 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) 00913 { 00914 /* Clear all the static flags */ 00915 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00916 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; 00917 hsd->State = HAL_SD_STATE_READY; 00918 hsd->Context = SD_CONTEXT_NONE; 00919 return HAL_ERROR; 00920 } 00921 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR)) 00922 { 00923 /* Clear all the static flags */ 00924 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 00925 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN; 00926 hsd->State = HAL_SD_STATE_READY; 00927 hsd->Context = SD_CONTEXT_NONE; 00928 return HAL_ERROR; 00929 } 00930 else 00931 { 00932 /* Nothing to do */ 00933 } 00934 00935 /* Clear all the static flags */ 00936 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS); 00937 00938 hsd->State = HAL_SD_STATE_READY; 00939 00940 return HAL_OK; 00941 } 00942 else 00943 { 00944 hsd->ErrorCode |= HAL_SD_ERROR_BUSY; 00945 return HAL_ERROR; 00946 } 00947 } 00948 00949 /** 00950 * @brief Reads block(s) from a specified address in a card. The Data transfer 00951 * is managed in interrupt mode. 00952 * @note This API should be followed by a check on the card state through 00953 * HAL_SD_GetCardState(). 00954 * @note You could also check the IT transfer process through the SD Rx 00955 * interrupt event. 00956 * @param hsd: Pointer to SD handle 00957 * @param pData: Pointer to the buffer that will contain the received data 00958 * @param BlockAdd: Block Address from where data is to be read 00959 * @param NumberOfBlocks: Number of blocks to read. 00960 * @retval HAL status 00961 */ 00962 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) 00963 { 00964 SDIO_DataInitTypeDef config; 00965 uint32_t errorstate; 00966 uint32_t add = BlockAdd; 00967 00968 if(NULL == pData) 00969 { 00970 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 00971 return HAL_ERROR; 00972 } 00973 00974 if(hsd->State == HAL_SD_STATE_READY) 00975 { 00976 hsd->ErrorCode = HAL_SD_ERROR_NONE; 00977 00978 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 00979 { 00980 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 00981 return HAL_ERROR; 00982 } 00983 00984 hsd->State = HAL_SD_STATE_BUSY; 00985 00986 /* Initialize data control register */ 00987 hsd->Instance->DCTRL = 0U; 00988 00989 hsd->pRxBuffPtr = pData; 00990 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks; 00991 00992 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF)); 00993 00994 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 00995 { 00996 add *= 512U; 00997 } 00998 00999 /* Configure the SD DPSM (Data Path State Machine) */ 01000 config.DataTimeOut = SDMMC_DATATIMEOUT; 01001 config.DataLength = BLOCKSIZE * NumberOfBlocks; 01002 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 01003 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; 01004 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 01005 config.DPSM = SDIO_DPSM_ENABLE; 01006 (void)SDIO_ConfigData(hsd->Instance, &config); 01007 01008 /* Read Blocks in IT mode */ 01009 if(NumberOfBlocks > 1U) 01010 { 01011 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT); 01012 01013 /* Read Multi Block command */ 01014 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add); 01015 } 01016 else 01017 { 01018 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT); 01019 01020 /* Read Single Block command */ 01021 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add); 01022 } 01023 if(errorstate != HAL_SD_ERROR_NONE) 01024 { 01025 /* Clear all the static flags */ 01026 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01027 hsd->ErrorCode |= errorstate; 01028 hsd->State = HAL_SD_STATE_READY; 01029 hsd->Context = SD_CONTEXT_NONE; 01030 return HAL_ERROR; 01031 } 01032 01033 return HAL_OK; 01034 } 01035 else 01036 { 01037 return HAL_BUSY; 01038 } 01039 } 01040 01041 /** 01042 * @brief Writes block(s) to a specified address in a card. The Data transfer 01043 * is managed in interrupt mode. 01044 * @note This API should be followed by a check on the card state through 01045 * HAL_SD_GetCardState(). 01046 * @note You could also check the IT transfer process through the SD Tx 01047 * interrupt event. 01048 * @param hsd: Pointer to SD handle 01049 * @param pData: Pointer to the buffer that will contain the data to transmit 01050 * @param BlockAdd: Block Address where data will be written 01051 * @param NumberOfBlocks: Number of blocks to write 01052 * @retval HAL status 01053 */ 01054 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) 01055 { 01056 SDIO_DataInitTypeDef config; 01057 uint32_t errorstate; 01058 uint32_t add = BlockAdd; 01059 01060 if(NULL == pData) 01061 { 01062 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01063 return HAL_ERROR; 01064 } 01065 01066 if(hsd->State == HAL_SD_STATE_READY) 01067 { 01068 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01069 01070 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 01071 { 01072 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01073 return HAL_ERROR; 01074 } 01075 01076 hsd->State = HAL_SD_STATE_BUSY; 01077 01078 /* Initialize data control register */ 01079 hsd->Instance->DCTRL = 0U; 01080 01081 hsd->pTxBuffPtr = pData; 01082 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks; 01083 01084 /* Enable transfer interrupts */ 01085 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE)); 01086 01087 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 01088 { 01089 add *= 512U; 01090 } 01091 01092 /* Write Blocks in Polling mode */ 01093 if(NumberOfBlocks > 1U) 01094 { 01095 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT); 01096 01097 /* Write Multi Block command */ 01098 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add); 01099 } 01100 else 01101 { 01102 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT); 01103 01104 /* Write Single Block command */ 01105 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add); 01106 } 01107 if(errorstate != HAL_SD_ERROR_NONE) 01108 { 01109 /* Clear all the static flags */ 01110 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01111 hsd->ErrorCode |= errorstate; 01112 hsd->State = HAL_SD_STATE_READY; 01113 hsd->Context = SD_CONTEXT_NONE; 01114 return HAL_ERROR; 01115 } 01116 01117 /* Configure the SD DPSM (Data Path State Machine) */ 01118 config.DataTimeOut = SDMMC_DATATIMEOUT; 01119 config.DataLength = BLOCKSIZE * NumberOfBlocks; 01120 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 01121 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD; 01122 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 01123 config.DPSM = SDIO_DPSM_ENABLE; 01124 (void)SDIO_ConfigData(hsd->Instance, &config); 01125 01126 return HAL_OK; 01127 } 01128 else 01129 { 01130 return HAL_BUSY; 01131 } 01132 } 01133 01134 /** 01135 * @brief Reads block(s) from a specified address in a card. The Data transfer 01136 * is managed by DMA mode. 01137 * @note This API should be followed by a check on the card state through 01138 * HAL_SD_GetCardState(). 01139 * @note You could also check the DMA transfer process through the SD Rx 01140 * interrupt event. 01141 * @param hsd: Pointer SD handle 01142 * @param pData: Pointer to the buffer that will contain the received data 01143 * @param BlockAdd: Block Address from where data is to be read 01144 * @param NumberOfBlocks: Number of blocks to read. 01145 * @retval HAL status 01146 */ 01147 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) 01148 { 01149 SDIO_DataInitTypeDef config; 01150 uint32_t errorstate; 01151 uint32_t add = BlockAdd; 01152 01153 if(NULL == pData) 01154 { 01155 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01156 return HAL_ERROR; 01157 } 01158 01159 if(hsd->State == HAL_SD_STATE_READY) 01160 { 01161 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01162 01163 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 01164 { 01165 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01166 return HAL_ERROR; 01167 } 01168 01169 hsd->State = HAL_SD_STATE_BUSY; 01170 01171 /* Initialize data control register */ 01172 hsd->Instance->DCTRL = 0U; 01173 01174 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND)); 01175 01176 /* Set the DMA transfer complete callback */ 01177 hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt; 01178 01179 /* Set the DMA error callback */ 01180 hsd->hdmarx->XferErrorCallback = SD_DMAError; 01181 01182 /* Set the DMA Abort callback */ 01183 hsd->hdmarx->XferAbortCallback = NULL; 01184 01185 /* Force DMA Direction */ 01186 hsd->hdmarx->Init.Direction = DMA_PERIPH_TO_MEMORY; 01187 MODIFY_REG(hsd->hdmarx->Instance->CCR, DMA_CCR_DIR, hsd->hdmarx->Init.Direction); 01188 01189 /* Enable the DMA Channel */ 01190 if(HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK) 01191 { 01192 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND)); 01193 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01194 hsd->ErrorCode |= HAL_SD_ERROR_DMA; 01195 hsd->State = HAL_SD_STATE_READY; 01196 return HAL_ERROR; 01197 } 01198 else 01199 { 01200 /* Enable SD DMA transfer */ 01201 __HAL_SD_DMA_ENABLE(hsd); 01202 01203 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 01204 { 01205 add *= 512U; 01206 } 01207 01208 /* Configure the SD DPSM (Data Path State Machine) */ 01209 config.DataTimeOut = SDMMC_DATATIMEOUT; 01210 config.DataLength = BLOCKSIZE * NumberOfBlocks; 01211 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 01212 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; 01213 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 01214 config.DPSM = SDIO_DPSM_ENABLE; 01215 (void)SDIO_ConfigData(hsd->Instance, &config); 01216 01217 /* Read Blocks in DMA mode */ 01218 if(NumberOfBlocks > 1U) 01219 { 01220 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA); 01221 01222 /* Read Multi Block command */ 01223 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add); 01224 } 01225 else 01226 { 01227 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA); 01228 01229 /* Read Single Block command */ 01230 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add); 01231 } 01232 if(errorstate != HAL_SD_ERROR_NONE) 01233 { 01234 /* Clear all the static flags */ 01235 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01236 hsd->ErrorCode |= errorstate; 01237 hsd->State = HAL_SD_STATE_READY; 01238 hsd->Context = SD_CONTEXT_NONE; 01239 return HAL_ERROR; 01240 } 01241 01242 return HAL_OK; 01243 } 01244 } 01245 else 01246 { 01247 return HAL_BUSY; 01248 } 01249 } 01250 01251 /** 01252 * @brief Writes block(s) to a specified address in a card. The Data transfer 01253 * is managed by DMA mode. 01254 * @note This API should be followed by a check on the card state through 01255 * HAL_SD_GetCardState(). 01256 * @note You could also check the DMA transfer process through the SD Tx 01257 * interrupt event. 01258 * @param hsd: Pointer to SD handle 01259 * @param pData: Pointer to the buffer that will contain the data to transmit 01260 * @param BlockAdd: Block Address where data will be written 01261 * @param NumberOfBlocks: Number of blocks to write 01262 * @retval HAL status 01263 */ 01264 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks) 01265 { 01266 SDIO_DataInitTypeDef config; 01267 uint32_t errorstate; 01268 uint32_t add = BlockAdd; 01269 01270 if(NULL == pData) 01271 { 01272 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01273 return HAL_ERROR; 01274 } 01275 01276 if(hsd->State == HAL_SD_STATE_READY) 01277 { 01278 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01279 01280 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr)) 01281 { 01282 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01283 return HAL_ERROR; 01284 } 01285 01286 hsd->State = HAL_SD_STATE_BUSY; 01287 01288 /* Initialize data control register */ 01289 hsd->Instance->DCTRL = 0U; 01290 01291 /* Enable SD Error interrupts */ 01292 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR)); 01293 01294 /* Set the DMA transfer complete callback */ 01295 hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt; 01296 01297 /* Set the DMA error callback */ 01298 hsd->hdmatx->XferErrorCallback = SD_DMAError; 01299 01300 /* Set the DMA Abort callback */ 01301 hsd->hdmatx->XferAbortCallback = NULL; 01302 01303 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 01304 { 01305 add *= 512U; 01306 } 01307 01308 /* Write Blocks in Polling mode */ 01309 if(NumberOfBlocks > 1U) 01310 { 01311 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA); 01312 01313 /* Write Multi Block command */ 01314 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add); 01315 } 01316 else 01317 { 01318 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA); 01319 01320 /* Write Single Block command */ 01321 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add); 01322 } 01323 if(errorstate != HAL_SD_ERROR_NONE) 01324 { 01325 /* Clear all the static flags */ 01326 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01327 hsd->ErrorCode |= errorstate; 01328 hsd->State = HAL_SD_STATE_READY; 01329 hsd->Context = SD_CONTEXT_NONE; 01330 return HAL_ERROR; 01331 } 01332 01333 /* Enable SDIO DMA transfer */ 01334 __HAL_SD_DMA_ENABLE(hsd); 01335 01336 /* Force DMA Direction */ 01337 hsd->hdmatx->Init.Direction = DMA_MEMORY_TO_PERIPH; 01338 MODIFY_REG(hsd->hdmatx->Instance->CCR, DMA_CCR_DIR, hsd->hdmatx->Init.Direction); 01339 01340 /* Enable the DMA Channel */ 01341 if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK) 01342 { 01343 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR)); 01344 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01345 hsd->ErrorCode |= HAL_SD_ERROR_DMA; 01346 hsd->State = HAL_SD_STATE_READY; 01347 hsd->Context = SD_CONTEXT_NONE; 01348 return HAL_ERROR; 01349 } 01350 else 01351 { 01352 /* Configure the SD DPSM (Data Path State Machine) */ 01353 config.DataTimeOut = SDMMC_DATATIMEOUT; 01354 config.DataLength = BLOCKSIZE * NumberOfBlocks; 01355 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B; 01356 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD; 01357 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 01358 config.DPSM = SDIO_DPSM_ENABLE; 01359 (void)SDIO_ConfigData(hsd->Instance, &config); 01360 01361 return HAL_OK; 01362 } 01363 } 01364 else 01365 { 01366 return HAL_BUSY; 01367 } 01368 } 01369 01370 /** 01371 * @brief Erases the specified memory area of the given SD card. 01372 * @note This API should be followed by a check on the card state through 01373 * HAL_SD_GetCardState(). 01374 * @param hsd: Pointer to SD handle 01375 * @param BlockStartAdd: Start Block address 01376 * @param BlockEndAdd: End Block address 01377 * @retval HAL status 01378 */ 01379 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd) 01380 { 01381 uint32_t errorstate; 01382 uint32_t start_add = BlockStartAdd; 01383 uint32_t end_add = BlockEndAdd; 01384 01385 if(hsd->State == HAL_SD_STATE_READY) 01386 { 01387 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01388 01389 if(end_add < start_add) 01390 { 01391 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 01392 return HAL_ERROR; 01393 } 01394 01395 if(end_add > (hsd->SdCard.LogBlockNbr)) 01396 { 01397 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE; 01398 return HAL_ERROR; 01399 } 01400 01401 hsd->State = HAL_SD_STATE_BUSY; 01402 01403 /* Check if the card command class supports erase command */ 01404 if(((hsd->SdCard.Class) & SDIO_CCCC_ERASE) == 0U) 01405 { 01406 /* Clear all the static flags */ 01407 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01408 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 01409 hsd->State = HAL_SD_STATE_READY; 01410 return HAL_ERROR; 01411 } 01412 01413 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 01414 { 01415 /* Clear all the static flags */ 01416 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01417 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 01418 hsd->State = HAL_SD_STATE_READY; 01419 return HAL_ERROR; 01420 } 01421 01422 /* Get start and end block for high capacity cards */ 01423 if(hsd->SdCard.CardType != CARD_SDHC_SDXC) 01424 { 01425 start_add *= 512U; 01426 end_add *= 512U; 01427 } 01428 01429 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */ 01430 if(hsd->SdCard.CardType != CARD_SECURED) 01431 { 01432 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */ 01433 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add); 01434 if(errorstate != HAL_SD_ERROR_NONE) 01435 { 01436 /* Clear all the static flags */ 01437 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01438 hsd->ErrorCode |= errorstate; 01439 hsd->State = HAL_SD_STATE_READY; 01440 return HAL_ERROR; 01441 } 01442 01443 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */ 01444 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add); 01445 if(errorstate != HAL_SD_ERROR_NONE) 01446 { 01447 /* Clear all the static flags */ 01448 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01449 hsd->ErrorCode |= errorstate; 01450 hsd->State = HAL_SD_STATE_READY; 01451 return HAL_ERROR; 01452 } 01453 } 01454 01455 /* Send CMD38 ERASE */ 01456 errorstate = SDMMC_CmdErase(hsd->Instance); 01457 if(errorstate != HAL_SD_ERROR_NONE) 01458 { 01459 /* Clear all the static flags */ 01460 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 01461 hsd->ErrorCode |= errorstate; 01462 hsd->State = HAL_SD_STATE_READY; 01463 return HAL_ERROR; 01464 } 01465 01466 hsd->State = HAL_SD_STATE_READY; 01467 01468 return HAL_OK; 01469 } 01470 else 01471 { 01472 return HAL_BUSY; 01473 } 01474 } 01475 01476 /** 01477 * @brief This function handles SD card interrupt request. 01478 * @param hsd: Pointer to SD handle 01479 * @retval None 01480 */ 01481 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd) 01482 { 01483 uint32_t errorstate; 01484 uint32_t context = hsd->Context; 01485 01486 /* Check for SDIO interrupt flags */ 01487 if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U)) 01488 { 01489 SD_Read_IT(hsd); 01490 } 01491 01492 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) != RESET) 01493 { 01494 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DATAEND); 01495 01496 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 01497 SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR | SDIO_IT_TXFIFOHE |\ 01498 SDIO_IT_RXFIFOHF); 01499 01500 hsd->Instance->DCTRL &= ~(SDIO_DCTRL_DTEN); 01501 01502 if((context & SD_CONTEXT_IT) != 0U) 01503 { 01504 if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)) 01505 { 01506 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 01507 if(errorstate != HAL_SD_ERROR_NONE) 01508 { 01509 hsd->ErrorCode |= errorstate; 01510 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01511 hsd->ErrorCallback(hsd); 01512 #else 01513 HAL_SD_ErrorCallback(hsd); 01514 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01515 } 01516 } 01517 01518 /* Clear all the static flags */ 01519 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS); 01520 01521 hsd->State = HAL_SD_STATE_READY; 01522 hsd->Context = SD_CONTEXT_NONE; 01523 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U)) 01524 { 01525 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01526 hsd->RxCpltCallback(hsd); 01527 #else 01528 HAL_SD_RxCpltCallback(hsd); 01529 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01530 } 01531 else 01532 { 01533 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01534 hsd->TxCpltCallback(hsd); 01535 #else 01536 HAL_SD_TxCpltCallback(hsd); 01537 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01538 } 01539 } 01540 else if((context & SD_CONTEXT_DMA) != 0U) 01541 { 01542 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U) 01543 { 01544 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 01545 if(errorstate != HAL_SD_ERROR_NONE) 01546 { 01547 hsd->ErrorCode |= errorstate; 01548 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01549 hsd->ErrorCallback(hsd); 01550 #else 01551 HAL_SD_ErrorCallback(hsd); 01552 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01553 } 01554 } 01555 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U)) 01556 { 01557 /* Disable the DMA transfer for transmit request by setting the DMAEN bit 01558 in the SD DCTRL register */ 01559 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); 01560 01561 hsd->State = HAL_SD_STATE_READY; 01562 01563 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01564 hsd->TxCpltCallback(hsd); 01565 #else 01566 HAL_SD_TxCpltCallback(hsd); 01567 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01568 } 01569 } 01570 else 01571 { 01572 /* Nothing to do */ 01573 } 01574 } 01575 01576 else if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U)) 01577 { 01578 SD_Write_IT(hsd); 01579 } 01580 01581 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR) != RESET) 01582 { 01583 /* Set Error code */ 01584 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL) != RESET) 01585 { 01586 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL; 01587 } 01588 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT) != RESET) 01589 { 01590 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT; 01591 } 01592 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR) != RESET) 01593 { 01594 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN; 01595 } 01596 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR) != RESET) 01597 { 01598 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN; 01599 } 01600 01601 /* Clear All flags */ 01602 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS | SDIO_FLAG_STBITERR); 01603 01604 /* Disable all interrupts */ 01605 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 01606 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR); 01607 01608 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance); 01609 01610 if((context & SD_CONTEXT_IT) != 0U) 01611 { 01612 /* Set the SD state to ready to be able to start again the process */ 01613 hsd->State = HAL_SD_STATE_READY; 01614 hsd->Context = SD_CONTEXT_NONE; 01615 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01616 hsd->ErrorCallback(hsd); 01617 #else 01618 HAL_SD_ErrorCallback(hsd); 01619 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01620 } 01621 else if((context & SD_CONTEXT_DMA) != 0U) 01622 { 01623 /* Abort the SD DMA channel */ 01624 if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)) 01625 { 01626 /* Set the DMA Tx abort callback */ 01627 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort; 01628 /* Abort DMA in IT mode */ 01629 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK) 01630 { 01631 SD_DMATxAbort(hsd->hdmatx); 01632 } 01633 } 01634 else if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U)) 01635 { 01636 /* Set the DMA Rx abort callback */ 01637 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort; 01638 /* Abort DMA in IT mode */ 01639 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK) 01640 { 01641 SD_DMARxAbort(hsd->hdmarx); 01642 } 01643 } 01644 else 01645 { 01646 hsd->ErrorCode = HAL_SD_ERROR_NONE; 01647 hsd->State = HAL_SD_STATE_READY; 01648 hsd->Context = SD_CONTEXT_NONE; 01649 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01650 hsd->AbortCpltCallback(hsd); 01651 #else 01652 HAL_SD_AbortCallback(hsd); 01653 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01654 } 01655 } 01656 else 01657 { 01658 /* Nothing to do */ 01659 } 01660 } 01661 else 01662 { 01663 /* Nothing to do */ 01664 } 01665 } 01666 01667 /** 01668 * @brief return the SD state 01669 * @param hsd: Pointer to sd handle 01670 * @retval HAL state 01671 */ 01672 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd) 01673 { 01674 return hsd->State; 01675 } 01676 01677 /** 01678 * @brief Return the SD error code 01679 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains 01680 * the configuration information. 01681 * @retval SD Error Code 01682 */ 01683 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd) 01684 { 01685 return hsd->ErrorCode; 01686 } 01687 01688 /** 01689 * @brief Tx Transfer completed callbacks 01690 * @param hsd: Pointer to SD handle 01691 * @retval None 01692 */ 01693 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd) 01694 { 01695 /* Prevent unused argument(s) compilation warning */ 01696 UNUSED(hsd); 01697 01698 /* NOTE : This function should not be modified, when the callback is needed, 01699 the HAL_SD_TxCpltCallback can be implemented in the user file 01700 */ 01701 } 01702 01703 /** 01704 * @brief Rx Transfer completed callbacks 01705 * @param hsd: Pointer SD handle 01706 * @retval None 01707 */ 01708 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd) 01709 { 01710 /* Prevent unused argument(s) compilation warning */ 01711 UNUSED(hsd); 01712 01713 /* NOTE : This function should not be modified, when the callback is needed, 01714 the HAL_SD_RxCpltCallback can be implemented in the user file 01715 */ 01716 } 01717 01718 /** 01719 * @brief SD error callbacks 01720 * @param hsd: Pointer SD handle 01721 * @retval None 01722 */ 01723 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd) 01724 { 01725 /* Prevent unused argument(s) compilation warning */ 01726 UNUSED(hsd); 01727 01728 /* NOTE : This function should not be modified, when the callback is needed, 01729 the HAL_SD_ErrorCallback can be implemented in the user file 01730 */ 01731 } 01732 01733 /** 01734 * @brief SD Abort callbacks 01735 * @param hsd: Pointer SD handle 01736 * @retval None 01737 */ 01738 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd) 01739 { 01740 /* Prevent unused argument(s) compilation warning */ 01741 UNUSED(hsd); 01742 01743 /* NOTE : This function should not be modified, when the callback is needed, 01744 the HAL_SD_AbortCallback can be implemented in the user file 01745 */ 01746 } 01747 01748 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 01749 /** 01750 * @brief Register a User SD Callback 01751 * To be used instead of the weak (surcharged) predefined callback 01752 * @param hsd : SD handle 01753 * @param CallbackID : ID of the callback to be registered 01754 * This parameter can be one of the following values: 01755 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID 01756 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID 01757 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID 01758 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID 01759 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID 01760 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID 01761 * @param pCallback : pointer to the Callback function 01762 * @retval status 01763 */ 01764 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback) 01765 { 01766 HAL_StatusTypeDef status = HAL_OK; 01767 01768 if(pCallback == NULL) 01769 { 01770 /* Update the error code */ 01771 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 01772 return HAL_ERROR; 01773 } 01774 01775 /* Process locked */ 01776 __HAL_LOCK(hsd); 01777 01778 if(hsd->State == HAL_SD_STATE_READY) 01779 { 01780 switch (CallbackID) 01781 { 01782 case HAL_SD_TX_CPLT_CB_ID : 01783 hsd->TxCpltCallback = pCallback; 01784 break; 01785 case HAL_SD_RX_CPLT_CB_ID : 01786 hsd->RxCpltCallback = pCallback; 01787 break; 01788 case HAL_SD_ERROR_CB_ID : 01789 hsd->ErrorCallback = pCallback; 01790 break; 01791 case HAL_SD_ABORT_CB_ID : 01792 hsd->AbortCpltCallback = pCallback; 01793 break; 01794 case HAL_SD_MSP_INIT_CB_ID : 01795 hsd->MspInitCallback = pCallback; 01796 break; 01797 case HAL_SD_MSP_DEINIT_CB_ID : 01798 hsd->MspDeInitCallback = pCallback; 01799 break; 01800 default : 01801 /* Update the error code */ 01802 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 01803 /* update return status */ 01804 status = HAL_ERROR; 01805 break; 01806 } 01807 } 01808 else if (hsd->State == HAL_SD_STATE_RESET) 01809 { 01810 switch (CallbackID) 01811 { 01812 case HAL_SD_MSP_INIT_CB_ID : 01813 hsd->MspInitCallback = pCallback; 01814 break; 01815 case HAL_SD_MSP_DEINIT_CB_ID : 01816 hsd->MspDeInitCallback = pCallback; 01817 break; 01818 default : 01819 /* Update the error code */ 01820 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 01821 /* update return status */ 01822 status = HAL_ERROR; 01823 break; 01824 } 01825 } 01826 else 01827 { 01828 /* Update the error code */ 01829 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 01830 /* update return status */ 01831 status = HAL_ERROR; 01832 } 01833 01834 /* Release Lock */ 01835 __HAL_UNLOCK(hsd); 01836 return status; 01837 } 01838 01839 /** 01840 * @brief Unregister a User SD Callback 01841 * SD Callback is redirected to the weak (surcharged) predefined callback 01842 * @param hsd : SD handle 01843 * @param CallbackID : ID of the callback to be unregistered 01844 * This parameter can be one of the following values: 01845 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID 01846 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID 01847 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID 01848 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID 01849 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID 01850 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID 01851 * @retval status 01852 */ 01853 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID) 01854 { 01855 HAL_StatusTypeDef status = HAL_OK; 01856 01857 /* Process locked */ 01858 __HAL_LOCK(hsd); 01859 01860 if(hsd->State == HAL_SD_STATE_READY) 01861 { 01862 switch (CallbackID) 01863 { 01864 case HAL_SD_TX_CPLT_CB_ID : 01865 hsd->TxCpltCallback = HAL_SD_TxCpltCallback; 01866 break; 01867 case HAL_SD_RX_CPLT_CB_ID : 01868 hsd->RxCpltCallback = HAL_SD_RxCpltCallback; 01869 break; 01870 case HAL_SD_ERROR_CB_ID : 01871 hsd->ErrorCallback = HAL_SD_ErrorCallback; 01872 break; 01873 case HAL_SD_ABORT_CB_ID : 01874 hsd->AbortCpltCallback = HAL_SD_AbortCallback; 01875 break; 01876 case HAL_SD_MSP_INIT_CB_ID : 01877 hsd->MspInitCallback = HAL_SD_MspInit; 01878 break; 01879 case HAL_SD_MSP_DEINIT_CB_ID : 01880 hsd->MspDeInitCallback = HAL_SD_MspDeInit; 01881 break; 01882 default : 01883 /* Update the error code */ 01884 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 01885 /* update return status */ 01886 status = HAL_ERROR; 01887 break; 01888 } 01889 } 01890 else if (hsd->State == HAL_SD_STATE_RESET) 01891 { 01892 switch (CallbackID) 01893 { 01894 case HAL_SD_MSP_INIT_CB_ID : 01895 hsd->MspInitCallback = HAL_SD_MspInit; 01896 break; 01897 case HAL_SD_MSP_DEINIT_CB_ID : 01898 hsd->MspDeInitCallback = HAL_SD_MspDeInit; 01899 break; 01900 default : 01901 /* Update the error code */ 01902 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 01903 /* update return status */ 01904 status = HAL_ERROR; 01905 break; 01906 } 01907 } 01908 else 01909 { 01910 /* Update the error code */ 01911 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK; 01912 /* update return status */ 01913 status = HAL_ERROR; 01914 } 01915 01916 /* Release Lock */ 01917 __HAL_UNLOCK(hsd); 01918 return status; 01919 } 01920 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 01921 01922 /** 01923 * @} 01924 */ 01925 01926 /** @addtogroup SD_Exported_Functions_Group3 01927 * @brief management functions 01928 * 01929 @verbatim 01930 ============================================================================== 01931 ##### Peripheral Control functions ##### 01932 ============================================================================== 01933 [..] 01934 This subsection provides a set of functions allowing to control the SD card 01935 operations and get the related information 01936 01937 @endverbatim 01938 * @{ 01939 */ 01940 01941 /** 01942 * @brief Returns information the information of the card which are stored on 01943 * the CID register. 01944 * @param hsd: Pointer to SD handle 01945 * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that 01946 * contains all CID register parameters 01947 * @retval HAL status 01948 */ 01949 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID) 01950 { 01951 pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U); 01952 01953 pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U); 01954 01955 pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U)); 01956 01957 pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU); 01958 01959 pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U); 01960 01961 pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U)); 01962 01963 pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U); 01964 01965 pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U); 01966 01967 pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U); 01968 01969 pCID->Reserved2 = 1U; 01970 01971 return HAL_OK; 01972 } 01973 01974 /** 01975 * @brief Returns information the information of the card which are stored on 01976 * the CSD register. 01977 * @param hsd: Pointer to SD handle 01978 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that 01979 * contains all CSD register parameters 01980 * @retval HAL status 01981 */ 01982 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD) 01983 { 01984 pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U); 01985 01986 pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U); 01987 01988 pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U); 01989 01990 pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U); 01991 01992 pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U); 01993 01994 pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU); 01995 01996 pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U); 01997 01998 pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U); 01999 02000 pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U); 02001 02002 pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U); 02003 02004 pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U); 02005 02006 pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U); 02007 02008 pCSD->Reserved2 = 0U; /*!< Reserved */ 02009 02010 if(hsd->SdCard.CardType == CARD_SDSC) 02011 { 02012 pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U)); 02013 02014 pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U); 02015 02016 pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U); 02017 02018 pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U); 02019 02020 pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U); 02021 02022 pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U); 02023 02024 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ; 02025 hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U)); 02026 hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU)); 02027 02028 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U); 02029 hsd->SdCard.LogBlockSize = 512U; 02030 } 02031 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC) 02032 { 02033 /* Byte 7 */ 02034 pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U)); 02035 02036 hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U); 02037 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr; 02038 hsd->SdCard.BlockSize = 512U; 02039 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize; 02040 } 02041 else 02042 { 02043 /* Clear all the static flags */ 02044 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02045 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02046 hsd->State = HAL_SD_STATE_READY; 02047 return HAL_ERROR; 02048 } 02049 02050 pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U); 02051 02052 pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U); 02053 02054 pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU); 02055 02056 pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U); 02057 02058 pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U); 02059 02060 pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U); 02061 02062 pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U); 02063 02064 pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U); 02065 02066 pCSD->Reserved3 = 0; 02067 02068 pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U); 02069 02070 pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U); 02071 02072 pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U); 02073 02074 pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U); 02075 02076 pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U); 02077 02078 pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U); 02079 02080 pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U); 02081 02082 pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U); 02083 02084 pCSD->Reserved4 = 1; 02085 02086 return HAL_OK; 02087 } 02088 02089 /** 02090 * @brief Gets the SD status info. 02091 * @param hsd: Pointer to SD handle 02092 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that 02093 * will contain the SD card status information 02094 * @retval HAL status 02095 */ 02096 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus) 02097 { 02098 uint32_t sd_status[16]; 02099 uint32_t errorstate; 02100 HAL_StatusTypeDef status = HAL_OK; 02101 02102 errorstate = SD_SendSDStatus(hsd, sd_status); 02103 if(errorstate != HAL_SD_ERROR_NONE) 02104 { 02105 /* Clear all the static flags */ 02106 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02107 hsd->ErrorCode |= errorstate; 02108 hsd->State = HAL_SD_STATE_READY; 02109 status = HAL_ERROR; 02110 } 02111 else 02112 { 02113 pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U); 02114 02115 pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U); 02116 02117 pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U)); 02118 02119 pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) | 02120 ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U)); 02121 02122 pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU); 02123 02124 pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U); 02125 02126 pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U); 02127 02128 pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU)); 02129 02130 pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U); 02131 02132 pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U); 02133 } 02134 02135 /* Set Block Size for Card */ 02136 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 02137 if(errorstate != HAL_SD_ERROR_NONE) 02138 { 02139 /* Clear all the static flags */ 02140 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02141 hsd->ErrorCode = errorstate; 02142 hsd->State = HAL_SD_STATE_READY; 02143 status = HAL_ERROR; 02144 } 02145 02146 return status; 02147 } 02148 02149 /** 02150 * @brief Gets the SD card info. 02151 * @param hsd: Pointer to SD handle 02152 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that 02153 * will contain the SD card status information 02154 * @retval HAL status 02155 */ 02156 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo) 02157 { 02158 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType); 02159 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion); 02160 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class); 02161 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd); 02162 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr); 02163 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize); 02164 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr); 02165 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize); 02166 02167 return HAL_OK; 02168 } 02169 02170 /** 02171 * @brief Enables wide bus operation for the requested card if supported by 02172 * card. 02173 * @param hsd: Pointer to SD handle 02174 * @param WideMode: Specifies the SD card wide bus mode 02175 * This parameter can be one of the following values: 02176 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer 02177 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer 02178 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer 02179 * @retval HAL status 02180 */ 02181 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode) 02182 { 02183 SDIO_InitTypeDef Init; 02184 uint32_t errorstate; 02185 HAL_StatusTypeDef status = HAL_OK; 02186 02187 /* Check the parameters */ 02188 assert_param(IS_SDIO_BUS_WIDE(WideMode)); 02189 02190 /* Change State */ 02191 hsd->State = HAL_SD_STATE_BUSY; 02192 02193 if(hsd->SdCard.CardType != CARD_SECURED) 02194 { 02195 if(WideMode == SDIO_BUS_WIDE_8B) 02196 { 02197 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02198 } 02199 else if(WideMode == SDIO_BUS_WIDE_4B) 02200 { 02201 errorstate = SD_WideBus_Enable(hsd); 02202 02203 hsd->ErrorCode |= errorstate; 02204 } 02205 else if(WideMode == SDIO_BUS_WIDE_1B) 02206 { 02207 errorstate = SD_WideBus_Disable(hsd); 02208 02209 hsd->ErrorCode |= errorstate; 02210 } 02211 else 02212 { 02213 /* WideMode is not a valid argument*/ 02214 hsd->ErrorCode |= HAL_SD_ERROR_PARAM; 02215 } 02216 } 02217 else 02218 { 02219 /* MMC Card does not support this feature */ 02220 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02221 } 02222 02223 if(hsd->ErrorCode != HAL_SD_ERROR_NONE) 02224 { 02225 /* Clear all the static flags */ 02226 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02227 hsd->State = HAL_SD_STATE_READY; 02228 status = HAL_ERROR; 02229 } 02230 else 02231 { 02232 /* Configure the SDIO peripheral */ 02233 Init.ClockEdge = hsd->Init.ClockEdge; 02234 Init.ClockBypass = hsd->Init.ClockBypass; 02235 Init.ClockPowerSave = hsd->Init.ClockPowerSave; 02236 Init.BusWide = WideMode; 02237 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl; 02238 Init.ClockDiv = hsd->Init.ClockDiv; 02239 (void)SDIO_Init(hsd->Instance, Init); 02240 } 02241 02242 /* Set Block Size for Card */ 02243 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE); 02244 if(errorstate != HAL_SD_ERROR_NONE) 02245 { 02246 /* Clear all the static flags */ 02247 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02248 hsd->ErrorCode |= errorstate; 02249 status = HAL_ERROR; 02250 } 02251 02252 /* Change State */ 02253 hsd->State = HAL_SD_STATE_READY; 02254 02255 return status; 02256 } 02257 02258 /** 02259 * @brief Gets the current sd card data state. 02260 * @param hsd: pointer to SD handle 02261 * @retval Card state 02262 */ 02263 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd) 02264 { 02265 uint32_t cardstate; 02266 uint32_t errorstate; 02267 uint32_t resp1 = 0; 02268 02269 errorstate = SD_SendStatus(hsd, &resp1); 02270 if(errorstate != HAL_SD_ERROR_NONE) 02271 { 02272 hsd->ErrorCode |= errorstate; 02273 } 02274 02275 cardstate = ((resp1 >> 9U) & 0x0FU); 02276 02277 return (HAL_SD_CardStateTypeDef)cardstate; 02278 } 02279 02280 /** 02281 * @brief Abort the current transfer and disable the SD. 02282 * @param hsd: pointer to a SD_HandleTypeDef structure that contains 02283 * the configuration information for SD module. 02284 * @retval HAL status 02285 */ 02286 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd) 02287 { 02288 HAL_SD_CardStateTypeDef CardState; 02289 uint32_t context = hsd->Context; 02290 02291 /* DIsable All interrupts */ 02292 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 02293 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); 02294 02295 /* Clear All flags */ 02296 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS); 02297 02298 CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN); 02299 02300 if ((context & SD_CONTEXT_DMA) != 0U) 02301 { 02302 /* Disable the SD DMA request */ 02303 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); 02304 02305 /* Abort the SD DMA Tx channel */ 02306 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)) 02307 { 02308 if(HAL_DMA_Abort(hsd->hdmatx) != HAL_OK) 02309 { 02310 hsd->ErrorCode |= HAL_SD_ERROR_DMA; 02311 } 02312 } 02313 /* Abort the SD DMA Rx channel */ 02314 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U)) 02315 { 02316 if(HAL_DMA_Abort(hsd->hdmarx) != HAL_OK) 02317 { 02318 hsd->ErrorCode |= HAL_SD_ERROR_DMA; 02319 } 02320 } 02321 else 02322 { 02323 /* Nothing to do */ 02324 } 02325 } 02326 02327 hsd->State = HAL_SD_STATE_READY; 02328 02329 /* Initialize the SD operation */ 02330 hsd->Context = SD_CONTEXT_NONE; 02331 02332 CardState = HAL_SD_GetCardState(hsd); 02333 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02334 { 02335 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance); 02336 } 02337 if(hsd->ErrorCode != HAL_SD_ERROR_NONE) 02338 { 02339 return HAL_ERROR; 02340 } 02341 return HAL_OK; 02342 } 02343 02344 /** 02345 * @brief Abort the current transfer and disable the SD (IT mode). 02346 * @param hsd: pointer to a SD_HandleTypeDef structure that contains 02347 * the configuration information for SD module. 02348 * @retval HAL status 02349 */ 02350 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd) 02351 { 02352 HAL_SD_CardStateTypeDef CardState; 02353 uint32_t context = hsd->Context; 02354 02355 /* Disable All interrupts */ 02356 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 02357 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); 02358 02359 CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN); 02360 02361 if ((context & SD_CONTEXT_DMA) != 0U) 02362 { 02363 /* Disable the SD DMA request */ 02364 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); 02365 02366 /* Abort the SD DMA Tx channel */ 02367 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)) 02368 { 02369 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort; 02370 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK) 02371 { 02372 hsd->hdmatx = NULL; 02373 } 02374 } 02375 /* Abort the SD DMA Rx channel */ 02376 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U)) 02377 { 02378 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort; 02379 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK) 02380 { 02381 hsd->hdmarx = NULL; 02382 } 02383 } 02384 else 02385 { 02386 /* Nothing to do */ 02387 } 02388 } 02389 /* No transfer ongoing on both DMA channels*/ 02390 else 02391 { 02392 /* Clear All flags */ 02393 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS); 02394 02395 CardState = HAL_SD_GetCardState(hsd); 02396 hsd->State = HAL_SD_STATE_READY; 02397 hsd->Context = SD_CONTEXT_NONE; 02398 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02399 { 02400 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance); 02401 } 02402 if(hsd->ErrorCode != HAL_SD_ERROR_NONE) 02403 { 02404 return HAL_ERROR; 02405 } 02406 else 02407 { 02408 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U) 02409 hsd->AbortCpltCallback(hsd); 02410 #else 02411 HAL_SD_AbortCallback(hsd); 02412 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */ 02413 } 02414 } 02415 02416 return HAL_OK; 02417 } 02418 02419 /** 02420 * @} 02421 */ 02422 02423 /** 02424 * @} 02425 */ 02426 02427 /* Private function ----------------------------------------------------------*/ 02428 /** @addtogroup SD_Private_Functions 02429 * @{ 02430 */ 02431 02432 /** 02433 * @brief DMA SD transmit process complete callback 02434 * @param hdma: DMA handle 02435 * @retval None 02436 */ 02437 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma) 02438 { 02439 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); 02440 02441 /* Enable DATAEND Interrupt */ 02442 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND)); 02443 } 02444 02445 /** 02446 * @brief DMA SD receive process complete callback 02447 * @param hdma: DMA handle 02448 * @retval None 02449 */ 02450 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma) 02451 { 02452 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); 02453 uint32_t errorstate; 02454 02455 /* Send stop command in multiblock write */ 02456 if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA)) 02457 { 02458 errorstate = SDMMC_CmdStopTransfer(hsd->Instance); 02459 if(errorstate != HAL_SD_ERROR_NONE) 02460 { 02461 hsd->ErrorCode |= errorstate; 02462 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1) 02463 hsd->ErrorCallback(hsd); 02464 #else 02465 HAL_SD_ErrorCallback(hsd); 02466 #endif 02467 } 02468 } 02469 02470 /* Disable the DMA transfer for transmit request by setting the DMAEN bit 02471 in the SD DCTRL register */ 02472 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN); 02473 02474 /* Clear all the static flags */ 02475 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS); 02476 02477 hsd->State = HAL_SD_STATE_READY; 02478 hsd->Context = SD_CONTEXT_NONE; 02479 02480 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1) 02481 hsd->RxCpltCallback(hsd); 02482 #else 02483 HAL_SD_RxCpltCallback(hsd); 02484 #endif 02485 } 02486 02487 /** 02488 * @brief DMA SD communication error callback 02489 * @param hdma: DMA handle 02490 * @retval None 02491 */ 02492 static void SD_DMAError(DMA_HandleTypeDef *hdma) 02493 { 02494 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); 02495 HAL_SD_CardStateTypeDef CardState; 02496 uint32_t RxErrorCode, TxErrorCode; 02497 02498 RxErrorCode = hsd->hdmarx->ErrorCode; 02499 TxErrorCode = hsd->hdmatx->ErrorCode; 02500 if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE)) 02501 { 02502 /* Clear All flags */ 02503 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS); 02504 02505 /* Disable All interrupts */ 02506 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\ 02507 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR); 02508 02509 hsd->ErrorCode |= HAL_SD_ERROR_DMA; 02510 CardState = HAL_SD_GetCardState(hsd); 02511 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02512 { 02513 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance); 02514 } 02515 02516 hsd->State= HAL_SD_STATE_READY; 02517 hsd->Context = SD_CONTEXT_NONE; 02518 } 02519 02520 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1) 02521 hsd->ErrorCallback(hsd); 02522 #else 02523 HAL_SD_ErrorCallback(hsd); 02524 #endif 02525 } 02526 02527 /** 02528 * @brief DMA SD Tx Abort callback 02529 * @param hdma: DMA handle 02530 * @retval None 02531 */ 02532 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma) 02533 { 02534 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); 02535 HAL_SD_CardStateTypeDef CardState; 02536 02537 /* Clear All flags */ 02538 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS); 02539 02540 CardState = HAL_SD_GetCardState(hsd); 02541 hsd->State = HAL_SD_STATE_READY; 02542 hsd->Context = SD_CONTEXT_NONE; 02543 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02544 { 02545 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance); 02546 } 02547 02548 if(hsd->ErrorCode == HAL_SD_ERROR_NONE) 02549 { 02550 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1) 02551 hsd->AbortCpltCallback(hsd); 02552 #else 02553 HAL_SD_AbortCallback(hsd); 02554 #endif 02555 } 02556 else 02557 { 02558 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1) 02559 hsd->ErrorCallback(hsd); 02560 #else 02561 HAL_SD_ErrorCallback(hsd); 02562 #endif 02563 } 02564 } 02565 02566 /** 02567 * @brief DMA SD Rx Abort callback 02568 * @param hdma: DMA handle 02569 * @retval None 02570 */ 02571 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma) 02572 { 02573 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent); 02574 HAL_SD_CardStateTypeDef CardState; 02575 02576 /* Clear All flags */ 02577 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS); 02578 02579 CardState = HAL_SD_GetCardState(hsd); 02580 hsd->State = HAL_SD_STATE_READY; 02581 hsd->Context = SD_CONTEXT_NONE; 02582 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING)) 02583 { 02584 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance); 02585 } 02586 02587 if(hsd->ErrorCode == HAL_SD_ERROR_NONE) 02588 { 02589 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1) 02590 hsd->AbortCpltCallback(hsd); 02591 #else 02592 HAL_SD_AbortCallback(hsd); 02593 #endif 02594 } 02595 else 02596 { 02597 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1) 02598 hsd->ErrorCallback(hsd); 02599 #else 02600 HAL_SD_ErrorCallback(hsd); 02601 #endif 02602 } 02603 } 02604 02605 /** 02606 * @brief Initializes the sd card. 02607 * @param hsd: Pointer to SD handle 02608 * @retval SD Card error state 02609 */ 02610 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd) 02611 { 02612 HAL_SD_CardCSDTypeDef CSD; 02613 uint32_t errorstate; 02614 uint16_t sd_rca = 1U; 02615 02616 /* Check the power State */ 02617 if(SDIO_GetPowerState(hsd->Instance) == 0U) 02618 { 02619 /* Power off */ 02620 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 02621 } 02622 02623 if(hsd->SdCard.CardType != CARD_SECURED) 02624 { 02625 /* Send CMD2 ALL_SEND_CID */ 02626 errorstate = SDMMC_CmdSendCID(hsd->Instance); 02627 if(errorstate != HAL_SD_ERROR_NONE) 02628 { 02629 return errorstate; 02630 } 02631 else 02632 { 02633 /* Get Card identification number data */ 02634 hsd->CID[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); 02635 hsd->CID[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2); 02636 hsd->CID[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3); 02637 hsd->CID[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4); 02638 } 02639 } 02640 02641 if(hsd->SdCard.CardType != CARD_SECURED) 02642 { 02643 /* Send CMD3 SET_REL_ADDR with argument 0 */ 02644 /* SD Card publishes its RCA. */ 02645 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca); 02646 if(errorstate != HAL_SD_ERROR_NONE) 02647 { 02648 return errorstate; 02649 } 02650 } 02651 if(hsd->SdCard.CardType != CARD_SECURED) 02652 { 02653 /* Get the SD card RCA */ 02654 hsd->SdCard.RelCardAdd = sd_rca; 02655 02656 /* Send CMD9 SEND_CSD with argument as card's RCA */ 02657 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 02658 if(errorstate != HAL_SD_ERROR_NONE) 02659 { 02660 return errorstate; 02661 } 02662 else 02663 { 02664 /* Get Card Specific Data */ 02665 hsd->CSD[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); 02666 hsd->CSD[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2); 02667 hsd->CSD[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3); 02668 hsd->CSD[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4); 02669 } 02670 } 02671 02672 /* Get the Card Class */ 02673 hsd->SdCard.Class = (SDIO_GetResponse(hsd->Instance, SDIO_RESP2) >> 20U); 02674 02675 /* Get CSD parameters */ 02676 if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK) 02677 { 02678 return HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02679 } 02680 02681 /* Select the Card */ 02682 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U)); 02683 if(errorstate != HAL_SD_ERROR_NONE) 02684 { 02685 return errorstate; 02686 } 02687 02688 /* Configure SDIO peripheral interface */ 02689 (void)SDIO_Init(hsd->Instance, hsd->Init); 02690 02691 /* All cards are initialized */ 02692 return HAL_SD_ERROR_NONE; 02693 } 02694 02695 /** 02696 * @brief Enquires cards about their operating voltage and configures clock 02697 * controls and stores SD information that will be needed in future 02698 * in the SD handle. 02699 * @param hsd: Pointer to SD handle 02700 * @retval error state 02701 */ 02702 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd) 02703 { 02704 __IO uint32_t count = 0U; 02705 uint32_t response = 0U, validvoltage = 0U; 02706 uint32_t errorstate; 02707 02708 /* CMD0: GO_IDLE_STATE */ 02709 errorstate = SDMMC_CmdGoIdleState(hsd->Instance); 02710 if(errorstate != HAL_SD_ERROR_NONE) 02711 { 02712 return errorstate; 02713 } 02714 02715 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */ 02716 errorstate = SDMMC_CmdOperCond(hsd->Instance); 02717 if(errorstate != HAL_SD_ERROR_NONE) 02718 { 02719 hsd->SdCard.CardVersion = CARD_V1_X; 02720 /* CMD0: GO_IDLE_STATE */ 02721 errorstate = SDMMC_CmdGoIdleState(hsd->Instance); 02722 if(errorstate != HAL_SD_ERROR_NONE) 02723 { 02724 return errorstate; 02725 } 02726 02727 } 02728 else 02729 { 02730 hsd->SdCard.CardVersion = CARD_V2_X; 02731 } 02732 02733 if( hsd->SdCard.CardVersion == CARD_V2_X) 02734 { 02735 /* SEND CMD55 APP_CMD with RCA as 0 */ 02736 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0); 02737 if(errorstate != HAL_SD_ERROR_NONE) 02738 { 02739 return HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02740 } 02741 } 02742 /* SD CARD */ 02743 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */ 02744 while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U)) 02745 { 02746 /* SEND CMD55 APP_CMD with RCA as 0 */ 02747 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0); 02748 if(errorstate != HAL_SD_ERROR_NONE) 02749 { 02750 return errorstate; 02751 } 02752 02753 /* Send CMD41 */ 02754 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY); 02755 if(errorstate != HAL_SD_ERROR_NONE) 02756 { 02757 return HAL_SD_ERROR_UNSUPPORTED_FEATURE; 02758 } 02759 02760 /* Get command response */ 02761 response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); 02762 02763 /* Get operating voltage*/ 02764 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U); 02765 02766 count++; 02767 } 02768 02769 if(count >= SDMMC_MAX_VOLT_TRIAL) 02770 { 02771 return HAL_SD_ERROR_INVALID_VOLTRANGE; 02772 } 02773 02774 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */ 02775 { 02776 hsd->SdCard.CardType = CARD_SDHC_SDXC; 02777 } 02778 else 02779 { 02780 hsd->SdCard.CardType = CARD_SDSC; 02781 } 02782 02783 02784 return HAL_SD_ERROR_NONE; 02785 } 02786 02787 /** 02788 * @brief Turns the SDIO output signals off. 02789 * @param hsd: Pointer to SD handle 02790 * @retval None 02791 */ 02792 static void SD_PowerOFF(SD_HandleTypeDef *hsd) 02793 { 02794 /* Set Power State to OFF */ 02795 (void)SDIO_PowerState_OFF(hsd->Instance); 02796 } 02797 02798 /** 02799 * @brief Send Status info command. 02800 * @param hsd: pointer to SD handle 02801 * @param pSDstatus: Pointer to the buffer that will contain the SD card status 02802 * SD Status register) 02803 * @retval error state 02804 */ 02805 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus) 02806 { 02807 SDIO_DataInitTypeDef config; 02808 uint32_t errorstate; 02809 uint32_t tickstart = HAL_GetTick(); 02810 uint32_t count; 02811 uint32_t *pData = pSDstatus; 02812 02813 /* Check SD response */ 02814 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 02815 { 02816 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 02817 } 02818 02819 /* Set block size for card if it is not equal to current block size for card */ 02820 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U); 02821 if(errorstate != HAL_SD_ERROR_NONE) 02822 { 02823 hsd->ErrorCode |= HAL_SD_ERROR_NONE; 02824 return errorstate; 02825 } 02826 02827 /* Send CMD55 */ 02828 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 02829 if(errorstate != HAL_SD_ERROR_NONE) 02830 { 02831 hsd->ErrorCode |= HAL_SD_ERROR_NONE; 02832 return errorstate; 02833 } 02834 02835 /* Configure the SD DPSM (Data Path State Machine) */ 02836 config.DataTimeOut = SDMMC_DATATIMEOUT; 02837 config.DataLength = 64U; 02838 config.DataBlockSize = SDIO_DATABLOCK_SIZE_64B; 02839 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; 02840 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 02841 config.DPSM = SDIO_DPSM_ENABLE; 02842 (void)SDIO_ConfigData(hsd->Instance, &config); 02843 02844 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */ 02845 errorstate = SDMMC_CmdStatusRegister(hsd->Instance); 02846 if(errorstate != HAL_SD_ERROR_NONE) 02847 { 02848 hsd->ErrorCode |= HAL_SD_ERROR_NONE; 02849 return errorstate; 02850 } 02851 02852 /* Get status data */ 02853 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND)) 02854 { 02855 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF)) 02856 { 02857 for(count = 0U; count < 8U; count++) 02858 { 02859 *pData = SDIO_ReadFIFO(hsd->Instance); 02860 pData++; 02861 } 02862 } 02863 02864 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 02865 { 02866 return HAL_SD_ERROR_TIMEOUT; 02867 } 02868 } 02869 02870 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) 02871 { 02872 return HAL_SD_ERROR_DATA_TIMEOUT; 02873 } 02874 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) 02875 { 02876 return HAL_SD_ERROR_DATA_CRC_FAIL; 02877 } 02878 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) 02879 { 02880 return HAL_SD_ERROR_RX_OVERRUN; 02881 } 02882 else 02883 { 02884 /* Nothing to do */ 02885 } 02886 02887 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))) 02888 { 02889 *pData = SDIO_ReadFIFO(hsd->Instance); 02890 pData++; 02891 02892 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 02893 { 02894 return HAL_SD_ERROR_TIMEOUT; 02895 } 02896 } 02897 02898 /* Clear all the static status flags*/ 02899 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS); 02900 02901 return HAL_SD_ERROR_NONE; 02902 } 02903 02904 /** 02905 * @brief Returns the current card's status. 02906 * @param hsd: Pointer to SD handle 02907 * @param pCardStatus: pointer to the buffer that will contain the SD card 02908 * status (Card Status register) 02909 * @retval error state 02910 */ 02911 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus) 02912 { 02913 uint32_t errorstate; 02914 02915 if(pCardStatus == NULL) 02916 { 02917 return HAL_SD_ERROR_PARAM; 02918 } 02919 02920 /* Send Status command */ 02921 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 02922 if(errorstate != HAL_SD_ERROR_NONE) 02923 { 02924 return errorstate; 02925 } 02926 02927 /* Get SD card status */ 02928 *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1); 02929 02930 return HAL_SD_ERROR_NONE; 02931 } 02932 02933 /** 02934 * @brief Enables the SDIO wide bus mode. 02935 * @param hsd: pointer to SD handle 02936 * @retval error state 02937 */ 02938 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd) 02939 { 02940 uint32_t scr[2U] = {0U, 0U}; 02941 uint32_t errorstate; 02942 02943 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 02944 { 02945 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 02946 } 02947 02948 /* Get SCR Register */ 02949 errorstate = SD_FindSCR(hsd, scr); 02950 if(errorstate != HAL_SD_ERROR_NONE) 02951 { 02952 return errorstate; 02953 } 02954 02955 /* If requested card supports wide bus operation */ 02956 if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO) 02957 { 02958 /* Send CMD55 APP_CMD with argument as card's RCA.*/ 02959 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 02960 if(errorstate != HAL_SD_ERROR_NONE) 02961 { 02962 return errorstate; 02963 } 02964 02965 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */ 02966 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U); 02967 if(errorstate != HAL_SD_ERROR_NONE) 02968 { 02969 return errorstate; 02970 } 02971 02972 return HAL_SD_ERROR_NONE; 02973 } 02974 else 02975 { 02976 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 02977 } 02978 } 02979 02980 /** 02981 * @brief Disables the SDIO wide bus mode. 02982 * @param hsd: Pointer to SD handle 02983 * @retval error state 02984 */ 02985 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd) 02986 { 02987 uint32_t scr[2U] = {0U, 0U}; 02988 uint32_t errorstate; 02989 02990 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED) 02991 { 02992 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED; 02993 } 02994 02995 /* Get SCR Register */ 02996 errorstate = SD_FindSCR(hsd, scr); 02997 if(errorstate != HAL_SD_ERROR_NONE) 02998 { 02999 return errorstate; 03000 } 03001 03002 /* If requested card supports 1 bit mode operation */ 03003 if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO) 03004 { 03005 /* Send CMD55 APP_CMD with argument as card's RCA */ 03006 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U)); 03007 if(errorstate != HAL_SD_ERROR_NONE) 03008 { 03009 return errorstate; 03010 } 03011 03012 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */ 03013 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U); 03014 if(errorstate != HAL_SD_ERROR_NONE) 03015 { 03016 return errorstate; 03017 } 03018 03019 return HAL_SD_ERROR_NONE; 03020 } 03021 else 03022 { 03023 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE; 03024 } 03025 } 03026 03027 03028 /** 03029 * @brief Finds the SD card SCR register value. 03030 * @param hsd: Pointer to SD handle 03031 * @param pSCR: pointer to the buffer that will contain the SCR value 03032 * @retval error state 03033 */ 03034 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR) 03035 { 03036 SDIO_DataInitTypeDef config; 03037 uint32_t errorstate; 03038 uint32_t tickstart = HAL_GetTick(); 03039 uint32_t index = 0U; 03040 uint32_t tempscr[2U] = {0U, 0U}; 03041 uint32_t *scr = pSCR; 03042 03043 /* Set Block Size To 8 Bytes */ 03044 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U); 03045 if(errorstate != HAL_SD_ERROR_NONE) 03046 { 03047 return errorstate; 03048 } 03049 03050 /* Send CMD55 APP_CMD with argument as card's RCA */ 03051 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U)); 03052 if(errorstate != HAL_SD_ERROR_NONE) 03053 { 03054 return errorstate; 03055 } 03056 03057 config.DataTimeOut = SDMMC_DATATIMEOUT; 03058 config.DataLength = 8U; 03059 config.DataBlockSize = SDIO_DATABLOCK_SIZE_8B; 03060 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO; 03061 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK; 03062 config.DPSM = SDIO_DPSM_ENABLE; 03063 (void)SDIO_ConfigData(hsd->Instance, &config); 03064 03065 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */ 03066 errorstate = SDMMC_CmdSendSCR(hsd->Instance); 03067 if(errorstate != HAL_SD_ERROR_NONE) 03068 { 03069 return errorstate; 03070 } 03071 03072 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT)) 03073 { 03074 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) 03075 { 03076 *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance); 03077 index++; 03078 } 03079 else if(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXACT)) 03080 { 03081 break; 03082 } 03083 03084 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT) 03085 { 03086 return HAL_SD_ERROR_TIMEOUT; 03087 } 03088 } 03089 03090 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT)) 03091 { 03092 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT); 03093 03094 return HAL_SD_ERROR_DATA_TIMEOUT; 03095 } 03096 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL)) 03097 { 03098 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL); 03099 03100 return HAL_SD_ERROR_DATA_CRC_FAIL; 03101 } 03102 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR)) 03103 { 03104 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR); 03105 03106 return HAL_SD_ERROR_RX_OVERRUN; 03107 } 03108 else 03109 { 03110 /* No error flag set */ 03111 /* Clear all the static flags */ 03112 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS); 03113 03114 *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\ 03115 ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24)); 03116 scr++; 03117 *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\ 03118 ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24)); 03119 03120 } 03121 03122 return HAL_SD_ERROR_NONE; 03123 } 03124 03125 /** 03126 * @brief Wrap up reading in non-blocking mode. 03127 * @param hsd: pointer to a SD_HandleTypeDef structure that contains 03128 * the configuration information. 03129 * @retval None 03130 */ 03131 static void SD_Read_IT(SD_HandleTypeDef *hsd) 03132 { 03133 uint32_t count, data, dataremaining; 03134 uint8_t* tmp; 03135 03136 tmp = hsd->pRxBuffPtr; 03137 dataremaining = hsd->RxXferSize; 03138 03139 if (dataremaining > 0U) 03140 { 03141 /* Read data from SDIO Rx FIFO */ 03142 for(count = 0U; count < 8U; count++) 03143 { 03144 data = SDIO_ReadFIFO(hsd->Instance); 03145 *tmp = (uint8_t)(data & 0xFFU); 03146 tmp++; 03147 dataremaining--; 03148 *tmp = (uint8_t)((data >> 8U) & 0xFFU); 03149 tmp++; 03150 dataremaining--; 03151 *tmp = (uint8_t)((data >> 16U) & 0xFFU); 03152 tmp++; 03153 dataremaining--; 03154 *tmp = (uint8_t)((data >> 24U) & 0xFFU); 03155 tmp++; 03156 dataremaining--; 03157 } 03158 03159 hsd->pRxBuffPtr = tmp; 03160 hsd->RxXferSize = dataremaining; 03161 } 03162 } 03163 03164 /** 03165 * @brief Wrap up writing in non-blocking mode. 03166 * @param hsd: pointer to a SD_HandleTypeDef structure that contains 03167 * the configuration information. 03168 * @retval None 03169 */ 03170 static void SD_Write_IT(SD_HandleTypeDef *hsd) 03171 { 03172 uint32_t count, data, dataremaining; 03173 uint8_t* tmp; 03174 03175 tmp = hsd->pTxBuffPtr; 03176 dataremaining = hsd->TxXferSize; 03177 03178 if (dataremaining > 0U) 03179 { 03180 /* Write data to SDIO Tx FIFO */ 03181 for(count = 0U; count < 8U; count++) 03182 { 03183 data = (uint32_t)(*tmp); 03184 tmp++; 03185 dataremaining--; 03186 data |= ((uint32_t)(*tmp) << 8U); 03187 tmp++; 03188 dataremaining--; 03189 data |= ((uint32_t)(*tmp) << 16U); 03190 tmp++; 03191 dataremaining--; 03192 data |= ((uint32_t)(*tmp) << 24U); 03193 tmp++; 03194 dataremaining--; 03195 (void)SDIO_WriteFIFO(hsd->Instance, &data); 03196 } 03197 03198 hsd->pTxBuffPtr = tmp; 03199 hsd->TxXferSize = dataremaining; 03200 } 03201 } 03202 03203 /** 03204 * @} 03205 */ 03206 03207 #endif /* HAL_SD_MODULE_ENABLED */ 03208 03209 /** 03210 * @} 03211 */ 03212 03213 /** 03214 * @} 03215 */ 03216 03217 #endif /* SDIO */ 03218 03219 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/