STM32F479xx HAL User Manual
stm32f4xx_hal_sd.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_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 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 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 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 HAL_SD_Init
00227     and  HAL_SD_DeInit only when these callbacks are null (not registered beforehand).
00228     If not, MspInit or MspDeInit are not null, the HAL_SD_Init and 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 HAL_SD_RegisterCallback before calling HAL_SD_DeInit
00237     or 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>&copy; Copyright (c) 2017 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 "stm32f4xx_hal.h"
00260 
00261 #if defined(SDIO)
00262 
00263 /** @addtogroup STM32F4xx_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 #if defined(SDIO_STA_STBITERR)
00639     while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
00640 #else /* SDIO_STA_STBITERR not defined */
00641     while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
00642 #endif /* SDIO_STA_STBITERR */
00643     {
00644       if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) && (dataremaining > 0U))
00645       {
00646         /* Read data from SDIO Rx FIFO */
00647         for(count = 0U; count < 8U; count++)
00648         {
00649           data = SDIO_ReadFIFO(hsd->Instance);
00650           *tempbuff = (uint8_t)(data & 0xFFU);
00651           tempbuff++;
00652           dataremaining--;
00653           *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
00654           tempbuff++;
00655           dataremaining--;
00656           *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
00657           tempbuff++;
00658           dataremaining--;
00659           *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
00660           tempbuff++;
00661           dataremaining--;
00662         }
00663       }
00664 
00665       if(((HAL_GetTick()-tickstart) >=  Timeout) || (Timeout == 0U))
00666       {
00667         /* Clear all the static flags */
00668         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00669         hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
00670         hsd->State= HAL_SD_STATE_READY;
00671         hsd->Context = SD_CONTEXT_NONE;
00672         return HAL_TIMEOUT;
00673       }
00674     }
00675     
00676     /* Send stop transmission command in case of multiblock read */
00677     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
00678     {
00679       if(hsd->SdCard.CardType != CARD_SECURED)
00680       {
00681         /* Send stop transmission command */
00682         errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
00683         if(errorstate != HAL_SD_ERROR_NONE)
00684         {
00685           /* Clear all the static flags */
00686           __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00687           hsd->ErrorCode |= errorstate;
00688           hsd->State = HAL_SD_STATE_READY;
00689           hsd->Context = SD_CONTEXT_NONE;
00690           return HAL_ERROR;
00691         }
00692       }
00693     }
00694 
00695     /* Get error state */
00696     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
00697     {
00698       /* Clear all the static flags */
00699       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00700       hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
00701       hsd->State = HAL_SD_STATE_READY;
00702       hsd->Context = SD_CONTEXT_NONE;
00703       return HAL_ERROR;
00704     }
00705     else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
00706     {
00707       /* Clear all the static flags */
00708       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00709       hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
00710       hsd->State = HAL_SD_STATE_READY;
00711       hsd->Context = SD_CONTEXT_NONE;
00712       return HAL_ERROR;
00713     }
00714     else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
00715     {
00716       /* Clear all the static flags */
00717       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00718       hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
00719       hsd->State = HAL_SD_STATE_READY;
00720       hsd->Context = SD_CONTEXT_NONE;
00721       return HAL_ERROR;
00722     }
00723     else
00724     {
00725       /* Nothing to do */
00726     }
00727 
00728     /* Empty FIFO if there is still any data */
00729     while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (dataremaining > 0U))
00730     {
00731       data = SDIO_ReadFIFO(hsd->Instance);
00732       *tempbuff = (uint8_t)(data & 0xFFU);
00733       tempbuff++;
00734       dataremaining--;
00735       *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
00736       tempbuff++;
00737       dataremaining--;
00738       *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
00739       tempbuff++;
00740       dataremaining--;
00741       *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
00742       tempbuff++;
00743       dataremaining--;
00744 
00745       if(((HAL_GetTick()-tickstart) >=  Timeout) || (Timeout == 0U))
00746       {
00747         /* Clear all the static flags */
00748         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00749         hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
00750         hsd->State= HAL_SD_STATE_READY;
00751         hsd->Context = SD_CONTEXT_NONE;
00752         return HAL_ERROR;
00753       }
00754     }
00755 
00756     /* Clear all the static flags */
00757     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
00758 
00759     hsd->State = HAL_SD_STATE_READY;
00760 
00761     return HAL_OK;
00762   }
00763   else
00764   {
00765     hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
00766     return HAL_ERROR;
00767   }
00768 }
00769 
00770 /**
00771   * @brief  Allows to write block(s) to a specified address in a card. The Data
00772   *         transfer is managed by polling mode.
00773   * @note   This API should be followed by a check on the card state through
00774   *         HAL_SD_GetCardState().
00775   * @param  hsd: Pointer to SD handle
00776   * @param  pData: pointer to the buffer that will contain the data to transmit
00777   * @param  BlockAdd: Block Address where data will be written
00778   * @param  NumberOfBlocks: Number of SD blocks to write
00779   * @param  Timeout: Specify timeout value
00780   * @retval HAL status
00781   */
00782 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
00783 {
00784   SDIO_DataInitTypeDef config;
00785   uint32_t errorstate;
00786   uint32_t tickstart = HAL_GetTick();
00787   uint32_t count, data, dataremaining;
00788   uint32_t add = BlockAdd;
00789   uint8_t *tempbuff = pData;
00790 
00791   if(NULL == pData)
00792   {
00793     hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
00794     return HAL_ERROR;
00795   }
00796 
00797   if(hsd->State == HAL_SD_STATE_READY)
00798   {
00799     hsd->ErrorCode = HAL_SD_ERROR_NONE;
00800 
00801     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
00802     {
00803       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
00804       return HAL_ERROR;
00805     }
00806 
00807     hsd->State = HAL_SD_STATE_BUSY;
00808 
00809     /* Initialize data control register */
00810     hsd->Instance->DCTRL = 0U;
00811 
00812     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
00813     {
00814       add *= 512U;
00815     }
00816 
00817     /* Configure the SD DPSM (Data Path State Machine) */
00818     config.DataTimeOut   = SDMMC_DATATIMEOUT;
00819     config.DataLength    = NumberOfBlocks * BLOCKSIZE;
00820     config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
00821     config.TransferDir   = SDIO_TRANSFER_DIR_TO_CARD;
00822     config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
00823     config.DPSM          = SDIO_DPSM_ENABLE;
00824     (void)SDIO_ConfigData(hsd->Instance, &config);
00825 
00826     /* Write Blocks in Polling mode */
00827     if(NumberOfBlocks > 1U)
00828     {
00829       hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
00830 
00831       /* Write Multi Block command */
00832       errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
00833     }
00834     else
00835     {
00836       hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
00837 
00838       /* Write Single Block command */
00839       errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
00840     }
00841     if(errorstate != HAL_SD_ERROR_NONE)
00842     {
00843       /* Clear all the static flags */
00844       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00845       hsd->ErrorCode |= errorstate;
00846       hsd->State = HAL_SD_STATE_READY;
00847       hsd->Context = SD_CONTEXT_NONE;
00848       return HAL_ERROR;
00849     }
00850 
00851     /* Write block(s) in polling mode */
00852     dataremaining = config.DataLength;
00853 #if defined(SDIO_STA_STBITERR)
00854     while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
00855 #else /* SDIO_STA_STBITERR not defined */
00856     while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
00857 #endif /* SDIO_STA_STBITERR */
00858     {
00859       if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) && (dataremaining > 0U))
00860       {
00861         /* Write data to SDIO Tx FIFO */
00862         for(count = 0U; count < 8U; count++)
00863         {
00864           data = (uint32_t)(*tempbuff);
00865           tempbuff++;
00866           dataremaining--;
00867           data |= ((uint32_t)(*tempbuff) << 8U);
00868           tempbuff++;
00869           dataremaining--;
00870           data |= ((uint32_t)(*tempbuff) << 16U);
00871           tempbuff++;
00872           dataremaining--;
00873           data |= ((uint32_t)(*tempbuff) << 24U);
00874           tempbuff++;
00875           dataremaining--;
00876           (void)SDIO_WriteFIFO(hsd->Instance, &data);
00877         }
00878       }
00879 
00880       if(((HAL_GetTick()-tickstart) >=  Timeout) || (Timeout == 0U))
00881       {
00882         /* Clear all the static flags */
00883         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00884         hsd->ErrorCode |= errorstate;
00885         hsd->State = HAL_SD_STATE_READY;
00886         hsd->Context = SD_CONTEXT_NONE;
00887         return HAL_TIMEOUT;
00888       }
00889     }
00890 
00891     /* Send stop transmission command in case of multiblock write */
00892     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
00893     {
00894       if(hsd->SdCard.CardType != CARD_SECURED)
00895       {
00896         /* Send stop transmission command */
00897         errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
00898         if(errorstate != HAL_SD_ERROR_NONE)
00899         {
00900           /* Clear all the static flags */
00901           __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00902           hsd->ErrorCode |= errorstate;
00903           hsd->State = HAL_SD_STATE_READY;
00904           hsd->Context = SD_CONTEXT_NONE;
00905           return HAL_ERROR;
00906         }
00907       }
00908     }
00909 
00910     /* Get error state */
00911     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
00912     {
00913       /* Clear all the static flags */
00914       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00915       hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
00916       hsd->State = HAL_SD_STATE_READY;
00917       hsd->Context = SD_CONTEXT_NONE;
00918       return HAL_ERROR;
00919     }
00920     else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
00921     {
00922       /* Clear all the static flags */
00923       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00924       hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
00925       hsd->State = HAL_SD_STATE_READY;
00926       hsd->Context = SD_CONTEXT_NONE;
00927       return HAL_ERROR;
00928     }
00929     else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
00930     {
00931       /* Clear all the static flags */
00932       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
00933       hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
00934       hsd->State = HAL_SD_STATE_READY;
00935       hsd->Context = SD_CONTEXT_NONE;
00936       return HAL_ERROR;
00937     }
00938     else
00939     {
00940       /* Nothing to do */
00941     }
00942 
00943     /* Clear all the static flags */
00944     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
00945 
00946     hsd->State = HAL_SD_STATE_READY;
00947 
00948     return HAL_OK;
00949   }
00950   else
00951   {
00952     hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
00953     return HAL_ERROR;
00954   }
00955 }
00956 
00957 /**
00958   * @brief  Reads block(s) from a specified address in a card. The Data transfer
00959   *         is managed in interrupt mode.
00960   * @note   This API should be followed by a check on the card state through
00961   *         HAL_SD_GetCardState().
00962   * @note   You could also check the IT transfer process through the SD Rx
00963   *         interrupt event.
00964   * @param  hsd: Pointer to SD handle
00965   * @param  pData: Pointer to the buffer that will contain the received data
00966   * @param  BlockAdd: Block Address from where data is to be read
00967   * @param  NumberOfBlocks: Number of blocks to read.
00968   * @retval HAL status
00969   */
00970 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
00971 {
00972   SDIO_DataInitTypeDef config;
00973   uint32_t errorstate;
00974   uint32_t add = BlockAdd;
00975 
00976   if(NULL == pData)
00977   {
00978     hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
00979     return HAL_ERROR;
00980   }
00981 
00982   if(hsd->State == HAL_SD_STATE_READY)
00983   {
00984     hsd->ErrorCode = HAL_SD_ERROR_NONE;
00985 
00986     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
00987     {
00988       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
00989       return HAL_ERROR;
00990     }
00991 
00992     hsd->State = HAL_SD_STATE_BUSY;
00993 
00994     /* Initialize data control register */
00995     hsd->Instance->DCTRL = 0U;
00996 
00997     hsd->pRxBuffPtr = pData;
00998     hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
00999 
01000 #if defined(SDIO_STA_STBITERR)
01001     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF | SDIO_IT_STBITERR));
01002 #else /* SDIO_STA_STBITERR not defined */
01003     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF));
01004 #endif /* SDIO_STA_STBITERR */
01005 
01006     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
01007     {
01008       add *= 512U;
01009     }
01010 
01011     /* Configure the SD DPSM (Data Path State Machine) */
01012     config.DataTimeOut   = SDMMC_DATATIMEOUT;
01013     config.DataLength    = BLOCKSIZE * NumberOfBlocks;
01014     config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
01015     config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
01016     config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
01017     config.DPSM          = SDIO_DPSM_ENABLE;
01018     (void)SDIO_ConfigData(hsd->Instance, &config);
01019 
01020     /* Read Blocks in IT mode */
01021     if(NumberOfBlocks > 1U)
01022     {
01023       hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
01024 
01025       /* Read Multi Block command */
01026       errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
01027     }
01028     else
01029     {
01030       hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
01031 
01032       /* Read Single Block command */
01033       errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
01034     }
01035     if(errorstate != HAL_SD_ERROR_NONE)
01036     {
01037       /* Clear all the static flags */
01038       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
01039       hsd->ErrorCode |= errorstate;
01040       hsd->State = HAL_SD_STATE_READY;
01041       hsd->Context = SD_CONTEXT_NONE;
01042       return HAL_ERROR;
01043     }
01044 
01045     return HAL_OK;
01046   }
01047   else
01048   {
01049     return HAL_BUSY;
01050   }
01051 }
01052 
01053 /**
01054   * @brief  Writes block(s) to a specified address in a card. The Data transfer
01055   *         is managed in interrupt mode.
01056   * @note   This API should be followed by a check on the card state through
01057   *         HAL_SD_GetCardState().
01058   * @note   You could also check the IT transfer process through the SD Tx
01059   *         interrupt event.
01060   * @param  hsd: Pointer to SD handle
01061   * @param  pData: Pointer to the buffer that will contain the data to transmit
01062   * @param  BlockAdd: Block Address where data will be written
01063   * @param  NumberOfBlocks: Number of blocks to write
01064   * @retval HAL status
01065   */
01066 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
01067 {
01068   SDIO_DataInitTypeDef config;
01069   uint32_t errorstate;
01070   uint32_t add = BlockAdd;
01071 
01072   if(NULL == pData)
01073   {
01074     hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
01075     return HAL_ERROR;
01076   }
01077 
01078   if(hsd->State == HAL_SD_STATE_READY)
01079   {
01080     hsd->ErrorCode = HAL_SD_ERROR_NONE;
01081 
01082     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
01083     {
01084       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
01085       return HAL_ERROR;
01086     }
01087 
01088     hsd->State = HAL_SD_STATE_BUSY;
01089 
01090     /* Initialize data control register */
01091     hsd->Instance->DCTRL = 0U;
01092 
01093     hsd->pTxBuffPtr = pData;
01094     hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
01095 
01096     /* Enable transfer interrupts */
01097 #if defined(SDIO_STA_STBITERR)
01098     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_STBITERR));
01099 #else /* SDIO_STA_STBITERR not defined */
01100     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE));
01101 #endif /* SDIO_STA_STBITERR */
01102 
01103     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
01104     {
01105       add *= 512U;
01106     }
01107 
01108     /* Write Blocks in Polling mode */
01109     if(NumberOfBlocks > 1U)
01110     {
01111       hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
01112 
01113       /* Write Multi Block command */
01114       errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
01115     }
01116     else
01117     {
01118       hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
01119 
01120       /* Write Single Block command */
01121       errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
01122     }
01123     if(errorstate != HAL_SD_ERROR_NONE)
01124     {
01125       /* Clear all the static flags */
01126       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
01127       hsd->ErrorCode |= errorstate;
01128       hsd->State = HAL_SD_STATE_READY;
01129       hsd->Context = SD_CONTEXT_NONE;
01130       return HAL_ERROR;
01131     }
01132 
01133     /* Configure the SD DPSM (Data Path State Machine) */
01134     config.DataTimeOut   = SDMMC_DATATIMEOUT;
01135     config.DataLength    = BLOCKSIZE * NumberOfBlocks;
01136     config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
01137     config.TransferDir   = SDIO_TRANSFER_DIR_TO_CARD;
01138     config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
01139     config.DPSM          = SDIO_DPSM_ENABLE;
01140     (void)SDIO_ConfigData(hsd->Instance, &config);
01141 
01142     return HAL_OK;
01143   }
01144   else
01145   {
01146     return HAL_BUSY;
01147   }
01148 }
01149 
01150 /**
01151   * @brief  Reads block(s) from a specified address in a card. The Data transfer
01152   *         is managed by DMA mode.
01153   * @note   This API should be followed by a check on the card state through
01154   *         HAL_SD_GetCardState().
01155   * @note   You could also check the DMA transfer process through the SD Rx
01156   *         interrupt event.
01157   * @param  hsd: Pointer SD handle
01158   * @param  pData: Pointer to the buffer that will contain the received data
01159   * @param  BlockAdd: Block Address from where data is to be read
01160   * @param  NumberOfBlocks: Number of blocks to read.
01161   * @retval HAL status
01162   */
01163 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
01164 {
01165   SDIO_DataInitTypeDef config;
01166   uint32_t errorstate;
01167   uint32_t add = BlockAdd;
01168 
01169   if(NULL == pData)
01170   {
01171     hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
01172     return HAL_ERROR;
01173   }
01174 
01175   if(hsd->State == HAL_SD_STATE_READY)
01176   {
01177     hsd->ErrorCode = HAL_SD_ERROR_NONE;
01178 
01179     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
01180     {
01181       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
01182       return HAL_ERROR;
01183     }
01184 
01185     hsd->State = HAL_SD_STATE_BUSY;
01186 
01187     /* Initialize data control register */
01188     hsd->Instance->DCTRL = 0U;
01189 
01190 #if defined(SDIO_STA_STBITERR)
01191     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_IT_STBITERR));
01192 #else /* SDIO_STA_STBITERR not defined */
01193     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
01194 #endif /* SDIO_STA_STBITERR */
01195 
01196     /* Set the DMA transfer complete callback */
01197     hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
01198 
01199     /* Set the DMA error callback */
01200     hsd->hdmarx->XferErrorCallback = SD_DMAError;
01201 
01202     /* Set the DMA Abort callback */
01203     hsd->hdmarx->XferAbortCallback = NULL;
01204 
01205     /* Force DMA Direction */
01206     hsd->hdmarx->Init.Direction = DMA_PERIPH_TO_MEMORY;
01207     MODIFY_REG(hsd->hdmarx->Instance->CR, DMA_SxCR_DIR, hsd->hdmarx->Init.Direction);
01208 
01209     /* Enable the DMA Channel */
01210     if(HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
01211     {
01212       __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
01213       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
01214       hsd->ErrorCode |= HAL_SD_ERROR_DMA;
01215       hsd->State = HAL_SD_STATE_READY;
01216       return HAL_ERROR;
01217     }
01218     else
01219     {
01220       /* Enable SD DMA transfer */
01221       __HAL_SD_DMA_ENABLE(hsd);
01222 
01223       if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
01224       {
01225         add *= 512U;
01226       }
01227 
01228       /* Configure the SD DPSM (Data Path State Machine) */
01229       config.DataTimeOut   = SDMMC_DATATIMEOUT;
01230       config.DataLength    = BLOCKSIZE * NumberOfBlocks;
01231       config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
01232       config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
01233       config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
01234       config.DPSM          = SDIO_DPSM_ENABLE;
01235       (void)SDIO_ConfigData(hsd->Instance, &config);
01236 
01237       /* Read Blocks in DMA mode */
01238       if(NumberOfBlocks > 1U)
01239       {
01240         hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
01241 
01242         /* Read Multi Block command */
01243         errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
01244       }
01245       else
01246       {
01247         hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
01248 
01249         /* Read Single Block command */
01250         errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
01251       }
01252       if(errorstate != HAL_SD_ERROR_NONE)
01253       {
01254         /* Clear all the static flags */
01255         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
01256         hsd->ErrorCode |= errorstate;
01257         hsd->State = HAL_SD_STATE_READY;
01258         hsd->Context = SD_CONTEXT_NONE;
01259         return HAL_ERROR;
01260       }
01261 
01262       return HAL_OK;
01263     }
01264   }
01265   else
01266   {
01267     return HAL_BUSY;
01268   }
01269 }
01270 
01271 /**
01272   * @brief  Writes block(s) to a specified address in a card. The Data transfer
01273   *         is managed by DMA mode.
01274   * @note   This API should be followed by a check on the card state through
01275   *         HAL_SD_GetCardState().
01276   * @note   You could also check the DMA transfer process through the SD Tx
01277   *         interrupt event.
01278   * @param  hsd: Pointer to SD handle
01279   * @param  pData: Pointer to the buffer that will contain the data to transmit
01280   * @param  BlockAdd: Block Address where data will be written
01281   * @param  NumberOfBlocks: Number of blocks to write
01282   * @retval HAL status
01283   */
01284 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
01285 {
01286   SDIO_DataInitTypeDef config;
01287   uint32_t errorstate;
01288   uint32_t add = BlockAdd;
01289 
01290   if(NULL == pData)
01291   {
01292     hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
01293     return HAL_ERROR;
01294   }
01295 
01296   if(hsd->State == HAL_SD_STATE_READY)
01297   {
01298     hsd->ErrorCode = HAL_SD_ERROR_NONE;
01299 
01300     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
01301     {
01302       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
01303       return HAL_ERROR;
01304     }
01305 
01306     hsd->State = HAL_SD_STATE_BUSY;
01307 
01308     /* Initialize data control register */
01309     hsd->Instance->DCTRL = 0U;
01310 
01311     /* Enable SD Error interrupts */
01312 #if defined(SDIO_STA_STBITERR)
01313     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));
01314 #else /* SDIO_STA_STBITERR not defined */
01315     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));   
01316 #endif /* SDIO_STA_STBITERR */
01317 
01318     /* Set the DMA transfer complete callback */
01319     hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
01320 
01321     /* Set the DMA error callback */
01322     hsd->hdmatx->XferErrorCallback = SD_DMAError;
01323 
01324     /* Set the DMA Abort callback */
01325     hsd->hdmatx->XferAbortCallback = NULL;
01326 
01327     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
01328     {
01329       add *= 512U;
01330     }
01331 
01332     /* Write Blocks in Polling mode */
01333     if(NumberOfBlocks > 1U)
01334     {
01335       hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
01336 
01337       /* Write Multi Block command */
01338       errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
01339     }
01340     else
01341     {
01342       hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
01343 
01344       /* Write Single Block command */
01345       errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
01346     }
01347     if(errorstate != HAL_SD_ERROR_NONE)
01348     {
01349       /* Clear all the static flags */
01350       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
01351       hsd->ErrorCode |= errorstate;
01352       hsd->State = HAL_SD_STATE_READY;
01353       hsd->Context = SD_CONTEXT_NONE;
01354       return HAL_ERROR;
01355     }
01356 
01357     /* Enable SDIO DMA transfer */
01358     __HAL_SD_DMA_ENABLE(hsd);
01359 
01360     /* Force DMA Direction */
01361     hsd->hdmatx->Init.Direction = DMA_MEMORY_TO_PERIPH;
01362     MODIFY_REG(hsd->hdmatx->Instance->CR, DMA_SxCR_DIR, hsd->hdmatx->Init.Direction);
01363 
01364     /* Enable the DMA Channel */
01365     if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
01366     {
01367 #if defined(SDIO_STA_STBITERR)
01368       __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));
01369 #else /* SDIO_STA_STBITERR not defined */
01370       __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));   
01371 #endif /* SDIO_STA_STBITERR */
01372       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
01373       hsd->ErrorCode |= HAL_SD_ERROR_DMA;
01374       hsd->State = HAL_SD_STATE_READY;
01375       hsd->Context = SD_CONTEXT_NONE;
01376       return HAL_ERROR;
01377     }
01378     else
01379     {
01380       /* Configure the SD DPSM (Data Path State Machine) */
01381       config.DataTimeOut   = SDMMC_DATATIMEOUT;
01382       config.DataLength    = BLOCKSIZE * NumberOfBlocks;
01383       config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
01384       config.TransferDir   = SDIO_TRANSFER_DIR_TO_CARD;
01385       config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
01386       config.DPSM          = SDIO_DPSM_ENABLE;
01387       (void)SDIO_ConfigData(hsd->Instance, &config);
01388 
01389       return HAL_OK;
01390     }
01391   }
01392   else
01393   {
01394     return HAL_BUSY;
01395   }
01396 }
01397 
01398 /**
01399   * @brief  Erases the specified memory area of the given SD card.
01400   * @note   This API should be followed by a check on the card state through
01401   *         HAL_SD_GetCardState().
01402   * @param  hsd: Pointer to SD handle
01403   * @param  BlockStartAdd: Start Block address
01404   * @param  BlockEndAdd: End Block address
01405   * @retval HAL status
01406   */
01407 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
01408 {
01409   uint32_t errorstate;
01410   uint32_t start_add = BlockStartAdd;
01411   uint32_t end_add = BlockEndAdd;
01412 
01413   if(hsd->State == HAL_SD_STATE_READY)
01414   {
01415     hsd->ErrorCode = HAL_SD_ERROR_NONE;
01416 
01417     if(end_add < start_add)
01418     {
01419       hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
01420       return HAL_ERROR;
01421     }
01422 
01423     if(end_add > (hsd->SdCard.LogBlockNbr))
01424     {
01425       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
01426       return HAL_ERROR;
01427     }
01428 
01429     hsd->State = HAL_SD_STATE_BUSY;
01430 
01431     /* Check if the card command class supports erase command */
01432     if(((hsd->SdCard.Class) & SDIO_CCCC_ERASE) == 0U)
01433     {
01434       /* Clear all the static flags */
01435       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
01436       hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
01437       hsd->State = HAL_SD_STATE_READY;
01438       return HAL_ERROR;
01439     }
01440 
01441     if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
01442     {
01443       /* Clear all the static flags */
01444       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
01445       hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
01446       hsd->State = HAL_SD_STATE_READY;
01447       return HAL_ERROR;
01448     }
01449 
01450     /* Get start and end block for high capacity cards */
01451     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
01452     {
01453       start_add *= 512U;
01454       end_add   *= 512U;
01455     }
01456 
01457     /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
01458     if(hsd->SdCard.CardType != CARD_SECURED)
01459     {
01460       /* Send CMD32 SD_ERASE_GRP_START with argument as addr  */
01461       errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
01462       if(errorstate != HAL_SD_ERROR_NONE)
01463       {
01464         /* Clear all the static flags */
01465         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
01466         hsd->ErrorCode |= errorstate;
01467         hsd->State = HAL_SD_STATE_READY;
01468         return HAL_ERROR;
01469       }
01470 
01471       /* Send CMD33 SD_ERASE_GRP_END with argument as addr  */
01472       errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
01473       if(errorstate != HAL_SD_ERROR_NONE)
01474       {
01475         /* Clear all the static flags */
01476         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
01477         hsd->ErrorCode |= errorstate;
01478         hsd->State = HAL_SD_STATE_READY;
01479         return HAL_ERROR;
01480       }
01481     }
01482 
01483     /* Send CMD38 ERASE */
01484     errorstate = SDMMC_CmdErase(hsd->Instance);
01485     if(errorstate != HAL_SD_ERROR_NONE)
01486     {
01487       /* Clear all the static flags */
01488       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
01489       hsd->ErrorCode |= errorstate;
01490       hsd->State = HAL_SD_STATE_READY;
01491       return HAL_ERROR;
01492     }
01493 
01494     hsd->State = HAL_SD_STATE_READY;
01495 
01496     return HAL_OK;
01497   }
01498   else
01499   {
01500     return HAL_BUSY;
01501   }
01502 }
01503 
01504 /**
01505   * @brief  This function handles SD card interrupt request.
01506   * @param  hsd: Pointer to SD handle
01507   * @retval None
01508   */
01509 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
01510 {
01511   uint32_t errorstate;
01512   uint32_t context = hsd->Context;
01513 
01514   /* Check for SDIO interrupt flags */
01515   if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
01516   {
01517     SD_Read_IT(hsd);
01518   }
01519 
01520   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) != RESET)
01521   {
01522     __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DATAEND);
01523 
01524 #if defined(SDIO_STA_STBITERR)
01525     __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND  | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
01526                              SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR  | SDIO_IT_TXFIFOHE |\
01527                              SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR);
01528 #else /* SDIO_STA_STBITERR not defined */
01529     __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND  | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
01530                              SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR  | SDIO_IT_TXFIFOHE |\
01531                              SDIO_IT_RXFIFOHF);
01532 #endif /* SDIO_STA_STBITERR */
01533 
01534     hsd->Instance->DCTRL &= ~(SDIO_DCTRL_DTEN);
01535     
01536     if((context & SD_CONTEXT_IT) != 0U)
01537     {
01538       if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
01539       {
01540         errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
01541         if(errorstate != HAL_SD_ERROR_NONE)
01542         {
01543           hsd->ErrorCode |= errorstate;
01544 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
01545           hsd->ErrorCallback(hsd);
01546 #else
01547           HAL_SD_ErrorCallback(hsd);
01548 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
01549         }
01550       }
01551 
01552       /* Clear all the static flags */
01553       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
01554 
01555       hsd->State = HAL_SD_STATE_READY;
01556       hsd->Context = SD_CONTEXT_NONE;
01557       if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
01558       {
01559 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
01560         hsd->RxCpltCallback(hsd);
01561 #else
01562         HAL_SD_RxCpltCallback(hsd);
01563 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
01564       }
01565       else
01566       {
01567 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
01568         hsd->TxCpltCallback(hsd);
01569 #else
01570         HAL_SD_TxCpltCallback(hsd);
01571 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
01572       }
01573     }
01574     else if((context & SD_CONTEXT_DMA) != 0U)
01575     {
01576       if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
01577       {
01578         errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
01579         if(errorstate != HAL_SD_ERROR_NONE)
01580         {
01581           hsd->ErrorCode |= errorstate;
01582 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
01583           hsd->ErrorCallback(hsd);
01584 #else
01585           HAL_SD_ErrorCallback(hsd);
01586 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
01587         }
01588       }
01589       if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
01590       {
01591         /* Disable the DMA transfer for transmit request by setting the DMAEN bit
01592         in the SD DCTRL register */
01593         hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
01594 
01595         hsd->State = HAL_SD_STATE_READY;
01596 
01597 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
01598         hsd->TxCpltCallback(hsd);
01599 #else
01600         HAL_SD_TxCpltCallback(hsd);
01601 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
01602       }
01603     }
01604     else
01605     {
01606       /* Nothing to do */
01607     }
01608   }
01609 
01610   else if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
01611   {
01612     SD_Write_IT(hsd);
01613   }
01614 
01615 #if defined(SDIO_STA_STBITERR)
01616   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR | SDIO_FLAG_STBITERR) != RESET)
01617 #else /* SDIO_STA_STBITERR not defined */
01618   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR) != RESET)
01619 #endif /* SDIO_STA_STBITERR */
01620   {
01621     /* Set Error code */
01622     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL) != RESET)
01623     {
01624       hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
01625     }
01626     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT) != RESET)
01627     {
01628       hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
01629     }
01630     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR) != RESET)
01631     {
01632       hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
01633     }
01634     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR) != RESET)
01635     {
01636       hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
01637     }
01638 #if defined(SDIO_STA_STBITERR)
01639     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_STBITERR) != RESET)
01640     {
01641       hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
01642     }
01643 #endif /* SDIO_STA_STBITERR */
01644 
01645 #if defined(SDIO_STA_STBITERR)
01646     /* Clear All flags */
01647     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS | SDIO_FLAG_STBITERR);
01648 
01649     /* Disable all interrupts */
01650     __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
01651                              SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
01652 #else /* SDIO_STA_STBITERR not defined */
01653     /* Clear All flags */
01654     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
01655 
01656     /* Disable all interrupts */
01657     __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
01658                              SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
01659 #endif /* SDIO_STA_STBITERR */
01660 
01661     hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
01662 
01663     if((context & SD_CONTEXT_IT) != 0U)
01664     {
01665       /* Set the SD state to ready to be able to start again the process */
01666       hsd->State = HAL_SD_STATE_READY;
01667       hsd->Context = SD_CONTEXT_NONE;
01668 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
01669       hsd->ErrorCallback(hsd);
01670 #else
01671       HAL_SD_ErrorCallback(hsd);
01672 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
01673     }
01674     else if((context & SD_CONTEXT_DMA) != 0U)
01675     {
01676       /* Abort the SD DMA channel */
01677       if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
01678       {
01679         /* Set the DMA Tx abort callback */
01680         hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
01681         /* Abort DMA in IT mode */
01682         if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
01683         {
01684           SD_DMATxAbort(hsd->hdmatx);
01685         }
01686       }
01687       else if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
01688       {
01689         /* Set the DMA Rx abort callback */
01690         hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
01691         /* Abort DMA in IT mode */
01692         if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
01693         {
01694           SD_DMARxAbort(hsd->hdmarx);
01695         }
01696       }
01697       else
01698       {
01699         hsd->ErrorCode = HAL_SD_ERROR_NONE;
01700         hsd->State = HAL_SD_STATE_READY;
01701         hsd->Context = SD_CONTEXT_NONE;
01702 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
01703         hsd->AbortCpltCallback(hsd);
01704 #else
01705         HAL_SD_AbortCallback(hsd);
01706 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
01707       }
01708     }
01709     else
01710     {
01711       /* Nothing to do */
01712     }
01713   }
01714   else
01715   {
01716     /* Nothing to do */
01717   }
01718 }
01719 
01720 /**
01721   * @brief return the SD state
01722   * @param hsd: Pointer to sd handle
01723   * @retval HAL state
01724   */
01725 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
01726 {
01727   return hsd->State;
01728 }
01729 
01730 /**
01731 * @brief  Return the SD error code
01732 * @param  hsd : Pointer to a SD_HandleTypeDef structure that contains
01733   *              the configuration information.
01734 * @retval SD Error Code
01735 */
01736 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
01737 {
01738   return hsd->ErrorCode;
01739 }
01740 
01741 /**
01742   * @brief Tx Transfer completed callbacks
01743   * @param hsd: Pointer to SD handle
01744   * @retval None
01745   */
01746 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
01747 {
01748   /* Prevent unused argument(s) compilation warning */
01749   UNUSED(hsd);
01750 
01751   /* NOTE : This function should not be modified, when the callback is needed,
01752             the HAL_SD_TxCpltCallback can be implemented in the user file
01753    */
01754 }
01755 
01756 /**
01757   * @brief Rx Transfer completed callbacks
01758   * @param hsd: Pointer SD handle
01759   * @retval None
01760   */
01761 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
01762 {
01763   /* Prevent unused argument(s) compilation warning */
01764   UNUSED(hsd);
01765 
01766   /* NOTE : This function should not be modified, when the callback is needed,
01767             the HAL_SD_RxCpltCallback can be implemented in the user file
01768    */
01769 }
01770 
01771 /**
01772   * @brief SD error callbacks
01773   * @param hsd: Pointer SD handle
01774   * @retval None
01775   */
01776 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
01777 {
01778   /* Prevent unused argument(s) compilation warning */
01779   UNUSED(hsd);
01780 
01781   /* NOTE : This function should not be modified, when the callback is needed,
01782             the HAL_SD_ErrorCallback can be implemented in the user file
01783    */
01784 }
01785 
01786 /**
01787   * @brief SD Abort callbacks
01788   * @param hsd: Pointer SD handle
01789   * @retval None
01790   */
01791 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
01792 {
01793   /* Prevent unused argument(s) compilation warning */
01794   UNUSED(hsd);
01795 
01796   /* NOTE : This function should not be modified, when the callback is needed,
01797             the HAL_SD_AbortCallback can be implemented in the user file
01798    */
01799 }
01800 
01801 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
01802 /**
01803   * @brief  Register a User SD Callback
01804   *         To be used instead of the weak (surcharged) predefined callback
01805   * @param hsd : SD handle
01806   * @param CallbackID : ID of the callback to be registered
01807   *        This parameter can be one of the following values:
01808   *          @arg @ref HAL_SD_TX_CPLT_CB_ID    SD Tx Complete Callback ID
01809   *          @arg @ref HAL_SD_RX_CPLT_CB_ID    SD Rx Complete Callback ID
01810   *          @arg @ref HAL_SD_ERROR_CB_ID      SD Error Callback ID
01811   *          @arg @ref HAL_SD_ABORT_CB_ID      SD Abort Callback ID
01812   *          @arg @ref HAL_SD_MSP_INIT_CB_ID   SD MspInit Callback ID
01813   *          @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
01814   * @param pCallback : pointer to the Callback function
01815   * @retval status
01816   */
01817 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
01818 {
01819   HAL_StatusTypeDef status = HAL_OK;
01820 
01821   if(pCallback == NULL)
01822   {
01823     /* Update the error code */
01824     hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
01825     return HAL_ERROR;
01826   }
01827 
01828   /* Process locked */
01829   __HAL_LOCK(hsd);
01830 
01831   if(hsd->State == HAL_SD_STATE_READY)
01832   {
01833     switch (CallbackID)
01834     {
01835     case HAL_SD_TX_CPLT_CB_ID :
01836       hsd->TxCpltCallback = pCallback;
01837       break;
01838     case HAL_SD_RX_CPLT_CB_ID :
01839       hsd->RxCpltCallback = pCallback;
01840       break;
01841     case HAL_SD_ERROR_CB_ID :
01842       hsd->ErrorCallback = pCallback;
01843       break;
01844     case HAL_SD_ABORT_CB_ID :
01845       hsd->AbortCpltCallback = pCallback;
01846       break;
01847     case HAL_SD_MSP_INIT_CB_ID :
01848       hsd->MspInitCallback = pCallback;
01849       break;
01850     case HAL_SD_MSP_DEINIT_CB_ID :
01851       hsd->MspDeInitCallback = pCallback;
01852       break;
01853     default :
01854       /* Update the error code */
01855       hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
01856       /* update return status */
01857       status =  HAL_ERROR;
01858       break;
01859     }
01860   }
01861   else if (hsd->State == HAL_SD_STATE_RESET)
01862   {
01863     switch (CallbackID)
01864     {
01865     case HAL_SD_MSP_INIT_CB_ID :
01866       hsd->MspInitCallback = pCallback;
01867       break;
01868     case HAL_SD_MSP_DEINIT_CB_ID :
01869       hsd->MspDeInitCallback = pCallback;
01870       break;
01871     default :
01872       /* Update the error code */
01873       hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
01874       /* update return status */
01875       status =  HAL_ERROR;
01876       break;
01877     }
01878   }
01879   else
01880   {
01881     /* Update the error code */
01882     hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
01883     /* update return status */
01884     status =  HAL_ERROR;
01885   }
01886 
01887   /* Release Lock */
01888   __HAL_UNLOCK(hsd);
01889   return status;
01890 }
01891 
01892 /**
01893   * @brief  Unregister a User SD Callback
01894   *         SD Callback is redirected to the weak (surcharged) predefined callback
01895   * @param hsd : SD handle
01896   * @param CallbackID : ID of the callback to be unregistered
01897   *        This parameter can be one of the following values:
01898   *          @arg @ref HAL_SD_TX_CPLT_CB_ID    SD Tx Complete Callback ID
01899   *          @arg @ref HAL_SD_RX_CPLT_CB_ID    SD Rx Complete Callback ID
01900   *          @arg @ref HAL_SD_ERROR_CB_ID      SD Error Callback ID
01901   *          @arg @ref HAL_SD_ABORT_CB_ID      SD Abort Callback ID
01902   *          @arg @ref HAL_SD_MSP_INIT_CB_ID   SD MspInit Callback ID
01903   *          @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
01904   * @retval status
01905   */
01906 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
01907 {
01908   HAL_StatusTypeDef status = HAL_OK;
01909 
01910   /* Process locked */
01911   __HAL_LOCK(hsd);
01912 
01913   if(hsd->State == HAL_SD_STATE_READY)
01914   {
01915     switch (CallbackID)
01916     {
01917     case HAL_SD_TX_CPLT_CB_ID :
01918       hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
01919       break;
01920     case HAL_SD_RX_CPLT_CB_ID :
01921       hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
01922       break;
01923     case HAL_SD_ERROR_CB_ID :
01924       hsd->ErrorCallback = HAL_SD_ErrorCallback;
01925       break;
01926     case HAL_SD_ABORT_CB_ID :
01927       hsd->AbortCpltCallback = HAL_SD_AbortCallback;
01928       break;
01929     case HAL_SD_MSP_INIT_CB_ID :
01930       hsd->MspInitCallback = HAL_SD_MspInit;
01931       break;
01932     case HAL_SD_MSP_DEINIT_CB_ID :
01933       hsd->MspDeInitCallback = HAL_SD_MspDeInit;
01934       break;
01935     default :
01936       /* Update the error code */
01937       hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
01938       /* update return status */
01939       status =  HAL_ERROR;
01940       break;
01941     }
01942   }
01943   else if (hsd->State == HAL_SD_STATE_RESET)
01944   {
01945     switch (CallbackID)
01946     {
01947     case HAL_SD_MSP_INIT_CB_ID :
01948       hsd->MspInitCallback = HAL_SD_MspInit;
01949       break;
01950     case HAL_SD_MSP_DEINIT_CB_ID :
01951       hsd->MspDeInitCallback = HAL_SD_MspDeInit;
01952       break;
01953     default :
01954       /* Update the error code */
01955       hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
01956       /* update return status */
01957       status =  HAL_ERROR;
01958       break;
01959     }
01960   }
01961   else
01962   {
01963     /* Update the error code */
01964     hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
01965     /* update return status */
01966     status =  HAL_ERROR;
01967   }
01968 
01969   /* Release Lock */
01970   __HAL_UNLOCK(hsd);
01971   return status;
01972 }
01973 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
01974 
01975 /**
01976   * @}
01977   */
01978 
01979 /** @addtogroup SD_Exported_Functions_Group3
01980  *  @brief   management functions
01981  *
01982 @verbatim
01983   ==============================================================================
01984                       ##### Peripheral Control functions #####
01985   ==============================================================================
01986   [..]
01987     This subsection provides a set of functions allowing to control the SD card
01988     operations and get the related information
01989 
01990 @endverbatim
01991   * @{
01992   */
01993 
01994 /**
01995   * @brief  Returns information the information of the card which are stored on
01996   *         the CID register.
01997   * @param  hsd: Pointer to SD handle
01998   * @param  pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that  
01999   *         contains all CID register parameters
02000   * @retval HAL status
02001   */
02002 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
02003 {
02004   pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
02005 
02006   pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
02007 
02008   pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
02009 
02010   pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
02011 
02012   pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
02013 
02014   pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
02015 
02016   pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
02017 
02018   pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
02019 
02020   pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
02021 
02022   pCID->Reserved2 = 1U;
02023 
02024   return HAL_OK;
02025 }
02026 
02027 /**
02028   * @brief  Returns information the information of the card which are stored on
02029   *         the CSD register.
02030   * @param  hsd: Pointer to SD handle
02031   * @param  pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that  
02032   *         contains all CSD register parameters
02033   * @retval HAL status
02034   */
02035 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
02036 {
02037   pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
02038 
02039   pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
02040 
02041   pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
02042 
02043   pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
02044 
02045   pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
02046 
02047   pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
02048 
02049   pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
02050 
02051   pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
02052 
02053   pCSD->PartBlockRead   = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
02054 
02055   pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
02056 
02057   pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
02058 
02059   pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
02060 
02061   pCSD->Reserved2 = 0U; /*!< Reserved */
02062 
02063   if(hsd->SdCard.CardType == CARD_SDSC)
02064   {
02065     pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
02066 
02067     pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
02068 
02069     pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
02070 
02071     pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
02072 
02073     pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
02074 
02075     pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
02076 
02077     hsd->SdCard.BlockNbr  = (pCSD->DeviceSize + 1U) ;
02078     hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
02079     hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
02080 
02081     hsd->SdCard.LogBlockNbr =  (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
02082     hsd->SdCard.LogBlockSize = 512U;
02083   }
02084   else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
02085   {
02086     /* Byte 7 */
02087     pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
02088 
02089     hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
02090     hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
02091     hsd->SdCard.BlockSize = 512U;
02092     hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
02093   }
02094   else
02095   {
02096     /* Clear all the static flags */
02097     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
02098     hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
02099     hsd->State = HAL_SD_STATE_READY;
02100     return HAL_ERROR;
02101   }
02102 
02103   pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
02104 
02105   pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
02106 
02107   pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
02108 
02109   pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
02110 
02111   pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
02112 
02113   pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
02114 
02115   pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
02116 
02117   pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
02118 
02119   pCSD->Reserved3 = 0;
02120 
02121   pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
02122 
02123   pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
02124 
02125   pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
02126 
02127   pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
02128 
02129   pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
02130 
02131   pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
02132 
02133   pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
02134 
02135   pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
02136 
02137   pCSD->Reserved4 = 1;
02138 
02139   return HAL_OK;
02140 }
02141 
02142 /**
02143   * @brief  Gets the SD status info.
02144   * @param  hsd: Pointer to SD handle
02145   * @param  pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that 
02146   *         will contain the SD card status information
02147   * @retval HAL status
02148   */
02149 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
02150 {
02151   uint32_t sd_status[16];
02152   uint32_t errorstate;
02153   HAL_StatusTypeDef status = HAL_OK;
02154 
02155   errorstate = SD_SendSDStatus(hsd, sd_status);
02156   if(errorstate != HAL_SD_ERROR_NONE)
02157   {
02158     /* Clear all the static flags */
02159     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
02160     hsd->ErrorCode |= errorstate;
02161     hsd->State = HAL_SD_STATE_READY;
02162     status = HAL_ERROR;
02163   }
02164   else
02165   {
02166     pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
02167 
02168     pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
02169 
02170     pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
02171 
02172     pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U)    | ((sd_status[1] & 0xFF00U) << 8U) |
02173                                   ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
02174 
02175     pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
02176 
02177     pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
02178 
02179     pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
02180 
02181     pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
02182 
02183     pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
02184 
02185     pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
02186   }
02187 
02188   /* Set Block Size for Card */
02189   errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
02190   if(errorstate != HAL_SD_ERROR_NONE)
02191   {
02192     /* Clear all the static flags */
02193     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
02194     hsd->ErrorCode = errorstate;
02195     hsd->State = HAL_SD_STATE_READY;
02196     status = HAL_ERROR;
02197   }
02198 
02199   return status;
02200 }
02201 
02202 /**
02203   * @brief  Gets the SD card info.
02204   * @param  hsd: Pointer to SD handle
02205   * @param  pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
02206   *         will contain the SD card status information
02207   * @retval HAL status
02208   */
02209 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
02210 {
02211   pCardInfo->CardType     = (uint32_t)(hsd->SdCard.CardType);
02212   pCardInfo->CardVersion  = (uint32_t)(hsd->SdCard.CardVersion);
02213   pCardInfo->Class        = (uint32_t)(hsd->SdCard.Class);
02214   pCardInfo->RelCardAdd   = (uint32_t)(hsd->SdCard.RelCardAdd);
02215   pCardInfo->BlockNbr     = (uint32_t)(hsd->SdCard.BlockNbr);
02216   pCardInfo->BlockSize    = (uint32_t)(hsd->SdCard.BlockSize);
02217   pCardInfo->LogBlockNbr  = (uint32_t)(hsd->SdCard.LogBlockNbr);
02218   pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
02219 
02220   return HAL_OK;
02221 }
02222 
02223 /**
02224   * @brief  Enables wide bus operation for the requested card if supported by
02225   *         card.
02226   * @param  hsd: Pointer to SD handle
02227   * @param  WideMode: Specifies the SD card wide bus mode
02228   *          This parameter can be one of the following values:
02229   *            @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
02230   *            @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
02231   *            @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
02232   * @retval HAL status
02233   */
02234 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
02235 {
02236   SDIO_InitTypeDef Init;
02237   uint32_t errorstate;
02238   HAL_StatusTypeDef status = HAL_OK;
02239 
02240   /* Check the parameters */
02241   assert_param(IS_SDIO_BUS_WIDE(WideMode));
02242 
02243   /* Change State */
02244   hsd->State = HAL_SD_STATE_BUSY;
02245 
02246   if(hsd->SdCard.CardType != CARD_SECURED)
02247   {
02248     if(WideMode == SDIO_BUS_WIDE_8B)
02249     {
02250       hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
02251     }
02252     else if(WideMode == SDIO_BUS_WIDE_4B)
02253     {
02254       errorstate = SD_WideBus_Enable(hsd);
02255 
02256       hsd->ErrorCode |= errorstate;
02257     }
02258     else if(WideMode == SDIO_BUS_WIDE_1B)
02259     {
02260       errorstate = SD_WideBus_Disable(hsd);
02261 
02262       hsd->ErrorCode |= errorstate;
02263     }
02264     else
02265     {
02266       /* WideMode is not a valid argument*/
02267       hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
02268     }
02269   }
02270   else
02271   {
02272     /* MMC Card does not support this feature */
02273     hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
02274   }
02275 
02276   if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
02277   {
02278     /* Clear all the static flags */
02279     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
02280     hsd->State = HAL_SD_STATE_READY;
02281     status = HAL_ERROR;
02282   }
02283   else
02284   {
02285     /* Configure the SDIO peripheral */
02286     Init.ClockEdge           = hsd->Init.ClockEdge;
02287     Init.ClockBypass         = hsd->Init.ClockBypass;
02288     Init.ClockPowerSave      = hsd->Init.ClockPowerSave;
02289     Init.BusWide             = WideMode;
02290     Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
02291     Init.ClockDiv            = hsd->Init.ClockDiv;
02292     (void)SDIO_Init(hsd->Instance, Init);
02293   }
02294 
02295   /* Set Block Size for Card */
02296   errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
02297   if(errorstate != HAL_SD_ERROR_NONE)
02298   {
02299     /* Clear all the static flags */
02300     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
02301     hsd->ErrorCode |= errorstate;
02302     status = HAL_ERROR;
02303   }
02304 
02305   /* Change State */
02306   hsd->State = HAL_SD_STATE_READY;
02307 
02308   return status;
02309 }
02310 
02311 /**
02312   * @brief  Gets the current sd card data state.
02313   * @param  hsd: pointer to SD handle
02314   * @retval Card state
02315   */
02316 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
02317 {
02318   uint32_t cardstate;
02319   uint32_t errorstate;
02320   uint32_t resp1 = 0;
02321 
02322   errorstate = SD_SendStatus(hsd, &resp1);
02323   if(errorstate != HAL_SD_ERROR_NONE)
02324   {
02325     hsd->ErrorCode |= errorstate;
02326   }
02327 
02328   cardstate = ((resp1 >> 9U) & 0x0FU);
02329 
02330   return (HAL_SD_CardStateTypeDef)cardstate;
02331 }
02332 
02333 /**
02334   * @brief  Abort the current transfer and disable the SD.
02335   * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
02336   *                the configuration information for SD module.
02337   * @retval HAL status
02338   */
02339 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
02340 {
02341   HAL_SD_CardStateTypeDef CardState;
02342   uint32_t context = hsd->Context;
02343 
02344   /* DIsable All interrupts */
02345   __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
02346                            SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
02347 
02348   /* Clear All flags */
02349   __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
02350 
02351   CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
02352 
02353   if ((context & SD_CONTEXT_DMA) != 0U)
02354   {
02355     /* Disable the SD DMA request */
02356     hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
02357 
02358     /* Abort the SD DMA Tx channel */
02359     if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
02360     {
02361       if(HAL_DMA_Abort(hsd->hdmatx) != HAL_OK)
02362       {
02363         hsd->ErrorCode |= HAL_SD_ERROR_DMA;
02364       }
02365     }
02366     /* Abort the SD DMA Rx channel */
02367     else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
02368     {
02369       if(HAL_DMA_Abort(hsd->hdmarx) != HAL_OK)
02370       {
02371         hsd->ErrorCode |= HAL_SD_ERROR_DMA;
02372       }
02373     }
02374     else
02375     {
02376       /* Nothing to do */
02377     }
02378   }
02379 
02380   hsd->State = HAL_SD_STATE_READY;
02381 
02382   /* Initialize the SD operation */
02383   hsd->Context = SD_CONTEXT_NONE;
02384 
02385   CardState = HAL_SD_GetCardState(hsd);
02386   if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
02387   {
02388     hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
02389   }
02390   if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
02391   {
02392     return HAL_ERROR;
02393   }
02394   return HAL_OK;
02395 }
02396 
02397 /**
02398   * @brief  Abort the current transfer and disable the SD (IT mode).
02399   * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
02400   *                the configuration information for SD module.
02401   * @retval HAL status
02402   */
02403 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
02404 {
02405   HAL_SD_CardStateTypeDef CardState;
02406   uint32_t context = hsd->Context;
02407 
02408   /* Disable All interrupts */
02409   __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
02410                            SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
02411 
02412   CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
02413 
02414   if ((context & SD_CONTEXT_DMA) != 0U)
02415   {
02416     /* Disable the SD DMA request */
02417     hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
02418 
02419     /* Abort the SD DMA Tx channel */
02420     if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
02421     {
02422       hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
02423       if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
02424       {
02425         hsd->hdmatx = NULL;
02426       }
02427     }
02428     /* Abort the SD DMA Rx channel */
02429     else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
02430     {
02431       hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
02432       if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
02433       {
02434         hsd->hdmarx = NULL;
02435       }
02436     }
02437     else
02438     {
02439       /* Nothing to do */
02440     }
02441   }
02442   /* No transfer ongoing on both DMA channels*/
02443   else
02444   {
02445     /* Clear All flags */
02446     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
02447 
02448     CardState = HAL_SD_GetCardState(hsd);
02449     hsd->State = HAL_SD_STATE_READY;
02450     hsd->Context = SD_CONTEXT_NONE;
02451     if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
02452     {
02453       hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
02454     }
02455     if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
02456     {
02457       return HAL_ERROR;
02458     }
02459     else
02460     {
02461 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
02462       hsd->AbortCpltCallback(hsd);
02463 #else
02464       HAL_SD_AbortCallback(hsd);
02465 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
02466     }
02467   }
02468 
02469   return HAL_OK;
02470 }
02471 
02472 /**
02473   * @}
02474   */
02475 
02476 /**
02477   * @}
02478   */
02479 
02480 /* Private function ----------------------------------------------------------*/
02481 /** @addtogroup SD_Private_Functions
02482   * @{
02483   */
02484 
02485 /**
02486   * @brief  DMA SD transmit process complete callback
02487   * @param  hdma: DMA handle
02488   * @retval None
02489   */
02490 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
02491 {
02492   SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
02493 
02494   /* Enable DATAEND Interrupt */
02495   __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND));
02496 }
02497 
02498 /**
02499   * @brief  DMA SD receive process complete callback
02500   * @param  hdma: DMA handle
02501   * @retval None
02502   */
02503 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
02504 {
02505   SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
02506   uint32_t errorstate;
02507 
02508   /* Send stop command in multiblock write */
02509   if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
02510   {
02511     errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
02512     if(errorstate != HAL_SD_ERROR_NONE)
02513     {
02514       hsd->ErrorCode |= errorstate;
02515 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
02516       hsd->ErrorCallback(hsd);
02517 #else
02518       HAL_SD_ErrorCallback(hsd);
02519 #endif
02520     }
02521   }
02522 
02523   /* Disable the DMA transfer for transmit request by setting the DMAEN bit
02524   in the SD DCTRL register */
02525   hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
02526 
02527   /* Clear all the static flags */
02528   __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
02529 
02530   hsd->State = HAL_SD_STATE_READY;
02531   hsd->Context = SD_CONTEXT_NONE;
02532 
02533 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
02534   hsd->RxCpltCallback(hsd);
02535 #else
02536   HAL_SD_RxCpltCallback(hsd);
02537 #endif
02538 }
02539 
02540 /**
02541   * @brief  DMA SD communication error callback
02542   * @param  hdma: DMA handle
02543   * @retval None
02544   */
02545 static void SD_DMAError(DMA_HandleTypeDef *hdma)
02546 {
02547   SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
02548   HAL_SD_CardStateTypeDef CardState;
02549   uint32_t RxErrorCode, TxErrorCode;
02550 
02551   /* if DMA error is FIFO error ignore it */
02552   if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
02553   {
02554     RxErrorCode = hsd->hdmarx->ErrorCode;
02555     TxErrorCode = hsd->hdmatx->ErrorCode;  
02556     if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
02557     {
02558       /* Clear All flags */
02559       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
02560 
02561       /* Disable All interrupts */
02562       __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
02563         SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
02564 
02565       hsd->ErrorCode |= HAL_SD_ERROR_DMA;
02566       CardState = HAL_SD_GetCardState(hsd);
02567       if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
02568       {
02569         hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
02570       }
02571 
02572       hsd->State= HAL_SD_STATE_READY;
02573       hsd->Context = SD_CONTEXT_NONE;
02574     }
02575 
02576 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
02577     hsd->ErrorCallback(hsd);
02578 #else
02579     HAL_SD_ErrorCallback(hsd);
02580 #endif
02581   }
02582 }
02583 
02584 /**
02585   * @brief  DMA SD Tx Abort callback
02586   * @param  hdma: DMA handle
02587   * @retval None
02588   */
02589 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
02590 {
02591   SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
02592   HAL_SD_CardStateTypeDef CardState;
02593 
02594   /* Clear All flags */
02595   __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
02596 
02597   CardState = HAL_SD_GetCardState(hsd);
02598   hsd->State = HAL_SD_STATE_READY;
02599   hsd->Context = SD_CONTEXT_NONE;
02600   if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
02601   {
02602     hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
02603   }
02604 
02605   if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
02606   {
02607 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
02608     hsd->AbortCpltCallback(hsd);
02609 #else
02610     HAL_SD_AbortCallback(hsd);
02611 #endif
02612   }
02613   else
02614   {
02615 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
02616     hsd->ErrorCallback(hsd);
02617 #else
02618     HAL_SD_ErrorCallback(hsd);
02619 #endif
02620   }
02621 }
02622 
02623 /**
02624   * @brief  DMA SD Rx Abort callback
02625   * @param  hdma: DMA handle
02626   * @retval None
02627   */
02628 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
02629 {
02630   SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
02631   HAL_SD_CardStateTypeDef CardState;
02632 
02633   /* Clear All flags */
02634   __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
02635 
02636   CardState = HAL_SD_GetCardState(hsd);
02637   hsd->State = HAL_SD_STATE_READY;
02638   hsd->Context = SD_CONTEXT_NONE;
02639   if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
02640   {
02641     hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
02642   }
02643 
02644   if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
02645   {
02646 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
02647     hsd->AbortCpltCallback(hsd);
02648 #else
02649     HAL_SD_AbortCallback(hsd);
02650 #endif
02651   }
02652   else
02653   {
02654 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
02655     hsd->ErrorCallback(hsd);
02656 #else
02657     HAL_SD_ErrorCallback(hsd);
02658 #endif
02659   }
02660 }
02661 
02662 /**
02663   * @brief  Initializes the sd card.
02664   * @param  hsd: Pointer to SD handle
02665   * @retval SD Card error state
02666   */
02667 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
02668 {
02669   HAL_SD_CardCSDTypeDef CSD;
02670   uint32_t errorstate;
02671   uint16_t sd_rca = 1U;
02672 
02673   /* Check the power State */
02674   if(SDIO_GetPowerState(hsd->Instance) == 0U)
02675   {
02676     /* Power off */
02677     return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
02678   }
02679 
02680   if(hsd->SdCard.CardType != CARD_SECURED)
02681   {
02682     /* Send CMD2 ALL_SEND_CID */
02683     errorstate = SDMMC_CmdSendCID(hsd->Instance);
02684     if(errorstate != HAL_SD_ERROR_NONE)
02685     {
02686       return errorstate;
02687     }
02688     else
02689     {
02690       /* Get Card identification number data */
02691       hsd->CID[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
02692       hsd->CID[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
02693       hsd->CID[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
02694       hsd->CID[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
02695     }
02696   }
02697 
02698   if(hsd->SdCard.CardType != CARD_SECURED)
02699   {
02700     /* Send CMD3 SET_REL_ADDR with argument 0 */
02701     /* SD Card publishes its RCA. */
02702     errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
02703     if(errorstate != HAL_SD_ERROR_NONE)
02704     {
02705       return errorstate;
02706     }
02707   }
02708   if(hsd->SdCard.CardType != CARD_SECURED)
02709   {
02710     /* Get the SD card RCA */
02711     hsd->SdCard.RelCardAdd = sd_rca;
02712 
02713     /* Send CMD9 SEND_CSD with argument as card's RCA */
02714     errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
02715     if(errorstate != HAL_SD_ERROR_NONE)
02716     {
02717       return errorstate;
02718     }
02719     else
02720     {
02721       /* Get Card Specific Data */
02722       hsd->CSD[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
02723       hsd->CSD[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
02724       hsd->CSD[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
02725       hsd->CSD[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
02726     }
02727   }
02728 
02729   /* Get the Card Class */
02730   hsd->SdCard.Class = (SDIO_GetResponse(hsd->Instance, SDIO_RESP2) >> 20U);
02731 
02732   /* Get CSD parameters */
02733   if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
02734   {
02735     return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
02736   }
02737 
02738   /* Select the Card */
02739   errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
02740   if(errorstate != HAL_SD_ERROR_NONE)
02741   {
02742     return errorstate;
02743   }
02744 
02745   /* Configure SDIO peripheral interface */
02746   (void)SDIO_Init(hsd->Instance, hsd->Init);
02747 
02748   /* All cards are initialized */
02749   return HAL_SD_ERROR_NONE;
02750 }
02751 
02752 /**
02753   * @brief  Enquires cards about their operating voltage and configures clock
02754   *         controls and stores SD information that will be needed in future
02755   *         in the SD handle.
02756   * @param  hsd: Pointer to SD handle
02757   * @retval error state
02758   */
02759 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
02760 {
02761   __IO uint32_t count = 0U;
02762   uint32_t response = 0U, validvoltage = 0U;
02763   uint32_t errorstate;
02764 
02765   /* CMD0: GO_IDLE_STATE */
02766   errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
02767   if(errorstate != HAL_SD_ERROR_NONE)
02768   {
02769     return errorstate;
02770   }
02771 
02772   /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
02773   errorstate = SDMMC_CmdOperCond(hsd->Instance);
02774   if(errorstate != HAL_SD_ERROR_NONE)
02775   {
02776     hsd->SdCard.CardVersion = CARD_V1_X;
02777     /* CMD0: GO_IDLE_STATE */
02778     errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
02779     if(errorstate != HAL_SD_ERROR_NONE)
02780     {
02781       return errorstate;
02782     }
02783 
02784   }
02785   else
02786   {
02787     hsd->SdCard.CardVersion = CARD_V2_X;
02788   }
02789 
02790   if( hsd->SdCard.CardVersion == CARD_V2_X)
02791   {
02792     /* SEND CMD55 APP_CMD with RCA as 0 */
02793     errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
02794     if(errorstate != HAL_SD_ERROR_NONE)
02795     {
02796       return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
02797     }
02798   }
02799   /* SD CARD */
02800   /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
02801   while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
02802   {
02803     /* SEND CMD55 APP_CMD with RCA as 0 */
02804     errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
02805     if(errorstate != HAL_SD_ERROR_NONE)
02806     {
02807       return errorstate;
02808     }
02809 
02810     /* Send CMD41 */
02811     errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
02812     if(errorstate != HAL_SD_ERROR_NONE)
02813     {
02814       return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
02815     }
02816 
02817     /* Get command response */
02818     response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
02819 
02820     /* Get operating voltage*/
02821     validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
02822 
02823     count++;
02824   }
02825 
02826   if(count >= SDMMC_MAX_VOLT_TRIAL)
02827   {
02828     return HAL_SD_ERROR_INVALID_VOLTRANGE;
02829   }
02830 
02831   if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
02832   {
02833     hsd->SdCard.CardType = CARD_SDHC_SDXC;
02834   }
02835   else
02836   {
02837     hsd->SdCard.CardType = CARD_SDSC;
02838   }
02839 
02840 
02841   return HAL_SD_ERROR_NONE;
02842 }
02843 
02844 /**
02845   * @brief  Turns the SDIO output signals off.
02846   * @param  hsd: Pointer to SD handle
02847   * @retval None
02848   */
02849 static void SD_PowerOFF(SD_HandleTypeDef *hsd)
02850 {
02851   /* Set Power State to OFF */
02852   (void)SDIO_PowerState_OFF(hsd->Instance);
02853 }
02854 
02855 /**
02856   * @brief  Send Status info command.
02857   * @param  hsd: pointer to SD handle
02858   * @param  pSDstatus: Pointer to the buffer that will contain the SD card status
02859   *         SD Status register)
02860   * @retval error state
02861   */
02862 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
02863 {
02864   SDIO_DataInitTypeDef config;
02865   uint32_t errorstate;
02866   uint32_t tickstart = HAL_GetTick();
02867   uint32_t count;
02868   uint32_t *pData = pSDstatus;
02869 
02870   /* Check SD response */
02871   if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
02872   {
02873     return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
02874   }
02875 
02876   /* Set block size for card if it is not equal to current block size for card */
02877   errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
02878   if(errorstate != HAL_SD_ERROR_NONE)
02879   {
02880     hsd->ErrorCode |= HAL_SD_ERROR_NONE;
02881     return errorstate;
02882   }
02883 
02884   /* Send CMD55 */
02885   errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
02886   if(errorstate != HAL_SD_ERROR_NONE)
02887   {
02888     hsd->ErrorCode |= HAL_SD_ERROR_NONE;
02889     return errorstate;
02890   }
02891 
02892   /* Configure the SD DPSM (Data Path State Machine) */
02893   config.DataTimeOut   = SDMMC_DATATIMEOUT;
02894   config.DataLength    = 64U;
02895   config.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
02896   config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
02897   config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
02898   config.DPSM          = SDIO_DPSM_ENABLE;
02899   (void)SDIO_ConfigData(hsd->Instance, &config);
02900 
02901   /* Send ACMD13 (SD_APP_STAUS)  with argument as card's RCA */
02902   errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
02903   if(errorstate != HAL_SD_ERROR_NONE)
02904   {
02905     hsd->ErrorCode |= HAL_SD_ERROR_NONE;
02906     return errorstate;
02907   }
02908 
02909   /* Get status data */
02910   while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
02911   {
02912     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
02913     {
02914       for(count = 0U; count < 8U; count++)
02915       {
02916         *pData = SDIO_ReadFIFO(hsd->Instance);
02917         pData++;
02918       }
02919     }
02920 
02921     if((HAL_GetTick() - tickstart) >=  SDMMC_DATATIMEOUT)
02922     {
02923       return HAL_SD_ERROR_TIMEOUT;
02924     }
02925   }
02926 
02927   if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
02928   {
02929     return HAL_SD_ERROR_DATA_TIMEOUT;
02930   }
02931   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
02932   {
02933     return HAL_SD_ERROR_DATA_CRC_FAIL;
02934   }
02935   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
02936   {
02937     return HAL_SD_ERROR_RX_OVERRUN;
02938   }
02939   else
02940   {
02941     /* Nothing to do */
02942   }
02943 
02944   while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)))
02945   {
02946     *pData = SDIO_ReadFIFO(hsd->Instance);
02947     pData++;
02948 
02949     if((HAL_GetTick() - tickstart) >=  SDMMC_DATATIMEOUT)
02950     {
02951       return HAL_SD_ERROR_TIMEOUT;
02952     }
02953   }
02954 
02955   /* Clear all the static status flags*/
02956   __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
02957 
02958   return HAL_SD_ERROR_NONE;
02959 }
02960 
02961 /**
02962   * @brief  Returns the current card's status.
02963   * @param  hsd: Pointer to SD handle
02964   * @param  pCardStatus: pointer to the buffer that will contain the SD card
02965   *         status (Card Status register)
02966   * @retval error state
02967   */
02968 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
02969 {
02970   uint32_t errorstate;
02971 
02972   if(pCardStatus == NULL)
02973   {
02974     return HAL_SD_ERROR_PARAM;
02975   }
02976 
02977   /* Send Status command */
02978   errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
02979   if(errorstate != HAL_SD_ERROR_NONE)
02980   {
02981     return errorstate;
02982   }
02983 
02984   /* Get SD card status */
02985   *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
02986 
02987   return HAL_SD_ERROR_NONE;
02988 }
02989 
02990 /**
02991   * @brief  Enables the SDIO wide bus mode.
02992   * @param  hsd: pointer to SD handle
02993   * @retval error state
02994   */
02995 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
02996 {
02997   uint32_t scr[2U] = {0U, 0U};
02998   uint32_t errorstate;
02999 
03000   if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
03001   {
03002     return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
03003   }
03004 
03005   /* Get SCR Register */
03006   errorstate = SD_FindSCR(hsd, scr);
03007   if(errorstate != HAL_SD_ERROR_NONE)
03008   {
03009     return errorstate;
03010   }
03011 
03012   /* If requested card supports wide bus operation */
03013   if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
03014   {
03015     /* Send CMD55 APP_CMD with argument as card's RCA.*/
03016     errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
03017     if(errorstate != HAL_SD_ERROR_NONE)
03018     {
03019       return errorstate;
03020     }
03021 
03022     /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
03023     errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
03024     if(errorstate != HAL_SD_ERROR_NONE)
03025     {
03026       return errorstate;
03027     }
03028 
03029     return HAL_SD_ERROR_NONE;
03030   }
03031   else
03032   {
03033     return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
03034   }
03035 }
03036 
03037 /**
03038   * @brief  Disables the SDIO wide bus mode.
03039   * @param  hsd: Pointer to SD handle
03040   * @retval error state
03041   */
03042 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
03043 {
03044   uint32_t scr[2U] = {0U, 0U};
03045   uint32_t errorstate;
03046 
03047   if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
03048   {
03049     return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
03050   }
03051 
03052   /* Get SCR Register */
03053   errorstate = SD_FindSCR(hsd, scr);
03054   if(errorstate != HAL_SD_ERROR_NONE)
03055   {
03056     return errorstate;
03057   }
03058 
03059   /* If requested card supports 1 bit mode operation */
03060   if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
03061   {
03062     /* Send CMD55 APP_CMD with argument as card's RCA */
03063     errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
03064     if(errorstate != HAL_SD_ERROR_NONE)
03065     {
03066       return errorstate;
03067     }
03068 
03069     /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
03070     errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
03071     if(errorstate != HAL_SD_ERROR_NONE)
03072     {
03073       return errorstate;
03074     }
03075 
03076     return HAL_SD_ERROR_NONE;
03077   }
03078   else
03079   {
03080     return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
03081   }
03082 }
03083 
03084 
03085 /**
03086   * @brief  Finds the SD card SCR register value.
03087   * @param  hsd: Pointer to SD handle
03088   * @param  pSCR: pointer to the buffer that will contain the SCR value
03089   * @retval error state
03090   */
03091 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
03092 {
03093   SDIO_DataInitTypeDef config;
03094   uint32_t errorstate;
03095   uint32_t tickstart = HAL_GetTick();
03096   uint32_t index = 0U;
03097   uint32_t tempscr[2U] = {0U, 0U};
03098   uint32_t *scr = pSCR;
03099 
03100   /* Set Block Size To 8 Bytes */
03101   errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
03102   if(errorstate != HAL_SD_ERROR_NONE)
03103   {
03104     return errorstate;
03105   }
03106 
03107   /* Send CMD55 APP_CMD with argument as card's RCA */
03108   errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
03109   if(errorstate != HAL_SD_ERROR_NONE)
03110   {
03111     return errorstate;
03112   }
03113 
03114   config.DataTimeOut   = SDMMC_DATATIMEOUT;
03115   config.DataLength    = 8U;
03116   config.DataBlockSize = SDIO_DATABLOCK_SIZE_8B;
03117   config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
03118   config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
03119   config.DPSM          = SDIO_DPSM_ENABLE;
03120   (void)SDIO_ConfigData(hsd->Instance, &config);
03121 
03122   /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
03123   errorstate = SDMMC_CmdSendSCR(hsd->Instance);
03124   if(errorstate != HAL_SD_ERROR_NONE)
03125   {
03126     return errorstate;
03127   }
03128 
03129   while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT))
03130   {
03131     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
03132     {
03133       *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
03134       index++;
03135     }
03136     else if(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXACT))
03137     {
03138       break;
03139     }
03140 
03141     if((HAL_GetTick() - tickstart) >=  SDMMC_DATATIMEOUT)
03142     {
03143       return HAL_SD_ERROR_TIMEOUT;
03144     }
03145   }
03146 
03147   if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
03148   {
03149     __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
03150 
03151     return HAL_SD_ERROR_DATA_TIMEOUT;
03152   }
03153   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
03154   {
03155     __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
03156 
03157     return HAL_SD_ERROR_DATA_CRC_FAIL;
03158   }
03159   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
03160   {
03161     __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
03162 
03163     return HAL_SD_ERROR_RX_OVERRUN;
03164   }
03165   else
03166   {
03167     /* No error flag set */
03168     /* Clear all the static flags */
03169     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
03170 
03171     *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24)  | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
03172             ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24));
03173     scr++;
03174     *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24)  | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
03175             ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24));
03176 
03177   }
03178 
03179   return HAL_SD_ERROR_NONE;
03180 }
03181 
03182 /**
03183   * @brief  Wrap up reading in non-blocking mode.
03184   * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
03185   *              the configuration information.
03186   * @retval None
03187   */
03188 static void SD_Read_IT(SD_HandleTypeDef *hsd)
03189 {
03190   uint32_t count, data, dataremaining;
03191   uint8_t* tmp;
03192 
03193   tmp = hsd->pRxBuffPtr;
03194   dataremaining = hsd->RxXferSize;
03195 
03196   if (dataremaining > 0U)
03197   {
03198     /* Read data from SDIO Rx FIFO */
03199     for(count = 0U; count < 8U; count++)
03200     {
03201       data = SDIO_ReadFIFO(hsd->Instance);
03202       *tmp = (uint8_t)(data & 0xFFU);
03203       tmp++;
03204       dataremaining--;
03205       *tmp = (uint8_t)((data >> 8U) & 0xFFU);
03206       tmp++;
03207       dataremaining--;
03208       *tmp = (uint8_t)((data >> 16U) & 0xFFU);
03209       tmp++;
03210       dataremaining--;
03211       *tmp = (uint8_t)((data >> 24U) & 0xFFU);
03212       tmp++;
03213       dataremaining--;
03214     }
03215 
03216     hsd->pRxBuffPtr = tmp;
03217     hsd->RxXferSize = dataremaining;
03218   }
03219 }
03220 
03221 /**
03222   * @brief  Wrap up writing in non-blocking mode.
03223   * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
03224   *              the configuration information.
03225   * @retval None
03226   */
03227 static void SD_Write_IT(SD_HandleTypeDef *hsd)
03228 {
03229   uint32_t count, data, dataremaining;
03230   uint8_t* tmp;
03231 
03232   tmp = hsd->pTxBuffPtr;
03233   dataremaining = hsd->TxXferSize;
03234 
03235   if (dataremaining > 0U)
03236   {
03237     /* Write data to SDIO Tx FIFO */
03238     for(count = 0U; count < 8U; count++)
03239     {
03240       data = (uint32_t)(*tmp);
03241       tmp++;
03242       dataremaining--;
03243       data |= ((uint32_t)(*tmp) << 8U);
03244       tmp++;
03245       dataremaining--;
03246       data |= ((uint32_t)(*tmp) << 16U);
03247       tmp++;
03248       dataremaining--;
03249       data |= ((uint32_t)(*tmp) << 24U);
03250       tmp++;
03251       dataremaining--;
03252       (void)SDIO_WriteFIFO(hsd->Instance, &data);
03253     }
03254 
03255     hsd->pTxBuffPtr = tmp;
03256     hsd->TxXferSize = dataremaining;
03257   }
03258 }
03259 
03260 /**
03261   * @}
03262   */
03263 
03264 #endif /* HAL_SD_MODULE_ENABLED */
03265 
03266 /**
03267   * @}
03268   */
03269 
03270 /**
03271   * @}
03272   */
03273 
03274 #endif /* SDIO */
03275 
03276 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/