Subversion Repositories DashDisplay

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32l1xx_hal_sd.c
  4.   * @author  MCD Application Team
  5.   * @brief   SD card HAL module driver.
  6.   *          This file provides firmware functions to manage the following
  7.   *          functionalities of the Secure Digital (SD) peripheral:
  8.   *           + Initialization and de-initialization functions
  9.   *           + IO operation functions
  10.   *           + Peripheral Control functions
  11.   *           + Peripheral State functions
  12.   *
  13.   @verbatim
  14.   ==============================================================================
  15.                         ##### How to use this driver #####
  16.   ==============================================================================
  17.   [..]
  18.     This driver implements a high level communication layer for read and write from/to
  19.     this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by
  20.     the user in HAL_SD_MspInit() function (MSP layer).
  21.     Basically, the MSP layer configuration should be the same as we provide in the
  22.     examples.
  23.     You can easily tailor this configuration according to hardware resources.
  24.  
  25.   [..]
  26.     This driver is a generic layered driver for SDIO memories which uses the HAL
  27.     SDIO driver functions to interface with SD and uSD cards devices.
  28.     It is used as follows:
  29.  
  30.     (#)Initialize the SDIO low level resources by implementing the HAL_SD_MspInit() API:
  31.         (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE();
  32.         (##) SDIO pins configuration for SD card
  33.             (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
  34.             (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init()
  35.                   and according to your pin assignment;
  36.         (##) DMA configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
  37.              and HAL_SD_WriteBlocks_DMA() APIs).
  38.             (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
  39.             (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
  40.         (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
  41.             (+++) Configure the SDIO and DMA interrupt priorities using functions
  42.                   HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority
  43.             (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ()
  44.             (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
  45.                   and __HAL_SD_DISABLE_IT() inside the communication process.
  46.             (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
  47.                   and __HAL_SD_CLEAR_IT()
  48.         (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
  49.              and HAL_SD_WriteBlocks_IT() APIs).
  50.             (+++) Configure the SDIO interrupt priorities using function HAL_NVIC_SetPriority();
  51.             (+++) Enable the NVIC SDIO IRQs using function HAL_NVIC_EnableIRQ()
  52.             (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
  53.                   and __HAL_SD_DISABLE_IT() inside the communication process.
  54.             (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
  55.                   and __HAL_SD_CLEAR_IT()
  56.     (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
  57.  
  58.  
  59.   *** SD Card Initialization and configuration ***
  60.   ================================================
  61.   [..]
  62.     To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
  63.     SDIO Peripheral(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
  64.     This function provide the following operations:
  65.  
  66.     (#) Apply the SD Card initialization process at 400KHz and check the SD Card
  67.         type (Standard Capacity or High Capacity). You can change or adapt this
  68.         frequency by adjusting the "ClockDiv" field.
  69.         The SD Card frequency (SDIO_CK) is computed as follows:
  70.  
  71.            SDIO_CK = SDIOCLK / (ClockDiv + 2)
  72.  
  73.         In initialization mode and according to the SD Card standard,
  74.         make sure that the SDIO_CK frequency doesn't exceed 400KHz.
  75.  
  76.         This phase of initialization is done through SDIO_Init() and
  77.         SDIO_PowerState_ON() SDIO low level APIs.
  78.  
  79.     (#) Initialize the SD card. The API used is HAL_SD_InitCard().
  80.         This phase allows the card initialization and identification
  81.         and check the SD Card type (Standard Capacity or High Capacity)
  82.         The initialization flow is compatible with SD standard.
  83.  
  84.         This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
  85.         of plug-off plug-in.
  86.  
  87.     (#) Configure the SD Card Data transfer frequency. You can change or adapt this
  88.         frequency by adjusting the "ClockDiv" field.
  89.         In transfer mode and according to the SD Card standard, make sure that the
  90.         SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
  91.         To be able to use a frequency higher than 24MHz, you should use the SDIO
  92.         peripheral in bypass mode. Refer to the corresponding reference manual
  93.         for more details.
  94.  
  95.     (#) Select the corresponding SD Card according to the address read with the step 2.
  96.  
  97.     (#) Configure the SD Card in wide bus mode: 4-bits data.
  98.  
  99.   *** SD Card Read operation ***
  100.   ==============================
  101.   [..]
  102.     (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
  103.         This function support only 512-bytes block length (the block size should be
  104.         chosen as 512 bytes).
  105.         You can choose either one block read operation or multiple block read operation
  106.         by adjusting the "NumberOfBlocks" parameter.
  107.         After this, you have to ensure that the transfer is done correctly. The check is done
  108.         through HAL_SD_GetCardState() function for SD card state.
  109.  
  110.     (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
  111.         This function support only 512-bytes block length (the block size should be
  112.         chosen as 512 bytes).
  113.         You can choose either one block read operation or multiple block read operation
  114.         by adjusting the "NumberOfBlocks" parameter.
  115.         After this, you have to ensure that the transfer is done correctly. The check is done
  116.         through HAL_SD_GetCardState() function for SD card state.
  117.         You could also check the DMA transfer process through the SD Rx interrupt event.
  118.  
  119.     (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
  120.         This function support only 512-bytes block length (the block size should be
  121.         chosen as 512 bytes).
  122.         You can choose either one block read operation or multiple block read operation
  123.         by adjusting the "NumberOfBlocks" parameter.
  124.         After this, you have to ensure that the transfer is done correctly. The check is done
  125.         through HAL_SD_GetCardState() function for SD card state.
  126.         You could also check the IT transfer process through the SD Rx interrupt event.
  127.  
  128.   *** SD Card Write operation ***
  129.   ===============================
  130.   [..]
  131.     (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
  132.         This function support only 512-bytes block length (the block size should be
  133.         chosen as 512 bytes).
  134.         You can choose either one block read operation or multiple block read operation
  135.         by adjusting the "NumberOfBlocks" parameter.
  136.         After this, you have to ensure that the transfer is done correctly. The check is done
  137.         through HAL_SD_GetCardState() function for SD card state.
  138.  
  139.     (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
  140.         This function support only 512-bytes block length (the block size should be
  141.         chosen as 512 bytes).
  142.         You can choose either one block read operation or multiple block read operation
  143.         by adjusting the "NumberOfBlocks" parameter.
  144.         After this, you have to ensure that the transfer is done correctly. The check is done
  145.         through HAL_SD_GetCardState() function for SD card state.
  146.         You could also check the DMA transfer process through the SD Tx interrupt event.
  147.  
  148.     (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
  149.         This function support only 512-bytes block length (the block size should be
  150.         chosen as 512 bytes).
  151.         You can choose either one block read operation or multiple block read operation
  152.         by adjusting the "NumberOfBlocks" parameter.
  153.         After this, you have to ensure that the transfer is done correctly. The check is done
  154.         through HAL_SD_GetCardState() function for SD card state.
  155.         You could also check the IT transfer process through the SD Tx interrupt event.
  156.  
  157.   *** SD card status ***
  158.   ======================
  159.   [..]
  160.     (+) The SD Status contains status bits that are related to the SD Memory
  161.         Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
  162.  
  163.   *** SD card information ***
  164.   ===========================
  165.   [..]
  166.     (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
  167.         It returns useful information about the SD card such as block size, card type,
  168.         block number ...
  169.  
  170.   *** SD card CSD register ***
  171.   ============================
  172.     (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
  173.         Some of the CSD parameters are useful for card initialization and identification.
  174.  
  175.   *** SD card CID register ***
  176.   ============================
  177.     (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
  178.         Some of the CSD parameters are useful for card initialization and identification.
  179.  
  180.   *** SD HAL driver macros list ***
  181.   ==================================
  182.   [..]
  183.     Below the list of most used macros in SD HAL driver.
  184.  
  185.     (+) __HAL_SD_ENABLE : Enable the SD device
  186.     (+) __HAL_SD_DISABLE : Disable the SD device
  187.     (+) __HAL_SD_DMA_ENABLE: Enable the SDIO DMA transfer
  188.     (+) __HAL_SD_DMA_DISABLE: Disable the SDIO DMA transfer
  189.     (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
  190.     (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
  191.     (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
  192.     (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
  193.  
  194.     (@) You can refer to the SD HAL driver header file for more useful macros
  195.  
  196.   *** Callback registration ***
  197.   =============================================
  198.   [..]
  199.     The compilation define USE_HAL_SD_REGISTER_CALLBACKS when set to 1
  200.     allows the user to configure dynamically the driver callbacks.
  201.  
  202.     Use Functions @ref HAL_SD_RegisterCallback() to register a user callback,
  203.     it allows to register following callbacks:
  204.       (+) TxCpltCallback : callback when a transmission transfer is completed.
  205.       (+) RxCpltCallback : callback when a reception transfer is completed.
  206.       (+) ErrorCallback : callback when error occurs.
  207.       (+) AbortCpltCallback : callback when abort is completed.
  208.       (+) MspInitCallback    : SD MspInit.
  209.       (+) MspDeInitCallback  : SD MspDeInit.
  210.     This function takes as parameters the HAL peripheral handle, the Callback ID
  211.     and a pointer to the user callback function.
  212.  
  213.     Use function @ref HAL_SD_UnRegisterCallback() to reset a callback to the default
  214.     weak (surcharged) function. It allows to reset following callbacks:
  215.       (+) TxCpltCallback : callback when a transmission transfer is completed.
  216.       (+) RxCpltCallback : callback when a reception transfer is completed.
  217.       (+) ErrorCallback : callback when error occurs.
  218.       (+) AbortCpltCallback : callback when abort is completed.
  219.       (+) MspInitCallback    : SD MspInit.
  220.       (+) MspDeInitCallback  : SD MspDeInit.
  221.     This function) takes as parameters the HAL peripheral handle and the Callback ID.
  222.  
  223.     By default, after the @ref HAL_SD_Init and if the state is HAL_SD_STATE_RESET
  224.     all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  225.     Exception done for MspInit and MspDeInit callbacks that are respectively
  226.     reset to the legacy weak (surcharged) functions in the @ref HAL_SD_Init
  227.     and @ref  HAL_SD_DeInit only when these callbacks are null (not registered beforehand).
  228.     If not, MspInit or MspDeInit are not null, the @ref HAL_SD_Init and @ref HAL_SD_DeInit
  229.     keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  230.  
  231.     Callbacks can be registered/unregistered in READY state only.
  232.     Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  233.     in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  234.     during the Init/DeInit.
  235.     In that case first register the MspInit/MspDeInit user callbacks
  236.     using @ref HAL_SD_RegisterCallback before calling @ref HAL_SD_DeInit
  237.     or @ref HAL_SD_Init function.
  238.  
  239.     When The compilation define USE_HAL_SD_REGISTER_CALLBACKS is set to 0 or
  240.     not defined, the callback registering feature is not available
  241.     and weak (surcharged) callbacks are used.
  242.  
  243.   @endverbatim
  244.   ******************************************************************************
  245.   * @attention
  246.   *
  247.   * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
  248.   * All rights reserved.</center></h2>
  249.   *
  250.   * This software component is licensed by ST under BSD 3-Clause license,
  251.   * the "License"; You may not use this file except in compliance with the
  252.   * License. You may obtain a copy of the License at:
  253.   *                       opensource.org/licenses/BSD-3-Clause
  254.   *
  255.   ******************************************************************************
  256.   */
  257.  
  258. /* Includes ------------------------------------------------------------------*/
  259. #include "stm32l1xx_hal.h"
  260.  
  261. #if defined(SDIO)
  262.  
  263. /** @addtogroup STM32L1xx_HAL_Driver
  264.   * @{
  265.   */
  266.  
  267. /** @addtogroup SD
  268.   * @{
  269.   */
  270.  
  271. #ifdef HAL_SD_MODULE_ENABLED
  272.  
  273. /* Private typedef -----------------------------------------------------------*/
  274. /* Private define ------------------------------------------------------------*/
  275. /** @addtogroup SD_Private_Defines
  276.   * @{
  277.   */
  278.  
  279. /**
  280.   * @}
  281.   */
  282.  
  283. /* Private macro -------------------------------------------------------------*/
  284. /* Private variables ---------------------------------------------------------*/
  285. /* Private function prototypes -----------------------------------------------*/
  286. /* Private functions ---------------------------------------------------------*/
  287. /** @defgroup SD_Private_Functions SD Private Functions
  288.   * @{
  289.   */
  290. static uint32_t SD_InitCard(SD_HandleTypeDef *hsd);
  291. static uint32_t SD_PowerON(SD_HandleTypeDef *hsd);
  292. static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
  293. static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
  294. static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd);
  295. static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
  296. static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
  297. static void SD_PowerOFF(SD_HandleTypeDef *hsd);
  298. static void SD_Write_IT(SD_HandleTypeDef *hsd);
  299. static void SD_Read_IT(SD_HandleTypeDef *hsd);
  300. static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  301. static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  302. static void SD_DMAError(DMA_HandleTypeDef *hdma);
  303. static void SD_DMATxAbort(DMA_HandleTypeDef *hdma);
  304. static void SD_DMARxAbort(DMA_HandleTypeDef *hdma);
  305. /**
  306.   * @}
  307.   */
  308.  
  309. /* Exported functions --------------------------------------------------------*/
  310. /** @addtogroup SD_Exported_Functions
  311.   * @{
  312.   */
  313.  
  314. /** @addtogroup SD_Exported_Functions_Group1
  315.  *  @brief   Initialization and de-initialization functions
  316.  *
  317. @verbatim
  318.   ==============================================================================
  319.           ##### Initialization and de-initialization functions #####
  320.   ==============================================================================
  321.   [..]
  322.     This section provides functions allowing to initialize/de-initialize the SD
  323.     card device to be ready for use.
  324.  
  325. @endverbatim
  326.   * @{
  327.   */
  328.  
  329. /**
  330.   * @brief  Initializes the SD according to the specified parameters in the
  331.             SD_HandleTypeDef and create the associated handle.
  332.   * @param  hsd: Pointer to the SD handle
  333.   * @retval HAL status
  334.   */
  335. HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
  336. {
  337.   /* Check the SD handle allocation */
  338.   if(hsd == NULL)
  339.   {
  340.     return HAL_ERROR;
  341.   }
  342.  
  343.   /* Check the parameters */
  344.   assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance));
  345.   assert_param(IS_SDIO_CLOCK_EDGE(hsd->Init.ClockEdge));
  346.   assert_param(IS_SDIO_CLOCK_BYPASS(hsd->Init.ClockBypass));
  347.   assert_param(IS_SDIO_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
  348.   assert_param(IS_SDIO_BUS_WIDE(hsd->Init.BusWide));
  349.   assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
  350.   assert_param(IS_SDIO_CLKDIV(hsd->Init.ClockDiv));
  351.  
  352.   if(hsd->State == HAL_SD_STATE_RESET)
  353.   {
  354.     /* Allocate lock resource and initialize it */
  355.     hsd->Lock = HAL_UNLOCKED;
  356. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  357.     /* Reset Callback pointers in HAL_SD_STATE_RESET only */
  358.     hsd->TxCpltCallback    = HAL_SD_TxCpltCallback;
  359.     hsd->RxCpltCallback    = HAL_SD_RxCpltCallback;
  360.     hsd->ErrorCallback     = HAL_SD_ErrorCallback;
  361.     hsd->AbortCpltCallback = HAL_SD_AbortCallback;
  362.  
  363.     if(hsd->MspInitCallback == NULL)
  364.     {
  365.       hsd->MspInitCallback = HAL_SD_MspInit;
  366.     }
  367.  
  368.     /* Init the low level hardware */
  369.     hsd->MspInitCallback(hsd);
  370. #else
  371.     /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  372.     HAL_SD_MspInit(hsd);
  373. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  374.   }
  375.  
  376.   hsd->State = HAL_SD_STATE_BUSY;
  377.  
  378.   /* Initialize the Card parameters */
  379.   if (HAL_SD_InitCard(hsd) != HAL_OK)
  380.   {
  381.     return HAL_ERROR;
  382.   }
  383.  
  384.   /* Initialize the error code */
  385.   hsd->ErrorCode = HAL_SD_ERROR_NONE;
  386.  
  387.   /* Initialize the SD operation */
  388.   hsd->Context = SD_CONTEXT_NONE;
  389.  
  390.   /* Initialize the SD state */
  391.   hsd->State = HAL_SD_STATE_READY;
  392.  
  393.   return HAL_OK;
  394. }
  395.  
  396. /**
  397.   * @brief  Initializes the SD Card.
  398.   * @param  hsd: Pointer to SD handle
  399.   * @note   This function initializes the SD card. It could be used when a card
  400.             re-initialization is needed.
  401.   * @retval HAL status
  402.   */
  403. HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
  404. {
  405.   uint32_t errorstate;
  406.   HAL_StatusTypeDef status;
  407.   SD_InitTypeDef Init;
  408.  
  409.   /* Default SDIO peripheral configuration for SD card initialization */
  410.   Init.ClockEdge           = SDIO_CLOCK_EDGE_RISING;
  411.   Init.ClockBypass         = SDIO_CLOCK_BYPASS_DISABLE;
  412.   Init.ClockPowerSave      = SDIO_CLOCK_POWER_SAVE_DISABLE;
  413.   Init.BusWide             = SDIO_BUS_WIDE_1B;
  414.   Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  415.   Init.ClockDiv            = SDIO_INIT_CLK_DIV;
  416.  
  417.   /* Initialize SDIO peripheral interface with default configuration */
  418.   status = SDIO_Init(hsd->Instance, Init);
  419.   if(status != HAL_OK)
  420.   {
  421.     return HAL_ERROR;
  422.   }
  423.  
  424.   /* Disable SDIO Clock */
  425.   __HAL_SD_DISABLE(hsd);
  426.  
  427.   /* Set Power State to ON */
  428.   status = SDIO_PowerState_ON(hsd->Instance);
  429.   if(status != HAL_OK)
  430.   {
  431.     return HAL_ERROR;
  432.   }
  433.  
  434.   /* Enable SDIO Clock */
  435.   __HAL_SD_ENABLE(hsd);
  436.  
  437.   /* Identify card operating voltage */
  438.   errorstate = SD_PowerON(hsd);
  439.   if(errorstate != HAL_SD_ERROR_NONE)
  440.   {
  441.     hsd->State = HAL_SD_STATE_READY;
  442.     hsd->ErrorCode |= errorstate;
  443.     return HAL_ERROR;
  444.   }
  445.  
  446.   /* Card initialization */
  447.   errorstate = SD_InitCard(hsd);
  448.   if(errorstate != HAL_SD_ERROR_NONE)
  449.   {
  450.     hsd->State = HAL_SD_STATE_READY;
  451.     hsd->ErrorCode |= errorstate;
  452.     return HAL_ERROR;
  453.   }
  454.  
  455.   return HAL_OK;
  456. }
  457.  
  458. /**
  459.   * @brief  De-Initializes the SD card.
  460.   * @param  hsd: Pointer to SD handle
  461.   * @retval HAL status
  462.   */
  463. HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
  464. {
  465.   /* Check the SD handle allocation */
  466.   if(hsd == NULL)
  467.   {
  468.     return HAL_ERROR;
  469.   }
  470.  
  471.   /* Check the parameters */
  472.   assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance));
  473.  
  474.   hsd->State = HAL_SD_STATE_BUSY;
  475.  
  476.   /* Set SD power state to off */
  477.   SD_PowerOFF(hsd);
  478.  
  479. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  480.   if(hsd->MspDeInitCallback == NULL)
  481.   {
  482.     hsd->MspDeInitCallback = HAL_SD_MspDeInit;
  483.   }
  484.  
  485.   /* DeInit the low level hardware */
  486.   hsd->MspDeInitCallback(hsd);
  487. #else
  488.   /* De-Initialize the MSP layer */
  489.   HAL_SD_MspDeInit(hsd);
  490. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  491.  
  492.   hsd->ErrorCode = HAL_SD_ERROR_NONE;
  493.   hsd->State = HAL_SD_STATE_RESET;
  494.  
  495.   return HAL_OK;
  496. }
  497.  
  498.  
  499. /**
  500.   * @brief  Initializes the SD MSP.
  501.   * @param  hsd: Pointer to SD handle
  502.   * @retval None
  503.   */
  504. __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
  505. {
  506.   /* Prevent unused argument(s) compilation warning */
  507.   UNUSED(hsd);
  508.  
  509.   /* NOTE : This function should not be modified, when the callback is needed,
  510.             the HAL_SD_MspInit could be implemented in the user file
  511.    */
  512. }
  513.  
  514. /**
  515.   * @brief  De-Initialize SD MSP.
  516.   * @param  hsd: Pointer to SD handle
  517.   * @retval None
  518.   */
  519. __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
  520. {
  521.   /* Prevent unused argument(s) compilation warning */
  522.   UNUSED(hsd);
  523.  
  524.   /* NOTE : This function should not be modified, when the callback is needed,
  525.             the HAL_SD_MspDeInit could be implemented in the user file
  526.    */
  527. }
  528.  
  529. /**
  530.   * @}
  531.   */
  532.  
  533. /** @addtogroup SD_Exported_Functions_Group2
  534.  *  @brief   Data transfer functions
  535.  *
  536. @verbatim
  537.   ==============================================================================
  538.                         ##### IO operation functions #####
  539.   ==============================================================================
  540.   [..]
  541.     This subsection provides a set of functions allowing to manage the data
  542.     transfer from/to SD card.
  543.  
  544. @endverbatim
  545.   * @{
  546.   */
  547.  
  548. /**
  549.   * @brief  Reads block(s) from a specified address in a card. The Data transfer
  550.   *         is managed by polling mode.
  551.   * @note   This API should be followed by a check on the card state through
  552.   *         HAL_SD_GetCardState().
  553.   * @param  hsd: Pointer to SD handle
  554.   * @param  pData: pointer to the buffer that will contain the received data
  555.   * @param  BlockAdd: Block Address from where data is to be read
  556.   * @param  NumberOfBlocks: Number of SD blocks to read
  557.   * @param  Timeout: Specify timeout value
  558.   * @retval HAL status
  559.   */
  560. HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
  561. {
  562.   SDIO_DataInitTypeDef config;
  563.   uint32_t errorstate;
  564.   uint32_t tickstart = HAL_GetTick();
  565.   uint32_t count, data, dataremaining;
  566.   uint32_t add = BlockAdd;
  567.   uint8_t *tempbuff = pData;
  568.  
  569.   if(NULL == pData)
  570.   {
  571.     hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  572.     return HAL_ERROR;
  573.   }
  574.  
  575.   if(hsd->State == HAL_SD_STATE_READY)
  576.   {
  577.     hsd->ErrorCode = HAL_SD_ERROR_NONE;
  578.  
  579.     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  580.     {
  581.       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  582.       return HAL_ERROR;
  583.     }
  584.  
  585.     hsd->State = HAL_SD_STATE_BUSY;
  586.  
  587.     /* Initialize data control register */
  588.     hsd->Instance->DCTRL = 0U;
  589.  
  590.     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  591.     {
  592.       add *= 512U;
  593.     }
  594.  
  595.     /* Set Block Size for Card */
  596.     errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  597.     if(errorstate != HAL_SD_ERROR_NONE)
  598.     {
  599.       /* Clear all the static flags */
  600.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  601.       hsd->ErrorCode |= errorstate;
  602.       hsd->State = HAL_SD_STATE_READY;
  603.       return HAL_ERROR;
  604.     }
  605.  
  606.     /* Configure the SD DPSM (Data Path State Machine) */
  607.     config.DataTimeOut   = SDMMC_DATATIMEOUT;
  608.     config.DataLength    = NumberOfBlocks * BLOCKSIZE;
  609.     config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  610.     config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
  611.     config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
  612.     config.DPSM          = SDIO_DPSM_ENABLE;
  613.     (void)SDIO_ConfigData(hsd->Instance, &config);
  614.  
  615.     /* Read block(s) in polling mode */
  616.     if(NumberOfBlocks > 1U)
  617.     {
  618.       hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
  619.  
  620.       /* Read Multi Block command */
  621.       errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
  622.     }
  623.     else
  624.     {
  625.       hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
  626.  
  627.       /* Read Single Block command */
  628.       errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
  629.     }
  630.     if(errorstate != HAL_SD_ERROR_NONE)
  631.     {
  632.       /* Clear all the static flags */
  633.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  634.       hsd->ErrorCode |= errorstate;
  635.       hsd->State = HAL_SD_STATE_READY;
  636.       hsd->Context = SD_CONTEXT_NONE;
  637.       return HAL_ERROR;
  638.     }
  639.  
  640.     /* Poll on SDIO flags */
  641.     dataremaining = config.DataLength;
  642.     while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
  643.     {
  644.       if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) && (dataremaining > 0U))
  645.       {
  646.         /* Read data from SDIO Rx FIFO */
  647.         for(count = 0U; count < 8U; count++)
  648.         {
  649.           data = SDIO_ReadFIFO(hsd->Instance);
  650.           *tempbuff = (uint8_t)(data & 0xFFU);
  651.           tempbuff++;
  652.           dataremaining--;
  653.           *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
  654.           tempbuff++;
  655.           dataremaining--;
  656.           *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
  657.           tempbuff++;
  658.           dataremaining--;
  659.           *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
  660.           tempbuff++;
  661.           dataremaining--;
  662.         }
  663.       }
  664.  
  665.       if(((HAL_GetTick()-tickstart) >=  Timeout) || (Timeout == 0U))
  666.       {
  667.         /* Clear all the static flags */
  668.         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  669.         hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
  670.         hsd->State= HAL_SD_STATE_READY;
  671.         hsd->Context = SD_CONTEXT_NONE;
  672.         return HAL_TIMEOUT;
  673.       }
  674.     }
  675.    
  676.     /* Send stop transmission command in case of multiblock read */
  677.     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  678.     {
  679.       if(hsd->SdCard.CardType != CARD_SECURED)
  680.       {
  681.         /* Send stop transmission command */
  682.         errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  683.         if(errorstate != HAL_SD_ERROR_NONE)
  684.         {
  685.           /* Clear all the static flags */
  686.           __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  687.           hsd->ErrorCode |= errorstate;
  688.           hsd->State = HAL_SD_STATE_READY;
  689.           hsd->Context = SD_CONTEXT_NONE;
  690.           return HAL_ERROR;
  691.         }
  692.       }
  693.     }
  694.  
  695.     /* Get error state */
  696.     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
  697.     {
  698.       /* Clear all the static flags */
  699.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  700.       hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  701.       hsd->State = HAL_SD_STATE_READY;
  702.       hsd->Context = SD_CONTEXT_NONE;
  703.       return HAL_ERROR;
  704.     }
  705.     else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
  706.     {
  707.       /* Clear all the static flags */
  708.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  709.       hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  710.       hsd->State = HAL_SD_STATE_READY;
  711.       hsd->Context = SD_CONTEXT_NONE;
  712.       return HAL_ERROR;
  713.     }
  714.     else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
  715.     {
  716.       /* Clear all the static flags */
  717.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  718.       hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
  719.       hsd->State = HAL_SD_STATE_READY;
  720.       hsd->Context = SD_CONTEXT_NONE;
  721.       return HAL_ERROR;
  722.     }
  723.     else
  724.     {
  725.       /* Nothing to do */
  726.     }
  727.  
  728.     /* Empty FIFO if there is still any data */
  729.     while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (dataremaining > 0U))
  730.     {
  731.       data = SDIO_ReadFIFO(hsd->Instance);
  732.       *tempbuff = (uint8_t)(data & 0xFFU);
  733.       tempbuff++;
  734.       dataremaining--;
  735.       *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
  736.       tempbuff++;
  737.       dataremaining--;
  738.       *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
  739.       tempbuff++;
  740.       dataremaining--;
  741.       *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
  742.       tempbuff++;
  743.       dataremaining--;
  744.  
  745.       if(((HAL_GetTick()-tickstart) >=  Timeout) || (Timeout == 0U))
  746.       {
  747.         /* Clear all the static flags */
  748.         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  749.         hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
  750.         hsd->State= HAL_SD_STATE_READY;
  751.         hsd->Context = SD_CONTEXT_NONE;
  752.         return HAL_ERROR;
  753.       }
  754.     }
  755.  
  756.     /* Clear all the static flags */
  757.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  758.  
  759.     hsd->State = HAL_SD_STATE_READY;
  760.  
  761.     return HAL_OK;
  762.   }
  763.   else
  764.   {
  765.     hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
  766.     return HAL_ERROR;
  767.   }
  768. }
  769.  
  770. /**
  771.   * @brief  Allows to write block(s) to a specified address in a card. The Data
  772.   *         transfer is managed by polling mode.
  773.   * @note   This API should be followed by a check on the card state through
  774.   *         HAL_SD_GetCardState().
  775.   * @param  hsd: Pointer to SD handle
  776.   * @param  pData: pointer to the buffer that will contain the data to transmit
  777.   * @param  BlockAdd: Block Address where data will be written
  778.   * @param  NumberOfBlocks: Number of SD blocks to write
  779.   * @param  Timeout: Specify timeout value
  780.   * @note   Due to limitation "SDIO hardware flow control" indicated in Errata Sheet :
  781.   *         In 4-bits bus wide mode, do not use this API otherwise underrun will occur and
  782.   *         there is not possibility to activate the flow control.
  783.   *         Use DMA mode when using 4-bits bus wide mode or decrease the frequency.
  784.   * @retval HAL status
  785.   */
  786. HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
  787. {
  788.   SDIO_DataInitTypeDef config;
  789.   uint32_t errorstate;
  790.   uint32_t tickstart = HAL_GetTick();
  791.   uint32_t count, data, dataremaining;
  792.   uint32_t add = BlockAdd;
  793.   uint8_t *tempbuff = pData;
  794.  
  795.   if(NULL == pData)
  796.   {
  797.     hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  798.     return HAL_ERROR;
  799.   }
  800.  
  801.   if(hsd->State == HAL_SD_STATE_READY)
  802.   {
  803.     hsd->ErrorCode = HAL_SD_ERROR_NONE;
  804.  
  805.     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  806.     {
  807.       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  808.       return HAL_ERROR;
  809.     }
  810.  
  811.     hsd->State = HAL_SD_STATE_BUSY;
  812.  
  813.     /* Initialize data control register */
  814.     hsd->Instance->DCTRL = 0U;
  815.  
  816.     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  817.     {
  818.       add *= 512U;
  819.     }
  820.  
  821.     /* Set Block Size for Card */
  822.     errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  823.     if(errorstate != HAL_SD_ERROR_NONE)
  824.     {
  825.       /* Clear all the static flags */
  826.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  827.       hsd->ErrorCode |= errorstate;
  828.       hsd->State = HAL_SD_STATE_READY;
  829.       return HAL_ERROR;
  830.     }
  831.  
  832.     /* Configure the SD DPSM (Data Path State Machine) */
  833.     config.DataTimeOut   = SDMMC_DATATIMEOUT;
  834.     config.DataLength    = NumberOfBlocks * BLOCKSIZE;
  835.     config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  836.     config.TransferDir   = SDIO_TRANSFER_DIR_TO_CARD;
  837.     config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
  838.     config.DPSM          = SDIO_DPSM_ENABLE;
  839.     (void)SDIO_ConfigData(hsd->Instance, &config);
  840.  
  841.     /* Write Blocks in Polling mode */
  842.     if(NumberOfBlocks > 1U)
  843.     {
  844.       hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
  845.  
  846.       /* Write Multi Block command */
  847.       errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
  848.     }
  849.     else
  850.     {
  851.       hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
  852.  
  853.       /* Write Single Block command */
  854.       errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
  855.     }
  856.     if(errorstate != HAL_SD_ERROR_NONE)
  857.     {
  858.       /* Clear all the static flags */
  859.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  860.       hsd->ErrorCode |= errorstate;
  861.       hsd->State = HAL_SD_STATE_READY;
  862.       hsd->Context = SD_CONTEXT_NONE;
  863.       return HAL_ERROR;
  864.     }
  865.  
  866.     /* Write block(s) in polling mode */
  867.     dataremaining = config.DataLength;
  868.     while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
  869.     {
  870.       if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) && (dataremaining > 0U))
  871.       {
  872.         /* Write data to SDIO Tx FIFO */
  873.         for(count = 0U; count < 8U; count++)
  874.         {
  875.           data = (uint32_t)(*tempbuff);
  876.           tempbuff++;
  877.           dataremaining--;
  878.           data |= ((uint32_t)(*tempbuff) << 8U);
  879.           tempbuff++;
  880.           dataremaining--;
  881.           data |= ((uint32_t)(*tempbuff) << 16U);
  882.           tempbuff++;
  883.           dataremaining--;
  884.           data |= ((uint32_t)(*tempbuff) << 24U);
  885.           tempbuff++;
  886.           dataremaining--;
  887.           (void)SDIO_WriteFIFO(hsd->Instance, &data);
  888.         }
  889.       }
  890.  
  891.       if(((HAL_GetTick()-tickstart) >=  Timeout) || (Timeout == 0U))
  892.       {
  893.         /* Clear all the static flags */
  894.         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  895.         hsd->ErrorCode |= errorstate;
  896.         hsd->State = HAL_SD_STATE_READY;
  897.         hsd->Context = SD_CONTEXT_NONE;
  898.         return HAL_TIMEOUT;
  899.       }
  900.     }
  901.  
  902.     /* Send stop transmission command in case of multiblock write */
  903.     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
  904.     {
  905.       if(hsd->SdCard.CardType != CARD_SECURED)
  906.       {
  907.         /* Send stop transmission command */
  908.         errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  909.         if(errorstate != HAL_SD_ERROR_NONE)
  910.         {
  911.           /* Clear all the static flags */
  912.           __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  913.           hsd->ErrorCode |= errorstate;
  914.           hsd->State = HAL_SD_STATE_READY;
  915.           hsd->Context = SD_CONTEXT_NONE;
  916.           return HAL_ERROR;
  917.         }
  918.       }
  919.     }
  920.  
  921.     /* Get error state */
  922.     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
  923.     {
  924.       /* Clear all the static flags */
  925.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  926.       hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  927.       hsd->State = HAL_SD_STATE_READY;
  928.       hsd->Context = SD_CONTEXT_NONE;
  929.       return HAL_ERROR;
  930.     }
  931.     else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
  932.     {
  933.       /* Clear all the static flags */
  934.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  935.       hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  936.       hsd->State = HAL_SD_STATE_READY;
  937.       hsd->Context = SD_CONTEXT_NONE;
  938.       return HAL_ERROR;
  939.     }
  940.     else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
  941.     {
  942.       /* Clear all the static flags */
  943.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  944.       hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
  945.       hsd->State = HAL_SD_STATE_READY;
  946.       hsd->Context = SD_CONTEXT_NONE;
  947.       return HAL_ERROR;
  948.     }
  949.     else
  950.     {
  951.       /* Nothing to do */
  952.     }
  953.  
  954.     /* Clear all the static flags */
  955.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  956.  
  957.     hsd->State = HAL_SD_STATE_READY;
  958.  
  959.     return HAL_OK;
  960.   }
  961.   else
  962.   {
  963.     hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
  964.     return HAL_ERROR;
  965.   }
  966. }
  967.  
  968. /**
  969.   * @brief  Reads block(s) from a specified address in a card. The Data transfer
  970.   *         is managed in interrupt mode.
  971.   * @note   This API should be followed by a check on the card state through
  972.   *         HAL_SD_GetCardState().
  973.   * @note   You could also check the IT transfer process through the SD Rx
  974.   *         interrupt event.
  975.   * @param  hsd: Pointer to SD handle
  976.   * @param  pData: Pointer to the buffer that will contain the received data
  977.   * @param  BlockAdd: Block Address from where data is to be read
  978.   * @param  NumberOfBlocks: Number of blocks to read.
  979.   * @retval HAL status
  980.   */
  981. HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  982. {
  983.   SDIO_DataInitTypeDef config;
  984.   uint32_t errorstate;
  985.   uint32_t add = BlockAdd;
  986.  
  987.   if(NULL == pData)
  988.   {
  989.     hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  990.     return HAL_ERROR;
  991.   }
  992.  
  993.   if(hsd->State == HAL_SD_STATE_READY)
  994.   {
  995.     hsd->ErrorCode = HAL_SD_ERROR_NONE;
  996.  
  997.     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  998.     {
  999.       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1000.       return HAL_ERROR;
  1001.     }
  1002.  
  1003.     hsd->State = HAL_SD_STATE_BUSY;
  1004.  
  1005.     /* Initialize data control register */
  1006.     hsd->Instance->DCTRL = 0U;
  1007.  
  1008.     hsd->pRxBuffPtr = pData;
  1009.     hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
  1010.  
  1011.     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF | SDIO_IT_STBITERR));
  1012.  
  1013.     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1014.     {
  1015.       add *= 512U;
  1016.     }
  1017.  
  1018.     /* Set Block Size for Card */
  1019.     errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  1020.     if(errorstate != HAL_SD_ERROR_NONE)
  1021.     {
  1022.       /* Clear all the static flags */
  1023.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1024.       hsd->ErrorCode |= errorstate;
  1025.       hsd->State = HAL_SD_STATE_READY;
  1026.       return HAL_ERROR;
  1027.     }
  1028.  
  1029.     /* Configure the SD DPSM (Data Path State Machine) */
  1030.     config.DataTimeOut   = SDMMC_DATATIMEOUT;
  1031.     config.DataLength    = BLOCKSIZE * NumberOfBlocks;
  1032.     config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  1033.     config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
  1034.     config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
  1035.     config.DPSM          = SDIO_DPSM_ENABLE;
  1036.     (void)SDIO_ConfigData(hsd->Instance, &config);
  1037.  
  1038.     /* Read Blocks in IT mode */
  1039.     if(NumberOfBlocks > 1U)
  1040.     {
  1041.       hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
  1042.  
  1043.       /* Read Multi Block command */
  1044.       errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
  1045.     }
  1046.     else
  1047.     {
  1048.       hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
  1049.  
  1050.       /* Read Single Block command */
  1051.       errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
  1052.     }
  1053.     if(errorstate != HAL_SD_ERROR_NONE)
  1054.     {
  1055.       /* Clear all the static flags */
  1056.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1057.       hsd->ErrorCode |= errorstate;
  1058.       hsd->State = HAL_SD_STATE_READY;
  1059.       hsd->Context = SD_CONTEXT_NONE;
  1060.       return HAL_ERROR;
  1061.     }
  1062.  
  1063.     return HAL_OK;
  1064.   }
  1065.   else
  1066.   {
  1067.     return HAL_BUSY;
  1068.   }
  1069. }
  1070.  
  1071. /**
  1072.   * @brief  Writes block(s) to a specified address in a card. The Data transfer
  1073.   *         is managed in interrupt mode.
  1074.   * @note   This API should be followed by a check on the card state through
  1075.   *         HAL_SD_GetCardState().
  1076.   * @note   You could also check the IT transfer process through the SD Tx
  1077.   *         interrupt event.
  1078.   * @param  hsd: Pointer to SD handle
  1079.   * @param  pData: Pointer to the buffer that will contain the data to transmit
  1080.   * @param  BlockAdd: Block Address where data will be written
  1081.   * @param  NumberOfBlocks: Number of blocks to write
  1082.   * @note   Due to limitation "SDIO hardware flow control" indicated in Errata Sheet :
  1083.   *         In 4-bits bus wide mode, do not use this API otherwise underrun will occur and
  1084.   *         there is not possibility to activate the flow control.
  1085.   *         Use DMA mode when using 4-bits bus wide mode or decrease the frequency.
  1086.   * @retval HAL status
  1087.   */
  1088. HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1089. {
  1090.   SDIO_DataInitTypeDef config;
  1091.   uint32_t errorstate;
  1092.   uint32_t add = BlockAdd;
  1093.  
  1094.   if(NULL == pData)
  1095.   {
  1096.     hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1097.     return HAL_ERROR;
  1098.   }
  1099.  
  1100.   if(hsd->State == HAL_SD_STATE_READY)
  1101.   {
  1102.     hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1103.  
  1104.     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  1105.     {
  1106.       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1107.       return HAL_ERROR;
  1108.     }
  1109.  
  1110.     hsd->State = HAL_SD_STATE_BUSY;
  1111.  
  1112.     /* Initialize data control register */
  1113.     hsd->Instance->DCTRL = 0U;
  1114.  
  1115.     hsd->pTxBuffPtr = pData;
  1116.     hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
  1117.  
  1118.     /* Enable transfer interrupts */
  1119.     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_STBITERR));
  1120.  
  1121.     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1122.     {
  1123.       add *= 512U;
  1124.     }
  1125.  
  1126.     /* Set Block Size for Card */
  1127.     errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  1128.     if(errorstate != HAL_SD_ERROR_NONE)
  1129.     {
  1130.       /* Clear all the static flags */
  1131.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1132.       hsd->ErrorCode |= errorstate;
  1133.       hsd->State = HAL_SD_STATE_READY;
  1134.       return HAL_ERROR;
  1135.     }
  1136.  
  1137.     /* Write Blocks in Polling mode */
  1138.     if(NumberOfBlocks > 1U)
  1139.     {
  1140.       hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
  1141.  
  1142.       /* Write Multi Block command */
  1143.       errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
  1144.     }
  1145.     else
  1146.     {
  1147.       hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
  1148.  
  1149.       /* Write Single Block command */
  1150.       errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
  1151.     }
  1152.     if(errorstate != HAL_SD_ERROR_NONE)
  1153.     {
  1154.       /* Clear all the static flags */
  1155.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1156.       hsd->ErrorCode |= errorstate;
  1157.       hsd->State = HAL_SD_STATE_READY;
  1158.       hsd->Context = SD_CONTEXT_NONE;
  1159.       return HAL_ERROR;
  1160.     }
  1161.  
  1162.     /* Configure the SD DPSM (Data Path State Machine) */
  1163.     config.DataTimeOut   = SDMMC_DATATIMEOUT;
  1164.     config.DataLength    = BLOCKSIZE * NumberOfBlocks;
  1165.     config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  1166.     config.TransferDir   = SDIO_TRANSFER_DIR_TO_CARD;
  1167.     config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
  1168.     config.DPSM          = SDIO_DPSM_ENABLE;
  1169.     (void)SDIO_ConfigData(hsd->Instance, &config);
  1170.  
  1171.     return HAL_OK;
  1172.   }
  1173.   else
  1174.   {
  1175.     return HAL_BUSY;
  1176.   }
  1177. }
  1178.  
  1179. /**
  1180.   * @brief  Reads block(s) from a specified address in a card. The Data transfer
  1181.   *         is managed by DMA mode.
  1182.   * @note   This API should be followed by a check on the card state through
  1183.   *         HAL_SD_GetCardState().
  1184.   * @note   You could also check the DMA transfer process through the SD Rx
  1185.   *         interrupt event.
  1186.   * @param  hsd: Pointer SD handle
  1187.   * @param  pData: Pointer to the buffer that will contain the received data
  1188.   * @param  BlockAdd: Block Address from where data is to be read
  1189.   * @param  NumberOfBlocks: Number of blocks to read.
  1190.   * @retval HAL status
  1191.   */
  1192. HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1193. {
  1194.   SDIO_DataInitTypeDef config;
  1195.   uint32_t errorstate;
  1196.   uint32_t add = BlockAdd;
  1197.  
  1198.   if(NULL == pData)
  1199.   {
  1200.     hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1201.     return HAL_ERROR;
  1202.   }
  1203.  
  1204.   if(hsd->State == HAL_SD_STATE_READY)
  1205.   {
  1206.     hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1207.  
  1208.     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  1209.     {
  1210.       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1211.       return HAL_ERROR;
  1212.     }
  1213.  
  1214.     hsd->State = HAL_SD_STATE_BUSY;
  1215.  
  1216.     /* Initialize data control register */
  1217.     hsd->Instance->DCTRL = 0U;
  1218.  
  1219.     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_IT_STBITERR));
  1220.  
  1221.     /* Set the DMA transfer complete callback */
  1222.     hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
  1223.  
  1224.     /* Set the DMA error callback */
  1225.     hsd->hdmarx->XferErrorCallback = SD_DMAError;
  1226.  
  1227.     /* Set the DMA Abort callback */
  1228.     hsd->hdmarx->XferAbortCallback = NULL;
  1229.  
  1230.     /* Enable the DMA Channel */
  1231.     if(HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
  1232.     {
  1233.       __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
  1234.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1235.       hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  1236.       hsd->State = HAL_SD_STATE_READY;
  1237.       return HAL_ERROR;
  1238.     }
  1239.     else
  1240.     {
  1241.       /* Enable SD DMA transfer */
  1242.       __HAL_SD_DMA_ENABLE(hsd);
  1243.  
  1244.       if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1245.       {
  1246.         add *= 512U;
  1247.       }
  1248.  
  1249.       /* Set Block Size for Card */
  1250.       errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  1251.       if(errorstate != HAL_SD_ERROR_NONE)
  1252.       {
  1253.         /* Clear all the static flags */
  1254.         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1255.         hsd->ErrorCode |= errorstate;
  1256.         hsd->State = HAL_SD_STATE_READY;
  1257.         return HAL_ERROR;
  1258.       }
  1259.  
  1260.       /* Configure the SD DPSM (Data Path State Machine) */
  1261.       config.DataTimeOut   = SDMMC_DATATIMEOUT;
  1262.       config.DataLength    = BLOCKSIZE * NumberOfBlocks;
  1263.       config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  1264.       config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
  1265.       config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
  1266.       config.DPSM          = SDIO_DPSM_ENABLE;
  1267.       (void)SDIO_ConfigData(hsd->Instance, &config);
  1268.  
  1269.       /* Read Blocks in DMA mode */
  1270.       if(NumberOfBlocks > 1U)
  1271.       {
  1272.         hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  1273.  
  1274.         /* Read Multi Block command */
  1275.         errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
  1276.       }
  1277.       else
  1278.       {
  1279.         hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
  1280.  
  1281.         /* Read Single Block command */
  1282.         errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
  1283.       }
  1284.       if(errorstate != HAL_SD_ERROR_NONE)
  1285.       {
  1286.         /* Clear all the static flags */
  1287.         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1288.         hsd->ErrorCode |= errorstate;
  1289.         hsd->State = HAL_SD_STATE_READY;
  1290.         hsd->Context = SD_CONTEXT_NONE;
  1291.         return HAL_ERROR;
  1292.       }
  1293.  
  1294.       return HAL_OK;
  1295.     }
  1296.   }
  1297.   else
  1298.   {
  1299.     return HAL_BUSY;
  1300.   }
  1301. }
  1302.  
  1303. /**
  1304.   * @brief  Writes block(s) to a specified address in a card. The Data transfer
  1305.   *         is managed by DMA mode.
  1306.   * @note   This API should be followed by a check on the card state through
  1307.   *         HAL_SD_GetCardState().
  1308.   * @note   You could also check the DMA transfer process through the SD Tx
  1309.   *         interrupt event.
  1310.   * @param  hsd: Pointer to SD handle
  1311.   * @param  pData: Pointer to the buffer that will contain the data to transmit
  1312.   * @param  BlockAdd: Block Address where data will be written
  1313.   * @param  NumberOfBlocks: Number of blocks to write
  1314.   * @retval HAL status
  1315.   */
  1316. HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
  1317. {
  1318.   SDIO_DataInitTypeDef config;
  1319.   uint32_t errorstate;
  1320.   uint32_t add = BlockAdd;
  1321.  
  1322.   if(NULL == pData)
  1323.   {
  1324.     hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1325.     return HAL_ERROR;
  1326.   }
  1327.  
  1328.   if(hsd->State == HAL_SD_STATE_READY)
  1329.   {
  1330.     hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1331.  
  1332.     if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
  1333.     {
  1334.       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1335.       return HAL_ERROR;
  1336.     }
  1337.  
  1338.     hsd->State = HAL_SD_STATE_BUSY;
  1339.  
  1340.     /* Initialize data control register */
  1341.     hsd->Instance->DCTRL = 0U;
  1342.  
  1343.     /* Enable SD Error interrupts */
  1344.     __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));
  1345.  
  1346.     /* Set the DMA transfer complete callback */
  1347.     hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
  1348.  
  1349.     /* Set the DMA error callback */
  1350.     hsd->hdmatx->XferErrorCallback = SD_DMAError;
  1351.  
  1352.     /* Set the DMA Abort callback */
  1353.     hsd->hdmatx->XferAbortCallback = NULL;
  1354.  
  1355.     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1356.     {
  1357.       add *= 512U;
  1358.     }
  1359.  
  1360.     /* Set Block Size for Card */
  1361.     errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
  1362.     if(errorstate != HAL_SD_ERROR_NONE)
  1363.     {
  1364.       /* Clear all the static flags */
  1365.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1366.       hsd->ErrorCode |= errorstate;
  1367.       hsd->State = HAL_SD_STATE_READY;
  1368.       return HAL_ERROR;
  1369.     }
  1370.  
  1371.     /* Write Blocks in Polling mode */
  1372.     if(NumberOfBlocks > 1U)
  1373.     {
  1374.       hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
  1375.  
  1376.       /* Write Multi Block command */
  1377.       errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
  1378.     }
  1379.     else
  1380.     {
  1381.       hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
  1382.  
  1383.       /* Write Single Block command */
  1384.       errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
  1385.     }
  1386.     if(errorstate != HAL_SD_ERROR_NONE)
  1387.     {
  1388.       /* Clear all the static flags */
  1389.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1390.       hsd->ErrorCode |= errorstate;
  1391.       hsd->State = HAL_SD_STATE_READY;
  1392.       hsd->Context = SD_CONTEXT_NONE;
  1393.       return HAL_ERROR;
  1394.     }
  1395.  
  1396.     /* Enable SDIO DMA transfer */
  1397.     __HAL_SD_DMA_ENABLE(hsd);
  1398.  
  1399.     /* Enable the DMA Channel */
  1400.     if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
  1401.     {
  1402.       __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));
  1403.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1404.       hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  1405.       hsd->State = HAL_SD_STATE_READY;
  1406.       hsd->Context = SD_CONTEXT_NONE;
  1407.       return HAL_ERROR;
  1408.     }
  1409.     else
  1410.     {
  1411.       /* Configure the SD DPSM (Data Path State Machine) */
  1412.       config.DataTimeOut   = SDMMC_DATATIMEOUT;
  1413.       config.DataLength    = BLOCKSIZE * NumberOfBlocks;
  1414.       config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
  1415.       config.TransferDir   = SDIO_TRANSFER_DIR_TO_CARD;
  1416.       config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
  1417.       config.DPSM          = SDIO_DPSM_ENABLE;
  1418.       (void)SDIO_ConfigData(hsd->Instance, &config);
  1419.  
  1420.       return HAL_OK;
  1421.     }
  1422.   }
  1423.   else
  1424.   {
  1425.     return HAL_BUSY;
  1426.   }
  1427. }
  1428.  
  1429. /**
  1430.   * @brief  Erases the specified memory area of the given SD card.
  1431.   * @note   This API should be followed by a check on the card state through
  1432.   *         HAL_SD_GetCardState().
  1433.   * @param  hsd: Pointer to SD handle
  1434.   * @param  BlockStartAdd: Start Block address
  1435.   * @param  BlockEndAdd: End Block address
  1436.   * @retval HAL status
  1437.   */
  1438. HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
  1439. {
  1440.   uint32_t errorstate;
  1441.   uint32_t start_add = BlockStartAdd;
  1442.   uint32_t end_add = BlockEndAdd;
  1443.  
  1444.   if(hsd->State == HAL_SD_STATE_READY)
  1445.   {
  1446.     hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1447.  
  1448.     if(end_add < start_add)
  1449.     {
  1450.       hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  1451.       return HAL_ERROR;
  1452.     }
  1453.  
  1454.     if(end_add > (hsd->SdCard.LogBlockNbr))
  1455.     {
  1456.       hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
  1457.       return HAL_ERROR;
  1458.     }
  1459.  
  1460.     hsd->State = HAL_SD_STATE_BUSY;
  1461.  
  1462.     /* Check if the card command class supports erase command */
  1463.     if(((hsd->SdCard.Class) & SDIO_CCCC_ERASE) == 0U)
  1464.     {
  1465.       /* Clear all the static flags */
  1466.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1467.       hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  1468.       hsd->State = HAL_SD_STATE_READY;
  1469.       return HAL_ERROR;
  1470.     }
  1471.  
  1472.     if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  1473.     {
  1474.       /* Clear all the static flags */
  1475.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1476.       hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  1477.       hsd->State = HAL_SD_STATE_READY;
  1478.       return HAL_ERROR;
  1479.     }
  1480.  
  1481.     /* Get start and end block for high capacity cards */
  1482.     if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
  1483.     {
  1484.       start_add *= 512U;
  1485.       end_add   *= 512U;
  1486.     }
  1487.  
  1488.     /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
  1489.     if(hsd->SdCard.CardType != CARD_SECURED)
  1490.     {
  1491.       /* Send CMD32 SD_ERASE_GRP_START with argument as addr  */
  1492.       errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
  1493.       if(errorstate != HAL_SD_ERROR_NONE)
  1494.       {
  1495.         /* Clear all the static flags */
  1496.         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1497.         hsd->ErrorCode |= errorstate;
  1498.         hsd->State = HAL_SD_STATE_READY;
  1499.         return HAL_ERROR;
  1500.       }
  1501.  
  1502.       /* Send CMD33 SD_ERASE_GRP_END with argument as addr  */
  1503.       errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
  1504.       if(errorstate != HAL_SD_ERROR_NONE)
  1505.       {
  1506.         /* Clear all the static flags */
  1507.         __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1508.         hsd->ErrorCode |= errorstate;
  1509.         hsd->State = HAL_SD_STATE_READY;
  1510.         return HAL_ERROR;
  1511.       }
  1512.     }
  1513.  
  1514.     /* Send CMD38 ERASE */
  1515.     errorstate = SDMMC_CmdErase(hsd->Instance);
  1516.     if(errorstate != HAL_SD_ERROR_NONE)
  1517.     {
  1518.       /* Clear all the static flags */
  1519.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  1520.       hsd->ErrorCode |= errorstate;
  1521.       hsd->State = HAL_SD_STATE_READY;
  1522.       return HAL_ERROR;
  1523.     }
  1524.  
  1525.     hsd->State = HAL_SD_STATE_READY;
  1526.  
  1527.     return HAL_OK;
  1528.   }
  1529.   else
  1530.   {
  1531.     return HAL_BUSY;
  1532.   }
  1533. }
  1534.  
  1535. /**
  1536.   * @brief  This function handles SD card interrupt request.
  1537.   * @param  hsd: Pointer to SD handle
  1538.   * @retval None
  1539.   */
  1540. void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
  1541. {
  1542.   uint32_t errorstate;
  1543.   uint32_t context = hsd->Context;
  1544.  
  1545.   /* Check for SDIO interrupt flags */
  1546.   if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
  1547.   {
  1548.     SD_Read_IT(hsd);
  1549.   }
  1550.  
  1551.   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) != RESET)
  1552.   {
  1553.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DATAEND);
  1554.  
  1555.     __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND  | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  1556.                              SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR  | SDIO_IT_TXFIFOHE |\
  1557.                              SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR);
  1558.  
  1559.     hsd->Instance->DCTRL &= ~(SDIO_DCTRL_DTEN);
  1560.    
  1561.     if((context & SD_CONTEXT_IT) != 0U)
  1562.     {
  1563.       if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  1564.       {
  1565.         errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  1566.         if(errorstate != HAL_SD_ERROR_NONE)
  1567.         {
  1568.           hsd->ErrorCode |= errorstate;
  1569. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1570.           hsd->ErrorCallback(hsd);
  1571. #else
  1572.           HAL_SD_ErrorCallback(hsd);
  1573. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1574.         }
  1575.       }
  1576.  
  1577.       /* Clear all the static flags */
  1578.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  1579.  
  1580.       hsd->State = HAL_SD_STATE_READY;
  1581.       hsd->Context = SD_CONTEXT_NONE;
  1582.       if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  1583.       {
  1584. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1585.         hsd->RxCpltCallback(hsd);
  1586. #else
  1587.         HAL_SD_RxCpltCallback(hsd);
  1588. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1589.       }
  1590.       else
  1591.       {
  1592. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1593.         hsd->TxCpltCallback(hsd);
  1594. #else
  1595.         HAL_SD_TxCpltCallback(hsd);
  1596. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1597.       }
  1598.     }
  1599.     else if((context & SD_CONTEXT_DMA) != 0U)
  1600.     {
  1601.       if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
  1602.       {
  1603.         errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  1604.         if(errorstate != HAL_SD_ERROR_NONE)
  1605.         {
  1606.           hsd->ErrorCode |= errorstate;
  1607. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1608.           hsd->ErrorCallback(hsd);
  1609. #else
  1610.           HAL_SD_ErrorCallback(hsd);
  1611. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1612.         }
  1613.       }
  1614.       if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
  1615.       {
  1616.         /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  1617.         in the SD DCTRL register */
  1618.         hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
  1619.  
  1620.         hsd->State = HAL_SD_STATE_READY;
  1621.  
  1622. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1623.         hsd->TxCpltCallback(hsd);
  1624. #else
  1625.         HAL_SD_TxCpltCallback(hsd);
  1626. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1627.       }
  1628.     }
  1629.     else
  1630.     {
  1631.       /* Nothing to do */
  1632.     }
  1633.   }
  1634.  
  1635.   else if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
  1636.   {
  1637.     SD_Write_IT(hsd);
  1638.   }
  1639.  
  1640.   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR | SDIO_FLAG_STBITERR) != RESET)
  1641.   {
  1642.     /* Set Error code */
  1643.     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL) != RESET)
  1644.     {
  1645.       hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
  1646.     }
  1647.     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT) != RESET)
  1648.     {
  1649.       hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  1650.     }
  1651.     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR) != RESET)
  1652.     {
  1653.       hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
  1654.     }
  1655.     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR) != RESET)
  1656.     {
  1657.       hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
  1658.     }
  1659.     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_STBITERR) != RESET)
  1660.     {
  1661.       hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
  1662.     }
  1663.  
  1664.     /* Clear All flags */
  1665.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS | SDIO_FLAG_STBITERR);
  1666.  
  1667.     /* Disable all interrupts */
  1668.     __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  1669.                              SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
  1670.  
  1671.     hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  1672.  
  1673.     if((context & SD_CONTEXT_IT) != 0U)
  1674.     {
  1675.       /* Set the SD state to ready to be able to start again the process */
  1676.       hsd->State = HAL_SD_STATE_READY;
  1677.       hsd->Context = SD_CONTEXT_NONE;
  1678. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1679.       hsd->ErrorCallback(hsd);
  1680. #else
  1681.       HAL_SD_ErrorCallback(hsd);
  1682. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1683.     }
  1684.     else if((context & SD_CONTEXT_DMA) != 0U)
  1685.     {
  1686.       /* Abort the SD DMA channel */
  1687.       if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  1688.       {
  1689.         /* Set the DMA Tx abort callback */
  1690.         hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
  1691.         /* Abort DMA in IT mode */
  1692.         if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
  1693.         {
  1694.           SD_DMATxAbort(hsd->hdmatx);
  1695.         }
  1696.       }
  1697.       else if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  1698.       {
  1699.         /* Set the DMA Rx abort callback */
  1700.         hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
  1701.         /* Abort DMA in IT mode */
  1702.         if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
  1703.         {
  1704.           SD_DMARxAbort(hsd->hdmarx);
  1705.         }
  1706.       }
  1707.       else
  1708.       {
  1709.         hsd->ErrorCode = HAL_SD_ERROR_NONE;
  1710.         hsd->State = HAL_SD_STATE_READY;
  1711.         hsd->Context = SD_CONTEXT_NONE;
  1712. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1713.         hsd->AbortCpltCallback(hsd);
  1714. #else
  1715.         HAL_SD_AbortCallback(hsd);
  1716. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1717.       }
  1718.     }
  1719.     else
  1720.     {
  1721.       /* Nothing to do */
  1722.     }
  1723.   }
  1724.   else
  1725.   {
  1726.     /* Nothing to do */
  1727.   }
  1728. }
  1729.  
  1730. /**
  1731.   * @brief return the SD state
  1732.   * @param hsd: Pointer to sd handle
  1733.   * @retval HAL state
  1734.   */
  1735. HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
  1736. {
  1737.   return hsd->State;
  1738. }
  1739.  
  1740. /**
  1741. * @brief  Return the SD error code
  1742. * @param  hsd : Pointer to a SD_HandleTypeDef structure that contains
  1743.   *              the configuration information.
  1744. * @retval SD Error Code
  1745. */
  1746. uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
  1747. {
  1748.   return hsd->ErrorCode;
  1749. }
  1750.  
  1751. /**
  1752.   * @brief Tx Transfer completed callbacks
  1753.   * @param hsd: Pointer to SD handle
  1754.   * @retval None
  1755.   */
  1756. __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
  1757. {
  1758.   /* Prevent unused argument(s) compilation warning */
  1759.   UNUSED(hsd);
  1760.  
  1761.   /* NOTE : This function should not be modified, when the callback is needed,
  1762.             the HAL_SD_TxCpltCallback can be implemented in the user file
  1763.    */
  1764. }
  1765.  
  1766. /**
  1767.   * @brief Rx Transfer completed callbacks
  1768.   * @param hsd: Pointer SD handle
  1769.   * @retval None
  1770.   */
  1771. __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
  1772. {
  1773.   /* Prevent unused argument(s) compilation warning */
  1774.   UNUSED(hsd);
  1775.  
  1776.   /* NOTE : This function should not be modified, when the callback is needed,
  1777.             the HAL_SD_RxCpltCallback can be implemented in the user file
  1778.    */
  1779. }
  1780.  
  1781. /**
  1782.   * @brief SD error callbacks
  1783.   * @param hsd: Pointer SD handle
  1784.   * @retval None
  1785.   */
  1786. __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
  1787. {
  1788.   /* Prevent unused argument(s) compilation warning */
  1789.   UNUSED(hsd);
  1790.  
  1791.   /* NOTE : This function should not be modified, when the callback is needed,
  1792.             the HAL_SD_ErrorCallback can be implemented in the user file
  1793.    */
  1794. }
  1795.  
  1796. /**
  1797.   * @brief SD Abort callbacks
  1798.   * @param hsd: Pointer SD handle
  1799.   * @retval None
  1800.   */
  1801. __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
  1802. {
  1803.   /* Prevent unused argument(s) compilation warning */
  1804.   UNUSED(hsd);
  1805.  
  1806.   /* NOTE : This function should not be modified, when the callback is needed,
  1807.             the HAL_SD_AbortCallback can be implemented in the user file
  1808.    */
  1809. }
  1810.  
  1811. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  1812. /**
  1813.   * @brief  Register a User SD Callback
  1814.   *         To be used instead of the weak (surcharged) predefined callback
  1815.   * @param hsd : SD handle
  1816.   * @param CallbackID : ID of the callback to be registered
  1817.   *        This parameter can be one of the following values:
  1818.   *          @arg @ref HAL_SD_TX_CPLT_CB_ID    SD Tx Complete Callback ID
  1819.   *          @arg @ref HAL_SD_RX_CPLT_CB_ID    SD Rx Complete Callback ID
  1820.   *          @arg @ref HAL_SD_ERROR_CB_ID      SD Error Callback ID
  1821.   *          @arg @ref HAL_SD_ABORT_CB_ID      SD Abort Callback ID
  1822.   *          @arg @ref HAL_SD_MSP_INIT_CB_ID   SD MspInit Callback ID
  1823.   *          @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
  1824.   * @param pCallback : pointer to the Callback function
  1825.   * @retval status
  1826.   */
  1827. HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
  1828. {
  1829.   HAL_StatusTypeDef status = HAL_OK;
  1830.  
  1831.   if(pCallback == NULL)
  1832.   {
  1833.     /* Update the error code */
  1834.     hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  1835.     return HAL_ERROR;
  1836.   }
  1837.  
  1838.   /* Process locked */
  1839.   __HAL_LOCK(hsd);
  1840.  
  1841.   if(hsd->State == HAL_SD_STATE_READY)
  1842.   {
  1843.     switch (CallbackID)
  1844.     {
  1845.     case HAL_SD_TX_CPLT_CB_ID :
  1846.       hsd->TxCpltCallback = pCallback;
  1847.       break;
  1848.     case HAL_SD_RX_CPLT_CB_ID :
  1849.       hsd->RxCpltCallback = pCallback;
  1850.       break;
  1851.     case HAL_SD_ERROR_CB_ID :
  1852.       hsd->ErrorCallback = pCallback;
  1853.       break;
  1854.     case HAL_SD_ABORT_CB_ID :
  1855.       hsd->AbortCpltCallback = pCallback;
  1856.       break;
  1857.     case HAL_SD_MSP_INIT_CB_ID :
  1858.       hsd->MspInitCallback = pCallback;
  1859.       break;
  1860.     case HAL_SD_MSP_DEINIT_CB_ID :
  1861.       hsd->MspDeInitCallback = pCallback;
  1862.       break;
  1863.     default :
  1864.       /* Update the error code */
  1865.       hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  1866.       /* update return status */
  1867.       status =  HAL_ERROR;
  1868.       break;
  1869.     }
  1870.   }
  1871.   else if (hsd->State == HAL_SD_STATE_RESET)
  1872.   {
  1873.     switch (CallbackID)
  1874.     {
  1875.     case HAL_SD_MSP_INIT_CB_ID :
  1876.       hsd->MspInitCallback = pCallback;
  1877.       break;
  1878.     case HAL_SD_MSP_DEINIT_CB_ID :
  1879.       hsd->MspDeInitCallback = pCallback;
  1880.       break;
  1881.     default :
  1882.       /* Update the error code */
  1883.       hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  1884.       /* update return status */
  1885.       status =  HAL_ERROR;
  1886.       break;
  1887.     }
  1888.   }
  1889.   else
  1890.   {
  1891.     /* Update the error code */
  1892.     hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  1893.     /* update return status */
  1894.     status =  HAL_ERROR;
  1895.   }
  1896.  
  1897.   /* Release Lock */
  1898.   __HAL_UNLOCK(hsd);
  1899.   return status;
  1900. }
  1901.  
  1902. /**
  1903.   * @brief  Unregister a User SD Callback
  1904.   *         SD Callback is redirected to the weak (surcharged) predefined callback
  1905.   * @param hsd : SD handle
  1906.   * @param CallbackID : ID of the callback to be unregistered
  1907.   *        This parameter can be one of the following values:
  1908.   *          @arg @ref HAL_SD_TX_CPLT_CB_ID    SD Tx Complete Callback ID
  1909.   *          @arg @ref HAL_SD_RX_CPLT_CB_ID    SD Rx Complete Callback ID
  1910.   *          @arg @ref HAL_SD_ERROR_CB_ID      SD Error Callback ID
  1911.   *          @arg @ref HAL_SD_ABORT_CB_ID      SD Abort Callback ID
  1912.   *          @arg @ref HAL_SD_MSP_INIT_CB_ID   SD MspInit Callback ID
  1913.   *          @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
  1914.   * @retval status
  1915.   */
  1916. HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
  1917. {
  1918.   HAL_StatusTypeDef status = HAL_OK;
  1919.  
  1920.   /* Process locked */
  1921.   __HAL_LOCK(hsd);
  1922.  
  1923.   if(hsd->State == HAL_SD_STATE_READY)
  1924.   {
  1925.     switch (CallbackID)
  1926.     {
  1927.     case HAL_SD_TX_CPLT_CB_ID :
  1928.       hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
  1929.       break;
  1930.     case HAL_SD_RX_CPLT_CB_ID :
  1931.       hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
  1932.       break;
  1933.     case HAL_SD_ERROR_CB_ID :
  1934.       hsd->ErrorCallback = HAL_SD_ErrorCallback;
  1935.       break;
  1936.     case HAL_SD_ABORT_CB_ID :
  1937.       hsd->AbortCpltCallback = HAL_SD_AbortCallback;
  1938.       break;
  1939.     case HAL_SD_MSP_INIT_CB_ID :
  1940.       hsd->MspInitCallback = HAL_SD_MspInit;
  1941.       break;
  1942.     case HAL_SD_MSP_DEINIT_CB_ID :
  1943.       hsd->MspDeInitCallback = HAL_SD_MspDeInit;
  1944.       break;
  1945.     default :
  1946.       /* Update the error code */
  1947.       hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  1948.       /* update return status */
  1949.       status =  HAL_ERROR;
  1950.       break;
  1951.     }
  1952.   }
  1953.   else if (hsd->State == HAL_SD_STATE_RESET)
  1954.   {
  1955.     switch (CallbackID)
  1956.     {
  1957.     case HAL_SD_MSP_INIT_CB_ID :
  1958.       hsd->MspInitCallback = HAL_SD_MspInit;
  1959.       break;
  1960.     case HAL_SD_MSP_DEINIT_CB_ID :
  1961.       hsd->MspDeInitCallback = HAL_SD_MspDeInit;
  1962.       break;
  1963.     default :
  1964.       /* Update the error code */
  1965.       hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  1966.       /* update return status */
  1967.       status =  HAL_ERROR;
  1968.       break;
  1969.     }
  1970.   }
  1971.   else
  1972.   {
  1973.     /* Update the error code */
  1974.     hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
  1975.     /* update return status */
  1976.     status =  HAL_ERROR;
  1977.   }
  1978.  
  1979.   /* Release Lock */
  1980.   __HAL_UNLOCK(hsd);
  1981.   return status;
  1982. }
  1983. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  1984.  
  1985. /**
  1986.   * @}
  1987.   */
  1988.  
  1989. /** @addtogroup SD_Exported_Functions_Group3
  1990.  *  @brief   management functions
  1991.  *
  1992. @verbatim
  1993.   ==============================================================================
  1994.                       ##### Peripheral Control functions #####
  1995.   ==============================================================================
  1996.   [..]
  1997.     This subsection provides a set of functions allowing to control the SD card
  1998.     operations and get the related information
  1999.  
  2000. @endverbatim
  2001.   * @{
  2002.   */
  2003.  
  2004. /**
  2005.   * @brief  Returns information the information of the card which are stored on
  2006.   *         the CID register.
  2007.   * @param  hsd: Pointer to SD handle
  2008.   * @param  pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that  
  2009.   *         contains all CID register parameters
  2010.   * @retval HAL status
  2011.   */
  2012. HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
  2013. {
  2014.   pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
  2015.  
  2016.   pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
  2017.  
  2018.   pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
  2019.  
  2020.   pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
  2021.  
  2022.   pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
  2023.  
  2024.   pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
  2025.  
  2026.   pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
  2027.  
  2028.   pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
  2029.  
  2030.   pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
  2031.  
  2032.   pCID->Reserved2 = 1U;
  2033.  
  2034.   return HAL_OK;
  2035. }
  2036.  
  2037. /**
  2038.   * @brief  Returns information the information of the card which are stored on
  2039.   *         the CSD register.
  2040.   * @param  hsd: Pointer to SD handle
  2041.   * @param  pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that  
  2042.   *         contains all CSD register parameters
  2043.   * @retval HAL status
  2044.   */
  2045. HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
  2046. {
  2047.   pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
  2048.  
  2049.   pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
  2050.  
  2051.   pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
  2052.  
  2053.   pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
  2054.  
  2055.   pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
  2056.  
  2057.   pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
  2058.  
  2059.   pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
  2060.  
  2061.   pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
  2062.  
  2063.   pCSD->PartBlockRead   = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
  2064.  
  2065.   pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
  2066.  
  2067.   pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
  2068.  
  2069.   pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
  2070.  
  2071.   pCSD->Reserved2 = 0U; /*!< Reserved */
  2072.  
  2073.   if(hsd->SdCard.CardType == CARD_SDSC)
  2074.   {
  2075.     pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
  2076.  
  2077.     pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
  2078.  
  2079.     pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
  2080.  
  2081.     pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
  2082.  
  2083.     pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
  2084.  
  2085.     pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
  2086.  
  2087.     hsd->SdCard.BlockNbr  = (pCSD->DeviceSize + 1U) ;
  2088.     hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
  2089.     hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
  2090.  
  2091.     hsd->SdCard.LogBlockNbr =  (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
  2092.     hsd->SdCard.LogBlockSize = 512U;
  2093.   }
  2094.   else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
  2095.   {
  2096.     /* Byte 7 */
  2097.     pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
  2098.  
  2099.     hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
  2100.     hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
  2101.     hsd->SdCard.BlockSize = 512U;
  2102.     hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
  2103.   }
  2104.   else
  2105.   {
  2106.     /* Clear all the static flags */
  2107.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  2108.     hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2109.     hsd->State = HAL_SD_STATE_READY;
  2110.     return HAL_ERROR;
  2111.   }
  2112.  
  2113.   pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
  2114.  
  2115.   pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
  2116.  
  2117.   pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
  2118.  
  2119.   pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
  2120.  
  2121.   pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
  2122.  
  2123.   pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
  2124.  
  2125.   pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
  2126.  
  2127.   pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
  2128.  
  2129.   pCSD->Reserved3 = 0;
  2130.  
  2131.   pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
  2132.  
  2133.   pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
  2134.  
  2135.   pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
  2136.  
  2137.   pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
  2138.  
  2139.   pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
  2140.  
  2141.   pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
  2142.  
  2143.   pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
  2144.  
  2145.   pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
  2146.  
  2147.   pCSD->Reserved4 = 1;
  2148.  
  2149.   return HAL_OK;
  2150. }
  2151.  
  2152. /**
  2153.   * @brief  Gets the SD status info.
  2154.   * @param  hsd: Pointer to SD handle
  2155.   * @param  pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
  2156.   *         will contain the SD card status information
  2157.   * @retval HAL status
  2158.   */
  2159. HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
  2160. {
  2161.   uint32_t sd_status[16];
  2162.   uint32_t errorstate;
  2163.  
  2164.   errorstate = SD_SendSDStatus(hsd, sd_status);
  2165.   if(errorstate != HAL_SD_ERROR_NONE)
  2166.   {
  2167.     /* Clear all the static flags */
  2168.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  2169.     hsd->ErrorCode |= errorstate;
  2170.     hsd->State = HAL_SD_STATE_READY;
  2171.     return HAL_ERROR;
  2172.   }
  2173.   else
  2174.   {
  2175.     pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
  2176.  
  2177.     pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
  2178.  
  2179.     pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
  2180.  
  2181.     pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U)    | ((sd_status[1] & 0xFF00U) << 8U) |
  2182.                                   ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
  2183.  
  2184.     pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
  2185.  
  2186.     pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
  2187.  
  2188.     pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
  2189.  
  2190.     pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
  2191.  
  2192.     pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
  2193.  
  2194.     pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
  2195.   }
  2196.  
  2197.   return HAL_OK;
  2198. }
  2199.  
  2200. /**
  2201.   * @brief  Gets the SD card info.
  2202.   * @param  hsd: Pointer to SD handle
  2203.   * @param  pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
  2204.   *         will contain the SD card status information
  2205.   * @retval HAL status
  2206.   */
  2207. HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
  2208. {
  2209.   pCardInfo->CardType     = (uint32_t)(hsd->SdCard.CardType);
  2210.   pCardInfo->CardVersion  = (uint32_t)(hsd->SdCard.CardVersion);
  2211.   pCardInfo->Class        = (uint32_t)(hsd->SdCard.Class);
  2212.   pCardInfo->RelCardAdd   = (uint32_t)(hsd->SdCard.RelCardAdd);
  2213.   pCardInfo->BlockNbr     = (uint32_t)(hsd->SdCard.BlockNbr);
  2214.   pCardInfo->BlockSize    = (uint32_t)(hsd->SdCard.BlockSize);
  2215.   pCardInfo->LogBlockNbr  = (uint32_t)(hsd->SdCard.LogBlockNbr);
  2216.   pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
  2217.  
  2218.   return HAL_OK;
  2219. }
  2220.  
  2221. /**
  2222.   * @brief  Enables wide bus operation for the requested card if supported by
  2223.   *         card.
  2224.   * @param  hsd: Pointer to SD handle
  2225.   * @param  WideMode: Specifies the SD card wide bus mode
  2226.   *          This parameter can be one of the following values:
  2227.   *            @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
  2228.   *            @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
  2229.   *            @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
  2230.   * @retval HAL status
  2231.   */
  2232. HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
  2233. {
  2234.   SDIO_InitTypeDef Init;
  2235.   uint32_t errorstate;
  2236.  
  2237.   /* Check the parameters */
  2238.   assert_param(IS_SDIO_BUS_WIDE(WideMode));
  2239.  
  2240.   /* Change State */
  2241.   hsd->State = HAL_SD_STATE_BUSY;
  2242.  
  2243.   if(hsd->SdCard.CardType != CARD_SECURED)
  2244.   {
  2245.     if(WideMode == SDIO_BUS_WIDE_8B)
  2246.     {
  2247.       hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2248.     }
  2249.     else if(WideMode == SDIO_BUS_WIDE_4B)
  2250.     {
  2251.       errorstate = SD_WideBus_Enable(hsd);
  2252.  
  2253.       hsd->ErrorCode |= errorstate;
  2254.     }
  2255.     else if(WideMode == SDIO_BUS_WIDE_1B)
  2256.     {
  2257.       errorstate = SD_WideBus_Disable(hsd);
  2258.  
  2259.       hsd->ErrorCode |= errorstate;
  2260.     }
  2261.     else
  2262.     {
  2263.       /* WideMode is not a valid argument*/
  2264.       hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
  2265.     }
  2266.   }
  2267.   else
  2268.   {
  2269.     /* MMC Card does not support this feature */
  2270.     hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2271.   }
  2272.  
  2273.   if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  2274.   {
  2275.     /* Clear all the static flags */
  2276.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  2277.     hsd->State = HAL_SD_STATE_READY;
  2278.     return HAL_ERROR;
  2279.   }
  2280.   else
  2281.   {
  2282.     /* Configure the SDIO peripheral */
  2283.     Init.ClockEdge           = hsd->Init.ClockEdge;
  2284.     Init.ClockBypass         = hsd->Init.ClockBypass;
  2285.     Init.ClockPowerSave      = hsd->Init.ClockPowerSave;
  2286.     Init.BusWide             = WideMode;
  2287.     Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
  2288.     Init.ClockDiv            = hsd->Init.ClockDiv;
  2289.     (void)SDIO_Init(hsd->Instance, Init);
  2290.   }
  2291.  
  2292.   /* Change State */
  2293.   hsd->State = HAL_SD_STATE_READY;
  2294.  
  2295.   return HAL_OK;
  2296. }
  2297.  
  2298. /**
  2299.   * @brief  Gets the current sd card data state.
  2300.   * @param  hsd: pointer to SD handle
  2301.   * @retval Card state
  2302.   */
  2303. HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
  2304. {
  2305.   uint32_t cardstate;
  2306.   uint32_t errorstate;
  2307.   uint32_t resp1 = 0;
  2308.  
  2309.   errorstate = SD_SendStatus(hsd, &resp1);
  2310.   if(errorstate != HAL_SD_ERROR_NONE)
  2311.   {
  2312.     hsd->ErrorCode |= errorstate;
  2313.   }
  2314.  
  2315.   cardstate = ((resp1 >> 9U) & 0x0FU);
  2316.  
  2317.   return (HAL_SD_CardStateTypeDef)cardstate;
  2318. }
  2319.  
  2320. /**
  2321.   * @brief  Abort the current transfer and disable the SD.
  2322.   * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
  2323.   *                the configuration information for SD module.
  2324.   * @retval HAL status
  2325.   */
  2326. HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
  2327. {
  2328.   HAL_SD_CardStateTypeDef CardState;
  2329.   uint32_t context = hsd->Context;
  2330.  
  2331.   /* DIsable All interrupts */
  2332.   __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  2333.                            SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
  2334.  
  2335.   /* Clear All flags */
  2336.   __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  2337.  
  2338.   CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
  2339.  
  2340.   if ((context & SD_CONTEXT_DMA) != 0U)
  2341.   {
  2342.     /* Disable the SD DMA request */
  2343.     hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
  2344.  
  2345.     /* Abort the SD DMA Tx channel */
  2346.     if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  2347.     {
  2348.       if(HAL_DMA_Abort(hsd->hdmatx) != HAL_OK)
  2349.       {
  2350.         hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  2351.       }
  2352.     }
  2353.     /* Abort the SD DMA Rx channel */
  2354.     else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  2355.     {
  2356.       if(HAL_DMA_Abort(hsd->hdmarx) != HAL_OK)
  2357.       {
  2358.         hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  2359.       }
  2360.     }
  2361.     else
  2362.     {
  2363.       /* Nothing to do */
  2364.     }
  2365.   }
  2366.  
  2367.   hsd->State = HAL_SD_STATE_READY;
  2368.  
  2369.   /* Initialize the SD operation */
  2370.   hsd->Context = SD_CONTEXT_NONE;
  2371.  
  2372.   CardState = HAL_SD_GetCardState(hsd);
  2373.   if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2374.   {
  2375.     hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
  2376.   }
  2377.   if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  2378.   {
  2379.     return HAL_ERROR;
  2380.   }
  2381.   return HAL_OK;
  2382. }
  2383.  
  2384. /**
  2385.   * @brief  Abort the current transfer and disable the SD (IT mode).
  2386.   * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
  2387.   *                the configuration information for SD module.
  2388.   * @retval HAL status
  2389.   */
  2390. HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
  2391. {
  2392.   HAL_SD_CardStateTypeDef CardState;
  2393.   uint32_t context = hsd->Context;
  2394.  
  2395.   /* Disable All interrupts */
  2396.   __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  2397.                            SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
  2398.  
  2399.   CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
  2400.  
  2401.   if ((context & SD_CONTEXT_DMA) != 0U)
  2402.   {
  2403.     /* Disable the SD DMA request */
  2404.     hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
  2405.  
  2406.     /* Abort the SD DMA Tx channel */
  2407.     if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
  2408.     {
  2409.       hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
  2410.       if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
  2411.       {
  2412.         hsd->hdmatx = NULL;
  2413.       }
  2414.     }
  2415.     /* Abort the SD DMA Rx channel */
  2416.     else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
  2417.     {
  2418.       hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
  2419.       if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
  2420.       {
  2421.         hsd->hdmarx = NULL;
  2422.       }
  2423.     }
  2424.     else
  2425.     {
  2426.       /* Nothing to do */
  2427.     }
  2428.   }
  2429.   /* No transfer ongoing on both DMA channels*/
  2430.   else
  2431.   {
  2432.     /* Clear All flags */
  2433.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  2434.  
  2435.     CardState = HAL_SD_GetCardState(hsd);
  2436.     hsd->State = HAL_SD_STATE_READY;
  2437.     hsd->Context = SD_CONTEXT_NONE;
  2438.     if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2439.     {
  2440.       hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
  2441.     }
  2442.     if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
  2443.     {
  2444.       return HAL_ERROR;
  2445.     }
  2446.     else
  2447.     {
  2448. #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
  2449.       hsd->AbortCpltCallback(hsd);
  2450. #else
  2451.       HAL_SD_AbortCallback(hsd);
  2452. #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
  2453.     }
  2454.   }
  2455.  
  2456.   return HAL_OK;
  2457. }
  2458.  
  2459. /**
  2460.   * @}
  2461.   */
  2462.  
  2463. /**
  2464.   * @}
  2465.   */
  2466.  
  2467. /* Private function ----------------------------------------------------------*/
  2468. /** @addtogroup SD_Private_Functions
  2469.   * @{
  2470.   */
  2471.  
  2472. /**
  2473.   * @brief  DMA SD transmit process complete callback
  2474.   * @param  hdma: DMA handle
  2475.   * @retval None
  2476.   */
  2477. static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2478. {
  2479.   SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2480.  
  2481.   /* Enable DATAEND Interrupt */
  2482.   __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND));
  2483. }
  2484.  
  2485. /**
  2486.   * @brief  DMA SD receive process complete callback
  2487.   * @param  hdma: DMA handle
  2488.   * @retval None
  2489.   */
  2490. static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2491. {
  2492.   SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2493.   uint32_t errorstate;
  2494.  
  2495.   /* Send stop command in multiblock write */
  2496.   if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
  2497.   {
  2498.     errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
  2499.     if(errorstate != HAL_SD_ERROR_NONE)
  2500.     {
  2501.       hsd->ErrorCode |= errorstate;
  2502. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2503.       hsd->ErrorCallback(hsd);
  2504. #else
  2505.       HAL_SD_ErrorCallback(hsd);
  2506. #endif
  2507.     }
  2508.   }
  2509.  
  2510.   /* Disable the DMA transfer for transmit request by setting the DMAEN bit
  2511.   in the SD DCTRL register */
  2512.   hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
  2513.  
  2514.   /* Clear all the static flags */
  2515.   __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  2516.  
  2517.   hsd->State = HAL_SD_STATE_READY;
  2518.   hsd->Context = SD_CONTEXT_NONE;
  2519.  
  2520. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2521.   hsd->RxCpltCallback(hsd);
  2522. #else
  2523.   HAL_SD_RxCpltCallback(hsd);
  2524. #endif
  2525. }
  2526.  
  2527. /**
  2528.   * @brief  DMA SD communication error callback
  2529.   * @param  hdma: DMA handle
  2530.   * @retval None
  2531.   */
  2532. static void SD_DMAError(DMA_HandleTypeDef *hdma)
  2533. {
  2534.   SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2535.   HAL_SD_CardStateTypeDef CardState;
  2536.   uint32_t RxErrorCode, TxErrorCode;
  2537.  
  2538.     RxErrorCode = hsd->hdmarx->ErrorCode;
  2539.     TxErrorCode = hsd->hdmatx->ErrorCode;  
  2540.     if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
  2541.     {
  2542.       /* Clear All flags */
  2543.       __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
  2544.  
  2545.       /* Disable All interrupts */
  2546.       __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
  2547.         SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
  2548.  
  2549.       hsd->ErrorCode |= HAL_SD_ERROR_DMA;
  2550.       CardState = HAL_SD_GetCardState(hsd);
  2551.       if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2552.       {
  2553.         hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  2554.       }
  2555.  
  2556.       hsd->State= HAL_SD_STATE_READY;
  2557.       hsd->Context = SD_CONTEXT_NONE;
  2558.     }
  2559.  
  2560. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2561.     hsd->ErrorCallback(hsd);
  2562. #else
  2563.     HAL_SD_ErrorCallback(hsd);
  2564. #endif
  2565. }
  2566.  
  2567. /**
  2568.   * @brief  DMA SD Tx Abort callback
  2569.   * @param  hdma: DMA handle
  2570.   * @retval None
  2571.   */
  2572. static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
  2573. {
  2574.   SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2575.   HAL_SD_CardStateTypeDef CardState;
  2576.  
  2577.   /* Clear All flags */
  2578.   __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  2579.  
  2580.   CardState = HAL_SD_GetCardState(hsd);
  2581.   hsd->State = HAL_SD_STATE_READY;
  2582.   hsd->Context = SD_CONTEXT_NONE;
  2583.   if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2584.   {
  2585.     hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  2586.   }
  2587.  
  2588.   if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
  2589.   {
  2590. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2591.     hsd->AbortCpltCallback(hsd);
  2592. #else
  2593.     HAL_SD_AbortCallback(hsd);
  2594. #endif
  2595.   }
  2596.   else
  2597.   {
  2598. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2599.     hsd->ErrorCallback(hsd);
  2600. #else
  2601.     HAL_SD_ErrorCallback(hsd);
  2602. #endif
  2603.   }
  2604. }
  2605.  
  2606. /**
  2607.   * @brief  DMA SD Rx Abort callback
  2608.   * @param  hdma: DMA handle
  2609.   * @retval None
  2610.   */
  2611. static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
  2612. {
  2613.   SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
  2614.   HAL_SD_CardStateTypeDef CardState;
  2615.  
  2616.   /* Clear All flags */
  2617.   __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  2618.  
  2619.   CardState = HAL_SD_GetCardState(hsd);
  2620.   hsd->State = HAL_SD_STATE_READY;
  2621.   hsd->Context = SD_CONTEXT_NONE;
  2622.   if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
  2623.   {
  2624.     hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
  2625.   }
  2626.  
  2627.   if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
  2628.   {
  2629. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2630.     hsd->AbortCpltCallback(hsd);
  2631. #else
  2632.     HAL_SD_AbortCallback(hsd);
  2633. #endif
  2634.   }
  2635.   else
  2636.   {
  2637. #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
  2638.     hsd->ErrorCallback(hsd);
  2639. #else
  2640.     HAL_SD_ErrorCallback(hsd);
  2641. #endif
  2642.   }
  2643. }
  2644.  
  2645. /**
  2646.   * @brief  Initializes the sd card.
  2647.   * @param  hsd: Pointer to SD handle
  2648.   * @retval SD Card error state
  2649.   */
  2650. static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
  2651. {
  2652.   HAL_SD_CardCSDTypeDef CSD;
  2653.   uint32_t errorstate;
  2654.   uint16_t sd_rca = 1U;
  2655.  
  2656.   /* Check the power State */
  2657.   if(SDIO_GetPowerState(hsd->Instance) == 0U)
  2658.   {
  2659.     /* Power off */
  2660.     return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  2661.   }
  2662.  
  2663.   if(hsd->SdCard.CardType != CARD_SECURED)
  2664.   {
  2665.     /* Send CMD2 ALL_SEND_CID */
  2666.     errorstate = SDMMC_CmdSendCID(hsd->Instance);
  2667.     if(errorstate != HAL_SD_ERROR_NONE)
  2668.     {
  2669.       return errorstate;
  2670.     }
  2671.     else
  2672.     {
  2673.       /* Get Card identification number data */
  2674.       hsd->CID[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
  2675.       hsd->CID[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
  2676.       hsd->CID[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
  2677.       hsd->CID[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
  2678.     }
  2679.   }
  2680.  
  2681.   if(hsd->SdCard.CardType != CARD_SECURED)
  2682.   {
  2683.     /* Send CMD3 SET_REL_ADDR with argument 0 */
  2684.     /* SD Card publishes its RCA. */
  2685.     errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
  2686.     if(errorstate != HAL_SD_ERROR_NONE)
  2687.     {
  2688.       return errorstate;
  2689.     }
  2690.   }
  2691.   if(hsd->SdCard.CardType != CARD_SECURED)
  2692.   {
  2693.     /* Get the SD card RCA */
  2694.     hsd->SdCard.RelCardAdd = sd_rca;
  2695.  
  2696.     /* Send CMD9 SEND_CSD with argument as card's RCA */
  2697.     errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  2698.     if(errorstate != HAL_SD_ERROR_NONE)
  2699.     {
  2700.       return errorstate;
  2701.     }
  2702.     else
  2703.     {
  2704.       /* Get Card Specific Data */
  2705.       hsd->CSD[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
  2706.       hsd->CSD[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
  2707.       hsd->CSD[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
  2708.       hsd->CSD[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
  2709.     }
  2710.   }
  2711.  
  2712.   /* Get the Card Class */
  2713.   hsd->SdCard.Class = (SDIO_GetResponse(hsd->Instance, SDIO_RESP2) >> 20U);
  2714.  
  2715.   /* Get CSD parameters */
  2716.   if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
  2717.   {
  2718.     return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2719.   }
  2720.  
  2721.   /* Select the Card */
  2722.   errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
  2723.   if(errorstate != HAL_SD_ERROR_NONE)
  2724.   {
  2725.     return errorstate;
  2726.   }
  2727.  
  2728.   /* Configure SDIO peripheral interface */
  2729.   (void)SDIO_Init(hsd->Instance, hsd->Init);
  2730.  
  2731.   /* All cards are initialized */
  2732.   return HAL_SD_ERROR_NONE;
  2733. }
  2734.  
  2735. /**
  2736.   * @brief  Enquires cards about their operating voltage and configures clock
  2737.   *         controls and stores SD information that will be needed in future
  2738.   *         in the SD handle.
  2739.   * @param  hsd: Pointer to SD handle
  2740.   * @retval error state
  2741.   */
  2742. static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
  2743. {
  2744.   __IO uint32_t count = 0U;
  2745.   uint32_t response = 0U, validvoltage = 0U;
  2746.   uint32_t errorstate;
  2747.  
  2748.   /* CMD0: GO_IDLE_STATE */
  2749.   errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
  2750.   if(errorstate != HAL_SD_ERROR_NONE)
  2751.   {
  2752.     return errorstate;
  2753.   }
  2754.  
  2755.   /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
  2756.   errorstate = SDMMC_CmdOperCond(hsd->Instance);
  2757.   if(errorstate != HAL_SD_ERROR_NONE)
  2758.   {
  2759.     hsd->SdCard.CardVersion = CARD_V1_X;
  2760.     /* CMD0: GO_IDLE_STATE */
  2761.     errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
  2762.     if(errorstate != HAL_SD_ERROR_NONE)
  2763.     {
  2764.       return errorstate;
  2765.     }
  2766.  
  2767.   }
  2768.   else
  2769.   {
  2770.     hsd->SdCard.CardVersion = CARD_V2_X;
  2771.   }
  2772.  
  2773.   if( hsd->SdCard.CardVersion == CARD_V2_X)
  2774.   {
  2775.     /* SEND CMD55 APP_CMD with RCA as 0 */
  2776.     errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
  2777.     if(errorstate != HAL_SD_ERROR_NONE)
  2778.     {
  2779.       return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2780.     }
  2781.   }
  2782.   /* SD CARD */
  2783.   /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
  2784.   while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
  2785.   {
  2786.     /* SEND CMD55 APP_CMD with RCA as 0 */
  2787.     errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
  2788.     if(errorstate != HAL_SD_ERROR_NONE)
  2789.     {
  2790.       return errorstate;
  2791.     }
  2792.  
  2793.     /* Send CMD41 */
  2794.     errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
  2795.     if(errorstate != HAL_SD_ERROR_NONE)
  2796.     {
  2797.       return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
  2798.     }
  2799.  
  2800.     /* Get command response */
  2801.     response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
  2802.  
  2803.     /* Get operating voltage*/
  2804.     validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
  2805.  
  2806.     count++;
  2807.   }
  2808.  
  2809.   if(count >= SDMMC_MAX_VOLT_TRIAL)
  2810.   {
  2811.     return HAL_SD_ERROR_INVALID_VOLTRANGE;
  2812.   }
  2813.  
  2814.   if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
  2815.   {
  2816.     hsd->SdCard.CardType = CARD_SDHC_SDXC;
  2817.   }
  2818.   else
  2819.   {
  2820.     hsd->SdCard.CardType = CARD_SDSC;
  2821.   }
  2822.  
  2823.  
  2824.   return HAL_SD_ERROR_NONE;
  2825. }
  2826.  
  2827. /**
  2828.   * @brief  Turns the SDIO output signals off.
  2829.   * @param  hsd: Pointer to SD handle
  2830.   * @retval None
  2831.   */
  2832. static void SD_PowerOFF(SD_HandleTypeDef *hsd)
  2833. {
  2834.   /* Set Power State to OFF */
  2835.   (void)SDIO_PowerState_OFF(hsd->Instance);
  2836. }
  2837.  
  2838. /**
  2839.   * @brief  Send Status info command.
  2840.   * @param  hsd: pointer to SD handle
  2841.   * @param  pSDstatus: Pointer to the buffer that will contain the SD card status
  2842.   *         SD Status register)
  2843.   * @retval error state
  2844.   */
  2845. static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
  2846. {
  2847.   SDIO_DataInitTypeDef config;
  2848.   uint32_t errorstate;
  2849.   uint32_t tickstart = HAL_GetTick();
  2850.   uint32_t count;
  2851.   uint32_t *pData = pSDstatus;
  2852.  
  2853.   /* Check SD response */
  2854.   if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  2855.   {
  2856.     return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  2857.   }
  2858.  
  2859.   /* Set block size for card if it is not equal to current block size for card */
  2860.   errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
  2861.   if(errorstate != HAL_SD_ERROR_NONE)
  2862.   {
  2863.     hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  2864.     return errorstate;
  2865.   }
  2866.  
  2867.   /* Send CMD55 */
  2868.   errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  2869.   if(errorstate != HAL_SD_ERROR_NONE)
  2870.   {
  2871.     hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  2872.     return errorstate;
  2873.   }
  2874.  
  2875.   /* Configure the SD DPSM (Data Path State Machine) */
  2876.   config.DataTimeOut   = SDMMC_DATATIMEOUT;
  2877.   config.DataLength    = 64U;
  2878.   config.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
  2879.   config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
  2880.   config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
  2881.   config.DPSM          = SDIO_DPSM_ENABLE;
  2882.   (void)SDIO_ConfigData(hsd->Instance, &config);
  2883.  
  2884.   /* Send ACMD13 (SD_APP_STAUS)  with argument as card's RCA */
  2885.   errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
  2886.   if(errorstate != HAL_SD_ERROR_NONE)
  2887.   {
  2888.     hsd->ErrorCode |= HAL_SD_ERROR_NONE;
  2889.     return errorstate;
  2890.   }
  2891.  
  2892.   /* Get status data */
  2893.   while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
  2894.   {
  2895.     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
  2896.     {
  2897.       for(count = 0U; count < 8U; count++)
  2898.       {
  2899.         *pData = SDIO_ReadFIFO(hsd->Instance);
  2900.         pData++;
  2901.       }
  2902.     }
  2903.  
  2904.     if((HAL_GetTick() - tickstart) >=  SDMMC_DATATIMEOUT)
  2905.     {
  2906.       return HAL_SD_ERROR_TIMEOUT;
  2907.     }
  2908.   }
  2909.  
  2910.   if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
  2911.   {
  2912.     return HAL_SD_ERROR_DATA_TIMEOUT;
  2913.   }
  2914.   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
  2915.   {
  2916.     return HAL_SD_ERROR_DATA_CRC_FAIL;
  2917.   }
  2918.   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
  2919.   {
  2920.     return HAL_SD_ERROR_RX_OVERRUN;
  2921.   }
  2922.   else
  2923.   {
  2924.     /* Nothing to do */
  2925.   }
  2926.  
  2927.   while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)))
  2928.   {
  2929.     *pData = SDIO_ReadFIFO(hsd->Instance);
  2930.     pData++;
  2931.  
  2932.     if((HAL_GetTick() - tickstart) >=  SDMMC_DATATIMEOUT)
  2933.     {
  2934.       return HAL_SD_ERROR_TIMEOUT;
  2935.     }
  2936.   }
  2937.  
  2938.   /* Clear all the static status flags*/
  2939.   __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  2940.  
  2941.   return HAL_SD_ERROR_NONE;
  2942. }
  2943.  
  2944. /**
  2945.   * @brief  Returns the current card's status.
  2946.   * @param  hsd: Pointer to SD handle
  2947.   * @param  pCardStatus: pointer to the buffer that will contain the SD card
  2948.   *         status (Card Status register)
  2949.   * @retval error state
  2950.   */
  2951. static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
  2952. {
  2953.   uint32_t errorstate;
  2954.  
  2955.   if(pCardStatus == NULL)
  2956.   {
  2957.     return HAL_SD_ERROR_PARAM;
  2958.   }
  2959.  
  2960.   /* Send Status command */
  2961.   errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  2962.   if(errorstate != HAL_SD_ERROR_NONE)
  2963.   {
  2964.     return errorstate;
  2965.   }
  2966.  
  2967.   /* Get SD card status */
  2968.   *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
  2969.  
  2970.   return HAL_SD_ERROR_NONE;
  2971. }
  2972.  
  2973. /**
  2974.   * @brief  Enables the SDIO wide bus mode.
  2975.   * @param  hsd: pointer to SD handle
  2976.   * @retval error state
  2977.   */
  2978. static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
  2979. {
  2980.   uint32_t scr[2U] = {0U, 0U};
  2981.   uint32_t errorstate;
  2982.  
  2983.   if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  2984.   {
  2985.     return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  2986.   }
  2987.  
  2988.   /* Get SCR Register */
  2989.   errorstate = SD_FindSCR(hsd, scr);
  2990.   if(errorstate != HAL_SD_ERROR_NONE)
  2991.   {
  2992.     return errorstate;
  2993.   }
  2994.  
  2995.   /* If requested card supports wide bus operation */
  2996.   if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
  2997.   {
  2998.     /* Send CMD55 APP_CMD with argument as card's RCA.*/
  2999.     errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  3000.     if(errorstate != HAL_SD_ERROR_NONE)
  3001.     {
  3002.       return errorstate;
  3003.     }
  3004.  
  3005.     /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
  3006.     errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
  3007.     if(errorstate != HAL_SD_ERROR_NONE)
  3008.     {
  3009.       return errorstate;
  3010.     }
  3011.  
  3012.     return HAL_SD_ERROR_NONE;
  3013.   }
  3014.   else
  3015.   {
  3016.     return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  3017.   }
  3018. }
  3019.  
  3020. /**
  3021.   * @brief  Disables the SDIO wide bus mode.
  3022.   * @param  hsd: Pointer to SD handle
  3023.   * @retval error state
  3024.   */
  3025. static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
  3026. {
  3027.   uint32_t scr[2U] = {0U, 0U};
  3028.   uint32_t errorstate;
  3029.  
  3030.   if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
  3031.   {
  3032.     return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
  3033.   }
  3034.  
  3035.   /* Get SCR Register */
  3036.   errorstate = SD_FindSCR(hsd, scr);
  3037.   if(errorstate != HAL_SD_ERROR_NONE)
  3038.   {
  3039.     return errorstate;
  3040.   }
  3041.  
  3042.   /* If requested card supports 1 bit mode operation */
  3043.   if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
  3044.   {
  3045.     /* Send CMD55 APP_CMD with argument as card's RCA */
  3046.     errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
  3047.     if(errorstate != HAL_SD_ERROR_NONE)
  3048.     {
  3049.       return errorstate;
  3050.     }
  3051.  
  3052.     /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
  3053.     errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
  3054.     if(errorstate != HAL_SD_ERROR_NONE)
  3055.     {
  3056.       return errorstate;
  3057.     }
  3058.  
  3059.     return HAL_SD_ERROR_NONE;
  3060.   }
  3061.   else
  3062.   {
  3063.     return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
  3064.   }
  3065. }
  3066.  
  3067.  
  3068. /**
  3069.   * @brief  Finds the SD card SCR register value.
  3070.   * @param  hsd: Pointer to SD handle
  3071.   * @param  pSCR: pointer to the buffer that will contain the SCR value
  3072.   * @retval error state
  3073.   */
  3074. static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
  3075. {
  3076.   SDIO_DataInitTypeDef config;
  3077.   uint32_t errorstate;
  3078.   uint32_t tickstart = HAL_GetTick();
  3079.   uint32_t index = 0U;
  3080.   uint32_t tempscr[2U] = {0U, 0U};
  3081.   uint32_t *scr = pSCR;
  3082.  
  3083.   /* Set Block Size To 8 Bytes */
  3084.   errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
  3085.   if(errorstate != HAL_SD_ERROR_NONE)
  3086.   {
  3087.     return errorstate;
  3088.   }
  3089.  
  3090.   /* Send CMD55 APP_CMD with argument as card's RCA */
  3091.   errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
  3092.   if(errorstate != HAL_SD_ERROR_NONE)
  3093.   {
  3094.     return errorstate;
  3095.   }
  3096.  
  3097.   config.DataTimeOut   = SDMMC_DATATIMEOUT;
  3098.   config.DataLength    = 8U;
  3099.   config.DataBlockSize = SDIO_DATABLOCK_SIZE_8B;
  3100.   config.TransferDir   = SDIO_TRANSFER_DIR_TO_SDIO;
  3101.   config.TransferMode  = SDIO_TRANSFER_MODE_BLOCK;
  3102.   config.DPSM          = SDIO_DPSM_ENABLE;
  3103.   (void)SDIO_ConfigData(hsd->Instance, &config);
  3104.  
  3105.   /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
  3106.   errorstate = SDMMC_CmdSendSCR(hsd->Instance);
  3107.   if(errorstate != HAL_SD_ERROR_NONE)
  3108.   {
  3109.     return errorstate;
  3110.   }
  3111.  
  3112.   while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
  3113.   {
  3114.     if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
  3115.     {
  3116.       *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
  3117.       index++;
  3118.     }
  3119.  
  3120.     if((HAL_GetTick() - tickstart) >=  SDMMC_DATATIMEOUT)
  3121.     {
  3122.       return HAL_SD_ERROR_TIMEOUT;
  3123.     }
  3124.   }
  3125.  
  3126.   if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
  3127.   {
  3128.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
  3129.  
  3130.     return HAL_SD_ERROR_DATA_TIMEOUT;
  3131.   }
  3132.   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
  3133.   {
  3134.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
  3135.  
  3136.     return HAL_SD_ERROR_DATA_CRC_FAIL;
  3137.   }
  3138.   else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
  3139.   {
  3140.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
  3141.  
  3142.     return HAL_SD_ERROR_RX_OVERRUN;
  3143.   }
  3144.   else
  3145.   {
  3146.     /* No error flag set */
  3147.     /* Clear all the static flags */
  3148.     __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
  3149.  
  3150.     *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24)  | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
  3151.             ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24));
  3152.     scr++;
  3153.     *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24)  | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
  3154.             ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24));
  3155.  
  3156.   }
  3157.  
  3158.   return HAL_SD_ERROR_NONE;
  3159. }
  3160.  
  3161. /**
  3162.   * @brief  Wrap up reading in non-blocking mode.
  3163.   * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
  3164.   *              the configuration information.
  3165.   * @retval None
  3166.   */
  3167. static void SD_Read_IT(SD_HandleTypeDef *hsd)
  3168. {
  3169.   uint32_t count, data, dataremaining;
  3170.   uint8_t* tmp;
  3171.  
  3172.   tmp = hsd->pRxBuffPtr;
  3173.   dataremaining = hsd->RxXferSize;
  3174.  
  3175.   if (dataremaining > 0U)
  3176.   {
  3177.     /* Read data from SDIO Rx FIFO */
  3178.     for(count = 0U; count < 8U; count++)
  3179.     {
  3180.       data = SDIO_ReadFIFO(hsd->Instance);
  3181.       *tmp = (uint8_t)(data & 0xFFU);
  3182.       tmp++;
  3183.       dataremaining--;
  3184.       *tmp = (uint8_t)((data >> 8U) & 0xFFU);
  3185.       tmp++;
  3186.       dataremaining--;
  3187.       *tmp = (uint8_t)((data >> 16U) & 0xFFU);
  3188.       tmp++;
  3189.       dataremaining--;
  3190.       *tmp = (uint8_t)((data >> 24U) & 0xFFU);
  3191.       tmp++;
  3192.       dataremaining--;
  3193.     }
  3194.  
  3195.     hsd->pRxBuffPtr = tmp;
  3196.     hsd->RxXferSize = dataremaining;
  3197.   }
  3198. }
  3199.  
  3200. /**
  3201.   * @brief  Wrap up writing in non-blocking mode.
  3202.   * @param  hsd: pointer to a SD_HandleTypeDef structure that contains
  3203.   *              the configuration information.
  3204.   * @retval None
  3205.   */
  3206. static void SD_Write_IT(SD_HandleTypeDef *hsd)
  3207. {
  3208.   uint32_t count, data, dataremaining;
  3209.   uint8_t* tmp;
  3210.  
  3211.   tmp = hsd->pTxBuffPtr;
  3212.   dataremaining = hsd->TxXferSize;
  3213.  
  3214.   if (dataremaining > 0U)
  3215.   {
  3216.     /* Write data to SDIO Tx FIFO */
  3217.     for(count = 0U; count < 8U; count++)
  3218.     {
  3219.       data = (uint32_t)(*tmp);
  3220.       tmp++;
  3221.       dataremaining--;
  3222.       data |= ((uint32_t)(*tmp) << 8U);
  3223.       tmp++;
  3224.       dataremaining--;
  3225.       data |= ((uint32_t)(*tmp) << 16U);
  3226.       tmp++;
  3227.       dataremaining--;
  3228.       data |= ((uint32_t)(*tmp) << 24U);
  3229.       tmp++;
  3230.       dataremaining--;
  3231.       (void)SDIO_WriteFIFO(hsd->Instance, &data);
  3232.     }
  3233.  
  3234.     hsd->pTxBuffPtr = tmp;
  3235.     hsd->TxXferSize = dataremaining;
  3236.   }
  3237. }
  3238.  
  3239. /**
  3240.   * @}
  3241.   */
  3242.  
  3243. #endif /* HAL_SD_MODULE_ENABLED */
  3244.  
  3245. /**
  3246.   * @}
  3247.   */
  3248.  
  3249. /**
  3250.   * @}
  3251.   */
  3252.  
  3253. #endif /* SDIO */
  3254.  
  3255. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  3256.