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