2 ******************************************************************************
3 * @file stm32f4xx_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) 2017 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 "stm32f4xx_hal.h"
263 /** @addtogroup STM32F4xx_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 #if defined(SDIO_STA_STBITERR)
643 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
644 #else /* SDIO_STA_STBITERR not defined */
645 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
646 #endif /* SDIO_STA_STBITERR */
648 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) && (dataremaining > 0U))
650 /* Read data from SDIO Rx FIFO */
651 for(count = 0U; count < 8U; count++)
653 data = SDIO_ReadFIFO(hsd->Instance);
654 *tempbuff = (uint8_t)(data & 0xFFU);
657 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
660 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
663 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
669 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
671 /* Clear all the static flags */
672 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
673 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
674 hsd->State= HAL_SD_STATE_READY;
675 hsd->Context = SD_CONTEXT_NONE;
680 /* Send stop transmission command in case of multiblock read */
681 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
683 if(hsd->SdCard.CardType != CARD_SECURED)
685 /* Send stop transmission command */
686 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
687 if(errorstate != HAL_SD_ERROR_NONE)
689 /* Clear all the static flags */
690 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
691 hsd->ErrorCode |= errorstate;
692 hsd->State = HAL_SD_STATE_READY;
693 hsd->Context = SD_CONTEXT_NONE;
699 /* Get error state */
700 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
702 /* Clear all the static flags */
703 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
704 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
705 hsd->State = HAL_SD_STATE_READY;
706 hsd->Context = SD_CONTEXT_NONE;
709 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
711 /* Clear all the static flags */
712 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
713 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
714 hsd->State = HAL_SD_STATE_READY;
715 hsd->Context = SD_CONTEXT_NONE;
718 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
720 /* Clear all the static flags */
721 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
722 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
723 hsd->State = HAL_SD_STATE_READY;
724 hsd->Context = SD_CONTEXT_NONE;
732 /* Empty FIFO if there is still any data */
733 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (dataremaining > 0U))
735 data = SDIO_ReadFIFO(hsd->Instance);
736 *tempbuff = (uint8_t)(data & 0xFFU);
739 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
742 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
745 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
749 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
751 /* Clear all the static flags */
752 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
753 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
754 hsd->State= HAL_SD_STATE_READY;
755 hsd->Context = SD_CONTEXT_NONE;
760 /* Clear all the static flags */
761 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
763 hsd->State = HAL_SD_STATE_READY;
769 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
775 * @brief Allows to write block(s) to a specified address in a card. The Data
776 * transfer is managed by polling mode.
777 * @note This API should be followed by a check on the card state through
778 * HAL_SD_GetCardState().
779 * @param hsd: Pointer to SD handle
780 * @param pData: pointer to the buffer that will contain the data to transmit
781 * @param BlockAdd: Block Address where data will be written
782 * @param NumberOfBlocks: Number of SD blocks to write
783 * @param Timeout: Specify timeout value
786 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
788 SDIO_DataInitTypeDef config;
790 uint32_t tickstart = HAL_GetTick();
791 uint32_t count, data, dataremaining;
792 uint32_t add = BlockAdd;
793 uint8_t *tempbuff = pData;
797 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
801 if(hsd->State == HAL_SD_STATE_READY)
803 hsd->ErrorCode = HAL_SD_ERROR_NONE;
805 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
807 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
811 hsd->State = HAL_SD_STATE_BUSY;
813 /* Initialize data control register */
814 hsd->Instance->DCTRL = 0U;
816 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
821 /* Set Block Size for Card */
822 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
823 if(errorstate != HAL_SD_ERROR_NONE)
825 /* Clear all the static flags */
826 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
827 hsd->ErrorCode |= errorstate;
828 hsd->State = HAL_SD_STATE_READY;
832 /* Configure the SD DPSM (Data Path State Machine) */
833 config.DataTimeOut = SDMMC_DATATIMEOUT;
834 config.DataLength = NumberOfBlocks * BLOCKSIZE;
835 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
836 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
837 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
838 config.DPSM = SDIO_DPSM_ENABLE;
839 (void)SDIO_ConfigData(hsd->Instance, &config);
841 /* Write Blocks in Polling mode */
842 if(NumberOfBlocks > 1U)
844 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
846 /* Write Multi Block command */
847 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
851 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
853 /* Write Single Block command */
854 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
856 if(errorstate != HAL_SD_ERROR_NONE)
858 /* Clear all the static flags */
859 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
860 hsd->ErrorCode |= errorstate;
861 hsd->State = HAL_SD_STATE_READY;
862 hsd->Context = SD_CONTEXT_NONE;
866 /* Write block(s) in polling mode */
867 dataremaining = config.DataLength;
868 #if defined(SDIO_STA_STBITERR)
869 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
870 #else /* SDIO_STA_STBITERR not defined */
871 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
872 #endif /* SDIO_STA_STBITERR */
874 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) && (dataremaining > 0U))
876 /* Write data to SDIO Tx FIFO */
877 for(count = 0U; count < 8U; count++)
879 data = (uint32_t)(*tempbuff);
882 data |= ((uint32_t)(*tempbuff) << 8U);
885 data |= ((uint32_t)(*tempbuff) << 16U);
888 data |= ((uint32_t)(*tempbuff) << 24U);
891 (void)SDIO_WriteFIFO(hsd->Instance, &data);
895 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
897 /* Clear all the static flags */
898 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
899 hsd->ErrorCode |= errorstate;
900 hsd->State = HAL_SD_STATE_READY;
901 hsd->Context = SD_CONTEXT_NONE;
906 /* Send stop transmission command in case of multiblock write */
907 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
909 if(hsd->SdCard.CardType != CARD_SECURED)
911 /* Send stop transmission command */
912 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
913 if(errorstate != HAL_SD_ERROR_NONE)
915 /* Clear all the static flags */
916 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
917 hsd->ErrorCode |= errorstate;
918 hsd->State = HAL_SD_STATE_READY;
919 hsd->Context = SD_CONTEXT_NONE;
925 /* Get error state */
926 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
928 /* Clear all the static flags */
929 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
930 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
931 hsd->State = HAL_SD_STATE_READY;
932 hsd->Context = SD_CONTEXT_NONE;
935 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
937 /* Clear all the static flags */
938 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
939 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
940 hsd->State = HAL_SD_STATE_READY;
941 hsd->Context = SD_CONTEXT_NONE;
944 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
946 /* Clear all the static flags */
947 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
948 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
949 hsd->State = HAL_SD_STATE_READY;
950 hsd->Context = SD_CONTEXT_NONE;
958 /* Clear all the static flags */
959 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
961 hsd->State = HAL_SD_STATE_READY;
967 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
973 * @brief Reads block(s) from a specified address in a card. The Data transfer
974 * is managed in interrupt mode.
975 * @note This API should be followed by a check on the card state through
976 * HAL_SD_GetCardState().
977 * @note You could also check the IT transfer process through the SD Rx
979 * @param hsd: Pointer to SD handle
980 * @param pData: Pointer to the buffer that will contain the received data
981 * @param BlockAdd: Block Address from where data is to be read
982 * @param NumberOfBlocks: Number of blocks to read.
985 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
987 SDIO_DataInitTypeDef config;
989 uint32_t add = BlockAdd;
993 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
997 if(hsd->State == HAL_SD_STATE_READY)
999 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1001 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1003 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1007 hsd->State = HAL_SD_STATE_BUSY;
1009 /* Initialize data control register */
1010 hsd->Instance->DCTRL = 0U;
1012 hsd->pRxBuffPtr = pData;
1013 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1015 #if defined(SDIO_STA_STBITERR)
1016 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF | SDIO_IT_STBITERR));
1017 #else /* SDIO_STA_STBITERR not defined */
1018 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF));
1019 #endif /* SDIO_STA_STBITERR */
1021 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1026 /* Set Block Size for Card */
1027 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1028 if(errorstate != HAL_SD_ERROR_NONE)
1030 /* Clear all the static flags */
1031 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1032 hsd->ErrorCode |= errorstate;
1033 hsd->State = HAL_SD_STATE_READY;
1037 /* Configure the SD DPSM (Data Path State Machine) */
1038 config.DataTimeOut = SDMMC_DATATIMEOUT;
1039 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1040 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1041 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
1042 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1043 config.DPSM = SDIO_DPSM_ENABLE;
1044 (void)SDIO_ConfigData(hsd->Instance, &config);
1046 /* Read Blocks in IT mode */
1047 if(NumberOfBlocks > 1U)
1049 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1051 /* Read Multi Block command */
1052 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1056 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
1058 /* Read Single Block command */
1059 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1061 if(errorstate != HAL_SD_ERROR_NONE)
1063 /* Clear all the static flags */
1064 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1065 hsd->ErrorCode |= errorstate;
1066 hsd->State = HAL_SD_STATE_READY;
1067 hsd->Context = SD_CONTEXT_NONE;
1080 * @brief Writes block(s) to a specified address in a card. The Data transfer
1081 * is managed in interrupt mode.
1082 * @note This API should be followed by a check on the card state through
1083 * HAL_SD_GetCardState().
1084 * @note You could also check the IT transfer process through the SD Tx
1086 * @param hsd: Pointer to SD handle
1087 * @param pData: Pointer to the buffer that will contain the data to transmit
1088 * @param BlockAdd: Block Address where data will be written
1089 * @param NumberOfBlocks: Number of blocks to write
1090 * @retval HAL status
1092 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1094 SDIO_DataInitTypeDef config;
1095 uint32_t errorstate;
1096 uint32_t add = BlockAdd;
1100 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1104 if(hsd->State == HAL_SD_STATE_READY)
1106 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1108 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1110 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1114 hsd->State = HAL_SD_STATE_BUSY;
1116 /* Initialize data control register */
1117 hsd->Instance->DCTRL = 0U;
1119 hsd->pTxBuffPtr = pData;
1120 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1122 /* Enable transfer interrupts */
1123 #if defined(SDIO_STA_STBITERR)
1124 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_STBITERR));
1125 #else /* SDIO_STA_STBITERR not defined */
1126 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE));
1127 #endif /* SDIO_STA_STBITERR */
1129 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1134 /* Set Block Size for Card */
1135 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1136 if(errorstate != HAL_SD_ERROR_NONE)
1138 /* Clear all the static flags */
1139 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1140 hsd->ErrorCode |= errorstate;
1141 hsd->State = HAL_SD_STATE_READY;
1145 /* Write Blocks in Polling mode */
1146 if(NumberOfBlocks > 1U)
1148 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1150 /* Write Multi Block command */
1151 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1155 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1157 /* Write Single Block command */
1158 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1160 if(errorstate != HAL_SD_ERROR_NONE)
1162 /* Clear all the static flags */
1163 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1164 hsd->ErrorCode |= errorstate;
1165 hsd->State = HAL_SD_STATE_READY;
1166 hsd->Context = SD_CONTEXT_NONE;
1170 /* Configure the SD DPSM (Data Path State Machine) */
1171 config.DataTimeOut = SDMMC_DATATIMEOUT;
1172 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1173 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1174 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
1175 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1176 config.DPSM = SDIO_DPSM_ENABLE;
1177 (void)SDIO_ConfigData(hsd->Instance, &config);
1188 * @brief Reads block(s) from a specified address in a card. The Data transfer
1189 * is managed by DMA mode.
1190 * @note This API should be followed by a check on the card state through
1191 * HAL_SD_GetCardState().
1192 * @note You could also check the DMA transfer process through the SD Rx
1194 * @param hsd: Pointer SD handle
1195 * @param pData: Pointer to the buffer that will contain the received data
1196 * @param BlockAdd: Block Address from where data is to be read
1197 * @param NumberOfBlocks: Number of blocks to read.
1198 * @retval HAL status
1200 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1202 SDIO_DataInitTypeDef config;
1203 uint32_t errorstate;
1204 uint32_t add = BlockAdd;
1208 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1212 if(hsd->State == HAL_SD_STATE_READY)
1214 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1216 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1218 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1222 hsd->State = HAL_SD_STATE_BUSY;
1224 /* Initialize data control register */
1225 hsd->Instance->DCTRL = 0U;
1227 #if defined(SDIO_STA_STBITERR)
1228 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_IT_STBITERR));
1229 #else /* SDIO_STA_STBITERR not defined */
1230 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
1231 #endif /* SDIO_STA_STBITERR */
1233 /* Set the DMA transfer complete callback */
1234 hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
1236 /* Set the DMA error callback */
1237 hsd->hdmarx->XferErrorCallback = SD_DMAError;
1239 /* Set the DMA Abort callback */
1240 hsd->hdmarx->XferAbortCallback = NULL;
1242 /* Enable the DMA Channel */
1243 if(HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1245 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
1246 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1247 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1248 hsd->State = HAL_SD_STATE_READY;
1253 /* Enable SD DMA transfer */
1254 // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
1256 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1260 /* Set Block Size for Card */
1261 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1262 if(errorstate != HAL_SD_ERROR_NONE)
1264 /* Clear all the static flags */
1265 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1266 hsd->ErrorCode |= errorstate;
1267 hsd->State = HAL_SD_STATE_READY;
1272 /* Configure the SD DPSM (Data Path State Machine) */
1273 config.DataTimeOut = SDMMC_DATATIMEOUT;
1274 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1275 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1276 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
1277 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1278 config.DPSM = SDIO_DPSM_ENABLE;
1280 // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
1281 // data is just discarded before the dpsm is started.
1282 __HAL_SD_DMA_ENABLE();
1284 (void)SDIO_ConfigData(hsd->Instance, &config);
1286 /* Read Blocks in DMA mode */
1287 if(NumberOfBlocks > 1U)
1289 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1291 /* Read Multi Block command */
1292 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1296 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1298 /* Read Single Block command */
1299 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1301 if(errorstate != HAL_SD_ERROR_NONE)
1303 /* Clear all the static flags */
1304 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1305 hsd->ErrorCode |= errorstate;
1306 hsd->State = HAL_SD_STATE_READY;
1307 hsd->Context = SD_CONTEXT_NONE;
1321 * @brief Writes block(s) to a specified address in a card. The Data transfer
1322 * is managed by DMA mode.
1323 * @note This API should be followed by a check on the card state through
1324 * HAL_SD_GetCardState().
1325 * @note You could also check the DMA transfer process through the SD Tx
1327 * @param hsd: Pointer to SD handle
1328 * @param pData: Pointer to the buffer that will contain the data to transmit
1329 * @param BlockAdd: Block Address where data will be written
1330 * @param NumberOfBlocks: Number of blocks to write
1331 * @retval HAL status
1333 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1335 SDIO_DataInitTypeDef config;
1336 uint32_t errorstate;
1337 uint32_t add = BlockAdd;
1341 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1345 if(hsd->State == HAL_SD_STATE_READY)
1347 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1349 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1351 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1355 if(NumberOfBlocks > 1U && hsd->SdCard.CardType == CARD_SDHC_SDXC)
1357 /* MM: Prepare for write */
1358 errorstate = SDMMC_CmdSetBlockCount(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd) << 16, NumberOfBlocks);
1359 if(errorstate != HAL_SD_ERROR_NONE)
1361 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1362 hsd->ErrorCode |= errorstate;
1363 hsd->State = HAL_SD_STATE_READY;
1368 hsd->State = HAL_SD_STATE_BUSY;
1370 /* Initialize data control register */
1371 hsd->Instance->DCTRL = 0U;
1373 /* Enable SD Error interrupts */
1374 #if defined(SDIO_STA_STBITERR)
1375 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));
1376 #else /* SDIO_STA_STBITERR not defined */
1377 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
1378 #endif /* SDIO_STA_STBITERR */
1380 /* Set the DMA transfer complete callback */
1381 hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
1383 /* Set the DMA error callback */
1384 hsd->hdmatx->XferErrorCallback = SD_DMAError;
1386 /* Set the DMA Abort callback */
1387 hsd->hdmatx->XferAbortCallback = NULL;
1389 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1393 /* Set Block Size for Card */
1394 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1395 if(errorstate != HAL_SD_ERROR_NONE)
1397 /* Clear all the static flags */
1398 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1399 hsd->ErrorCode |= errorstate;
1400 hsd->State = HAL_SD_STATE_READY;
1405 /* Write Blocks in Polling mode */
1406 if(NumberOfBlocks > 1U)
1408 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1410 /* Write Multi Block command */
1411 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1415 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1417 /* Write Single Block command */
1418 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1420 if(errorstate != HAL_SD_ERROR_NONE)
1422 /* Clear all the static flags */
1423 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1424 hsd->ErrorCode |= errorstate;
1425 hsd->State = HAL_SD_STATE_READY;
1426 hsd->Context = SD_CONTEXT_NONE;
1430 /* Enable SDIO DMA transfer */
1431 // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
1433 /* Enable the DMA Channel */
1434 if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1436 #if defined(SDIO_STA_STBITERR)
1437 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));
1438 #else /* SDIO_STA_STBITERR not defined */
1439 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
1440 #endif /* SDIO_STA_STBITERR */
1441 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1442 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1443 hsd->State = HAL_SD_STATE_READY;
1444 hsd->Context = SD_CONTEXT_NONE;
1449 /* Configure the SD DPSM (Data Path State Machine) */
1450 config.DataTimeOut = SDMMC_DATATIMEOUT;
1451 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1452 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1453 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
1454 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1455 config.DPSM = SDIO_DPSM_ENABLE;
1457 // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
1458 // data is just discarded before the dpsm is started.
1459 __HAL_SD_DMA_ENABLE();
1461 (void)SDIO_ConfigData(hsd->Instance, &config);
1473 * @brief Erases the specified memory area of the given SD card.
1474 * @note This API should be followed by a check on the card state through
1475 * HAL_SD_GetCardState().
1476 * @param hsd: Pointer to SD handle
1477 * @param BlockStartAdd: Start Block address
1478 * @param BlockEndAdd: End Block address
1479 * @retval HAL status
1481 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1483 uint32_t errorstate;
1484 uint32_t start_add = BlockStartAdd;
1485 uint32_t end_add = BlockEndAdd;
1487 if(hsd->State == HAL_SD_STATE_READY)
1489 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1491 if(end_add < start_add)
1493 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1497 if(end_add > (hsd->SdCard.LogBlockNbr))
1499 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1503 hsd->State = HAL_SD_STATE_BUSY;
1505 /* Check if the card command class supports erase command */
1506 if(((hsd->SdCard.Class) & SDIO_CCCC_ERASE) == 0U)
1508 /* Clear all the static flags */
1509 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1510 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1511 hsd->State = HAL_SD_STATE_READY;
1515 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1517 /* Clear all the static flags */
1518 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1519 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1520 hsd->State = HAL_SD_STATE_READY;
1524 /* Get start and end block for high capacity cards */
1525 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1531 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1532 if(hsd->SdCard.CardType != CARD_SECURED)
1534 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1535 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
1536 if(errorstate != HAL_SD_ERROR_NONE)
1538 /* Clear all the static flags */
1539 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1540 hsd->ErrorCode |= errorstate;
1541 hsd->State = HAL_SD_STATE_READY;
1545 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1546 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
1547 if(errorstate != HAL_SD_ERROR_NONE)
1549 /* Clear all the static flags */
1550 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1551 hsd->ErrorCode |= errorstate;
1552 hsd->State = HAL_SD_STATE_READY;
1557 /* Send CMD38 ERASE */
1558 errorstate = SDMMC_CmdErase(hsd->Instance);
1559 if(errorstate != HAL_SD_ERROR_NONE)
1561 /* Clear all the static flags */
1562 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1563 hsd->ErrorCode |= errorstate;
1564 hsd->State = HAL_SD_STATE_READY;
1568 hsd->State = HAL_SD_STATE_READY;
1579 * @brief This function handles SD card interrupt request.
1580 * @param hsd: Pointer to SD handle
1583 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1585 uint32_t errorstate;
1586 uint32_t context = hsd->Context;
1588 /* Check for SDIO interrupt flags */
1589 if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1594 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) != RESET)
1596 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DATAEND);
1598 #if defined(SDIO_STA_STBITERR)
1599 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1600 SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR | SDIO_IT_TXFIFOHE |\
1601 SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR);
1602 #else /* SDIO_STA_STBITERR not defined */
1603 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1604 SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR | SDIO_IT_TXFIFOHE |\
1606 #endif /* SDIO_STA_STBITERR */
1608 hsd->Instance->DCTRL &= ~(SDIO_DCTRL_DTEN);
1610 if((context & SD_CONTEXT_IT) != 0U)
1612 if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1614 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1615 if(errorstate != HAL_SD_ERROR_NONE)
1617 hsd->ErrorCode |= errorstate;
1618 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1619 hsd->ErrorCallback(hsd);
1621 HAL_SD_ErrorCallback(hsd);
1622 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1626 /* Clear all the static flags */
1627 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
1629 hsd->State = HAL_SD_STATE_READY;
1630 hsd->Context = SD_CONTEXT_NONE;
1631 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1633 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1634 hsd->RxCpltCallback(hsd);
1636 HAL_SD_RxCpltCallback(hsd);
1637 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
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 */
1648 else if((context & SD_CONTEXT_DMA) != 0U)
1650 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1652 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1653 if(errorstate != HAL_SD_ERROR_NONE)
1655 hsd->ErrorCode |= errorstate;
1656 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1657 hsd->ErrorCallback(hsd);
1659 HAL_SD_ErrorCallback(hsd);
1660 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1662 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
1664 hsd->State = HAL_SD_STATE_READY;
1665 hsd->Context = SD_CONTEXT_NONE;
1667 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
1669 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1670 in the SD DCTRL register */
1671 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
1673 hsd->State = HAL_SD_STATE_READY;
1675 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1676 hsd->TxCpltCallback(hsd);
1678 HAL_SD_TxCpltCallback(hsd);
1679 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1688 else if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1693 #if defined(SDIO_STA_STBITERR)
1694 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR | SDIO_FLAG_STBITERR) != RESET)
1695 #else /* SDIO_STA_STBITERR not defined */
1696 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR) != RESET)
1697 #endif /* SDIO_STA_STBITERR */
1699 /* Set Error code */
1700 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL) != RESET)
1702 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1704 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT) != RESET)
1706 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1708 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR) != RESET)
1710 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1712 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR) != RESET)
1714 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1716 #if defined(SDIO_STA_STBITERR)
1717 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_STBITERR) != RESET)
1719 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1721 #endif /* SDIO_STA_STBITERR */
1723 #if defined(SDIO_STA_STBITERR)
1724 /* Clear All flags */
1725 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS | SDIO_FLAG_STBITERR);
1727 /* Disable all interrupts */
1728 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1729 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
1730 #else /* SDIO_STA_STBITERR not defined */
1731 /* Clear All flags */
1732 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
1734 /* Disable all interrupts */
1735 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1736 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
1737 #endif /* SDIO_STA_STBITERR */
1739 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
1741 if((context & SD_CONTEXT_IT) != 0U)
1743 /* Set the SD state to ready to be able to start again the process */
1744 hsd->State = HAL_SD_STATE_READY;
1745 hsd->Context = SD_CONTEXT_NONE;
1746 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1747 hsd->ErrorCallback(hsd);
1749 HAL_SD_ErrorCallback(hsd);
1750 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1752 else if((context & SD_CONTEXT_DMA) != 0U)
1754 /* Abort the SD DMA channel */
1755 if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1757 /* Set the DMA Tx abort callback */
1758 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
1759 /* Abort DMA in IT mode */
1760 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
1762 SD_DMATxAbort(hsd->hdmatx);
1765 else if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1767 /* Set the DMA Rx abort callback */
1768 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
1769 /* Abort DMA in IT mode */
1770 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
1772 SD_DMARxAbort(hsd->hdmarx);
1777 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1778 hsd->State = HAL_SD_STATE_READY;
1779 hsd->Context = SD_CONTEXT_NONE;
1780 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1781 hsd->AbortCpltCallback(hsd);
1783 HAL_SD_AbortCallback(hsd);
1784 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1799 * @brief return the SD state
1800 * @param hsd: Pointer to sd handle
1803 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
1809 * @brief Return the SD error code
1810 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
1811 * the configuration information.
1812 * @retval SD Error Code
1814 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
1816 return hsd->ErrorCode;
1820 * @brief Tx Transfer completed callbacks
1821 * @param hsd: Pointer to SD handle
1824 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
1826 /* Prevent unused argument(s) compilation warning */
1829 /* NOTE : This function should not be modified, when the callback is needed,
1830 the HAL_SD_TxCpltCallback can be implemented in the user file
1835 * @brief Rx Transfer completed callbacks
1836 * @param hsd: Pointer SD handle
1839 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
1841 /* Prevent unused argument(s) compilation warning */
1844 /* NOTE : This function should not be modified, when the callback is needed,
1845 the HAL_SD_RxCpltCallback can be implemented in the user file
1850 * @brief SD error callbacks
1851 * @param hsd: Pointer SD handle
1854 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1856 /* Prevent unused argument(s) compilation warning */
1859 /* NOTE : This function should not be modified, when the callback is needed,
1860 the HAL_SD_ErrorCallback can be implemented in the user file
1865 * @brief SD Abort callbacks
1866 * @param hsd: Pointer SD handle
1869 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
1871 /* Prevent unused argument(s) compilation warning */
1874 /* NOTE : This function should not be modified, when the callback is needed,
1875 the HAL_SD_AbortCallback can be implemented in the user file
1879 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1881 * @brief Register a User SD Callback
1882 * To be used instead of the weak (surcharged) predefined callback
1883 * @param hsd : SD handle
1884 * @param CallbackID : ID of the callback to be registered
1885 * This parameter can be one of the following values:
1886 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1887 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1888 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1889 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1890 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1891 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1892 * @param pCallback : pointer to the Callback function
1895 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
1897 HAL_StatusTypeDef status = HAL_OK;
1899 if(pCallback == NULL)
1901 /* Update the error code */
1902 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1906 /* Process locked */
1909 if(hsd->State == HAL_SD_STATE_READY)
1913 case HAL_SD_TX_CPLT_CB_ID :
1914 hsd->TxCpltCallback = pCallback;
1916 case HAL_SD_RX_CPLT_CB_ID :
1917 hsd->RxCpltCallback = pCallback;
1919 case HAL_SD_ERROR_CB_ID :
1920 hsd->ErrorCallback = pCallback;
1922 case HAL_SD_ABORT_CB_ID :
1923 hsd->AbortCpltCallback = pCallback;
1925 case HAL_SD_MSP_INIT_CB_ID :
1926 hsd->MspInitCallback = pCallback;
1928 case HAL_SD_MSP_DEINIT_CB_ID :
1929 hsd->MspDeInitCallback = pCallback;
1932 /* Update the error code */
1933 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1934 /* update return status */
1939 else if (hsd->State == HAL_SD_STATE_RESET)
1943 case HAL_SD_MSP_INIT_CB_ID :
1944 hsd->MspInitCallback = pCallback;
1946 case HAL_SD_MSP_DEINIT_CB_ID :
1947 hsd->MspDeInitCallback = pCallback;
1950 /* Update the error code */
1951 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1952 /* update return status */
1959 /* Update the error code */
1960 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1961 /* update return status */
1971 * @brief Unregister a User SD Callback
1972 * SD Callback is redirected to the weak (surcharged) predefined callback
1973 * @param hsd : SD handle
1974 * @param CallbackID : ID of the callback to be unregistered
1975 * This parameter can be one of the following values:
1976 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1977 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1978 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1979 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1980 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1981 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1984 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
1986 HAL_StatusTypeDef status = HAL_OK;
1988 /* Process locked */
1991 if(hsd->State == HAL_SD_STATE_READY)
1995 case HAL_SD_TX_CPLT_CB_ID :
1996 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
1998 case HAL_SD_RX_CPLT_CB_ID :
1999 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
2001 case HAL_SD_ERROR_CB_ID :
2002 hsd->ErrorCallback = HAL_SD_ErrorCallback;
2004 case HAL_SD_ABORT_CB_ID :
2005 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
2007 case HAL_SD_MSP_INIT_CB_ID :
2008 hsd->MspInitCallback = HAL_SD_MspInit;
2010 case HAL_SD_MSP_DEINIT_CB_ID :
2011 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
2014 /* Update the error code */
2015 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2016 /* update return status */
2021 else if (hsd->State == HAL_SD_STATE_RESET)
2025 case HAL_SD_MSP_INIT_CB_ID :
2026 hsd->MspInitCallback = HAL_SD_MspInit;
2028 case HAL_SD_MSP_DEINIT_CB_ID :
2029 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
2032 /* Update the error code */
2033 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2034 /* update return status */
2041 /* Update the error code */
2042 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2043 /* update return status */
2051 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2057 /** @addtogroup SD_Exported_Functions_Group3
2058 * @brief management functions
2061 ==============================================================================
2062 ##### Peripheral Control functions #####
2063 ==============================================================================
2065 This subsection provides a set of functions allowing to control the SD card
2066 operations and get the related information
2073 * @brief Returns information the information of the card which are stored on
2075 * @param hsd: Pointer to SD handle
2076 * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that
2077 * contains all CID register parameters
2078 * @retval HAL status
2080 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
2082 pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
2084 pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
2086 pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
2088 pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
2090 pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
2092 pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
2094 pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
2096 pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
2098 pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
2100 pCID->Reserved2 = 1U;
2106 * @brief Returns information the information of the card which are stored on
2108 * @param hsd: Pointer to SD handle
2109 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
2110 * contains all CSD register parameters
2111 * @retval HAL status
2113 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
2115 pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
2117 pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
2119 pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
2121 pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
2123 pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
2125 pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
2127 pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
2129 pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
2131 pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
2133 pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
2135 pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
2137 pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
2139 pCSD->Reserved2 = 0U; /*!< Reserved */
2141 if(hsd->SdCard.CardType == CARD_SDSC)
2143 pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
2145 pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
2147 pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
2149 pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
2151 pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
2153 pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
2155 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2156 hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2157 hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2159 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
2160 hsd->SdCard.LogBlockSize = 512U;
2162 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
2165 pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
2167 hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
2168 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
2169 hsd->SdCard.BlockSize = 512U;
2170 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
2174 /* Clear all the static flags */
2175 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2176 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2177 hsd->State = HAL_SD_STATE_READY;
2181 pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
2183 pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
2185 pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
2187 pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
2189 pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
2191 pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
2193 pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
2195 pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
2197 pCSD->Reserved3 = 0;
2199 pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
2201 pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
2203 pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
2205 pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
2207 pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
2209 pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
2211 pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
2213 pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
2215 pCSD->Reserved4 = 1;
2221 * @brief Gets the SD status info.
2222 * @param hsd: Pointer to SD handle
2223 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
2224 * will contain the SD card status information
2225 * @retval HAL status
2227 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
2229 uint32_t sd_status[16];
2230 uint32_t errorstate;
2232 errorstate = SD_SendSDStatus(hsd, sd_status);
2233 if(errorstate != HAL_SD_ERROR_NONE)
2235 /* Clear all the static flags */
2236 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2237 hsd->ErrorCode |= errorstate;
2238 hsd->State = HAL_SD_STATE_READY;
2243 pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
2245 pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
2247 pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
2249 pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
2250 ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
2252 pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
2254 pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
2256 pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
2258 pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
2260 pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
2262 pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
2269 * @brief Gets the SD card info.
2270 * @param hsd: Pointer to SD handle
2271 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
2272 * will contain the SD card status information
2273 * @retval HAL status
2275 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
2277 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
2278 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
2279 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
2280 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
2281 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
2282 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
2283 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
2284 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
2290 * @brief Enables wide bus operation for the requested card if supported by
2292 * @param hsd: Pointer to SD handle
2293 * @param WideMode: Specifies the SD card wide bus mode
2294 * This parameter can be one of the following values:
2295 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
2296 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
2297 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
2298 * @retval HAL status
2300 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
2302 SDIO_InitTypeDef Init;
2303 uint32_t errorstate;
2305 /* Check the parameters */
2306 assert_param(IS_SDIO_BUS_WIDE(WideMode));
2309 hsd->State = HAL_SD_STATE_BUSY;
2311 if(hsd->SdCard.CardType != CARD_SECURED)
2313 if(WideMode == SDIO_BUS_WIDE_8B)
2315 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2317 else if(WideMode == SDIO_BUS_WIDE_4B)
2319 errorstate = SD_WideBus_Enable(hsd);
2321 hsd->ErrorCode |= errorstate;
2323 else if(WideMode == SDIO_BUS_WIDE_1B)
2325 errorstate = SD_WideBus_Disable(hsd);
2327 hsd->ErrorCode |= errorstate;
2331 /* WideMode is not a valid argument*/
2332 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2337 /* MMC Card does not support this feature */
2338 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2341 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2343 /* Clear all the static flags */
2344 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2345 hsd->State = HAL_SD_STATE_READY;
2350 /* Configure the SDIO peripheral */
2351 Init.ClockEdge = hsd->Init.ClockEdge;
2352 Init.ClockBypass = hsd->Init.ClockBypass;
2353 Init.ClockPowerSave = hsd->Init.ClockPowerSave;
2354 Init.BusWide = WideMode;
2355 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2356 Init.ClockDiv = hsd->Init.ClockDiv;
2357 (void)SDIO_Init(hsd->Instance, Init);
2361 hsd->State = HAL_SD_STATE_READY;
2367 * @brief Gets the current sd card data state.
2368 * @param hsd: pointer to SD handle
2369 * @retval Card state
2371 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2374 uint32_t errorstate;
2377 errorstate = SD_SendStatus(hsd, &resp1);
2378 if(errorstate != HAL_SD_ERROR_NONE)
2380 hsd->ErrorCode |= errorstate;
2383 cardstate = ((resp1 >> 9U) & 0x0FU);
2385 return (HAL_SD_CardStateTypeDef)cardstate;
2389 * @brief Abort the current transfer and disable the SD.
2390 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2391 * the configuration information for SD module.
2392 * @retval HAL status
2394 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
2396 HAL_SD_CardStateTypeDef CardState;
2397 uint32_t context = hsd->Context;
2399 /* DIsable All interrupts */
2400 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2401 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2403 /* Clear All flags */
2404 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2406 CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
2408 if ((context & SD_CONTEXT_DMA) != 0U)
2410 /* Disable the SD DMA request */
2411 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2413 /* Abort the SD DMA Tx channel */
2414 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2416 if(HAL_DMA_Abort(hsd->hdmatx) != HAL_OK)
2418 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2421 /* Abort the SD DMA Rx channel */
2422 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2424 if(HAL_DMA_Abort(hsd->hdmarx) != HAL_OK)
2426 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2435 hsd->State = HAL_SD_STATE_READY;
2437 /* Initialize the SD operation */
2438 hsd->Context = SD_CONTEXT_NONE;
2440 CardState = HAL_SD_GetCardState(hsd);
2441 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2443 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2445 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2453 * @brief Abort the current transfer and disable the SD (IT mode).
2454 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2455 * the configuration information for SD module.
2456 * @retval HAL status
2458 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
2460 HAL_SD_CardStateTypeDef CardState;
2461 uint32_t context = hsd->Context;
2463 /* Disable All interrupts */
2464 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2465 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2467 CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
2469 if ((context & SD_CONTEXT_DMA) != 0U)
2471 /* Disable the SD DMA request */
2472 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2474 /* Abort the SD DMA Tx channel */
2475 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2477 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
2478 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
2483 /* Abort the SD DMA Rx channel */
2484 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2486 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
2487 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
2497 /* No transfer ongoing on both DMA channels*/
2500 /* Clear All flags */
2501 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2503 CardState = HAL_SD_GetCardState(hsd);
2504 hsd->State = HAL_SD_STATE_READY;
2505 hsd->Context = SD_CONTEXT_NONE;
2506 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2508 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2510 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2516 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2517 hsd->AbortCpltCallback(hsd);
2519 HAL_SD_AbortCallback(hsd);
2520 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2535 /* Private function ----------------------------------------------------------*/
2536 /** @addtogroup SD_Private_Functions
2541 * @brief DMA SD transmit process complete callback
2542 * @param hdma: DMA handle
2545 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2547 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2549 /* Enable DATAEND Interrupt */
2550 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND));
2554 * @brief DMA SD receive process complete callback
2555 * @param hdma: DMA handle
2558 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2560 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2561 uint32_t errorstate;
2563 /* Send stop command in multiblock write */
2564 if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
2566 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
2567 if(errorstate != HAL_SD_ERROR_NONE)
2569 hsd->ErrorCode |= errorstate;
2570 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2571 hsd->ErrorCallback(hsd);
2573 HAL_SD_ErrorCallback(hsd);
2578 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2579 in the SD DCTRL register */
2580 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2582 /* Clear all the static flags */
2583 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2585 hsd->State = HAL_SD_STATE_READY;
2586 hsd->Context = SD_CONTEXT_NONE;
2588 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2589 hsd->RxCpltCallback(hsd);
2591 HAL_SD_RxCpltCallback(hsd);
2596 * @brief DMA SD communication error callback
2597 * @param hdma: DMA handle
2600 static void SD_DMAError(DMA_HandleTypeDef *hdma)
2602 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2603 HAL_SD_CardStateTypeDef CardState;
2604 uint32_t RxErrorCode, TxErrorCode;
2606 /* if DMA error is FIFO error ignore it */
2607 if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
2609 RxErrorCode = hsd->hdmarx->ErrorCode;
2610 TxErrorCode = hsd->hdmatx->ErrorCode;
2611 if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
2613 /* Clear All flags */
2614 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2616 /* Disable All interrupts */
2617 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2618 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2620 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2621 CardState = HAL_SD_GetCardState(hsd);
2622 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2624 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2627 hsd->State= HAL_SD_STATE_READY;
2628 hsd->Context = SD_CONTEXT_NONE;
2631 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2632 hsd->ErrorCallback(hsd);
2634 HAL_SD_ErrorCallback(hsd);
2640 * @brief DMA SD Tx Abort callback
2641 * @param hdma: DMA handle
2644 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
2646 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2647 HAL_SD_CardStateTypeDef CardState;
2649 /* Clear All flags */
2650 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2652 CardState = HAL_SD_GetCardState(hsd);
2653 hsd->State = HAL_SD_STATE_READY;
2654 hsd->Context = SD_CONTEXT_NONE;
2655 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2657 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2660 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2662 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2663 hsd->AbortCpltCallback(hsd);
2665 HAL_SD_AbortCallback(hsd);
2670 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2671 hsd->ErrorCallback(hsd);
2673 HAL_SD_ErrorCallback(hsd);
2679 * @brief DMA SD Rx Abort callback
2680 * @param hdma: DMA handle
2683 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
2685 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2686 HAL_SD_CardStateTypeDef CardState;
2688 /* Clear All flags */
2689 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2691 CardState = HAL_SD_GetCardState(hsd);
2692 hsd->State = HAL_SD_STATE_READY;
2693 hsd->Context = SD_CONTEXT_NONE;
2694 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2696 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2699 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2701 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2702 hsd->AbortCpltCallback(hsd);
2704 HAL_SD_AbortCallback(hsd);
2709 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2710 hsd->ErrorCallback(hsd);
2712 HAL_SD_ErrorCallback(hsd);
2718 * @brief Initializes the sd card.
2719 * @param hsd: Pointer to SD handle
2720 * @retval SD Card error state
2722 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
2724 HAL_SD_CardCSDTypeDef CSD;
2725 uint32_t errorstate;
2726 uint16_t sd_rca = 1U;
2728 /* Check the power State */
2729 if(SDIO_GetPowerState(hsd->Instance) == 0U)
2732 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2735 if(hsd->SdCard.CardType != CARD_SECURED)
2737 /* Send CMD2 ALL_SEND_CID */
2738 errorstate = SDMMC_CmdSendCID(hsd->Instance);
2739 if(errorstate != HAL_SD_ERROR_NONE)
2745 /* Get Card identification number data */
2746 hsd->CID[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2747 hsd->CID[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2748 hsd->CID[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2749 hsd->CID[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2753 if(hsd->SdCard.CardType != CARD_SECURED)
2755 /* Send CMD3 SET_REL_ADDR with argument 0 */
2756 /* SD Card publishes its RCA. */
2757 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
2758 if(errorstate != HAL_SD_ERROR_NONE)
2763 if(hsd->SdCard.CardType != CARD_SECURED)
2765 /* Get the SD card RCA */
2766 hsd->SdCard.RelCardAdd = sd_rca;
2768 /* Send CMD9 SEND_CSD with argument as card's RCA */
2769 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2770 if(errorstate != HAL_SD_ERROR_NONE)
2776 /* Get Card Specific Data */
2777 hsd->CSD[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2778 hsd->CSD[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2779 hsd->CSD[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2780 hsd->CSD[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2784 /* Get the Card Class */
2785 hsd->SdCard.Class = (SDIO_GetResponse(hsd->Instance, SDIO_RESP2) >> 20U);
2787 /* Get CSD parameters */
2788 if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
2790 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2793 /* Select the Card */
2794 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
2795 if(errorstate != HAL_SD_ERROR_NONE)
2800 /* Configure SDIO peripheral interface */
2801 (void)SDIO_Init(hsd->Instance, hsd->Init);
2803 /* All cards are initialized */
2804 return HAL_SD_ERROR_NONE;
2808 * @brief Enquires cards about their operating voltage and configures clock
2809 * controls and stores SD information that will be needed in future
2811 * @param hsd: Pointer to SD handle
2812 * @retval error state
2814 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
2816 __IO uint32_t count = 0U;
2817 uint32_t response = 0U, validvoltage = 0U;
2818 uint32_t errorstate;
2820 /* CMD0: GO_IDLE_STATE */
2821 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2822 if(errorstate != HAL_SD_ERROR_NONE)
2827 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2828 errorstate = SDMMC_CmdOperCond(hsd->Instance);
2829 if(errorstate != HAL_SD_ERROR_NONE)
2831 hsd->SdCard.CardVersion = CARD_V1_X;
2832 /* CMD0: GO_IDLE_STATE */
2833 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2834 if(errorstate != HAL_SD_ERROR_NONE)
2842 hsd->SdCard.CardVersion = CARD_V2_X;
2845 if( hsd->SdCard.CardVersion == CARD_V2_X)
2847 /* SEND CMD55 APP_CMD with RCA as 0 */
2848 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2849 if(errorstate != HAL_SD_ERROR_NONE)
2851 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2855 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2856 while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
2858 /* SEND CMD55 APP_CMD with RCA as 0 */
2859 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2860 if(errorstate != HAL_SD_ERROR_NONE)
2866 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
2867 if(errorstate != HAL_SD_ERROR_NONE)
2869 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2872 /* Get command response */
2873 response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2875 /* Get operating voltage*/
2876 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
2881 if(count >= SDMMC_MAX_VOLT_TRIAL)
2883 return HAL_SD_ERROR_INVALID_VOLTRANGE;
2886 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
2888 hsd->SdCard.CardType = CARD_SDHC_SDXC;
2892 hsd->SdCard.CardType = CARD_SDSC;
2896 return HAL_SD_ERROR_NONE;
2900 * @brief Turns the SDIO output signals off.
2901 * @param hsd: Pointer to SD handle
2904 static void SD_PowerOFF(SD_HandleTypeDef *hsd)
2906 /* Set Power State to OFF */
2907 (void)SDIO_PowerState_OFF(hsd->Instance);
2911 * @brief Send Status info command.
2912 * @param hsd: pointer to SD handle
2913 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
2914 * SD Status register)
2915 * @retval error state
2917 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
2919 SDIO_DataInitTypeDef config;
2920 uint32_t errorstate;
2921 uint32_t tickstart = HAL_GetTick();
2923 uint32_t *pData = pSDstatus;
2925 /* Check SD response */
2926 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2928 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2931 /* Set block size for card if it is not equal to current block size for card */
2932 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
2933 if(errorstate != HAL_SD_ERROR_NONE)
2935 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2940 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2941 if(errorstate != HAL_SD_ERROR_NONE)
2943 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2947 /* Configure the SD DPSM (Data Path State Machine) */
2948 config.DataTimeOut = SDMMC_DATATIMEOUT;
2949 config.DataLength = 64U;
2950 config.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
2951 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
2952 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
2953 config.DPSM = SDIO_DPSM_ENABLE;
2954 (void)SDIO_ConfigData(hsd->Instance, &config);
2956 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
2957 errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
2958 if(errorstate != HAL_SD_ERROR_NONE)
2960 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2964 /* Get status data */
2965 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
2967 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
2969 for(count = 0U; count < 8U; count++)
2971 *pData = SDIO_ReadFIFO(hsd->Instance);
2976 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2978 return HAL_SD_ERROR_TIMEOUT;
2982 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
2984 return HAL_SD_ERROR_DATA_TIMEOUT;
2986 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
2988 return HAL_SD_ERROR_DATA_CRC_FAIL;
2990 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
2992 return HAL_SD_ERROR_RX_OVERRUN;
2999 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)))
3001 *pData = SDIO_ReadFIFO(hsd->Instance);
3004 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3006 return HAL_SD_ERROR_TIMEOUT;
3010 /* Clear all the static status flags*/
3011 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
3013 return HAL_SD_ERROR_NONE;
3017 * @brief Returns the current card's status.
3018 * @param hsd: Pointer to SD handle
3019 * @param pCardStatus: pointer to the buffer that will contain the SD card
3020 * status (Card Status register)
3021 * @retval error state
3023 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
3025 uint32_t errorstate;
3027 if(pCardStatus == NULL)
3029 return HAL_SD_ERROR_PARAM;
3032 /* Send Status command */
3033 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3034 if(errorstate != HAL_SD_ERROR_NONE)
3039 /* Get SD card status */
3040 *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
3042 return HAL_SD_ERROR_NONE;
3046 * @brief Enables the SDIO wide bus mode.
3047 * @param hsd: pointer to SD handle
3048 * @retval error state
3050 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
3052 uint32_t scr[2U] = {0U, 0U};
3053 uint32_t errorstate;
3055 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3057 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3060 /* Get SCR Register */
3061 errorstate = SD_FindSCR(hsd, scr);
3062 if(errorstate != HAL_SD_ERROR_NONE)
3067 /* If requested card supports wide bus operation */
3068 if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
3070 /* Send CMD55 APP_CMD with argument as card's RCA.*/
3071 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3072 if(errorstate != HAL_SD_ERROR_NONE)
3077 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3078 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
3079 if(errorstate != HAL_SD_ERROR_NONE)
3084 return HAL_SD_ERROR_NONE;
3088 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3093 * @brief Disables the SDIO wide bus mode.
3094 * @param hsd: Pointer to SD handle
3095 * @retval error state
3097 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3099 uint32_t scr[2U] = {0U, 0U};
3100 uint32_t errorstate;
3102 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3104 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;