2 ******************************************************************************
3 * @file stm32f2xx_hal_sd.c
4 * @author MCD Application Team
5 * @brief SD card HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Secure Digital (SD) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral Control functions
11 * + Peripheral State functions
14 ==============================================================================
15 ##### How to use this driver #####
16 ==============================================================================
18 This driver implements a high level communication layer for read and write from/to
19 this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by
20 the user in HAL_SD_MspInit() function (MSP layer).
21 Basically, the MSP layer configuration should be the same as we provide in the
23 You can easily tailor this configuration according to hardware resources.
26 This driver is a generic layered driver for SDIO memories which uses the HAL
27 SDIO driver functions to interface with SD and uSD cards devices.
28 It is used as follows:
30 (#)Initialize the SDIO low level resources by implementing the HAL_SD_MspInit() API:
31 (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE();
32 (##) SDIO pins configuration for SD card
33 (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
34 (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init()
35 and according to your pin assignment;
36 (##) DMA configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
37 and HAL_SD_WriteBlocks_DMA() APIs).
38 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
39 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
40 (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
41 (+++) Configure the SDIO and DMA interrupt priorities using functions
42 HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority
43 (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ()
44 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
45 and __HAL_SD_DISABLE_IT() inside the communication process.
46 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
47 and __HAL_SD_CLEAR_IT()
48 (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
49 and HAL_SD_WriteBlocks_IT() APIs).
50 (+++) Configure the SDIO interrupt priorities using function HAL_NVIC_SetPriority();
51 (+++) Enable the NVIC SDIO IRQs using function HAL_NVIC_EnableIRQ()
52 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
53 and __HAL_SD_DISABLE_IT() inside the communication process.
54 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
55 and __HAL_SD_CLEAR_IT()
56 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
59 *** SD Card Initialization and configuration ***
60 ================================================
62 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
63 SDIO Peripheral(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
64 This function provide the following operations:
66 (#) Apply the SD Card initialization process at 400KHz and check the SD Card
67 type (Standard Capacity or High Capacity). You can change or adapt this
68 frequency by adjusting the "ClockDiv" field.
69 The SD Card frequency (SDIO_CK) is computed as follows:
71 SDIO_CK = SDIOCLK / (ClockDiv + 2)
73 In initialization mode and according to the SD Card standard,
74 make sure that the SDIO_CK frequency doesn't exceed 400KHz.
76 This phase of initialization is done through SDIO_Init() and
77 SDIO_PowerState_ON() SDIO low level APIs.
79 (#) Initialize the SD card. The API used is HAL_SD_InitCard().
80 This phase allows the card initialization and identification
81 and check the SD Card type (Standard Capacity or High Capacity)
82 The initialization flow is compatible with SD standard.
84 This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
87 (#) Configure the SD Card Data transfer frequency. You can change or adapt this
88 frequency by adjusting the "ClockDiv" field.
89 In transfer mode and according to the SD Card standard, make sure that the
90 SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
91 To be able to use a frequency higher than 24MHz, you should use the SDIO
92 peripheral in bypass mode. Refer to the corresponding reference manual
95 (#) Select the corresponding SD Card according to the address read with the step 2.
97 (#) Configure the SD Card in wide bus mode: 4-bits data.
99 *** SD Card Read operation ***
100 ==============================
102 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
103 This function support only 512-bytes block length (the block size should be
104 chosen as 512 bytes).
105 You can choose either one block read operation or multiple block read operation
106 by adjusting the "NumberOfBlocks" parameter.
107 After this, you have to ensure that the transfer is done correctly. The check is done
108 through HAL_SD_GetCardState() function for SD card state.
110 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
111 This function support only 512-bytes block length (the block size should be
112 chosen as 512 bytes).
113 You can choose either one block read operation or multiple block read operation
114 by adjusting the "NumberOfBlocks" parameter.
115 After this, you have to ensure that the transfer is done correctly. The check is done
116 through HAL_SD_GetCardState() function for SD card state.
117 You could also check the DMA transfer process through the SD Rx interrupt event.
119 (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
120 This function support only 512-bytes block length (the block size should be
121 chosen as 512 bytes).
122 You can choose either one block read operation or multiple block read operation
123 by adjusting the "NumberOfBlocks" parameter.
124 After this, you have to ensure that the transfer is done correctly. The check is done
125 through HAL_SD_GetCardState() function for SD card state.
126 You could also check the IT transfer process through the SD Rx interrupt event.
128 *** SD Card Write operation ***
129 ===============================
131 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
132 This function support only 512-bytes block length (the block size should be
133 chosen as 512 bytes).
134 You can choose either one block read operation or multiple block read operation
135 by adjusting the "NumberOfBlocks" parameter.
136 After this, you have to ensure that the transfer is done correctly. The check is done
137 through HAL_SD_GetCardState() function for SD card state.
139 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
140 This function support only 512-bytes block length (the block size should be
141 chosen as 512 bytes).
142 You can choose either one block read operation or multiple block read operation
143 by adjusting the "NumberOfBlocks" parameter.
144 After this, you have to ensure that the transfer is done correctly. The check is done
145 through HAL_SD_GetCardState() function for SD card state.
146 You could also check the DMA transfer process through the SD Tx interrupt event.
148 (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
149 This function support only 512-bytes block length (the block size should be
150 chosen as 512 bytes).
151 You can choose either one block read operation or multiple block read operation
152 by adjusting the "NumberOfBlocks" parameter.
153 After this, you have to ensure that the transfer is done correctly. The check is done
154 through HAL_SD_GetCardState() function for SD card state.
155 You could also check the IT transfer process through the SD Tx interrupt event.
157 *** SD card status ***
158 ======================
160 (+) The SD Status contains status bits that are related to the SD Memory
161 Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
163 *** SD card information ***
164 ===========================
166 (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
167 It returns useful information about the SD card such as block size, card type,
170 *** SD card CSD register ***
171 ============================
172 (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
173 Some of the CSD parameters are useful for card initialization and identification.
175 *** SD card CID register ***
176 ============================
177 (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
178 Some of the CSD parameters are useful for card initialization and identification.
180 *** SD HAL driver macros list ***
181 ==================================
183 Below the list of most used macros in SD HAL driver.
185 (+) __HAL_SD_ENABLE : Enable the SD device
186 (+) __HAL_SD_DISABLE : Disable the SD device
187 (+) __HAL_SD_DMA_ENABLE: Enable the SDIO DMA transfer
188 (+) __HAL_SD_DMA_DISABLE: Disable the SDIO DMA transfer
189 (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
190 (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
191 (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
192 (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
194 (@) You can refer to the SD HAL driver header file for more useful macros
196 *** Callback registration ***
197 =============================================
199 The compilation define USE_HAL_SD_REGISTER_CALLBACKS when set to 1
200 allows the user to configure dynamically the driver callbacks.
202 Use Functions @ref HAL_SD_RegisterCallback() to register a user callback,
203 it allows to register following callbacks:
204 (+) TxCpltCallback : callback when a transmission transfer is completed.
205 (+) RxCpltCallback : callback when a reception transfer is completed.
206 (+) ErrorCallback : callback when error occurs.
207 (+) AbortCpltCallback : callback when abort is completed.
208 (+) MspInitCallback : SD MspInit.
209 (+) MspDeInitCallback : SD MspDeInit.
210 This function takes as parameters the HAL peripheral handle, the Callback ID
211 and a pointer to the user callback function.
213 Use function @ref HAL_SD_UnRegisterCallback() to reset a callback to the default
214 weak (surcharged) function. It allows to reset following callbacks:
215 (+) TxCpltCallback : callback when a transmission transfer is completed.
216 (+) RxCpltCallback : callback when a reception transfer is completed.
217 (+) ErrorCallback : callback when error occurs.
218 (+) AbortCpltCallback : callback when abort is completed.
219 (+) MspInitCallback : SD MspInit.
220 (+) MspDeInitCallback : SD MspDeInit.
221 This function) takes as parameters the HAL peripheral handle and the Callback ID.
223 By default, after the @ref HAL_SD_Init and if the state is HAL_SD_STATE_RESET
224 all callbacks are reset to the corresponding legacy weak (surcharged) functions.
225 Exception done for MspInit and MspDeInit callbacks that are respectively
226 reset to the legacy weak (surcharged) functions in the @ref HAL_SD_Init
227 and @ref HAL_SD_DeInit only when these callbacks are null (not registered beforehand).
228 If not, MspInit or MspDeInit are not null, the @ref HAL_SD_Init and @ref HAL_SD_DeInit
229 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
231 Callbacks can be registered/unregistered in READY state only.
232 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
233 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
234 during the Init/DeInit.
235 In that case first register the MspInit/MspDeInit user callbacks
236 using @ref HAL_SD_RegisterCallback before calling @ref HAL_SD_DeInit
237 or @ref HAL_SD_Init function.
239 When The compilation define USE_HAL_SD_REGISTER_CALLBACKS is set to 0 or
240 not defined, the callback registering feature is not available
241 and weak (surcharged) callbacks are used.
244 ******************************************************************************
247 * <h2><center>© Copyright (c) 2018 STMicroelectronics.
248 * All rights reserved.</center></h2>
250 * This software component is licensed by ST under BSD 3-Clause license,
251 * the "License"; You may not use this file except in compliance with the
252 * License. You may obtain a copy of the License at:
253 * opensource.org/licenses/BSD-3-Clause
255 ******************************************************************************
258 /* Includes ------------------------------------------------------------------*/
259 #include "stm32f2xx_hal.h"
263 /** @addtogroup STM32F2xx_HAL_Driver
271 #ifdef HAL_SD_MODULE_ENABLED
273 /* Private typedef -----------------------------------------------------------*/
274 /* Private define ------------------------------------------------------------*/
275 /** @addtogroup SD_Private_Defines
283 /* Private macro -------------------------------------------------------------*/
284 /* Private variables ---------------------------------------------------------*/
285 /* Private function prototypes -----------------------------------------------*/
286 /* Private functions ---------------------------------------------------------*/
287 /** @defgroup SD_Private_Functions SD Private Functions
290 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd);
291 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd);
292 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
293 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
294 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd);
295 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
296 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
297 static void SD_PowerOFF(SD_HandleTypeDef *hsd);
298 static void SD_Write_IT(SD_HandleTypeDef *hsd);
299 static void SD_Read_IT(SD_HandleTypeDef *hsd);
300 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
301 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
302 static void SD_DMAError(DMA_HandleTypeDef *hdma);
303 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma);
304 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma);
309 /* Exported functions --------------------------------------------------------*/
310 /** @addtogroup SD_Exported_Functions
314 /** @addtogroup SD_Exported_Functions_Group1
315 * @brief Initialization and de-initialization functions
318 ==============================================================================
319 ##### Initialization and de-initialization functions #####
320 ==============================================================================
322 This section provides functions allowing to initialize/de-initialize the SD
323 card device to be ready for use.
330 * @brief Initializes the SD according to the specified parameters in the
331 SD_HandleTypeDef and create the associated handle.
332 * @param hsd: Pointer to the SD handle
335 HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
337 /* Check the SD handle allocation */
343 /* Check the parameters */
344 assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance));
345 assert_param(IS_SDIO_CLOCK_EDGE(hsd->Init.ClockEdge));
346 assert_param(IS_SDIO_CLOCK_BYPASS(hsd->Init.ClockBypass));
347 assert_param(IS_SDIO_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
348 assert_param(IS_SDIO_BUS_WIDE(hsd->Init.BusWide));
349 assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
350 assert_param(IS_SDIO_CLKDIV(hsd->Init.ClockDiv));
352 if(hsd->State == HAL_SD_STATE_RESET)
354 /* Allocate lock resource and initialize it */
355 hsd->Lock = HAL_UNLOCKED;
356 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
357 /* Reset Callback pointers in HAL_SD_STATE_RESET only */
358 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
359 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
360 hsd->ErrorCallback = HAL_SD_ErrorCallback;
361 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
363 if(hsd->MspInitCallback == NULL)
365 hsd->MspInitCallback = HAL_SD_MspInit;
368 /* Init the low level hardware */
369 hsd->MspInitCallback(hsd);
371 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
373 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
376 hsd->State = HAL_SD_STATE_BUSY;
378 /* Initialize the Card parameters */
379 if (HAL_SD_InitCard(hsd) != HAL_OK)
384 /* Initialize the error code */
385 hsd->ErrorCode = HAL_SD_ERROR_NONE;
387 /* Initialize the SD operation */
388 hsd->Context = SD_CONTEXT_NONE;
390 /* Initialize the SD state */
391 hsd->State = HAL_SD_STATE_READY;
397 * @brief Initializes the SD Card.
398 * @param hsd: Pointer to SD handle
399 * @note This function initializes the SD card. It could be used when a card
400 re-initialization is needed.
403 HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
406 HAL_StatusTypeDef status;
409 /* Default SDIO peripheral configuration for SD card initialization */
410 Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
411 Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
412 Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
413 Init.BusWide = SDIO_BUS_WIDE_1B;
414 Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
415 Init.ClockDiv = SDIO_INIT_CLK_DIV;
417 /* Initialize SDIO peripheral interface with default configuration */
418 status = SDIO_Init(hsd->Instance, Init);
424 /* Disable SDIO Clock */
425 __HAL_SD_DISABLE(hsd);
427 /* Set Power State to ON */
428 (void)SDIO_PowerState_ON(hsd->Instance);
430 /* Enable SDIO Clock */
431 __HAL_SD_ENABLE(hsd);
433 /* 1ms: required power up waiting time before starting the SD initialization
437 /* Identify card operating voltage */
438 errorstate = SD_PowerON(hsd);
439 if(errorstate != HAL_SD_ERROR_NONE)
441 hsd->State = HAL_SD_STATE_READY;
442 hsd->ErrorCode |= errorstate;
446 /* Card initialization */
447 errorstate = SD_InitCard(hsd);
448 if(errorstate != HAL_SD_ERROR_NONE)
450 hsd->State = HAL_SD_STATE_READY;
451 hsd->ErrorCode |= errorstate;
459 * @brief De-Initializes the SD card.
460 * @param hsd: Pointer to SD handle
463 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
465 /* Check the SD handle allocation */
471 /* Check the parameters */
472 assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance));
474 hsd->State = HAL_SD_STATE_BUSY;
476 /* Set SD power state to off */
479 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
480 if(hsd->MspDeInitCallback == NULL)
482 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
485 /* DeInit the low level hardware */
486 hsd->MspDeInitCallback(hsd);
488 /* De-Initialize the MSP layer */
489 HAL_SD_MspDeInit(hsd);
490 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
492 hsd->ErrorCode = HAL_SD_ERROR_NONE;
493 hsd->State = HAL_SD_STATE_RESET;
500 * @brief Initializes the SD MSP.
501 * @param hsd: Pointer to SD handle
504 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
506 /* Prevent unused argument(s) compilation warning */
509 /* NOTE : This function should not be modified, when the callback is needed,
510 the HAL_SD_MspInit could be implemented in the user file
515 * @brief De-Initialize SD MSP.
516 * @param hsd: Pointer to SD handle
519 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
521 /* Prevent unused argument(s) compilation warning */
524 /* NOTE : This function should not be modified, when the callback is needed,
525 the HAL_SD_MspDeInit could be implemented in the user file
533 /** @addtogroup SD_Exported_Functions_Group2
534 * @brief Data transfer functions
537 ==============================================================================
538 ##### IO operation functions #####
539 ==============================================================================
541 This subsection provides a set of functions allowing to manage the data
542 transfer from/to SD card.
549 * @brief Reads block(s) from a specified address in a card. The Data transfer
550 * is managed by polling mode.
551 * @note This API should be followed by a check on the card state through
552 * HAL_SD_GetCardState().
553 * @param hsd: Pointer to SD handle
554 * @param pData: pointer to the buffer that will contain the received data
555 * @param BlockAdd: Block Address from where data is to be read
556 * @param NumberOfBlocks: Number of SD blocks to read
557 * @param Timeout: Specify timeout value
560 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
562 SDIO_DataInitTypeDef config;
564 uint32_t tickstart = HAL_GetTick();
565 uint32_t count, data, dataremaining;
566 uint32_t add = BlockAdd;
567 uint8_t *tempbuff = pData;
571 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
575 if(hsd->State == HAL_SD_STATE_READY)
577 hsd->ErrorCode = HAL_SD_ERROR_NONE;
579 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
581 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
585 hsd->State = HAL_SD_STATE_BUSY;
587 /* Initialize data control register */
588 hsd->Instance->DCTRL = 0U;
590 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
595 /* Set Block Size for Card */
596 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
597 if(errorstate != HAL_SD_ERROR_NONE)
599 /* Clear all the static flags */
600 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
601 hsd->ErrorCode |= errorstate;
602 hsd->State = HAL_SD_STATE_READY;
606 /* Configure the SD DPSM (Data Path State Machine) */
607 config.DataTimeOut = SDMMC_DATATIMEOUT;
608 config.DataLength = NumberOfBlocks * BLOCKSIZE;
609 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
610 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
611 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
612 config.DPSM = SDIO_DPSM_ENABLE;
613 (void)SDIO_ConfigData(hsd->Instance, &config);
615 /* Read block(s) in polling mode */
616 if(NumberOfBlocks > 1U)
618 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
620 /* Read Multi Block command */
621 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
625 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
627 /* Read Single Block command */
628 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
630 if(errorstate != HAL_SD_ERROR_NONE)
632 /* Clear all the static flags */
633 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
634 hsd->ErrorCode |= errorstate;
635 hsd->State = HAL_SD_STATE_READY;
636 hsd->Context = SD_CONTEXT_NONE;
640 /* Poll on SDIO flags */
641 dataremaining = config.DataLength;
642 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
644 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) && (dataremaining > 0U))
646 /* Read data from SDIO Rx FIFO */
647 for(count = 0U; count < 8U; count++)
649 data = SDIO_ReadFIFO(hsd->Instance);
650 *tempbuff = (uint8_t)(data & 0xFFU);
653 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
656 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
659 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
665 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
667 /* Clear all the static flags */
668 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
669 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
670 hsd->State= HAL_SD_STATE_READY;
671 hsd->Context = SD_CONTEXT_NONE;
676 /* Send stop transmission command in case of multiblock read */
677 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
679 if(hsd->SdCard.CardType != CARD_SECURED)
681 /* Send stop transmission command */
682 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
683 if(errorstate != HAL_SD_ERROR_NONE)
685 /* Clear all the static flags */
686 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
687 hsd->ErrorCode |= errorstate;
688 hsd->State = HAL_SD_STATE_READY;
689 hsd->Context = SD_CONTEXT_NONE;
695 /* Get error state */
696 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
698 /* Clear all the static flags */
699 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
700 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
701 hsd->State = HAL_SD_STATE_READY;
702 hsd->Context = SD_CONTEXT_NONE;
705 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
707 /* Clear all the static flags */
708 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
709 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
710 hsd->State = HAL_SD_STATE_READY;
711 hsd->Context = SD_CONTEXT_NONE;
714 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
716 /* Clear all the static flags */
717 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
718 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
719 hsd->State = HAL_SD_STATE_READY;
720 hsd->Context = SD_CONTEXT_NONE;
728 /* Empty FIFO if there is still any data */
729 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (dataremaining > 0U))
731 data = SDIO_ReadFIFO(hsd->Instance);
732 *tempbuff = (uint8_t)(data & 0xFFU);
735 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
738 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
741 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
745 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
747 /* Clear all the static flags */
748 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
749 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
750 hsd->State= HAL_SD_STATE_READY;
751 hsd->Context = SD_CONTEXT_NONE;
756 /* Clear all the static flags */
757 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
759 hsd->State = HAL_SD_STATE_READY;
765 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
771 * @brief Allows to write block(s) to a specified address in a card. The Data
772 * transfer is managed by polling mode.
773 * @note This API should be followed by a check on the card state through
774 * HAL_SD_GetCardState().
775 * @param hsd: Pointer to SD handle
776 * @param pData: pointer to the buffer that will contain the data to transmit
777 * @param BlockAdd: Block Address where data will be written
778 * @param NumberOfBlocks: Number of SD blocks to write
779 * @param Timeout: Specify timeout value
782 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
784 SDIO_DataInitTypeDef config;
786 uint32_t tickstart = HAL_GetTick();
787 uint32_t count, data, dataremaining;
788 uint32_t add = BlockAdd;
789 uint8_t *tempbuff = pData;
793 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
797 if(hsd->State == HAL_SD_STATE_READY)
799 hsd->ErrorCode = HAL_SD_ERROR_NONE;
801 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
803 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
807 hsd->State = HAL_SD_STATE_BUSY;
809 /* Initialize data control register */
810 hsd->Instance->DCTRL = 0U;
812 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
817 /* Set Block Size for Card */
818 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
819 if(errorstate != HAL_SD_ERROR_NONE)
821 /* Clear all the static flags */
822 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
823 hsd->ErrorCode |= errorstate;
824 hsd->State = HAL_SD_STATE_READY;
828 /* Configure the SD DPSM (Data Path State Machine) */
829 config.DataTimeOut = SDMMC_DATATIMEOUT;
830 config.DataLength = NumberOfBlocks * BLOCKSIZE;
831 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
832 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
833 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
834 config.DPSM = SDIO_DPSM_ENABLE;
835 (void)SDIO_ConfigData(hsd->Instance, &config);
837 /* Write Blocks in Polling mode */
838 if(NumberOfBlocks > 1U)
840 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
842 /* Write Multi Block command */
843 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
847 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
849 /* Write Single Block command */
850 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
852 if(errorstate != HAL_SD_ERROR_NONE)
854 /* Clear all the static flags */
855 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
856 hsd->ErrorCode |= errorstate;
857 hsd->State = HAL_SD_STATE_READY;
858 hsd->Context = SD_CONTEXT_NONE;
862 /* Write block(s) in polling mode */
863 dataremaining = config.DataLength;
864 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
866 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) && (dataremaining > 0U))
868 /* Write data to SDIO Tx FIFO */
869 for(count = 0U; count < 8U; count++)
871 data = (uint32_t)(*tempbuff);
874 data |= ((uint32_t)(*tempbuff) << 8U);
877 data |= ((uint32_t)(*tempbuff) << 16U);
880 data |= ((uint32_t)(*tempbuff) << 24U);
883 (void)SDIO_WriteFIFO(hsd->Instance, &data);
887 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
889 /* Clear all the static flags */
890 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
891 hsd->ErrorCode |= errorstate;
892 hsd->State = HAL_SD_STATE_READY;
893 hsd->Context = SD_CONTEXT_NONE;
898 /* Send stop transmission command in case of multiblock write */
899 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
901 if(hsd->SdCard.CardType != CARD_SECURED)
903 /* Send stop transmission command */
904 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
905 if(errorstate != HAL_SD_ERROR_NONE)
907 /* Clear all the static flags */
908 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
909 hsd->ErrorCode |= errorstate;
910 hsd->State = HAL_SD_STATE_READY;
911 hsd->Context = SD_CONTEXT_NONE;
917 /* Get error state */
918 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
920 /* Clear all the static flags */
921 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
922 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
923 hsd->State = HAL_SD_STATE_READY;
924 hsd->Context = SD_CONTEXT_NONE;
927 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
929 /* Clear all the static flags */
930 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
931 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
932 hsd->State = HAL_SD_STATE_READY;
933 hsd->Context = SD_CONTEXT_NONE;
936 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
938 /* Clear all the static flags */
939 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
940 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
941 hsd->State = HAL_SD_STATE_READY;
942 hsd->Context = SD_CONTEXT_NONE;
950 /* Clear all the static flags */
951 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
953 hsd->State = HAL_SD_STATE_READY;
959 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
965 * @brief Reads block(s) from a specified address in a card. The Data transfer
966 * is managed in interrupt mode.
967 * @note This API should be followed by a check on the card state through
968 * HAL_SD_GetCardState().
969 * @note You could also check the IT transfer process through the SD Rx
971 * @param hsd: Pointer to SD handle
972 * @param pData: Pointer to the buffer that will contain the received data
973 * @param BlockAdd: Block Address from where data is to be read
974 * @param NumberOfBlocks: Number of blocks to read.
977 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
979 SDIO_DataInitTypeDef config;
981 uint32_t add = BlockAdd;
985 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
989 if(hsd->State == HAL_SD_STATE_READY)
991 hsd->ErrorCode = HAL_SD_ERROR_NONE;
993 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
995 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
999 hsd->State = HAL_SD_STATE_BUSY;
1001 /* Initialize data control register */
1002 hsd->Instance->DCTRL = 0U;
1004 hsd->pRxBuffPtr = pData;
1005 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1007 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF));
1009 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1014 /* Set Block Size for Card */
1015 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1016 if(errorstate != HAL_SD_ERROR_NONE)
1018 /* Clear all the static flags */
1019 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1020 hsd->ErrorCode |= errorstate;
1021 hsd->State = HAL_SD_STATE_READY;
1025 /* Configure the SD DPSM (Data Path State Machine) */
1026 config.DataTimeOut = SDMMC_DATATIMEOUT;
1027 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1028 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1029 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
1030 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1031 config.DPSM = SDIO_DPSM_ENABLE;
1032 (void)SDIO_ConfigData(hsd->Instance, &config);
1034 /* Read Blocks in IT mode */
1035 if(NumberOfBlocks > 1U)
1037 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1039 /* Read Multi Block command */
1040 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1044 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
1046 /* Read Single Block command */
1047 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1049 if(errorstate != HAL_SD_ERROR_NONE)
1051 /* Clear all the static flags */
1052 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1053 hsd->ErrorCode |= errorstate;
1054 hsd->State = HAL_SD_STATE_READY;
1055 hsd->Context = SD_CONTEXT_NONE;
1068 * @brief Writes block(s) to a specified address in a card. The Data transfer
1069 * is managed in interrupt mode.
1070 * @note This API should be followed by a check on the card state through
1071 * HAL_SD_GetCardState().
1072 * @note You could also check the IT transfer process through the SD Tx
1074 * @param hsd: Pointer to SD handle
1075 * @param pData: Pointer to the buffer that will contain the data to transmit
1076 * @param BlockAdd: Block Address where data will be written
1077 * @param NumberOfBlocks: Number of blocks to write
1078 * @retval HAL status
1080 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1082 SDIO_DataInitTypeDef config;
1083 uint32_t errorstate;
1084 uint32_t add = BlockAdd;
1088 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1092 if(hsd->State == HAL_SD_STATE_READY)
1094 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1096 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1098 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1102 hsd->State = HAL_SD_STATE_BUSY;
1104 /* Initialize data control register */
1105 hsd->Instance->DCTRL = 0U;
1107 hsd->pTxBuffPtr = pData;
1108 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1110 /* Enable transfer interrupts */
1111 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE));
1113 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1118 /* Set Block Size for Card */
1119 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1120 if(errorstate != HAL_SD_ERROR_NONE)
1122 /* Clear all the static flags */
1123 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1124 hsd->ErrorCode |= errorstate;
1125 hsd->State = HAL_SD_STATE_READY;
1129 /* Write Blocks in Polling mode */
1130 if(NumberOfBlocks > 1U)
1132 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1134 /* Write Multi Block command */
1135 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1139 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1141 /* Write Single Block command */
1142 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1144 if(errorstate != HAL_SD_ERROR_NONE)
1146 /* Clear all the static flags */
1147 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1148 hsd->ErrorCode |= errorstate;
1149 hsd->State = HAL_SD_STATE_READY;
1150 hsd->Context = SD_CONTEXT_NONE;
1154 /* Configure the SD DPSM (Data Path State Machine) */
1155 config.DataTimeOut = SDMMC_DATATIMEOUT;
1156 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1157 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1158 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
1159 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1160 config.DPSM = SDIO_DPSM_ENABLE;
1161 (void)SDIO_ConfigData(hsd->Instance, &config);
1172 * @brief Reads block(s) from a specified address in a card. The Data transfer
1173 * is managed by DMA mode.
1174 * @note This API should be followed by a check on the card state through
1175 * HAL_SD_GetCardState().
1176 * @note You could also check the DMA transfer process through the SD Rx
1178 * @param hsd: Pointer SD handle
1179 * @param pData: Pointer to the buffer that will contain the received data
1180 * @param BlockAdd: Block Address from where data is to be read
1181 * @param NumberOfBlocks: Number of blocks to read.
1182 * @retval HAL status
1184 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1186 SDIO_DataInitTypeDef config;
1187 uint32_t errorstate;
1188 uint32_t add = BlockAdd;
1192 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1196 if(hsd->State == HAL_SD_STATE_READY)
1198 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1200 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1202 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1206 hsd->State = HAL_SD_STATE_BUSY;
1208 /* Initialize data control register */
1209 hsd->Instance->DCTRL = 0U;
1211 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
1213 /* Set the DMA transfer complete callback */
1214 hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
1216 /* Set the DMA error callback */
1217 hsd->hdmarx->XferErrorCallback = SD_DMAError;
1219 /* Set the DMA Abort callback */
1220 hsd->hdmarx->XferAbortCallback = NULL;
1222 /* Enable the DMA Channel */
1223 if(HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1225 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
1226 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1227 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1228 hsd->State = HAL_SD_STATE_READY;
1233 /* Enable SD DMA transfer */
1234 // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
1236 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1240 /* Set Block Size for Card */
1241 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1242 if(errorstate != HAL_SD_ERROR_NONE)
1244 /* Clear all the static flags */
1245 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1246 hsd->ErrorCode |= errorstate;
1247 hsd->State = HAL_SD_STATE_READY;
1252 /* Configure the SD DPSM (Data Path State Machine) */
1253 config.DataTimeOut = SDMMC_DATATIMEOUT;
1254 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1255 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1256 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
1257 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1258 config.DPSM = SDIO_DPSM_ENABLE;
1260 // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
1261 // data is just discarded before the dpsm is started.
1262 __HAL_SD_DMA_ENABLE();
1264 (void)SDIO_ConfigData(hsd->Instance, &config);
1266 /* Read Blocks in DMA mode */
1267 if(NumberOfBlocks > 1U)
1269 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1271 /* Read Multi Block command */
1272 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1276 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1278 /* Read Single Block command */
1279 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1281 if(errorstate != HAL_SD_ERROR_NONE)
1283 /* Clear all the static flags */
1284 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1285 hsd->ErrorCode |= errorstate;
1286 hsd->State = HAL_SD_STATE_READY;
1287 hsd->Context = SD_CONTEXT_NONE;
1301 * @brief Writes block(s) to a specified address in a card. The Data transfer
1302 * is managed by DMA mode.
1303 * @note This API should be followed by a check on the card state through
1304 * HAL_SD_GetCardState().
1305 * @note You could also check the DMA transfer process through the SD Tx
1307 * @param hsd: Pointer to SD handle
1308 * @param pData: Pointer to the buffer that will contain the data to transmit
1309 * @param BlockAdd: Block Address where data will be written
1310 * @param NumberOfBlocks: Number of blocks to write
1311 * @retval HAL status
1313 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1315 SDIO_DataInitTypeDef config;
1316 uint32_t errorstate;
1317 uint32_t add = BlockAdd;
1321 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1325 if(hsd->State == HAL_SD_STATE_READY)
1327 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1329 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1331 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1335 if(NumberOfBlocks > 1U && hsd->SdCard.CardType == CARD_SDHC_SDXC)
1337 /* MM: Prepare for write */
1338 errorstate = SDMMC_CmdSetBlockCount(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd) << 16, NumberOfBlocks);
1339 if(errorstate != HAL_SD_ERROR_NONE)
1341 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1342 hsd->ErrorCode |= errorstate;
1343 hsd->State = HAL_SD_STATE_READY;
1348 hsd->State = HAL_SD_STATE_BUSY;
1350 /* Initialize data control register */
1351 hsd->Instance->DCTRL = 0U;
1353 /* Enable SD Error interrupts */
1354 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
1356 /* Set the DMA transfer complete callback */
1357 hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
1359 /* Set the DMA error callback */
1360 hsd->hdmatx->XferErrorCallback = SD_DMAError;
1362 /* Set the DMA Abort callback */
1363 hsd->hdmatx->XferAbortCallback = NULL;
1365 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1369 /* Set Block Size for Card */
1370 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1371 if(errorstate != HAL_SD_ERROR_NONE)
1373 /* Clear all the static flags */
1374 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1375 hsd->ErrorCode |= errorstate;
1376 hsd->State = HAL_SD_STATE_READY;
1381 /* Write Blocks in Polling mode */
1382 if(NumberOfBlocks > 1U)
1384 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1386 /* Write Multi Block command */
1387 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1391 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1393 /* Write Single Block command */
1394 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1396 if(errorstate != HAL_SD_ERROR_NONE)
1398 /* Clear all the static flags */
1399 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1400 hsd->ErrorCode |= errorstate;
1401 hsd->State = HAL_SD_STATE_READY;
1402 hsd->Context = SD_CONTEXT_NONE;
1406 /* Enable SDIO DMA transfer */
1407 // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
1409 /* Enable the DMA Channel */
1410 if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1412 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
1413 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1414 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1415 hsd->State = HAL_SD_STATE_READY;
1416 hsd->Context = SD_CONTEXT_NONE;
1421 /* Configure the SD DPSM (Data Path State Machine) */
1422 config.DataTimeOut = SDMMC_DATATIMEOUT;
1423 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1424 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1425 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
1426 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1427 config.DPSM = SDIO_DPSM_ENABLE;
1429 // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
1430 // data is just discarded before the dpsm is started.
1431 __HAL_SD_DMA_ENABLE();
1433 (void)SDIO_ConfigData(hsd->Instance, &config);
1445 * @brief Erases the specified memory area of the given SD card.
1446 * @note This API should be followed by a check on the card state through
1447 * HAL_SD_GetCardState().
1448 * @param hsd: Pointer to SD handle
1449 * @param BlockStartAdd: Start Block address
1450 * @param BlockEndAdd: End Block address
1451 * @retval HAL status
1453 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1455 uint32_t errorstate;
1456 uint32_t start_add = BlockStartAdd;
1457 uint32_t end_add = BlockEndAdd;
1459 if(hsd->State == HAL_SD_STATE_READY)
1461 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1463 if(end_add < start_add)
1465 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1469 if(end_add > (hsd->SdCard.LogBlockNbr))
1471 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1475 hsd->State = HAL_SD_STATE_BUSY;
1477 /* Check if the card command class supports erase command */
1478 if(((hsd->SdCard.Class) & SDIO_CCCC_ERASE) == 0U)
1480 /* Clear all the static flags */
1481 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1482 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1483 hsd->State = HAL_SD_STATE_READY;
1487 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1489 /* Clear all the static flags */
1490 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1491 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1492 hsd->State = HAL_SD_STATE_READY;
1496 /* Get start and end block for high capacity cards */
1497 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1503 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1504 if(hsd->SdCard.CardType != CARD_SECURED)
1506 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1507 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
1508 if(errorstate != HAL_SD_ERROR_NONE)
1510 /* Clear all the static flags */
1511 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1512 hsd->ErrorCode |= errorstate;
1513 hsd->State = HAL_SD_STATE_READY;
1517 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1518 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
1519 if(errorstate != HAL_SD_ERROR_NONE)
1521 /* Clear all the static flags */
1522 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1523 hsd->ErrorCode |= errorstate;
1524 hsd->State = HAL_SD_STATE_READY;
1529 /* Send CMD38 ERASE */
1530 errorstate = SDMMC_CmdErase(hsd->Instance);
1531 if(errorstate != HAL_SD_ERROR_NONE)
1533 /* Clear all the static flags */
1534 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1535 hsd->ErrorCode |= errorstate;
1536 hsd->State = HAL_SD_STATE_READY;
1540 hsd->State = HAL_SD_STATE_READY;
1551 * @brief This function handles SD card interrupt request.
1552 * @param hsd: Pointer to SD handle
1555 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1557 uint32_t errorstate;
1558 uint32_t context = hsd->Context;
1560 /* Check for SDIO interrupt flags */
1561 if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1566 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) != RESET)
1568 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DATAEND);
1570 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1571 SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR | SDIO_IT_TXFIFOHE |\
1574 hsd->Instance->DCTRL &= ~(SDIO_DCTRL_DTEN);
1576 if((context & SD_CONTEXT_IT) != 0U)
1578 if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1580 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1581 if(errorstate != HAL_SD_ERROR_NONE)
1583 hsd->ErrorCode |= errorstate;
1584 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1585 hsd->ErrorCallback(hsd);
1587 HAL_SD_ErrorCallback(hsd);
1588 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1592 /* Clear all the static flags */
1593 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
1595 hsd->State = HAL_SD_STATE_READY;
1596 hsd->Context = SD_CONTEXT_NONE;
1597 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1599 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1600 hsd->RxCpltCallback(hsd);
1602 HAL_SD_RxCpltCallback(hsd);
1603 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1607 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1608 hsd->TxCpltCallback(hsd);
1610 HAL_SD_TxCpltCallback(hsd);
1611 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1614 else if((context & SD_CONTEXT_DMA) != 0U)
1616 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1618 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1619 if(errorstate != HAL_SD_ERROR_NONE)
1621 hsd->ErrorCode |= errorstate;
1622 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1623 hsd->ErrorCallback(hsd);
1625 HAL_SD_ErrorCallback(hsd);
1626 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1628 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
1630 hsd->State = HAL_SD_STATE_READY;
1631 hsd->Context = SD_CONTEXT_NONE;
1633 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
1635 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1636 in the SD DCTRL register */
1637 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
1639 hsd->State = HAL_SD_STATE_READY;
1641 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1642 hsd->TxCpltCallback(hsd);
1644 HAL_SD_TxCpltCallback(hsd);
1645 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1654 else if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1659 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR) != RESET)
1661 /* Set Error code */
1662 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL) != RESET)
1664 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1666 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT) != RESET)
1668 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1670 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR) != RESET)
1672 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1674 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR) != RESET)
1676 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1679 /* Clear All flags */
1680 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS | SDIO_FLAG_STBITERR);
1682 /* Disable all interrupts */
1683 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1684 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
1686 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
1688 if((context & SD_CONTEXT_IT) != 0U)
1690 /* Set the SD state to ready to be able to start again the process */
1691 hsd->State = HAL_SD_STATE_READY;
1692 hsd->Context = SD_CONTEXT_NONE;
1693 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1694 hsd->ErrorCallback(hsd);
1696 HAL_SD_ErrorCallback(hsd);
1697 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1699 else if((context & SD_CONTEXT_DMA) != 0U)
1701 /* Abort the SD DMA channel */
1702 if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1704 /* Set the DMA Tx abort callback */
1705 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
1706 /* Abort DMA in IT mode */
1707 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
1709 SD_DMATxAbort(hsd->hdmatx);
1712 else if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1714 /* Set the DMA Rx abort callback */
1715 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
1716 /* Abort DMA in IT mode */
1717 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
1719 SD_DMARxAbort(hsd->hdmarx);
1724 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1725 hsd->State = HAL_SD_STATE_READY;
1726 hsd->Context = SD_CONTEXT_NONE;
1727 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1728 hsd->AbortCpltCallback(hsd);
1730 HAL_SD_AbortCallback(hsd);
1731 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1746 * @brief return the SD state
1747 * @param hsd: Pointer to sd handle
1750 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
1756 * @brief Return the SD error code
1757 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
1758 * the configuration information.
1759 * @retval SD Error Code
1761 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
1763 return hsd->ErrorCode;
1767 * @brief Tx Transfer completed callbacks
1768 * @param hsd: Pointer to SD handle
1771 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
1773 /* Prevent unused argument(s) compilation warning */
1776 /* NOTE : This function should not be modified, when the callback is needed,
1777 the HAL_SD_TxCpltCallback can be implemented in the user file
1782 * @brief Rx Transfer completed callbacks
1783 * @param hsd: Pointer SD handle
1786 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
1788 /* Prevent unused argument(s) compilation warning */
1791 /* NOTE : This function should not be modified, when the callback is needed,
1792 the HAL_SD_RxCpltCallback can be implemented in the user file
1797 * @brief SD error callbacks
1798 * @param hsd: Pointer SD handle
1801 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1803 /* Prevent unused argument(s) compilation warning */
1806 /* NOTE : This function should not be modified, when the callback is needed,
1807 the HAL_SD_ErrorCallback can be implemented in the user file
1812 * @brief SD Abort callbacks
1813 * @param hsd: Pointer SD handle
1816 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
1818 /* Prevent unused argument(s) compilation warning */
1821 /* NOTE : This function should not be modified, when the callback is needed,
1822 the HAL_SD_AbortCallback can be implemented in the user file
1826 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1828 * @brief Register a User SD Callback
1829 * To be used instead of the weak (surcharged) predefined callback
1830 * @param hsd : SD handle
1831 * @param CallbackID : ID of the callback to be registered
1832 * This parameter can be one of the following values:
1833 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1834 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1835 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1836 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1837 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1838 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1839 * @param pCallback : pointer to the Callback function
1842 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
1844 HAL_StatusTypeDef status = HAL_OK;
1846 if(pCallback == NULL)
1848 /* Update the error code */
1849 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1853 /* Process locked */
1856 if(hsd->State == HAL_SD_STATE_READY)
1860 case HAL_SD_TX_CPLT_CB_ID :
1861 hsd->TxCpltCallback = pCallback;
1863 case HAL_SD_RX_CPLT_CB_ID :
1864 hsd->RxCpltCallback = pCallback;
1866 case HAL_SD_ERROR_CB_ID :
1867 hsd->ErrorCallback = pCallback;
1869 case HAL_SD_ABORT_CB_ID :
1870 hsd->AbortCpltCallback = pCallback;
1872 case HAL_SD_MSP_INIT_CB_ID :
1873 hsd->MspInitCallback = pCallback;
1875 case HAL_SD_MSP_DEINIT_CB_ID :
1876 hsd->MspDeInitCallback = pCallback;
1879 /* Update the error code */
1880 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1881 /* update return status */
1886 else if (hsd->State == HAL_SD_STATE_RESET)
1890 case HAL_SD_MSP_INIT_CB_ID :
1891 hsd->MspInitCallback = pCallback;
1893 case HAL_SD_MSP_DEINIT_CB_ID :
1894 hsd->MspDeInitCallback = pCallback;
1897 /* Update the error code */
1898 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1899 /* update return status */
1906 /* Update the error code */
1907 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1908 /* update return status */
1918 * @brief Unregister a User SD Callback
1919 * SD Callback is redirected to the weak (surcharged) predefined callback
1920 * @param hsd : SD handle
1921 * @param CallbackID : ID of the callback to be unregistered
1922 * This parameter can be one of the following values:
1923 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1924 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1925 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1926 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1927 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1928 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1931 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
1933 HAL_StatusTypeDef status = HAL_OK;
1935 /* Process locked */
1938 if(hsd->State == HAL_SD_STATE_READY)
1942 case HAL_SD_TX_CPLT_CB_ID :
1943 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
1945 case HAL_SD_RX_CPLT_CB_ID :
1946 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
1948 case HAL_SD_ERROR_CB_ID :
1949 hsd->ErrorCallback = HAL_SD_ErrorCallback;
1951 case HAL_SD_ABORT_CB_ID :
1952 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
1954 case HAL_SD_MSP_INIT_CB_ID :
1955 hsd->MspInitCallback = HAL_SD_MspInit;
1957 case HAL_SD_MSP_DEINIT_CB_ID :
1958 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1961 /* Update the error code */
1962 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1963 /* update return status */
1968 else if (hsd->State == HAL_SD_STATE_RESET)
1972 case HAL_SD_MSP_INIT_CB_ID :
1973 hsd->MspInitCallback = HAL_SD_MspInit;
1975 case HAL_SD_MSP_DEINIT_CB_ID :
1976 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1979 /* Update the error code */
1980 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1981 /* update return status */
1988 /* Update the error code */
1989 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1990 /* update return status */
1998 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2004 /** @addtogroup SD_Exported_Functions_Group3
2005 * @brief management functions
2008 ==============================================================================
2009 ##### Peripheral Control functions #####
2010 ==============================================================================
2012 This subsection provides a set of functions allowing to control the SD card
2013 operations and get the related information
2020 * @brief Returns information the information of the card which are stored on
2022 * @param hsd: Pointer to SD handle
2023 * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that
2024 * contains all CID register parameters
2025 * @retval HAL status
2027 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
2029 pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
2031 pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
2033 pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
2035 pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
2037 pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
2039 pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
2041 pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
2043 pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
2045 pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
2047 pCID->Reserved2 = 1U;
2053 * @brief Returns information the information of the card which are stored on
2055 * @param hsd: Pointer to SD handle
2056 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
2057 * contains all CSD register parameters
2058 * @retval HAL status
2060 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
2062 pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
2064 pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
2066 pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
2068 pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
2070 pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
2072 pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
2074 pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
2076 pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
2078 pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
2080 pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
2082 pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
2084 pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
2086 pCSD->Reserved2 = 0U; /*!< Reserved */
2088 if(hsd->SdCard.CardType == CARD_SDSC)
2090 pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
2092 pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
2094 pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
2096 pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
2098 pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
2100 pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
2102 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2103 hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2104 hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2106 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
2107 hsd->SdCard.LogBlockSize = 512U;
2109 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
2112 pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
2114 hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
2115 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
2116 hsd->SdCard.BlockSize = 512U;
2117 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
2121 /* Clear all the static flags */
2122 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2123 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2124 hsd->State = HAL_SD_STATE_READY;
2128 pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
2130 pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
2132 pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
2134 pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
2136 pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
2138 pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
2140 pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
2142 pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
2144 pCSD->Reserved3 = 0;
2146 pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
2148 pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
2150 pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
2152 pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
2154 pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
2156 pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
2158 pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
2160 pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
2162 pCSD->Reserved4 = 1;
2168 * @brief Gets the SD status info.
2169 * @param hsd: Pointer to SD handle
2170 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
2171 * will contain the SD card status information
2172 * @retval HAL status
2174 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
2176 uint32_t sd_status[16];
2177 uint32_t errorstate;
2179 errorstate = SD_SendSDStatus(hsd, sd_status);
2180 if(errorstate != HAL_SD_ERROR_NONE)
2182 /* Clear all the static flags */
2183 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2184 hsd->ErrorCode |= errorstate;
2185 hsd->State = HAL_SD_STATE_READY;
2190 pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
2192 pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
2194 pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
2196 pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
2197 ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
2199 pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
2201 pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
2203 pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
2205 pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
2207 pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
2209 pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
2216 * @brief Gets the SD card info.
2217 * @param hsd: Pointer to SD handle
2218 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
2219 * will contain the SD card status information
2220 * @retval HAL status
2222 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
2224 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
2225 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
2226 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
2227 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
2228 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
2229 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
2230 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
2231 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
2237 * @brief Enables wide bus operation for the requested card if supported by
2239 * @param hsd: Pointer to SD handle
2240 * @param WideMode: Specifies the SD card wide bus mode
2241 * This parameter can be one of the following values:
2242 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
2243 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
2244 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
2245 * @retval HAL status
2247 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
2249 SDIO_InitTypeDef Init;
2250 uint32_t errorstate;
2252 /* Check the parameters */
2253 assert_param(IS_SDIO_BUS_WIDE(WideMode));
2256 hsd->State = HAL_SD_STATE_BUSY;
2258 if(hsd->SdCard.CardType != CARD_SECURED)
2260 if(WideMode == SDIO_BUS_WIDE_8B)
2262 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2264 else if(WideMode == SDIO_BUS_WIDE_4B)
2266 errorstate = SD_WideBus_Enable(hsd);
2268 hsd->ErrorCode |= errorstate;
2270 else if(WideMode == SDIO_BUS_WIDE_1B)
2272 errorstate = SD_WideBus_Disable(hsd);
2274 hsd->ErrorCode |= errorstate;
2278 /* WideMode is not a valid argument*/
2279 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2284 /* MMC Card does not support this feature */
2285 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2288 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2290 /* Clear all the static flags */
2291 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2292 hsd->State = HAL_SD_STATE_READY;
2297 /* Configure the SDIO peripheral */
2298 Init.ClockEdge = hsd->Init.ClockEdge;
2299 Init.ClockBypass = hsd->Init.ClockBypass;
2300 Init.ClockPowerSave = hsd->Init.ClockPowerSave;
2301 Init.BusWide = WideMode;
2302 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2303 Init.ClockDiv = hsd->Init.ClockDiv;
2304 (void)SDIO_Init(hsd->Instance, Init);
2308 hsd->State = HAL_SD_STATE_READY;
2314 * @brief Gets the current sd card data state.
2315 * @param hsd: pointer to SD handle
2316 * @retval Card state
2318 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2321 uint32_t errorstate;
2324 errorstate = SD_SendStatus(hsd, &resp1);
2325 if(errorstate != HAL_SD_ERROR_NONE)
2327 hsd->ErrorCode |= errorstate;
2330 cardstate = ((resp1 >> 9U) & 0x0FU);
2332 return (HAL_SD_CardStateTypeDef)cardstate;
2336 * @brief Abort the current transfer and disable the SD.
2337 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2338 * the configuration information for SD module.
2339 * @retval HAL status
2341 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
2343 HAL_SD_CardStateTypeDef CardState;
2344 uint32_t context = hsd->Context;
2346 /* DIsable All interrupts */
2347 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2348 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2350 /* Clear All flags */
2351 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2353 CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
2355 if ((context & SD_CONTEXT_DMA) != 0U)
2357 /* Disable the SD DMA request */
2358 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2360 /* Abort the SD DMA Tx channel */
2361 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2363 if(HAL_DMA_Abort(hsd->hdmatx) != HAL_OK)
2365 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2368 /* Abort the SD DMA Rx channel */
2369 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2371 if(HAL_DMA_Abort(hsd->hdmarx) != HAL_OK)
2373 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2382 hsd->State = HAL_SD_STATE_READY;
2384 /* Initialize the SD operation */
2385 hsd->Context = SD_CONTEXT_NONE;
2387 CardState = HAL_SD_GetCardState(hsd);
2388 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2390 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2392 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2400 * @brief Abort the current transfer and disable the SD (IT mode).
2401 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2402 * the configuration information for SD module.
2403 * @retval HAL status
2405 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
2407 HAL_SD_CardStateTypeDef CardState;
2408 uint32_t context = hsd->Context;
2410 /* Disable All interrupts */
2411 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2412 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2414 CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
2416 if ((context & SD_CONTEXT_DMA) != 0U)
2418 /* Disable the SD DMA request */
2419 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2421 /* Abort the SD DMA Tx channel */
2422 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2424 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
2425 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
2430 /* Abort the SD DMA Rx channel */
2431 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2433 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
2434 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
2444 /* No transfer ongoing on both DMA channels*/
2447 /* Clear All flags */
2448 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2450 CardState = HAL_SD_GetCardState(hsd);
2451 hsd->State = HAL_SD_STATE_READY;
2452 hsd->Context = SD_CONTEXT_NONE;
2453 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2455 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2457 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2463 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2464 hsd->AbortCpltCallback(hsd);
2466 HAL_SD_AbortCallback(hsd);
2467 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2482 /* Private function ----------------------------------------------------------*/
2483 /** @addtogroup SD_Private_Functions
2488 * @brief DMA SD transmit process complete callback
2489 * @param hdma: DMA handle
2492 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2494 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2496 /* Enable DATAEND Interrupt */
2497 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND));
2501 * @brief DMA SD receive process complete callback
2502 * @param hdma: DMA handle
2505 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2507 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2508 uint32_t errorstate;
2510 /* Send stop command in multiblock write */
2511 if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
2513 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
2514 if(errorstate != HAL_SD_ERROR_NONE)
2516 hsd->ErrorCode |= errorstate;
2517 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2518 hsd->ErrorCallback(hsd);
2520 HAL_SD_ErrorCallback(hsd);
2525 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2526 in the SD DCTRL register */
2527 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2529 /* Clear all the static flags */
2530 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2532 hsd->State = HAL_SD_STATE_READY;
2533 hsd->Context = SD_CONTEXT_NONE;
2535 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2536 hsd->RxCpltCallback(hsd);
2538 HAL_SD_RxCpltCallback(hsd);
2543 * @brief DMA SD communication error callback
2544 * @param hdma: DMA handle
2547 static void SD_DMAError(DMA_HandleTypeDef *hdma)
2549 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2550 HAL_SD_CardStateTypeDef CardState;
2551 uint32_t RxErrorCode, TxErrorCode;
2553 /* if DMA error is FIFO error ignore it */
2554 if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
2556 RxErrorCode = hsd->hdmarx->ErrorCode;
2557 TxErrorCode = hsd->hdmatx->ErrorCode;
2558 if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
2560 /* Clear All flags */
2561 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2563 /* Disable All interrupts */
2564 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2565 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2567 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2568 CardState = HAL_SD_GetCardState(hsd);
2569 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2571 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2574 hsd->State= HAL_SD_STATE_READY;
2575 hsd->Context = SD_CONTEXT_NONE;
2578 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2579 hsd->ErrorCallback(hsd);
2581 HAL_SD_ErrorCallback(hsd);
2587 * @brief DMA SD Tx Abort callback
2588 * @param hdma: DMA handle
2591 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
2593 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2594 HAL_SD_CardStateTypeDef CardState;
2596 /* Clear All flags */
2597 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2599 CardState = HAL_SD_GetCardState(hsd);
2600 hsd->State = HAL_SD_STATE_READY;
2601 hsd->Context = SD_CONTEXT_NONE;
2602 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2604 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2607 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2609 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2610 hsd->AbortCpltCallback(hsd);
2612 HAL_SD_AbortCallback(hsd);
2617 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2618 hsd->ErrorCallback(hsd);
2620 HAL_SD_ErrorCallback(hsd);
2626 * @brief DMA SD Rx Abort callback
2627 * @param hdma: DMA handle
2630 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
2632 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2633 HAL_SD_CardStateTypeDef CardState;
2635 /* Clear All flags */
2636 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2638 CardState = HAL_SD_GetCardState(hsd);
2639 hsd->State = HAL_SD_STATE_READY;
2640 hsd->Context = SD_CONTEXT_NONE;
2641 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2643 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2646 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2648 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2649 hsd->AbortCpltCallback(hsd);
2651 HAL_SD_AbortCallback(hsd);
2656 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2657 hsd->ErrorCallback(hsd);
2659 HAL_SD_ErrorCallback(hsd);
2665 * @brief Initializes the sd card.
2666 * @param hsd: Pointer to SD handle
2667 * @retval SD Card error state
2669 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
2671 HAL_SD_CardCSDTypeDef CSD;
2672 uint32_t errorstate;
2673 uint16_t sd_rca = 1U;
2675 /* Check the power State */
2676 if(SDIO_GetPowerState(hsd->Instance) == 0U)
2679 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2682 if(hsd->SdCard.CardType != CARD_SECURED)
2684 /* Send CMD2 ALL_SEND_CID */
2685 errorstate = SDMMC_CmdSendCID(hsd->Instance);
2686 if(errorstate != HAL_SD_ERROR_NONE)
2692 /* Get Card identification number data */
2693 hsd->CID[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2694 hsd->CID[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2695 hsd->CID[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2696 hsd->CID[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2700 if(hsd->SdCard.CardType != CARD_SECURED)
2702 /* Send CMD3 SET_REL_ADDR with argument 0 */
2703 /* SD Card publishes its RCA. */
2704 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
2705 if(errorstate != HAL_SD_ERROR_NONE)
2710 if(hsd->SdCard.CardType != CARD_SECURED)
2712 /* Get the SD card RCA */
2713 hsd->SdCard.RelCardAdd = sd_rca;
2715 /* Send CMD9 SEND_CSD with argument as card's RCA */
2716 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2717 if(errorstate != HAL_SD_ERROR_NONE)
2723 /* Get Card Specific Data */
2724 hsd->CSD[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2725 hsd->CSD[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2726 hsd->CSD[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2727 hsd->CSD[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2731 /* Get the Card Class */
2732 hsd->SdCard.Class = (SDIO_GetResponse(hsd->Instance, SDIO_RESP2) >> 20U);
2734 /* Get CSD parameters */
2735 if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
2737 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2740 /* Select the Card */
2741 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
2742 if(errorstate != HAL_SD_ERROR_NONE)
2747 /* Configure SDIO peripheral interface */
2748 (void)SDIO_Init(hsd->Instance, hsd->Init);
2750 /* All cards are initialized */
2751 return HAL_SD_ERROR_NONE;
2755 * @brief Enquires cards about their operating voltage and configures clock
2756 * controls and stores SD information that will be needed in future
2758 * @param hsd: Pointer to SD handle
2759 * @retval error state
2761 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
2763 __IO uint32_t count = 0U;
2764 uint32_t response = 0U, validvoltage = 0U;
2765 uint32_t errorstate;
2767 /* CMD0: GO_IDLE_STATE */
2768 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2769 if(errorstate != HAL_SD_ERROR_NONE)
2774 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2775 errorstate = SDMMC_CmdOperCond(hsd->Instance);
2776 if(errorstate != HAL_SD_ERROR_NONE)
2778 hsd->SdCard.CardVersion = CARD_V1_X;
2779 /* CMD0: GO_IDLE_STATE */
2780 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2781 if(errorstate != HAL_SD_ERROR_NONE)
2789 hsd->SdCard.CardVersion = CARD_V2_X;
2792 if( hsd->SdCard.CardVersion == CARD_V2_X)
2794 /* SEND CMD55 APP_CMD with RCA as 0 */
2795 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2796 if(errorstate != HAL_SD_ERROR_NONE)
2798 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2802 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2803 while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
2805 /* SEND CMD55 APP_CMD with RCA as 0 */
2806 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2807 if(errorstate != HAL_SD_ERROR_NONE)
2813 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
2814 if(errorstate != HAL_SD_ERROR_NONE)
2816 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2819 /* Get command response */
2820 response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2822 /* Get operating voltage*/
2823 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
2828 if(count >= SDMMC_MAX_VOLT_TRIAL)
2830 return HAL_SD_ERROR_INVALID_VOLTRANGE;
2833 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
2835 hsd->SdCard.CardType = CARD_SDHC_SDXC;
2839 hsd->SdCard.CardType = CARD_SDSC;
2843 return HAL_SD_ERROR_NONE;
2847 * @brief Turns the SDIO output signals off.
2848 * @param hsd: Pointer to SD handle
2851 static void SD_PowerOFF(SD_HandleTypeDef *hsd)
2853 /* Set Power State to OFF */
2854 (void)SDIO_PowerState_OFF(hsd->Instance);
2858 * @brief Send Status info command.
2859 * @param hsd: pointer to SD handle
2860 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
2861 * SD Status register)
2862 * @retval error state
2864 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
2866 SDIO_DataInitTypeDef config;
2867 uint32_t errorstate;
2868 uint32_t tickstart = HAL_GetTick();
2870 uint32_t *pData = pSDstatus;
2872 /* Check SD response */
2873 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2875 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2878 /* Set block size for card if it is not equal to current block size for card */
2879 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
2880 if(errorstate != HAL_SD_ERROR_NONE)
2882 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2887 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2888 if(errorstate != HAL_SD_ERROR_NONE)
2890 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2894 /* Configure the SD DPSM (Data Path State Machine) */
2895 config.DataTimeOut = SDMMC_DATATIMEOUT;
2896 config.DataLength = 64U;
2897 config.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
2898 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
2899 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
2900 config.DPSM = SDIO_DPSM_ENABLE;
2901 (void)SDIO_ConfigData(hsd->Instance, &config);
2903 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
2904 errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
2905 if(errorstate != HAL_SD_ERROR_NONE)
2907 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2911 /* Get status data */
2912 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
2914 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
2916 for(count = 0U; count < 8U; count++)
2918 *pData = SDIO_ReadFIFO(hsd->Instance);
2923 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2925 return HAL_SD_ERROR_TIMEOUT;
2929 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
2931 return HAL_SD_ERROR_DATA_TIMEOUT;
2933 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
2935 return HAL_SD_ERROR_DATA_CRC_FAIL;
2937 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
2939 return HAL_SD_ERROR_RX_OVERRUN;
2946 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)))
2948 *pData = SDIO_ReadFIFO(hsd->Instance);
2951 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2953 return HAL_SD_ERROR_TIMEOUT;
2957 /* Clear all the static status flags*/
2958 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2960 return HAL_SD_ERROR_NONE;
2964 * @brief Returns the current card's status.
2965 * @param hsd: Pointer to SD handle
2966 * @param pCardStatus: pointer to the buffer that will contain the SD card
2967 * status (Card Status register)
2968 * @retval error state
2970 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
2972 uint32_t errorstate;
2974 if(pCardStatus == NULL)
2976 return HAL_SD_ERROR_PARAM;
2979 /* Send Status command */
2980 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2981 if(errorstate != HAL_SD_ERROR_NONE)
2986 /* Get SD card status */
2987 *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2989 return HAL_SD_ERROR_NONE;
2993 * @brief Enables the SDIO wide bus mode.
2994 * @param hsd: pointer to SD handle
2995 * @retval error state
2997 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
2999 uint32_t scr[2U] = {0U, 0U};
3000 uint32_t errorstate;
3002 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3004 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3007 /* Get SCR Register */
3008 errorstate = SD_FindSCR(hsd, scr);
3009 if(errorstate != HAL_SD_ERROR_NONE)
3014 /* If requested card supports wide bus operation */
3015 if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
3017 /* Send CMD55 APP_CMD with argument as card's RCA.*/
3018 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3019 if(errorstate != HAL_SD_ERROR_NONE)
3024 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3025 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
3026 if(errorstate != HAL_SD_ERROR_NONE)
3031 return HAL_SD_ERROR_NONE;
3035 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3040 * @brief Disables the SDIO wide bus mode.
3041 * @param hsd: Pointer to SD handle
3042 * @retval error state
3044 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3046 uint32_t scr[2U] = {0U, 0U};
3047 uint32_t errorstate;
3049 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3051 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3054 /* Get SCR Register */
3055 errorstate = SD_FindSCR(hsd, scr);
3056 if(errorstate != HAL_SD_ERROR_NONE)
3061 /* If requested card supports 1 bit mode operation */
3062 if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
3064 /* Send CMD55 APP_CMD with argument as card's RCA */
3065 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3066 if(errorstate != HAL_SD_ERROR_NONE)
3071 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3072 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
3073 if(errorstate != HAL_SD_ERROR_NONE)
3078 return HAL_SD_ERROR_NONE;
3082 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3088 * @brief Finds the SD card SCR register value.
3089 * @param hsd: Pointer to SD handle
3090 * @param pSCR: pointer to the buffer that will contain the SCR value
3091 * @retval error state
3093 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
3095 SDIO_DataInitTypeDef config;
3096 uint32_t errorstate;
3097 uint32_t tickstart = HAL_GetTick();
3098 uint32_t index = 0U;
3099 uint32_t tempscr[2U] = {0U, 0U};
3100 uint32_t *scr = pSCR;
3102 /* Set Block Size To 8 Bytes */
3103 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
3104 if(errorstate != HAL_SD_ERROR_NONE)