Create release.yml
[betaflight.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_sd.c
blobb2467ca30f5fd283bff0d8b5cf068a3713b00488
1 /**
2 ******************************************************************************
3 * @file stm32h7xx_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
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 (SDMMC 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.
25 [..]
26 This driver is a generic layered driver for SDMMC memories which uses the HAL
27 SDMMC driver functions to interface with SD and uSD cards devices.
28 It is used as follows:
30 (#)Initialize the SDMMC low level resources by implementing the HAL_SD_MspInit() API:
31 (##) Enable the SDMMC interface clock using __HAL_RCC_SDMMC_CLK_ENABLE();
32 (##) SDMMC pins configuration for SD card
33 (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
34 (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init()
35 and according to your pin assignment;
36 (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
37 and HAL_SD_WriteBlocks_IT() APIs).
38 (+++) Configure the SDMMC interrupt priorities using function HAL_NVIC_SetPriority();
39 (+++) Enable the NVIC SDMMC IRQs using function HAL_NVIC_EnableIRQ()
40 (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
41 and __HAL_SD_DISABLE_IT() inside the communication process.
42 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
43 and __HAL_SD_CLEAR_IT()
44 (##) No general propose DMA Configuration is needed, an Internal DMA for SDMMC Peripheral are used.
46 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
49 *** SD Card Initialization and configuration ***
50 ================================================
51 [..]
52 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
53 SDMMC Peripheral(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
54 This function provide the following operations:
56 (#) Apply the SD Card initialization process at 400KHz and check the SD Card
57 type (Standard Capacity or High Capacity). You can change or adapt this
58 frequency by adjusting the "ClockDiv" field.
59 The SD Card frequency (SDMMC_CK) is computed as follows:
61 SDMMC_CK = SDMMCCLK / (2 * ClockDiv)
63 In initialization mode and according to the SD Card standard,
64 make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
66 This phase of initialization is done through SDMMC_Init() and
67 SDMMC_PowerState_ON() SDMMC low level APIs.
69 (#) Initialize the SD card. The API used is HAL_SD_InitCard().
70 This phase allows the card initialization and identification
71 and check the SD Card type (Standard Capacity or High Capacity)
72 The initialization flow is compatible with SD standard.
74 This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
75 of plug-off plug-in.
77 (#) Configure the SD Card Data transfer frequency. You can change or adapt this
78 frequency by adjusting the "ClockDiv" field.
79 In transfer mode and according to the SD Card standard, make sure that the
80 SDMMC_CK frequency doesn't exceed 25MHz and 100MHz in High-speed mode switch.
82 (#) Select the corresponding SD Card according to the address read with the step 2.
84 (#) Configure the SD Card in wide bus mode: 4-bits data.
86 *** SD Card Read operation ***
87 ==============================
88 [..]
89 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
90 This function support only 512-bytes block length (the block size should be
91 chosen as 512 bytes).
92 You can choose either one block read operation or multiple block read operation
93 by adjusting the "NumberOfBlocks" parameter.
94 After this, you have to ensure that the transfer is done correctly. The check is done
95 through HAL_SD_GetCardState() function for SD card state.
97 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
98 This function support only 512-bytes block length (the block size should be
99 chosen as 512 bytes).
100 You can choose either one block read operation or multiple block read operation
101 by adjusting the "NumberOfBlocks" parameter.
102 After this, you have to ensure that the transfer is done correctly. The check is done
103 through HAL_SD_GetCardState() function for SD card state.
104 You could also check the DMA transfer process through the SD Rx interrupt event.
106 (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
107 This function support only 512-bytes block length (the block size should be
108 chosen as 512 bytes).
109 You can choose either one block read operation or multiple block read operation
110 by adjusting the "NumberOfBlocks" parameter.
111 After this, you have to ensure that the transfer is done correctly. The check is done
112 through HAL_SD_GetCardState() function for SD card state.
113 You could also check the IT transfer process through the SD Rx interrupt event.
115 *** SD Card Write operation ***
116 ===============================
117 [..]
118 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
119 This function support only 512-bytes block length (the block size should be
120 chosen as 512 bytes).
121 You can choose either one block read operation or multiple block read operation
122 by adjusting the "NumberOfBlocks" parameter.
123 After this, you have to ensure that the transfer is done correctly. The check is done
124 through HAL_SD_GetCardState() function for SD card state.
126 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
127 This function support only 512-bytes block length (the block size should be
128 chosen as 512 bytes).
129 You can choose either one block read operation or multiple block read operation
130 by adjusting the "NumberOfBlocks" parameter.
131 After this, you have to ensure that the transfer is done correctly. The check is done
132 through HAL_SD_GetCardState() function for SD card state.
133 You could also check the DMA transfer process through the SD Tx interrupt event.
135 (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
136 This function support only 512-bytes block length (the block size should be
137 chosen as 512 bytes).
138 You can choose either one block read operation or multiple block read operation
139 by adjusting the "NumberOfBlocks" parameter.
140 After this, you have to ensure that the transfer is done correctly. The check is done
141 through HAL_SD_GetCardState() function for SD card state.
142 You could also check the IT transfer process through the SD Tx interrupt event.
144 *** SD card status ***
145 ======================
146 [..]
147 (+) The SD Status contains status bits that are related to the SD Memory
148 Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
150 *** SD card information ***
151 ===========================
152 [..]
153 (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
154 It returns useful information about the SD card such as block size, card type,
155 block number ...
157 *** SD card CSD register ***
158 ============================
159 (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
160 Some of the CSD parameters are useful for card initialization and identification.
162 *** SD card CID register ***
163 ============================
164 (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
165 Some of the CSD parameters are useful for card initialization and identification.
167 *** SD HAL driver macros list ***
168 ==================================
169 [..]
170 Below the list of most used macros in SD HAL driver.
172 (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
173 (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
174 (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
175 (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
177 (@) You can refer to the SD HAL driver header file for more useful macros
179 *** Callback registration ***
180 =============================================
181 [..]
182 The compilation define USE_HAL_SD_REGISTER_CALLBACKS when set to 1
183 allows the user to configure dynamically the driver callbacks.
185 Use Functions @ref HAL_SD_RegisterCallback() to register a user callback,
186 it allows to register following callbacks:
187 (+) TxCpltCallback : callback when a transmission transfer is completed.
188 (+) RxCpltCallback : callback when a reception transfer is completed.
189 (+) ErrorCallback : callback when error occurs.
190 (+) AbortCpltCallback : callback when abort is completed.
191 (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed.
192 (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed.
193 (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed.
194 (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed.
195 (+) MspInitCallback : SD MspInit.
196 (+) MspDeInitCallback : SD MspDeInit.
197 This function takes as parameters the HAL peripheral handle, the Callback ID
198 and a pointer to the user callback function.
199 For specific callbacks TransceiverCallback use dedicated register callbacks:
200 respectively @ref HAL_SD_RegisterTransceiverCallback().
202 Use function @ref HAL_SD_UnRegisterCallback() to reset a callback to the default
203 weak (surcharged) function. It allows to reset 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 (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed.
209 (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed.
210 (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed.
211 (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed.
212 (+) MspInitCallback : SD MspInit.
213 (+) MspDeInitCallback : SD MspDeInit.
214 This function) takes as parameters the HAL peripheral handle and the Callback ID.
215 For specific callbacks TransceiverCallback use dedicated unregister callbacks:
216 respectively @ref HAL_SD_UnRegisterTransceiverCallback().
218 By default, after the @ref HAL_SD_Init and if the state is HAL_SD_STATE_RESET
219 all callbacks are reset to the corresponding legacy weak (surcharged) functions.
220 Exception done for MspInit and MspDeInit callbacks that are respectively
221 reset to the legacy weak (surcharged) functions in the @ref HAL_SD_Init
222 and @ref HAL_SD_DeInit only when these callbacks are null (not registered beforehand).
223 If not, MspInit or MspDeInit are not null, the @ref HAL_SD_Init and @ref HAL_SD_DeInit
224 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
226 Callbacks can be registered/unregistered in READY state only.
227 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
228 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
229 during the Init/DeInit.
230 In that case first register the MspInit/MspDeInit user callbacks
231 using @ref HAL_SD_RegisterCallback before calling @ref HAL_SD_DeInit
232 or @ref HAL_SD_Init function.
234 When The compilation define USE_HAL_SD_REGISTER_CALLBACKS is set to 0 or
235 not defined, the callback registering feature is not available
236 and weak (surcharged) callbacks are used.
238 @endverbatim
239 ******************************************************************************
240 * @attention
242 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
243 * All rights reserved.</center></h2>
245 * This software component is licensed by ST under BSD 3-Clause license,
246 * the "License"; You may not use this file except in compliance with the
247 * License. You may obtain a copy of the License at:
248 * opensource.org/licenses/BSD-3-Clause
250 ******************************************************************************
253 /* Includes ------------------------------------------------------------------*/
254 #include "stm32h7xx_hal.h"
256 /** @addtogroup STM32H7xx_HAL_Driver
257 * @{
260 /** @addtogroup SD
261 * @{
264 #ifdef HAL_SD_MODULE_ENABLED
266 /* Private typedef -----------------------------------------------------------*/
267 /* Private define ------------------------------------------------------------*/
268 /** @addtogroup SD_Private_Defines
269 * @{
273 * @}
276 /* Private macro -------------------------------------------------------------*/
277 #if defined (DLYB_SDMMC1) && defined (DLYB_SDMMC2)
278 #define SD_GET_DLYB_INSTANCE(SDMMC_INSTANCE) (((SDMMC_INSTANCE) == SDMMC1)? \
279 DLYB_SDMMC1 : DLYB_SDMMC2 )
280 #elif defined (DLYB_SDMMC1)
281 #define SD_GET_DLYB_INSTANCE(SDMMC_INSTANCE) ( DLYB_SDMMC1 )
282 #endif
283 /* Private variables ---------------------------------------------------------*/
284 /* Private function prototypes -----------------------------------------------*/
285 /* Private functions ---------------------------------------------------------*/
286 /** @defgroup SD_Private_Functions SD Private Functions
287 * @{
289 static uint32_t SD_InitCard (SD_HandleTypeDef *hsd);
290 static uint32_t SD_PowerON (SD_HandleTypeDef *hsd);
291 static uint32_t SD_SendSDStatus (SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
292 static uint32_t SD_SendStatus (SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
293 static uint32_t SD_WideBus_Enable (SD_HandleTypeDef *hsd);
294 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
295 static uint32_t SD_FindSCR (SD_HandleTypeDef *hsd, uint32_t *pSCR);
296 static void SD_PowerOFF (SD_HandleTypeDef *hsd);
297 static void SD_Write_IT (SD_HandleTypeDef *hsd);
298 static void SD_Read_IT (SD_HandleTypeDef *hsd);
299 static uint32_t SD_HighSpeed (SD_HandleTypeDef *hsd);
300 #if (USE_SD_TRANSCEIVER != 0U)
301 static uint32_t SD_UltraHighSpeed (SD_HandleTypeDef *hsd);
302 static uint32_t SD_DDR_Mode (SD_HandleTypeDef *hsd);
303 #endif /* USE_SD_TRANSCEIVER */
305 * @}
308 /* Exported functions --------------------------------------------------------*/
309 /** @addtogroup SD_Exported_Functions
310 * @{
313 /** @addtogroup SD_Exported_Functions_Group1
314 * @brief Initialization and de-initialization functions
316 @verbatim
317 ==============================================================================
318 ##### Initialization and de-initialization functions #####
319 ==============================================================================
320 [..]
321 This section provides functions allowing to initialize/de-initialize the SD
322 card device to be ready for use.
324 @endverbatim
325 * @{
329 * @brief Initializes the SD according to the specified parameters in the
330 SD_HandleTypeDef and create the associated handle.
331 * @param hsd: Pointer to the SD handle
332 * @retval HAL status
334 HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
336 HAL_SD_CardStatusTypeDef CardStatus;
337 uint32_t speedgrade, unitsize;
338 uint32_t tickstart;
340 /* Check the SD handle allocation */
341 if(hsd == NULL)
343 return HAL_ERROR;
346 /* Check the parameters */
347 assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
348 assert_param(IS_SDMMC_CLOCK_EDGE(hsd->Init.ClockEdge));
349 assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
350 assert_param(IS_SDMMC_BUS_WIDE(hsd->Init.BusWide));
351 assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
352 assert_param(IS_SDMMC_CLKDIV(hsd->Init.ClockDiv));
354 if(hsd->State == HAL_SD_STATE_RESET)
356 /* Allocate lock resource and initialize it */
357 hsd->Lock = HAL_UNLOCKED;
359 #if (USE_SD_TRANSCEIVER != 0U)
360 /* Force SDMMC_TRANSCEIVER_PRESENT for Legacy usage */
361 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_UNKNOWN)
363 hsd->Init.TranceiverPresent = SDMMC_TRANSCEIVER_PRESENT;
365 #endif
366 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
367 /* Reset Callback pointers in HAL_SD_STATE_RESET only */
368 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
369 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
370 hsd->ErrorCallback = HAL_SD_ErrorCallback;
371 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
372 hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuf0CpltCallback;
373 hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuf1CpltCallback;
374 hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuf0CpltCallback;
375 hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuf1CpltCallback;
376 #if (USE_SD_TRANSCEIVER != 0U)
377 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
379 hsd->DriveTransceiver_1_8V_Callback = HAL_SD_DriveTransceiver_1_8V_Callback;
381 #endif /* USE_SD_TRANSCEIVER */
383 if(hsd->MspInitCallback == NULL)
385 hsd->MspInitCallback = HAL_SD_MspInit;
388 /* Init the low level hardware */
389 hsd->MspInitCallback(hsd);
390 #else
391 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
392 HAL_SD_MspInit(hsd);
393 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
396 hsd->State = HAL_SD_STATE_BUSY;
398 /* Initialize the Card parameters */
399 if (HAL_SD_InitCard(hsd) != HAL_OK)
401 return HAL_ERROR;
404 if( HAL_SD_GetCardStatus(hsd, &CardStatus) != HAL_OK)
406 return HAL_ERROR;
408 /* Get Initial Card Speed from Card Status*/
409 speedgrade = CardStatus.UhsSpeedGrade;
410 unitsize = CardStatus.UhsAllocationUnitSize;
411 if ((hsd->SdCard.CardType == CARD_SDHC_SDXC) && ((speedgrade != 0U) || (unitsize != 0U)))
413 hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
415 else
417 if (hsd->SdCard.CardType == CARD_SDHC_SDXC)
419 hsd->SdCard.CardSpeed = CARD_HIGH_SPEED;
421 else
423 hsd->SdCard.CardSpeed = CARD_NORMAL_SPEED;
427 /* Configure the bus wide */
428 if(HAL_SD_ConfigWideBusOperation(hsd, hsd->Init.BusWide) != HAL_OK)
430 return HAL_ERROR;
433 /* Verify that SD card is ready to use after Initialization */
434 tickstart = HAL_GetTick();
435 while((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER))
437 if((HAL_GetTick()-tickstart) >= SDMMC_DATATIMEOUT)
439 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
440 hsd->State= HAL_SD_STATE_READY;
441 return HAL_TIMEOUT;
445 /* Initialize the error code */
446 hsd->ErrorCode = HAL_SD_ERROR_NONE;
448 /* Initialize the SD operation */
449 hsd->Context = SD_CONTEXT_NONE;
451 /* Initialize the SD state */
452 hsd->State = HAL_SD_STATE_READY;
454 return HAL_OK;
458 * @brief Initializes the SD Card.
459 * @param hsd: Pointer to SD handle
460 * @note This function initializes the SD card. It could be used when a card
461 re-initialization is needed.
462 * @retval HAL status
464 HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
466 uint32_t errorstate;
467 SD_InitTypeDef Init;
468 uint32_t sdmmc_clk;
470 /* Default SDMMC peripheral configuration for SD card initialization */
471 Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
472 Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
473 Init.BusWide = SDMMC_BUS_WIDE_1B;
474 Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
476 /* Init Clock should be less or equal to 400Khz*/
477 sdmmc_clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SDMMC);
478 Init.ClockDiv = sdmmc_clk/(2U*400000U);
480 #if (USE_SD_TRANSCEIVER != 0U)
481 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
483 /* Set Transceiver polarity */
484 hsd->Instance->POWER |= SDMMC_POWER_DIRPOL;
486 #elif defined (USE_SD_DIRPOL)
487 /* Set Transceiver polarity */
488 hsd->Instance->POWER |= SDMMC_POWER_DIRPOL;
489 #endif /* USE_SD_TRANSCEIVER */
491 /* Initialize SDMMC peripheral interface with default configuration */
492 (void)SDMMC_Init(hsd->Instance, Init);
494 /* Set Power State to ON */
495 (void)SDMMC_PowerState_ON(hsd->Instance);
497 /* wait 74 Cycles: required power up waiting time before starting
498 the SD initialization sequence */
499 sdmmc_clk = sdmmc_clk/(2U*Init.ClockDiv);
501 if(sdmmc_clk != 0U)
503 HAL_Delay(1U+ (74U*1000U/(sdmmc_clk)));
505 else
507 HAL_Delay(2U);
510 /* Identify card operating voltage */
511 errorstate = SD_PowerON(hsd);
512 if(errorstate != HAL_SD_ERROR_NONE)
514 hsd->State = HAL_SD_STATE_READY;
515 hsd->ErrorCode |= errorstate;
516 return HAL_ERROR;
519 /* Card initialization */
520 errorstate = SD_InitCard(hsd);
521 if(errorstate != HAL_SD_ERROR_NONE)
523 hsd->State = HAL_SD_STATE_READY;
524 hsd->ErrorCode |= errorstate;
525 return HAL_ERROR;
528 /* Set Block Size for Card */
529 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
530 if(errorstate != HAL_SD_ERROR_NONE)
532 /* Clear all the static flags */
533 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
534 hsd->ErrorCode |= errorstate;
535 hsd->State = HAL_SD_STATE_READY;
536 return HAL_ERROR;
539 return HAL_OK;
543 * @brief De-Initializes the SD card.
544 * @param hsd: Pointer to SD handle
545 * @retval HAL status
547 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
549 /* Check the SD handle allocation */
550 if(hsd == NULL)
552 return HAL_ERROR;
555 /* Check the parameters */
556 assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
558 hsd->State = HAL_SD_STATE_BUSY;
560 #if (USE_SD_TRANSCEIVER != 0U)
561 /* Desactivate the 1.8V Mode */
562 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
564 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
565 if(hsd->DriveTransceiver_1_8V_Callback == NULL)
567 hsd->DriveTransceiver_1_8V_Callback = HAL_SD_DriveTransceiver_1_8V_Callback;
569 hsd->DriveTransceiver_1_8V_Callback(RESET);
570 #else
571 HAL_SD_DriveTransceiver_1_8V_Callback(RESET);
572 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
574 #endif /* USE_SD_TRANSCEIVER */
576 /* Set SD power state to off */
577 SD_PowerOFF(hsd);
579 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
580 if(hsd->MspDeInitCallback == NULL)
582 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
585 /* DeInit the low level hardware */
586 hsd->MspDeInitCallback(hsd);
587 #else
588 /* De-Initialize the MSP layer */
589 HAL_SD_MspDeInit(hsd);
590 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
592 hsd->ErrorCode = HAL_SD_ERROR_NONE;
593 hsd->State = HAL_SD_STATE_RESET;
595 return HAL_OK;
600 * @brief Initializes the SD MSP.
601 * @param hsd: Pointer to SD handle
602 * @retval None
604 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
606 /* Prevent unused argument(s) compilation warning */
607 UNUSED(hsd);
609 /* NOTE : This function should not be modified, when the callback is needed,
610 the HAL_SD_MspInit could be implemented in the user file
615 * @brief De-Initialize SD MSP.
616 * @param hsd: Pointer to SD handle
617 * @retval None
619 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
621 /* Prevent unused argument(s) compilation warning */
622 UNUSED(hsd);
624 /* NOTE : This function should not be modified, when the callback is needed,
625 the HAL_SD_MspDeInit could be implemented in the user file
630 * @}
633 /** @addtogroup SD_Exported_Functions_Group2
634 * @brief Data transfer functions
636 @verbatim
637 ==============================================================================
638 ##### IO operation functions #####
639 ==============================================================================
640 [..]
641 This subsection provides a set of functions allowing to manage the data
642 transfer from/to SD card.
644 @endverbatim
645 * @{
649 * @brief Reads block(s) from a specified address in a card. The Data transfer
650 * is managed by polling mode.
651 * @note This API should be followed by a check on the card state through
652 * HAL_SD_GetCardState().
653 * @param hsd: Pointer to SD handle
654 * @param pData: pointer to the buffer that will contain the received data
655 * @param BlockAdd: Block Address from where data is to be read
656 * @param NumberOfBlocks: Number of SD blocks to read
657 * @param Timeout: Specify timeout value
658 * @retval HAL status
660 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
662 SDMMC_DataInitTypeDef config;
663 uint32_t errorstate;
664 uint32_t tickstart = HAL_GetTick();
665 uint32_t count, data, dataremaining;
666 uint32_t add = BlockAdd;
667 uint8_t *tempbuff = pData;
669 if(NULL == pData)
671 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
672 return HAL_ERROR;
675 if(hsd->State == HAL_SD_STATE_READY)
677 hsd->ErrorCode = HAL_SD_ERROR_NONE;
679 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
681 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
682 return HAL_ERROR;
685 hsd->State = HAL_SD_STATE_BUSY;
687 /* Initialize data control register */
688 hsd->Instance->DCTRL = 0U;
690 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
692 add *= 512U;
695 /* Configure the SD DPSM (Data Path State Machine) */
696 config.DataTimeOut = SDMMC_DATATIMEOUT;
697 config.DataLength = NumberOfBlocks * BLOCKSIZE;
698 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
699 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
700 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
701 config.DPSM = SDMMC_DPSM_DISABLE;
702 (void)SDMMC_ConfigData(hsd->Instance, &config);
703 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
705 /* Read block(s) in polling mode */
706 if(NumberOfBlocks > 1U)
708 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
710 /* Read Multi Block command */
711 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
713 else
715 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
717 /* Read Single Block command */
718 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
720 if(errorstate != HAL_SD_ERROR_NONE)
722 /* Clear all the static flags */
723 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
724 hsd->ErrorCode |= errorstate;
725 hsd->State = HAL_SD_STATE_READY;
726 hsd->Context = SD_CONTEXT_NONE;
727 return HAL_ERROR;
730 /* Poll on SDMMC flags */
731 dataremaining = config.DataLength;
732 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
734 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) && (dataremaining >= 32U))
736 /* Read data from SDMMC Rx FIFO */
737 for(count = 0U; count < 8U; count++)
739 data = SDMMC_ReadFIFO(hsd->Instance);
740 *tempbuff = (uint8_t)(data & 0xFFU);
741 tempbuff++;
742 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
743 tempbuff++;
744 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
745 tempbuff++;
746 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
747 tempbuff++;
749 dataremaining -= 32U;
752 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
754 /* Clear all the static flags */
755 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
756 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
757 hsd->State= HAL_SD_STATE_READY;
758 hsd->Context = SD_CONTEXT_NONE;
759 return HAL_TIMEOUT;
762 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
764 /* Send stop transmission command in case of multiblock read */
765 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
767 if(hsd->SdCard.CardType != CARD_SECURED)
769 /* Send stop transmission command */
770 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
771 if(errorstate != HAL_SD_ERROR_NONE)
773 /* Clear all the static flags */
774 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
775 hsd->ErrorCode |= errorstate;
776 hsd->State = HAL_SD_STATE_READY;
777 hsd->Context = SD_CONTEXT_NONE;
778 return HAL_ERROR;
783 /* Get error state */
784 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
786 /* Clear all the static flags */
787 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
788 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
789 hsd->State = HAL_SD_STATE_READY;
790 hsd->Context = SD_CONTEXT_NONE;
791 return HAL_ERROR;
793 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
795 /* Clear all the static flags */
796 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
797 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
798 hsd->State = HAL_SD_STATE_READY;
799 hsd->Context = SD_CONTEXT_NONE;
800 return HAL_ERROR;
802 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
804 /* Clear all the static flags */
805 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
806 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
807 hsd->State = HAL_SD_STATE_READY;
808 hsd->Context = SD_CONTEXT_NONE;
809 return HAL_ERROR;
811 else
813 /* Nothing to do */
816 /* Clear all the static flags */
817 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
819 hsd->State = HAL_SD_STATE_READY;
821 return HAL_OK;
823 else
825 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
826 return HAL_ERROR;
831 * @brief Allows to write block(s) to a specified address in a card. The Data
832 * transfer is managed by polling mode.
833 * @note This API should be followed by a check on the card state through
834 * HAL_SD_GetCardState().
835 * @param hsd: Pointer to SD handle
836 * @param pData: pointer to the buffer that will contain the data to transmit
837 * @param BlockAdd: Block Address where data will be written
838 * @param NumberOfBlocks: Number of SD blocks to write
839 * @param Timeout: Specify timeout value
840 * @retval HAL status
842 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
844 SDMMC_DataInitTypeDef config;
845 uint32_t errorstate;
846 uint32_t tickstart = HAL_GetTick();
847 uint32_t count, data, dataremaining;
848 uint32_t add = BlockAdd;
849 uint8_t *tempbuff = pData;
851 if(NULL == pData)
853 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
854 return HAL_ERROR;
857 if(hsd->State == HAL_SD_STATE_READY)
859 hsd->ErrorCode = HAL_SD_ERROR_NONE;
861 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
863 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
864 return HAL_ERROR;
867 hsd->State = HAL_SD_STATE_BUSY;
869 /* Initialize data control register */
870 hsd->Instance->DCTRL = 0U;
872 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
874 add *= 512U;
877 /* Configure the SD DPSM (Data Path State Machine) */
878 config.DataTimeOut = SDMMC_DATATIMEOUT;
879 config.DataLength = NumberOfBlocks * BLOCKSIZE;
880 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
881 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
882 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
883 config.DPSM = SDMMC_DPSM_DISABLE;
884 (void)SDMMC_ConfigData(hsd->Instance, &config);
885 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
887 /* Write Blocks in Polling mode */
888 if(NumberOfBlocks > 1U)
890 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
892 /* Write Multi Block command */
893 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
895 else
897 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
899 /* Write Single Block command */
900 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
902 if(errorstate != HAL_SD_ERROR_NONE)
904 /* Clear all the static flags */
905 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
906 hsd->ErrorCode |= errorstate;
907 hsd->State = HAL_SD_STATE_READY;
908 hsd->Context = SD_CONTEXT_NONE;
909 return HAL_ERROR;
912 /* Write block(s) in polling mode */
913 dataremaining = config.DataLength;
914 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
916 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) && (dataremaining >= 32U))
918 /* Write data to SDMMC Tx FIFO */
919 for(count = 0U; count < 8U; count++)
921 data = (uint32_t)(*tempbuff);
922 tempbuff++;
923 data |= ((uint32_t)(*tempbuff) << 8U);
924 tempbuff++;
925 data |= ((uint32_t)(*tempbuff) << 16U);
926 tempbuff++;
927 data |= ((uint32_t)(*tempbuff) << 24U);
928 tempbuff++;
929 (void)SDMMC_WriteFIFO(hsd->Instance, &data);
931 dataremaining -= 32U;
934 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
936 /* Clear all the static flags */
937 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
938 hsd->ErrorCode |= errorstate;
939 hsd->State = HAL_SD_STATE_READY;
940 hsd->Context = SD_CONTEXT_NONE;
941 return HAL_TIMEOUT;
944 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
946 /* Send stop transmission command in case of multiblock write */
947 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
949 if(hsd->SdCard.CardType != CARD_SECURED)
951 /* Send stop transmission command */
952 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
953 if(errorstate != HAL_SD_ERROR_NONE)
955 /* Clear all the static flags */
956 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
957 hsd->ErrorCode |= errorstate;
958 hsd->State = HAL_SD_STATE_READY;
959 hsd->Context = SD_CONTEXT_NONE;
960 return HAL_ERROR;
965 /* Get error state */
966 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
968 /* Clear all the static flags */
969 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
970 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
971 hsd->State = HAL_SD_STATE_READY;
972 hsd->Context = SD_CONTEXT_NONE;
973 return HAL_ERROR;
975 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
977 /* Clear all the static flags */
978 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
979 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
980 hsd->State = HAL_SD_STATE_READY;
981 hsd->Context = SD_CONTEXT_NONE;
982 return HAL_ERROR;
984 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
986 /* Clear all the static flags */
987 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
988 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
989 hsd->State = HAL_SD_STATE_READY;
990 hsd->Context = SD_CONTEXT_NONE;
991 return HAL_ERROR;
993 else
995 /* Nothing to do */
998 /* Clear all the static flags */
999 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1001 hsd->State = HAL_SD_STATE_READY;
1003 return HAL_OK;
1005 else
1007 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
1008 return HAL_ERROR;
1013 * @brief Reads block(s) from a specified address in a card. The Data transfer
1014 * is managed in interrupt mode.
1015 * @note This API should be followed by a check on the card state through
1016 * HAL_SD_GetCardState().
1017 * @note You could also check the IT transfer process through the SD Rx
1018 * interrupt event.
1019 * @param hsd: Pointer to SD handle
1020 * @param pData: Pointer to the buffer that will contain the received data
1021 * @param BlockAdd: Block Address from where data is to be read
1022 * @param NumberOfBlocks: Number of blocks to read.
1023 * @retval HAL status
1025 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1027 SDMMC_DataInitTypeDef config;
1028 uint32_t errorstate;
1029 uint32_t add = BlockAdd;
1031 if(NULL == pData)
1033 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1034 return HAL_ERROR;
1037 if(hsd->State == HAL_SD_STATE_READY)
1039 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1041 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1043 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1044 return HAL_ERROR;
1047 hsd->State = HAL_SD_STATE_BUSY;
1049 /* Initialize data control register */
1050 hsd->Instance->DCTRL = 0U;
1052 hsd->pRxBuffPtr = pData;
1053 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1055 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1057 add *= 512U;
1060 /* Configure the SD DPSM (Data Path State Machine) */
1061 config.DataTimeOut = SDMMC_DATATIMEOUT;
1062 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1063 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1064 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1065 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1066 config.DPSM = SDMMC_DPSM_DISABLE;
1067 (void)SDMMC_ConfigData(hsd->Instance, &config);
1068 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1070 /* Read Blocks in IT mode */
1071 if(NumberOfBlocks > 1U)
1073 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1075 /* Read Multi Block command */
1076 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1078 else
1080 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
1082 /* Read Single Block command */
1083 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1085 if(errorstate != HAL_SD_ERROR_NONE)
1087 /* Clear all the static flags */
1088 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1089 hsd->ErrorCode |= errorstate;
1090 hsd->State = HAL_SD_STATE_READY;
1091 hsd->Context = SD_CONTEXT_NONE;
1092 return HAL_ERROR;
1095 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
1097 return HAL_OK;
1099 else
1101 return HAL_BUSY;
1106 * @brief Writes block(s) to a specified address in a card. The Data transfer
1107 * is managed in interrupt mode.
1108 * @note This API should be followed by a check on the card state through
1109 * HAL_SD_GetCardState().
1110 * @note You could also check the IT transfer process through the SD Tx
1111 * interrupt event.
1112 * @param hsd: Pointer to SD handle
1113 * @param pData: Pointer to the buffer that will contain the data to transmit
1114 * @param BlockAdd: Block Address where data will be written
1115 * @param NumberOfBlocks: Number of blocks to write
1116 * @retval HAL status
1118 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1120 SDMMC_DataInitTypeDef config;
1121 uint32_t errorstate;
1122 uint32_t add = BlockAdd;
1124 if(NULL == pData)
1126 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1127 return HAL_ERROR;
1130 if(hsd->State == HAL_SD_STATE_READY)
1132 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1134 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1136 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1137 return HAL_ERROR;
1140 hsd->State = HAL_SD_STATE_BUSY;
1142 /* Initialize data control register */
1143 hsd->Instance->DCTRL = 0U;
1145 hsd->pTxBuffPtr = pData;
1146 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1148 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1150 add *= 512U;
1153 /* Configure the SD DPSM (Data Path State Machine) */
1154 config.DataTimeOut = SDMMC_DATATIMEOUT;
1155 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1156 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1157 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1158 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1159 config.DPSM = SDMMC_DPSM_DISABLE;
1160 (void)SDMMC_ConfigData(hsd->Instance, &config);
1162 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1164 /* Write Blocks in Polling mode */
1165 if(NumberOfBlocks > 1U)
1167 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1169 /* Write Multi Block command */
1170 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1172 else
1174 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1176 /* Write Single Block command */
1177 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1179 if(errorstate != HAL_SD_ERROR_NONE)
1181 /* Clear all the static flags */
1182 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1183 hsd->ErrorCode |= errorstate;
1184 hsd->State = HAL_SD_STATE_READY;
1185 hsd->Context = SD_CONTEXT_NONE;
1186 return HAL_ERROR;
1189 /* Enable transfer interrupts */
1190 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
1192 return HAL_OK;
1194 else
1196 return HAL_BUSY;
1201 * @brief Reads block(s) from a specified address in a card. The Data transfer
1202 * is managed by DMA mode.
1203 * @note This API should be followed by a check on the card state through
1204 * HAL_SD_GetCardState().
1205 * @note You could also check the DMA transfer process through the SD Rx
1206 * interrupt event.
1207 * @param hsd: Pointer SD handle
1208 * @param pData: Pointer to the buffer that will contain the received data
1209 * @param BlockAdd: Block Address from where data is to be read
1210 * @param NumberOfBlocks: Number of blocks to read.
1211 * @retval HAL status
1213 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1215 SDMMC_DataInitTypeDef config;
1216 uint32_t errorstate;
1217 uint32_t add = BlockAdd;
1219 if(NULL == pData)
1221 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1222 return HAL_ERROR;
1225 if(hsd->State == HAL_SD_STATE_READY)
1227 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1229 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1231 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1232 return HAL_ERROR;
1235 hsd->State = HAL_SD_STATE_BUSY;
1237 /* Initialize data control register */
1238 hsd->Instance->DCTRL = 0U;
1240 hsd->pRxBuffPtr = pData;
1241 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1243 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1245 add *= 512U;
1248 /* Configure the SD DPSM (Data Path State Machine) */
1249 config.DataTimeOut = SDMMC_DATATIMEOUT;
1250 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1251 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1252 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1253 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1254 config.DPSM = SDMMC_DPSM_DISABLE;
1255 (void)SDMMC_ConfigData(hsd->Instance, &config);
1257 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1258 hsd->Instance->IDMABASE0 = (uint32_t) pData ;
1259 hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1261 /* Read Blocks in DMA mode */
1262 if(NumberOfBlocks > 1U)
1264 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1266 /* Read Multi Block command */
1267 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1269 else
1271 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1273 /* Read Single Block command */
1274 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1276 if(errorstate != HAL_SD_ERROR_NONE)
1278 /* Clear all the static flags */
1279 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1280 hsd->ErrorCode |= errorstate;
1281 hsd->State = HAL_SD_STATE_READY;
1282 hsd->Context = SD_CONTEXT_NONE;
1283 return HAL_ERROR;
1286 /* Enable transfer interrupts */
1287 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1290 return HAL_OK;
1292 else
1294 return HAL_BUSY;
1299 * @brief Writes block(s) to a specified address in a card. The Data transfer
1300 * is managed by DMA mode.
1301 * @note This API should be followed by a check on the card state through
1302 * HAL_SD_GetCardState().
1303 * @note You could also check the DMA transfer process through the SD Tx
1304 * interrupt event.
1305 * @param hsd: Pointer to SD handle
1306 * @param pData: Pointer to the buffer that will contain the data to transmit
1307 * @param BlockAdd: Block Address where data will be written
1308 * @param NumberOfBlocks: Number of blocks to write
1309 * @retval HAL status
1311 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1313 SDMMC_DataInitTypeDef config;
1314 uint32_t errorstate;
1315 uint32_t add = BlockAdd;
1317 if(NULL == pData)
1319 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1320 return HAL_ERROR;
1323 if(hsd->State == HAL_SD_STATE_READY)
1325 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1327 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1329 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1330 return HAL_ERROR;
1333 hsd->State = HAL_SD_STATE_BUSY;
1335 /* Initialize data control register */
1336 hsd->Instance->DCTRL = 0U;
1338 hsd->pTxBuffPtr = pData;
1339 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1341 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1343 add *= 512U;
1346 /* Configure the SD DPSM (Data Path State Machine) */
1347 config.DataTimeOut = SDMMC_DATATIMEOUT;
1348 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1349 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1350 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1351 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1352 config.DPSM = SDMMC_DPSM_DISABLE;
1353 (void)SDMMC_ConfigData(hsd->Instance, &config);
1356 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1358 hsd->Instance->IDMABASE0 = (uint32_t) pData ;
1359 hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1361 /* Write Blocks in Polling mode */
1362 if(NumberOfBlocks > 1U)
1364 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1366 /* Write Multi Block command */
1367 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1369 else
1371 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1373 /* Write Single Block command */
1374 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1376 if(errorstate != HAL_SD_ERROR_NONE)
1378 /* Clear all the static flags */
1379 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1380 hsd->ErrorCode |= errorstate;
1381 hsd->State = HAL_SD_STATE_READY;
1382 hsd->Context = SD_CONTEXT_NONE;
1383 return HAL_ERROR;
1386 /* Enable transfer interrupts */
1387 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1389 return HAL_OK;
1391 else
1393 return HAL_BUSY;
1398 * @brief Erases the specified memory area of the given SD card.
1399 * @note This API should be followed by a check on the card state through
1400 * HAL_SD_GetCardState().
1401 * @param hsd: Pointer to SD handle
1402 * @param BlockStartAdd: Start Block address
1403 * @param BlockEndAdd: End Block address
1404 * @retval HAL status
1406 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1408 uint32_t errorstate;
1409 uint32_t start_add = BlockStartAdd;
1410 uint32_t end_add = BlockEndAdd;
1412 if(hsd->State == HAL_SD_STATE_READY)
1414 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1416 if(end_add < start_add)
1418 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1419 return HAL_ERROR;
1422 if(end_add > (hsd->SdCard.LogBlockNbr))
1424 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1425 return HAL_ERROR;
1428 hsd->State = HAL_SD_STATE_BUSY;
1430 /* Check if the card command class supports erase command */
1431 if(((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U)
1433 /* Clear all the static flags */
1434 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1435 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1436 hsd->State = HAL_SD_STATE_READY;
1437 return HAL_ERROR;
1440 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1442 /* Clear all the static flags */
1443 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1444 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1445 hsd->State = HAL_SD_STATE_READY;
1446 return HAL_ERROR;
1449 /* Get start and end block for high capacity cards */
1450 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1452 start_add *= 512U;
1453 end_add *= 512U;
1456 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1457 if(hsd->SdCard.CardType != CARD_SECURED)
1459 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1460 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
1461 if(errorstate != HAL_SD_ERROR_NONE)
1463 /* Clear all the static flags */
1464 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1465 hsd->ErrorCode |= errorstate;
1466 hsd->State = HAL_SD_STATE_READY;
1467 return HAL_ERROR;
1470 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1471 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
1472 if(errorstate != HAL_SD_ERROR_NONE)
1474 /* Clear all the static flags */
1475 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1476 hsd->ErrorCode |= errorstate;
1477 hsd->State = HAL_SD_STATE_READY;
1478 return HAL_ERROR;
1482 /* Send CMD38 ERASE */
1483 errorstate = SDMMC_CmdErase(hsd->Instance, 0UL);
1484 if(errorstate != HAL_SD_ERROR_NONE)
1486 /* Clear all the static flags */
1487 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1488 hsd->ErrorCode |= errorstate;
1489 hsd->State = HAL_SD_STATE_READY;
1490 return HAL_ERROR;
1493 hsd->State = HAL_SD_STATE_READY;
1495 return HAL_OK;
1497 else
1499 return HAL_BUSY;
1504 * @brief This function handles SD card interrupt request.
1505 * @param hsd: Pointer to SD handle
1506 * @retval None
1508 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1510 uint32_t errorstate;
1511 uint32_t context = hsd->Context;
1513 /* Check for SDMMC interrupt flags */
1514 if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1516 SD_Read_IT(hsd);
1519 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) != RESET)
1521 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND);
1523 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT |\
1524 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE |\
1525 SDMMC_IT_RXFIFOHF);
1527 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
1528 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
1530 if((context & SD_CONTEXT_IT) != 0U)
1532 if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1534 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1535 if(errorstate != HAL_SD_ERROR_NONE)
1537 hsd->ErrorCode |= errorstate;
1538 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1539 hsd->ErrorCallback(hsd);
1540 #else
1541 HAL_SD_ErrorCallback(hsd);
1542 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1546 /* Clear all the static flags */
1547 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1549 hsd->State = HAL_SD_STATE_READY;
1550 hsd->Context = SD_CONTEXT_NONE;
1551 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1553 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1554 hsd->RxCpltCallback(hsd);
1555 #else
1556 HAL_SD_RxCpltCallback(hsd);
1557 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1559 else
1561 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1562 hsd->TxCpltCallback(hsd);
1563 #else
1564 HAL_SD_TxCpltCallback(hsd);
1565 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1568 else if((context & SD_CONTEXT_DMA) != 0U)
1570 hsd->Instance->DLEN = 0;
1571 hsd->Instance->DCTRL = 0;
1572 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1574 /* Stop Transfer for Write Multi blocks or Read Multi blocks */
1575 if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1577 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1578 if(errorstate != HAL_SD_ERROR_NONE)
1580 hsd->ErrorCode |= errorstate;
1581 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1582 hsd->ErrorCallback(hsd);
1583 #else
1584 HAL_SD_ErrorCallback(hsd);
1585 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1589 hsd->State = HAL_SD_STATE_READY;
1590 hsd->Context = SD_CONTEXT_NONE;
1591 if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1593 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1594 hsd->TxCpltCallback(hsd);
1595 #else
1596 HAL_SD_TxCpltCallback(hsd);
1597 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1599 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1601 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1602 hsd->RxCpltCallback(hsd);
1603 #else
1604 HAL_SD_RxCpltCallback(hsd);
1605 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1608 else
1610 /* Nothing to do */
1614 else if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1616 SD_Write_IT(hsd);
1619 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR | SDMMC_FLAG_TXUNDERR) != RESET)
1621 /* Set Error code */
1622 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL) != RESET)
1624 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1626 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT) != RESET)
1628 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1630 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXOVERR) != RESET)
1632 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1634 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXUNDERR) != RESET)
1636 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1639 /* Clear All flags */
1640 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1642 /* Disable all interrupts */
1643 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
1644 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
1646 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
1647 hsd->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
1648 hsd->Instance->CMD |= SDMMC_CMD_CMDSTOP;
1649 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
1650 hsd->Instance->CMD &= ~(SDMMC_CMD_CMDSTOP);
1651 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DABORT);
1653 if((context & SD_CONTEXT_IT) != 0U)
1655 /* Set the SD state to ready to be able to start again the process */
1656 hsd->State = HAL_SD_STATE_READY;
1657 hsd->Context = SD_CONTEXT_NONE;
1658 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1659 hsd->ErrorCallback(hsd);
1660 #else
1661 HAL_SD_ErrorCallback(hsd);
1662 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1664 else if((context & SD_CONTEXT_DMA) != 0U)
1666 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
1668 /* Disable Internal DMA */
1669 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
1670 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1672 /* Set the SD state to ready to be able to start again the process */
1673 hsd->State = HAL_SD_STATE_READY;
1674 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1675 hsd->ErrorCallback(hsd);
1676 #else
1677 HAL_SD_ErrorCallback(hsd);
1678 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1681 else
1683 /* Nothing to do */
1687 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_IDMABTC) != RESET)
1689 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_IDMABTC);
1690 if(READ_BIT(hsd->Instance->IDMACTRL, SDMMC_IDMA_IDMABACT) == 0U)
1692 /* Current buffer is buffer0, Transfer complete for buffer1 */
1693 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1695 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1696 hsd->Write_DMADblBuf1CpltCallback(hsd);
1697 #else
1698 HAL_SDEx_Write_DMADoubleBuf1CpltCallback(hsd);
1699 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1701 else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
1703 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1704 hsd->Read_DMADblBuf1CpltCallback(hsd);
1705 #else
1706 HAL_SDEx_Read_DMADoubleBuf1CpltCallback(hsd);
1707 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1710 else /* SD_DMA_BUFFER1 */
1712 /* Current buffer is buffer1, Transfer complete for buffer0 */
1713 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1715 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1716 hsd->Write_DMADblBuf0CpltCallback(hsd);
1717 #else
1718 HAL_SDEx_Write_DMADoubleBuf0CpltCallback(hsd);
1719 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1721 else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
1723 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1724 hsd->Read_DMADblBuf0CpltCallback(hsd);
1725 #else
1726 HAL_SDEx_Read_DMADoubleBuf0CpltCallback(hsd);
1727 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1731 else
1733 /* Nothing to do */
1738 * @brief return the SD state
1739 * @param hsd: Pointer to sd handle
1740 * @retval HAL state
1742 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
1744 return hsd->State;
1748 * @brief Return the SD error code
1749 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
1750 * the configuration information.
1751 * @retval SD Error Code
1753 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
1755 return hsd->ErrorCode;
1759 * @brief Tx Transfer completed callbacks
1760 * @param hsd: Pointer to SD handle
1761 * @retval None
1763 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
1765 /* Prevent unused argument(s) compilation warning */
1766 UNUSED(hsd);
1768 /* NOTE : This function should not be modified, when the callback is needed,
1769 the HAL_SD_TxCpltCallback can be implemented in the user file
1774 * @brief Rx Transfer completed callbacks
1775 * @param hsd: Pointer SD handle
1776 * @retval None
1778 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
1780 /* Prevent unused argument(s) compilation warning */
1781 UNUSED(hsd);
1783 /* NOTE : This function should not be modified, when the callback is needed,
1784 the HAL_SD_RxCpltCallback can be implemented in the user file
1789 * @brief SD error callbacks
1790 * @param hsd: Pointer SD handle
1791 * @retval None
1793 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1795 /* Prevent unused argument(s) compilation warning */
1796 UNUSED(hsd);
1798 /* NOTE : This function should not be modified, when the callback is needed,
1799 the HAL_SD_ErrorCallback can be implemented in the user file
1804 * @brief SD Abort callbacks
1805 * @param hsd: Pointer SD handle
1806 * @retval None
1808 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
1810 /* Prevent unused argument(s) compilation warning */
1811 UNUSED(hsd);
1813 /* NOTE : This function should not be modified, when the callback is needed,
1814 the HAL_SD_AbortCallback can be implemented in the user file
1818 #if (USE_SD_TRANSCEIVER != 0U)
1820 * @brief Enable/Disable the SD Transceiver 1.8V Mode Callback.
1821 * @param status: Voltage Switch State
1822 * @retval None
1824 __weak void HAL_SD_DriveTransceiver_1_8V_Callback(FlagStatus status)
1826 /* Prevent unused argument(s) compilation warning */
1827 UNUSED(status);
1828 /* NOTE : This function should not be modified, when the callback is needed,
1829 the HAL_SD_EnableTransceiver could be implemented in the user file
1832 #endif /* USE_SD_TRANSCEIVER */
1834 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1836 * @brief Register a User SD Callback
1837 * To be used instead of the weak (surcharged) predefined callback
1838 * @param hsd : SD handle
1839 * @param CallbackID : ID of the callback to be registered
1840 * This parameter can be one of the following values:
1841 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1842 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1843 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1844 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1845 * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID
1846 * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID
1847 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID
1848 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID
1849 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1850 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1851 * @param pCallback : pointer to the Callback function
1852 * @retval status
1854 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
1856 HAL_StatusTypeDef status = HAL_OK;
1858 if(pCallback == NULL)
1860 /* Update the error code */
1861 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1862 return HAL_ERROR;
1865 /* Process locked */
1866 __HAL_LOCK(hsd);
1868 if(hsd->State == HAL_SD_STATE_READY)
1870 switch (CallbackID)
1872 case HAL_SD_TX_CPLT_CB_ID :
1873 hsd->TxCpltCallback = pCallback;
1874 break;
1875 case HAL_SD_RX_CPLT_CB_ID :
1876 hsd->RxCpltCallback = pCallback;
1877 break;
1878 case HAL_SD_ERROR_CB_ID :
1879 hsd->ErrorCallback = pCallback;
1880 break;
1881 case HAL_SD_ABORT_CB_ID :
1882 hsd->AbortCpltCallback = pCallback;
1883 break;
1884 case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID :
1885 hsd->Read_DMADblBuf0CpltCallback = pCallback;
1886 break;
1887 case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID :
1888 hsd->Read_DMADblBuf1CpltCallback = pCallback;
1889 break;
1890 case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
1891 hsd->Write_DMADblBuf0CpltCallback = pCallback;
1892 break;
1893 case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
1894 hsd->Write_DMADblBuf1CpltCallback = pCallback;
1895 break;
1896 case HAL_SD_MSP_INIT_CB_ID :
1897 hsd->MspInitCallback = pCallback;
1898 break;
1899 case HAL_SD_MSP_DEINIT_CB_ID :
1900 hsd->MspDeInitCallback = pCallback;
1901 break;
1902 default :
1903 /* Update the error code */
1904 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1905 /* update return status */
1906 status = HAL_ERROR;
1907 break;
1910 else if (hsd->State == HAL_SD_STATE_RESET)
1912 switch (CallbackID)
1914 case HAL_SD_MSP_INIT_CB_ID :
1915 hsd->MspInitCallback = pCallback;
1916 break;
1917 case HAL_SD_MSP_DEINIT_CB_ID :
1918 hsd->MspDeInitCallback = pCallback;
1919 break;
1920 default :
1921 /* Update the error code */
1922 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1923 /* update return status */
1924 status = HAL_ERROR;
1925 break;
1928 else
1930 /* Update the error code */
1931 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1932 /* update return status */
1933 status = HAL_ERROR;
1936 /* Release Lock */
1937 __HAL_UNLOCK(hsd);
1938 return status;
1942 * @brief Unregister a User SD Callback
1943 * SD Callback is redirected to the weak (surcharged) predefined callback
1944 * @param hsd : SD handle
1945 * @param CallbackID : ID of the callback to be unregistered
1946 * This parameter can be one of the following values:
1947 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1948 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1949 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1950 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1951 * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID
1952 * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID
1953 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID
1954 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID
1955 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1956 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1957 * @retval status
1959 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
1961 HAL_StatusTypeDef status = HAL_OK;
1963 /* Process locked */
1964 __HAL_LOCK(hsd);
1966 if(hsd->State == HAL_SD_STATE_READY)
1968 switch (CallbackID)
1970 case HAL_SD_TX_CPLT_CB_ID :
1971 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
1972 break;
1973 case HAL_SD_RX_CPLT_CB_ID :
1974 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
1975 break;
1976 case HAL_SD_ERROR_CB_ID :
1977 hsd->ErrorCallback = HAL_SD_ErrorCallback;
1978 break;
1979 case HAL_SD_ABORT_CB_ID :
1980 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
1981 break;
1982 case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID :
1983 hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuf0CpltCallback;
1984 break;
1985 case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID :
1986 hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuf1CpltCallback;
1987 break;
1988 case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
1989 hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuf0CpltCallback;
1990 break;
1991 case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
1992 hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuf1CpltCallback;
1993 break;
1994 case HAL_SD_MSP_INIT_CB_ID :
1995 hsd->MspInitCallback = HAL_SD_MspInit;
1996 break;
1997 case HAL_SD_MSP_DEINIT_CB_ID :
1998 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1999 break;
2000 default :
2001 /* Update the error code */
2002 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2003 /* update return status */
2004 status = HAL_ERROR;
2005 break;
2008 else if (hsd->State == HAL_SD_STATE_RESET)
2010 switch (CallbackID)
2012 case HAL_SD_MSP_INIT_CB_ID :
2013 hsd->MspInitCallback = HAL_SD_MspInit;
2014 break;
2015 case HAL_SD_MSP_DEINIT_CB_ID :
2016 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
2017 break;
2018 default :
2019 /* Update the error code */
2020 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2021 /* update return status */
2022 status = HAL_ERROR;
2023 break;
2026 else
2028 /* Update the error code */
2029 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2030 /* update return status */
2031 status = HAL_ERROR;
2034 /* Release Lock */
2035 __HAL_UNLOCK(hsd);
2036 return status;
2039 #if (USE_SD_TRANSCEIVER != 0U)
2041 * @brief Register a User SD Transceiver Callback
2042 * To be used instead of the weak (surcharged) predefined callback
2043 * @param hsd : SD handle
2044 * @param pCallback : pointer to the Callback function
2045 * @retval status
2047 HAL_StatusTypeDef HAL_SD_RegisterTransceiverCallback(SD_HandleTypeDef *hsd, pSD_TransceiverCallbackTypeDef pCallback)
2049 HAL_StatusTypeDef status = HAL_OK;
2051 if(pCallback == NULL)
2053 /* Update the error code */
2054 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2055 return HAL_ERROR;
2058 /* Process locked */
2059 __HAL_LOCK(hsd);
2061 if(hsd->State == HAL_SD_STATE_READY)
2063 hsd->DriveTransceiver_1_8V_Callback = pCallback;
2065 else
2067 /* Update the error code */
2068 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2069 /* update return status */
2070 status = HAL_ERROR;
2073 /* Release Lock */
2074 __HAL_UNLOCK(hsd);
2075 return status;
2079 * @brief Unregister a User SD Transceiver Callback
2080 * SD Callback is redirected to the weak (surcharged) predefined callback
2081 * @param hsd : SD handle
2082 * @retval status
2084 HAL_StatusTypeDef HAL_SD_UnRegisterTransceiverCallback(SD_HandleTypeDef *hsd)
2086 HAL_StatusTypeDef status = HAL_OK;
2088 /* Process locked */
2089 __HAL_LOCK(hsd);
2091 if(hsd->State == HAL_SD_STATE_READY)
2093 hsd->DriveTransceiver_1_8V_Callback = HAL_SD_DriveTransceiver_1_8V_Callback;
2095 else
2097 /* Update the error code */
2098 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2099 /* update return status */
2100 status = HAL_ERROR;
2103 /* Release Lock */
2104 __HAL_UNLOCK(hsd);
2105 return status;
2107 #endif /* USE_SD_TRANSCEIVER */
2108 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2111 * @}
2114 /** @addtogroup SD_Exported_Functions_Group3
2115 * @brief management functions
2117 @verbatim
2118 ==============================================================================
2119 ##### Peripheral Control functions #####
2120 ==============================================================================
2121 [..]
2122 This subsection provides a set of functions allowing to control the SD card
2123 operations and get the related information
2125 @endverbatim
2126 * @{
2130 * @brief Returns information the information of the card which are stored on
2131 * the CID register.
2132 * @param hsd: Pointer to SD handle
2133 * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that
2134 * contains all CID register parameters
2135 * @retval HAL status
2137 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
2139 pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
2141 pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
2143 pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
2145 pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
2147 pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
2149 pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
2151 pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
2153 pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
2155 pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
2157 pCID->Reserved2 = 1U;
2159 return HAL_OK;
2163 * @brief Returns information the information of the card which are stored on
2164 * the CSD register.
2165 * @param hsd: Pointer to SD handle
2166 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
2167 * contains all CSD register parameters
2168 * @retval HAL status
2170 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
2172 pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
2174 pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
2176 pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
2178 pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
2180 pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
2182 pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
2184 pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
2186 pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
2188 pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
2190 pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
2192 pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
2194 pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
2196 pCSD->Reserved2 = 0U; /*!< Reserved */
2198 if(hsd->SdCard.CardType == CARD_SDSC)
2200 pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
2202 pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
2204 pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
2206 pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
2208 pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
2210 pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
2212 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2213 hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2214 hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2216 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
2217 hsd->SdCard.LogBlockSize = 512U;
2219 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
2221 /* Byte 7 */
2222 pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
2224 hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
2225 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
2226 hsd->SdCard.BlockSize = 512U;
2227 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
2229 else
2231 /* Clear all the static flags */
2232 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2233 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2234 hsd->State = HAL_SD_STATE_READY;
2235 return HAL_ERROR;
2238 pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
2240 pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
2242 pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
2244 pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
2246 pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
2248 pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
2250 pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
2252 pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
2254 pCSD->Reserved3 = 0;
2256 pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
2258 pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
2260 pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
2262 pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
2264 pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
2266 pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
2268 pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
2270 pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
2272 pCSD->Reserved4 = 1;
2274 return HAL_OK;
2278 * @brief Gets the SD status info.
2279 * @param hsd: Pointer to SD handle
2280 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
2281 * will contain the SD card status information
2282 * @retval HAL status
2284 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
2286 uint32_t sd_status[16];
2287 uint32_t errorstate;
2288 HAL_StatusTypeDef status = HAL_OK;
2290 errorstate = SD_SendSDStatus(hsd, sd_status);
2291 if(errorstate != HAL_SD_ERROR_NONE)
2293 /* Clear all the static flags */
2294 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2295 hsd->ErrorCode |= errorstate;
2296 hsd->State = HAL_SD_STATE_READY;
2297 status = HAL_ERROR;
2299 else
2301 pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
2303 pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
2305 pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
2307 pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
2308 ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
2310 pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
2312 pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
2314 pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
2316 pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
2318 pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
2320 pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
2322 pStatus->UhsSpeedGrade = (uint8_t)((sd_status[3] & 0x00F0U) >> 4U);
2323 pStatus->UhsAllocationUnitSize = (uint8_t)(sd_status[3] & 0x000FU) ;
2324 pStatus->VideoSpeedClass = (uint8_t)((sd_status[4] & 0xFF000000U) >> 24U);
2327 /* Set Block Size for Card */
2328 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
2329 if(errorstate != HAL_SD_ERROR_NONE)
2331 /* Clear all the static flags */
2332 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2333 hsd->ErrorCode = errorstate;
2334 hsd->State = HAL_SD_STATE_READY;
2335 status = HAL_ERROR;
2338 return status;
2342 * @brief Gets the SD card info.
2343 * @param hsd: Pointer to SD handle
2344 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
2345 * will contain the SD card status information
2346 * @retval HAL status
2348 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
2350 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
2351 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
2352 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
2353 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
2354 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
2355 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
2356 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
2357 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
2359 return HAL_OK;
2363 * @brief Enables wide bus operation for the requested card if supported by
2364 * card.
2365 * @param hsd: Pointer to SD handle
2366 * @param WideMode: Specifies the SD card wide bus mode
2367 * This parameter can be one of the following values:
2368 * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer
2369 * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
2370 * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
2371 * @retval HAL status
2373 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
2375 SDMMC_InitTypeDef Init;
2376 uint32_t errorstate;
2377 HAL_StatusTypeDef status = HAL_OK;
2379 /* Check the parameters */
2380 assert_param(IS_SDMMC_BUS_WIDE(WideMode));
2382 /* Change State */
2383 hsd->State = HAL_SD_STATE_BUSY;
2385 if(hsd->SdCard.CardType != CARD_SECURED)
2387 if(WideMode == SDMMC_BUS_WIDE_8B)
2389 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2391 else if(WideMode == SDMMC_BUS_WIDE_4B)
2393 errorstate = SD_WideBus_Enable(hsd);
2395 hsd->ErrorCode |= errorstate;
2397 else if(WideMode == SDMMC_BUS_WIDE_1B)
2399 errorstate = SD_WideBus_Disable(hsd);
2401 hsd->ErrorCode |= errorstate;
2403 else
2405 /* WideMode is not a valid argument*/
2406 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2409 else
2411 /* MMC Card does not support this feature */
2412 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2415 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2417 /* Clear all the static flags */
2418 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2419 status = HAL_ERROR;
2421 else
2423 /* Configure the SDMMC peripheral */
2424 Init.ClockEdge = hsd->Init.ClockEdge;
2425 Init.ClockPowerSave = hsd->Init.ClockPowerSave;
2426 Init.BusWide = WideMode;
2427 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2429 /* Check if user Clock div < Normal speed 25Mhz, no change in Clockdiv */
2430 if(hsd->Init.ClockDiv >= SDMMC_NSpeed_CLK_DIV)
2432 Init.ClockDiv = hsd->Init.ClockDiv;
2434 else if (hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
2436 /* UltraHigh speed SD card,user Clock div */
2437 Init.ClockDiv = hsd->Init.ClockDiv;
2439 else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
2441 /* High speed SD card, Max Frequency = 50Mhz */
2442 Init.ClockDiv = SDMMC_HSpeed_CLK_DIV;
2444 else
2446 /* No High speed SD card, Max Frequency = 25Mhz */
2447 Init.ClockDiv = SDMMC_NSpeed_CLK_DIV;
2450 (void)SDMMC_Init(hsd->Instance, Init);
2453 /* Set Block Size for Card */
2454 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
2455 if(errorstate != HAL_SD_ERROR_NONE)
2457 /* Clear all the static flags */
2458 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2459 hsd->ErrorCode |= errorstate;
2460 status = HAL_ERROR;
2463 /* Change State */
2464 hsd->State = HAL_SD_STATE_READY;
2466 return status;
2470 * @brief Configure the speed bus mode
2471 * @param hsd: Pointer to the SD handle
2472 * @param SpeedMode: Specifies the SD card speed bus mode
2473 * This parameter can be one of the following values:
2474 * @arg SDMMC_SPEED_MODE_AUTO: Max speed mode supported by the card
2475 * @arg SDMMC_SPEED_MODE_DEFAULT: Default Speed/SDR12 mode
2476 * @arg SDMMC_SPEED_MODE_HIGH: High Speed/SDR25 mode
2477 * @arg SDMMC_SPEED_MODE_ULTRA: Ultra high speed mode
2478 * @retval HAL status
2481 HAL_StatusTypeDef HAL_SD_ConfigSpeedBusOperation(SD_HandleTypeDef *hsd, uint32_t SpeedMode)
2483 uint32_t tickstart;
2484 uint32_t errorstate;
2485 HAL_StatusTypeDef status = HAL_OK;
2487 /* Check the parameters */
2488 assert_param(IS_SDMMC_SPEED_MODE(SpeedMode));
2489 /* Change State */
2490 hsd->State = HAL_SD_STATE_BUSY;
2492 #if (USE_SD_TRANSCEIVER != 0U)
2493 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
2495 switch (SpeedMode)
2497 case SDMMC_SPEED_MODE_AUTO:
2499 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2500 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2502 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED;
2503 /* Enable Ultra High Speed */
2504 if (SD_UltraHighSpeed(hsd) != HAL_SD_ERROR_NONE)
2506 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2508 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2509 status = HAL_ERROR;
2513 else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
2515 /* Enable High Speed */
2516 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2518 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2519 status = HAL_ERROR;
2522 else
2524 /*Nothing to do, Use defaultSpeed */
2526 break;
2528 case SDMMC_SPEED_MODE_ULTRA:
2530 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2531 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2533 /* Enable UltraHigh Speed */
2534 if (SD_UltraHighSpeed(hsd) != HAL_SD_ERROR_NONE)
2536 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2537 status = HAL_ERROR;
2539 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED;
2541 else
2543 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2544 status = HAL_ERROR;
2546 break;
2548 case SDMMC_SPEED_MODE_DDR:
2550 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2551 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2553 /* Enable DDR Mode*/
2554 if (SD_DDR_Mode(hsd) != HAL_SD_ERROR_NONE)
2556 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2557 status = HAL_ERROR;
2559 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED | SDMMC_CLKCR_DDR;
2561 else
2563 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2564 status = HAL_ERROR;
2566 break;
2568 case SDMMC_SPEED_MODE_HIGH:
2570 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2571 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2572 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2574 /* Enable High Speed */
2575 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2577 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2578 status = HAL_ERROR;
2581 else
2583 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2584 status = HAL_ERROR;
2586 break;
2588 case SDMMC_SPEED_MODE_DEFAULT:
2589 break;
2590 default:
2591 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2592 status = HAL_ERROR;
2593 break;
2596 else
2598 switch (SpeedMode)
2600 case SDMMC_SPEED_MODE_AUTO:
2602 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2603 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2604 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2606 /* Enable High Speed */
2607 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2609 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2610 status = HAL_ERROR;
2613 else
2615 /*Nothing to do, Use defaultSpeed */
2617 break;
2619 case SDMMC_SPEED_MODE_HIGH:
2621 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2622 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2623 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2625 /* Enable High Speed */
2626 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2628 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2629 status = HAL_ERROR;
2632 else
2634 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2635 status = HAL_ERROR;
2637 break;
2639 case SDMMC_SPEED_MODE_DEFAULT:
2640 break;
2641 case SDMMC_SPEED_MODE_ULTRA: /*not valid without transceiver*/
2642 default:
2643 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2644 status = HAL_ERROR;
2645 break;
2648 #else
2649 switch (SpeedMode)
2651 case SDMMC_SPEED_MODE_AUTO:
2653 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2654 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2655 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2657 /* Enable High Speed */
2658 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2660 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2661 status = HAL_ERROR;
2664 else
2666 /*Nothing to do, Use defaultSpeed */
2668 break;
2670 case SDMMC_SPEED_MODE_HIGH:
2672 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2673 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2674 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2676 /* Enable High Speed */
2677 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2679 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2680 status = HAL_ERROR;
2683 else
2685 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2686 status = HAL_ERROR;
2688 break;
2690 case SDMMC_SPEED_MODE_DEFAULT:
2691 break;
2692 case SDMMC_SPEED_MODE_ULTRA: /*not valid without transceiver*/
2693 default:
2694 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2695 status = HAL_ERROR;
2696 break;
2698 #endif /* USE_SD_TRANSCEIVER */
2700 /* Verify that SD card is ready to use after Speed mode switch*/
2701 tickstart = HAL_GetTick();
2702 while ((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER))
2704 if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2706 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
2707 hsd->State = HAL_SD_STATE_READY;
2708 return HAL_TIMEOUT;
2712 /* Set Block Size for Card */
2713 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
2714 if(errorstate != HAL_SD_ERROR_NONE)
2716 /* Clear all the static flags */
2717 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2718 hsd->ErrorCode |= errorstate;
2719 status = HAL_ERROR;
2722 /* Change State */
2723 hsd->State = HAL_SD_STATE_READY;
2724 return status;
2728 * @brief Gets the current sd card data state.
2729 * @param hsd: pointer to SD handle
2730 * @retval Card state
2732 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2734 uint32_t cardstate;
2735 uint32_t errorstate;
2736 uint32_t resp1 = 0;
2738 errorstate = SD_SendStatus(hsd, &resp1);
2739 if(errorstate != HAL_SD_ERROR_NONE)
2741 hsd->ErrorCode |= errorstate;
2744 cardstate = ((resp1 >> 9U) & 0x0FU);
2746 return (HAL_SD_CardStateTypeDef)cardstate;
2750 * @brief Abort the current transfer and disable the SD.
2751 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2752 * the configuration information for SD module.
2753 * @retval HAL status
2755 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
2757 HAL_SD_CardStateTypeDef CardState;
2759 /* DIsable All interrupts */
2760 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2761 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2763 /* Clear All flags */
2764 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2766 /* If IDMA Context, disable Internal DMA */
2767 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
2769 hsd->State = HAL_SD_STATE_READY;
2771 /* Initialize the SD operation */
2772 hsd->Context = SD_CONTEXT_NONE;
2774 CardState = HAL_SD_GetCardState(hsd);
2775 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2777 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2779 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2781 return HAL_ERROR;
2783 return HAL_OK;
2787 * @brief Abort the current transfer and disable the SD (IT mode).
2788 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2789 * the configuration information for SD module.
2790 * @retval HAL status
2792 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
2794 HAL_SD_CardStateTypeDef CardState;
2796 /* Disable All interrupts */
2797 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2798 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2800 /* If IDMA Context, disable Internal DMA */
2801 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
2803 /* Clear All flags */
2804 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2806 CardState = HAL_SD_GetCardState(hsd);
2807 hsd->State = HAL_SD_STATE_READY;
2809 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2811 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2814 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2816 return HAL_ERROR;
2818 else
2820 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2821 hsd->AbortCpltCallback(hsd);
2822 #else
2823 HAL_SD_AbortCallback(hsd);
2824 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2827 return HAL_OK;
2831 * @}
2835 * @}
2838 /* Private function ----------------------------------------------------------*/
2839 /** @addtogroup SD_Private_Functions
2840 * @{
2845 * @brief Initializes the sd card.
2846 * @param hsd: Pointer to SD handle
2847 * @retval SD Card error state
2849 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
2851 HAL_SD_CardCSDTypeDef CSD;
2852 uint32_t errorstate;
2853 uint16_t sd_rca = 1U;
2855 /* Check the power State */
2856 if(SDMMC_GetPowerState(hsd->Instance) == 0U)
2858 /* Power off */
2859 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2862 if(hsd->SdCard.CardType != CARD_SECURED)
2864 /* Send CMD2 ALL_SEND_CID */
2865 errorstate = SDMMC_CmdSendCID(hsd->Instance);
2866 if(errorstate != HAL_SD_ERROR_NONE)
2868 return errorstate;
2870 else
2872 /* Get Card identification number data */
2873 hsd->CID[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2874 hsd->CID[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
2875 hsd->CID[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
2876 hsd->CID[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
2880 if(hsd->SdCard.CardType != CARD_SECURED)
2882 /* Send CMD3 SET_REL_ADDR with argument 0 */
2883 /* SD Card publishes its RCA. */
2884 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
2885 if(errorstate != HAL_SD_ERROR_NONE)
2887 return errorstate;
2890 if(hsd->SdCard.CardType != CARD_SECURED)
2892 /* Get the SD card RCA */
2893 hsd->SdCard.RelCardAdd = sd_rca;
2895 /* Send CMD9 SEND_CSD with argument as card's RCA */
2896 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2897 if(errorstate != HAL_SD_ERROR_NONE)
2899 return errorstate;
2901 else
2903 /* Get Card Specific Data */
2904 hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2905 hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
2906 hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
2907 hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
2911 /* Get the Card Class */
2912 hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20U);
2914 /* Get CSD parameters */
2915 if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
2917 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2920 /* Select the Card */
2921 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
2922 if(errorstate != HAL_SD_ERROR_NONE)
2924 return errorstate;
2927 /* All cards are initialized */
2928 return HAL_SD_ERROR_NONE;
2932 * @brief Enquires cards about their operating voltage and configures clock
2933 * controls and stores SD information that will be needed in future
2934 * in the SD handle.
2935 * @param hsd: Pointer to SD handle
2936 * @retval error state
2938 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
2940 __IO uint32_t count = 0U;
2941 uint32_t response = 0U, validvoltage = 0U;
2942 uint32_t errorstate;
2943 #if (USE_SD_TRANSCEIVER != 0U)
2944 uint32_t tickstart = HAL_GetTick();
2945 #endif /* USE_SD_TRANSCEIVER */
2947 /* CMD0: GO_IDLE_STATE */
2948 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2949 if(errorstate != HAL_SD_ERROR_NONE)
2951 return errorstate;
2954 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2955 errorstate = SDMMC_CmdOperCond(hsd->Instance);
2956 if(errorstate != HAL_SD_ERROR_NONE)
2958 hsd->SdCard.CardVersion = CARD_V1_X;
2959 /* CMD0: GO_IDLE_STATE */
2960 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2961 if(errorstate != HAL_SD_ERROR_NONE)
2963 return errorstate;
2967 else
2969 hsd->SdCard.CardVersion = CARD_V2_X;
2972 if( hsd->SdCard.CardVersion == CARD_V2_X)
2974 /* SEND CMD55 APP_CMD with RCA as 0 */
2975 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2976 if(errorstate != HAL_SD_ERROR_NONE)
2978 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2981 /* SD CARD */
2982 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2983 while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
2985 /* SEND CMD55 APP_CMD with RCA as 0 */
2986 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2987 if(errorstate != HAL_SD_ERROR_NONE)
2989 return errorstate;
2992 /* Send CMD41 */
2993 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
2994 if(errorstate != HAL_SD_ERROR_NONE)
2996 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2999 /* Get command response */
3000 response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3002 /* Get operating voltage*/
3003 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
3005 count++;
3008 if(count >= SDMMC_MAX_VOLT_TRIAL)
3010 return HAL_SD_ERROR_INVALID_VOLTRANGE;
3013 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
3015 hsd->SdCard.CardType = CARD_SDHC_SDXC;
3016 #if (USE_SD_TRANSCEIVER != 0U)
3017 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
3019 if((response & SD_SWITCH_1_8V_CAPACITY) == SD_SWITCH_1_8V_CAPACITY)
3021 hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
3023 /* Start switching procedue */
3024 hsd->Instance->POWER |= SDMMC_POWER_VSWITCHEN;
3026 /* Send CMD11 to switch 1.8V mode */
3027 errorstate = SDMMC_CmdVoltageSwitch(hsd->Instance);
3028 if(errorstate != HAL_SD_ERROR_NONE)
3030 return errorstate;
3033 /* Check to CKSTOP */
3034 while(( hsd->Instance->STA & SDMMC_FLAG_CKSTOP) != SDMMC_FLAG_CKSTOP)
3036 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3038 return HAL_SD_ERROR_TIMEOUT;
3042 /* Clear CKSTOP Flag */
3043 hsd->Instance->ICR = SDMMC_FLAG_CKSTOP;
3045 /* Check to BusyD0 */
3046 if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) != SDMMC_FLAG_BUSYD0)
3048 /* Error when activate Voltage Switch in SDMMC Peripheral */
3049 return SDMMC_ERROR_UNSUPPORTED_FEATURE;
3051 else
3053 /* Enable Transceiver Switch PIN */
3054 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3055 hsd->DriveTransceiver_1_8V_Callback(SET);
3056 #else
3057 HAL_SD_DriveTransceiver_1_8V_Callback(SET);
3058 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
3060 /* Switch ready */
3061 hsd->Instance->POWER |= SDMMC_POWER_VSWITCH;
3063 /* Check VSWEND Flag */
3064 while(( hsd->Instance->STA & SDMMC_FLAG_VSWEND) != SDMMC_FLAG_VSWEND)
3066 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3068 return HAL_SD_ERROR_TIMEOUT;
3072 /* Clear VSWEND Flag */
3073 hsd->Instance->ICR = SDMMC_FLAG_VSWEND;
3075 /* Check BusyD0 status */
3076 if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) == SDMMC_FLAG_BUSYD0)
3078 /* Error when enabling 1.8V mode */
3079 return HAL_SD_ERROR_INVALID_VOLTRANGE;
3081 /* Switch to 1.8V OK */
3083 /* Disable VSWITCH FLAG from SDMMC Peripheral */
3084 hsd->Instance->POWER = 0x13U;
3086 /* Clean Status flags */
3087 hsd->Instance->ICR = 0xFFFFFFFFU;
3091 #endif /* USE_SD_TRANSCEIVER */
3094 return HAL_SD_ERROR_NONE;
3098 * @brief Turns the SDMMC output signals off.
3099 * @param hsd: Pointer to SD handle
3100 * @retval None
3102 static void SD_PowerOFF(SD_HandleTypeDef *hsd)
3104 /* Set Power State to OFF */
3105 (void)SDMMC_PowerState_OFF(hsd->Instance);
3109 * @brief Send Status info command.
3110 * @param hsd: pointer to SD handle
3111 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
3112 * SD Status register)
3113 * @retval error state
3115 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
3117 SDMMC_DataInitTypeDef config;
3118 uint32_t errorstate;
3119 uint32_t tickstart = HAL_GetTick();
3120 uint32_t count;
3121 uint32_t *pData = pSDstatus;
3123 /* Check SD response */
3124 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3126 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3129 /* Set block size for card if it is not equal to current block size for card */
3130 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
3131 if(errorstate != HAL_SD_ERROR_NONE)
3133 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3134 return errorstate;
3137 /* Send CMD55 */
3138 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3139 if(errorstate != HAL_SD_ERROR_NONE)
3141 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3142 return errorstate;
3145 /* Configure the SD DPSM (Data Path State Machine) */
3146 config.DataTimeOut = SDMMC_DATATIMEOUT;
3147 config.DataLength = 64U;
3148 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
3149 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3150 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3151 config.DPSM = SDMMC_DPSM_ENABLE;
3152 (void)SDMMC_ConfigData(hsd->Instance, &config);
3154 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
3155 errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
3156 if(errorstate != HAL_SD_ERROR_NONE)
3158 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3159 return errorstate;
3162 /* Get status data */
3163 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
3165 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3167 for(count = 0U; count < 8U; count++)
3169 *pData = SDMMC_ReadFIFO(hsd->Instance);
3170 pData++;
3174 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3176 return HAL_SD_ERROR_TIMEOUT;
3180 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3182 return HAL_SD_ERROR_DATA_TIMEOUT;
3184 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3186 return HAL_SD_ERROR_DATA_CRC_FAIL;
3188 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3190 return HAL_SD_ERROR_RX_OVERRUN;
3192 else
3194 /* Nothing to do */
3197 while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DPSMACT)))
3199 *pData = SDMMC_ReadFIFO(hsd->Instance);
3200 pData++;
3202 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3204 return HAL_SD_ERROR_TIMEOUT;
3208 /* Clear all the static status flags*/
3209 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3211 return HAL_SD_ERROR_NONE;
3215 * @brief Returns the current card's status.
3216 * @param hsd: Pointer to SD handle
3217 * @param pCardStatus: pointer to the buffer that will contain the SD card
3218 * status (Card Status register)
3219 * @retval error state
3221 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
3223 uint32_t errorstate;
3225 if(pCardStatus == NULL)
3227 return HAL_SD_ERROR_PARAM;
3230 /* Send Status command */
3231 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3232 if(errorstate != HAL_SD_ERROR_NONE)
3234 return errorstate;
3237 /* Get SD card status */
3238 *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3240 return HAL_SD_ERROR_NONE;
3244 * @brief Enables the SDMMC wide bus mode.
3245 * @param hsd: pointer to SD handle
3246 * @retval error state
3248 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
3250 uint32_t scr[2U] = {0UL, 0UL};
3251 uint32_t errorstate;
3253 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3255 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3258 /* Get SCR Register */
3259 errorstate = SD_FindSCR(hsd, scr);
3260 if(errorstate != HAL_SD_ERROR_NONE)
3262 return errorstate;
3265 /* If requested card supports wide bus operation */
3266 if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
3268 /* Send CMD55 APP_CMD with argument as card's RCA.*/
3269 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3270 if(errorstate != HAL_SD_ERROR_NONE)
3272 return errorstate;
3275 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3276 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
3277 if(errorstate != HAL_SD_ERROR_NONE)
3279 return errorstate;
3282 return HAL_SD_ERROR_NONE;
3284 else
3286 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3291 * @brief Disables the SDMMC wide bus mode.
3292 * @param hsd: Pointer to SD handle
3293 * @retval error state
3295 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3297 uint32_t scr[2U] = {0UL, 0UL};
3298 uint32_t errorstate;
3300 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3302 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3305 /* Get SCR Register */
3306 errorstate = SD_FindSCR(hsd, scr);
3307 if(errorstate != HAL_SD_ERROR_NONE)
3309 return errorstate;
3312 /* If requested card supports 1 bit mode operation */
3313 if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
3315 /* Send CMD55 APP_CMD with argument as card's RCA */
3316 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3317 if(errorstate != HAL_SD_ERROR_NONE)
3319 return errorstate;
3322 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3323 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
3324 if(errorstate != HAL_SD_ERROR_NONE)
3326 return errorstate;
3329 return HAL_SD_ERROR_NONE;
3331 else
3333 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3339 * @brief Finds the SD card SCR register value.
3340 * @param hsd: Pointer to SD handle
3341 * @param pSCR: pointer to the buffer that will contain the SCR value
3342 * @retval error state
3344 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
3346 SDMMC_DataInitTypeDef config;
3347 uint32_t errorstate;
3348 uint32_t tickstart = HAL_GetTick();
3349 uint32_t index = 0U;
3350 uint32_t tempscr[2U] = {0UL, 0UL};
3351 uint32_t *scr = pSCR;
3353 /* Set Block Size To 8 Bytes */
3354 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
3355 if(errorstate != HAL_SD_ERROR_NONE)
3357 return errorstate;
3360 /* Send CMD55 APP_CMD with argument as card's RCA */
3361 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
3362 if(errorstate != HAL_SD_ERROR_NONE)
3364 return errorstate;
3367 config.DataTimeOut = SDMMC_DATATIMEOUT;
3368 config.DataLength = 8U;
3369 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
3370 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3371 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3372 config.DPSM = SDMMC_DPSM_ENABLE;
3373 (void)SDMMC_ConfigData(hsd->Instance, &config);
3375 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3376 errorstate = SDMMC_CmdSendSCR(hsd->Instance);
3377 if(errorstate != HAL_SD_ERROR_NONE)
3379 return errorstate;
3382 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND | SDMMC_FLAG_DATAEND))
3384 if((!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOE)) && (index == 0U))
3386 tempscr[0] = SDMMC_ReadFIFO(hsd->Instance);
3387 tempscr[1] = SDMMC_ReadFIFO(hsd->Instance);
3388 index++;
3392 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3394 return HAL_SD_ERROR_TIMEOUT;
3398 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3400 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3402 return HAL_SD_ERROR_DATA_TIMEOUT;
3404 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3406 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3408 return HAL_SD_ERROR_DATA_CRC_FAIL;
3410 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3412 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3414 return HAL_SD_ERROR_RX_OVERRUN;
3416 else
3418 /* No error flag set */
3419 /* Clear all the static flags */
3420 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3422 *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
3423 ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24));
3424 scr++;
3425 *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
3426 ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24));
3430 return HAL_SD_ERROR_NONE;
3434 * @brief Wrap up reading in non-blocking mode.
3435 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3436 * the configuration information.
3437 * @retval None
3439 static void SD_Read_IT(SD_HandleTypeDef *hsd)
3441 uint32_t count, data;
3442 uint8_t* tmp;
3444 tmp = hsd->pRxBuffPtr;
3446 if (hsd->RxXferSize >= 32U)
3448 /* Read data from SDMMC Rx FIFO */
3449 for(count = 0U; count < 8U; count++)
3451 data = SDMMC_ReadFIFO(hsd->Instance);
3452 *tmp = (uint8_t)(data & 0xFFU);
3453 tmp++;
3454 *tmp = (uint8_t)((data >> 8U) & 0xFFU);
3455 tmp++;
3456 *tmp = (uint8_t)((data >> 16U) & 0xFFU);
3457 tmp++;
3458 *tmp = (uint8_t)((data >> 24U) & 0xFFU);
3459 tmp++;
3462 hsd->pRxBuffPtr = tmp;
3463 hsd->RxXferSize -= 32U;
3468 * @brief Wrap up writing in non-blocking mode.
3469 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3470 * the configuration information.
3471 * @retval None
3473 static void SD_Write_IT(SD_HandleTypeDef *hsd)
3475 uint32_t count, data;
3476 uint8_t* tmp;
3478 tmp = hsd->pTxBuffPtr;
3480 if (hsd->TxXferSize >= 32U)
3482 /* Write data to SDMMC Tx FIFO */
3483 for(count = 0U; count < 8U; count++)
3485 data = (uint32_t)(*tmp);
3486 tmp++;
3487 data |= ((uint32_t)(*tmp) << 8U);
3488 tmp++;
3489 data |= ((uint32_t)(*tmp) << 16U);
3490 tmp++;
3491 data |= ((uint32_t)(*tmp) << 24U);
3492 tmp++;
3493 (void)SDMMC_WriteFIFO(hsd->Instance, &data);
3496 hsd->pTxBuffPtr = tmp;
3497 hsd->TxXferSize -= 32U;
3502 * @brief Switches the SD card to High Speed mode.
3503 * This API must be used after "Transfer State"
3504 * @note This operation should be followed by the configuration
3505 * of PLL to have SDMMCCK clock between 50 and 120 MHz
3506 * @param hsd: SD handle
3507 * @retval SD Card error state
3509 uint32_t SD_HighSpeed(SD_HandleTypeDef *hsd)
3511 uint32_t errorstate = HAL_SD_ERROR_NONE;
3512 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
3513 uint32_t SD_hs[16] = {0};
3514 uint32_t count, loop = 0 ;
3515 uint32_t Timeout = HAL_GetTick();
3517 if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
3519 /* Standard Speed Card <= 12.5Mhz */
3520 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3523 if(hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
3525 /* Initialize the Data control register */
3526 hsd->Instance->DCTRL = 0;
3527 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
3529 if (errorstate != HAL_SD_ERROR_NONE)
3531 return errorstate;
3534 /* Configure the SD DPSM (Data Path State Machine) */
3535 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
3536 sdmmc_datainitstructure.DataLength = 64U;
3537 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
3538 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3539 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3540 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
3542 (void)SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure);
3545 errorstate = SDMMC_CmdSwitch(hsd->Instance,SDMMC_SDR25_SWITCH_PATTERN);
3546 if(errorstate != HAL_SD_ERROR_NONE)
3548 return errorstate;
3551 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
3553 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3555 for (count = 0U; count < 8U; count++)
3557 SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
3559 loop ++;
3562 if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
3564 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
3565 hsd->State= HAL_SD_STATE_READY;
3566 return HAL_SD_ERROR_TIMEOUT;
3570 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3572 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3574 return errorstate;
3576 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3578 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3580 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
3582 return errorstate;
3584 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3586 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3588 errorstate = SDMMC_ERROR_RX_OVERRUN;
3590 return errorstate;
3592 else
3594 /* No error flag set */
3597 /* Clear all the static flags */
3598 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3600 /* Test if the switch mode HS is ok */
3601 if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
3603 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
3608 return errorstate;
3611 #if (USE_SD_TRANSCEIVER != 0U)
3613 * @brief Switches the SD card to Ultra High Speed mode.
3614 * This API must be used after "Transfer State"
3615 * @note This operation should be followed by the configuration
3616 * of PLL to have SDMMCCK clock between 50 and 120 MHz
3617 * @param hsd: SD handle
3618 * @retval SD Card error state
3620 static uint32_t SD_UltraHighSpeed(SD_HandleTypeDef *hsd)
3622 uint32_t errorstate = HAL_SD_ERROR_NONE;
3623 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
3624 uint32_t SD_hs[16] = {0};
3625 uint32_t count, loop = 0 ;
3626 uint32_t Timeout = HAL_GetTick();
3628 if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
3630 /* Standard Speed Card <= 12.5Mhz */
3631 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3634 if(hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
3636 /* Initialize the Data control register */
3637 hsd->Instance->DCTRL = 0;
3638 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
3640 if (errorstate != HAL_SD_ERROR_NONE)
3642 return errorstate;
3645 /* Configure the SD DPSM (Data Path State Machine) */
3646 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
3647 sdmmc_datainitstructure.DataLength = 64U;
3648 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
3649 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3650 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3651 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
3653 if ( SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK)
3655 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3658 errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_SDR104_SWITCH_PATTERN);
3659 if(errorstate != HAL_SD_ERROR_NONE)
3661 return errorstate;
3664 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
3666 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3668 for (count = 0U; count < 8U; count++)
3670 SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
3672 loop ++;
3675 if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
3677 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
3678 hsd->State= HAL_SD_STATE_READY;
3679 return HAL_SD_ERROR_TIMEOUT;
3683 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3685 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3687 return errorstate;
3689 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3691 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3693 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
3695 return errorstate;
3697 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3699 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3701 errorstate = SDMMC_ERROR_RX_OVERRUN;
3703 return errorstate;
3705 else
3707 /* No error flag set */
3710 /* Clear all the static flags */
3711 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3713 /* Test if the switch mode HS is ok */
3714 if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
3716 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
3718 else
3720 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3721 hsd->DriveTransceiver_1_8V_Callback(SET);
3722 #else
3723 HAL_SD_DriveTransceiver_1_8V_Callback(SET);
3724 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
3725 #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
3726 /* Enable DelayBlock Peripheral */
3727 /* SDMMC_FB_CLK tuned feedback clock selected as receive clock, for SDR104 */
3728 MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX,SDMMC_CLKCR_SELCLKRX_1);
3729 if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
3731 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3733 #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
3737 return errorstate;
3741 * @brief Switches the SD card to Double Data Rate (DDR) mode.
3742 * This API must be used after "Transfer State"
3743 * @note This operation should be followed by the configuration
3744 * of PLL to have SDMMCCK clock less than 50MHz
3745 * @param hsd: SD handle
3746 * @retval SD Card error state
3748 static uint32_t SD_DDR_Mode(SD_HandleTypeDef *hsd)
3750 uint32_t errorstate = HAL_SD_ERROR_NONE;
3751 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
3752 uint32_t SD_hs[16] = {0};
3753 uint32_t count, loop = 0 ;
3754 uint32_t Timeout = HAL_GetTick();
3756 if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
3758 /* Standard Speed Card <= 12.5Mhz */
3759 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3762 if(hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
3764 /* Initialize the Data control register */
3765 hsd->Instance->DCTRL = 0;
3766 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
3768 if (errorstate != HAL_SD_ERROR_NONE)
3770 return errorstate;
3773 /* Configure the SD DPSM (Data Path State Machine) */
3774 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
3775 sdmmc_datainitstructure.DataLength = 64U;
3776 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
3777 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3778 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3779 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
3781 if ( SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK)
3783 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3786 errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_DDR50_SWITCH_PATTERN);
3787 if(errorstate != HAL_SD_ERROR_NONE)
3789 return errorstate;
3792 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
3794 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3796 for (count = 0U; count < 8U; count++)
3798 SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
3800 loop ++;
3803 if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
3805 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
3806 hsd->State= HAL_SD_STATE_READY;
3807 return HAL_SD_ERROR_TIMEOUT;
3811 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3813 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3815 return errorstate;
3817 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3819 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3821 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
3823 return errorstate;
3825 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3827 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3829 errorstate = SDMMC_ERROR_RX_OVERRUN;
3831 return errorstate;
3833 else
3835 /* No error flag set */
3838 /* Clear all the static flags */
3839 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3841 /* Test if the switch mode is ok */
3842 if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
3844 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
3846 else
3848 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3849 hsd->DriveTransceiver_1_8V_Callback(SET);
3850 #else
3851 HAL_SD_DriveTransceiver_1_8V_Callback(SET);
3852 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
3853 #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
3854 /* Enable DelayBlock Peripheral */
3855 /* SDMMC_CKin feedback clock selected as receive clock, for DDR50 */
3856 MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX,SDMMC_CLKCR_SELCLKRX_0);
3857 if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
3859 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3861 #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
3865 return errorstate;
3868 #endif /* USE_SD_TRANSCEIVER */
3871 * @brief Read DMA Buffer 0 Transfer completed callbacks
3872 * @param hsd: SD handle
3873 * @retval None
3875 __weak void HAL_SDEx_Read_DMADoubleBuf0CpltCallback(SD_HandleTypeDef *hsd)
3877 /* Prevent unused argument(s) compilation warning */
3878 UNUSED(hsd);
3880 /* NOTE : This function should not be modified, when the callback is needed,
3881 the HAL_SDEx_Read_DMADoubleBuf0CpltCallback can be implemented in the user file
3886 * @brief Read DMA Buffer 1 Transfer completed callbacks
3887 * @param hsd: SD handle
3888 * @retval None
3890 __weak void HAL_SDEx_Read_DMADoubleBuf1CpltCallback(SD_HandleTypeDef *hsd)
3892 /* Prevent unused argument(s) compilation warning */
3893 UNUSED(hsd);
3895 /* NOTE : This function should not be modified, when the callback is needed,
3896 the HAL_SDEx_Read_DMADoubleBuf1CpltCallback can be implemented in the user file
3901 * @brief Write DMA Buffer 0 Transfer completed callbacks
3902 * @param hsd: SD handle
3903 * @retval None
3905 __weak void HAL_SDEx_Write_DMADoubleBuf0CpltCallback(SD_HandleTypeDef *hsd)
3907 /* Prevent unused argument(s) compilation warning */
3908 UNUSED(hsd);
3910 /* NOTE : This function should not be modified, when the callback is needed,
3911 the HAL_SDEx_Write_DMADoubleBuf0CpltCallback can be implemented in the user file
3916 * @brief Write DMA Buffer 1 Transfer completed callbacks
3917 * @param hsd: SD handle
3918 * @retval None
3920 __weak void HAL_SDEx_Write_DMADoubleBuf1CpltCallback(SD_HandleTypeDef *hsd)
3922 /* Prevent unused argument(s) compilation warning */
3923 UNUSED(hsd);
3925 /* NOTE : This function should not be modified, when the callback is needed,
3926 the HAL_SDEx_Write_DMADoubleBuf1CpltCallback can be implemented in the user file
3932 * @}
3935 #endif /* HAL_SD_MODULE_ENABLED */
3938 * @}
3942 * @}
3945 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/