Updated and Validated
[betaflight.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_sd.c
blob781d5888a50ddf1da6267f38bda58a0d1afd3162
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_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 * + SD card Control 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 implement 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 (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
37 and HAL_SD_WriteBlocks_DMA() APIs).
38 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
39 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
40 (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
41 (+++) Configure the SDMMC and DMA interrupt priorities using functions
42 HAL_NVIC_SetPriority(); DMA priority is superior to SDMMC's priority
43 (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ()
44 (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
45 and __HAL_SD_DISABLE_IT() inside the communication process.
46 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
47 and __HAL_SD_CLEAR_IT()
48 (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
49 and HAL_SD_WriteBlocks_IT() APIs).
50 (+++) Configure the SDMMC interrupt priorities using function
51 HAL_NVIC_SetPriority();
52 (+++) Enable the NVIC SDMMC IRQs using function HAL_NVIC_EnableIRQ()
53 (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
54 and __HAL_SD_DISABLE_IT() inside the communication process.
55 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
56 and __HAL_SD_CLEAR_IT()
57 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
60 *** SD Card Initialization and configuration ***
61 ================================================
62 [..]
63 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
64 SDMMC IP (STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
65 This function provide the following operations:
67 (#) Initialize the SDMMC peripheral interface with defaullt configuration.
68 The initialization process is done at 400KHz. You can change or adapt
69 this frequency by adjusting the "ClockDiv" field.
70 The SD Card frequency (SDMMC_CK) is computed as follows:
72 SDMMC_CK = SDMMCCLK / (ClockDiv + 2)
74 In initialization mode and according to the SD Card standard,
75 make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
77 This phase of initialization is done through SDMMC_Init() and
78 SDMMC_PowerState_ON() SDMMC low level APIs.
80 (#) Initialize the SD card. The API used is HAL_SD_InitCard().
81 This phase allows the card initialization and identification
82 and check the SD Card type (Standard Capacity or High Capacity)
83 The initialization flow is compatible with SD standard.
85 This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
86 of plug-off plug-in.
88 (#) Configure the SD Card Data transfer frequency. By Default, the card transfer
89 frequency is set to 24MHz. You can change or adapt this frequency by adjusting
90 the "ClockDiv" field.
91 In transfer mode and according to the SD Card standard, make sure that the
92 SDMMC_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
93 To be able to use a frequency higher than 24MHz, you should use the SDMMC
94 peripheral in bypass mode. Refer to the corresponding reference manual
95 for more details.
97 (#) Select the corresponding SD Card according to the address read with the step 2.
99 (#) Configure the SD Card in wide bus mode: 4-bits data.
101 *** SD Card Read operation ***
102 ==============================
103 [..]
104 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
105 This function allows the read of 512 bytes blocks.
106 You can choose either one block read operation or multiple block read operation
107 by adjusting the "NumberOfBlocks" parameter.
108 After this, you have to ensure that the transfer is done correctly. The check is done
109 through HAL_SD_GetCardState() function for SD card state.
111 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
112 This function allows the read of 512 bytes blocks.
113 You can choose either one block read operation or multiple block read operation
114 by adjusting the "NumberOfBlocks" parameter.
115 After this, you have to ensure that the transfer is done correctly. The check is done
116 through HAL_SD_GetCardState() function for SD card state.
117 You could also check the DMA transfer process through the SD Rx interrupt event.
119 (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
120 This function allows the read of 512 bytes blocks.
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.
125 You could also check the IT transfer process through the SD Rx interrupt event.
127 *** SD Card Write operation ***
128 ===============================
129 [..]
130 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
131 This function allows the read of 512 bytes blocks.
132 You can choose either one block read operation or multiple block read operation
133 by adjusting the "NumberOfBlocks" parameter.
134 After this, you have to ensure that the transfer is done correctly. The check is done
135 through HAL_SD_GetCardState() function for SD card state.
137 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
138 This function allows the read of 512 bytes blocks.
139 You can choose either one block read operation or multiple block read operation
140 by adjusting the "NumberOfBlocks" parameter.
141 After this, you have to ensure that the transfer is done correctly. The check is done
142 through HAL_SD_GetCardState() function for SD card state.
143 You could also check the DMA transfer process through the SD Tx interrupt event.
145 (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
146 This function allows the read of 512 bytes blocks.
147 You can choose either one block read operation or multiple block read operation
148 by adjusting the "NumberOfBlocks" parameter.
149 After this, you have to ensure that the transfer is done correctly. The check is done
150 through HAL_SD_GetCardState() function for SD card state.
151 You could also check the IT transfer process through the SD Tx interrupt event.
153 *** SD card status ***
154 ======================
155 [..]
156 (+) The SD Status contains status bits that are related to the SD Memory
157 Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
159 *** SD card information ***
160 ===========================
161 [..]
162 (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
163 It returns useful information about the SD card such as block size, card type,
164 block number ...
166 *** SD card CSD register ***
167 ============================
168 [..]
169 (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
170 Some of the CSD parameters are useful for card initialization and identification.
172 *** SD card CID register ***
173 ============================
174 [..]
175 (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
176 Some of the CSD parameters are useful for card initialization and identification.
178 *** SD HAL driver macros list ***
179 ==================================
180 [..]
181 Below the list of most used macros in SD HAL driver.
183 (+) __HAL_SD_ENABLE : Enable the SD device
184 (+) __HAL_SD_DISABLE : Disable the SD device
185 (+) __HAL_SD_DMA_ENABLE: Enable the SDMMC DMA transfer
186 (+) __HAL_SD_DMA_DISABLE: Disable the SDMMC DMA transfer
187 (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
188 (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
189 (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
190 (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
192 [..]
193 (@) You can refer to the SD HAL driver header file for more useful macros
195 *** Callback registration ***
196 =============================================
197 [..]
198 The compilation define USE_HAL_SD_REGISTER_CALLBACKS when set to 1
199 allows the user to configure dynamically the driver callbacks.
201 Use Functions @ref HAL_SD_RegisterCallback() to register a user callback,
202 it allows to register following callbacks:
203 (+) TxCpltCallback : callback when a transmission transfer is completed.
204 (+) RxCpltCallback : callback when a reception transfer is completed.
205 (+) ErrorCallback : callback when error occurs.
206 (+) AbortCpltCallback : callback when abort is completed.
207 (+) MspInitCallback : SD MspInit.
208 (+) MspDeInitCallback : SD MspDeInit.
209 This function takes as parameters the HAL peripheral handle, the Callback ID
210 and a pointer to the user callback function.
212 Use function @ref HAL_SD_UnRegisterCallback() to reset a callback to the default
213 weak (surcharged) function. It allows to reset following callbacks:
214 (+) TxCpltCallback : callback when a transmission transfer is completed.
215 (+) RxCpltCallback : callback when a reception transfer is completed.
216 (+) ErrorCallback : callback when error occurs.
217 (+) AbortCpltCallback : callback when abort is completed.
218 (+) MspInitCallback : SD MspInit.
219 (+) MspDeInitCallback : SD MspDeInit.
220 This function) takes as parameters the HAL peripheral handle and the Callback ID.
222 By default, after the @ref HAL_SD_Init and if the state is HAL_SD_STATE_RESET
223 all callbacks are reset to the corresponding legacy weak (surcharged) functions.
224 Exception done for MspInit and MspDeInit callbacks that are respectively
225 reset to the legacy weak (surcharged) functions in the @ref HAL_SD_Init
226 and @ref HAL_SD_DeInit only when these callbacks are null (not registered beforehand).
227 If not, MspInit or MspDeInit are not null, the @ref HAL_SD_Init and @ref HAL_SD_DeInit
228 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
230 Callbacks can be registered/unregistered in READY state only.
231 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
232 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
233 during the Init/DeInit.
234 In that case first register the MspInit/MspDeInit user callbacks
235 using @ref HAL_SD_RegisterCallback before calling @ref HAL_SD_DeInit
236 or @ref HAL_SD_Init function.
238 When The compilation define USE_HAL_SD_REGISTER_CALLBACKS is set to 0 or
239 not defined, the callback registering feature is not available
240 and weak (surcharged) callbacks are used.
242 @endverbatim
243 ******************************************************************************
244 * @attention
246 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
247 * All rights reserved.</center></h2>
249 * This software component is licensed by ST under BSD 3-Clause license,
250 * the "License"; You may not use this file except in compliance with the
251 * License. You may obtain a copy of the License at:
252 * opensource.org/licenses/BSD-3-Clause
254 ******************************************************************************
257 /* Includes ------------------------------------------------------------------*/
258 #include "stm32f7xx_hal.h"
260 /** @addtogroup STM32F7xx_HAL_Driver
261 * @{
264 /** @addtogroup SD
265 * @{
268 #ifdef HAL_SD_MODULE_ENABLED
270 /* Private typedef -----------------------------------------------------------*/
271 /* Private define ------------------------------------------------------------*/
272 /** @addtogroup SD_Private_Defines
273 * @{
277 * @}
280 /* Private macro -------------------------------------------------------------*/
281 /* Private variables ---------------------------------------------------------*/
282 /* Private function prototypes -----------------------------------------------*/
283 /* Private functions ---------------------------------------------------------*/
284 /** @defgroup SD_Private_Functions SD Private Functions
285 * @{
287 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd);
288 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd);
289 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
290 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
291 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd);
292 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
293 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
294 static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd);
295 static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd);
296 static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd);
297 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
298 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
299 static void SD_DMAError(DMA_HandleTypeDef *hdma);
300 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma);
301 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma);
303 * @}
306 /* Exported functions --------------------------------------------------------*/
307 /** @addtogroup SD_Exported_Functions
308 * @{
311 /** @addtogroup SD_Exported_Functions_Group1
312 * @brief Initialization and de-initialization functions
314 @verbatim
315 ==============================================================================
316 ##### Initialization and de-initialization functions #####
317 ==============================================================================
318 [..]
319 This section provides functions allowing to initialize/de-initialize the SD
320 card device to be ready for use.
322 @endverbatim
323 * @{
327 * @brief Initializes the SD according to the specified parameters in the
328 SD_HandleTypeDef and create the associated handle.
329 * @param hsd Pointer to the SD handle
330 * @retval HAL status
332 HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
334 /* Check the SD handle allocation */
335 if(hsd == NULL)
337 return HAL_ERROR;
340 /* Check the parameters */
341 assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
342 assert_param(IS_SDMMC_CLOCK_EDGE(hsd->Init.ClockEdge));
343 assert_param(IS_SDMMC_CLOCK_BYPASS(hsd->Init.ClockBypass));
344 assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
345 assert_param(IS_SDMMC_BUS_WIDE(hsd->Init.BusWide));
346 assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
347 assert_param(IS_SDMMC_CLKDIV(hsd->Init.ClockDiv));
349 if(hsd->State == HAL_SD_STATE_RESET)
351 /* Allocate lock resource and initialize it */
352 hsd->Lock = HAL_UNLOCKED;
353 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
354 /* Reset Callback pointers in HAL_SD_STATE_RESET only */
355 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
356 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
357 hsd->ErrorCallback = HAL_SD_ErrorCallback;
358 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
360 if(hsd->MspInitCallback == NULL)
362 hsd->MspInitCallback = HAL_SD_MspInit;
365 /* Init the low level hardware */
366 hsd->MspInitCallback(hsd);
367 #else
368 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
369 HAL_SD_MspInit(hsd);
370 #endif
373 hsd->State = HAL_SD_STATE_BUSY;
375 /* Initialize the Card parameters */
376 HAL_SD_InitCard(hsd);
378 /* Initialize the error code */
379 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
381 /* Initialize the SD operation */
382 hsd->Context = SD_CONTEXT_NONE;
384 /* Initialize the SD state */
385 hsd->State = HAL_SD_STATE_READY;
387 return HAL_OK;
391 * @brief Initializes the SD Card.
392 * @param hsd Pointer to SD handle
393 * @note This function initializes the SD card. It could be used when a card
394 re-initialization is needed.
395 * @retval HAL status
397 HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
399 uint32_t errorstate = HAL_SD_ERROR_NONE;
400 SD_InitTypeDef Init;
402 /* Default SDMMC peripheral configuration for SD card initialization */
403 Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
404 Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
405 Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
406 Init.BusWide = SDMMC_BUS_WIDE_1B;
407 Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
408 Init.ClockDiv = SDMMC_INIT_CLK_DIV;
410 /* Initialize SDMMC peripheral interface with default configuration */
411 SDMMC_Init(hsd->Instance, Init);
413 /* Disable SDMMC Clock */
414 __HAL_SD_DISABLE(hsd);
416 /* Set Power State to ON */
417 SDMMC_PowerState_ON(hsd->Instance);
419 /* Enable SDMMC Clock */
420 __HAL_SD_ENABLE(hsd);
422 /* Required power up waiting time before starting the SD initialization sequence */
423 HAL_Delay(2);
425 /* Identify card operating voltage */
426 errorstate = SD_PowerON(hsd);
427 if(errorstate != HAL_SD_ERROR_NONE)
429 hsd->State = HAL_SD_STATE_READY;
430 hsd->ErrorCode |= errorstate;
431 return HAL_ERROR;
434 /* Card initialization */
435 errorstate = SD_InitCard(hsd);
436 if(errorstate != HAL_SD_ERROR_NONE)
438 hsd->State = HAL_SD_STATE_READY;
439 hsd->ErrorCode |= errorstate;
440 return HAL_ERROR;
443 return HAL_OK;
447 * @brief De-Initializes the SD card.
448 * @param hsd Pointer to SD handle
449 * @retval HAL status
451 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
453 /* Check the SD handle allocation */
454 if(hsd == NULL)
456 return HAL_ERROR;
459 /* Check the parameters */
460 assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
462 hsd->State = HAL_SD_STATE_BUSY;
464 /* Set SD power state to off */
465 SD_PowerOFF(hsd);
467 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
468 if(hsd->MspDeInitCallback == NULL)
470 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
473 /* DeInit the low level hardware */
474 hsd->MspDeInitCallback(hsd);
475 #else
476 /* De-Initialize the MSP layer */
477 HAL_SD_MspDeInit(hsd);
478 #endif
480 hsd->ErrorCode = HAL_SD_ERROR_NONE;
481 hsd->State = HAL_SD_STATE_RESET;
483 return HAL_OK;
488 * @brief Initializes the SD MSP.
489 * @param hsd Pointer to SD handle
490 * @retval None
492 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
494 /* Prevent unused argument(s) compilation warning */
495 UNUSED(hsd);
497 /* NOTE : This function Should not be modified, when the callback is needed,
498 the HAL_SD_MspInit could be implemented in the user file
503 * @brief De-Initialize SD MSP.
504 * @param hsd Pointer to SD handle
505 * @retval None
507 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
509 /* Prevent unused argument(s) compilation warning */
510 UNUSED(hsd);
512 /* NOTE : This function Should not be modified, when the callback is needed,
513 the HAL_SD_MspDeInit could be implemented in the user file
518 * @}
521 /** @addtogroup SD_Exported_Functions_Group2
522 * @brief Data transfer functions
524 @verbatim
525 ==============================================================================
526 ##### IO operation functions #####
527 ==============================================================================
528 [..]
529 This subsection provides a set of functions allowing to manage the data
530 transfer from/to SD card.
532 @endverbatim
533 * @{
537 * @brief Reads block(s) from a specified address in a card. The Data transfer
538 * is managed by polling mode.
539 * @note This API should be followed by a check on the card state through
540 * HAL_SD_GetCardState().
541 * @param hsd Pointer to SD handle
542 * @param pData pointer to the buffer that will contain the received data
543 * @param BlockAdd Block Address from where data is to be read
544 * @param NumberOfBlocks Number of SD blocks to read
545 * @param Timeout Specify timeout value
546 * @retval HAL status
548 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
550 SDMMC_DataInitTypeDef config;
551 uint32_t errorstate = HAL_SD_ERROR_NONE;
552 uint32_t tickstart = HAL_GetTick();
553 uint32_t count = 0, *tempbuff = (uint32_t *)pData;
555 if(NULL == pData)
557 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
558 return HAL_ERROR;
561 if(hsd->State == HAL_SD_STATE_READY)
563 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
565 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
567 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
568 return HAL_ERROR;
571 hsd->State = HAL_SD_STATE_BUSY;
573 /* Initialize data control register */
574 hsd->Instance->DCTRL = 0;
576 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
578 BlockAdd *= 512;
581 /* Set Block Size for Card */
582 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
583 if(errorstate != HAL_SD_ERROR_NONE)
585 /* Clear all the static flags */
586 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
587 hsd->ErrorCode |= errorstate;
588 hsd->State = HAL_SD_STATE_READY;
589 return HAL_ERROR;
592 /* Configure the SD DPSM (Data Path State Machine) */
593 config.DataTimeOut = SDMMC_DATATIMEOUT;
594 config.DataLength = NumberOfBlocks * BLOCKSIZE;
595 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
596 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
597 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
598 config.DPSM = SDMMC_DPSM_ENABLE;
599 SDMMC_ConfigData(hsd->Instance, &config);
601 /* Read block(s) in polling mode */
602 if(NumberOfBlocks > 1)
604 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
606 /* Read Multi Block command */
607 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
609 else
611 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
613 /* Read Single Block command */
614 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
616 if(errorstate != HAL_SD_ERROR_NONE)
618 /* Clear all the static flags */
619 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
620 hsd->ErrorCode |= errorstate;
621 hsd->State = HAL_SD_STATE_READY;
622 return HAL_ERROR;
625 /* Poll on SDMMC flags */
626 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
628 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
630 /* Read data from SDMMC Rx FIFO */
631 for(count = 0U; count < 8U; count++)
633 *(tempbuff + count) = SDMMC_ReadFIFO(hsd->Instance);
635 tempbuff += 8U;
638 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
640 /* Clear all the static flags */
641 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
642 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
643 hsd->State= HAL_SD_STATE_READY;
644 return HAL_TIMEOUT;
648 /* Send stop transmission command in case of multiblock read */
649 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
651 if(hsd->SdCard.CardType != CARD_SECURED)
653 /* Send stop transmission command */
654 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
655 if(errorstate != HAL_SD_ERROR_NONE)
657 /* Clear all the static flags */
658 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
659 hsd->ErrorCode |= errorstate;
660 hsd->State = HAL_SD_STATE_READY;
661 return HAL_ERROR;
666 /* Get error state */
667 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
669 /* Clear all the static flags */
670 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
671 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
672 hsd->State = HAL_SD_STATE_READY;
673 return HAL_ERROR;
675 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
677 /* Clear all the static flags */
678 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
679 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
680 hsd->State = HAL_SD_STATE_READY;
681 return HAL_ERROR;
683 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
685 /* Clear all the static flags */
686 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
687 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
688 hsd->State = HAL_SD_STATE_READY;
689 return HAL_ERROR;
692 /* Empty FIFO if there is still any data */
693 while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)))
695 *tempbuff = SDMMC_ReadFIFO(hsd->Instance);
696 tempbuff++;
698 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
700 /* Clear all the static flags */
701 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
702 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
703 hsd->State= HAL_SD_STATE_READY;
704 return HAL_ERROR;
708 /* Clear all the static flags */
709 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
711 hsd->State = HAL_SD_STATE_READY;
713 return HAL_OK;
715 else
717 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
718 return HAL_ERROR;
723 * @brief Allows to write block(s) to a specified address in a card. The Data
724 * transfer is managed by polling mode.
725 * @note This API should be followed by a check on the card state through
726 * HAL_SD_GetCardState().
727 * @param hsd Pointer to SD handle
728 * @param pData pointer to the buffer that will contain the data to transmit
729 * @param BlockAdd Block Address where data will be written
730 * @param NumberOfBlocks Number of SD blocks to write
731 * @param Timeout Specify timeout value
732 * @retval HAL status
734 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
736 SDMMC_DataInitTypeDef config;
737 uint32_t errorstate = HAL_SD_ERROR_NONE;
738 uint32_t tickstart = HAL_GetTick();
739 uint32_t count = 0;
740 uint32_t *tempbuff = (uint32_t *)pData;
742 if(NULL == pData)
744 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
745 return HAL_ERROR;
748 if(hsd->State == HAL_SD_STATE_READY)
750 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
752 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
754 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
755 return HAL_ERROR;
758 hsd->State = HAL_SD_STATE_BUSY;
760 /* Initialize data control register */
761 hsd->Instance->DCTRL = 0;
763 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
765 BlockAdd *= 512;
768 /* Set Block Size for Card */
769 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
770 if(errorstate != HAL_SD_ERROR_NONE)
772 /* Clear all the static flags */
773 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
774 hsd->ErrorCode |= errorstate;
775 hsd->State = HAL_SD_STATE_READY;
776 return HAL_ERROR;
779 /* Write Blocks in Polling mode */
780 if(NumberOfBlocks > 1U)
782 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
784 /* Write Multi Block command */
785 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
787 else
789 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
791 /* Write Single Block command */
792 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
794 if(errorstate != HAL_SD_ERROR_NONE)
796 /* Clear all the static flags */
797 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
798 hsd->ErrorCode |= errorstate;
799 hsd->State = HAL_SD_STATE_READY;
800 return HAL_ERROR;
803 /* Configure the SD DPSM (Data Path State Machine) */
804 config.DataTimeOut = SDMMC_DATATIMEOUT;
805 config.DataLength = NumberOfBlocks * BLOCKSIZE;
806 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
807 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
808 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
809 config.DPSM = SDMMC_DPSM_ENABLE;
810 SDMMC_ConfigData(hsd->Instance, &config);
812 /* Write block(s) in polling mode */
813 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
815 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE))
817 /* Write data to SDMMC Tx FIFO */
818 for(count = 0U; count < 8U; count++)
820 SDMMC_WriteFIFO(hsd->Instance, (tempbuff + count));
822 tempbuff += 8U;
825 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
827 /* Clear all the static flags */
828 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
829 hsd->ErrorCode |= errorstate;
830 hsd->State = HAL_SD_STATE_READY;
831 return HAL_TIMEOUT;
835 /* Send stop transmission command in case of multiblock write */
836 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
838 if(hsd->SdCard.CardType != CARD_SECURED)
840 /* Send stop transmission command */
841 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
842 if(errorstate != HAL_SD_ERROR_NONE)
844 /* Clear all the static flags */
845 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
846 hsd->ErrorCode |= errorstate;
847 hsd->State = HAL_SD_STATE_READY;
848 return HAL_ERROR;
853 /* Get error state */
854 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
856 /* Clear all the static flags */
857 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
858 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
859 hsd->State = HAL_SD_STATE_READY;
860 return HAL_ERROR;
862 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
864 /* Clear all the static flags */
865 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
866 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
867 hsd->State = HAL_SD_STATE_READY;
868 return HAL_ERROR;
870 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
872 /* Clear all the static flags */
873 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
874 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
875 hsd->State = HAL_SD_STATE_READY;
876 return HAL_ERROR;
879 /* Clear all the static flags */
880 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
882 hsd->State = HAL_SD_STATE_READY;
884 return HAL_OK;
886 else
888 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
889 return HAL_ERROR;
894 * @brief Reads block(s) from a specified address in a card. The Data transfer
895 * is managed in interrupt mode.
896 * @note This API should be followed by a check on the card state through
897 * HAL_SD_GetCardState().
898 * @note You could also check the IT transfer process through the SD Rx
899 * interrupt event.
900 * @param hsd Pointer to SD handle
901 * @param pData Pointer to the buffer that will contain the received data
902 * @param BlockAdd Block Address from where data is to be read
903 * @param NumberOfBlocks Number of blocks to read.
904 * @retval HAL status
906 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
908 SDMMC_DataInitTypeDef config;
909 uint32_t errorstate = HAL_SD_ERROR_NONE;
911 if(NULL == pData)
913 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
914 return HAL_ERROR;
917 if(hsd->State == HAL_SD_STATE_READY)
919 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
921 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
923 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
924 return HAL_ERROR;
927 hsd->State = HAL_SD_STATE_BUSY;
929 /* Initialize data control register */
930 hsd->Instance->DCTRL = 0U;
932 hsd->pRxBuffPtr = (uint32_t *)pData;
933 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
935 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND | SDMMC_FLAG_RXFIFOHF));
937 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
939 BlockAdd *= 512U;
942 /* Configure the SD DPSM (Data Path State Machine) */
943 config.DataTimeOut = SDMMC_DATATIMEOUT;
944 config.DataLength = BLOCKSIZE * NumberOfBlocks;
945 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
946 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
947 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
948 config.DPSM = SDMMC_DPSM_ENABLE;
949 SDMMC_ConfigData(hsd->Instance, &config);
951 /* Set Block Size for Card */
952 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
953 if(errorstate != HAL_SD_ERROR_NONE)
955 /* Clear all the static flags */
956 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
957 hsd->ErrorCode |= errorstate;
958 hsd->State = HAL_SD_STATE_READY;
959 return HAL_ERROR;
962 /* Read Blocks in IT mode */
963 if(NumberOfBlocks > 1U)
965 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
967 /* Read Multi Block command */
968 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
970 else
972 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
974 /* Read Single Block command */
975 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
977 if(errorstate != HAL_SD_ERROR_NONE)
979 /* Clear all the static flags */
980 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
981 hsd->ErrorCode |= errorstate;
982 hsd->State = HAL_SD_STATE_READY;
983 return HAL_ERROR;
986 return HAL_OK;
988 else
990 return HAL_BUSY;
995 * @brief Writes block(s) to a specified address in a card. The Data transfer
996 * is managed in interrupt mode.
997 * @note This API should be followed by a check on the card state through
998 * HAL_SD_GetCardState().
999 * @note You could also check the IT transfer process through the SD Tx
1000 * interrupt event.
1001 * @param hsd Pointer to SD handle
1002 * @param pData Pointer to the buffer that will contain the data to transmit
1003 * @param BlockAdd Block Address where data will be written
1004 * @param NumberOfBlocks Number of blocks to write
1005 * @retval HAL status
1007 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1009 SDMMC_DataInitTypeDef config;
1010 uint32_t errorstate = HAL_SD_ERROR_NONE;
1012 if(NULL == pData)
1014 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1015 return HAL_ERROR;
1018 if(hsd->State == HAL_SD_STATE_READY)
1020 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
1022 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1024 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1025 return HAL_ERROR;
1028 hsd->State = HAL_SD_STATE_BUSY;
1030 /* Initialize data control register */
1031 hsd->Instance->DCTRL = 0U;
1033 hsd->pTxBuffPtr = (uint32_t *)pData;
1034 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1036 /* Enable transfer interrupts */
1037 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND | SDMMC_FLAG_TXFIFOHE));
1039 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1041 BlockAdd *= 512U;
1044 /* Set Block Size for Card */
1045 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1046 if(errorstate != HAL_SD_ERROR_NONE)
1048 /* Clear all the static flags */
1049 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1050 hsd->ErrorCode |= errorstate;
1051 hsd->State = HAL_SD_STATE_READY;
1052 return HAL_ERROR;
1055 /* Write Blocks in Polling mode */
1056 if(NumberOfBlocks > 1U)
1058 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1060 /* Write Multi Block command */
1061 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
1063 else
1065 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1067 /* Write Single Block command */
1068 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
1070 if(errorstate != HAL_SD_ERROR_NONE)
1072 /* Clear all the static flags */
1073 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1074 hsd->ErrorCode |= errorstate;
1075 hsd->State = HAL_SD_STATE_READY;
1076 return HAL_ERROR;
1079 /* Configure the SD DPSM (Data Path State Machine) */
1080 config.DataTimeOut = SDMMC_DATATIMEOUT;
1081 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1082 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1083 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1084 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1085 config.DPSM = SDMMC_DPSM_ENABLE;
1086 SDMMC_ConfigData(hsd->Instance, &config);
1088 return HAL_OK;
1090 else
1092 return HAL_BUSY;
1097 * @brief Reads block(s) from a specified address in a card. The Data transfer
1098 * is managed by DMA mode.
1099 * @note This API should be followed by a check on the card state through
1100 * HAL_SD_GetCardState().
1101 * @note You could also check the DMA transfer process through the SD Rx
1102 * interrupt event.
1103 * @param hsd Pointer SD handle
1104 * @param pData Pointer to the buffer that will contain the received data
1105 * @param BlockAdd Block Address from where data is to be read
1106 * @param NumberOfBlocks Number of blocks to read.
1107 * @retval HAL status
1109 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1111 SDMMC_DataInitTypeDef config;
1112 uint32_t errorstate = HAL_SD_ERROR_NONE;
1114 if(NULL == pData)
1116 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1117 return HAL_ERROR;
1120 if(hsd->State == HAL_SD_STATE_READY)
1122 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
1124 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1126 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1127 return HAL_ERROR;
1130 hsd->State = HAL_SD_STATE_BUSY;
1132 /* Initialize data control register */
1133 hsd->Instance->DCTRL = 0U;
1135 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1137 /* Set the DMA transfer complete callback */
1138 hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
1140 /* Set the DMA error callback */
1141 hsd->hdmarx->XferErrorCallback = SD_DMAError;
1143 /* Set the DMA Abort callback */
1144 hsd->hdmarx->XferAbortCallback = NULL;
1146 /* Enable the DMA Channel */
1147 HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
1149 /* Enable SD DMA transfer */
1150 __HAL_SD_DMA_ENABLE(hsd);
1152 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1154 BlockAdd *= 512U;
1157 /* Configure the SD DPSM (Data Path State Machine) */
1158 config.DataTimeOut = SDMMC_DATATIMEOUT;
1159 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1160 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1161 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1162 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1163 config.DPSM = SDMMC_DPSM_ENABLE;
1164 SDMMC_ConfigData(hsd->Instance, &config);
1166 /* Set Block Size for Card */
1167 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1168 if(errorstate != HAL_SD_ERROR_NONE)
1170 /* Clear all the static flags */
1171 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1172 hsd->ErrorCode |= errorstate;
1173 hsd->State = HAL_SD_STATE_READY;
1174 return HAL_ERROR;
1177 /* Read Blocks in DMA mode */
1178 if(NumberOfBlocks > 1U)
1180 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1182 /* Read Multi Block command */
1183 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
1185 else
1187 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1189 /* Read Single Block command */
1190 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
1192 if(errorstate != HAL_SD_ERROR_NONE)
1194 /* Clear all the static flags */
1195 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1196 hsd->ErrorCode |= errorstate;
1197 hsd->State = HAL_SD_STATE_READY;
1198 return HAL_ERROR;
1201 return HAL_OK;
1203 else
1205 return HAL_BUSY;
1210 * @brief Writes block(s) to a specified address in a card. The Data transfer
1211 * is managed by DMA mode.
1212 * @note This API should be followed by a check on the card state through
1213 * HAL_SD_GetCardState().
1214 * @note You could also check the DMA transfer process through the SD Tx
1215 * interrupt event.
1216 * @param hsd Pointer to SD handle
1217 * @param pData Pointer to the buffer that will contain the data to transmit
1218 * @param BlockAdd Block Address where data will be written
1219 * @param NumberOfBlocks Number of blocks to write
1220 * @retval HAL status
1222 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1224 SDMMC_DataInitTypeDef config;
1225 uint32_t errorstate = HAL_SD_ERROR_NONE;
1227 if(NULL == pData)
1229 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1230 return HAL_ERROR;
1233 if(hsd->State == HAL_SD_STATE_READY)
1235 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
1237 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1239 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1240 return HAL_ERROR;
1243 hsd->State = HAL_SD_STATE_BUSY;
1245 /* Initialize data control register */
1246 hsd->Instance->DCTRL = 0U;
1248 /* Enable SD Error interrupts */
1249 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR));
1251 /* Set the DMA transfer complete callback */
1252 hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
1254 /* Set the DMA error callback */
1255 hsd->hdmatx->XferErrorCallback = SD_DMAError;
1257 /* Set the DMA Abort callback */
1258 hsd->hdmatx->XferAbortCallback = NULL;
1260 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1262 BlockAdd *= 512U;
1265 /* Set Block Size for Card */
1266 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1267 if(errorstate != HAL_SD_ERROR_NONE)
1269 /* Clear all the static flags */
1270 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1271 hsd->ErrorCode |= errorstate;
1272 hsd->State = HAL_SD_STATE_READY;
1273 return HAL_ERROR;
1276 /* Write Blocks in Polling mode */
1277 if(NumberOfBlocks > 1U)
1279 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1281 /* Write Multi Block command */
1282 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
1284 else
1286 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1288 /* Write Single Block command */
1289 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
1291 if(errorstate != HAL_SD_ERROR_NONE)
1293 /* Clear all the static flags */
1294 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1295 hsd->ErrorCode |= errorstate;
1296 hsd->State = HAL_SD_STATE_READY;
1297 return HAL_ERROR;
1300 /* Enable SDMMC DMA transfer */
1301 __HAL_SD_DMA_ENABLE(hsd);
1303 /* Enable the DMA Channel */
1304 HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
1306 /* Configure the SD DPSM (Data Path State Machine) */
1307 config.DataTimeOut = SDMMC_DATATIMEOUT;
1308 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1309 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1310 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1311 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1312 config.DPSM = SDMMC_DPSM_ENABLE;
1313 SDMMC_ConfigData(hsd->Instance, &config);
1315 return HAL_OK;
1317 else
1319 return HAL_BUSY;
1324 * @brief Erases the specified memory area of the given SD card.
1325 * @note This API should be followed by a check on the card state through
1326 * HAL_SD_GetCardState().
1327 * @param hsd Pointer to SD handle
1328 * @param BlockStartAdd Start Block address
1329 * @param BlockEndAdd End Block address
1330 * @retval HAL status
1332 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1334 uint32_t errorstate = HAL_SD_ERROR_NONE;
1336 if(hsd->State == HAL_SD_STATE_READY)
1338 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
1340 if(BlockEndAdd < BlockStartAdd)
1342 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1343 return HAL_ERROR;
1346 if(BlockEndAdd > (hsd->SdCard.LogBlockNbr))
1348 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1349 return HAL_ERROR;
1352 hsd->State = HAL_SD_STATE_BUSY;
1354 /* Check if the card command class supports erase command */
1355 if(((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U)
1357 /* Clear all the static flags */
1358 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1359 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1360 hsd->State = HAL_SD_STATE_READY;
1361 return HAL_ERROR;
1364 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1366 /* Clear all the static flags */
1367 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1368 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1369 hsd->State = HAL_SD_STATE_READY;
1370 return HAL_ERROR;
1373 /* Get start and end block for high capacity cards */
1374 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1376 BlockStartAdd *= 512U;
1377 BlockEndAdd *= 512U;
1380 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1381 if(hsd->SdCard.CardType != CARD_SECURED)
1383 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1384 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, BlockStartAdd);
1385 if(errorstate != HAL_SD_ERROR_NONE)
1387 /* Clear all the static flags */
1388 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1389 hsd->ErrorCode |= errorstate;
1390 hsd->State = HAL_SD_STATE_READY;
1391 return HAL_ERROR;
1394 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1395 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, BlockEndAdd);
1396 if(errorstate != HAL_SD_ERROR_NONE)
1398 /* Clear all the static flags */
1399 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1400 hsd->ErrorCode |= errorstate;
1401 hsd->State = HAL_SD_STATE_READY;
1402 return HAL_ERROR;
1406 /* Send CMD38 ERASE */
1407 errorstate = SDMMC_CmdErase(hsd->Instance);
1408 if(errorstate != HAL_SD_ERROR_NONE)
1410 /* Clear all the static flags */
1411 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1412 hsd->ErrorCode |= errorstate;
1413 hsd->State = HAL_SD_STATE_READY;
1414 return HAL_ERROR;
1417 hsd->State = HAL_SD_STATE_READY;
1419 return HAL_OK;
1421 else
1423 return HAL_BUSY;
1428 * @brief This function handles SD card interrupt request.
1429 * @param hsd Pointer to SD handle
1430 * @retval None
1432 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1434 uint32_t errorstate = HAL_SD_ERROR_NONE;
1436 /* Check for SDMMC interrupt flags */
1437 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DATAEND) != RESET)
1439 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND);
1441 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
1442 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
1444 if((hsd->Context & SD_CONTEXT_IT) != RESET)
1446 if(((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET))
1448 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1449 if(errorstate != HAL_SD_ERROR_NONE)
1451 hsd->ErrorCode |= errorstate;
1452 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
1453 hsd->ErrorCallback(hsd);
1454 #else
1455 HAL_SD_ErrorCallback(hsd);
1456 #endif
1460 /* Clear all the static flags */
1461 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1463 hsd->State = HAL_SD_STATE_READY;
1464 if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET))
1466 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
1467 hsd->RxCpltCallback(hsd);
1468 #else
1469 HAL_SD_RxCpltCallback(hsd);
1470 #endif
1472 else
1474 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
1475 hsd->TxCpltCallback(hsd);
1476 #else
1477 HAL_SD_TxCpltCallback(hsd);
1478 #endif
1481 else if((hsd->Context & SD_CONTEXT_DMA) != RESET)
1483 if((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET)
1485 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1486 if(errorstate != HAL_SD_ERROR_NONE)
1488 hsd->ErrorCode |= errorstate;
1489 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
1490 hsd->ErrorCallback(hsd);
1491 #else
1492 HAL_SD_ErrorCallback(hsd);
1493 #endif
1496 if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) == RESET) && ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == RESET))
1498 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1499 in the SD DCTRL register */
1500 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
1502 hsd->State = HAL_SD_STATE_READY;
1504 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
1505 hsd->TxCpltCallback(hsd);
1506 #else
1507 HAL_SD_TxCpltCallback(hsd);
1508 #endif
1513 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXFIFOHE) != RESET)
1515 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_TXFIFOHE);
1517 SD_Write_IT(hsd);
1520 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXFIFOHF) != RESET)
1522 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXFIFOHF);
1524 SD_Read_IT(hsd);
1527 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_TXUNDERR) != RESET)
1529 /* Set Error code */
1530 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL) != RESET)
1532 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1534 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT) != RESET)
1536 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1538 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXOVERR) != RESET)
1540 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1542 if(__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXUNDERR) != RESET)
1544 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1547 /* Clear All flags */
1548 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1550 /* Disable all interrupts */
1551 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
1552 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
1554 if((hsd->Context & SD_CONTEXT_DMA) != RESET)
1556 /* Abort the SD DMA Streams */
1557 if(hsd->hdmatx != NULL)
1559 /* Set the DMA Tx abort callback */
1560 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
1561 /* Abort DMA in IT mode */
1562 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
1564 SD_DMATxAbort(hsd->hdmatx);
1567 else if(hsd->hdmarx != NULL)
1569 /* Set the DMA Rx abort callback */
1570 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
1571 /* Abort DMA in IT mode */
1572 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
1574 SD_DMARxAbort(hsd->hdmarx);
1577 else
1579 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1580 hsd->State = HAL_SD_STATE_READY;
1581 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
1582 hsd->AbortCpltCallback(hsd);
1583 #else
1584 HAL_SD_AbortCallback(hsd);
1585 #endif
1588 else if((hsd->Context & SD_CONTEXT_IT) != RESET)
1590 /* Set the SD state to ready to be able to start again the process */
1591 hsd->State = HAL_SD_STATE_READY;
1592 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
1593 hsd->ErrorCallback(hsd);
1594 #else
1595 HAL_SD_ErrorCallback(hsd);
1596 #endif
1602 * @brief return the SD state
1603 * @param hsd Pointer to sd handle
1604 * @retval HAL state
1606 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
1608 return hsd->State;
1612 * @brief Return the SD error code
1613 * @param hsd Pointer to a SD_HandleTypeDef structure that contains
1614 * the configuration information.
1615 * @retval SD Error Code
1617 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
1619 return hsd->ErrorCode;
1623 * @brief Tx Transfer completed callbacks
1624 * @param hsd Pointer to SD handle
1625 * @retval None
1627 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
1629 /* Prevent unused argument(s) compilation warning */
1630 UNUSED(hsd);
1632 /* NOTE : This function should not be modified, when the callback is needed,
1633 the HAL_SD_TxCpltCallback can be implemented in the user file
1638 * @brief Rx Transfer completed callbacks
1639 * @param hsd Pointer SD handle
1640 * @retval None
1642 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
1644 /* Prevent unused argument(s) compilation warning */
1645 UNUSED(hsd);
1647 /* NOTE : This function should not be modified, when the callback is needed,
1648 the HAL_SD_RxCpltCallback can be implemented in the user file
1653 * @brief SD error callbacks
1654 * @param hsd Pointer SD handle
1655 * @retval None
1657 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1659 /* Prevent unused argument(s) compilation warning */
1660 UNUSED(hsd);
1662 /* NOTE : This function should not be modified, when the callback is needed,
1663 the HAL_SD_ErrorCallback can be implemented in the user file
1668 * @brief SD Abort callbacks
1669 * @param hsd Pointer SD handle
1670 * @retval None
1672 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
1674 /* Prevent unused argument(s) compilation warning */
1675 UNUSED(hsd);
1677 /* NOTE : This function should not be modified, when the callback is needed,
1678 the HAL_SD_ErrorCallback can be implemented in the user file
1682 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
1684 * @brief Register a User SD Callback
1685 * To be used instead of the weak (surcharged) predefined callback
1686 * @param hsd : SD handle
1687 * @param CallbackId : Id of the callback to be registered
1688 * This parameter can be one of the following values:
1689 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1690 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1691 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1692 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1693 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1694 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1695 * @param pCallback : pointer to the Callback function
1696 * @retval status
1698 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackId, pSD_CallbackTypeDef pCallback)
1700 HAL_StatusTypeDef status = HAL_OK;
1702 if(pCallback == NULL)
1704 /* Update the error code */
1705 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1706 return HAL_ERROR;
1709 /* Process locked */
1710 __HAL_LOCK(hsd);
1712 if(hsd->State == HAL_SD_STATE_READY)
1714 switch (CallbackId)
1716 case HAL_SD_TX_CPLT_CB_ID :
1717 hsd->TxCpltCallback = pCallback;
1718 break;
1719 case HAL_SD_RX_CPLT_CB_ID :
1720 hsd->RxCpltCallback = pCallback;
1721 break;
1722 case HAL_SD_ERROR_CB_ID :
1723 hsd->ErrorCallback = pCallback;
1724 break;
1725 case HAL_SD_ABORT_CB_ID :
1726 hsd->AbortCpltCallback = pCallback;
1727 break;
1728 case HAL_SD_MSP_INIT_CB_ID :
1729 hsd->MspInitCallback = pCallback;
1730 break;
1731 case HAL_SD_MSP_DEINIT_CB_ID :
1732 hsd->MspDeInitCallback = pCallback;
1733 break;
1734 default :
1735 /* Update the error code */
1736 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1737 /* update return status */
1738 status = HAL_ERROR;
1739 break;
1742 else if (hsd->State == HAL_SD_STATE_RESET)
1744 switch (CallbackId)
1746 case HAL_SD_MSP_INIT_CB_ID :
1747 hsd->MspInitCallback = pCallback;
1748 break;
1749 case HAL_SD_MSP_DEINIT_CB_ID :
1750 hsd->MspDeInitCallback = pCallback;
1751 break;
1752 default :
1753 /* Update the error code */
1754 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1755 /* update return status */
1756 status = HAL_ERROR;
1757 break;
1760 else
1762 /* Update the error code */
1763 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1764 /* update return status */
1765 status = HAL_ERROR;
1768 /* Release Lock */
1769 __HAL_UNLOCK(hsd);
1770 return status;
1774 * @brief Unregister a User SD Callback
1775 * SD Callback is redirected to the weak (surcharged) predefined callback
1776 * @param hsd : SD handle
1777 * @param CallbackId : Id of the callback to be unregistered
1778 * This parameter can be one of the following values:
1779 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1780 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1781 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1782 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1783 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1784 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1785 * @retval status
1787 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackId)
1789 HAL_StatusTypeDef status = HAL_OK;
1791 /* Process locked */
1792 __HAL_LOCK(hsd);
1794 if(hsd->State == HAL_SD_STATE_READY)
1796 switch (CallbackId)
1798 case HAL_SD_TX_CPLT_CB_ID :
1799 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
1800 break;
1801 case HAL_SD_RX_CPLT_CB_ID :
1802 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
1803 break;
1804 case HAL_SD_ERROR_CB_ID :
1805 hsd->ErrorCallback = HAL_SD_ErrorCallback;
1806 break;
1807 case HAL_SD_ABORT_CB_ID :
1808 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
1809 break;
1810 case HAL_SD_MSP_INIT_CB_ID :
1811 hsd->MspInitCallback = HAL_SD_MspInit;
1812 break;
1813 case HAL_SD_MSP_DEINIT_CB_ID :
1814 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1815 break;
1816 default :
1817 /* Update the error code */
1818 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1819 /* update return status */
1820 status = HAL_ERROR;
1821 break;
1824 else if (hsd->State == HAL_SD_STATE_RESET)
1826 switch (CallbackId)
1828 case HAL_SD_MSP_INIT_CB_ID :
1829 hsd->MspInitCallback = HAL_SD_MspInit;
1830 break;
1831 case HAL_SD_MSP_DEINIT_CB_ID :
1832 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1833 break;
1834 default :
1835 /* Update the error code */
1836 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1837 /* update return status */
1838 status = HAL_ERROR;
1839 break;
1842 else
1844 /* Update the error code */
1845 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1846 /* update return status */
1847 status = HAL_ERROR;
1850 /* Release Lock */
1851 __HAL_UNLOCK(hsd);
1852 return status;
1854 #endif
1857 * @}
1860 /** @addtogroup SD_Exported_Functions_Group3
1861 * @brief management functions
1863 @verbatim
1864 ==============================================================================
1865 ##### Peripheral Control functions #####
1866 ==============================================================================
1867 [..]
1868 This subsection provides a set of functions allowing to control the SD card
1869 operations and get the related information
1871 @endverbatim
1872 * @{
1876 * @brief Returns information the information of the card which are stored on
1877 * the CID register.
1878 * @param hsd Pointer to SD handle
1879 * @param pCID Pointer to a HAL_SD_CardCIDTypeDef structure that
1880 * contains all CID register parameters
1881 * @retval HAL status
1883 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
1885 uint32_t tmp = 0;
1887 /* Byte 0 */
1888 tmp = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24);
1889 pCID->ManufacturerID = tmp;
1891 /* Byte 1 */
1892 tmp = (uint8_t)((hsd->CID[0] & 0x00FF0000) >> 16);
1893 pCID->OEM_AppliID = tmp << 8;
1895 /* Byte 2 */
1896 tmp = (uint8_t)((hsd->CID[0] & 0x000000FF00) >> 8);
1897 pCID->OEM_AppliID |= tmp;
1899 /* Byte 3 */
1900 tmp = (uint8_t)(hsd->CID[0] & 0x000000FF);
1901 pCID->ProdName1 = tmp << 24;
1903 /* Byte 4 */
1904 tmp = (uint8_t)((hsd->CID[1] & 0xFF000000U) >> 24);
1905 pCID->ProdName1 |= tmp << 16;
1907 /* Byte 5 */
1908 tmp = (uint8_t)((hsd->CID[1] & 0x00FF0000) >> 16);
1909 pCID->ProdName1 |= tmp << 8;
1911 /* Byte 6 */
1912 tmp = (uint8_t)((hsd->CID[1] & 0x0000FF00) >> 8);
1913 pCID->ProdName1 |= tmp;
1915 /* Byte 7 */
1916 tmp = (uint8_t)(hsd->CID[1] & 0x000000FF);
1917 pCID->ProdName2 = tmp;
1919 /* Byte 8 */
1920 tmp = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24);
1921 pCID->ProdRev = tmp;
1923 /* Byte 9 */
1924 tmp = (uint8_t)((hsd->CID[2] & 0x00FF0000) >> 16);
1925 pCID->ProdSN = tmp << 24;
1927 /* Byte 10 */
1928 tmp = (uint8_t)((hsd->CID[2] & 0x0000FF00) >> 8);
1929 pCID->ProdSN |= tmp << 16;
1931 /* Byte 11 */
1932 tmp = (uint8_t)(hsd->CID[2] & 0x000000FF);
1933 pCID->ProdSN |= tmp << 8;
1935 /* Byte 12 */
1936 tmp = (uint8_t)((hsd->CID[3] & 0xFF000000U) >> 24);
1937 pCID->ProdSN |= tmp;
1939 /* Byte 13 */
1940 tmp = (uint8_t)((hsd->CID[3] & 0x00FF0000) >> 16);
1941 pCID->Reserved1 |= (tmp & 0xF0) >> 4;
1942 pCID->ManufactDate = (tmp & 0x0F) << 8;
1944 /* Byte 14 */
1945 tmp = (uint8_t)((hsd->CID[3] & 0x0000FF00) >> 8);
1946 pCID->ManufactDate |= tmp;
1948 /* Byte 15 */
1949 tmp = (uint8_t)(hsd->CID[3] & 0x000000FF);
1950 pCID->CID_CRC = (tmp & 0xFE) >> 1;
1951 pCID->Reserved2 = 1;
1953 return HAL_OK;
1957 * @brief Returns information the information of the card which are stored on
1958 * the CSD register.
1959 * @param hsd Pointer to SD handle
1960 * @param pCSD Pointer to a HAL_SD_CardCSDTypeDef structure that
1961 * contains all CSD register parameters
1962 * @retval HAL status
1964 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
1966 uint32_t tmp = 0;
1968 /* Byte 0 */
1969 tmp = (hsd->CSD[0] & 0xFF000000U) >> 24;
1970 pCSD->CSDStruct = (uint8_t)((tmp & 0xC0) >> 6);
1971 pCSD->SysSpecVersion = (uint8_t)((tmp & 0x3C) >> 2);
1972 pCSD->Reserved1 = tmp & 0x03;
1974 /* Byte 1 */
1975 tmp = (hsd->CSD[0] & 0x00FF0000) >> 16;
1976 pCSD->TAAC = (uint8_t)tmp;
1978 /* Byte 2 */
1979 tmp = (hsd->CSD[0] & 0x0000FF00) >> 8;
1980 pCSD->NSAC = (uint8_t)tmp;
1982 /* Byte 3 */
1983 tmp = hsd->CSD[0] & 0x000000FF;
1984 pCSD->MaxBusClkFrec = (uint8_t)tmp;
1986 /* Byte 4 */
1987 tmp = (hsd->CSD[1] & 0xFF000000U) >> 24;
1988 pCSD->CardComdClasses = (uint16_t)(tmp << 4);
1990 /* Byte 5 */
1991 tmp = (hsd->CSD[1] & 0x00FF0000U) >> 16;
1992 pCSD->CardComdClasses |= (uint16_t)((tmp & 0xF0) >> 4);
1993 pCSD->RdBlockLen = (uint8_t)(tmp & 0x0F);
1995 /* Byte 6 */
1996 tmp = (hsd->CSD[1] & 0x0000FF00U) >> 8;
1997 pCSD->PartBlockRead = (uint8_t)((tmp & 0x80) >> 7);
1998 pCSD->WrBlockMisalign = (uint8_t)((tmp & 0x40) >> 6);
1999 pCSD->RdBlockMisalign = (uint8_t)((tmp & 0x20) >> 5);
2000 pCSD->DSRImpl = (uint8_t)((tmp & 0x10) >> 4);
2001 pCSD->Reserved2 = 0; /*!< Reserved */
2003 if(hsd->SdCard.CardType == CARD_SDSC)
2005 pCSD->DeviceSize = (tmp & 0x03) << 10;
2007 /* Byte 7 */
2008 tmp = (uint8_t)(hsd->CSD[1] & 0x000000FFU);
2009 pCSD->DeviceSize |= (tmp) << 2;
2011 /* Byte 8 */
2012 tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000U) >> 24);
2013 pCSD->DeviceSize |= (tmp & 0xC0) >> 6;
2015 pCSD->MaxRdCurrentVDDMin = (tmp & 0x38) >> 3;
2016 pCSD->MaxRdCurrentVDDMax = (tmp & 0x07);
2018 /* Byte 9 */
2019 tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000U) >> 16);
2020 pCSD->MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5;
2021 pCSD->MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2;
2022 pCSD->DeviceSizeMul = (tmp & 0x03) << 1;
2023 /* Byte 10 */
2024 tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00U) >> 8);
2025 pCSD->DeviceSizeMul |= (tmp & 0x80) >> 7;
2027 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1) ;
2028 hsd->SdCard.BlockNbr *= (1 << (pCSD->DeviceSizeMul + 2));
2029 hsd->SdCard.BlockSize = 1 << (pCSD->RdBlockLen);
2031 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512);
2032 hsd->SdCard.LogBlockSize = 512;
2034 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
2036 /* Byte 7 */
2037 tmp = (uint8_t)(hsd->CSD[1] & 0x000000FFU);
2038 pCSD->DeviceSize = (tmp & 0x3F) << 16;
2040 /* Byte 8 */
2041 tmp = (uint8_t)((hsd->CSD[2] & 0xFF000000U) >> 24);
2043 pCSD->DeviceSize |= (tmp << 8);
2045 /* Byte 9 */
2046 tmp = (uint8_t)((hsd->CSD[2] & 0x00FF0000U) >> 16);
2048 pCSD->DeviceSize |= (tmp);
2050 /* Byte 10 */
2051 tmp = (uint8_t)((hsd->CSD[2] & 0x0000FF00U) >> 8);
2053 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr = (((uint64_t)pCSD->DeviceSize + 1) * 1024);
2054 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize = 512;
2056 else
2058 /* Clear all the static flags */
2059 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2060 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2061 hsd->State = HAL_SD_STATE_READY;
2062 return HAL_ERROR;
2065 pCSD->EraseGrSize = (tmp & 0x40) >> 6;
2066 pCSD->EraseGrMul = (tmp & 0x3F) << 1;
2068 /* Byte 11 */
2069 tmp = (uint8_t)(hsd->CSD[2] & 0x000000FF);
2070 pCSD->EraseGrMul |= (tmp & 0x80) >> 7;
2071 pCSD->WrProtectGrSize = (tmp & 0x7F);
2073 /* Byte 12 */
2074 tmp = (uint8_t)((hsd->CSD[3] & 0xFF000000U) >> 24);
2075 pCSD->WrProtectGrEnable = (tmp & 0x80) >> 7;
2076 pCSD->ManDeflECC = (tmp & 0x60) >> 5;
2077 pCSD->WrSpeedFact = (tmp & 0x1C) >> 2;
2078 pCSD->MaxWrBlockLen = (tmp & 0x03) << 2;
2080 /* Byte 13 */
2081 tmp = (uint8_t)((hsd->CSD[3] & 0x00FF0000) >> 16);
2082 pCSD->MaxWrBlockLen |= (tmp & 0xC0) >> 6;
2083 pCSD->WriteBlockPaPartial = (tmp & 0x20) >> 5;
2084 pCSD->Reserved3 = 0;
2085 pCSD->ContentProtectAppli = (tmp & 0x01);
2087 /* Byte 14 */
2088 tmp = (uint8_t)((hsd->CSD[3] & 0x0000FF00) >> 8);
2089 pCSD->FileFormatGrouop = (tmp & 0x80) >> 7;
2090 pCSD->CopyFlag = (tmp & 0x40) >> 6;
2091 pCSD->PermWrProtect = (tmp & 0x20) >> 5;
2092 pCSD->TempWrProtect = (tmp & 0x10) >> 4;
2093 pCSD->FileFormat = (tmp & 0x0C) >> 2;
2094 pCSD->ECC = (tmp & 0x03);
2096 /* Byte 15 */
2097 tmp = (uint8_t)(hsd->CSD[3] & 0x000000FF);
2098 pCSD->CSD_CRC = (tmp & 0xFE) >> 1;
2099 pCSD->Reserved4 = 1;
2101 return HAL_OK;
2105 * @brief Gets the SD status info.
2106 * @param hsd Pointer to SD handle
2107 * @param pStatus Pointer to the HAL_SD_CardStatusTypeDef structure that
2108 * will contain the SD card status information
2109 * @retval HAL status
2111 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
2113 uint32_t tmp = 0;
2114 uint32_t sd_status[16];
2115 uint32_t errorstate = HAL_SD_ERROR_NONE;
2117 errorstate = SD_SendSDStatus(hsd, sd_status);
2118 if(errorstate != HAL_OK)
2120 /* Clear all the static flags */
2121 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2122 hsd->ErrorCode |= errorstate;
2123 hsd->State = HAL_SD_STATE_READY;
2124 return HAL_ERROR;
2126 else
2128 /* Byte 0 */
2129 tmp = (sd_status[0] & 0xC0) >> 6;
2130 pStatus->DataBusWidth = (uint8_t)tmp;
2132 /* Byte 0 */
2133 tmp = (sd_status[0] & 0x20) >> 5;
2134 pStatus->SecuredMode = (uint8_t)tmp;
2136 /* Byte 2 */
2137 tmp = (sd_status[0] & 0x00FF0000U) >> 16;
2138 pStatus->CardType = (uint16_t)(tmp << 8);
2140 /* Byte 3 */
2141 tmp = (sd_status[0] & 0xFF000000U) >> 24;
2142 pStatus->CardType |= (uint16_t)tmp;
2144 /* Byte 4 */
2145 tmp = (sd_status[1] & 0xFF);
2146 pStatus->ProtectedAreaSize = (uint32_t)(tmp << 24);
2148 /* Byte 5 */
2149 tmp = (sd_status[1] & 0xFF00) >> 8;
2150 pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 16);
2152 /* Byte 6 */
2153 tmp = (sd_status[1] & 0xFF0000) >> 16;
2154 pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 8);
2156 /* Byte 7 */
2157 tmp = (sd_status[1] & 0xFF000000U) >> 24;
2158 pStatus->ProtectedAreaSize |= (uint32_t)tmp;
2160 /* Byte 8 */
2161 tmp = (sd_status[2] & 0xFF);
2162 pStatus->SpeedClass = (uint8_t)tmp;
2164 /* Byte 9 */
2165 tmp = (sd_status[2] & 0xFF00) >> 8;
2166 pStatus->PerformanceMove = (uint8_t)tmp;
2168 /* Byte 10 */
2169 tmp = (sd_status[2] & 0xF00000) >> 20;
2170 pStatus->AllocationUnitSize = (uint8_t)tmp;
2172 /* Byte 11 */
2173 tmp = (sd_status[2] & 0xFF000000U) >> 24;
2174 pStatus->EraseSize = (uint16_t)(tmp << 8);
2176 /* Byte 12 */
2177 tmp = (sd_status[3] & 0xFF);
2178 pStatus->EraseSize |= (uint16_t)tmp;
2180 /* Byte 13 */
2181 tmp = (sd_status[3] & 0xFC00) >> 10;
2182 pStatus->EraseTimeout = (uint8_t)tmp;
2184 /* Byte 13 */
2185 tmp = (sd_status[3] & 0x0300) >> 8;
2186 pStatus->EraseOffset = (uint8_t)tmp;
2189 return HAL_OK;
2193 * @brief Gets the SD card info.
2194 * @param hsd Pointer to SD handle
2195 * @param pCardInfo Pointer to the HAL_SD_CardInfoTypeDef structure that
2196 * will contain the SD card status information
2197 * @retval HAL status
2199 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
2201 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
2202 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
2203 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
2204 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
2205 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
2206 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
2207 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
2208 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
2210 return HAL_OK;
2214 * @brief Enables wide bus operation for the requested card if supported by
2215 * card.
2216 * @param hsd Pointer to SD handle
2217 * @param WideMode Specifies the SD card wide bus mode
2218 * This parameter can be one of the following values:
2219 * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer
2220 * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
2221 * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
2222 * @retval HAL status
2224 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
2226 SDMMC_InitTypeDef Init;
2227 uint32_t errorstate = HAL_SD_ERROR_NONE;
2229 /* Check the parameters */
2230 assert_param(IS_SDMMC_BUS_WIDE(WideMode));
2232 /* Chnage Satte */
2233 hsd->State = HAL_SD_STATE_BUSY;
2235 if(hsd->SdCard.CardType != CARD_SECURED)
2237 if(WideMode == SDMMC_BUS_WIDE_8B)
2239 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2241 else if(WideMode == SDMMC_BUS_WIDE_4B)
2243 errorstate = SD_WideBus_Enable(hsd);
2245 hsd->ErrorCode |= errorstate;
2247 else if(WideMode == SDMMC_BUS_WIDE_1B)
2249 errorstate = SD_WideBus_Disable(hsd);
2251 hsd->ErrorCode |= errorstate;
2253 else
2255 /* WideMode is not a valid argument*/
2256 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2259 else
2261 /* MMC Card does not support this feature */
2262 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2265 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2267 /* Clear all the static flags */
2268 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2269 hsd->State = HAL_SD_STATE_READY;
2270 return HAL_ERROR;
2272 else
2274 /* Configure the SDMMC peripheral */
2275 Init.ClockEdge = hsd->Init.ClockEdge;
2276 Init.ClockBypass = hsd->Init.ClockBypass;
2277 Init.ClockPowerSave = hsd->Init.ClockPowerSave;
2278 Init.BusWide = WideMode;
2279 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2280 Init.ClockDiv = hsd->Init.ClockDiv;
2281 SDMMC_Init(hsd->Instance, Init);
2284 /* Change State */
2285 hsd->State = HAL_SD_STATE_READY;
2287 return HAL_OK;
2292 * @brief Gets the current sd card data state.
2293 * @param hsd pointer to SD handle
2294 * @retval Card state
2296 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2298 HAL_SD_CardStateTypeDef cardstate = HAL_SD_CARD_TRANSFER;
2299 uint32_t errorstate = HAL_SD_ERROR_NONE;
2300 uint32_t resp1 = 0;
2302 errorstate = SD_SendStatus(hsd, &resp1);
2303 if(errorstate != HAL_OK)
2305 hsd->ErrorCode |= errorstate;
2308 cardstate = (HAL_SD_CardStateTypeDef)((resp1 >> 9) & 0x0F);
2310 return cardstate;
2314 * @brief Abort the current transfer and disable the SD.
2315 * @param hsd pointer to a SD_HandleTypeDef structure that contains
2316 * the configuration information for SD module.
2317 * @retval HAL status
2319 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
2321 HAL_SD_CardStateTypeDef CardState;
2323 /* DIsable All interrupts */
2324 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2325 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2327 /* Clear All flags */
2328 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2330 if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL))
2332 /* Disable the SD DMA request */
2333 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
2335 /* Abort the SD DMA Tx Stream */
2336 if(hsd->hdmatx != NULL)
2338 HAL_DMA_Abort(hsd->hdmatx);
2340 /* Abort the SD DMA Rx Stream */
2341 if(hsd->hdmarx != NULL)
2343 HAL_DMA_Abort(hsd->hdmarx);
2347 hsd->State = HAL_SD_STATE_READY;
2348 CardState = HAL_SD_GetCardState(hsd);
2349 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2351 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2353 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2355 return HAL_ERROR;
2357 return HAL_OK;
2361 * @brief Abort the current transfer and disable the SD (IT mode).
2362 * @param hsd pointer to a SD_HandleTypeDef structure that contains
2363 * the configuration information for SD module.
2364 * @retval HAL status
2366 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
2368 HAL_SD_CardStateTypeDef CardState;
2370 /* DIsable All interrupts */
2371 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2372 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2374 /* Clear All flags */
2375 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2377 if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL))
2379 /* Disable the SD DMA request */
2380 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
2382 /* Abort the SD DMA Tx Stream */
2383 if(hsd->hdmatx != NULL)
2385 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
2386 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
2388 hsd->hdmatx = NULL;
2391 /* Abort the SD DMA Rx Stream */
2392 if(hsd->hdmarx != NULL)
2394 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
2395 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
2397 hsd->hdmarx = NULL;
2402 /* No transfer ongoing on both DMA channels*/
2403 if((hsd->hdmatx == NULL) && (hsd->hdmarx == NULL))
2405 CardState = HAL_SD_GetCardState(hsd);
2406 hsd->State = HAL_SD_STATE_READY;
2407 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2409 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2411 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2413 return HAL_ERROR;
2415 else
2417 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2418 hsd->AbortCpltCallback(hsd);
2419 #else
2420 HAL_SD_AbortCallback(hsd);
2421 #endif
2425 return HAL_OK;
2429 * @}
2433 * @}
2436 /* Private function ----------------------------------------------------------*/
2437 /** @addtogroup SD_Private_Functions
2438 * @{
2442 * @brief DMA SD transmit process complete callback
2443 * @param hdma DMA handle
2444 * @retval None
2446 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2448 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2450 /* Enable DATAEND Interrupt */
2451 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DATAEND));
2455 * @brief DMA SD receive process complete callback
2456 * @param hdma DMA handle
2457 * @retval None
2459 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2461 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2462 uint32_t errorstate = HAL_SD_ERROR_NONE;
2464 /* Send stop command in multiblock write */
2465 if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
2467 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
2468 if(errorstate != HAL_SD_ERROR_NONE)
2470 hsd->ErrorCode |= errorstate;
2471 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2472 hsd->ErrorCallback(hsd);
2473 #else
2474 HAL_SD_ErrorCallback(hsd);
2475 #endif
2479 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2480 in the SD DCTRL register */
2481 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
2483 /* Clear all the static flags */
2484 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2486 hsd->State = HAL_SD_STATE_READY;
2488 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2489 hsd->RxCpltCallback(hsd);
2490 #else
2491 HAL_SD_RxCpltCallback(hsd);
2492 #endif
2496 * @brief DMA SD communication error callback
2497 * @param hdma DMA handle
2498 * @retval None
2500 static void SD_DMAError(DMA_HandleTypeDef *hdma)
2502 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2503 HAL_SD_CardStateTypeDef CardState;
2505 /* if DMA error is FIFO error ignore it */
2506 if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
2508 if((hsd->hdmarx->ErrorCode == HAL_DMA_ERROR_TE) || (hsd->hdmatx->ErrorCode == HAL_DMA_ERROR_TE))
2510 /* Clear All flags */
2511 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2513 /* Disable All interrupts */
2514 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT|\
2515 SDMMC_IT_TXUNDERR| SDMMC_IT_RXOVERR);
2517 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2518 CardState = HAL_SD_GetCardState(hsd);
2519 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2521 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2524 hsd->State= HAL_SD_STATE_READY;
2527 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2528 hsd->ErrorCallback(hsd);
2529 #else
2530 HAL_SD_ErrorCallback(hsd);
2531 #endif
2536 * @brief DMA SD Tx Abort callback
2537 * @param hdma DMA handle
2538 * @retval None
2540 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
2542 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2543 HAL_SD_CardStateTypeDef CardState;
2545 if(hsd->hdmatx != NULL)
2547 hsd->hdmatx = NULL;
2550 /* All DMA channels are aborted */
2551 if(hsd->hdmarx == NULL)
2553 CardState = HAL_SD_GetCardState(hsd);
2554 hsd->ErrorCode = HAL_SD_ERROR_NONE;
2555 hsd->State = HAL_SD_STATE_READY;
2556 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2558 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2560 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2562 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2563 hsd->AbortCpltCallback(hsd);
2564 #else
2565 HAL_SD_AbortCallback(hsd);
2566 #endif
2568 else
2570 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2571 hsd->ErrorCallback(hsd);
2572 #else
2573 HAL_SD_ErrorCallback(hsd);
2574 #endif
2581 * @brief DMA SD Rx Abort callback
2582 * @param hdma DMA handle
2583 * @retval None
2585 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
2587 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2588 HAL_SD_CardStateTypeDef CardState;
2590 if(hsd->hdmarx != NULL)
2592 hsd->hdmarx = NULL;
2595 /* All DMA channels are aborted */
2596 if(hsd->hdmatx == NULL)
2598 CardState = HAL_SD_GetCardState(hsd);
2599 hsd->ErrorCode = HAL_SD_ERROR_NONE;
2600 hsd->State = HAL_SD_STATE_READY;
2601 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2603 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2605 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2607 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2608 hsd->AbortCpltCallback(hsd);
2609 #else
2610 HAL_SD_AbortCallback(hsd);
2611 #endif
2613 else
2615 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2616 hsd->ErrorCallback(hsd);
2617 #else
2618 HAL_SD_ErrorCallback(hsd);
2619 #endif
2627 * @brief Initializes the sd card.
2628 * @param hsd Pointer to SD handle
2629 * @retval SD Card error state
2631 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
2633 HAL_SD_CardCSDTypeDef CSD;
2634 uint32_t errorstate = HAL_SD_ERROR_NONE;
2635 uint16_t sd_rca = 1;
2637 /* Check the power State */
2638 if(SDMMC_GetPowerState(hsd->Instance) == 0)
2640 /* Power off */
2641 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2644 if(hsd->SdCard.CardType != CARD_SECURED)
2646 /* Send CMD2 ALL_SEND_CID */
2647 errorstate = SDMMC_CmdSendCID(hsd->Instance);
2648 if(errorstate != HAL_SD_ERROR_NONE)
2650 return errorstate;
2652 else
2654 /* Get Card identification number data */
2655 hsd->CID[0] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2656 hsd->CID[1] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
2657 hsd->CID[2] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
2658 hsd->CID[3] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
2662 if(hsd->SdCard.CardType != CARD_SECURED)
2664 /* Send CMD3 SET_REL_ADDR with argument 0 */
2665 /* SD Card publishes its RCA. */
2666 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
2667 if(errorstate != HAL_SD_ERROR_NONE)
2669 return errorstate;
2672 if(hsd->SdCard.CardType != CARD_SECURED)
2674 /* Get the SD card RCA */
2675 hsd->SdCard.RelCardAdd = sd_rca;
2677 /* Send CMD9 SEND_CSD with argument as card's RCA */
2678 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2679 if(errorstate != HAL_SD_ERROR_NONE)
2681 return errorstate;
2683 else
2685 /* Get Card Specific Data */
2686 hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2687 hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
2688 hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
2689 hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
2693 /* Get the Card Class */
2694 hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20);
2696 /* Get CSD parameters */
2697 HAL_SD_GetCardCSD(hsd, &CSD);
2699 /* Select the Card */
2700 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16));
2701 if(errorstate != HAL_SD_ERROR_NONE)
2703 return errorstate;
2706 /* Configure SDMMC peripheral interface */
2707 SDMMC_Init(hsd->Instance, hsd->Init);
2709 /* All cards are initialized */
2710 return HAL_SD_ERROR_NONE;
2714 * @brief Enquires cards about their operating voltage and configures clock
2715 * controls and stores SD information that will be needed in future
2716 * in the SD handle.
2717 * @param hsd Pointer to SD handle
2718 * @retval error state
2720 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
2722 __IO uint32_t count = 0;
2723 uint32_t response = 0, validvoltage = 0;
2724 uint32_t errorstate = HAL_SD_ERROR_NONE;
2726 /* CMD0: GO_IDLE_STATE */
2727 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2728 if(errorstate != HAL_SD_ERROR_NONE)
2730 return errorstate;
2733 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2734 errorstate = SDMMC_CmdOperCond(hsd->Instance);
2735 if(errorstate != HAL_SD_ERROR_NONE)
2737 hsd->SdCard.CardVersion = CARD_V1_X;
2739 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2740 while(validvoltage == 0)
2742 if(count++ == SDMMC_MAX_VOLT_TRIAL)
2744 return HAL_SD_ERROR_INVALID_VOLTRANGE;
2747 /* SEND CMD55 APP_CMD with RCA as 0 */
2748 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2749 if(errorstate != HAL_SD_ERROR_NONE)
2751 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2754 /* Send CMD41 */
2755 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_STD_CAPACITY);
2756 if(errorstate != HAL_SD_ERROR_NONE)
2758 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2761 /* Get command response */
2762 response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2764 /* Get operating voltage*/
2765 validvoltage = (((response >> 31) == 1) ? 1 : 0);
2767 /* Card type is SDSC */
2768 hsd->SdCard.CardType = CARD_SDSC;
2770 else
2772 hsd->SdCard.CardVersion = CARD_V2_X;
2774 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2775 while(validvoltage == 0)
2777 if(count++ == SDMMC_MAX_VOLT_TRIAL)
2779 return HAL_SD_ERROR_INVALID_VOLTRANGE;
2782 /* SEND CMD55 APP_CMD with RCA as 0 */
2783 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2784 if(errorstate != HAL_SD_ERROR_NONE)
2786 return errorstate;
2789 /* Send CMD41 */
2790 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_HIGH_CAPACITY);
2791 if(errorstate != HAL_SD_ERROR_NONE)
2793 return errorstate;
2796 /* Get command response */
2797 response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2799 /* Get operating voltage*/
2800 validvoltage = (((response >> 31) == 1) ? 1 : 0);
2803 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
2805 hsd->SdCard.CardType = CARD_SDHC_SDXC;
2807 else
2809 hsd->SdCard.CardType = CARD_SDSC;
2813 return HAL_SD_ERROR_NONE;
2817 * @brief Turns the SDMMC output signals off.
2818 * @param hsd Pointer to SD handle
2819 * @retval HAL status
2821 static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd)
2823 /* Set Power State to OFF */
2824 SDMMC_PowerState_OFF(hsd->Instance);
2826 return HAL_OK;
2830 * @brief Send Status info command.
2831 * @param hsd pointer to SD handle
2832 * @param pSDstatus Pointer to the buffer that will contain the SD card status
2833 * SD Status register)
2834 * @retval error state
2836 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
2838 SDMMC_DataInitTypeDef config;
2839 uint32_t errorstate = HAL_SD_ERROR_NONE;
2840 uint32_t tickstart = HAL_GetTick();
2841 uint32_t count = 0;
2843 /* Check SD response */
2844 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2846 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2849 /* Set block size for card if it is not equal to current block size for card */
2850 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64);
2851 if(errorstate != HAL_SD_ERROR_NONE)
2853 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2854 return errorstate;
2857 /* Send CMD55 */
2858 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
2859 if(errorstate != HAL_SD_ERROR_NONE)
2861 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2862 return errorstate;
2865 /* Configure the SD DPSM (Data Path State Machine) */
2866 config.DataTimeOut = SDMMC_DATATIMEOUT;
2867 config.DataLength = 64;
2868 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
2869 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
2870 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
2871 config.DPSM = SDMMC_DPSM_ENABLE;
2872 SDMMC_ConfigData(hsd->Instance, &config);
2874 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
2875 errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
2876 if(errorstate != HAL_SD_ERROR_NONE)
2878 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2879 return errorstate;
2882 /* Get status data */
2883 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
2885 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
2887 for(count = 0; count < 8; count++)
2889 *(pSDstatus + count) = SDMMC_ReadFIFO(hsd->Instance);
2892 pSDstatus += 8;
2895 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2897 return HAL_SD_ERROR_TIMEOUT;
2901 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
2903 return HAL_SD_ERROR_DATA_TIMEOUT;
2905 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
2907 return HAL_SD_ERROR_DATA_CRC_FAIL;
2909 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
2911 return HAL_SD_ERROR_RX_OVERRUN;
2914 while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL)))
2916 *pSDstatus = SDMMC_ReadFIFO(hsd->Instance);
2917 pSDstatus++;
2919 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2921 return HAL_SD_ERROR_TIMEOUT;
2925 /* Clear all the static status flags*/
2926 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2928 return HAL_SD_ERROR_NONE;
2932 * @brief Returns the current card's status.
2933 * @param hsd Pointer to SD handle
2934 * @param pCardStatus pointer to the buffer that will contain the SD card
2935 * status (Card Status register)
2936 * @retval error state
2938 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
2940 uint32_t errorstate = HAL_SD_ERROR_NONE;
2942 if(pCardStatus == NULL)
2944 return HAL_SD_ERROR_PARAM;
2947 /* Send Status command */
2948 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
2949 if(errorstate != HAL_OK)
2951 return errorstate;
2954 /* Get SD card status */
2955 *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
2957 return HAL_SD_ERROR_NONE;
2961 * @brief Enables the SDMMC wide bus mode.
2962 * @param hsd pointer to SD handle
2963 * @retval error state
2965 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
2967 uint32_t scr[2] = {0, 0};
2968 uint32_t errorstate = HAL_SD_ERROR_NONE;
2970 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2972 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2975 /* Get SCR Register */
2976 errorstate = SD_FindSCR(hsd, scr);
2977 if(errorstate != HAL_OK)
2979 return errorstate;
2982 /* If requested card supports wide bus operation */
2983 if((scr[1] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
2985 /* Send CMD55 APP_CMD with argument as card's RCA.*/
2986 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
2987 if(errorstate != HAL_OK)
2989 return errorstate;
2992 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
2993 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2);
2994 if(errorstate != HAL_OK)
2996 return errorstate;
2999 return HAL_SD_ERROR_NONE;
3001 else
3003 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3008 * @brief Disables the SDMMC wide bus mode.
3009 * @param hsd Pointer to SD handle
3010 * @retval error state
3012 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3014 uint32_t scr[2] = {0, 0};
3015 uint32_t errorstate = HAL_SD_ERROR_NONE;
3017 if((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3019 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3022 /* Get SCR Register */
3023 errorstate = SD_FindSCR(hsd, scr);
3024 if(errorstate != HAL_OK)
3026 return errorstate;
3029 /* If requested card supports 1 bit mode operation */
3030 if((scr[1] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
3032 /* Send CMD55 APP_CMD with argument as card's RCA */
3033 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16));
3034 if(errorstate != HAL_OK)
3036 return errorstate;
3039 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3040 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0);
3041 if(errorstate != HAL_OK)
3043 return errorstate;
3046 return HAL_SD_ERROR_NONE;
3048 else
3050 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3056 * @brief Finds the SD card SCR register value.
3057 * @param hsd Pointer to SD handle
3058 * @param pSCR pointer to the buffer that will contain the SCR value
3059 * @retval error state
3061 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
3063 SDMMC_DataInitTypeDef config;
3064 uint32_t errorstate = HAL_SD_ERROR_NONE;
3065 uint32_t tickstart = HAL_GetTick();
3066 uint32_t index = 0;
3067 uint32_t tempscr[2] = {0, 0};
3069 /* Set Block Size To 8 Bytes */
3070 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8);
3071 if(errorstate != HAL_OK)
3073 return errorstate;
3076 /* Send CMD55 APP_CMD with argument as card's RCA */
3077 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16));
3078 if(errorstate != HAL_OK)
3080 return errorstate;
3083 config.DataTimeOut = SDMMC_DATATIMEOUT;
3084 config.DataLength = 8;
3085 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
3086 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3087 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3088 config.DPSM = SDMMC_DPSM_ENABLE;
3089 SDMMC_ConfigData(hsd->Instance, &config);
3091 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3092 errorstate = SDMMC_CmdSendSCR(hsd->Instance);
3093 if(errorstate != HAL_OK)
3095 return errorstate;
3098 while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND))
3100 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXDAVL))
3102 *(tempscr + index) = SDMMC_ReadFIFO(hsd->Instance);
3103 index++;
3106 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3108 return HAL_SD_ERROR_TIMEOUT;
3112 if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3114 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3116 return HAL_SD_ERROR_DATA_TIMEOUT;
3118 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3120 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3122 return HAL_SD_ERROR_DATA_CRC_FAIL;
3124 else if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3126 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3128 return HAL_SD_ERROR_RX_OVERRUN;
3130 else
3132 /* No error flag set */
3133 /* Clear all the static flags */
3134 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
3136 *(pSCR + 1) = ((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
3137 ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24);
3139 *(pSCR) = ((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
3140 ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24);
3143 return HAL_SD_ERROR_NONE;
3147 * @brief Wrap up reading in non-blocking mode.
3148 * @param hsd pointer to a SD_HandleTypeDef structure that contains
3149 * the configuration information.
3150 * @retval HAL status
3152 static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd)
3154 uint32_t count = 0;
3155 uint32_t* tmp;
3157 tmp = (uint32_t*)hsd->pRxBuffPtr;
3159 /* Read data from SDMMC Rx FIFO */
3160 for(count = 0; count < 8; count++)
3162 *(tmp + count) = SDMMC_ReadFIFO(hsd->Instance);
3165 hsd->pRxBuffPtr += 8;
3167 return HAL_OK;
3171 * @brief Wrap up writing in non-blocking mode.
3172 * @param hsd pointer to a SD_HandleTypeDef structure that contains
3173 * the configuration information.
3174 * @retval HAL status
3176 static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd)
3178 uint32_t count = 0;
3179 uint32_t* tmp;
3181 tmp = (uint32_t*)hsd->pTxBuffPtr;
3183 /* Write data to SDMMC Tx FIFO */
3184 for(count = 0; count < 8; count++)
3186 SDMMC_WriteFIFO(hsd->Instance, (tmp + count));
3189 hsd->pTxBuffPtr += 8;
3191 return HAL_OK;
3195 * @}
3198 #endif /* HAL_SD_MODULE_ENABLED */
3201 * @}
3205 * @}
3208 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/