Set blackbox file handler to NULL after closing file
[inav.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_sd.c
blob4ba47ad84d8578d71a6a402cfc28af1dad8dd17d
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 HAL_StatusTypeDef status;
468 SD_InitTypeDef Init;
469 uint32_t sdmmc_clk;
471 /* Default SDMMC peripheral configuration for SD card initialization */
472 Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
473 Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
474 Init.BusWide = SDMMC_BUS_WIDE_1B;
475 Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
476 Init.ClockDiv = SDMMC_INIT_CLK_DIV;
478 #if (USE_SD_TRANSCEIVER != 0U) || defined (USE_SD_DIRPOL)
479 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
481 /* Set Transceiver polarity */
482 hsd->Instance->POWER |= SDMMC_POWER_DIRPOL;
484 #endif /* USE_SD_TRANSCEIVER */
486 /* Initialize SDMMC peripheral interface with default configuration */
487 status = SDMMC_Init(hsd->Instance, Init);
488 if(status != HAL_OK)
490 return HAL_ERROR;
493 /* Set Power State to ON */
494 status = SDMMC_PowerState_ON(hsd->Instance);
495 if(status != HAL_OK)
497 return HAL_ERROR;
500 /* wait 74 Cycles: required power up waiting time before starting
501 the SD initialization sequence */
502 sdmmc_clk = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_SDMMC)/(2U*SDMMC_INIT_CLK_DIV);
504 if(sdmmc_clk != 0U)
506 HAL_Delay(1U+ (74U*1000U/(sdmmc_clk)));
508 else
510 HAL_Delay(2U);
513 /* Identify card operating voltage */
514 errorstate = SD_PowerON(hsd);
515 if(errorstate != HAL_SD_ERROR_NONE)
517 hsd->State = HAL_SD_STATE_READY;
518 hsd->ErrorCode |= errorstate;
519 return HAL_ERROR;
522 /* Card initialization */
523 errorstate = SD_InitCard(hsd);
524 if(errorstate != HAL_SD_ERROR_NONE)
526 hsd->State = HAL_SD_STATE_READY;
527 hsd->ErrorCode |= errorstate;
528 return HAL_ERROR;
531 return HAL_OK;
535 * @brief De-Initializes the SD card.
536 * @param hsd: Pointer to SD handle
537 * @retval HAL status
539 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
541 /* Check the SD handle allocation */
542 if(hsd == NULL)
544 return HAL_ERROR;
547 /* Check the parameters */
548 assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
550 hsd->State = HAL_SD_STATE_BUSY;
552 #if (USE_SD_TRANSCEIVER != 0U)
553 /* Desactivate the 1.8V Mode */
554 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
556 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
557 if(hsd->DriveTransceiver_1_8V_Callback == NULL)
559 hsd->DriveTransceiver_1_8V_Callback = HAL_SD_DriveTransceiver_1_8V_Callback;
561 hsd->DriveTransceiver_1_8V_Callback(RESET);
562 #else
563 HAL_SD_DriveTransceiver_1_8V_Callback(RESET);
564 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
566 #endif /* USE_SD_TRANSCEIVER */
568 /* Set SD power state to off */
569 SD_PowerOFF(hsd);
571 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
572 if(hsd->MspDeInitCallback == NULL)
574 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
577 /* DeInit the low level hardware */
578 hsd->MspDeInitCallback(hsd);
579 #else
580 /* De-Initialize the MSP layer */
581 HAL_SD_MspDeInit(hsd);
582 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
584 hsd->ErrorCode = HAL_SD_ERROR_NONE;
585 hsd->State = HAL_SD_STATE_RESET;
587 return HAL_OK;
592 * @brief Initializes the SD MSP.
593 * @param hsd: Pointer to SD handle
594 * @retval None
596 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
598 /* Prevent unused argument(s) compilation warning */
599 UNUSED(hsd);
601 /* NOTE : This function should not be modified, when the callback is needed,
602 the HAL_SD_MspInit could be implemented in the user file
607 * @brief De-Initialize SD MSP.
608 * @param hsd: Pointer to SD handle
609 * @retval None
611 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
613 /* Prevent unused argument(s) compilation warning */
614 UNUSED(hsd);
616 /* NOTE : This function should not be modified, when the callback is needed,
617 the HAL_SD_MspDeInit could be implemented in the user file
622 * @}
625 /** @addtogroup SD_Exported_Functions_Group2
626 * @brief Data transfer functions
628 @verbatim
629 ==============================================================================
630 ##### IO operation functions #####
631 ==============================================================================
632 [..]
633 This subsection provides a set of functions allowing to manage the data
634 transfer from/to SD card.
636 @endverbatim
637 * @{
641 * @brief Reads block(s) from a specified address in a card. The Data transfer
642 * is managed by polling mode.
643 * @note This API should be followed by a check on the card state through
644 * HAL_SD_GetCardState().
645 * @param hsd: Pointer to SD handle
646 * @param pData: pointer to the buffer that will contain the received data
647 * @param BlockAdd: Block Address from where data is to be read
648 * @param NumberOfBlocks: Number of SD blocks to read
649 * @param Timeout: Specify timeout value
650 * @retval HAL status
652 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
654 SDMMC_DataInitTypeDef config;
655 uint32_t errorstate;
656 uint32_t tickstart = HAL_GetTick();
657 uint32_t count, data, dataremaining;
658 uint32_t add = BlockAdd;
659 uint8_t *tempbuff = pData;
661 if(NULL == pData)
663 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
664 return HAL_ERROR;
667 if(hsd->State == HAL_SD_STATE_READY)
669 hsd->ErrorCode = HAL_SD_ERROR_NONE;
671 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
673 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
674 return HAL_ERROR;
677 hsd->State = HAL_SD_STATE_BUSY;
679 /* Initialize data control register */
680 hsd->Instance->DCTRL = 0U;
682 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
684 add *= 512U;
687 /* Set Block Size for Card */
688 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
689 if(errorstate != HAL_SD_ERROR_NONE)
691 /* Clear all the static flags */
692 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
693 hsd->ErrorCode |= errorstate;
694 hsd->State = HAL_SD_STATE_READY;
695 return HAL_ERROR;
698 /* Configure the SD DPSM (Data Path State Machine) */
699 config.DataTimeOut = SDMMC_DATATIMEOUT;
700 config.DataLength = NumberOfBlocks * BLOCKSIZE;
701 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
702 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
703 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
704 config.DPSM = SDMMC_DPSM_DISABLE;
705 (void)SDMMC_ConfigData(hsd->Instance, &config);
706 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
708 /* Read block(s) in polling mode */
709 if(NumberOfBlocks > 1U)
711 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
713 /* Read Multi Block command */
714 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
716 else
718 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
720 /* Read Single Block command */
721 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
723 if(errorstate != HAL_SD_ERROR_NONE)
725 /* Clear all the static flags */
726 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
727 hsd->ErrorCode |= errorstate;
728 hsd->State = HAL_SD_STATE_READY;
729 hsd->Context = SD_CONTEXT_NONE;
730 return HAL_ERROR;
733 /* Poll on SDMMC flags */
734 dataremaining = config.DataLength;
735 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
737 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) && (dataremaining >= 32U))
739 /* Read data from SDMMC Rx FIFO */
740 for(count = 0U; count < 8U; count++)
742 data = SDMMC_ReadFIFO(hsd->Instance);
743 *tempbuff = (uint8_t)(data & 0xFFU);
744 tempbuff++;
745 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
746 tempbuff++;
747 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
748 tempbuff++;
749 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
750 tempbuff++;
752 dataremaining -= 32U;
755 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
757 /* Clear all the static flags */
758 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
759 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
760 hsd->State= HAL_SD_STATE_READY;
761 hsd->Context = SD_CONTEXT_NONE;
762 return HAL_TIMEOUT;
765 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
767 /* Send stop transmission command in case of multiblock read */
768 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
770 if(hsd->SdCard.CardType != CARD_SECURED)
772 /* Send stop transmission command */
773 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
774 if(errorstate != HAL_SD_ERROR_NONE)
776 /* Clear all the static flags */
777 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
778 hsd->ErrorCode |= errorstate;
779 hsd->State = HAL_SD_STATE_READY;
780 hsd->Context = SD_CONTEXT_NONE;
781 return HAL_ERROR;
786 /* Get error state */
787 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
789 /* Clear all the static flags */
790 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
791 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
792 hsd->State = HAL_SD_STATE_READY;
793 hsd->Context = SD_CONTEXT_NONE;
794 return HAL_ERROR;
796 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
798 /* Clear all the static flags */
799 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
800 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
801 hsd->State = HAL_SD_STATE_READY;
802 hsd->Context = SD_CONTEXT_NONE;
803 return HAL_ERROR;
805 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
807 /* Clear all the static flags */
808 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
809 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
810 hsd->State = HAL_SD_STATE_READY;
811 hsd->Context = SD_CONTEXT_NONE;
812 return HAL_ERROR;
814 else
816 /* Nothing to do */
819 /* Clear all the static flags */
820 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
822 hsd->State = HAL_SD_STATE_READY;
824 return HAL_OK;
826 else
828 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
829 return HAL_ERROR;
834 * @brief Allows to write block(s) to a specified address in a card. The Data
835 * transfer is managed by polling mode.
836 * @note This API should be followed by a check on the card state through
837 * HAL_SD_GetCardState().
838 * @param hsd: Pointer to SD handle
839 * @param pData: pointer to the buffer that will contain the data to transmit
840 * @param BlockAdd: Block Address where data will be written
841 * @param NumberOfBlocks: Number of SD blocks to write
842 * @param Timeout: Specify timeout value
843 * @retval HAL status
845 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
847 SDMMC_DataInitTypeDef config;
848 uint32_t errorstate;
849 uint32_t tickstart = HAL_GetTick();
850 uint32_t count, data, dataremaining;
851 uint32_t add = BlockAdd;
852 uint8_t *tempbuff = pData;
854 if(NULL == pData)
856 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
857 return HAL_ERROR;
860 if(hsd->State == HAL_SD_STATE_READY)
862 hsd->ErrorCode = HAL_SD_ERROR_NONE;
864 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
866 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
867 return HAL_ERROR;
870 hsd->State = HAL_SD_STATE_BUSY;
872 /* Initialize data control register */
873 hsd->Instance->DCTRL = 0U;
875 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
877 add *= 512U;
880 /* Set Block Size for Card */
881 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
882 if(errorstate != HAL_SD_ERROR_NONE)
884 /* Clear all the static flags */
885 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
886 hsd->ErrorCode |= errorstate;
887 hsd->State = HAL_SD_STATE_READY;
888 return HAL_ERROR;
891 /* Configure the SD DPSM (Data Path State Machine) */
892 config.DataTimeOut = SDMMC_DATATIMEOUT;
893 config.DataLength = NumberOfBlocks * BLOCKSIZE;
894 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
895 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
896 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
897 config.DPSM = SDMMC_DPSM_DISABLE;
898 (void)SDMMC_ConfigData(hsd->Instance, &config);
899 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
901 /* Write Blocks in Polling mode */
902 if(NumberOfBlocks > 1U)
904 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
906 /* Write Multi Block command */
907 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
909 else
911 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
913 /* Write Single Block command */
914 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
916 if(errorstate != HAL_SD_ERROR_NONE)
918 /* Clear all the static flags */
919 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
920 hsd->ErrorCode |= errorstate;
921 hsd->State = HAL_SD_STATE_READY;
922 hsd->Context = SD_CONTEXT_NONE;
923 return HAL_ERROR;
926 /* Write block(s) in polling mode */
927 dataremaining = config.DataLength;
928 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
930 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) && (dataremaining >= 32U))
932 /* Write data to SDMMC Tx FIFO */
933 for(count = 0U; count < 8U; count++)
935 data = (uint32_t)(*tempbuff);
936 tempbuff++;
937 data |= ((uint32_t)(*tempbuff) << 8U);
938 tempbuff++;
939 data |= ((uint32_t)(*tempbuff) << 16U);
940 tempbuff++;
941 data |= ((uint32_t)(*tempbuff) << 24U);
942 tempbuff++;
943 (void)SDMMC_WriteFIFO(hsd->Instance, &data);
945 dataremaining -= 32U;
948 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
950 /* Clear all the static flags */
951 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
952 hsd->ErrorCode |= errorstate;
953 hsd->State = HAL_SD_STATE_READY;
954 hsd->Context = SD_CONTEXT_NONE;
955 return HAL_TIMEOUT;
958 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
960 /* Send stop transmission command in case of multiblock write */
961 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
963 if(hsd->SdCard.CardType != CARD_SECURED)
965 /* Send stop transmission command */
966 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
967 if(errorstate != HAL_SD_ERROR_NONE)
969 /* Clear all the static flags */
970 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
971 hsd->ErrorCode |= errorstate;
972 hsd->State = HAL_SD_STATE_READY;
973 hsd->Context = SD_CONTEXT_NONE;
974 return HAL_ERROR;
979 /* Get error state */
980 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
982 /* Clear all the static flags */
983 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
984 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
985 hsd->State = HAL_SD_STATE_READY;
986 hsd->Context = SD_CONTEXT_NONE;
987 return HAL_ERROR;
989 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
991 /* Clear all the static flags */
992 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
993 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
994 hsd->State = HAL_SD_STATE_READY;
995 hsd->Context = SD_CONTEXT_NONE;
996 return HAL_ERROR;
998 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
1000 /* Clear all the static flags */
1001 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1002 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1003 hsd->State = HAL_SD_STATE_READY;
1004 hsd->Context = SD_CONTEXT_NONE;
1005 return HAL_ERROR;
1007 else
1009 /* Nothing to do */
1012 /* Clear all the static flags */
1013 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1015 hsd->State = HAL_SD_STATE_READY;
1017 return HAL_OK;
1019 else
1021 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
1022 return HAL_ERROR;
1027 * @brief Reads block(s) from a specified address in a card. The Data transfer
1028 * is managed in interrupt mode.
1029 * @note This API should be followed by a check on the card state through
1030 * HAL_SD_GetCardState().
1031 * @note You could also check the IT transfer process through the SD Rx
1032 * interrupt event.
1033 * @param hsd: Pointer to SD handle
1034 * @param pData: Pointer to the buffer that will contain the received data
1035 * @param BlockAdd: Block Address from where data is to be read
1036 * @param NumberOfBlocks: Number of blocks to read.
1037 * @retval HAL status
1039 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1041 SDMMC_DataInitTypeDef config;
1042 uint32_t errorstate;
1043 uint32_t add = BlockAdd;
1045 if(NULL == pData)
1047 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1048 return HAL_ERROR;
1051 if(hsd->State == HAL_SD_STATE_READY)
1053 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1055 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1057 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1058 return HAL_ERROR;
1061 hsd->State = HAL_SD_STATE_BUSY;
1063 /* Initialize data control register */
1064 hsd->Instance->DCTRL = 0U;
1066 hsd->pRxBuffPtr = pData;
1067 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1069 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1071 add *= 512U;
1074 /* Set Block Size for Card */
1075 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1076 if(errorstate != HAL_SD_ERROR_NONE)
1078 /* Clear all the static flags */
1079 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1080 hsd->ErrorCode |= errorstate;
1081 hsd->State = HAL_SD_STATE_READY;
1082 return HAL_ERROR;
1085 /* Configure the SD DPSM (Data Path State Machine) */
1086 config.DataTimeOut = SDMMC_DATATIMEOUT;
1087 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1088 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1089 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1090 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1091 config.DPSM = SDMMC_DPSM_DISABLE;
1092 (void)SDMMC_ConfigData(hsd->Instance, &config);
1093 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1095 /* Read Blocks in IT mode */
1096 if(NumberOfBlocks > 1U)
1098 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1100 /* Read Multi Block command */
1101 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1103 else
1105 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
1107 /* Read Single Block command */
1108 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1110 if(errorstate != HAL_SD_ERROR_NONE)
1112 /* Clear all the static flags */
1113 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1114 hsd->ErrorCode |= errorstate;
1115 hsd->State = HAL_SD_STATE_READY;
1116 hsd->Context = SD_CONTEXT_NONE;
1117 return HAL_ERROR;
1120 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
1122 return HAL_OK;
1124 else
1126 return HAL_BUSY;
1131 * @brief Writes block(s) to a specified address in a card. The Data transfer
1132 * is managed in interrupt mode.
1133 * @note This API should be followed by a check on the card state through
1134 * HAL_SD_GetCardState().
1135 * @note You could also check the IT transfer process through the SD Tx
1136 * interrupt event.
1137 * @param hsd: Pointer to SD handle
1138 * @param pData: Pointer to the buffer that will contain the data to transmit
1139 * @param BlockAdd: Block Address where data will be written
1140 * @param NumberOfBlocks: Number of blocks to write
1141 * @retval HAL status
1143 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1145 SDMMC_DataInitTypeDef config;
1146 uint32_t errorstate;
1147 uint32_t add = BlockAdd;
1149 if(NULL == pData)
1151 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1152 return HAL_ERROR;
1155 if(hsd->State == HAL_SD_STATE_READY)
1157 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1159 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1161 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1162 return HAL_ERROR;
1165 hsd->State = HAL_SD_STATE_BUSY;
1167 /* Initialize data control register */
1168 hsd->Instance->DCTRL = 0U;
1170 hsd->pTxBuffPtr = pData;
1171 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1173 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1175 add *= 512U;
1178 /* Set Block Size for Card */
1179 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1180 if(errorstate != HAL_SD_ERROR_NONE)
1182 /* Clear all the static flags */
1183 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1184 hsd->ErrorCode |= errorstate;
1185 hsd->State = HAL_SD_STATE_READY;
1186 return HAL_ERROR;
1189 /* Configure the SD DPSM (Data Path State Machine) */
1190 config.DataTimeOut = SDMMC_DATATIMEOUT;
1191 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1192 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1193 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1194 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1195 config.DPSM = SDMMC_DPSM_DISABLE;
1196 (void)SDMMC_ConfigData(hsd->Instance, &config);
1198 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1200 /* Write Blocks in Polling mode */
1201 if(NumberOfBlocks > 1U)
1203 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1205 /* Write Multi Block command */
1206 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1208 else
1210 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1212 /* Write Single Block command */
1213 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1215 if(errorstate != HAL_SD_ERROR_NONE)
1217 /* Clear all the static flags */
1218 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1219 hsd->ErrorCode |= errorstate;
1220 hsd->State = HAL_SD_STATE_READY;
1221 hsd->Context = SD_CONTEXT_NONE;
1222 return HAL_ERROR;
1225 /* Enable transfer interrupts */
1226 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
1228 return HAL_OK;
1230 else
1232 return HAL_BUSY;
1237 * @brief Reads block(s) from a specified address in a card. The Data transfer
1238 * is managed by DMA mode.
1239 * @note This API should be followed by a check on the card state through
1240 * HAL_SD_GetCardState().
1241 * @note You could also check the DMA transfer process through the SD Rx
1242 * interrupt event.
1243 * @param hsd: Pointer SD handle
1244 * @param pData: Pointer to the buffer that will contain the received data
1245 * @param BlockAdd: Block Address from where data is to be read
1246 * @param NumberOfBlocks: Number of blocks to read.
1247 * @retval HAL status
1249 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1251 SDMMC_DataInitTypeDef config;
1252 uint32_t errorstate;
1253 uint32_t add = BlockAdd;
1255 if(NULL == pData)
1257 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1258 return HAL_ERROR;
1261 if(hsd->State == HAL_SD_STATE_READY)
1263 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1265 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1267 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1268 return HAL_ERROR;
1271 hsd->State = HAL_SD_STATE_BUSY;
1273 /* Initialize data control register */
1274 hsd->Instance->DCTRL = 0U;
1276 hsd->pRxBuffPtr = pData;
1277 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1279 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1281 add *= 512U;
1284 /* Set Block Size for Card */
1285 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1286 if(errorstate != HAL_SD_ERROR_NONE)
1288 /* Clear all the static flags */
1289 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1290 hsd->ErrorCode |= errorstate;
1291 hsd->State = HAL_SD_STATE_READY;
1292 return HAL_ERROR;
1295 /* Configure the SD DPSM (Data Path State Machine) */
1296 config.DataTimeOut = SDMMC_DATATIMEOUT;
1297 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1298 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1299 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1300 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1301 config.DPSM = SDMMC_DPSM_DISABLE;
1302 (void)SDMMC_ConfigData(hsd->Instance, &config);
1304 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1305 hsd->Instance->IDMABASE0 = (uint32_t) pData ;
1306 hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1308 /* Read Blocks in DMA mode */
1309 if(NumberOfBlocks > 1U)
1311 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1313 /* Read Multi Block command */
1314 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1316 else
1318 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1320 /* Read Single Block command */
1321 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1323 if(errorstate != HAL_SD_ERROR_NONE)
1325 /* Clear all the static flags */
1326 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1327 hsd->ErrorCode |= errorstate;
1328 hsd->State = HAL_SD_STATE_READY;
1329 hsd->Context = SD_CONTEXT_NONE;
1330 return HAL_ERROR;
1333 /* Enable transfer interrupts */
1334 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1337 return HAL_OK;
1339 else
1341 return HAL_BUSY;
1346 * @brief Writes block(s) to a specified address in a card. The Data transfer
1347 * is managed by DMA mode.
1348 * @note This API should be followed by a check on the card state through
1349 * HAL_SD_GetCardState().
1350 * @note You could also check the DMA transfer process through the SD Tx
1351 * interrupt event.
1352 * @param hsd: Pointer to SD handle
1353 * @param pData: Pointer to the buffer that will contain the data to transmit
1354 * @param BlockAdd: Block Address where data will be written
1355 * @param NumberOfBlocks: Number of blocks to write
1356 * @retval HAL status
1358 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1360 SDMMC_DataInitTypeDef config;
1361 uint32_t errorstate;
1362 uint32_t add = BlockAdd;
1364 if(NULL == pData)
1366 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1367 return HAL_ERROR;
1370 if(hsd->State == HAL_SD_STATE_READY)
1372 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1374 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1376 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1377 return HAL_ERROR;
1380 hsd->State = HAL_SD_STATE_BUSY;
1382 /* Initialize data control register */
1383 hsd->Instance->DCTRL = 0U;
1385 hsd->pTxBuffPtr = pData;
1386 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1388 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1390 add *= 512U;
1393 /* Set Block Size for Card */
1394 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1395 if(errorstate != HAL_SD_ERROR_NONE)
1397 /* Clear all the static flags */
1398 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1399 hsd->ErrorCode |= errorstate;
1400 hsd->State = HAL_SD_STATE_READY;
1401 return HAL_ERROR;
1403 /* Configure the SD DPSM (Data Path State Machine) */
1404 config.DataTimeOut = SDMMC_DATATIMEOUT;
1405 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1406 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1407 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1408 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1409 config.DPSM = SDMMC_DPSM_DISABLE;
1410 (void)SDMMC_ConfigData(hsd->Instance, &config);
1413 __SDMMC_CMDTRANS_ENABLE( hsd->Instance);
1415 hsd->Instance->IDMABASE0 = (uint32_t) pData ;
1416 hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1418 /* Write Blocks in Polling mode */
1419 if(NumberOfBlocks > 1U)
1421 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1423 /* Write Multi Block command */
1424 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1426 else
1428 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1430 /* Write Single Block command */
1431 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1433 if(errorstate != HAL_SD_ERROR_NONE)
1435 /* Clear all the static flags */
1436 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1437 hsd->ErrorCode |= errorstate;
1438 hsd->State = HAL_SD_STATE_READY;
1439 hsd->Context = SD_CONTEXT_NONE;
1440 return HAL_ERROR;
1443 /* Enable transfer interrupts */
1444 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1446 return HAL_OK;
1448 else
1450 return HAL_BUSY;
1455 * @brief Erases the specified memory area of the given SD card.
1456 * @note This API should be followed by a check on the card state through
1457 * HAL_SD_GetCardState().
1458 * @param hsd: Pointer to SD handle
1459 * @param BlockStartAdd: Start Block address
1460 * @param BlockEndAdd: End Block address
1461 * @retval HAL status
1463 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1465 uint32_t errorstate;
1466 uint32_t start_add = BlockStartAdd;
1467 uint32_t end_add = BlockEndAdd;
1469 if(hsd->State == HAL_SD_STATE_READY)
1471 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1473 if(end_add < start_add)
1475 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1476 return HAL_ERROR;
1479 if(end_add > (hsd->SdCard.LogBlockNbr))
1481 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1482 return HAL_ERROR;
1485 hsd->State = HAL_SD_STATE_BUSY;
1487 /* Check if the card command class supports erase command */
1488 if(((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U)
1490 /* Clear all the static flags */
1491 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1492 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1493 hsd->State = HAL_SD_STATE_READY;
1494 return HAL_ERROR;
1497 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1499 /* Clear all the static flags */
1500 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1501 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1502 hsd->State = HAL_SD_STATE_READY;
1503 return HAL_ERROR;
1506 /* Get start and end block for high capacity cards */
1507 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1509 start_add *= 512U;
1510 end_add *= 512U;
1513 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1514 if(hsd->SdCard.CardType != CARD_SECURED)
1516 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1517 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
1518 if(errorstate != HAL_SD_ERROR_NONE)
1520 /* Clear all the static flags */
1521 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1522 hsd->ErrorCode |= errorstate;
1523 hsd->State = HAL_SD_STATE_READY;
1524 return HAL_ERROR;
1527 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1528 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
1529 if(errorstate != HAL_SD_ERROR_NONE)
1531 /* Clear all the static flags */
1532 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1533 hsd->ErrorCode |= errorstate;
1534 hsd->State = HAL_SD_STATE_READY;
1535 return HAL_ERROR;
1539 /* Send CMD38 ERASE */
1540 errorstate = SDMMC_CmdErase(hsd->Instance);
1541 if(errorstate != HAL_SD_ERROR_NONE)
1543 /* Clear all the static flags */
1544 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1545 hsd->ErrorCode |= errorstate;
1546 hsd->State = HAL_SD_STATE_READY;
1547 return HAL_ERROR;
1550 hsd->State = HAL_SD_STATE_READY;
1552 return HAL_OK;
1554 else
1556 return HAL_BUSY;
1561 * @brief This function handles SD card interrupt request.
1562 * @param hsd: Pointer to SD handle
1563 * @retval None
1565 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1567 uint32_t errorstate;
1568 uint32_t context = hsd->Context;
1570 /* Check for SDMMC interrupt flags */
1571 if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1573 SD_Read_IT(hsd);
1576 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) != RESET)
1578 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND);
1580 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT |\
1581 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE |\
1582 SDMMC_IT_RXFIFOHF);
1584 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
1585 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
1587 if((context & SD_CONTEXT_IT) != 0U)
1589 if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1591 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1592 if(errorstate != HAL_SD_ERROR_NONE)
1594 hsd->ErrorCode |= errorstate;
1595 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1596 hsd->ErrorCallback(hsd);
1597 #else
1598 HAL_SD_ErrorCallback(hsd);
1599 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1603 /* Clear all the static flags */
1604 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1606 hsd->State = HAL_SD_STATE_READY;
1607 hsd->Context = SD_CONTEXT_NONE;
1608 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1610 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1611 hsd->RxCpltCallback(hsd);
1612 #else
1613 HAL_SD_RxCpltCallback(hsd);
1614 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1616 else
1618 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1619 hsd->TxCpltCallback(hsd);
1620 #else
1621 HAL_SD_TxCpltCallback(hsd);
1622 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1625 else if((context & SD_CONTEXT_DMA) != 0U)
1627 hsd->Instance->DLEN = 0;
1628 hsd->Instance->DCTRL = 0;
1629 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1631 /* Stop Transfer for Write Multi blocks or Read Multi blocks */
1632 if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1634 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1635 if(errorstate != HAL_SD_ERROR_NONE)
1637 hsd->ErrorCode |= errorstate;
1638 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1639 hsd->ErrorCallback(hsd);
1640 #else
1641 HAL_SD_ErrorCallback(hsd);
1642 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1646 hsd->State = HAL_SD_STATE_READY;
1647 hsd->Context = SD_CONTEXT_NONE;
1648 if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1650 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1651 hsd->TxCpltCallback(hsd);
1652 #else
1653 HAL_SD_TxCpltCallback(hsd);
1654 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1656 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1658 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1659 hsd->RxCpltCallback(hsd);
1660 #else
1661 HAL_SD_RxCpltCallback(hsd);
1662 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1665 else
1667 /* Nothing to do */
1671 else if((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1673 SD_Write_IT(hsd);
1676 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR | SDMMC_FLAG_TXUNDERR) != RESET)
1678 /* Set Error code */
1679 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL) != RESET)
1681 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1683 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT) != RESET)
1685 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1687 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXOVERR) != RESET)
1689 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1691 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXUNDERR) != RESET)
1693 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1696 /* Clear All flags */
1697 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1699 /* Disable all interrupts */
1700 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
1701 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
1703 __SDMMC_CMDTRANS_DISABLE( hsd->Instance);
1704 hsd->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
1705 hsd->Instance->CMD |= SDMMC_CMD_CMDSTOP;
1706 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
1707 hsd->Instance->CMD &= ~(SDMMC_CMD_CMDSTOP);
1708 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DABORT);
1710 if((context & SD_CONTEXT_IT) != 0U)
1712 /* Set the SD state to ready to be able to start again the process */
1713 hsd->State = HAL_SD_STATE_READY;
1714 hsd->Context = SD_CONTEXT_NONE;
1715 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1716 hsd->ErrorCallback(hsd);
1717 #else
1718 HAL_SD_ErrorCallback(hsd);
1719 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1721 else if((context & SD_CONTEXT_DMA) != 0U)
1723 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
1725 /* Disable Internal DMA */
1726 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
1727 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1729 /* Set the SD state to ready to be able to start again the process */
1730 hsd->State = HAL_SD_STATE_READY;
1731 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1732 hsd->ErrorCallback(hsd);
1733 #else
1734 HAL_SD_ErrorCallback(hsd);
1735 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1738 else
1740 /* Nothing to do */
1744 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_IDMABTC) != RESET)
1746 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_IDMABTC);
1747 if(READ_BIT(hsd->Instance->IDMACTRL, SDMMC_IDMA_IDMABACT) == 0U)
1749 /* Current buffer is buffer0, Transfer complete for buffer1 */
1750 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1752 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1753 hsd->Write_DMADblBuf1CpltCallback(hsd);
1754 #else
1755 HAL_SDEx_Write_DMADoubleBuf1CpltCallback(hsd);
1756 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1758 else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
1760 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1761 hsd->Read_DMADblBuf1CpltCallback(hsd);
1762 #else
1763 HAL_SDEx_Read_DMADoubleBuf1CpltCallback(hsd);
1764 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1767 else /* SD_DMA_BUFFER1 */
1769 /* Current buffer is buffer1, Transfer complete for buffer0 */
1770 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1772 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1773 hsd->Write_DMADblBuf0CpltCallback(hsd);
1774 #else
1775 HAL_SDEx_Write_DMADoubleBuf0CpltCallback(hsd);
1776 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1778 else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
1780 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1781 hsd->Read_DMADblBuf0CpltCallback(hsd);
1782 #else
1783 HAL_SDEx_Read_DMADoubleBuf0CpltCallback(hsd);
1784 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1788 else
1790 /* Nothing to do */
1795 * @brief return the SD state
1796 * @param hsd: Pointer to sd handle
1797 * @retval HAL state
1799 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
1801 return hsd->State;
1805 * @brief Return the SD error code
1806 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
1807 * the configuration information.
1808 * @retval SD Error Code
1810 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
1812 return hsd->ErrorCode;
1816 * @brief Tx Transfer completed callbacks
1817 * @param hsd: Pointer to SD handle
1818 * @retval None
1820 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
1822 /* Prevent unused argument(s) compilation warning */
1823 UNUSED(hsd);
1825 /* NOTE : This function should not be modified, when the callback is needed,
1826 the HAL_SD_TxCpltCallback can be implemented in the user file
1831 * @brief Rx Transfer completed callbacks
1832 * @param hsd: Pointer SD handle
1833 * @retval None
1835 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
1837 /* Prevent unused argument(s) compilation warning */
1838 UNUSED(hsd);
1840 /* NOTE : This function should not be modified, when the callback is needed,
1841 the HAL_SD_RxCpltCallback can be implemented in the user file
1846 * @brief SD error callbacks
1847 * @param hsd: Pointer SD handle
1848 * @retval None
1850 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1852 /* Prevent unused argument(s) compilation warning */
1853 UNUSED(hsd);
1855 /* NOTE : This function should not be modified, when the callback is needed,
1856 the HAL_SD_ErrorCallback can be implemented in the user file
1861 * @brief SD Abort callbacks
1862 * @param hsd: Pointer SD handle
1863 * @retval None
1865 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
1867 /* Prevent unused argument(s) compilation warning */
1868 UNUSED(hsd);
1870 /* NOTE : This function should not be modified, when the callback is needed,
1871 the HAL_SD_AbortCallback can be implemented in the user file
1875 #if (USE_SD_TRANSCEIVER != 0U)
1877 * @brief Enable/Disable the SD Transceiver 1.8V Mode Callback.
1878 * @param status: Voltage Switch State
1879 * @retval None
1881 __weak void HAL_SD_DriveTransceiver_1_8V_Callback(FlagStatus status)
1883 /* Prevent unused argument(s) compilation warning */
1884 UNUSED(status);
1885 /* NOTE : This function should not be modified, when the callback is needed,
1886 the HAL_SD_EnableTransceiver could be implemented in the user file
1889 #endif /* USE_SD_TRANSCEIVER */
1891 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1893 * @brief Register a User SD Callback
1894 * To be used instead of the weak (surcharged) predefined callback
1895 * @param hsd : SD handle
1896 * @param CallbackID : ID of the callback to be registered
1897 * This parameter can be one of the following values:
1898 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1899 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1900 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1901 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1902 * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID
1903 * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID
1904 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID
1905 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID
1906 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1907 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1908 * @param pCallback : pointer to the Callback function
1909 * @retval status
1911 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
1913 HAL_StatusTypeDef status = HAL_OK;
1915 if(pCallback == NULL)
1917 /* Update the error code */
1918 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1919 return HAL_ERROR;
1922 /* Process locked */
1923 __HAL_LOCK(hsd);
1925 if(hsd->State == HAL_SD_STATE_READY)
1927 switch (CallbackID)
1929 case HAL_SD_TX_CPLT_CB_ID :
1930 hsd->TxCpltCallback = pCallback;
1931 break;
1932 case HAL_SD_RX_CPLT_CB_ID :
1933 hsd->RxCpltCallback = pCallback;
1934 break;
1935 case HAL_SD_ERROR_CB_ID :
1936 hsd->ErrorCallback = pCallback;
1937 break;
1938 case HAL_SD_ABORT_CB_ID :
1939 hsd->AbortCpltCallback = pCallback;
1940 break;
1941 case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID :
1942 hsd->Read_DMADblBuf0CpltCallback = pCallback;
1943 break;
1944 case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID :
1945 hsd->Read_DMADblBuf1CpltCallback = pCallback;
1946 break;
1947 case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
1948 hsd->Write_DMADblBuf0CpltCallback = pCallback;
1949 break;
1950 case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
1951 hsd->Write_DMADblBuf1CpltCallback = pCallback;
1952 break;
1953 case HAL_SD_MSP_INIT_CB_ID :
1954 hsd->MspInitCallback = pCallback;
1955 break;
1956 case HAL_SD_MSP_DEINIT_CB_ID :
1957 hsd->MspDeInitCallback = pCallback;
1958 break;
1959 default :
1960 /* Update the error code */
1961 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1962 /* update return status */
1963 status = HAL_ERROR;
1964 break;
1967 else if (hsd->State == HAL_SD_STATE_RESET)
1969 switch (CallbackID)
1971 case HAL_SD_MSP_INIT_CB_ID :
1972 hsd->MspInitCallback = pCallback;
1973 break;
1974 case HAL_SD_MSP_DEINIT_CB_ID :
1975 hsd->MspDeInitCallback = pCallback;
1976 break;
1977 default :
1978 /* Update the error code */
1979 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1980 /* update return status */
1981 status = HAL_ERROR;
1982 break;
1985 else
1987 /* Update the error code */
1988 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1989 /* update return status */
1990 status = HAL_ERROR;
1993 /* Release Lock */
1994 __HAL_UNLOCK(hsd);
1995 return status;
1999 * @brief Unregister a User SD Callback
2000 * SD Callback is redirected to the weak (surcharged) predefined callback
2001 * @param hsd : SD handle
2002 * @param CallbackID : ID of the callback to be unregistered
2003 * This parameter can be one of the following values:
2004 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
2005 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
2006 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
2007 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
2008 * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID
2009 * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID
2010 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID
2011 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID
2012 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
2013 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
2014 * @retval status
2016 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
2018 HAL_StatusTypeDef status = HAL_OK;
2020 /* Process locked */
2021 __HAL_LOCK(hsd);
2023 if(hsd->State == HAL_SD_STATE_READY)
2025 switch (CallbackID)
2027 case HAL_SD_TX_CPLT_CB_ID :
2028 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
2029 break;
2030 case HAL_SD_RX_CPLT_CB_ID :
2031 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
2032 break;
2033 case HAL_SD_ERROR_CB_ID :
2034 hsd->ErrorCallback = HAL_SD_ErrorCallback;
2035 break;
2036 case HAL_SD_ABORT_CB_ID :
2037 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
2038 break;
2039 case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID :
2040 hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuf0CpltCallback;
2041 break;
2042 case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID :
2043 hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuf1CpltCallback;
2044 break;
2045 case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
2046 hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuf0CpltCallback;
2047 break;
2048 case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
2049 hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuf1CpltCallback;
2050 break;
2051 case HAL_SD_MSP_INIT_CB_ID :
2052 hsd->MspInitCallback = HAL_SD_MspInit;
2053 break;
2054 case HAL_SD_MSP_DEINIT_CB_ID :
2055 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
2056 break;
2057 default :
2058 /* Update the error code */
2059 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2060 /* update return status */
2061 status = HAL_ERROR;
2062 break;
2065 else if (hsd->State == HAL_SD_STATE_RESET)
2067 switch (CallbackID)
2069 case HAL_SD_MSP_INIT_CB_ID :
2070 hsd->MspInitCallback = HAL_SD_MspInit;
2071 break;
2072 case HAL_SD_MSP_DEINIT_CB_ID :
2073 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
2074 break;
2075 default :
2076 /* Update the error code */
2077 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2078 /* update return status */
2079 status = HAL_ERROR;
2080 break;
2083 else
2085 /* Update the error code */
2086 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2087 /* update return status */
2088 status = HAL_ERROR;
2091 /* Release Lock */
2092 __HAL_UNLOCK(hsd);
2093 return status;
2096 #if (USE_SD_TRANSCEIVER != 0U)
2098 * @brief Register a User SD Transceiver Callback
2099 * To be used instead of the weak (surcharged) predefined callback
2100 * @param hsd : SD handle
2101 * @param pCallback : pointer to the Callback function
2102 * @retval status
2104 HAL_StatusTypeDef HAL_SD_RegisterTransceiverCallback(SD_HandleTypeDef *hsd, pSD_TransceiverCallbackTypeDef pCallback)
2106 HAL_StatusTypeDef status = HAL_OK;
2108 if(pCallback == NULL)
2110 /* Update the error code */
2111 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2112 return HAL_ERROR;
2115 /* Process locked */
2116 __HAL_LOCK(hsd);
2118 if(hsd->State == HAL_SD_STATE_READY)
2120 hsd->DriveTransceiver_1_8V_Callback = pCallback;
2122 else
2124 /* Update the error code */
2125 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2126 /* update return status */
2127 status = HAL_ERROR;
2130 /* Release Lock */
2131 __HAL_UNLOCK(hsd);
2132 return status;
2136 * @brief Unregister a User SD Transceiver Callback
2137 * SD Callback is redirected to the weak (surcharged) predefined callback
2138 * @param hsd : SD handle
2139 * @retval status
2141 HAL_StatusTypeDef HAL_SD_UnRegisterTransceiverCallback(SD_HandleTypeDef *hsd)
2143 HAL_StatusTypeDef status = HAL_OK;
2145 /* Process locked */
2146 __HAL_LOCK(hsd);
2148 if(hsd->State == HAL_SD_STATE_READY)
2150 hsd->DriveTransceiver_1_8V_Callback = HAL_SD_DriveTransceiver_1_8V_Callback;
2152 else
2154 /* Update the error code */
2155 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2156 /* update return status */
2157 status = HAL_ERROR;
2160 /* Release Lock */
2161 __HAL_UNLOCK(hsd);
2162 return status;
2164 #endif /* USE_SD_TRANSCEIVER */
2165 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2168 * @}
2171 /** @addtogroup SD_Exported_Functions_Group3
2172 * @brief management functions
2174 @verbatim
2175 ==============================================================================
2176 ##### Peripheral Control functions #####
2177 ==============================================================================
2178 [..]
2179 This subsection provides a set of functions allowing to control the SD card
2180 operations and get the related information
2182 @endverbatim
2183 * @{
2187 * @brief Returns information the information of the card which are stored on
2188 * the CID register.
2189 * @param hsd: Pointer to SD handle
2190 * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that
2191 * contains all CID register parameters
2192 * @retval HAL status
2194 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
2196 pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
2198 pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
2200 pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
2202 pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
2204 pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
2206 pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
2208 pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
2210 pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
2212 pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
2214 pCID->Reserved2 = 1U;
2216 return HAL_OK;
2220 * @brief Returns information the information of the card which are stored on
2221 * the CSD register.
2222 * @param hsd: Pointer to SD handle
2223 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
2224 * contains all CSD register parameters
2225 * @retval HAL status
2227 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
2229 pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
2231 pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
2233 pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
2235 pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
2237 pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
2239 pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
2241 pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
2243 pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
2245 pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
2247 pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
2249 pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
2251 pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
2253 pCSD->Reserved2 = 0U; /*!< Reserved */
2255 if(hsd->SdCard.CardType == CARD_SDSC)
2257 pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
2259 pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
2261 pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
2263 pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
2265 pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
2267 pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
2269 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2270 hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2271 hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2273 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
2274 hsd->SdCard.LogBlockSize = 512U;
2276 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
2278 /* Byte 7 */
2279 pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
2281 hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
2282 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
2283 hsd->SdCard.BlockSize = 512U;
2284 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
2286 else
2288 /* Clear all the static flags */
2289 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2290 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2291 hsd->State = HAL_SD_STATE_READY;
2292 return HAL_ERROR;
2295 pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
2297 pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
2299 pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
2301 pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
2303 pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
2305 pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
2307 pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
2309 pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
2311 pCSD->Reserved3 = 0;
2313 pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
2315 pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
2317 pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
2319 pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
2321 pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
2323 pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
2325 pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
2327 pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
2329 pCSD->Reserved4 = 1;
2331 return HAL_OK;
2335 * @brief Gets the SD status info.
2336 * @param hsd: Pointer to SD handle
2337 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
2338 * will contain the SD card status information
2339 * @retval HAL status
2341 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
2343 uint32_t sd_status[16];
2344 uint32_t errorstate;
2346 errorstate = SD_SendSDStatus(hsd, sd_status);
2347 if(errorstate != HAL_SD_ERROR_NONE)
2349 /* Clear all the static flags */
2350 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2351 hsd->ErrorCode |= errorstate;
2352 hsd->State = HAL_SD_STATE_READY;
2353 return HAL_ERROR;
2355 else
2357 pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
2359 pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
2361 pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
2363 pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
2364 ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
2366 pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
2368 pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
2370 pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
2372 pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
2374 pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
2376 pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
2378 pStatus->UhsSpeedGrade = (uint8_t)((sd_status[3] & 0x00F0U) >> 4U);
2379 pStatus->UhsAllocationUnitSize = (uint8_t)(sd_status[3] & 0x000FU) ;
2380 pStatus->VideoSpeedClass = (uint8_t)((sd_status[4] & 0xFF000000U) >> 24U);
2383 return HAL_OK;
2387 * @brief Gets the SD card info.
2388 * @param hsd: Pointer to SD handle
2389 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
2390 * will contain the SD card status information
2391 * @retval HAL status
2393 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
2395 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
2396 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
2397 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
2398 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
2399 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
2400 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
2401 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
2402 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
2404 return HAL_OK;
2408 * @brief Enables wide bus operation for the requested card if supported by
2409 * card.
2410 * @param hsd: Pointer to SD handle
2411 * @param WideMode: Specifies the SD card wide bus mode
2412 * This parameter can be one of the following values:
2413 * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer
2414 * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
2415 * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
2416 * @retval HAL status
2418 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
2420 SDMMC_InitTypeDef Init;
2421 uint32_t errorstate;
2423 /* Check the parameters */
2424 assert_param(IS_SDMMC_BUS_WIDE(WideMode));
2426 /* Change State */
2427 hsd->State = HAL_SD_STATE_BUSY;
2429 if(hsd->SdCard.CardType != CARD_SECURED)
2431 if(WideMode == SDMMC_BUS_WIDE_8B)
2433 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2435 else if(WideMode == SDMMC_BUS_WIDE_4B)
2437 errorstate = SD_WideBus_Enable(hsd);
2439 hsd->ErrorCode |= errorstate;
2441 else if(WideMode == SDMMC_BUS_WIDE_1B)
2443 errorstate = SD_WideBus_Disable(hsd);
2445 hsd->ErrorCode |= errorstate;
2447 else
2449 /* WideMode is not a valid argument*/
2450 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2453 else
2455 /* MMC Card does not support this feature */
2456 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2459 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2461 /* Clear all the static flags */
2462 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2463 hsd->State = HAL_SD_STATE_READY;
2464 return HAL_ERROR;
2466 else
2468 /* Configure the SDMMC peripheral */
2469 Init.ClockEdge = hsd->Init.ClockEdge;
2470 Init.ClockPowerSave = hsd->Init.ClockPowerSave;
2471 Init.BusWide = WideMode;
2472 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2474 /* Check if user Clock div < Normal speed 25Mhz, no change in Clockdiv */
2475 if(hsd->Init.ClockDiv >= SDMMC_NSpeed_CLK_DIV)
2477 Init.ClockDiv = hsd->Init.ClockDiv;
2479 else if (hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
2481 /* UltraHigh speed SD card,user Clock div */
2482 Init.ClockDiv = hsd->Init.ClockDiv;
2484 else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
2486 /* High speed SD card, Max Frequency = 50Mhz */
2487 Init.ClockDiv = SDMMC_HSpeed_CLK_DIV;
2489 else
2491 /* No High speed SD card, Max Frequency = 25Mhz */
2492 Init.ClockDiv = SDMMC_NSpeed_CLK_DIV;
2495 (void)SDMMC_Init(hsd->Instance, Init);
2498 /* Change State */
2499 hsd->State = HAL_SD_STATE_READY;
2501 return HAL_OK;
2505 * @brief Configure the speed bus mode
2506 * @param hsd: Pointer to the SD handle
2507 * @param SpeedMode: Specifies the SD card speed bus mode
2508 * This parameter can be one of the following values:
2509 * @arg SDMMC_SPEED_MODE_AUTO: Max speed mode supported by the card
2510 * @arg SDMMC_SPEED_MODE_DEFAULT: Default Speed/SDR12 mode
2511 * @arg SDMMC_SPEED_MODE_HIGH: High Speed/SDR25 mode
2512 * @arg SDMMC_SPEED_MODE_ULTRA: Ultra high speed mode
2513 * @retval HAL status
2516 HAL_StatusTypeDef HAL_SD_ConfigSpeedBusOperation(SD_HandleTypeDef *hsd, uint32_t SpeedMode)
2518 uint32_t tickstart;
2519 HAL_StatusTypeDef status = HAL_OK;
2521 /* Check the parameters */
2522 assert_param(IS_SDMMC_SPEED_MODE(SpeedMode));
2523 /* Change State */
2524 hsd->State = HAL_SD_STATE_BUSY;
2526 #if (USE_SD_TRANSCEIVER != 0U)
2527 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
2529 switch (SpeedMode)
2531 case SDMMC_SPEED_MODE_AUTO:
2533 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2534 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2536 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED;
2537 /* Enable Ultra High Speed */
2538 if (SD_UltraHighSpeed(hsd) != HAL_SD_ERROR_NONE)
2540 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2542 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2543 status = HAL_ERROR;
2547 else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
2549 /* Enable High Speed */
2550 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2552 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2553 status = HAL_ERROR;
2556 else
2558 /*Nothing to do, Use defaultSpeed */
2560 break;
2562 case SDMMC_SPEED_MODE_ULTRA:
2564 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2565 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2567 /* Enable UltraHigh Speed */
2568 if (SD_UltraHighSpeed(hsd) != HAL_SD_ERROR_NONE)
2570 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2571 status = HAL_ERROR;
2573 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED;
2575 else
2577 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2578 status = HAL_ERROR;
2580 break;
2582 case SDMMC_SPEED_MODE_DDR:
2584 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2585 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2587 /* Enable DDR Mode*/
2588 if (SD_DDR_Mode(hsd) != HAL_SD_ERROR_NONE)
2590 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2591 status = HAL_ERROR;
2593 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED | SDMMC_CLKCR_DDR;
2595 else
2597 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2598 status = HAL_ERROR;
2600 break;
2602 case SDMMC_SPEED_MODE_HIGH:
2604 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2605 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2606 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2608 /* Enable High Speed */
2609 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2611 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2612 status = HAL_ERROR;
2615 else
2617 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2618 status = HAL_ERROR;
2620 break;
2622 case SDMMC_SPEED_MODE_DEFAULT:
2623 break;
2624 default:
2625 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2626 status = HAL_ERROR;
2627 break;
2630 else
2632 switch (SpeedMode)
2634 case SDMMC_SPEED_MODE_AUTO:
2636 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2637 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2638 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2640 /* Enable High Speed */
2641 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2643 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2644 status = HAL_ERROR;
2647 else
2649 /*Nothing to do, Use defaultSpeed */
2651 break;
2653 case SDMMC_SPEED_MODE_HIGH:
2655 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2656 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2657 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2659 /* Enable High Speed */
2660 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2662 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2663 status = HAL_ERROR;
2666 else
2668 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2669 status = HAL_ERROR;
2671 break;
2673 case SDMMC_SPEED_MODE_DEFAULT:
2674 break;
2675 case SDMMC_SPEED_MODE_ULTRA: /*not valid without transceiver*/
2676 default:
2677 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2678 status = HAL_ERROR;
2679 break;
2682 #else
2683 switch (SpeedMode)
2685 case SDMMC_SPEED_MODE_AUTO:
2687 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2688 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2689 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2691 /* Enable High Speed */
2692 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2694 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2695 status = HAL_ERROR;
2698 else
2700 /*Nothing to do, Use defaultSpeed */
2702 break;
2704 case SDMMC_SPEED_MODE_HIGH:
2706 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2707 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2708 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2710 /* Enable High Speed */
2711 if (SD_HighSpeed(hsd) != HAL_SD_ERROR_NONE)
2713 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2714 status = HAL_ERROR;
2717 else
2719 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2720 status = HAL_ERROR;
2722 break;
2724 case SDMMC_SPEED_MODE_DEFAULT:
2725 break;
2726 case SDMMC_SPEED_MODE_ULTRA: /*not valid without transceiver*/
2727 default:
2728 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2729 status = HAL_ERROR;
2730 break;
2732 #endif /* USE_SD_TRANSCEIVER */
2734 /* Verify that SD card is ready to use after Speed mode switch*/
2735 tickstart = HAL_GetTick();
2736 while ((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER))
2738 if ((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2740 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
2741 hsd->State = HAL_SD_STATE_READY;
2742 return HAL_TIMEOUT;
2746 /* Change State */
2747 hsd->State = HAL_SD_STATE_READY;
2748 return status;
2752 * @brief Gets the current sd card data state.
2753 * @param hsd: pointer to SD handle
2754 * @retval Card state
2756 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2758 uint32_t cardstate;
2759 uint32_t errorstate;
2760 uint32_t resp1 = 0;
2762 errorstate = SD_SendStatus(hsd, &resp1);
2763 if(errorstate != HAL_SD_ERROR_NONE)
2765 hsd->ErrorCode |= errorstate;
2768 cardstate = ((resp1 >> 9U) & 0x0FU);
2770 return (HAL_SD_CardStateTypeDef)cardstate;
2774 * @brief Abort the current transfer and disable the SD.
2775 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2776 * the configuration information for SD module.
2777 * @retval HAL status
2779 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
2781 HAL_SD_CardStateTypeDef CardState;
2783 /* DIsable All interrupts */
2784 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2785 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2787 /* Clear All flags */
2788 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2790 /* If IDMA Context, disable Internal DMA */
2791 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
2793 hsd->State = HAL_SD_STATE_READY;
2795 /* Initialize the SD operation */
2796 hsd->Context = SD_CONTEXT_NONE;
2798 CardState = HAL_SD_GetCardState(hsd);
2799 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2801 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2803 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2805 return HAL_ERROR;
2807 return HAL_OK;
2811 * @brief Abort the current transfer and disable the SD (IT mode).
2812 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2813 * the configuration information for SD module.
2814 * @retval HAL status
2816 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
2818 HAL_SD_CardStateTypeDef CardState;
2820 /* Disable All interrupts */
2821 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2822 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2824 /* If IDMA Context, disable Internal DMA */
2825 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
2827 /* Clear All flags */
2828 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2830 CardState = HAL_SD_GetCardState(hsd);
2831 hsd->State = HAL_SD_STATE_READY;
2833 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2835 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2838 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2840 return HAL_ERROR;
2842 else
2844 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2845 hsd->AbortCpltCallback(hsd);
2846 #else
2847 HAL_SD_AbortCallback(hsd);
2848 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2851 return HAL_OK;
2855 * @}
2859 * @}
2862 /* Private function ----------------------------------------------------------*/
2863 /** @addtogroup SD_Private_Functions
2864 * @{
2869 * @brief Initializes the sd card.
2870 * @param hsd: Pointer to SD handle
2871 * @retval SD Card error state
2873 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
2875 HAL_SD_CardCSDTypeDef CSD;
2876 uint32_t errorstate;
2877 uint16_t sd_rca = 1U;
2879 /* Check the power State */
2880 if(SDMMC_GetPowerState(hsd->Instance) == 0U)
2882 /* Power off */
2883 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2886 if(hsd->SdCard.CardType != CARD_SECURED)
2888 /* Send CMD2 ALL_SEND_CID */
2889 errorstate = SDMMC_CmdSendCID(hsd->Instance);
2890 if(errorstate != HAL_SD_ERROR_NONE)
2892 return errorstate;
2894 else
2896 /* Get Card identification number data */
2897 hsd->CID[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2898 hsd->CID[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
2899 hsd->CID[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
2900 hsd->CID[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
2904 if(hsd->SdCard.CardType != CARD_SECURED)
2906 /* Send CMD3 SET_REL_ADDR with argument 0 */
2907 /* SD Card publishes its RCA. */
2908 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
2909 if(errorstate != HAL_SD_ERROR_NONE)
2911 return errorstate;
2914 if(hsd->SdCard.CardType != CARD_SECURED)
2916 /* Get the SD card RCA */
2917 hsd->SdCard.RelCardAdd = sd_rca;
2919 /* Send CMD9 SEND_CSD with argument as card's RCA */
2920 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2921 if(errorstate != HAL_SD_ERROR_NONE)
2923 return errorstate;
2925 else
2927 /* Get Card Specific Data */
2928 hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2929 hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
2930 hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
2931 hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
2935 /* Get the Card Class */
2936 hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20U);
2938 /* Get CSD parameters */
2939 if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
2941 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2944 /* Select the Card */
2945 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
2946 if(errorstate != HAL_SD_ERROR_NONE)
2948 return errorstate;
2951 /* All cards are initialized */
2952 return HAL_SD_ERROR_NONE;
2956 * @brief Enquires cards about their operating voltage and configures clock
2957 * controls and stores SD information that will be needed in future
2958 * in the SD handle.
2959 * @param hsd: Pointer to SD handle
2960 * @retval error state
2962 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
2964 __IO uint32_t count = 0U;
2965 uint32_t response = 0U, validvoltage = 0U;
2966 uint32_t errorstate;
2967 #if (USE_SD_TRANSCEIVER != 0U)
2968 uint32_t tickstart = HAL_GetTick();
2969 #endif /* USE_SD_TRANSCEIVER */
2971 /* CMD0: GO_IDLE_STATE */
2972 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2973 if(errorstate != HAL_SD_ERROR_NONE)
2975 return errorstate;
2978 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2979 errorstate = SDMMC_CmdOperCond(hsd->Instance);
2980 if(errorstate != HAL_SD_ERROR_NONE)
2982 hsd->SdCard.CardVersion = CARD_V1_X;
2983 /* CMD0: GO_IDLE_STATE */
2984 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2985 if(errorstate != HAL_SD_ERROR_NONE)
2987 return errorstate;
2991 else
2993 hsd->SdCard.CardVersion = CARD_V2_X;
2996 if( hsd->SdCard.CardVersion == CARD_V2_X)
2998 /* SEND CMD55 APP_CMD with RCA as 0 */
2999 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
3000 if(errorstate != HAL_SD_ERROR_NONE)
3002 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
3005 /* SD CARD */
3006 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
3007 while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
3009 /* SEND CMD55 APP_CMD with RCA as 0 */
3010 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
3011 if(errorstate != HAL_SD_ERROR_NONE)
3013 return errorstate;
3016 /* Send CMD41 */
3017 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
3018 if(errorstate != HAL_SD_ERROR_NONE)
3020 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
3023 /* Get command response */
3024 response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3026 /* Get operating voltage*/
3027 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
3029 count++;
3032 if(count >= SDMMC_MAX_VOLT_TRIAL)
3034 return HAL_SD_ERROR_INVALID_VOLTRANGE;
3037 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
3039 hsd->SdCard.CardType = CARD_SDHC_SDXC;
3040 #if (USE_SD_TRANSCEIVER != 0U)
3041 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
3043 if((response & SD_SWITCH_1_8V_CAPACITY) == SD_SWITCH_1_8V_CAPACITY)
3045 hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
3047 /* Start switching procedue */
3048 hsd->Instance->POWER |= SDMMC_POWER_VSWITCHEN;
3050 /* Send CMD11 to switch 1.8V mode */
3051 errorstate = SDMMC_CmdVoltageSwitch(hsd->Instance);
3052 if(errorstate != HAL_SD_ERROR_NONE)
3054 return errorstate;
3057 /* Check to CKSTOP */
3058 while(( hsd->Instance->STA & SDMMC_FLAG_CKSTOP) != SDMMC_FLAG_CKSTOP)
3060 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3062 return HAL_SD_ERROR_TIMEOUT;
3066 /* Clear CKSTOP Flag */
3067 hsd->Instance->ICR = SDMMC_FLAG_CKSTOP;
3069 /* Check to BusyD0 */
3070 if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) != SDMMC_FLAG_BUSYD0)
3072 /* Error when activate Voltage Switch in SDMMC Peripheral */
3073 return SDMMC_ERROR_UNSUPPORTED_FEATURE;
3075 else
3077 /* Enable Transceiver Switch PIN */
3078 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3079 hsd->DriveTransceiver_1_8V_Callback(SET);
3080 #else
3081 HAL_SD_DriveTransceiver_1_8V_Callback(SET);
3082 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
3084 /* Switch ready */
3085 hsd->Instance->POWER |= SDMMC_POWER_VSWITCH;
3087 /* Check VSWEND Flag */
3088 while(( hsd->Instance->STA & SDMMC_FLAG_VSWEND) != SDMMC_FLAG_VSWEND)
3090 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3092 return HAL_SD_ERROR_TIMEOUT;
3096 /* Clear VSWEND Flag */
3097 hsd->Instance->ICR = SDMMC_FLAG_VSWEND;
3099 /* Check BusyD0 status */
3100 if(( hsd->Instance->STA & SDMMC_FLAG_BUSYD0) == SDMMC_FLAG_BUSYD0)
3102 /* Error when enabling 1.8V mode */
3103 return HAL_SD_ERROR_INVALID_VOLTRANGE;
3105 /* Switch to 1.8V OK */
3107 /* Disable VSWITCH FLAG from SDMMC Peripheral */
3108 hsd->Instance->POWER = 0x13U;
3110 /* Clean Status flags */
3111 hsd->Instance->ICR = 0xFFFFFFFFU;
3115 #endif /* USE_SD_TRANSCEIVER */
3118 return HAL_SD_ERROR_NONE;
3122 * @brief Turns the SDMMC output signals off.
3123 * @param hsd: Pointer to SD handle
3124 * @retval None
3126 static void SD_PowerOFF(SD_HandleTypeDef *hsd)
3128 /* Set Power State to OFF */
3129 (void)SDMMC_PowerState_OFF(hsd->Instance);
3133 * @brief Send Status info command.
3134 * @param hsd: pointer to SD handle
3135 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
3136 * SD Status register)
3137 * @retval error state
3139 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
3141 SDMMC_DataInitTypeDef config;
3142 uint32_t errorstate;
3143 uint32_t tickstart = HAL_GetTick();
3144 uint32_t count;
3145 uint32_t *pData = pSDstatus;
3147 /* Check SD response */
3148 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3150 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3153 /* Set block size for card if it is not equal to current block size for card */
3154 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
3155 if(errorstate != HAL_SD_ERROR_NONE)
3157 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3158 return errorstate;
3161 /* Send CMD55 */
3162 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3163 if(errorstate != HAL_SD_ERROR_NONE)
3165 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3166 return errorstate;
3169 /* Configure the SD DPSM (Data Path State Machine) */
3170 config.DataTimeOut = SDMMC_DATATIMEOUT;
3171 config.DataLength = 64U;
3172 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
3173 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3174 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3175 config.DPSM = SDMMC_DPSM_ENABLE;
3176 (void)SDMMC_ConfigData(hsd->Instance, &config);
3178 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
3179 errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
3180 if(errorstate != HAL_SD_ERROR_NONE)
3182 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3183 return errorstate;
3186 /* Get status data */
3187 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
3189 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3191 for(count = 0U; count < 8U; count++)
3193 *pData = SDMMC_ReadFIFO(hsd->Instance);
3194 pData++;
3198 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3200 return HAL_SD_ERROR_TIMEOUT;
3204 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3206 return HAL_SD_ERROR_DATA_TIMEOUT;
3208 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3210 return HAL_SD_ERROR_DATA_CRC_FAIL;
3212 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3214 return HAL_SD_ERROR_RX_OVERRUN;
3216 else
3218 /* Nothing to do */
3221 while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DPSMACT)))
3223 *pData = SDMMC_ReadFIFO(hsd->Instance);
3224 pData++;
3226 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3228 return HAL_SD_ERROR_TIMEOUT;
3232 /* Clear all the static status flags*/
3233 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3235 return HAL_SD_ERROR_NONE;
3239 * @brief Returns the current card's status.
3240 * @param hsd: Pointer to SD handle
3241 * @param pCardStatus: pointer to the buffer that will contain the SD card
3242 * status (Card Status register)
3243 * @retval error state
3245 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
3247 uint32_t errorstate;
3249 if(pCardStatus == NULL)
3251 return HAL_SD_ERROR_PARAM;
3254 /* Send Status command */
3255 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3256 if(errorstate != HAL_SD_ERROR_NONE)
3258 return errorstate;
3261 /* Get SD card status */
3262 *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3264 return HAL_SD_ERROR_NONE;
3268 * @brief Enables the SDMMC wide bus mode.
3269 * @param hsd: pointer to SD handle
3270 * @retval error state
3272 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
3274 uint32_t scr[2U] = {0UL, 0UL};
3275 uint32_t errorstate;
3277 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3279 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3282 /* Get SCR Register */
3283 errorstate = SD_FindSCR(hsd, scr);
3284 if(errorstate != HAL_SD_ERROR_NONE)
3286 return errorstate;
3289 /* If requested card supports wide bus operation */
3290 if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
3292 /* Send CMD55 APP_CMD with argument as card's RCA.*/
3293 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3294 if(errorstate != HAL_SD_ERROR_NONE)
3296 return errorstate;
3299 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3300 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
3301 if(errorstate != HAL_SD_ERROR_NONE)
3303 return errorstate;
3306 return HAL_SD_ERROR_NONE;
3308 else
3310 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3315 * @brief Disables the SDMMC wide bus mode.
3316 * @param hsd: Pointer to SD handle
3317 * @retval error state
3319 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3321 uint32_t scr[2U] = {0UL, 0UL};
3322 uint32_t errorstate;
3324 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3326 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3329 /* Get SCR Register */
3330 errorstate = SD_FindSCR(hsd, scr);
3331 if(errorstate != HAL_SD_ERROR_NONE)
3333 return errorstate;
3336 /* If requested card supports 1 bit mode operation */
3337 if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
3339 /* Send CMD55 APP_CMD with argument as card's RCA */
3340 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3341 if(errorstate != HAL_SD_ERROR_NONE)
3343 return errorstate;
3346 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3347 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
3348 if(errorstate != HAL_SD_ERROR_NONE)
3350 return errorstate;
3353 return HAL_SD_ERROR_NONE;
3355 else
3357 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3363 * @brief Finds the SD card SCR register value.
3364 * @param hsd: Pointer to SD handle
3365 * @param pSCR: pointer to the buffer that will contain the SCR value
3366 * @retval error state
3368 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
3370 SDMMC_DataInitTypeDef config;
3371 uint32_t errorstate;
3372 uint32_t tickstart = HAL_GetTick();
3373 uint32_t index = 0U;
3374 uint32_t tempscr[2U] = {0UL, 0UL};
3375 uint32_t *scr = pSCR;
3377 /* Set Block Size To 8 Bytes */
3378 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
3379 if(errorstate != HAL_SD_ERROR_NONE)
3381 return errorstate;
3384 /* Send CMD55 APP_CMD with argument as card's RCA */
3385 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
3386 if(errorstate != HAL_SD_ERROR_NONE)
3388 return errorstate;
3391 config.DataTimeOut = SDMMC_DATATIMEOUT;
3392 config.DataLength = 8U;
3393 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
3394 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3395 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3396 config.DPSM = SDMMC_DPSM_ENABLE;
3397 (void)SDMMC_ConfigData(hsd->Instance, &config);
3399 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3400 errorstate = SDMMC_CmdSendSCR(hsd->Instance);
3401 if(errorstate != HAL_SD_ERROR_NONE)
3403 return errorstate;
3406 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND | SDMMC_FLAG_DATAEND))
3408 if((!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOE)) && (index == 0U))
3410 tempscr[0] = SDMMC_ReadFIFO(hsd->Instance);
3411 tempscr[1] = SDMMC_ReadFIFO(hsd->Instance);
3412 index++;
3416 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3418 return HAL_SD_ERROR_TIMEOUT;
3422 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3424 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3426 return HAL_SD_ERROR_DATA_TIMEOUT;
3428 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3430 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3432 return HAL_SD_ERROR_DATA_CRC_FAIL;
3434 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3436 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3438 return HAL_SD_ERROR_RX_OVERRUN;
3440 else
3442 /* No error flag set */
3443 /* Clear all the static flags */
3444 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3446 *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
3447 ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24));
3448 scr++;
3449 *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
3450 ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24));
3454 return HAL_SD_ERROR_NONE;
3458 * @brief Wrap up reading in non-blocking mode.
3459 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3460 * the configuration information.
3461 * @retval None
3463 static void SD_Read_IT(SD_HandleTypeDef *hsd)
3465 uint32_t count, data;
3466 uint8_t* tmp;
3468 tmp = hsd->pRxBuffPtr;
3470 if (hsd->RxXferSize >= 32U)
3472 /* Read data from SDMMC Rx FIFO */
3473 for(count = 0U; count < 8U; count++)
3475 data = SDMMC_ReadFIFO(hsd->Instance);
3476 *tmp = (uint8_t)(data & 0xFFU);
3477 tmp++;
3478 *tmp = (uint8_t)((data >> 8U) & 0xFFU);
3479 tmp++;
3480 *tmp = (uint8_t)((data >> 16U) & 0xFFU);
3481 tmp++;
3482 *tmp = (uint8_t)((data >> 24U) & 0xFFU);
3483 tmp++;
3486 hsd->pRxBuffPtr = tmp;
3487 hsd->RxXferSize -= 32U;
3492 * @brief Wrap up writing in non-blocking mode.
3493 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3494 * the configuration information.
3495 * @retval None
3497 static void SD_Write_IT(SD_HandleTypeDef *hsd)
3499 uint32_t count, data;
3500 uint8_t* tmp;
3502 tmp = hsd->pTxBuffPtr;
3504 if (hsd->TxXferSize >= 32U)
3506 /* Write data to SDMMC Tx FIFO */
3507 for(count = 0U; count < 8U; count++)
3509 data = (uint32_t)(*tmp);
3510 tmp++;
3511 data |= ((uint32_t)(*tmp) << 8U);
3512 tmp++;
3513 data |= ((uint32_t)(*tmp) << 16U);
3514 tmp++;
3515 data |= ((uint32_t)(*tmp) << 24U);
3516 tmp++;
3517 (void)SDMMC_WriteFIFO(hsd->Instance, &data);
3520 hsd->pTxBuffPtr = tmp;
3521 hsd->TxXferSize -= 32U;
3526 * @brief Switches the SD card to High Speed mode.
3527 * This API must be used after "Transfer State"
3528 * @note This operation should be followed by the configuration
3529 * of PLL to have SDMMCCK clock between 50 and 120 MHz
3530 * @param hsd: SD handle
3531 * @retval SD Card error state
3533 uint32_t SD_HighSpeed(SD_HandleTypeDef *hsd)
3535 uint32_t errorstate = HAL_SD_ERROR_NONE;
3536 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
3537 uint32_t SD_hs[16] = {0};
3538 uint32_t count, loop = 0 ;
3539 uint32_t Timeout = HAL_GetTick();
3541 if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
3543 /* Standard Speed Card <= 12.5Mhz */
3544 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3547 if(hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
3549 /* Initialize the Data control register */
3550 hsd->Instance->DCTRL = 0;
3551 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64);
3553 if (errorstate != HAL_SD_ERROR_NONE)
3555 return errorstate;
3558 /* Configure the SD DPSM (Data Path State Machine) */
3559 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
3560 sdmmc_datainitstructure.DataLength = 64;
3561 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
3562 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3563 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3564 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
3566 if ( SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK)
3568 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3572 errorstate = SDMMC_CmdSwitch(hsd->Instance,SDMMC_SDR25_SWITCH_PATTERN);
3573 if(errorstate != HAL_SD_ERROR_NONE)
3575 return errorstate;
3578 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
3580 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3582 for (count = 0U; count < 8U; count++)
3584 SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
3586 loop ++;
3589 if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
3591 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
3592 hsd->State= HAL_SD_STATE_READY;
3593 return HAL_SD_ERROR_TIMEOUT;
3597 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3599 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3601 return errorstate;
3603 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3605 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3607 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
3609 return errorstate;
3611 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3613 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3615 errorstate = SDMMC_ERROR_RX_OVERRUN;
3617 return errorstate;
3619 else
3621 /* No error flag set */
3624 /* Clear all the static flags */
3625 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3627 /* Test if the switch mode HS is ok */
3628 if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
3630 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
3635 return errorstate;
3638 #if (USE_SD_TRANSCEIVER != 0U)
3640 * @brief Switches the SD card to Ultra High Speed mode.
3641 * This API must be used after "Transfer State"
3642 * @note This operation should be followed by the configuration
3643 * of PLL to have SDMMCCK clock between 50 and 120 MHz
3644 * @param hsd: SD handle
3645 * @retval SD Card error state
3647 static uint32_t SD_UltraHighSpeed(SD_HandleTypeDef *hsd)
3649 uint32_t errorstate = HAL_SD_ERROR_NONE;
3650 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
3651 uint32_t SD_hs[16] = {0};
3652 uint32_t count, loop = 0 ;
3653 uint32_t Timeout = HAL_GetTick();
3655 if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
3657 /* Standard Speed Card <= 12.5Mhz */
3658 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3661 if(hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
3663 /* Initialize the Data control register */
3664 hsd->Instance->DCTRL = 0;
3665 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64);
3667 if (errorstate != HAL_SD_ERROR_NONE)
3669 return errorstate;
3672 /* Configure the SD DPSM (Data Path State Machine) */
3673 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
3674 sdmmc_datainitstructure.DataLength = 64;
3675 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
3676 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3677 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3678 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
3680 if ( SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK)
3682 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3685 errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_SDR104_SWITCH_PATTERN);
3686 if(errorstate != HAL_SD_ERROR_NONE)
3688 return errorstate;
3691 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
3693 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3695 for (count = 0U; count < 8U; count++)
3697 SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
3699 loop ++;
3702 if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
3704 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
3705 hsd->State= HAL_SD_STATE_READY;
3706 return HAL_SD_ERROR_TIMEOUT;
3710 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3712 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3714 return errorstate;
3716 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3718 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3720 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
3722 return errorstate;
3724 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3726 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3728 errorstate = SDMMC_ERROR_RX_OVERRUN;
3730 return errorstate;
3732 else
3734 /* No error flag set */
3737 /* Clear all the static flags */
3738 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3740 /* Test if the switch mode HS is ok */
3741 if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
3743 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
3745 else
3747 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3748 hsd->DriveTransceiver_1_8V_Callback(SET);
3749 #else
3750 HAL_SD_DriveTransceiver_1_8V_Callback(SET);
3751 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
3752 #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
3753 /* Enable DelayBlock Peripheral */
3754 /* SDMMC_FB_CLK tuned feedback clock selected as receive clock, for SDR104 */
3755 MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX,SDMMC_CLKCR_SELCLKRX_1);
3756 if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
3758 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3760 #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
3764 return errorstate;
3768 * @brief Switches the SD card to Double Data Rate (DDR) mode.
3769 * This API must be used after "Transfer State"
3770 * @note This operation should be followed by the configuration
3771 * of PLL to have SDMMCCK clock less than 50MHz
3772 * @param hsd: SD handle
3773 * @retval SD Card error state
3775 static uint32_t SD_DDR_Mode(SD_HandleTypeDef *hsd)
3777 uint32_t errorstate = HAL_SD_ERROR_NONE;
3778 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
3779 uint32_t SD_hs[16] = {0};
3780 uint32_t count, loop = 0 ;
3781 uint32_t Timeout = HAL_GetTick();
3783 if(hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
3785 /* Standard Speed Card <= 12.5Mhz */
3786 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3789 if(hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
3791 /* Initialize the Data control register */
3792 hsd->Instance->DCTRL = 0;
3793 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64);
3795 if (errorstate != HAL_SD_ERROR_NONE)
3797 return errorstate;
3800 /* Configure the SD DPSM (Data Path State Machine) */
3801 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
3802 sdmmc_datainitstructure.DataLength = 64;
3803 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
3804 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3805 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3806 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
3808 if ( SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK)
3810 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3813 errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_DDR50_SWITCH_PATTERN);
3814 if(errorstate != HAL_SD_ERROR_NONE)
3816 return errorstate;
3819 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND| SDMMC_FLAG_DATAEND ))
3821 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3823 for (count = 0U; count < 8U; count++)
3825 SD_hs[(8U*loop)+count] = SDMMC_ReadFIFO(hsd->Instance);
3827 loop ++;
3830 if((HAL_GetTick()-Timeout) >= SDMMC_DATATIMEOUT)
3832 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
3833 hsd->State= HAL_SD_STATE_READY;
3834 return HAL_SD_ERROR_TIMEOUT;
3838 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3840 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3842 return errorstate;
3844 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3846 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3848 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
3850 return errorstate;
3852 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3854 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3856 errorstate = SDMMC_ERROR_RX_OVERRUN;
3858 return errorstate;
3860 else
3862 /* No error flag set */
3865 /* Clear all the static flags */
3866 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3868 /* Test if the switch mode is ok */
3869 if ((((uint8_t*)SD_hs)[13] & 2U) != 2U)
3871 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
3873 else
3875 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3876 hsd->DriveTransceiver_1_8V_Callback(SET);
3877 #else
3878 HAL_SD_DriveTransceiver_1_8V_Callback(SET);
3879 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
3880 #if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
3881 /* Enable DelayBlock Peripheral */
3882 /* SDMMC_CKin feedback clock selected as receive clock, for DDR50 */
3883 MODIFY_REG(hsd->Instance->CLKCR, SDMMC_CLKCR_SELCLKRX,SDMMC_CLKCR_SELCLKRX_0);
3884 if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
3886 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3888 #endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
3892 return errorstate;
3895 #endif /* USE_SD_TRANSCEIVER */
3898 * @brief Read DMA Buffer 0 Transfer completed callbacks
3899 * @param hsd: SD handle
3900 * @retval None
3902 __weak void HAL_SDEx_Read_DMADoubleBuf0CpltCallback(SD_HandleTypeDef *hsd)
3904 /* Prevent unused argument(s) compilation warning */
3905 UNUSED(hsd);
3907 /* NOTE : This function should not be modified, when the callback is needed,
3908 the HAL_SDEx_Read_DMADoubleBuf0CpltCallback can be implemented in the user file
3913 * @brief Read DMA Buffer 1 Transfer completed callbacks
3914 * @param hsd: SD handle
3915 * @retval None
3917 __weak void HAL_SDEx_Read_DMADoubleBuf1CpltCallback(SD_HandleTypeDef *hsd)
3919 /* Prevent unused argument(s) compilation warning */
3920 UNUSED(hsd);
3922 /* NOTE : This function should not be modified, when the callback is needed,
3923 the HAL_SDEx_Read_DMADoubleBuf1CpltCallback can be implemented in the user file
3928 * @brief Write DMA Buffer 0 Transfer completed callbacks
3929 * @param hsd: SD handle
3930 * @retval None
3932 __weak void HAL_SDEx_Write_DMADoubleBuf0CpltCallback(SD_HandleTypeDef *hsd)
3934 /* Prevent unused argument(s) compilation warning */
3935 UNUSED(hsd);
3937 /* NOTE : This function should not be modified, when the callback is needed,
3938 the HAL_SDEx_Write_DMADoubleBuf0CpltCallback can be implemented in the user file
3943 * @brief Write DMA Buffer 1 Transfer completed callbacks
3944 * @param hsd: SD handle
3945 * @retval None
3947 __weak void HAL_SDEx_Write_DMADoubleBuf1CpltCallback(SD_HandleTypeDef *hsd)
3949 /* Prevent unused argument(s) compilation warning */
3950 UNUSED(hsd);
3952 /* NOTE : This function should not be modified, when the callback is needed,
3953 the HAL_SDEx_Write_DMADoubleBuf1CpltCallback can be implemented in the user file
3959 * @}
3962 #endif /* HAL_SD_MODULE_ENABLED */
3965 * @}
3969 * @}
3972 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/