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(hsd
);
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 hsd
->State
= HAL_SD_STATE_BUSY
;
1337 /* Initialize data control register */
1338 hsd
->Instance
->DCTRL
= 0U;
1340 /* Enable SD Error interrupts */
1341 __HAL_SD_ENABLE_IT(hsd
, (SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_TXUNDERR
));
1343 /* Set the DMA transfer complete callback */
1344 hsd
->hdmatx
->XferCpltCallback
= SD_DMATransmitCplt
;
1346 /* Set the DMA error callback */
1347 hsd
->hdmatx
->XferErrorCallback
= SD_DMAError
;
1349 /* Set the DMA Abort callback */
1350 hsd
->hdmatx
->XferAbortCallback
= NULL
;
1352 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
1356 /* Set Block Size for Card */
1357 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
1358 if(errorstate
!= HAL_SD_ERROR_NONE
)
1360 /* Clear all the static flags */
1361 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1362 hsd
->ErrorCode
|= errorstate
;
1363 hsd
->State
= HAL_SD_STATE_READY
;
1368 /* Write Blocks in Polling mode */
1369 if(NumberOfBlocks
> 1U)
1371 hsd
->Context
= (SD_CONTEXT_WRITE_MULTIPLE_BLOCK
| SD_CONTEXT_DMA
);
1373 /* MM: Prepare for write */
1375 SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->RCA << 16));
1376 SDIO_CmdInitTypeDef mm_cmdinit;
1377 mm_cmdinit.Argument = (uint32_t)NumberOfBlocks;
1378 mm_cmdinit.CmdIndex = SDMMC_CMD_SET_BLOCK_COUNT;
1379 mm_cmdinit.Response = SDIO_RESPONSE_SHORT;
1380 mm_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
1381 mm_cmdinit.CPSM = SDIO_CPSM_ENABLE;
1382 (void)SDIO_SendCommand(hsd->Instance, &mm_cmdinit);
1383 SDMMC_GetCmdResp1(hsd->Instance, SDMMC_CMD_SET_BLOCK_COUNT, SDIO_CMDTIMEOUT);*/
1385 /* Write Multi Block command */
1386 errorstate
= SDMMC_CmdWriteMultiBlock(hsd
->Instance
, add
);
1390 hsd
->Context
= (SD_CONTEXT_WRITE_SINGLE_BLOCK
| SD_CONTEXT_DMA
);
1392 /* Write Single Block command */
1393 errorstate
= SDMMC_CmdWriteSingleBlock(hsd
->Instance
, add
);
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
;
1401 hsd
->Context
= SD_CONTEXT_NONE
;
1405 /* Enable SDIO DMA transfer */
1406 // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
1408 /* Enable the DMA Channel */
1409 if(HAL_DMA_Start_IT(hsd
->hdmatx
, (uint32_t)pData
, (uint32_t)&hsd
->Instance
->FIFO
, (uint32_t)(BLOCKSIZE
* NumberOfBlocks
)/4U) != HAL_OK
)
1411 __HAL_SD_DISABLE_IT(hsd
, (SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_TXUNDERR
));
1412 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1413 hsd
->ErrorCode
|= HAL_SD_ERROR_DMA
;
1414 hsd
->State
= HAL_SD_STATE_READY
;
1415 hsd
->Context
= SD_CONTEXT_NONE
;
1420 /* Configure the SD DPSM (Data Path State Machine) */
1421 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
1422 config
.DataLength
= BLOCKSIZE
* NumberOfBlocks
;
1423 config
.DataBlockSize
= SDIO_DATABLOCK_SIZE_512B
;
1424 config
.TransferDir
= SDIO_TRANSFER_DIR_TO_CARD
;
1425 config
.TransferMode
= SDIO_TRANSFER_MODE_BLOCK
;
1426 config
.DPSM
= SDIO_DPSM_ENABLE
;
1428 // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
1429 // data is just discarded before the dpsm is started.
1430 __HAL_SD_DMA_ENABLE();
1432 (void)SDIO_ConfigData(hsd
->Instance
, &config
);
1444 * @brief Erases the specified memory area of the given SD card.
1445 * @note This API should be followed by a check on the card state through
1446 * HAL_SD_GetCardState().
1447 * @param hsd: Pointer to SD handle
1448 * @param BlockStartAdd: Start Block address
1449 * @param BlockEndAdd: End Block address
1450 * @retval HAL status
1452 HAL_StatusTypeDef
HAL_SD_Erase(SD_HandleTypeDef
*hsd
, uint32_t BlockStartAdd
, uint32_t BlockEndAdd
)
1454 uint32_t errorstate
;
1455 uint32_t start_add
= BlockStartAdd
;
1456 uint32_t end_add
= BlockEndAdd
;
1458 if(hsd
->State
== HAL_SD_STATE_READY
)
1460 hsd
->ErrorCode
= HAL_SD_ERROR_NONE
;
1462 if(end_add
< start_add
)
1464 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
1468 if(end_add
> (hsd
->SdCard
.LogBlockNbr
))
1470 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
1474 hsd
->State
= HAL_SD_STATE_BUSY
;
1476 /* Check if the card command class supports erase command */
1477 if(((hsd
->SdCard
.Class
) & SDIO_CCCC_ERASE
) == 0U)
1479 /* Clear all the static flags */
1480 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1481 hsd
->ErrorCode
|= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
1482 hsd
->State
= HAL_SD_STATE_READY
;
1486 if((SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
1488 /* Clear all the static flags */
1489 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1490 hsd
->ErrorCode
|= HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
1491 hsd
->State
= HAL_SD_STATE_READY
;
1495 /* Get start and end block for high capacity cards */
1496 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
1502 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1503 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
1505 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1506 errorstate
= SDMMC_CmdSDEraseStartAdd(hsd
->Instance
, start_add
);
1507 if(errorstate
!= HAL_SD_ERROR_NONE
)
1509 /* Clear all the static flags */
1510 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1511 hsd
->ErrorCode
|= errorstate
;
1512 hsd
->State
= HAL_SD_STATE_READY
;
1516 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1517 errorstate
= SDMMC_CmdSDEraseEndAdd(hsd
->Instance
, end_add
);
1518 if(errorstate
!= HAL_SD_ERROR_NONE
)
1520 /* Clear all the static flags */
1521 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1522 hsd
->ErrorCode
|= errorstate
;
1523 hsd
->State
= HAL_SD_STATE_READY
;
1528 /* Send CMD38 ERASE */
1529 errorstate
= SDMMC_CmdErase(hsd
->Instance
);
1530 if(errorstate
!= HAL_SD_ERROR_NONE
)
1532 /* Clear all the static flags */
1533 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1534 hsd
->ErrorCode
|= errorstate
;
1535 hsd
->State
= HAL_SD_STATE_READY
;
1539 hsd
->State
= HAL_SD_STATE_READY
;
1550 * @brief This function handles SD card interrupt request.
1551 * @param hsd: Pointer to SD handle
1554 void HAL_SD_IRQHandler(SD_HandleTypeDef
*hsd
)
1556 uint32_t errorstate
;
1557 uint32_t context
= hsd
->Context
;
1559 /* Check for SDIO interrupt flags */
1560 if((__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXFIFOHF
) != RESET
) && ((context
& SD_CONTEXT_IT
) != 0U))
1565 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DATAEND
) != RESET
)
1567 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_FLAG_DATAEND
);
1569 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
1570 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
| SDIO_IT_TXFIFOHE
|\
1573 hsd
->Instance
->DCTRL
&= ~(SDIO_DCTRL_DTEN
);
1575 if((context
& SD_CONTEXT_IT
) != 0U)
1577 if(((context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) != 0U) || ((context
& SD_CONTEXT_WRITE_MULTIPLE_BLOCK
) != 0U))
1579 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
1580 if(errorstate
!= HAL_SD_ERROR_NONE
)
1582 hsd
->ErrorCode
|= errorstate
;
1583 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1584 hsd
->ErrorCallback(hsd
);
1586 HAL_SD_ErrorCallback(hsd
);
1587 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1591 /* Clear all the static flags */
1592 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_DATA_FLAGS
);
1594 hsd
->State
= HAL_SD_STATE_READY
;
1595 hsd
->Context
= SD_CONTEXT_NONE
;
1596 if(((context
& SD_CONTEXT_READ_SINGLE_BLOCK
) != 0U) || ((context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) != 0U))
1598 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1599 hsd
->RxCpltCallback(hsd
);
1601 HAL_SD_RxCpltCallback(hsd
);
1602 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1606 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1607 hsd
->TxCpltCallback(hsd
);
1609 HAL_SD_TxCpltCallback(hsd
);
1610 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1613 else if((context
& SD_CONTEXT_DMA
) != 0U)
1615 if((context
& SD_CONTEXT_WRITE_MULTIPLE_BLOCK
) != 0U)
1617 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
1618 if(errorstate
!= HAL_SD_ERROR_NONE
)
1620 hsd
->ErrorCode
|= errorstate
;
1621 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1622 hsd
->ErrorCallback(hsd
);
1624 HAL_SD_ErrorCallback(hsd
);
1625 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1628 if(((context
& SD_CONTEXT_READ_SINGLE_BLOCK
) == 0U) && ((context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) == 0U))
1630 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1631 in the SD DCTRL register */
1632 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN
);
1634 hsd
->State
= HAL_SD_STATE_READY
;
1636 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1637 hsd
->TxCpltCallback(hsd
);
1639 HAL_SD_TxCpltCallback(hsd
);
1640 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1649 else if((__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_TXFIFOHE
) != RESET
) && ((context
& SD_CONTEXT_IT
) != 0U))
1654 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DCRCFAIL
| SDIO_FLAG_DTIMEOUT
| SDIO_FLAG_RXOVERR
| SDIO_FLAG_TXUNDERR
) != RESET
)
1656 /* Set Error code */
1657 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DCRCFAIL
) != RESET
)
1659 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_CRC_FAIL
;
1661 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DTIMEOUT
) != RESET
)
1663 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_TIMEOUT
;
1665 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
) != RESET
)
1667 hsd
->ErrorCode
|= HAL_SD_ERROR_RX_OVERRUN
;
1669 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_TXUNDERR
) != RESET
)
1671 hsd
->ErrorCode
|= HAL_SD_ERROR_TX_UNDERRUN
;
1674 /* Clear All flags */
1675 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_DATA_FLAGS
| SDIO_FLAG_STBITERR
);
1677 /* Disable all interrupts */
1678 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
1679 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
| SDIO_IT_STBITERR
);
1681 hsd
->ErrorCode
|= SDMMC_CmdStopTransfer(hsd
->Instance
);
1683 if((context
& SD_CONTEXT_IT
) != 0U)
1685 /* Set the SD state to ready to be able to start again the process */
1686 hsd
->State
= HAL_SD_STATE_READY
;
1687 hsd
->Context
= SD_CONTEXT_NONE
;
1688 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1689 hsd
->ErrorCallback(hsd
);
1691 HAL_SD_ErrorCallback(hsd
);
1692 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1694 else if((context
& SD_CONTEXT_DMA
) != 0U)
1696 /* Abort the SD DMA channel */
1697 if(((context
& SD_CONTEXT_WRITE_SINGLE_BLOCK
) != 0U) || ((context
& SD_CONTEXT_WRITE_MULTIPLE_BLOCK
) != 0U))
1699 /* Set the DMA Tx abort callback */
1700 hsd
->hdmatx
->XferAbortCallback
= SD_DMATxAbort
;
1701 /* Abort DMA in IT mode */
1702 if(HAL_DMA_Abort_IT(hsd
->hdmatx
) != HAL_OK
)
1704 SD_DMATxAbort(hsd
->hdmatx
);
1707 else if(((context
& SD_CONTEXT_READ_SINGLE_BLOCK
) != 0U) || ((context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) != 0U))
1709 /* Set the DMA Rx abort callback */
1710 hsd
->hdmarx
->XferAbortCallback
= SD_DMARxAbort
;
1711 /* Abort DMA in IT mode */
1712 if(HAL_DMA_Abort_IT(hsd
->hdmarx
) != HAL_OK
)
1714 SD_DMARxAbort(hsd
->hdmarx
);
1719 hsd
->ErrorCode
= HAL_SD_ERROR_NONE
;
1720 hsd
->State
= HAL_SD_STATE_READY
;
1721 hsd
->Context
= SD_CONTEXT_NONE
;
1722 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1723 hsd
->AbortCpltCallback(hsd
);
1725 HAL_SD_AbortCallback(hsd
);
1726 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1741 * @brief return the SD state
1742 * @param hsd: Pointer to sd handle
1745 HAL_SD_StateTypeDef
HAL_SD_GetState(SD_HandleTypeDef
*hsd
)
1751 * @brief Return the SD error code
1752 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
1753 * the configuration information.
1754 * @retval SD Error Code
1756 uint32_t HAL_SD_GetError(SD_HandleTypeDef
*hsd
)
1758 return hsd
->ErrorCode
;
1762 * @brief Tx Transfer completed callbacks
1763 * @param hsd: Pointer to SD handle
1766 __weak
void HAL_SD_TxCpltCallback(SD_HandleTypeDef
*hsd
)
1768 /* Prevent unused argument(s) compilation warning */
1771 /* NOTE : This function should not be modified, when the callback is needed,
1772 the HAL_SD_TxCpltCallback can be implemented in the user file
1777 * @brief Rx Transfer completed callbacks
1778 * @param hsd: Pointer SD handle
1781 __weak
void HAL_SD_RxCpltCallback(SD_HandleTypeDef
*hsd
)
1783 /* Prevent unused argument(s) compilation warning */
1786 /* NOTE : This function should not be modified, when the callback is needed,
1787 the HAL_SD_RxCpltCallback can be implemented in the user file
1792 * @brief SD error callbacks
1793 * @param hsd: Pointer SD handle
1796 __weak
void HAL_SD_ErrorCallback(SD_HandleTypeDef
*hsd
)
1798 /* Prevent unused argument(s) compilation warning */
1801 /* NOTE : This function should not be modified, when the callback is needed,
1802 the HAL_SD_ErrorCallback can be implemented in the user file
1807 * @brief SD Abort callbacks
1808 * @param hsd: Pointer SD handle
1811 __weak
void HAL_SD_AbortCallback(SD_HandleTypeDef
*hsd
)
1813 /* Prevent unused argument(s) compilation warning */
1816 /* NOTE : This function should not be modified, when the callback is needed,
1817 the HAL_SD_AbortCallback can be implemented in the user file
1821 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1823 * @brief Register a User SD Callback
1824 * To be used instead of the weak (surcharged) predefined callback
1825 * @param hsd : SD handle
1826 * @param CallbackID : ID of the callback to be registered
1827 * This parameter can be one of the following values:
1828 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1829 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1830 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1831 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1832 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1833 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1834 * @param pCallback : pointer to the Callback function
1837 HAL_StatusTypeDef
HAL_SD_RegisterCallback(SD_HandleTypeDef
*hsd
, HAL_SD_CallbackIDTypeDef CallbackID
, pSD_CallbackTypeDef pCallback
)
1839 HAL_StatusTypeDef status
= HAL_OK
;
1841 if(pCallback
== NULL
)
1843 /* Update the error code */
1844 hsd
->ErrorCode
|= HAL_SD_ERROR_INVALID_CALLBACK
;
1848 /* Process locked */
1851 if(hsd
->State
== HAL_SD_STATE_READY
)
1855 case HAL_SD_TX_CPLT_CB_ID
:
1856 hsd
->TxCpltCallback
= pCallback
;
1858 case HAL_SD_RX_CPLT_CB_ID
:
1859 hsd
->RxCpltCallback
= pCallback
;
1861 case HAL_SD_ERROR_CB_ID
:
1862 hsd
->ErrorCallback
= pCallback
;
1864 case HAL_SD_ABORT_CB_ID
:
1865 hsd
->AbortCpltCallback
= pCallback
;
1867 case HAL_SD_MSP_INIT_CB_ID
:
1868 hsd
->MspInitCallback
= pCallback
;
1870 case HAL_SD_MSP_DEINIT_CB_ID
:
1871 hsd
->MspDeInitCallback
= pCallback
;
1874 /* Update the error code */
1875 hsd
->ErrorCode
|= HAL_SD_ERROR_INVALID_CALLBACK
;
1876 /* update return status */
1881 else if (hsd
->State
== HAL_SD_STATE_RESET
)
1885 case HAL_SD_MSP_INIT_CB_ID
:
1886 hsd
->MspInitCallback
= pCallback
;
1888 case HAL_SD_MSP_DEINIT_CB_ID
:
1889 hsd
->MspDeInitCallback
= pCallback
;
1892 /* Update the error code */
1893 hsd
->ErrorCode
|= HAL_SD_ERROR_INVALID_CALLBACK
;
1894 /* update return status */
1901 /* Update the error code */
1902 hsd
->ErrorCode
|= HAL_SD_ERROR_INVALID_CALLBACK
;
1903 /* update return status */
1913 * @brief Unregister a User SD Callback
1914 * SD Callback is redirected to the weak (surcharged) predefined callback
1915 * @param hsd : SD handle
1916 * @param CallbackID : ID of the callback to be unregistered
1917 * This parameter can be one of the following values:
1918 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1919 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1920 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1921 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1922 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1923 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1926 HAL_StatusTypeDef
HAL_SD_UnRegisterCallback(SD_HandleTypeDef
*hsd
, HAL_SD_CallbackIDTypeDef CallbackID
)
1928 HAL_StatusTypeDef status
= HAL_OK
;
1930 /* Process locked */
1933 if(hsd
->State
== HAL_SD_STATE_READY
)
1937 case HAL_SD_TX_CPLT_CB_ID
:
1938 hsd
->TxCpltCallback
= HAL_SD_TxCpltCallback
;
1940 case HAL_SD_RX_CPLT_CB_ID
:
1941 hsd
->RxCpltCallback
= HAL_SD_RxCpltCallback
;
1943 case HAL_SD_ERROR_CB_ID
:
1944 hsd
->ErrorCallback
= HAL_SD_ErrorCallback
;
1946 case HAL_SD_ABORT_CB_ID
:
1947 hsd
->AbortCpltCallback
= HAL_SD_AbortCallback
;
1949 case HAL_SD_MSP_INIT_CB_ID
:
1950 hsd
->MspInitCallback
= HAL_SD_MspInit
;
1952 case HAL_SD_MSP_DEINIT_CB_ID
:
1953 hsd
->MspDeInitCallback
= HAL_SD_MspDeInit
;
1956 /* Update the error code */
1957 hsd
->ErrorCode
|= HAL_SD_ERROR_INVALID_CALLBACK
;
1958 /* update return status */
1963 else if (hsd
->State
== HAL_SD_STATE_RESET
)
1967 case HAL_SD_MSP_INIT_CB_ID
:
1968 hsd
->MspInitCallback
= HAL_SD_MspInit
;
1970 case HAL_SD_MSP_DEINIT_CB_ID
:
1971 hsd
->MspDeInitCallback
= HAL_SD_MspDeInit
;
1974 /* Update the error code */
1975 hsd
->ErrorCode
|= HAL_SD_ERROR_INVALID_CALLBACK
;
1976 /* update return status */
1983 /* Update the error code */
1984 hsd
->ErrorCode
|= HAL_SD_ERROR_INVALID_CALLBACK
;
1985 /* update return status */
1993 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1999 /** @addtogroup SD_Exported_Functions_Group3
2000 * @brief management functions
2003 ==============================================================================
2004 ##### Peripheral Control functions #####
2005 ==============================================================================
2007 This subsection provides a set of functions allowing to control the SD card
2008 operations and get the related information
2015 * @brief Returns information the information of the card which are stored on
2017 * @param hsd: Pointer to SD handle
2018 * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that
2019 * contains all CID register parameters
2020 * @retval HAL status
2022 HAL_StatusTypeDef
HAL_SD_GetCardCID(SD_HandleTypeDef
*hsd
, HAL_SD_CardCIDTypeDef
*pCID
)
2024 pCID
->ManufacturerID
= (uint8_t)((hsd
->CID
[0] & 0xFF000000U
) >> 24U);
2026 pCID
->OEM_AppliID
= (uint16_t)((hsd
->CID
[0] & 0x00FFFF00U
) >> 8U);
2028 pCID
->ProdName1
= (((hsd
->CID
[0] & 0x000000FFU
) << 24U) | ((hsd
->CID
[1] & 0xFFFFFF00U
) >> 8U));
2030 pCID
->ProdName2
= (uint8_t)(hsd
->CID
[1] & 0x000000FFU
);
2032 pCID
->ProdRev
= (uint8_t)((hsd
->CID
[2] & 0xFF000000U
) >> 24U);
2034 pCID
->ProdSN
= (((hsd
->CID
[2] & 0x00FFFFFFU
) << 8U) | ((hsd
->CID
[3] & 0xFF000000U
) >> 24U));
2036 pCID
->Reserved1
= (uint8_t)((hsd
->CID
[3] & 0x00F00000U
) >> 20U);
2038 pCID
->ManufactDate
= (uint16_t)((hsd
->CID
[3] & 0x000FFF00U
) >> 8U);
2040 pCID
->CID_CRC
= (uint8_t)((hsd
->CID
[3] & 0x000000FEU
) >> 1U);
2042 pCID
->Reserved2
= 1U;
2048 * @brief Returns information the information of the card which are stored on
2050 * @param hsd: Pointer to SD handle
2051 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
2052 * contains all CSD register parameters
2053 * @retval HAL status
2055 HAL_StatusTypeDef
HAL_SD_GetCardCSD(SD_HandleTypeDef
*hsd
, HAL_SD_CardCSDTypeDef
*pCSD
)
2057 pCSD
->CSDStruct
= (uint8_t)((hsd
->CSD
[0] & 0xC0000000U
) >> 30U);
2059 pCSD
->SysSpecVersion
= (uint8_t)((hsd
->CSD
[0] & 0x3C000000U
) >> 26U);
2061 pCSD
->Reserved1
= (uint8_t)((hsd
->CSD
[0] & 0x03000000U
) >> 24U);
2063 pCSD
->TAAC
= (uint8_t)((hsd
->CSD
[0] & 0x00FF0000U
) >> 16U);
2065 pCSD
->NSAC
= (uint8_t)((hsd
->CSD
[0] & 0x0000FF00U
) >> 8U);
2067 pCSD
->MaxBusClkFrec
= (uint8_t)(hsd
->CSD
[0] & 0x000000FFU
);
2069 pCSD
->CardComdClasses
= (uint16_t)((hsd
->CSD
[1] & 0xFFF00000U
) >> 20U);
2071 pCSD
->RdBlockLen
= (uint8_t)((hsd
->CSD
[1] & 0x000F0000U
) >> 16U);
2073 pCSD
->PartBlockRead
= (uint8_t)((hsd
->CSD
[1] & 0x00008000U
) >> 15U);
2075 pCSD
->WrBlockMisalign
= (uint8_t)((hsd
->CSD
[1] & 0x00004000U
) >> 14U);
2077 pCSD
->RdBlockMisalign
= (uint8_t)((hsd
->CSD
[1] & 0x00002000U
) >> 13U);
2079 pCSD
->DSRImpl
= (uint8_t)((hsd
->CSD
[1] & 0x00001000U
) >> 12U);
2081 pCSD
->Reserved2
= 0U; /*!< Reserved */
2083 if(hsd
->SdCard
.CardType
== CARD_SDSC
)
2085 pCSD
->DeviceSize
= (((hsd
->CSD
[1] & 0x000003FFU
) << 2U) | ((hsd
->CSD
[2] & 0xC0000000U
) >> 30U));
2087 pCSD
->MaxRdCurrentVDDMin
= (uint8_t)((hsd
->CSD
[2] & 0x38000000U
) >> 27U);
2089 pCSD
->MaxRdCurrentVDDMax
= (uint8_t)((hsd
->CSD
[2] & 0x07000000U
) >> 24U);
2091 pCSD
->MaxWrCurrentVDDMin
= (uint8_t)((hsd
->CSD
[2] & 0x00E00000U
) >> 21U);
2093 pCSD
->MaxWrCurrentVDDMax
= (uint8_t)((hsd
->CSD
[2] & 0x001C0000U
) >> 18U);
2095 pCSD
->DeviceSizeMul
= (uint8_t)((hsd
->CSD
[2] & 0x00038000U
) >> 15U);
2097 hsd
->SdCard
.BlockNbr
= (pCSD
->DeviceSize
+ 1U) ;
2098 hsd
->SdCard
.BlockNbr
*= (1UL << ((pCSD
->DeviceSizeMul
& 0x07U
) + 2U));
2099 hsd
->SdCard
.BlockSize
= (1UL << (pCSD
->RdBlockLen
& 0x0FU
));
2101 hsd
->SdCard
.LogBlockNbr
= (hsd
->SdCard
.BlockNbr
) * ((hsd
->SdCard
.BlockSize
) / 512U);
2102 hsd
->SdCard
.LogBlockSize
= 512U;
2104 else if(hsd
->SdCard
.CardType
== CARD_SDHC_SDXC
)
2107 pCSD
->DeviceSize
= (((hsd
->CSD
[1] & 0x0000003FU
) << 16U) | ((hsd
->CSD
[2] & 0xFFFF0000U
) >> 16U));
2109 hsd
->SdCard
.BlockNbr
= ((pCSD
->DeviceSize
+ 1U) * 1024U);
2110 hsd
->SdCard
.LogBlockNbr
= hsd
->SdCard
.BlockNbr
;
2111 hsd
->SdCard
.BlockSize
= 512U;
2112 hsd
->SdCard
.LogBlockSize
= hsd
->SdCard
.BlockSize
;
2116 /* Clear all the static flags */
2117 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
2118 hsd
->ErrorCode
|= HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2119 hsd
->State
= HAL_SD_STATE_READY
;
2123 pCSD
->EraseGrSize
= (uint8_t)((hsd
->CSD
[2] & 0x00004000U
) >> 14U);
2125 pCSD
->EraseGrMul
= (uint8_t)((hsd
->CSD
[2] & 0x00003F80U
) >> 7U);
2127 pCSD
->WrProtectGrSize
= (uint8_t)(hsd
->CSD
[2] & 0x0000007FU
);
2129 pCSD
->WrProtectGrEnable
= (uint8_t)((hsd
->CSD
[3] & 0x80000000U
) >> 31U);
2131 pCSD
->ManDeflECC
= (uint8_t)((hsd
->CSD
[3] & 0x60000000U
) >> 29U);
2133 pCSD
->WrSpeedFact
= (uint8_t)((hsd
->CSD
[3] & 0x1C000000U
) >> 26U);
2135 pCSD
->MaxWrBlockLen
= (uint8_t)((hsd
->CSD
[3] & 0x03C00000U
) >> 22U);
2137 pCSD
->WriteBlockPaPartial
= (uint8_t)((hsd
->CSD
[3] & 0x00200000U
) >> 21U);
2139 pCSD
->Reserved3
= 0;
2141 pCSD
->ContentProtectAppli
= (uint8_t)((hsd
->CSD
[3] & 0x00010000U
) >> 16U);
2143 pCSD
->FileFormatGroup
= (uint8_t)((hsd
->CSD
[3] & 0x00008000U
) >> 15U);
2145 pCSD
->CopyFlag
= (uint8_t)((hsd
->CSD
[3] & 0x00004000U
) >> 14U);
2147 pCSD
->PermWrProtect
= (uint8_t)((hsd
->CSD
[3] & 0x00002000U
) >> 13U);
2149 pCSD
->TempWrProtect
= (uint8_t)((hsd
->CSD
[3] & 0x00001000U
) >> 12U);
2151 pCSD
->FileFormat
= (uint8_t)((hsd
->CSD
[3] & 0x00000C00U
) >> 10U);
2153 pCSD
->ECC
= (uint8_t)((hsd
->CSD
[3] & 0x00000300U
) >> 8U);
2155 pCSD
->CSD_CRC
= (uint8_t)((hsd
->CSD
[3] & 0x000000FEU
) >> 1U);
2157 pCSD
->Reserved4
= 1;
2163 * @brief Gets the SD status info.
2164 * @param hsd: Pointer to SD handle
2165 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
2166 * will contain the SD card status information
2167 * @retval HAL status
2169 HAL_StatusTypeDef
HAL_SD_GetCardStatus(SD_HandleTypeDef
*hsd
, HAL_SD_CardStatusTypeDef
*pStatus
)
2171 uint32_t sd_status
[16];
2172 uint32_t errorstate
;
2174 errorstate
= SD_SendSDStatus(hsd
, sd_status
);
2175 if(errorstate
!= HAL_SD_ERROR_NONE
)
2177 /* Clear all the static flags */
2178 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
2179 hsd
->ErrorCode
|= errorstate
;
2180 hsd
->State
= HAL_SD_STATE_READY
;
2185 pStatus
->DataBusWidth
= (uint8_t)((sd_status
[0] & 0xC0U
) >> 6U);
2187 pStatus
->SecuredMode
= (uint8_t)((sd_status
[0] & 0x20U
) >> 5U);
2189 pStatus
->CardType
= (uint16_t)(((sd_status
[0] & 0x00FF0000U
) >> 8U) | ((sd_status
[0] & 0xFF000000U
) >> 24U));
2191 pStatus
->ProtectedAreaSize
= (((sd_status
[1] & 0xFFU
) << 24U) | ((sd_status
[1] & 0xFF00U
) << 8U) |
2192 ((sd_status
[1] & 0xFF0000U
) >> 8U) | ((sd_status
[1] & 0xFF000000U
) >> 24U));
2194 pStatus
->SpeedClass
= (uint8_t)(sd_status
[2] & 0xFFU
);
2196 pStatus
->PerformanceMove
= (uint8_t)((sd_status
[2] & 0xFF00U
) >> 8U);
2198 pStatus
->AllocationUnitSize
= (uint8_t)((sd_status
[2] & 0xF00000U
) >> 20U);
2200 pStatus
->EraseSize
= (uint16_t)(((sd_status
[2] & 0xFF000000U
) >> 16U) | (sd_status
[3] & 0xFFU
));
2202 pStatus
->EraseTimeout
= (uint8_t)((sd_status
[3] & 0xFC00U
) >> 10U);
2204 pStatus
->EraseOffset
= (uint8_t)((sd_status
[3] & 0x0300U
) >> 8U);
2211 * @brief Gets the SD card info.
2212 * @param hsd: Pointer to SD handle
2213 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
2214 * will contain the SD card status information
2215 * @retval HAL status
2217 HAL_StatusTypeDef
HAL_SD_GetCardInfo(SD_HandleTypeDef
*hsd
, HAL_SD_CardInfoTypeDef
*pCardInfo
)
2219 pCardInfo
->CardType
= (uint32_t)(hsd
->SdCard
.CardType
);
2220 pCardInfo
->CardVersion
= (uint32_t)(hsd
->SdCard
.CardVersion
);
2221 pCardInfo
->Class
= (uint32_t)(hsd
->SdCard
.Class
);
2222 pCardInfo
->RelCardAdd
= (uint32_t)(hsd
->SdCard
.RelCardAdd
);
2223 pCardInfo
->BlockNbr
= (uint32_t)(hsd
->SdCard
.BlockNbr
);
2224 pCardInfo
->BlockSize
= (uint32_t)(hsd
->SdCard
.BlockSize
);
2225 pCardInfo
->LogBlockNbr
= (uint32_t)(hsd
->SdCard
.LogBlockNbr
);
2226 pCardInfo
->LogBlockSize
= (uint32_t)(hsd
->SdCard
.LogBlockSize
);
2232 * @brief Enables wide bus operation for the requested card if supported by
2234 * @param hsd: Pointer to SD handle
2235 * @param WideMode: Specifies the SD card wide bus mode
2236 * This parameter can be one of the following values:
2237 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
2238 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
2239 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
2240 * @retval HAL status
2242 HAL_StatusTypeDef
HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef
*hsd
, uint32_t WideMode
)
2244 SDIO_InitTypeDef Init
;
2245 uint32_t errorstate
;
2247 /* Check the parameters */
2248 assert_param(IS_SDIO_BUS_WIDE(WideMode
));
2251 hsd
->State
= HAL_SD_STATE_BUSY
;
2253 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
2255 if(WideMode
== SDIO_BUS_WIDE_8B
)
2257 hsd
->ErrorCode
|= HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2259 else if(WideMode
== SDIO_BUS_WIDE_4B
)
2261 errorstate
= SD_WideBus_Enable(hsd
);
2263 hsd
->ErrorCode
|= errorstate
;
2265 else if(WideMode
== SDIO_BUS_WIDE_1B
)
2267 errorstate
= SD_WideBus_Disable(hsd
);
2269 hsd
->ErrorCode
|= errorstate
;
2273 /* WideMode is not a valid argument*/
2274 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
2279 /* MMC Card does not support this feature */
2280 hsd
->ErrorCode
|= HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2283 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2285 /* Clear all the static flags */
2286 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
2287 hsd
->State
= HAL_SD_STATE_READY
;
2292 /* Configure the SDIO peripheral */
2293 Init
.ClockEdge
= hsd
->Init
.ClockEdge
;
2294 Init
.ClockBypass
= hsd
->Init
.ClockBypass
;
2295 Init
.ClockPowerSave
= hsd
->Init
.ClockPowerSave
;
2296 Init
.BusWide
= WideMode
;
2297 Init
.HardwareFlowControl
= hsd
->Init
.HardwareFlowControl
;
2298 Init
.ClockDiv
= hsd
->Init
.ClockDiv
;
2299 (void)SDIO_Init(hsd
->Instance
, Init
);
2303 hsd
->State
= HAL_SD_STATE_READY
;
2309 * @brief Gets the current sd card data state.
2310 * @param hsd: pointer to SD handle
2311 * @retval Card state
2313 HAL_SD_CardStateTypeDef
HAL_SD_GetCardState(SD_HandleTypeDef
*hsd
)
2316 uint32_t errorstate
;
2319 errorstate
= SD_SendStatus(hsd
, &resp1
);
2320 if(errorstate
!= HAL_SD_ERROR_NONE
)
2322 hsd
->ErrorCode
|= errorstate
;
2325 cardstate
= ((resp1
>> 9U) & 0x0FU
);
2327 return (HAL_SD_CardStateTypeDef
)cardstate
;
2331 * @brief Abort the current transfer and disable the SD.
2332 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2333 * the configuration information for SD module.
2334 * @retval HAL status
2336 HAL_StatusTypeDef
HAL_SD_Abort(SD_HandleTypeDef
*hsd
)
2338 HAL_SD_CardStateTypeDef CardState
;
2339 uint32_t context
= hsd
->Context
;
2341 /* DIsable All interrupts */
2342 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
2343 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
);
2345 /* Clear All flags */
2346 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_DATA_FLAGS
);
2348 CLEAR_BIT(hsd
->Instance
->DCTRL
, SDIO_DCTRL_DTEN
);
2350 if ((context
& SD_CONTEXT_DMA
) != 0U)
2352 /* Disable the SD DMA request */
2353 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN
);
2355 /* Abort the SD DMA Tx channel */
2356 if (((context
& SD_CONTEXT_WRITE_SINGLE_BLOCK
) != 0U) || ((context
& SD_CONTEXT_WRITE_MULTIPLE_BLOCK
) != 0U))
2358 if(HAL_DMA_Abort(hsd
->hdmatx
) != HAL_OK
)
2360 hsd
->ErrorCode
|= HAL_SD_ERROR_DMA
;
2363 /* Abort the SD DMA Rx channel */
2364 else if (((context
& SD_CONTEXT_READ_SINGLE_BLOCK
) != 0U) || ((context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) != 0U))
2366 if(HAL_DMA_Abort(hsd
->hdmarx
) != HAL_OK
)
2368 hsd
->ErrorCode
|= HAL_SD_ERROR_DMA
;
2377 hsd
->State
= HAL_SD_STATE_READY
;
2379 /* Initialize the SD operation */
2380 hsd
->Context
= SD_CONTEXT_NONE
;
2382 CardState
= HAL_SD_GetCardState(hsd
);
2383 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2385 hsd
->ErrorCode
= SDMMC_CmdStopTransfer(hsd
->Instance
);
2387 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2395 * @brief Abort the current transfer and disable the SD (IT mode).
2396 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2397 * the configuration information for SD module.
2398 * @retval HAL status
2400 HAL_StatusTypeDef
HAL_SD_Abort_IT(SD_HandleTypeDef
*hsd
)
2402 HAL_SD_CardStateTypeDef CardState
;
2403 uint32_t context
= hsd
->Context
;
2405 /* Disable All interrupts */
2406 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
2407 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
);
2409 CLEAR_BIT(hsd
->Instance
->DCTRL
, SDIO_DCTRL_DTEN
);
2411 if ((context
& SD_CONTEXT_DMA
) != 0U)
2413 /* Disable the SD DMA request */
2414 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN
);
2416 /* Abort the SD DMA Tx channel */
2417 if (((context
& SD_CONTEXT_WRITE_SINGLE_BLOCK
) != 0U) || ((context
& SD_CONTEXT_WRITE_MULTIPLE_BLOCK
) != 0U))
2419 hsd
->hdmatx
->XferAbortCallback
= SD_DMATxAbort
;
2420 if(HAL_DMA_Abort_IT(hsd
->hdmatx
) != HAL_OK
)
2425 /* Abort the SD DMA Rx channel */
2426 else if (((context
& SD_CONTEXT_READ_SINGLE_BLOCK
) != 0U) || ((context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) != 0U))
2428 hsd
->hdmarx
->XferAbortCallback
= SD_DMARxAbort
;
2429 if(HAL_DMA_Abort_IT(hsd
->hdmarx
) != HAL_OK
)
2439 /* No transfer ongoing on both DMA channels*/
2442 /* Clear All flags */
2443 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_DATA_FLAGS
);
2445 CardState
= HAL_SD_GetCardState(hsd
);
2446 hsd
->State
= HAL_SD_STATE_READY
;
2447 hsd
->Context
= SD_CONTEXT_NONE
;
2448 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2450 hsd
->ErrorCode
= SDMMC_CmdStopTransfer(hsd
->Instance
);
2452 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2458 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2459 hsd
->AbortCpltCallback(hsd
);
2461 HAL_SD_AbortCallback(hsd
);
2462 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2477 /* Private function ----------------------------------------------------------*/
2478 /** @addtogroup SD_Private_Functions
2483 * @brief DMA SD transmit process complete callback
2484 * @param hdma: DMA handle
2487 static void SD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
2489 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2491 /* Enable DATAEND Interrupt */
2492 __HAL_SD_ENABLE_IT(hsd
, (SDIO_IT_DATAEND
));
2496 * @brief DMA SD receive process complete callback
2497 * @param hdma: DMA handle
2500 static void SD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
2502 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2503 uint32_t errorstate
;
2505 /* Send stop command in multiblock write */
2506 if(hsd
->Context
== (SD_CONTEXT_READ_MULTIPLE_BLOCK
| SD_CONTEXT_DMA
))
2508 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
2509 if(errorstate
!= HAL_SD_ERROR_NONE
)
2511 hsd
->ErrorCode
|= errorstate
;
2512 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2513 hsd
->ErrorCallback(hsd
);
2515 HAL_SD_ErrorCallback(hsd
);
2520 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2521 in the SD DCTRL register */
2522 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN
);
2524 /* Clear all the static flags */
2525 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_DATA_FLAGS
);
2527 hsd
->State
= HAL_SD_STATE_READY
;
2528 hsd
->Context
= SD_CONTEXT_NONE
;
2530 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2531 hsd
->RxCpltCallback(hsd
);
2533 HAL_SD_RxCpltCallback(hsd
);
2538 * @brief DMA SD communication error callback
2539 * @param hdma: DMA handle
2542 static void SD_DMAError(DMA_HandleTypeDef
*hdma
)
2544 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2545 HAL_SD_CardStateTypeDef CardState
;
2546 uint32_t RxErrorCode
, TxErrorCode
;
2548 /* if DMA error is FIFO error ignore it */
2549 if(HAL_DMA_GetError(hdma
) != HAL_DMA_ERROR_FE
)
2551 RxErrorCode
= hsd
->hdmarx
->ErrorCode
;
2552 TxErrorCode
= hsd
->hdmatx
->ErrorCode
;
2553 if((RxErrorCode
== HAL_DMA_ERROR_TE
) || (TxErrorCode
== HAL_DMA_ERROR_TE
))
2555 /* Clear All flags */
2556 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
2558 /* Disable All interrupts */
2559 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
2560 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
);
2562 hsd
->ErrorCode
|= HAL_SD_ERROR_DMA
;
2563 CardState
= HAL_SD_GetCardState(hsd
);
2564 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2566 hsd
->ErrorCode
|= SDMMC_CmdStopTransfer(hsd
->Instance
);
2569 hsd
->State
= HAL_SD_STATE_READY
;
2570 hsd
->Context
= SD_CONTEXT_NONE
;
2573 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2574 hsd
->ErrorCallback(hsd
);
2576 HAL_SD_ErrorCallback(hsd
);
2582 * @brief DMA SD Tx Abort callback
2583 * @param hdma: DMA handle
2586 static void SD_DMATxAbort(DMA_HandleTypeDef
*hdma
)
2588 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2589 HAL_SD_CardStateTypeDef CardState
;
2591 /* Clear All flags */
2592 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_DATA_FLAGS
);
2594 CardState
= HAL_SD_GetCardState(hsd
);
2595 hsd
->State
= HAL_SD_STATE_READY
;
2596 hsd
->Context
= SD_CONTEXT_NONE
;
2597 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2599 hsd
->ErrorCode
|= SDMMC_CmdStopTransfer(hsd
->Instance
);
2602 if(hsd
->ErrorCode
== HAL_SD_ERROR_NONE
)
2604 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2605 hsd
->AbortCpltCallback(hsd
);
2607 HAL_SD_AbortCallback(hsd
);
2612 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2613 hsd
->ErrorCallback(hsd
);
2615 HAL_SD_ErrorCallback(hsd
);
2621 * @brief DMA SD Rx Abort callback
2622 * @param hdma: DMA handle
2625 static void SD_DMARxAbort(DMA_HandleTypeDef
*hdma
)
2627 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2628 HAL_SD_CardStateTypeDef CardState
;
2630 /* Clear All flags */
2631 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_DATA_FLAGS
);
2633 CardState
= HAL_SD_GetCardState(hsd
);
2634 hsd
->State
= HAL_SD_STATE_READY
;
2635 hsd
->Context
= SD_CONTEXT_NONE
;
2636 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2638 hsd
->ErrorCode
|= SDMMC_CmdStopTransfer(hsd
->Instance
);
2641 if(hsd
->ErrorCode
== HAL_SD_ERROR_NONE
)
2643 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2644 hsd
->AbortCpltCallback(hsd
);
2646 HAL_SD_AbortCallback(hsd
);
2651 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2652 hsd
->ErrorCallback(hsd
);
2654 HAL_SD_ErrorCallback(hsd
);
2660 * @brief Initializes the sd card.
2661 * @param hsd: Pointer to SD handle
2662 * @retval SD Card error state
2664 static uint32_t SD_InitCard(SD_HandleTypeDef
*hsd
)
2666 HAL_SD_CardCSDTypeDef CSD
;
2667 uint32_t errorstate
;
2668 uint16_t sd_rca
= 1U;
2670 /* Check the power State */
2671 if(SDIO_GetPowerState(hsd
->Instance
) == 0U)
2674 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
2677 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
2679 /* Send CMD2 ALL_SEND_CID */
2680 errorstate
= SDMMC_CmdSendCID(hsd
->Instance
);
2681 if(errorstate
!= HAL_SD_ERROR_NONE
)
2687 /* Get Card identification number data */
2688 hsd
->CID
[0U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
);
2689 hsd
->CID
[1U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP2
);
2690 hsd
->CID
[2U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP3
);
2691 hsd
->CID
[3U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP4
);
2695 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
2697 /* Send CMD3 SET_REL_ADDR with argument 0 */
2698 /* SD Card publishes its RCA. */
2699 errorstate
= SDMMC_CmdSetRelAdd(hsd
->Instance
, &sd_rca
);
2700 if(errorstate
!= HAL_SD_ERROR_NONE
)
2705 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
2707 /* Get the SD card RCA */
2708 hsd
->SdCard
.RelCardAdd
= sd_rca
;
2710 /* Send CMD9 SEND_CSD with argument as card's RCA */
2711 errorstate
= SDMMC_CmdSendCSD(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16U));
2712 if(errorstate
!= HAL_SD_ERROR_NONE
)
2718 /* Get Card Specific Data */
2719 hsd
->CSD
[0U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
);
2720 hsd
->CSD
[1U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP2
);
2721 hsd
->CSD
[2U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP3
);
2722 hsd
->CSD
[3U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP4
);
2726 /* Get the Card Class */
2727 hsd
->SdCard
.Class
= (SDIO_GetResponse(hsd
->Instance
, SDIO_RESP2
) >> 20U);
2729 /* Get CSD parameters */
2730 if (HAL_SD_GetCardCSD(hsd
, &CSD
) != HAL_OK
)
2732 return HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2735 /* Select the Card */
2736 errorstate
= SDMMC_CmdSelDesel(hsd
->Instance
, (uint32_t)(((uint32_t)hsd
->SdCard
.RelCardAdd
) << 16U));
2737 if(errorstate
!= HAL_SD_ERROR_NONE
)
2742 /* Configure SDIO peripheral interface */
2743 (void)SDIO_Init(hsd
->Instance
, hsd
->Init
);
2745 /* All cards are initialized */
2746 return HAL_SD_ERROR_NONE
;
2750 * @brief Enquires cards about their operating voltage and configures clock
2751 * controls and stores SD information that will be needed in future
2753 * @param hsd: Pointer to SD handle
2754 * @retval error state
2756 static uint32_t SD_PowerON(SD_HandleTypeDef
*hsd
)
2758 __IO
uint32_t count
= 0U;
2759 uint32_t response
= 0U, validvoltage
= 0U;
2760 uint32_t errorstate
;
2762 /* CMD0: GO_IDLE_STATE */
2763 errorstate
= SDMMC_CmdGoIdleState(hsd
->Instance
);
2764 if(errorstate
!= HAL_SD_ERROR_NONE
)
2769 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2770 errorstate
= SDMMC_CmdOperCond(hsd
->Instance
);
2771 if(errorstate
!= HAL_SD_ERROR_NONE
)
2773 hsd
->SdCard
.CardVersion
= CARD_V1_X
;
2774 /* CMD0: GO_IDLE_STATE */
2775 errorstate
= SDMMC_CmdGoIdleState(hsd
->Instance
);
2776 if(errorstate
!= HAL_SD_ERROR_NONE
)
2784 hsd
->SdCard
.CardVersion
= CARD_V2_X
;
2787 if( hsd
->SdCard
.CardVersion
== CARD_V2_X
)
2789 /* SEND CMD55 APP_CMD with RCA as 0 */
2790 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, 0);
2791 if(errorstate
!= HAL_SD_ERROR_NONE
)
2793 return HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2797 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2798 while((count
< SDMMC_MAX_VOLT_TRIAL
) && (validvoltage
== 0U))
2800 /* SEND CMD55 APP_CMD with RCA as 0 */
2801 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, 0);
2802 if(errorstate
!= HAL_SD_ERROR_NONE
)
2808 errorstate
= SDMMC_CmdAppOperCommand(hsd
->Instance
, SDMMC_VOLTAGE_WINDOW_SD
| SDMMC_HIGH_CAPACITY
| SD_SWITCH_1_8V_CAPACITY
);
2809 if(errorstate
!= HAL_SD_ERROR_NONE
)
2811 return HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2814 /* Get command response */
2815 response
= SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
);
2817 /* Get operating voltage*/
2818 validvoltage
= (((response
>> 31U) == 1U) ? 1U : 0U);
2823 if(count
>= SDMMC_MAX_VOLT_TRIAL
)
2825 return HAL_SD_ERROR_INVALID_VOLTRANGE
;
2828 if((response
& SDMMC_HIGH_CAPACITY
) == SDMMC_HIGH_CAPACITY
) /* (response &= SD_HIGH_CAPACITY) */
2830 hsd
->SdCard
.CardType
= CARD_SDHC_SDXC
;
2834 hsd
->SdCard
.CardType
= CARD_SDSC
;
2838 return HAL_SD_ERROR_NONE
;
2842 * @brief Turns the SDIO output signals off.
2843 * @param hsd: Pointer to SD handle
2846 static void SD_PowerOFF(SD_HandleTypeDef
*hsd
)
2848 /* Set Power State to OFF */
2849 (void)SDIO_PowerState_OFF(hsd
->Instance
);
2853 * @brief Send Status info command.
2854 * @param hsd: pointer to SD handle
2855 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
2856 * SD Status register)
2857 * @retval error state
2859 static uint32_t SD_SendSDStatus(SD_HandleTypeDef
*hsd
, uint32_t *pSDstatus
)
2861 SDIO_DataInitTypeDef config
;
2862 uint32_t errorstate
;
2863 uint32_t tickstart
= HAL_GetTick();
2865 uint32_t *pData
= pSDstatus
;
2867 /* Check SD response */
2868 if((SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
2870 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
2873 /* Set block size for card if it is not equal to current block size for card */
2874 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, 64U);
2875 if(errorstate
!= HAL_SD_ERROR_NONE
)
2877 hsd
->ErrorCode
|= HAL_SD_ERROR_NONE
;
2882 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16U));
2883 if(errorstate
!= HAL_SD_ERROR_NONE
)
2885 hsd
->ErrorCode
|= HAL_SD_ERROR_NONE
;
2889 /* Configure the SD DPSM (Data Path State Machine) */
2890 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
2891 config
.DataLength
= 64U;
2892 config
.DataBlockSize
= SDIO_DATABLOCK_SIZE_64B
;
2893 config
.TransferDir
= SDIO_TRANSFER_DIR_TO_SDIO
;
2894 config
.TransferMode
= SDIO_TRANSFER_MODE_BLOCK
;
2895 config
.DPSM
= SDIO_DPSM_ENABLE
;
2896 (void)SDIO_ConfigData(hsd
->Instance
, &config
);
2898 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
2899 errorstate
= SDMMC_CmdStatusRegister(hsd
->Instance
);
2900 if(errorstate
!= HAL_SD_ERROR_NONE
)
2902 hsd
->ErrorCode
|= HAL_SD_ERROR_NONE
;
2906 /* Get status data */
2907 while(!__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
| SDIO_FLAG_DCRCFAIL
| SDIO_FLAG_DTIMEOUT
| SDIO_FLAG_DBCKEND
))
2909 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXFIFOHF
))
2911 for(count
= 0U; count
< 8U; count
++)
2913 *pData
= SDIO_ReadFIFO(hsd
->Instance
);
2918 if((HAL_GetTick() - tickstart
) >= SDMMC_DATATIMEOUT
)
2920 return HAL_SD_ERROR_TIMEOUT
;
2924 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DTIMEOUT
))
2926 return HAL_SD_ERROR_DATA_TIMEOUT
;
2928 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DCRCFAIL
))
2930 return HAL_SD_ERROR_DATA_CRC_FAIL
;
2932 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
))
2934 return HAL_SD_ERROR_RX_OVERRUN
;
2941 while ((__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXDAVL
)))
2943 *pData
= SDIO_ReadFIFO(hsd
->Instance
);
2946 if((HAL_GetTick() - tickstart
) >= SDMMC_DATATIMEOUT
)
2948 return HAL_SD_ERROR_TIMEOUT
;
2952 /* Clear all the static status flags*/
2953 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_DATA_FLAGS
);
2955 return HAL_SD_ERROR_NONE
;
2959 * @brief Returns the current card's status.
2960 * @param hsd: Pointer to SD handle
2961 * @param pCardStatus: pointer to the buffer that will contain the SD card
2962 * status (Card Status register)
2963 * @retval error state
2965 static uint32_t SD_SendStatus(SD_HandleTypeDef
*hsd
, uint32_t *pCardStatus
)
2967 uint32_t errorstate
;
2969 if(pCardStatus
== NULL
)
2971 return HAL_SD_ERROR_PARAM
;
2974 /* Send Status command */
2975 errorstate
= SDMMC_CmdSendStatus(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16U));
2976 if(errorstate
!= HAL_SD_ERROR_NONE
)
2981 /* Get SD card status */
2982 *pCardStatus
= SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
);
2984 return HAL_SD_ERROR_NONE
;
2988 * @brief Enables the SDIO wide bus mode.
2989 * @param hsd: pointer to SD handle
2990 * @retval error state
2992 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef
*hsd
)
2994 uint32_t scr
[2U] = {0U, 0U};
2995 uint32_t errorstate
;
2997 if((SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
2999 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
3002 /* Get SCR Register */
3003 errorstate
= SD_FindSCR(hsd
, scr
);
3004 if(errorstate
!= HAL_SD_ERROR_NONE
)
3009 /* If requested card supports wide bus operation */
3010 if((scr
[1U] & SDMMC_WIDE_BUS_SUPPORT
) != SDMMC_ALLZERO
)
3012 /* Send CMD55 APP_CMD with argument as card's RCA.*/
3013 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16U));
3014 if(errorstate
!= HAL_SD_ERROR_NONE
)
3019 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3020 errorstate
= SDMMC_CmdBusWidth(hsd
->Instance
, 2U);
3021 if(errorstate
!= HAL_SD_ERROR_NONE
)
3026 return HAL_SD_ERROR_NONE
;
3030 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
3035 * @brief Disables the SDIO wide bus mode.
3036 * @param hsd: Pointer to SD handle
3037 * @retval error state
3039 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef
*hsd
)
3041 uint32_t scr
[2U] = {0U, 0U};
3042 uint32_t errorstate
;
3044 if((SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
3046 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
3049 /* Get SCR Register */
3050 errorstate
= SD_FindSCR(hsd
, scr
);
3051 if(errorstate
!= HAL_SD_ERROR_NONE
)
3056 /* If requested card supports 1 bit mode operation */
3057 if((scr
[1U] & SDMMC_SINGLE_BUS_SUPPORT
) != SDMMC_ALLZERO
)
3059 /* Send CMD55 APP_CMD with argument as card's RCA */
3060 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16U));
3061 if(errorstate
!= HAL_SD_ERROR_NONE
)
3066 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3067 errorstate
= SDMMC_CmdBusWidth(hsd
->Instance
, 0U);
3068 if(errorstate
!= HAL_SD_ERROR_NONE
)
3073 return HAL_SD_ERROR_NONE
;
3077 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
3083 * @brief Finds the SD card SCR register value.
3084 * @param hsd: Pointer to SD handle
3085 * @param pSCR: pointer to the buffer that will contain the SCR value
3086 * @retval error state
3088 static uint32_t SD_FindSCR(SD_HandleTypeDef
*hsd
, uint32_t *pSCR
)
3090 SDIO_DataInitTypeDef config
;
3091 uint32_t errorstate
;
3092 uint32_t tickstart
= HAL_GetTick();
3093 uint32_t index
= 0U;
3094 uint32_t tempscr
[2U] = {0U, 0U};
3095 uint32_t *scr
= pSCR
;
3097 /* Set Block Size To 8 Bytes */
3098 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, 8U);
3099 if(errorstate
!= HAL_SD_ERROR_NONE
)
3104 /* Send CMD55 APP_CMD with argument as card's RCA */
3105 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)((hsd
->SdCard
.RelCardAdd
) << 16U));
3106 if(errorstate
!= HAL_SD_ERROR_NONE
)
3111 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
3112 config
.DataLength
= 8U;
3113 config
.DataBlockSize
= SDIO_DATABLOCK_SIZE_8B
;
3114 config
.TransferDir
= SDIO_TRANSFER_DIR_TO_SDIO
;
3115 config
.TransferMode
= SDIO_TRANSFER_MODE_BLOCK
;
3116 config
.DPSM
= SDIO_DPSM_ENABLE
;
3117 (void)SDIO_ConfigData(hsd
->Instance
, &config
);
3119 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3120 errorstate
= SDMMC_CmdSendSCR(hsd
->Instance
);
3121 if(errorstate
!= HAL_SD_ERROR_NONE
)
3126 while(!__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
| SDIO_FLAG_DCRCFAIL
| SDIO_FLAG_DTIMEOUT
| SDIO_FLAG_DBCKEND
))
3128 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXDAVL
))
3130 *(tempscr
+ index
) = SDIO_ReadFIFO(hsd
->Instance
);
3134 if((HAL_GetTick() - tickstart
) >= SDMMC_DATATIMEOUT
)
3136 return HAL_SD_ERROR_TIMEOUT
;
3140 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DTIMEOUT
))
3142 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_FLAG_DTIMEOUT
);
3144 return HAL_SD_ERROR_DATA_TIMEOUT
;
3146 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DCRCFAIL
))
3148 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_FLAG_DCRCFAIL
);
3150 return HAL_SD_ERROR_DATA_CRC_FAIL
;
3152 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
))
3154 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_FLAG_RXOVERR
);
3156 return HAL_SD_ERROR_RX_OVERRUN
;
3160 /* No error flag set */
3161 /* Clear all the static flags */
3162 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_DATA_FLAGS
);
3164 *scr
= (((tempscr
[1] & SDMMC_0TO7BITS
) << 24) | ((tempscr
[1] & SDMMC_8TO15BITS
) << 8) |\
3165 ((tempscr
[1] & SDMMC_16TO23BITS
) >> 8) | ((tempscr
[1] & SDMMC_24TO31BITS
) >> 24));
3167 *scr
= (((tempscr
[0] & SDMMC_0TO7BITS
) << 24) | ((tempscr
[0] & SDMMC_8TO15BITS
) << 8) |\
3168 ((tempscr
[0] & SDMMC_16TO23BITS
) >> 8) | ((tempscr
[0] & SDMMC_24TO31BITS
) >> 24));
3172 return HAL_SD_ERROR_NONE
;
3176 * @brief Wrap up reading in non-blocking mode.
3177 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3178 * the configuration information.
3181 static void SD_Read_IT(SD_HandleTypeDef
*hsd
)
3183 uint32_t count
, data
, dataremaining
;
3186 tmp
= hsd
->pRxBuffPtr
;
3187 dataremaining
= hsd
->RxXferSize
;
3189 if (dataremaining
> 0U)
3191 /* Read data from SDIO Rx FIFO */
3192 for(count
= 0U; count
< 8U; count
++)
3194 data
= SDIO_ReadFIFO(hsd
->Instance
);
3195 *tmp
= (uint8_t)(data
& 0xFFU
);
3198 *tmp
= (uint8_t)((data
>> 8U) & 0xFFU
);
3201 *tmp
= (uint8_t)((data
>> 16U) & 0xFFU
);
3204 *tmp
= (uint8_t)((data
>> 24U) & 0xFFU
);
3209 hsd
->pRxBuffPtr
= tmp
;
3210 hsd
->RxXferSize
= dataremaining
;
3215 * @brief Wrap up writing in non-blocking mode.
3216 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3217 * the configuration information.
3220 static void SD_Write_IT(SD_HandleTypeDef
*hsd
)
3222 uint32_t count
, data
, dataremaining
;
3225 tmp
= hsd
->pTxBuffPtr
;
3226 dataremaining
= hsd
->TxXferSize
;
3228 if (dataremaining
> 0U)
3230 /* Write data to SDIO Tx FIFO */
3231 for(count
= 0U; count
< 8U; count
++)
3233 data
= (uint32_t)(*tmp
);
3236 data
|= ((uint32_t)(*tmp
) << 8U);
3239 data
|= ((uint32_t)(*tmp
) << 16U);
3242 data
|= ((uint32_t)(*tmp
) << 24U);
3245 (void)SDIO_WriteFIFO(hsd
->Instance
, &data
);
3248 hsd
->pTxBuffPtr
= tmp
;
3249 hsd
->TxXferSize
= dataremaining
;
3257 #endif /* HAL_SD_MODULE_ENABLED */
3269 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/