STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
stm32h7xx_hal_sd.c
Go to the documentation of this file.
1/**
2 ******************************************************************************
3 * @file stm32h7xx_hal_sd.c
4 * @author MCD Application Team
5 * @brief SD card HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Secure Digital (SD) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral Control functions
11 * + Peripheral State functions
12 *
13 ******************************************************************************
14 * @attention
15 *
16 * Copyright (c) 2017 STMicroelectronics.
17 * All rights reserved.
18 *
19 * This software is licensed under terms that can be found in the LICENSE file
20 * in the root directory of this software component.
21 * If no LICENSE file comes with this software, it is provided AS-IS.
22 *
23 ******************************************************************************
24 @verbatim
25 ==============================================================================
26 ##### How to use this driver #####
27 ==============================================================================
28 [..]
29 This driver implements a high level communication layer for read and write from/to
30 this memory. The needed STM32 hardware resources (SDMMC and GPIO) are performed by
31 the user in HAL_SD_MspInit() function (MSP layer).
32 Basically, the MSP layer configuration should be the same as we provide in the
33 examples.
34 You can easily tailor this configuration according to hardware resources.
35
36 [..]
37 This driver is a generic layered driver for SDMMC memories which uses the HAL
38 SDMMC driver functions to interface with SD and uSD cards devices.
39 It is used as follows:
40
41 (#)Initialize the SDMMC low level resources by implementing the HAL_SD_MspInit() API:
42 (##) Enable the SDMMC interface clock using __HAL_RCC_SDMMC_CLK_ENABLE();
43 (##) SDMMC pins configuration for SD card
44 (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
45 (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init()
46 and according to your pin assignment;
47 (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
48 and HAL_SD_WriteBlocks_IT() APIs).
49 (+++) Configure the SDMMC interrupt priorities using function HAL_NVIC_SetPriority();
50 (+++) Enable the NVIC SDMMC IRQs using function HAL_NVIC_EnableIRQ()
51 (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
52 and __HAL_SD_DISABLE_IT() inside the communication process.
53 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
54 and __HAL_SD_CLEAR_IT()
55 (##) No general propose DMA Configuration is needed, an Internal DMA for SDMMC Peripheral are used.
56
57 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
58
59 *** SD Card Initialization and configuration ***
60 ================================================
61 [..]
62 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
63 SDMMC Peripheral(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
64 This function provide the following operations:
65
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 (SDMMC_CK) is computed as follows:
70
71 SDMMC_CK = SDMMCCLK / (2 * ClockDiv)
72
73 In initialization mode and according to the SD Card standard,
74 make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
75
76 This phase of initialization is done through SDMMC_Init() and
77 SDMMC_PowerState_ON() SDMMC low level APIs.
78
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.
83
84 This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
85 of plug-off plug-in.
86
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 SDMMC_CK frequency doesn't exceed 25MHz and 100MHz in High-speed mode switch.
91
92 (#) Select the corresponding SD Card according to the address read with the step 2.
93
94 (#) Configure the SD Card in wide bus mode: 4-bits data.
95
96 *** SD Card Read operation ***
97 ==============================
98 [..]
99 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
100 This function support only 512-bytes block length (the block size should be
101 chosen as 512 bytes).
102 You can choose either one block read operation or multiple block read operation
103 by adjusting the "NumberOfBlocks" parameter.
104 After this, you have to ensure that the transfer is done correctly. The check is done
105 through HAL_SD_GetCardState() function for SD card state.
106
107 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
108 This function support only 512-bytes block length (the block size should be
109 chosen as 512 bytes).
110 You can choose either one block read operation or multiple block read operation
111 by adjusting the "NumberOfBlocks" parameter.
112 After this, you have to ensure that the transfer is done correctly. The check is done
113 through HAL_SD_GetCardState() function for SD card state.
114 You could also check the DMA transfer process through the SD Rx interrupt event.
115
116 (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
117 This function support only 512-bytes block length (the block size should be
118 chosen as 512 bytes).
119 You can choose either one block read operation or multiple block read operation
120 by adjusting the "NumberOfBlocks" parameter.
121 After this, you have to ensure that the transfer is done correctly. The check is done
122 through HAL_SD_GetCardState() function for SD card state.
123 You could also check the IT transfer process through the SD Rx interrupt event.
124
125 *** SD Card Write operation ***
126 ===============================
127 [..]
128 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
129 This function support only 512-bytes block length (the block size should be
130 chosen as 512 bytes).
131 You can choose either one block read operation or multiple block read operation
132 by adjusting the "NumberOfBlocks" parameter.
133 After this, you have to ensure that the transfer is done correctly. The check is done
134 through HAL_SD_GetCardState() function for SD card state.
135
136 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
137 This function support only 512-bytes block length (the block size should be
138 chosen as 512 bytes).
139 You can choose either one block read operation or multiple block read operation
140 by adjusting the "NumberOfBlocks" parameter.
141 After this, you have to ensure that the transfer is done correctly. The check is done
142 through HAL_SD_GetCardState() function for SD card state.
143 You could also check the DMA transfer process through the SD Tx interrupt event.
144
145 (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
146 This function support only 512-bytes block length (the block size should be
147 chosen as 512 bytes).
148 You can choose either one block read operation or multiple block read operation
149 by adjusting the "NumberOfBlocks" parameter.
150 After this, you have to ensure that the transfer is done correctly. The check is done
151 through HAL_SD_GetCardState() function for SD card state.
152 You could also check the IT transfer process through the SD Tx interrupt event.
153
154 *** SD card status ***
155 ======================
156 [..]
157 (+) The SD Status contains status bits that are related to the SD Memory
158 Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
159
160 *** SD card information ***
161 ===========================
162 [..]
163 (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
164 It returns useful information about the SD card such as block size, card type,
165 block number ...
166
167 *** SD card CSD register ***
168 ============================
169 (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
170 Some of the CSD parameters are useful for card initialization and identification.
171
172 *** SD card CID register ***
173 ============================
174 (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
175 Some of the CSD parameters are useful for card initialization and identification.
176
177 *** SD HAL driver macros list ***
178 ==================================
179 [..]
180 Below the list of most used macros in SD HAL driver.
181
182 (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
183 (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
184 (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
185 (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
186
187 (@) You can refer to the SD HAL driver header file for more useful macros
188
189 *** Callback registration ***
190 =============================================
191 [..]
192 The compilation define USE_HAL_SD_REGISTER_CALLBACKS when set to 1
193 allows the user to configure dynamically the driver callbacks.
194
195 Use Functions HAL_SD_RegisterCallback() to register a user callback,
196 it allows to register following callbacks:
197 (+) TxCpltCallback : callback when a transmission transfer is completed.
198 (+) RxCpltCallback : callback when a reception transfer is completed.
199 (+) ErrorCallback : callback when error occurs.
200 (+) AbortCpltCallback : callback when abort is completed.
201 (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed.
202 (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed.
203 (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed.
204 (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed.
205 (+) MspInitCallback : SD MspInit.
206 (+) MspDeInitCallback : SD MspDeInit.
207 This function takes as parameters the HAL peripheral handle, the Callback ID
208 and a pointer to the user callback function.
209 For specific callbacks TransceiverCallback use dedicated register callbacks:
210 respectively HAL_SD_RegisterTransceiverCallback().
211
212 Use function HAL_SD_UnRegisterCallback() to reset a callback to the default
213 weak (overridden) function. It allows to reset following callbacks:
214 (+) TxCpltCallback : callback when a transmission transfer is completed.
215 (+) RxCpltCallback : callback when a reception transfer is completed.
216 (+) ErrorCallback : callback when error occurs.
217 (+) AbortCpltCallback : callback when abort is completed.
218 (+) Read_DMADblBuf0CpltCallback : callback when the DMA reception of first buffer is completed.
219 (+) Read_DMADblBuf1CpltCallback : callback when the DMA reception of second buffer is completed.
220 (+) Write_DMADblBuf0CpltCallback : callback when the DMA transmission of first buffer is completed.
221 (+) Write_DMADblBuf1CpltCallback : callback when the DMA transmission of second buffer is completed.
222 (+) MspInitCallback : SD MspInit.
223 (+) MspDeInitCallback : SD MspDeInit.
224 This function) takes as parameters the HAL peripheral handle and the Callback ID.
225 For specific callbacks TransceiverCallback use dedicated unregister callbacks:
226 respectively HAL_SD_UnRegisterTransceiverCallback().
227
228 By default, after the HAL_SD_Init and if the state is HAL_SD_STATE_RESET
229 all callbacks are reset to the corresponding legacy weak (overridden) functions.
230 Exception done for MspInit and MspDeInit callbacks that are respectively
231 reset to the legacy weak (overridden) functions in the HAL_SD_Init
232 and HAL_SD_DeInit only when these callbacks are null (not registered beforehand).
233 If not, MspInit or MspDeInit are not null, the HAL_SD_Init and HAL_SD_DeInit
234 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
235
236 Callbacks can be registered/unregistered in READY state only.
237 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
238 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
239 during the Init/DeInit.
240 In that case first register the MspInit/MspDeInit user callbacks
241 using HAL_SD_RegisterCallback before calling HAL_SD_DeInit
242 or HAL_SD_Init function.
243
244 When The compilation define USE_HAL_SD_REGISTER_CALLBACKS is set to 0 or
245 not defined, the callback registering feature is not available
246 and weak (overridden) callbacks are used.
247
248 @endverbatim
249 ******************************************************************************
250 */
251
252/* Includes ------------------------------------------------------------------*/
253#include "stm32h7xx_hal.h"
254
255/** @addtogroup STM32H7xx_HAL_Driver
256 * @{
257 */
258
259/** @addtogroup SD
260 * @{
261 */
262
263#if defined (SDMMC1) || defined (SDMMC2)
264#ifdef HAL_SD_MODULE_ENABLED
265
266/* Private typedef -----------------------------------------------------------*/
267/* Private define ------------------------------------------------------------*/
268/** @addtogroup SD_Private_Defines
269 * @{
270 */
271/* Frequencies used in the driver for clock divider calculation */
272#define SD_INIT_FREQ 400000U /* Initialization phase : 400 kHz max */
273#define SD_NORMAL_SPEED_FREQ 25000000U /* Normal speed phase : 25 MHz max */
274#define SD_HIGH_SPEED_FREQ 50000000U /* High speed phase : 50 MHz max */
275/* Private macro -------------------------------------------------------------*/
276#if defined (DLYB_SDMMC1) && defined (DLYB_SDMMC2)
277#define SD_GET_DLYB_INSTANCE(SDMMC_INSTANCE) (((SDMMC_INSTANCE) == SDMMC1)? \
278 DLYB_SDMMC1 : DLYB_SDMMC2 )
279#elif defined (DLYB_SDMMC1)
280#define SD_GET_DLYB_INSTANCE(SDMMC_INSTANCE) ( DLYB_SDMMC1 )
281#endif /* (DLYB_SDMMC1) && defined (DLYB_SDMMC2) */
282
283/**
284 * @}
285 */
286
287/* Private variables ---------------------------------------------------------*/
288/* Private function prototypes -----------------------------------------------*/
289/* Private functions ---------------------------------------------------------*/
290/** @defgroup SD_Private_Functions SD Private Functions
291 * @{
292 */
293static uint32_t SD_InitCard(SD_HandleTypeDef *hsd);
294static uint32_t SD_PowerON(SD_HandleTypeDef *hsd);
295static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
296static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
297static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd);
298static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
299static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
300static void SD_PowerOFF(SD_HandleTypeDef *hsd);
301static void SD_Write_IT(SD_HandleTypeDef *hsd);
302static void SD_Read_IT(SD_HandleTypeDef *hsd);
303static uint32_t SD_SwitchSpeed(SD_HandleTypeDef *hsd, uint32_t SwitchSpeedMode);
304#if (USE_SD_TRANSCEIVER != 0U)
305static uint32_t SD_UltraHighSpeed(SD_HandleTypeDef *hsd, uint32_t UltraHighSpeedMode);
306static uint32_t SD_DDR_Mode(SD_HandleTypeDef *hsd);
307#endif /* USE_SD_TRANSCEIVER */
308/**
309 * @}
310 */
311
312/* Exported functions --------------------------------------------------------*/
313/** @addtogroup SD_Exported_Functions
314 * @{
315 */
316
317/** @addtogroup SD_Exported_Functions_Group1
318 * @brief Initialization and de-initialization functions
319 *
320@verbatim
321 ==============================================================================
322 ##### Initialization and de-initialization functions #####
323 ==============================================================================
324 [..]
325 This section provides functions allowing to initialize/de-initialize the SD
326 card device to be ready for use.
327
328@endverbatim
329 * @{
330 */
331
332/**
333 * @brief Initializes the SD according to the specified parameters in the
334 SD_HandleTypeDef and create the associated handle.
335 * @param hsd: Pointer to the SD handle
336 * @retval HAL status
337 */
338HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
339{
340 HAL_SD_CardStatusTypeDef CardStatus;
341 uint32_t speedgrade;
342 uint32_t unitsize;
343 uint32_t tickstart;
344
345 /* Check the SD handle allocation */
346 if (hsd == NULL)
347 {
348 return HAL_ERROR;
349 }
350
351 /* Check the parameters */
352 assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
353 assert_param(IS_SDMMC_CLOCK_EDGE(hsd->Init.ClockEdge));
354 assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
355 assert_param(IS_SDMMC_BUS_WIDE(hsd->Init.BusWide));
356 assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
357 assert_param(IS_SDMMC_CLKDIV(hsd->Init.ClockDiv));
358
359 if (hsd->State == HAL_SD_STATE_RESET)
360 {
361 /* Allocate lock resource and initialize it */
362 hsd->Lock = HAL_UNLOCKED;
363
364#if (USE_SD_TRANSCEIVER != 0U)
365 /* Force SDMMC_TRANSCEIVER_PRESENT for Legacy usage */
366 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_UNKNOWN)
367 {
368 hsd->Init.TranceiverPresent = SDMMC_TRANSCEIVER_PRESENT;
369 }
370#endif /*USE_SD_TRANSCEIVER */
371#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
372 /* Reset Callback pointers in HAL_SD_STATE_RESET only */
373 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
374 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
375 hsd->ErrorCallback = HAL_SD_ErrorCallback;
376 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
377 hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuf0CpltCallback;
378 hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuf1CpltCallback;
379 hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuf0CpltCallback;
380 hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuf1CpltCallback;
381#if (USE_SD_TRANSCEIVER != 0U)
382 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
383 {
384 hsd->DriveTransceiver_1_8V_Callback = HAL_SD_DriveTransceiver_1_8V_Callback;
385 }
386#endif /* USE_SD_TRANSCEIVER */
387
388 if (hsd->MspInitCallback == NULL)
389 {
390 hsd->MspInitCallback = HAL_SD_MspInit;
391 }
392
393 /* Init the low level hardware */
394 hsd->MspInitCallback(hsd);
395#else
396 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
397 HAL_SD_MspInit(hsd);
398#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
399 }
400
401 hsd->State = HAL_SD_STATE_PROGRAMMING;
402
403 /* Initialize the Card parameters */
404 if (HAL_SD_InitCard(hsd) != HAL_OK)
405 {
406 return HAL_ERROR;
407 }
408
409 if (HAL_SD_GetCardStatus(hsd, &CardStatus) != HAL_OK)
410 {
411 return HAL_ERROR;
412 }
413 /* Get Initial Card Speed from Card Status*/
414 speedgrade = CardStatus.UhsSpeedGrade;
415 unitsize = CardStatus.UhsAllocationUnitSize;
416 if ((hsd->SdCard.CardType == CARD_SDHC_SDXC) && ((speedgrade != 0U) || (unitsize != 0U)))
417 {
418 hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
419 }
420 else
421 {
422 if (hsd->SdCard.CardType == CARD_SDHC_SDXC)
423 {
424 hsd->SdCard.CardSpeed = CARD_HIGH_SPEED;
425 }
426 else
427 {
428 hsd->SdCard.CardSpeed = CARD_NORMAL_SPEED;
429 }
430
431 }
432 /* Configure the bus wide */
433 if (HAL_SD_ConfigWideBusOperation(hsd, hsd->Init.BusWide) != HAL_OK)
434 {
435 return HAL_ERROR;
436 }
437
438 /* Verify that SD card is ready to use after Initialization */
439 tickstart = HAL_GetTick();
440 while ((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER))
441 {
442 if ((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
443 {
444 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
445 hsd->State = HAL_SD_STATE_READY;
446 return HAL_TIMEOUT;
447 }
448 }
449
450 /* Initialize the error code */
451 hsd->ErrorCode = HAL_SD_ERROR_NONE;
452
453 /* Initialize the SD operation */
454 hsd->Context = SD_CONTEXT_NONE;
455
456 /* Initialize the SD state */
457 hsd->State = HAL_SD_STATE_READY;
458
459 return HAL_OK;
460}
461
462/**
463 * @brief Initializes the SD Card.
464 * @param hsd: Pointer to SD handle
465 * @note This function initializes the SD card. It could be used when a card
466 re-initialization is needed.
467 * @retval HAL status
468 */
469HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
470{
471 uint32_t errorstate;
472 SD_InitTypeDef Init = {0U};
473 uint32_t sdmmc_clk;
474
475 /* Default SDMMC peripheral configuration for SD card initialization */
476 Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
477 Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
478 Init.BusWide = SDMMC_BUS_WIDE_1B;
479 Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
480
481 /* Init Clock should be less or equal to 400Khz*/
483 if (sdmmc_clk == 0U)
484 {
485 hsd->State = HAL_SD_STATE_READY;
486 hsd->ErrorCode = SDMMC_ERROR_INVALID_PARAMETER;
487 return HAL_ERROR;
488 }
489 if (sdmmc_clk <= SD_INIT_FREQ)
490 {
491 Init.ClockDiv = 0U;
492 }
493 else
494 {
495 Init.ClockDiv = (sdmmc_clk / (2U * SD_INIT_FREQ)) + 1U;
496 }
497
498#if (USE_SD_TRANSCEIVER != 0U)
499 Init.TranceiverPresent = hsd->Init.TranceiverPresent;
500
501 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
502 {
503 /* Set Transceiver polarity */
504 hsd->Instance->POWER |= SDMMC_POWER_DIRPOL;
505 }
506#elif defined (USE_SD_DIRPOL)
507 /* Set Transceiver polarity */
508 hsd->Instance->POWER |= SDMMC_POWER_DIRPOL;
509#endif /* USE_SD_TRANSCEIVER */
510
511 /* Initialize SDMMC peripheral interface with default configuration */
512 (void)SDMMC_Init(hsd->Instance, Init);
513
514 /* Set Power State to ON */
515 (void)SDMMC_PowerState_ON(hsd->Instance);
516
517 /* wait 74 Cycles: required power up waiting time before starting
518 the SD initialization sequence */
519 if (Init.ClockDiv != 0U)
520 {
521 sdmmc_clk = sdmmc_clk / (2U * Init.ClockDiv);
522 }
523
524 if (sdmmc_clk != 0U)
525 {
526 HAL_Delay(1U + (74U * 1000U / (sdmmc_clk)));
527 }
528
529 /* Identify card operating voltage */
530 errorstate = SD_PowerON(hsd);
531 if (errorstate != HAL_SD_ERROR_NONE)
532 {
533 hsd->State = HAL_SD_STATE_READY;
534 hsd->ErrorCode |= errorstate;
535 return HAL_ERROR;
536 }
537
538 /* Card initialization */
539 errorstate = SD_InitCard(hsd);
540 if (errorstate != HAL_SD_ERROR_NONE)
541 {
542 hsd->State = HAL_SD_STATE_READY;
543 hsd->ErrorCode |= errorstate;
544 return HAL_ERROR;
545 }
546
547 /* Set Block Size for Card */
548 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
549 if (errorstate != HAL_SD_ERROR_NONE)
550 {
551 /* Clear all the static flags */
552 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
553 hsd->ErrorCode |= errorstate;
554 hsd->State = HAL_SD_STATE_READY;
555 return HAL_ERROR;
556 }
557
558 return HAL_OK;
559}
560
561/**
562 * @brief De-Initializes the SD card.
563 * @param hsd: Pointer to SD handle
564 * @retval HAL status
565 */
566HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
567{
568 /* Check the SD handle allocation */
569 if (hsd == NULL)
570 {
571 return HAL_ERROR;
572 }
573
574 /* Check the parameters */
575 assert_param(IS_SDMMC_ALL_INSTANCE(hsd->Instance));
576
577 hsd->State = HAL_SD_STATE_BUSY;
578
579#if (USE_SD_TRANSCEIVER != 0U)
580 /* Deactivate the 1.8V Mode */
581 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
582 {
583#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
584 if (hsd->DriveTransceiver_1_8V_Callback == NULL)
585 {
586 hsd->DriveTransceiver_1_8V_Callback = HAL_SD_DriveTransceiver_1_8V_Callback;
587 }
588 hsd->DriveTransceiver_1_8V_Callback(RESET);
589#else
590 HAL_SD_DriveTransceiver_1_8V_Callback(RESET);
591#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
592 }
593#endif /* USE_SD_TRANSCEIVER */
594
595 /* Set SD power state to off */
596 SD_PowerOFF(hsd);
597
598#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
599 if (hsd->MspDeInitCallback == NULL)
600 {
601 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
602 }
603
604 /* DeInit the low level hardware */
605 hsd->MspDeInitCallback(hsd);
606#else
607 /* De-Initialize the MSP layer */
608 HAL_SD_MspDeInit(hsd);
609#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
610
611 hsd->ErrorCode = HAL_SD_ERROR_NONE;
612 hsd->State = HAL_SD_STATE_RESET;
613
614 return HAL_OK;
615}
616
617/**
618 * @brief Initializes the SD MSP.
619 * @param hsd: Pointer to SD handle
620 * @retval None
621 */
622__weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
623{
624 /* Prevent unused argument(s) compilation warning */
625 UNUSED(hsd);
626
627 /* NOTE : This function should not be modified, when the callback is needed,
628 the HAL_SD_MspInit could be implemented in the user file
629 */
630}
631
632/**
633 * @brief De-Initialize SD MSP.
634 * @param hsd: Pointer to SD handle
635 * @retval None
636 */
637__weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
638{
639 /* Prevent unused argument(s) compilation warning */
640 UNUSED(hsd);
641
642 /* NOTE : This function should not be modified, when the callback is needed,
643 the HAL_SD_MspDeInit could be implemented in the user file
644 */
645}
646
647/**
648 * @}
649 */
650
651/** @addtogroup SD_Exported_Functions_Group2
652 * @brief Data transfer functions
653 *
654@verbatim
655 ==============================================================================
656 ##### IO operation functions #####
657 ==============================================================================
658 [..]
659 This subsection provides a set of functions allowing to manage the data
660 transfer from/to SD card.
661
662@endverbatim
663 * @{
664 */
665
666/**
667 * @brief Reads block(s) from a specified address in a card. The Data transfer
668 * is managed by polling mode.
669 * @note This API should be followed by a check on the card state through
670 * HAL_SD_GetCardState().
671 * @param hsd: Pointer to SD handle
672 * @param pData: pointer to the buffer that will contain the received data
673 * @param BlockAdd: Block Address from where data is to be read
674 * @param NumberOfBlocks: Number of SD blocks to read
675 * @param Timeout: Specify timeout value
676 * @retval HAL status
677 */
678HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks,
679 uint32_t Timeout)
680{
681 SDMMC_DataInitTypeDef config;
682 uint32_t errorstate;
683 uint32_t tickstart = HAL_GetTick();
684 uint32_t count;
685 uint32_t data;
686 uint32_t dataremaining;
687 uint32_t add = BlockAdd;
688 uint8_t *tempbuff = pData;
689
690 if (NULL == pData)
691 {
692 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
693 return HAL_ERROR;
694 }
695
696 if (hsd->State == HAL_SD_STATE_READY)
697 {
698 hsd->ErrorCode = HAL_SD_ERROR_NONE;
699
700 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
701 {
702 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
703 return HAL_ERROR;
704 }
705
706 hsd->State = HAL_SD_STATE_BUSY;
707
708 /* Initialize data control register */
709 hsd->Instance->DCTRL = 0U;
710
711 if (hsd->SdCard.CardType != CARD_SDHC_SDXC)
712 {
713 add *= BLOCKSIZE;
714 }
715
716 /* Configure the SD DPSM (Data Path State Machine) */
717 config.DataTimeOut = SDMMC_DATATIMEOUT;
718 config.DataLength = NumberOfBlocks * BLOCKSIZE;
719 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
720 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
721 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
722 config.DPSM = SDMMC_DPSM_DISABLE;
723 (void)SDMMC_ConfigData(hsd->Instance, &config);
724 __SDMMC_CMDTRANS_ENABLE(hsd->Instance);
725
726 /* Read block(s) in polling mode */
727 if (NumberOfBlocks > 1U)
728 {
729 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
730
731 /* Read Multi Block command */
732 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
733 }
734 else
735 {
736 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
737
738 /* Read Single Block command */
739 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
740 }
741 if (errorstate != HAL_SD_ERROR_NONE)
742 {
743 /* Clear all the static flags */
744 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
745 hsd->ErrorCode |= errorstate;
746 hsd->State = HAL_SD_STATE_READY;
747 hsd->Context = SD_CONTEXT_NONE;
748 return HAL_ERROR;
749 }
750
751 /* Poll on SDMMC flags */
752 dataremaining = config.DataLength;
753 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
754 {
755 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) && (dataremaining >= SDMMC_FIFO_SIZE))
756 {
757 /* Read data from SDMMC Rx FIFO */
758 for (count = 0U; count < (SDMMC_FIFO_SIZE / 4U); count++)
759 {
760 data = SDMMC_ReadFIFO(hsd->Instance);
761 *tempbuff = (uint8_t)(data & 0xFFU);
762 tempbuff++;
763 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
764 tempbuff++;
765 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
766 tempbuff++;
767 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
768 tempbuff++;
769 }
770 dataremaining -= SDMMC_FIFO_SIZE;
771 }
772
773 if (((HAL_GetTick() - tickstart) >= Timeout) || (Timeout == 0U))
774 {
775 /* Clear all the static flags */
776 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
777 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
778 hsd->State = HAL_SD_STATE_READY;
779 hsd->Context = SD_CONTEXT_NONE;
780 return HAL_TIMEOUT;
781 }
782 }
783 __SDMMC_CMDTRANS_DISABLE(hsd->Instance);
784
785 /* Send stop transmission command in case of multiblock read */
786 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
787 {
788 if (hsd->SdCard.CardType != CARD_SECURED)
789 {
790 /* Send stop transmission command */
791 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
792 if (errorstate != HAL_SD_ERROR_NONE)
793 {
794 /* Clear all the static flags */
795 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
796 hsd->ErrorCode |= errorstate;
797 hsd->State = HAL_SD_STATE_READY;
798 hsd->Context = SD_CONTEXT_NONE;
799 return HAL_ERROR;
800 }
801 }
802 }
803
804 /* Get error state */
805 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
806 {
807 /* Clear all the static flags */
808 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
809 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
810 hsd->State = HAL_SD_STATE_READY;
811 hsd->Context = SD_CONTEXT_NONE;
812 return HAL_ERROR;
813 }
814 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
815 {
816 /* Clear all the static flags */
817 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
818 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
819 hsd->State = HAL_SD_STATE_READY;
820 hsd->Context = SD_CONTEXT_NONE;
821 return HAL_ERROR;
822 }
823 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
824 {
825 /* Clear all the static flags */
826 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
827 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
828 hsd->State = HAL_SD_STATE_READY;
829 hsd->Context = SD_CONTEXT_NONE;
830 return HAL_ERROR;
831 }
832 else
833 {
834 /* Nothing to do */
835 }
836
837 /* Clear all the static flags */
838 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
839
840 hsd->State = HAL_SD_STATE_READY;
841
842 return HAL_OK;
843 }
844 else
845 {
846 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
847 return HAL_ERROR;
848 }
849}
850
851/**
852 * @brief Allows to write block(s) to a specified address in a card. The Data
853 * transfer is managed by polling mode.
854 * @note This API should be followed by a check on the card state through
855 * HAL_SD_GetCardState().
856 * @param hsd: Pointer to SD handle
857 * @param pData: pointer to the buffer that will contain the data to transmit
858 * @param BlockAdd: Block Address where data will be written
859 * @param NumberOfBlocks: Number of SD blocks to write
860 * @param Timeout: Specify timeout value
861 * @retval HAL status
862 */
863HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, const uint8_t *pData, uint32_t BlockAdd,
864 uint32_t NumberOfBlocks, uint32_t Timeout)
865{
866 SDMMC_DataInitTypeDef config;
867 uint32_t errorstate;
868 uint32_t tickstart = HAL_GetTick();
869 uint32_t count;
870 uint32_t data;
871 uint32_t dataremaining;
872 uint32_t add = BlockAdd;
873 const uint8_t *tempbuff = pData;
874
875 if (NULL == pData)
876 {
877 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
878 return HAL_ERROR;
879 }
880
881 if (hsd->State == HAL_SD_STATE_READY)
882 {
883 hsd->ErrorCode = HAL_SD_ERROR_NONE;
884
885 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
886 {
887 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
888 return HAL_ERROR;
889 }
890
891 hsd->State = HAL_SD_STATE_BUSY;
892
893 /* Initialize data control register */
894 hsd->Instance->DCTRL = 0U;
895
896 if (hsd->SdCard.CardType != CARD_SDHC_SDXC)
897 {
898 add *= BLOCKSIZE;
899 }
900
901 /* Configure the SD DPSM (Data Path State Machine) */
902 config.DataTimeOut = SDMMC_DATATIMEOUT;
903 config.DataLength = NumberOfBlocks * BLOCKSIZE;
904 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
905 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
906 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
907 config.DPSM = SDMMC_DPSM_DISABLE;
908 (void)SDMMC_ConfigData(hsd->Instance, &config);
909 __SDMMC_CMDTRANS_ENABLE(hsd->Instance);
910
911 /* Write Blocks in Polling mode */
912 if (NumberOfBlocks > 1U)
913 {
914 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
915
916 /* Write Multi Block command */
917 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
918 }
919 else
920 {
921 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
922
923 /* Write Single Block command */
924 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
925 }
926 if (errorstate != HAL_SD_ERROR_NONE)
927 {
928 /* Clear all the static flags */
929 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
930 hsd->ErrorCode |= errorstate;
931 hsd->State = HAL_SD_STATE_READY;
932 hsd->Context = SD_CONTEXT_NONE;
933 return HAL_ERROR;
934 }
935
936 /* Write block(s) in polling mode */
937 dataremaining = config.DataLength;
938 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT |
939 SDMMC_FLAG_DATAEND))
940 {
941 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) && (dataremaining >= SDMMC_FIFO_SIZE))
942 {
943 /* Write data to SDMMC Tx FIFO */
944 for (count = 0U; count < (SDMMC_FIFO_SIZE / 4U); count++)
945 {
946 data = (uint32_t)(*tempbuff);
947 tempbuff++;
948 data |= ((uint32_t)(*tempbuff) << 8U);
949 tempbuff++;
950 data |= ((uint32_t)(*tempbuff) << 16U);
951 tempbuff++;
952 data |= ((uint32_t)(*tempbuff) << 24U);
953 tempbuff++;
954 (void)SDMMC_WriteFIFO(hsd->Instance, &data);
955 }
956 dataremaining -= SDMMC_FIFO_SIZE;
957 }
958
959 if (((HAL_GetTick() - tickstart) >= Timeout) || (Timeout == 0U))
960 {
961 /* Clear all the static flags */
962 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
963 hsd->ErrorCode |= errorstate;
964 hsd->State = HAL_SD_STATE_READY;
965 hsd->Context = SD_CONTEXT_NONE;
966 return HAL_TIMEOUT;
967 }
968 }
969 __SDMMC_CMDTRANS_DISABLE(hsd->Instance);
970
971 /* Send stop transmission command in case of multiblock write */
972 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) && (NumberOfBlocks > 1U))
973 {
974 if (hsd->SdCard.CardType != CARD_SECURED)
975 {
976 /* Send stop transmission command */
977 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
978 if (errorstate != HAL_SD_ERROR_NONE)
979 {
980 /* Clear all the static flags */
981 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
982 hsd->ErrorCode |= errorstate;
983 hsd->State = HAL_SD_STATE_READY;
984 hsd->Context = SD_CONTEXT_NONE;
985 return HAL_ERROR;
986 }
987 }
988 }
989
990 /* Get error state */
991 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
992 {
993 /* Clear all the static flags */
994 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
995 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
996 hsd->State = HAL_SD_STATE_READY;
997 hsd->Context = SD_CONTEXT_NONE;
998 return HAL_ERROR;
999 }
1000 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
1001 {
1002 /* Clear all the static flags */
1003 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1004 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1005 hsd->State = HAL_SD_STATE_READY;
1006 hsd->Context = SD_CONTEXT_NONE;
1007 return HAL_ERROR;
1008 }
1009 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR))
1010 {
1011 /* Clear all the static flags */
1012 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1013 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1014 hsd->State = HAL_SD_STATE_READY;
1015 hsd->Context = SD_CONTEXT_NONE;
1016 return HAL_ERROR;
1017 }
1018 else
1019 {
1020 /* Nothing to do */
1021 }
1022
1023 /* Clear all the static flags */
1024 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1025
1026 hsd->State = HAL_SD_STATE_READY;
1027
1028 return HAL_OK;
1029 }
1030 else
1031 {
1032 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
1033 return HAL_ERROR;
1034 }
1035}
1036
1037/**
1038 * @brief Reads block(s) from a specified address in a card. The Data transfer
1039 * is managed in interrupt mode.
1040 * @note This API should be followed by a check on the card state through
1041 * HAL_SD_GetCardState().
1042 * @note You could also check the IT transfer process through the SD Rx
1043 * interrupt event.
1044 * @param hsd: Pointer to SD handle
1045 * @param pData: Pointer to the buffer that will contain the received data
1046 * @param BlockAdd: Block Address from where data is to be read
1047 * @param NumberOfBlocks: Number of blocks to read.
1048 * @retval HAL status
1049 */
1050HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd,
1051 uint32_t NumberOfBlocks)
1052{
1053 SDMMC_DataInitTypeDef config;
1054 uint32_t errorstate;
1055 uint32_t add = BlockAdd;
1056
1057 if (NULL == pData)
1058 {
1059 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1060 return HAL_ERROR;
1061 }
1062
1063 if (hsd->State == HAL_SD_STATE_READY)
1064 {
1065 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1066
1067 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1068 {
1069 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1070 return HAL_ERROR;
1071 }
1072
1073 hsd->State = HAL_SD_STATE_BUSY;
1074
1075 /* Initialize data control register */
1076 hsd->Instance->DCTRL = 0U;
1077
1078 hsd->pRxBuffPtr = pData;
1079 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1080
1081 if (hsd->SdCard.CardType != CARD_SDHC_SDXC)
1082 {
1083 add *= BLOCKSIZE;
1084 }
1085
1086 /* Configure the SD DPSM (Data Path State Machine) */
1087 config.DataTimeOut = SDMMC_DATATIMEOUT;
1088 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1089 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1090 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1091 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1092 config.DPSM = SDMMC_DPSM_DISABLE;
1093 (void)SDMMC_ConfigData(hsd->Instance, &config);
1094 __SDMMC_CMDTRANS_ENABLE(hsd->Instance);
1095
1096 /* Read Blocks in IT mode */
1097 if (NumberOfBlocks > 1U)
1098 {
1099 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1100
1101 /* Read Multi Block command */
1102 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1103 }
1104 else
1105 {
1106 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
1107
1108 /* Read Single Block command */
1109 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1110 }
1111 if (errorstate != HAL_SD_ERROR_NONE)
1112 {
1113 /* Clear all the static flags */
1114 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1115 hsd->ErrorCode |= errorstate;
1116 hsd->State = HAL_SD_STATE_READY;
1117 hsd->Context = SD_CONTEXT_NONE;
1118 return HAL_ERROR;
1119 }
1120
1121 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND |
1122 SDMMC_FLAG_RXFIFOHF));
1123
1124 return HAL_OK;
1125 }
1126 else
1127 {
1128 return HAL_BUSY;
1129 }
1130}
1131
1132/**
1133 * @brief Writes block(s) to a specified address in a card. The Data transfer
1134 * is managed in interrupt mode.
1135 * @note This API should be followed by a check on the card state through
1136 * HAL_SD_GetCardState().
1137 * @note You could also check the IT transfer process through the SD Tx
1138 * interrupt event.
1139 * @param hsd: Pointer to SD handle
1140 * @param pData: Pointer to the buffer that will contain the data to transmit
1141 * @param BlockAdd: Block Address where data will be written
1142 * @param NumberOfBlocks: Number of blocks to write
1143 * @retval HAL status
1144 */
1145HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, const uint8_t *pData, uint32_t BlockAdd,
1146 uint32_t NumberOfBlocks)
1147{
1148 SDMMC_DataInitTypeDef config;
1149 uint32_t errorstate;
1150 uint32_t add = BlockAdd;
1151
1152 if (NULL == pData)
1153 {
1154 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1155 return HAL_ERROR;
1156 }
1157
1158 if (hsd->State == HAL_SD_STATE_READY)
1159 {
1160 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1161
1162 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1163 {
1164 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1165 return HAL_ERROR;
1166 }
1167
1168 hsd->State = HAL_SD_STATE_BUSY;
1169
1170 /* Initialize data control register */
1171 hsd->Instance->DCTRL = 0U;
1172
1173 hsd->pTxBuffPtr = pData;
1174 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1175
1176 if (hsd->SdCard.CardType != CARD_SDHC_SDXC)
1177 {
1178 add *= BLOCKSIZE;
1179 }
1180
1181 /* Configure the SD DPSM (Data Path State Machine) */
1182 config.DataTimeOut = SDMMC_DATATIMEOUT;
1183 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1184 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1185 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1186 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1187 config.DPSM = SDMMC_DPSM_DISABLE;
1188 (void)SDMMC_ConfigData(hsd->Instance, &config);
1189
1190 __SDMMC_CMDTRANS_ENABLE(hsd->Instance);
1191
1192 /* Write Blocks in Polling mode */
1193 if (NumberOfBlocks > 1U)
1194 {
1195 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1196
1197 /* Write Multi Block command */
1198 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1199 }
1200 else
1201 {
1202 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1203
1204 /* Write Single Block command */
1205 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1206 }
1207 if (errorstate != HAL_SD_ERROR_NONE)
1208 {
1209 /* Clear all the static flags */
1210 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1211 hsd->ErrorCode |= errorstate;
1212 hsd->State = HAL_SD_STATE_READY;
1213 hsd->Context = SD_CONTEXT_NONE;
1214 return HAL_ERROR;
1215 }
1216
1217 /* Enable transfer interrupts */
1218 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND |
1219 SDMMC_FLAG_TXFIFOHE));
1220
1221 return HAL_OK;
1222 }
1223 else
1224 {
1225 return HAL_BUSY;
1226 }
1227}
1228
1229/**
1230 * @brief Reads block(s) from a specified address in a card. The Data transfer
1231 * is managed by DMA mode.
1232 * @note This API should be followed by a check on the card state through
1233 * HAL_SD_GetCardState().
1234 * @note You could also check the DMA transfer process through the SD Rx
1235 * interrupt event.
1236 * @param hsd: Pointer SD handle
1237 * @param pData: Pointer to the buffer that will contain the received data
1238 * @param BlockAdd: Block Address from where data is to be read
1239 * @param NumberOfBlocks: Number of blocks to read.
1240 * @retval HAL status
1241 */
1242HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd,
1243 uint32_t NumberOfBlocks)
1244{
1245 SDMMC_DataInitTypeDef config;
1246 uint32_t errorstate;
1247 uint32_t add = BlockAdd;
1248
1249 if (NULL == pData)
1250 {
1251 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1252 return HAL_ERROR;
1253 }
1254
1255 if (hsd->State == HAL_SD_STATE_READY)
1256 {
1257 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1258
1259 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1260 {
1261 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1262 return HAL_ERROR;
1263 }
1264
1265 hsd->State = HAL_SD_STATE_BUSY;
1266
1267 /* Initialize data control register */
1268 hsd->Instance->DCTRL = 0U;
1269
1270 hsd->pRxBuffPtr = pData;
1271 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1272
1273 if (hsd->SdCard.CardType != CARD_SDHC_SDXC)
1274 {
1275 add *= BLOCKSIZE;
1276 }
1277
1278 /* Configure the SD DPSM (Data Path State Machine) */
1279 config.DataTimeOut = SDMMC_DATATIMEOUT;
1280 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1281 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1282 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
1283 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1284 config.DPSM = SDMMC_DPSM_DISABLE;
1285 (void)SDMMC_ConfigData(hsd->Instance, &config);
1286
1287 __SDMMC_CMDTRANS_ENABLE(hsd->Instance);
1288 hsd->Instance->IDMABASE0 = (uint32_t) pData ;
1289 hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1290
1291 /* Read Blocks in DMA mode */
1292 if (NumberOfBlocks > 1U)
1293 {
1294 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1295
1296 /* Read Multi Block command */
1297 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1298 }
1299 else
1300 {
1301 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1302
1303 /* Read Single Block command */
1304 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1305 }
1306 if (errorstate != HAL_SD_ERROR_NONE)
1307 {
1308 /* Clear all the static flags */
1309 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1310 hsd->ErrorCode |= errorstate;
1311 hsd->State = HAL_SD_STATE_READY;
1312 hsd->Context = SD_CONTEXT_NONE;
1313 return HAL_ERROR;
1314 }
1315
1316 /* Enable transfer interrupts */
1317 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_RXOVERR | SDMMC_IT_DATAEND));
1318
1319 return HAL_OK;
1320 }
1321 else
1322 {
1323 return HAL_BUSY;
1324 }
1325}
1326
1327/**
1328 * @brief Writes block(s) to a specified address in a card. The Data transfer
1329 * is managed by DMA mode.
1330 * @note This API should be followed by a check on the card state through
1331 * HAL_SD_GetCardState().
1332 * @note You could also check the DMA transfer process through the SD Tx
1333 * interrupt event.
1334 * @param hsd: Pointer to SD handle
1335 * @param pData: Pointer to the buffer that will contain the data to transmit
1336 * @param BlockAdd: Block Address where data will be written
1337 * @param NumberOfBlocks: Number of blocks to write
1338 * @retval HAL status
1339 */
1340HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, const uint8_t *pData, uint32_t BlockAdd,
1341 uint32_t NumberOfBlocks)
1342{
1343 SDMMC_DataInitTypeDef config;
1344 uint32_t errorstate;
1345 uint32_t add = BlockAdd;
1346
1347 if (NULL == pData)
1348 {
1349 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1350 return HAL_ERROR;
1351 }
1352
1353 if (hsd->State == HAL_SD_STATE_READY)
1354 {
1355 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1356
1357 if ((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1358 {
1359 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1360 return HAL_ERROR;
1361 }
1362
1363 hsd->State = HAL_SD_STATE_BUSY;
1364
1365 /* Initialize data control register */
1366 hsd->Instance->DCTRL = 0U;
1367
1368 hsd->pTxBuffPtr = pData;
1369 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1370
1371 if (hsd->SdCard.CardType != CARD_SDHC_SDXC)
1372 {
1373 add *= BLOCKSIZE;
1374 }
1375
1376 /* Configure the SD DPSM (Data Path State Machine) */
1377 config.DataTimeOut = SDMMC_DATATIMEOUT;
1378 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1379 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_512B;
1380 config.TransferDir = SDMMC_TRANSFER_DIR_TO_CARD;
1381 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
1382 config.DPSM = SDMMC_DPSM_DISABLE;
1383 (void)SDMMC_ConfigData(hsd->Instance, &config);
1384
1385 __SDMMC_CMDTRANS_ENABLE(hsd->Instance);
1386
1387 hsd->Instance->IDMABASE0 = (uint32_t) pData ;
1388 hsd->Instance->IDMACTRL = SDMMC_ENABLE_IDMA_SINGLE_BUFF;
1389
1390 /* Write Blocks in Polling mode */
1391 if (NumberOfBlocks > 1U)
1392 {
1393 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1394
1395 /* Write Multi Block command */
1396 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1397 }
1398 else
1399 {
1400 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1401
1402 /* Write Single Block command */
1403 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1404 }
1405 if (errorstate != HAL_SD_ERROR_NONE)
1406 {
1407 /* Clear all the static flags */
1408 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1409 hsd->ErrorCode |= errorstate;
1410 hsd->State = HAL_SD_STATE_READY;
1411 hsd->Context = SD_CONTEXT_NONE;
1412 return HAL_ERROR;
1413 }
1414
1415 /* Enable transfer interrupts */
1416 __HAL_SD_ENABLE_IT(hsd, (SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | SDMMC_IT_TXUNDERR | SDMMC_IT_DATAEND));
1417
1418 return HAL_OK;
1419 }
1420 else
1421 {
1422 return HAL_BUSY;
1423 }
1424}
1425
1426/**
1427 * @brief Erases the specified memory area of the given SD card.
1428 * @note This API should be followed by a check on the card state through
1429 * HAL_SD_GetCardState().
1430 * @param hsd: Pointer to SD handle
1431 * @param BlockStartAdd: Start Block address
1432 * @param BlockEndAdd: End Block address
1433 * @retval HAL status
1434 */
1435HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1436{
1437 uint32_t errorstate;
1438 uint32_t start_add = BlockStartAdd;
1439 uint32_t end_add = BlockEndAdd;
1440
1441 if (hsd->State == HAL_SD_STATE_READY)
1442 {
1443 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1444
1445 if (end_add < start_add)
1446 {
1447 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1448 return HAL_ERROR;
1449 }
1450
1451 if (end_add > (hsd->SdCard.LogBlockNbr))
1452 {
1453 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1454 return HAL_ERROR;
1455 }
1456
1457 hsd->State = HAL_SD_STATE_BUSY;
1458
1459 /* Check if the card command class supports erase command */
1460 if (((hsd->SdCard.Class) & SDMMC_CCCC_ERASE) == 0U)
1461 {
1462 /* Clear all the static flags */
1463 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1464 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1465 hsd->State = HAL_SD_STATE_READY;
1466 return HAL_ERROR;
1467 }
1468
1469 if ((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1470 {
1471 /* Clear all the static flags */
1472 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1473 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1474 hsd->State = HAL_SD_STATE_READY;
1475 return HAL_ERROR;
1476 }
1477
1478 /* Get start and end block for high capacity cards */
1479 if (hsd->SdCard.CardType != CARD_SDHC_SDXC)
1480 {
1481 start_add *= BLOCKSIZE;
1482 end_add *= BLOCKSIZE;
1483 }
1484
1485 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1486 if (hsd->SdCard.CardType != CARD_SECURED)
1487 {
1488 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1489 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
1490 if (errorstate != HAL_SD_ERROR_NONE)
1491 {
1492 /* Clear all the static flags */
1493 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1494 hsd->ErrorCode |= errorstate;
1495 hsd->State = HAL_SD_STATE_READY;
1496 return HAL_ERROR;
1497 }
1498
1499 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1500 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
1501 if (errorstate != HAL_SD_ERROR_NONE)
1502 {
1503 /* Clear all the static flags */
1504 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1505 hsd->ErrorCode |= errorstate;
1506 hsd->State = HAL_SD_STATE_READY;
1507 return HAL_ERROR;
1508 }
1509 }
1510
1511 /* Send CMD38 ERASE */
1512 errorstate = SDMMC_CmdErase(hsd->Instance, 0UL);
1513 if (errorstate != HAL_SD_ERROR_NONE)
1514 {
1515 /* Clear all the static flags */
1516 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
1517 hsd->ErrorCode |= errorstate;
1518 hsd->State = HAL_SD_STATE_READY;
1519 return HAL_ERROR;
1520 }
1521
1522 hsd->State = HAL_SD_STATE_READY;
1523
1524 return HAL_OK;
1525 }
1526 else
1527 {
1528 return HAL_BUSY;
1529 }
1530}
1531
1532/**
1533 * @brief This function handles SD card interrupt request.
1534 * @param hsd: Pointer to SD handle
1535 * @retval None
1536 */
1537void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1538{
1539 uint32_t errorstate;
1540 uint32_t context = hsd->Context;
1541
1542 /* Check for SDMMC interrupt flags */
1543 if ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1544 {
1545 SD_Read_IT(hsd);
1546 }
1547
1548 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND) != RESET)
1549 {
1550 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DATAEND);
1551
1552 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | \
1553 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR | SDMMC_IT_TXFIFOHE | \
1554 SDMMC_IT_RXFIFOHF);
1555
1556 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
1557 __SDMMC_CMDTRANS_DISABLE(hsd->Instance);
1558
1559 if ((context & SD_CONTEXT_IT) != 0U)
1560 {
1561 if (((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1562 {
1563 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1564 if (errorstate != HAL_SD_ERROR_NONE)
1565 {
1566 hsd->ErrorCode |= errorstate;
1567#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1568 hsd->ErrorCallback(hsd);
1569#else
1570 HAL_SD_ErrorCallback(hsd);
1571#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1572 }
1573 }
1574
1575 /* Clear all the static flags */
1576 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1577
1578 hsd->State = HAL_SD_STATE_READY;
1579 hsd->Context = SD_CONTEXT_NONE;
1580 if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1581 {
1582#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1583 hsd->RxCpltCallback(hsd);
1584#else
1585 HAL_SD_RxCpltCallback(hsd);
1586#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1587 }
1588 else
1589 {
1590#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1591 hsd->TxCpltCallback(hsd);
1592#else
1593 HAL_SD_TxCpltCallback(hsd);
1594#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1595 }
1596 }
1597 else if ((context & SD_CONTEXT_DMA) != 0U)
1598 {
1599 hsd->Instance->DLEN = 0;
1600 hsd->Instance->DCTRL = 0;
1601 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1602
1603 /* Stop Transfer for Write Multi blocks or Read Multi blocks */
1604 if (((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1605 {
1606 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1607 if (errorstate != HAL_SD_ERROR_NONE)
1608 {
1609 hsd->ErrorCode |= errorstate;
1610#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1611 hsd->ErrorCallback(hsd);
1612#else
1613 HAL_SD_ErrorCallback(hsd);
1614#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1615 }
1616 }
1617
1618 hsd->State = HAL_SD_STATE_READY;
1619 hsd->Context = SD_CONTEXT_NONE;
1620 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1621 {
1622#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1623 hsd->TxCpltCallback(hsd);
1624#else
1625 HAL_SD_TxCpltCallback(hsd);
1626#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1627 }
1628 if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1629 {
1630#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1631 hsd->RxCpltCallback(hsd);
1632#else
1633 HAL_SD_RxCpltCallback(hsd);
1634#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1635 }
1636 }
1637 else
1638 {
1639 /* Nothing to do */
1640 }
1641 }
1642
1643 else if ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1644 {
1645 SD_Write_IT(hsd);
1646 }
1647
1648 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_RXOVERR |
1649 SDMMC_FLAG_TXUNDERR) != RESET)
1650 {
1651 /* Set Error code */
1652 if (__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DCRCFAIL) != RESET)
1653 {
1654 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1655 }
1656 if (__HAL_SD_GET_FLAG(hsd, SDMMC_IT_DTIMEOUT) != RESET)
1657 {
1658 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1659 }
1660 if (__HAL_SD_GET_FLAG(hsd, SDMMC_IT_RXOVERR) != RESET)
1661 {
1662 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1663 }
1664 if (__HAL_SD_GET_FLAG(hsd, SDMMC_IT_TXUNDERR) != RESET)
1665 {
1666 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1667 }
1668
1669 /* Clear All flags */
1670 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
1671
1672 /* Disable all interrupts */
1673 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | \
1674 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR);
1675
1676 __SDMMC_CMDTRANS_DISABLE(hsd->Instance);
1677 hsd->Instance->DCTRL |= SDMMC_DCTRL_FIFORST;
1678 hsd->Instance->CMD |= SDMMC_CMD_CMDSTOP;
1679 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
1680 hsd->Instance->CMD &= ~(SDMMC_CMD_CMDSTOP);
1681 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DABORT);
1682
1683 if ((context & SD_CONTEXT_IT) != 0U)
1684 {
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);
1690#else
1691 HAL_SD_ErrorCallback(hsd);
1692#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1693 }
1694 else if ((context & SD_CONTEXT_DMA) != 0U)
1695 {
1696 if (hsd->ErrorCode != HAL_SD_ERROR_NONE)
1697 {
1698 /* Disable Internal DMA */
1699 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_IDMABTC);
1700 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
1701
1702 /* Set the SD state to ready to be able to start again the process */
1703 hsd->State = HAL_SD_STATE_READY;
1704#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1705 hsd->ErrorCallback(hsd);
1706#else
1707 HAL_SD_ErrorCallback(hsd);
1708#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1709 }
1710 }
1711 else
1712 {
1713 /* Nothing to do */
1714 }
1715 }
1716
1717 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_IDMABTC) != RESET)
1718 {
1719 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_IDMABTC);
1720 if (READ_BIT(hsd->Instance->IDMACTRL, SDMMC_IDMA_IDMABACT) == 0U)
1721 {
1722 /* Current buffer is buffer0, Transfer complete for buffer1 */
1723 if ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1724 {
1725#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1726 hsd->Write_DMADblBuf1CpltCallback(hsd);
1727#else
1728 HAL_SDEx_Write_DMADoubleBuf1CpltCallback(hsd);
1729#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1730 }
1731 else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
1732 {
1733#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1734 hsd->Read_DMADblBuf1CpltCallback(hsd);
1735#else
1736 HAL_SDEx_Read_DMADoubleBuf1CpltCallback(hsd);
1737#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1738 }
1739 }
1740 else /* SD_DMA_BUFFER1 */
1741 {
1742 /* Current buffer is buffer1, Transfer complete for buffer0 */
1743 if ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1744 {
1745#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1746 hsd->Write_DMADblBuf0CpltCallback(hsd);
1747#else
1748 HAL_SDEx_Write_DMADoubleBuf0CpltCallback(hsd);
1749#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1750 }
1751 else /* SD_CONTEXT_READ_MULTIPLE_BLOCK */
1752 {
1753#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1754 hsd->Read_DMADblBuf0CpltCallback(hsd);
1755#else
1756 HAL_SDEx_Read_DMADoubleBuf0CpltCallback(hsd);
1757#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1758 }
1759 }
1760 }
1761 else
1762 {
1763 /* Nothing to do */
1764 }
1765}
1766
1767/**
1768 * @brief return the SD state
1769 * @param hsd: Pointer to sd handle
1770 * @retval HAL state
1771 */
1772HAL_SD_StateTypeDef HAL_SD_GetState(const SD_HandleTypeDef *hsd)
1773{
1774 return hsd->State;
1775}
1776
1777/**
1778 * @brief Return the SD error code
1779 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
1780 * the configuration information.
1781 * @retval SD Error Code
1782 */
1783uint32_t HAL_SD_GetError(const SD_HandleTypeDef *hsd)
1784{
1785 return hsd->ErrorCode;
1786}
1787
1788/**
1789 * @brief Tx Transfer completed callbacks
1790 * @param hsd: Pointer to SD handle
1791 * @retval None
1792 */
1793__weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
1794{
1795 /* Prevent unused argument(s) compilation warning */
1796 UNUSED(hsd);
1797
1798 /* NOTE : This function should not be modified, when the callback is needed,
1799 the HAL_SD_TxCpltCallback can be implemented in the user file
1800 */
1801}
1802
1803/**
1804 * @brief Rx Transfer completed callbacks
1805 * @param hsd: Pointer SD handle
1806 * @retval None
1807 */
1808__weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
1809{
1810 /* Prevent unused argument(s) compilation warning */
1811 UNUSED(hsd);
1812
1813 /* NOTE : This function should not be modified, when the callback is needed,
1814 the HAL_SD_RxCpltCallback can be implemented in the user file
1815 */
1816}
1817
1818/**
1819 * @brief SD error callbacks
1820 * @param hsd: Pointer SD handle
1821 * @retval None
1822 */
1823__weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1824{
1825 /* Prevent unused argument(s) compilation warning */
1826 UNUSED(hsd);
1827
1828 /* NOTE : This function should not be modified, when the callback is needed,
1829 the HAL_SD_ErrorCallback can be implemented in the user file
1830 */
1831}
1832
1833/**
1834 * @brief SD Abort callbacks
1835 * @param hsd: Pointer SD handle
1836 * @retval None
1837 */
1838__weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
1839{
1840 /* Prevent unused argument(s) compilation warning */
1841 UNUSED(hsd);
1842
1843 /* NOTE : This function should not be modified, when the callback is needed,
1844 the HAL_SD_AbortCallback can be implemented in the user file
1845 */
1846}
1847
1848#if (USE_SD_TRANSCEIVER != 0U)
1849/**
1850 * @brief Enable/Disable the SD Transceiver 1.8V Mode Callback.
1851 * @param status: Voltage Switch State
1852 * @retval None
1853 */
1854__weak void HAL_SD_DriveTransceiver_1_8V_Callback(FlagStatus status)
1855{
1856 /* Prevent unused argument(s) compilation warning */
1857 UNUSED(status);
1858 /* NOTE : This function should not be modified, when the callback is needed,
1859 the HAL_SD_EnableTransceiver could be implemented in the user file
1860 */
1861}
1862#endif /* USE_SD_TRANSCEIVER */
1863
1864#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1865/**
1866 * @brief Register a User SD Callback
1867 * To be used instead of the weak (overridden) predefined callback
1868 * @note The HAL_SD_RegisterCallback() may be called before HAL_SD_Init() in
1869 * HAL_SD_STATE_RESET to register callbacks for HAL_SD_MSP_INIT_CB_ID
1870 * and HAL_SD_MSP_DEINIT_CB_ID.
1871 * @param hsd : SD handle
1872 * @param CallbackID : ID of the callback to be registered
1873 * This parameter can be one of the following values:
1874 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1875 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1876 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1877 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1878 * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID
1879 * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID
1880 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID
1881 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID
1882 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1883 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1884 * @param pCallback : pointer to the Callback function
1885 * @retval status
1886 */
1887HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID,
1888 pSD_CallbackTypeDef pCallback)
1889{
1891
1892 if (pCallback == NULL)
1893 {
1894 /* Update the error code */
1895 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1896 return HAL_ERROR;
1897 }
1898
1899 if (hsd->State == HAL_SD_STATE_READY)
1900 {
1901 switch (CallbackID)
1902 {
1903 case HAL_SD_TX_CPLT_CB_ID :
1904 hsd->TxCpltCallback = pCallback;
1905 break;
1906 case HAL_SD_RX_CPLT_CB_ID :
1907 hsd->RxCpltCallback = pCallback;
1908 break;
1909 case HAL_SD_ERROR_CB_ID :
1910 hsd->ErrorCallback = pCallback;
1911 break;
1912 case HAL_SD_ABORT_CB_ID :
1913 hsd->AbortCpltCallback = pCallback;
1914 break;
1915 case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID :
1916 hsd->Read_DMADblBuf0CpltCallback = pCallback;
1917 break;
1918 case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID :
1919 hsd->Read_DMADblBuf1CpltCallback = pCallback;
1920 break;
1921 case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
1922 hsd->Write_DMADblBuf0CpltCallback = pCallback;
1923 break;
1924 case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
1925 hsd->Write_DMADblBuf1CpltCallback = pCallback;
1926 break;
1927 case HAL_SD_MSP_INIT_CB_ID :
1928 hsd->MspInitCallback = pCallback;
1929 break;
1930 case HAL_SD_MSP_DEINIT_CB_ID :
1931 hsd->MspDeInitCallback = pCallback;
1932 break;
1933 default :
1934 /* Update the error code */
1935 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1936 /* update return status */
1937 status = HAL_ERROR;
1938 break;
1939 }
1940 }
1941 else if (hsd->State == HAL_SD_STATE_RESET)
1942 {
1943 switch (CallbackID)
1944 {
1945 case HAL_SD_MSP_INIT_CB_ID :
1946 hsd->MspInitCallback = pCallback;
1947 break;
1948 case HAL_SD_MSP_DEINIT_CB_ID :
1949 hsd->MspDeInitCallback = pCallback;
1950 break;
1951 default :
1952 /* Update the error code */
1953 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1954 /* update return status */
1955 status = HAL_ERROR;
1956 break;
1957 }
1958 }
1959 else
1960 {
1961 /* Update the error code */
1962 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1963 /* update return status */
1964 status = HAL_ERROR;
1965 }
1966
1967 return status;
1968}
1969
1970/**
1971 * @brief Unregister a User SD Callback
1972 * SD Callback is redirected to the weak (overridden) predefined callback
1973 * @note The HAL_SD_UnRegisterCallback() may be called before HAL_SD_Init() in
1974 * HAL_SD_STATE_RESET to register callbacks for HAL_SD_MSP_INIT_CB_ID
1975 * and HAL_SD_MSP_DEINIT_CB_ID.
1976 * @param hsd : SD handle
1977 * @param CallbackID : ID of the callback to be unregistered
1978 * This parameter can be one of the following values:
1979 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1980 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1981 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1982 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1983 * @arg @ref HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Rx Double buffer 0 Callback ID
1984 * @arg @ref HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Rx Double buffer 1 Callback ID
1985 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID SD DMA Tx Double buffer 0 Callback ID
1986 * @arg @ref HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID SD DMA Tx Double buffer 1 Callback ID
1987 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1988 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1989 * @retval status
1990 */
1991HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
1992{
1994
1995 if (hsd->State == HAL_SD_STATE_READY)
1996 {
1997 switch (CallbackID)
1998 {
1999 case HAL_SD_TX_CPLT_CB_ID :
2000 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
2001 break;
2002 case HAL_SD_RX_CPLT_CB_ID :
2003 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
2004 break;
2005 case HAL_SD_ERROR_CB_ID :
2006 hsd->ErrorCallback = HAL_SD_ErrorCallback;
2007 break;
2008 case HAL_SD_ABORT_CB_ID :
2009 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
2010 break;
2011 case HAL_SD_READ_DMA_DBL_BUF0_CPLT_CB_ID :
2012 hsd->Read_DMADblBuf0CpltCallback = HAL_SDEx_Read_DMADoubleBuf0CpltCallback;
2013 break;
2014 case HAL_SD_READ_DMA_DBL_BUF1_CPLT_CB_ID :
2015 hsd->Read_DMADblBuf1CpltCallback = HAL_SDEx_Read_DMADoubleBuf1CpltCallback;
2016 break;
2017 case HAL_SD_WRITE_DMA_DBL_BUF0_CPLT_CB_ID :
2018 hsd->Write_DMADblBuf0CpltCallback = HAL_SDEx_Write_DMADoubleBuf0CpltCallback;
2019 break;
2020 case HAL_SD_WRITE_DMA_DBL_BUF1_CPLT_CB_ID :
2021 hsd->Write_DMADblBuf1CpltCallback = HAL_SDEx_Write_DMADoubleBuf1CpltCallback;
2022 break;
2023 case HAL_SD_MSP_INIT_CB_ID :
2024 hsd->MspInitCallback = HAL_SD_MspInit;
2025 break;
2026 case HAL_SD_MSP_DEINIT_CB_ID :
2027 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
2028 break;
2029 default :
2030 /* Update the error code */
2031 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2032 /* update return status */
2033 status = HAL_ERROR;
2034 break;
2035 }
2036 }
2037 else if (hsd->State == HAL_SD_STATE_RESET)
2038 {
2039 switch (CallbackID)
2040 {
2041 case HAL_SD_MSP_INIT_CB_ID :
2042 hsd->MspInitCallback = HAL_SD_MspInit;
2043 break;
2044 case HAL_SD_MSP_DEINIT_CB_ID :
2045 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
2046 break;
2047 default :
2048 /* Update the error code */
2049 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2050 /* update return status */
2051 status = HAL_ERROR;
2052 break;
2053 }
2054 }
2055 else
2056 {
2057 /* Update the error code */
2058 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2059 /* update return status */
2060 status = HAL_ERROR;
2061 }
2062
2063 return status;
2064}
2065
2066#if (USE_SD_TRANSCEIVER != 0U)
2067/**
2068 * @brief Register a User SD Transceiver Callback
2069 * To be used instead of the weak (overridden) predefined callback
2070 * @param hsd : SD handle
2071 * @param pCallback : pointer to the Callback function
2072 * @retval status
2073 */
2074HAL_StatusTypeDef HAL_SD_RegisterTransceiverCallback(SD_HandleTypeDef *hsd, pSD_TransceiverCallbackTypeDef pCallback)
2075{
2077
2078 if (pCallback == NULL)
2079 {
2080 /* Update the error code */
2081 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2082 return HAL_ERROR;
2083 }
2084
2085 /* Process locked */
2086 __HAL_LOCK(hsd);
2087
2088 if (hsd->State == HAL_SD_STATE_READY)
2089 {
2090 hsd->DriveTransceiver_1_8V_Callback = pCallback;
2091 }
2092 else
2093 {
2094 /* Update the error code */
2095 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2096 /* update return status */
2097 status = HAL_ERROR;
2098 }
2099
2100 /* Release Lock */
2101 __HAL_UNLOCK(hsd);
2102 return status;
2103}
2104
2105/**
2106 * @brief Unregister a User SD Transceiver Callback
2107 * SD Callback is redirected to the weak (overridden) predefined callback
2108 * @param hsd : SD handle
2109 * @retval status
2110 */
2111HAL_StatusTypeDef HAL_SD_UnRegisterTransceiverCallback(SD_HandleTypeDef *hsd)
2112{
2114
2115 /* Process locked */
2116 __HAL_LOCK(hsd);
2117
2118 if (hsd->State == HAL_SD_STATE_READY)
2119 {
2120 hsd->DriveTransceiver_1_8V_Callback = HAL_SD_DriveTransceiver_1_8V_Callback;
2121 }
2122 else
2123 {
2124 /* Update the error code */
2125 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
2126 /* update return status */
2127 status = HAL_ERROR;
2128 }
2129
2130 /* Release Lock */
2131 __HAL_UNLOCK(hsd);
2132 return status;
2133}
2134#endif /* USE_SD_TRANSCEIVER */
2135#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2136
2137/**
2138 * @}
2139 */
2140
2141/** @addtogroup SD_Exported_Functions_Group3
2142 * @brief management functions
2143 *
2144@verbatim
2145 ==============================================================================
2146 ##### Peripheral Control functions #####
2147 ==============================================================================
2148 [..]
2149 This subsection provides a set of functions allowing to control the SD card
2150 operations and get the related information
2151
2152@endverbatim
2153 * @{
2154 */
2155
2156/**
2157 * @brief Returns information the information of the card which are stored on
2158 * the CID register.
2159 * @param hsd: Pointer to SD handle
2160 * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that
2161 * contains all CID register parameters
2162 * @retval HAL status
2163 */
2164HAL_StatusTypeDef HAL_SD_GetCardCID(const SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
2165{
2166 pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
2167
2168 pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
2169
2170 pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
2171
2172 pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
2173
2174 pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
2175
2176 pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
2177
2178 pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
2179
2180 pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
2181
2182 pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
2183
2184 pCID->Reserved2 = 1U;
2185
2186 return HAL_OK;
2187}
2188
2189/**
2190 * @brief Returns information the information of the card which are stored on
2191 * the CSD register.
2192 * @param hsd: Pointer to SD handle
2193 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
2194 * contains all CSD register parameters
2195 * @retval HAL status
2196 */
2197HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
2198{
2199 pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
2200
2201 pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
2202
2203 pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
2204
2205 pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
2206
2207 pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
2208
2209 pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
2210
2211 pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
2212
2213 pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
2214
2215 pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
2216
2217 pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
2218
2219 pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
2220
2221 pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
2222
2223 pCSD->Reserved2 = 0U; /*!< Reserved */
2224
2225 if (hsd->SdCard.CardType == CARD_SDSC)
2226 {
2227 pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
2228
2229 pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
2230
2231 pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
2232
2233 pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
2234
2235 pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
2236
2237 pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
2238
2239 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2240 hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2241 hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2242
2243 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / BLOCKSIZE);
2244 hsd->SdCard.LogBlockSize = BLOCKSIZE;
2245 }
2246 else if (hsd->SdCard.CardType == CARD_SDHC_SDXC)
2247 {
2248 /* Byte 7 */
2249 pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
2250
2251 hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
2252 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
2253 hsd->SdCard.BlockSize = BLOCKSIZE;
2254 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
2255 }
2256 else
2257 {
2258 /* Clear all the static flags */
2259 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2260 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2261 hsd->State = HAL_SD_STATE_READY;
2262 return HAL_ERROR;
2263 }
2264
2265 pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
2266
2267 pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
2268
2269 pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
2270
2271 pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
2272
2273 pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
2274
2275 pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
2276
2277 pCSD->MaxWrBlockLen = (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
2278
2279 pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
2280
2281 pCSD->Reserved3 = 0;
2282
2283 pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
2284
2285 pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
2286
2287 pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
2288
2289 pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
2290
2291 pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
2292
2293 pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
2294
2295 pCSD->ECC = (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
2296
2297 pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
2298
2299 pCSD->Reserved4 = 1;
2300
2301 return HAL_OK;
2302}
2303
2304/**
2305 * @brief Gets the SD status info.( shall be called if there is no SD transaction ongoing )
2306 * @param hsd: Pointer to SD handle
2307 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
2308 * will contain the SD card status information
2309 * @retval HAL status
2310 */
2311HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
2312{
2313 uint32_t sd_status[16];
2314 uint32_t errorstate;
2316
2317 if (hsd->State == HAL_SD_STATE_BUSY)
2318 {
2319 return HAL_ERROR;
2320 }
2321
2322 errorstate = SD_SendSDStatus(hsd, sd_status);
2323 if (errorstate != HAL_SD_ERROR_NONE)
2324 {
2325 /* Clear all the static flags */
2326 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2327 hsd->ErrorCode |= errorstate;
2328 hsd->State = HAL_SD_STATE_READY;
2329 status = HAL_ERROR;
2330 }
2331 else
2332 {
2333 pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
2334
2335 pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
2336
2337 pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
2338
2339 pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
2340 ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
2341
2342 pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
2343
2344 pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
2345
2346 pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
2347
2348 pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
2349
2350 pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
2351
2352 pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
2353
2354 pStatus->UhsSpeedGrade = (uint8_t)((sd_status[3] & 0x00F0U) >> 4U);
2355 pStatus->UhsAllocationUnitSize = (uint8_t)(sd_status[3] & 0x000FU) ;
2356 pStatus->VideoSpeedClass = (uint8_t)((sd_status[4] & 0xFF000000U) >> 24U);
2357 }
2358
2359 /* Set Block Size for Card */
2360 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
2361 if (errorstate != HAL_SD_ERROR_NONE)
2362 {
2363 /* Clear all the static flags */
2364 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2365 hsd->ErrorCode = errorstate;
2366 hsd->State = HAL_SD_STATE_READY;
2367 status = HAL_ERROR;
2368 }
2369
2370 return status;
2371}
2372
2373/**
2374 * @brief Gets the SD card info.
2375 * @param hsd: Pointer to SD handle
2376 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
2377 * will contain the SD card status information
2378 * @retval HAL status
2379 */
2380HAL_StatusTypeDef HAL_SD_GetCardInfo(const SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
2381{
2382 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
2383 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
2384 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
2385 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
2386 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
2387 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
2388 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
2389 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
2390
2391 return HAL_OK;
2392}
2393
2394/**
2395 * @brief Enables wide bus operation for the requested card if supported by
2396 * card.
2397 * @param hsd: Pointer to SD handle
2398 * @param WideMode: Specifies the SD card wide bus mode
2399 * This parameter can be one of the following values:
2400 * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer
2401 * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
2402 * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
2403 * @retval HAL status
2404 */
2405HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
2406{
2407 SDMMC_InitTypeDef Init;
2408 uint32_t errorstate;
2409 uint32_t sdmmc_clk;
2410
2412
2413 /* Check the parameters */
2414 assert_param(IS_SDMMC_BUS_WIDE(WideMode));
2415
2416 /* Change State */
2417 hsd->State = HAL_SD_STATE_BUSY;
2418
2419 if (hsd->SdCard.CardType != CARD_SECURED)
2420 {
2421 if (WideMode == SDMMC_BUS_WIDE_8B)
2422 {
2423 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2424 }
2425 else if (WideMode == SDMMC_BUS_WIDE_4B)
2426 {
2427 errorstate = SD_WideBus_Enable(hsd);
2428
2429 hsd->ErrorCode |= errorstate;
2430 }
2431 else if (WideMode == SDMMC_BUS_WIDE_1B)
2432 {
2433 errorstate = SD_WideBus_Disable(hsd);
2434
2435 hsd->ErrorCode |= errorstate;
2436 }
2437 else
2438 {
2439 /* WideMode is not a valid argument*/
2440 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2441 }
2442 }
2443 else
2444 {
2445 /* SD Card does not support this feature */
2446 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2447 }
2448
2449 if (hsd->ErrorCode != HAL_SD_ERROR_NONE)
2450 {
2451 /* Clear all the static flags */
2452 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2453 status = HAL_ERROR;
2454 }
2455 else
2456 {
2458 if (sdmmc_clk != 0U)
2459 {
2460 /* Configure the SDMMC peripheral */
2461 Init.ClockEdge = hsd->Init.ClockEdge;
2462 Init.ClockPowerSave = hsd->Init.ClockPowerSave;
2463 Init.BusWide = WideMode;
2464 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2465
2466 /* Check if user Clock div < Normal speed 25Mhz, no change in Clockdiv */
2467 if (hsd->Init.ClockDiv >= (sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ)))
2468 {
2469 Init.ClockDiv = hsd->Init.ClockDiv;
2470 }
2471 else if (hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
2472 {
2473 /* UltraHigh speed SD card,user Clock div */
2474 Init.ClockDiv = hsd->Init.ClockDiv;
2475 }
2476 else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
2477 {
2478 /* High speed SD card, Max Frequency = 50Mhz */
2479 if (hsd->Init.ClockDiv == 0U)
2480 {
2481 if (sdmmc_clk > SD_HIGH_SPEED_FREQ)
2482 {
2483 Init.ClockDiv = sdmmc_clk / (2U * SD_HIGH_SPEED_FREQ);
2484 }
2485 else
2486 {
2487 Init.ClockDiv = hsd->Init.ClockDiv;
2488 }
2489 }
2490 else
2491 {
2492 if ((sdmmc_clk / (2U * hsd->Init.ClockDiv)) > SD_HIGH_SPEED_FREQ)
2493 {
2494 Init.ClockDiv = sdmmc_clk / (2U * SD_HIGH_SPEED_FREQ);
2495 }
2496 else
2497 {
2498 Init.ClockDiv = hsd->Init.ClockDiv;
2499 }
2500 }
2501 }
2502 else
2503 {
2504 /* No High speed SD card, Max Frequency = 25Mhz */
2505 if (hsd->Init.ClockDiv == 0U)
2506 {
2507 if (sdmmc_clk > SD_NORMAL_SPEED_FREQ)
2508 {
2509 Init.ClockDiv = sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ);
2510 }
2511 else
2512 {
2513 Init.ClockDiv = hsd->Init.ClockDiv;
2514 }
2515 }
2516 else
2517 {
2518 if ((sdmmc_clk / (2U * hsd->Init.ClockDiv)) > SD_NORMAL_SPEED_FREQ)
2519 {
2520 Init.ClockDiv = sdmmc_clk / (2U * SD_NORMAL_SPEED_FREQ);
2521 }
2522 else
2523 {
2524 Init.ClockDiv = hsd->Init.ClockDiv;
2525 }
2526 }
2527 }
2528
2529#if (USE_SD_TRANSCEIVER != 0U)
2530 Init.TranceiverPresent = hsd->Init.TranceiverPresent;
2531#endif /* USE_SD_TRANSCEIVER */
2532
2533 (void)SDMMC_Init(hsd->Instance, Init);
2534 }
2535 else
2536 {
2537 hsd->ErrorCode |= SDMMC_ERROR_INVALID_PARAMETER;
2538 status = HAL_ERROR;
2539 }
2540 }
2541
2542 /* Set Block Size for Card */
2543 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
2544 if (errorstate != HAL_SD_ERROR_NONE)
2545 {
2546 /* Clear all the static flags */
2547 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2548 hsd->ErrorCode |= errorstate;
2549 status = HAL_ERROR;
2550 }
2551
2552 /* Change State */
2553 hsd->State = HAL_SD_STATE_READY;
2554
2555 return status;
2556}
2557
2558/**
2559 * @brief Configure the speed bus mode
2560 * @param hsd: Pointer to the SD handle
2561 * @param SpeedMode: Specifies the SD card speed bus mode
2562 * This parameter can be one of the following values:
2563 * @arg SDMMC_SPEED_MODE_AUTO: Max speed mode supported by the card
2564 * @arg SDMMC_SPEED_MODE_DEFAULT: Default Speed/SDR12 mode
2565 * @arg SDMMC_SPEED_MODE_HIGH: High Speed/SDR25 mode
2566 * @arg SDMMC_SPEED_MODE_ULTRA: Ultra high speed mode
2567 * @retval HAL status
2568 */
2569
2570HAL_StatusTypeDef HAL_SD_ConfigSpeedBusOperation(SD_HandleTypeDef *hsd, uint32_t SpeedMode)
2571{
2572 uint32_t tickstart;
2573 uint32_t errorstate;
2575
2576 /* Check the parameters */
2577 assert_param(IS_SDMMC_SPEED_MODE(SpeedMode));
2578 /* Change State */
2579 hsd->State = HAL_SD_STATE_BUSY;
2580
2581#if (USE_SD_TRANSCEIVER != 0U)
2582 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
2583 {
2584 switch (SpeedMode)
2585 {
2586 case SDMMC_SPEED_MODE_AUTO:
2587 {
2588 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2589 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2590 {
2591 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED;
2592 /* Enable Ultra High Speed */
2593 if (SD_UltraHighSpeed(hsd, SDMMC_SDR104_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2594 {
2595 if (SD_SwitchSpeed(hsd, SDMMC_SDR25_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2596 {
2597 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2598 status = HAL_ERROR;
2599 }
2600 }
2601 }
2602 else if (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED)
2603 {
2604 /* Enable High Speed */
2605 if (SD_SwitchSpeed(hsd, SDMMC_SDR25_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2606 {
2607 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2608 status = HAL_ERROR;
2609 }
2610 }
2611 else
2612 {
2613 /*Nothing to do, Use defaultSpeed */
2614 }
2615 break;
2616 }
2617 case SDMMC_SPEED_MODE_ULTRA_SDR104:
2618 {
2619 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2620 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2621 {
2622 /* Enable UltraHigh Speed */
2623 if (SD_UltraHighSpeed(hsd, SDMMC_SDR104_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2624 {
2625 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2626 status = HAL_ERROR;
2627 }
2628 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED;
2629 }
2630 else
2631 {
2632 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2633 status = HAL_ERROR;
2634 }
2635 break;
2636 }
2637 case SDMMC_SPEED_MODE_ULTRA_SDR50:
2638 {
2639 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2640 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2641 {
2642 /* Enable UltraHigh Speed */
2643 if (SD_UltraHighSpeed(hsd, SDMMC_SDR50_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2644 {
2645 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2646 status = HAL_ERROR;
2647 }
2648 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED;
2649 }
2650 else
2651 {
2652 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2653 status = HAL_ERROR;
2654 }
2655 break;
2656 }
2657 case SDMMC_SPEED_MODE_DDR:
2658 {
2659 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2660 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2661 {
2662 /* Enable DDR Mode*/
2663 if (SD_DDR_Mode(hsd) != HAL_SD_ERROR_NONE)
2664 {
2665 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2666 status = HAL_ERROR;
2667 }
2668 hsd->Instance->CLKCR |= SDMMC_CLKCR_BUSSPEED | SDMMC_CLKCR_DDR;
2669 }
2670 else
2671 {
2672 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2673 status = HAL_ERROR;
2674 }
2675 break;
2676 }
2677 case SDMMC_SPEED_MODE_HIGH:
2678 {
2679 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2680 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2681 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2682 {
2683 /* Enable High Speed */
2684 if (SD_SwitchSpeed(hsd, SDMMC_SDR25_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2685 {
2686 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2687 status = HAL_ERROR;
2688 }
2689 }
2690 else
2691 {
2692 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2693 status = HAL_ERROR;
2694 }
2695 break;
2696 }
2697 case SDMMC_SPEED_MODE_DEFAULT:
2698 {
2699 /* Switch to default Speed */
2700 if (SD_SwitchSpeed(hsd, SDMMC_SDR12_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2701 {
2702 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2703 status = HAL_ERROR;
2704 }
2705
2706 break;
2707 }
2708 default:
2709 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2710 status = HAL_ERROR;
2711 break;
2712 }
2713 }
2714 else
2715 {
2716 switch (SpeedMode)
2717 {
2718 case SDMMC_SPEED_MODE_AUTO:
2719 {
2720 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2721 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2722 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2723 {
2724 /* Enable High Speed */
2725 if (SD_SwitchSpeed(hsd, SDMMC_SDR25_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2726 {
2727 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2728 status = HAL_ERROR;
2729 }
2730 }
2731 else
2732 {
2733 /*Nothing to do, Use defaultSpeed */
2734 }
2735 break;
2736 }
2737 case SDMMC_SPEED_MODE_HIGH:
2738 {
2739 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2740 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2741 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2742 {
2743 /* Enable High Speed */
2744 if (SD_SwitchSpeed(hsd, SDMMC_SDR25_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2745 {
2746 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2747 status = HAL_ERROR;
2748 }
2749 }
2750 else
2751 {
2752 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2753 status = HAL_ERROR;
2754 }
2755 break;
2756 }
2757 case SDMMC_SPEED_MODE_DEFAULT:
2758 {
2759 /* Switch to default Speed */
2760 if (SD_SwitchSpeed(hsd, SDMMC_SDR12_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2761 {
2762 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2763 status = HAL_ERROR;
2764 }
2765
2766 break;
2767 }
2768 case SDMMC_SPEED_MODE_ULTRA: /*not valid without transceiver*/
2769 default:
2770 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2771 status = HAL_ERROR;
2772 break;
2773 }
2774 }
2775#else
2776 switch (SpeedMode)
2777 {
2778 case SDMMC_SPEED_MODE_AUTO:
2779 {
2780 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2781 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2782 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2783 {
2784 /* Enable High Speed */
2785 if (SD_SwitchSpeed(hsd, SDMMC_SDR25_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2786 {
2787 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2788 status = HAL_ERROR;
2789 }
2790 }
2791 else
2792 {
2793 /*Nothing to do, Use defaultSpeed */
2794 }
2795 break;
2796 }
2797 case SDMMC_SPEED_MODE_HIGH:
2798 {
2799 if ((hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED) ||
2800 (hsd->SdCard.CardSpeed == CARD_HIGH_SPEED) ||
2801 (hsd->SdCard.CardType == CARD_SDHC_SDXC))
2802 {
2803 /* Enable High Speed */
2804 if (SD_SwitchSpeed(hsd, SDMMC_SDR25_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2805 {
2806 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2807 status = HAL_ERROR;
2808 }
2809 }
2810 else
2811 {
2812 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2813 status = HAL_ERROR;
2814 }
2815 break;
2816 }
2817 case SDMMC_SPEED_MODE_DEFAULT:
2818 {
2819 /* Switch to default Speed */
2820 if (SD_SwitchSpeed(hsd, SDMMC_SDR12_SWITCH_PATTERN) != HAL_SD_ERROR_NONE)
2821 {
2822 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2823 status = HAL_ERROR;
2824 }
2825
2826 break;
2827 }
2828 case SDMMC_SPEED_MODE_ULTRA: /*not valid without transceiver*/
2829 default:
2830 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2831 status = HAL_ERROR;
2832 break;
2833 }
2834#endif /* USE_SD_TRANSCEIVER */
2835
2836 /* Verify that SD card is ready to use after Speed mode switch*/
2837 tickstart = HAL_GetTick();
2838 while ((HAL_SD_GetCardState(hsd) != HAL_SD_CARD_TRANSFER))
2839 {
2840 if ((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
2841 {
2842 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
2843 hsd->State = HAL_SD_STATE_READY;
2844 return HAL_TIMEOUT;
2845 }
2846 }
2847
2848 /* Set Block Size for Card */
2849 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
2850 if (errorstate != HAL_SD_ERROR_NONE)
2851 {
2852 /* Clear all the static flags */
2853 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_FLAGS);
2854 hsd->ErrorCode |= errorstate;
2855 status = HAL_ERROR;
2856 }
2857
2858 /* Change State */
2859 hsd->State = HAL_SD_STATE_READY;
2860 return status;
2861}
2862
2863/**
2864 * @brief Gets the current sd card data state.
2865 * @param hsd: pointer to SD handle
2866 * @retval Card state
2867 */
2868HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2869{
2870 uint32_t cardstate;
2871 uint32_t errorstate;
2872 uint32_t resp1 = 0;
2873
2874 errorstate = SD_SendStatus(hsd, &resp1);
2875 if (errorstate != HAL_SD_ERROR_NONE)
2876 {
2877 hsd->ErrorCode |= errorstate;
2878 }
2879
2880 cardstate = ((resp1 >> 9U) & 0x0FU);
2881
2882 return (HAL_SD_CardStateTypeDef)cardstate;
2883}
2884
2885/**
2886 * @brief Abort the current transfer and disable the SD.
2887 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2888 * the configuration information for SD module.
2889 * @retval HAL status
2890 */
2891HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
2892{
2893 uint32_t error_code;
2894 uint32_t tickstart;
2895
2896 if (hsd->State == HAL_SD_STATE_BUSY)
2897 {
2898 /* DIsable All interrupts */
2899 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | \
2900 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR);
2901 __SDMMC_CMDTRANS_DISABLE(hsd->Instance);
2902
2903 /*we will send the CMD12 in all cases in order to stop the data transfers*/
2904 /*In case the data transfer just finished , the external memory will not respond
2905 and will return HAL_SD_ERROR_CMD_RSP_TIMEOUT*/
2906 /*In case the data transfer aborted , the external memory will respond and will return HAL_SD_ERROR_NONE*/
2907 /*Other scenario will return HAL_ERROR*/
2908
2909 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2910 error_code = hsd->ErrorCode;
2911 if ((error_code != HAL_SD_ERROR_NONE) && (error_code != HAL_SD_ERROR_CMD_RSP_TIMEOUT))
2912 {
2913 return HAL_ERROR;
2914 }
2915
2916 tickstart = HAL_GetTick();
2917 if ((hsd->Instance->DCTRL & SDMMC_DCTRL_DTDIR) == SDMMC_TRANSFER_DIR_TO_CARD)
2918 {
2919 if (hsd->ErrorCode == HAL_SD_ERROR_NONE)
2920 {
2921 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DABORT | SDMMC_FLAG_BUSYD0END))
2922 {
2923 if ((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
2924 {
2925 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
2926 hsd->State = HAL_SD_STATE_READY;
2927 return HAL_TIMEOUT;
2928 }
2929 }
2930 }
2931
2932 if (hsd->ErrorCode == HAL_SD_ERROR_CMD_RSP_TIMEOUT)
2933 {
2934 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DATAEND))
2935 {
2936 if ((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
2937 {
2938 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
2939 hsd->State = HAL_SD_STATE_READY;
2940 return HAL_TIMEOUT;
2941 }
2942 }
2943 }
2944 }
2945 else if ((hsd->Instance->DCTRL & SDMMC_DCTRL_DTDIR) == SDMMC_TRANSFER_DIR_TO_SDMMC)
2946 {
2947 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DABORT | SDMMC_FLAG_DATAEND))
2948 {
2949 if ((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
2950 {
2951 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
2952 hsd->State = HAL_SD_STATE_READY;
2953 return HAL_TIMEOUT;
2954 }
2955 }
2956 }
2957 else
2958 {
2959 /* Nothing to do*/
2960 }
2961
2962 /*The reason of all these while conditions previously is that we need to wait the SDMMC and clear
2963 the appropriate flags that will be set depending of the abort/non abort of the memory */
2964 /*Not waiting the SDMMC flags will cause the next SDMMC_DISABLE_IDMA to not get cleared
2965 and will result in next SDMMC read/write operation to fail */
2966
2967 /*SDMMC ready for clear data flags*/
2968 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_BUSYD0END);
2969 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
2970 /* If IDMA Context, disable Internal DMA */
2971 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
2972
2973 hsd->State = HAL_SD_STATE_READY;
2974
2975 /* Initialize the SD operation */
2976 hsd->Context = SD_CONTEXT_NONE;
2977 }
2978 return HAL_OK;
2979}
2980
2981/**
2982 * @brief Abort the current transfer and disable the SD (IT mode).
2983 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2984 * the configuration information for SD module.
2985 * @retval HAL status
2986 */
2987HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
2988{
2989 HAL_SD_CardStateTypeDef CardState;
2990
2991 /* Disable All interrupts */
2992 __HAL_SD_DISABLE_IT(hsd, SDMMC_IT_DATAEND | SDMMC_IT_DCRCFAIL | SDMMC_IT_DTIMEOUT | \
2993 SDMMC_IT_TXUNDERR | SDMMC_IT_RXOVERR);
2994
2995 /* If IDMA Context, disable Internal DMA */
2996 hsd->Instance->IDMACTRL = SDMMC_DISABLE_IDMA;
2997
2998 /* Clear All flags */
2999 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3000
3001 CardState = HAL_SD_GetCardState(hsd);
3002 hsd->State = HAL_SD_STATE_READY;
3003
3004 if ((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
3005 {
3006 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
3007 }
3008
3009 if (hsd->ErrorCode != HAL_SD_ERROR_NONE)
3010 {
3011 return HAL_ERROR;
3012 }
3013 else
3014 {
3015#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3016 hsd->AbortCpltCallback(hsd);
3017#else
3018 HAL_SD_AbortCallback(hsd);
3019#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
3020 }
3021
3022 return HAL_OK;
3023}
3024
3025/**
3026 * @}
3027 */
3028
3029/**
3030 * @}
3031 */
3032
3033/* Private function ----------------------------------------------------------*/
3034/** @addtogroup SD_Private_Functions
3035 * @{
3036 */
3037
3038/**
3039 * @brief Initializes the sd card.
3040 * @param hsd: Pointer to SD handle
3041 * @retval SD Card error state
3042 */
3043static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
3044{
3045 HAL_SD_CardCSDTypeDef CSD;
3046 uint32_t errorstate;
3047 uint16_t sd_rca = 0U;
3048 uint32_t tickstart = HAL_GetTick();
3049
3050 /* Check the power State */
3051 if (SDMMC_GetPowerState(hsd->Instance) == 0U)
3052 {
3053 /* Power off */
3054 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3055 }
3056
3057 if (hsd->SdCard.CardType != CARD_SECURED)
3058 {
3059 /* Send CMD2 ALL_SEND_CID */
3060 errorstate = SDMMC_CmdSendCID(hsd->Instance);
3061 if (errorstate != HAL_SD_ERROR_NONE)
3062 {
3063 return errorstate;
3064 }
3065 else
3066 {
3067 /* Get Card identification number data */
3068 hsd->CID[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3069 hsd->CID[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
3070 hsd->CID[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
3071 hsd->CID[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
3072 }
3073 }
3074
3075 if (hsd->SdCard.CardType != CARD_SECURED)
3076 {
3077 /* Send CMD3 SET_REL_ADDR with argument 0 */
3078 /* SD Card publishes its RCA. */
3079 while (sd_rca == 0U)
3080 {
3081 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
3082 if (errorstate != HAL_SD_ERROR_NONE)
3083 {
3084 return errorstate;
3085 }
3086 if ((HAL_GetTick() - tickstart) >= SDMMC_CMDTIMEOUT)
3087 {
3088 return HAL_SD_ERROR_TIMEOUT;
3089 }
3090 }
3091 }
3092 if (hsd->SdCard.CardType != CARD_SECURED)
3093 {
3094 /* Get the SD card RCA */
3095 hsd->SdCard.RelCardAdd = sd_rca;
3096
3097 /* Send CMD9 SEND_CSD with argument as card's RCA */
3098 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3099 if (errorstate != HAL_SD_ERROR_NONE)
3100 {
3101 return errorstate;
3102 }
3103 else
3104 {
3105 /* Get Card Specific Data */
3106 hsd->CSD[0U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3107 hsd->CSD[1U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2);
3108 hsd->CSD[2U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP3);
3109 hsd->CSD[3U] = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP4);
3110 }
3111 }
3112
3113 /* Get the Card Class */
3114 hsd->SdCard.Class = (SDMMC_GetResponse(hsd->Instance, SDMMC_RESP2) >> 20U);
3115
3116 /* Get CSD parameters */
3117 if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
3118 {
3119 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
3120 }
3121
3122 /* Select the Card */
3123 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
3124 if (errorstate != HAL_SD_ERROR_NONE)
3125 {
3126 return errorstate;
3127 }
3128
3129 /* All cards are initialized */
3130 return HAL_SD_ERROR_NONE;
3131}
3132
3133/**
3134 * @brief Enquires cards about their operating voltage and configures clock
3135 * controls and stores SD information that will be needed in future
3136 * in the SD handle.
3137 * @param hsd: Pointer to SD handle
3138 * @retval error state
3139 */
3140static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
3141{
3142 __IO uint32_t count = 0U;
3143 uint32_t response = 0U;
3144 uint32_t validvoltage = 0U;
3145 uint32_t errorstate;
3146#if (USE_SD_TRANSCEIVER != 0U)
3147 uint32_t tickstart = HAL_GetTick();
3148#endif /* USE_SD_TRANSCEIVER */
3149
3150 /* CMD0: GO_IDLE_STATE */
3151 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
3152 if (errorstate != HAL_SD_ERROR_NONE)
3153 {
3154 return errorstate;
3155 }
3156
3157 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
3158 errorstate = SDMMC_CmdOperCond(hsd->Instance);
3159 if (errorstate == SDMMC_ERROR_CMD_RSP_TIMEOUT) /* No response to CMD8 */
3160 {
3161 hsd->SdCard.CardVersion = CARD_V1_X;
3162 /* CMD0: GO_IDLE_STATE */
3163 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
3164 if (errorstate != HAL_SD_ERROR_NONE)
3165 {
3166 return errorstate;
3167 }
3168
3169 }
3170 else
3171 {
3172 hsd->SdCard.CardVersion = CARD_V2_X;
3173 }
3174
3175 if (hsd->SdCard.CardVersion == CARD_V2_X)
3176 {
3177 /* SEND CMD55 APP_CMD with RCA as 0 */
3178 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
3179 if (errorstate != HAL_SD_ERROR_NONE)
3180 {
3181 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
3182 }
3183 }
3184 /* SD CARD */
3185 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
3186 while ((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
3187 {
3188 /* SEND CMD55 APP_CMD with RCA as 0 */
3189 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
3190 if (errorstate != HAL_SD_ERROR_NONE)
3191 {
3192 return errorstate;
3193 }
3194
3195 /* Send CMD41 */
3196 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY |
3197 SD_SWITCH_1_8V_CAPACITY);
3198 if (errorstate != HAL_SD_ERROR_NONE)
3199 {
3200 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
3201 }
3202
3203 /* Get command response */
3204 response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3205
3206 /* Get operating voltage*/
3207 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
3208
3209 count++;
3210 }
3211
3212 if (count >= SDMMC_MAX_VOLT_TRIAL)
3213 {
3214 return HAL_SD_ERROR_INVALID_VOLTRANGE;
3215 }
3216
3217 /* Set default card type */
3218 hsd->SdCard.CardType = CARD_SDSC;
3219
3220 if ((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY)
3221 {
3222 hsd->SdCard.CardType = CARD_SDHC_SDXC;
3223#if (USE_SD_TRANSCEIVER != 0U)
3224 if (hsd->Init.TranceiverPresent == SDMMC_TRANSCEIVER_PRESENT)
3225 {
3226 if ((response & SD_SWITCH_1_8V_CAPACITY) == SD_SWITCH_1_8V_CAPACITY)
3227 {
3228 hsd->SdCard.CardSpeed = CARD_ULTRA_HIGH_SPEED;
3229
3230 /* Start switching procedue */
3231 hsd->Instance->POWER |= SDMMC_POWER_VSWITCHEN;
3232
3233 /* Send CMD11 to switch 1.8V mode */
3234 errorstate = SDMMC_CmdVoltageSwitch(hsd->Instance);
3235 if (errorstate != HAL_SD_ERROR_NONE)
3236 {
3237 return errorstate;
3238 }
3239
3240 /* Check to CKSTOP */
3241 while ((hsd->Instance->STA & SDMMC_FLAG_CKSTOP) != SDMMC_FLAG_CKSTOP)
3242 {
3243 if ((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
3244 {
3245 return HAL_SD_ERROR_TIMEOUT;
3246 }
3247 }
3248
3249 /* Clear CKSTOP Flag */
3250 hsd->Instance->ICR = SDMMC_FLAG_CKSTOP;
3251
3252 /* Check to BusyD0 */
3253 if ((hsd->Instance->STA & SDMMC_FLAG_BUSYD0) != SDMMC_FLAG_BUSYD0)
3254 {
3255 /* Error when activate Voltage Switch in SDMMC Peripheral */
3256 return SDMMC_ERROR_UNSUPPORTED_FEATURE;
3257 }
3258 else
3259 {
3260 /* Enable Transceiver Switch PIN */
3261#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3262 hsd->DriveTransceiver_1_8V_Callback(SET);
3263#else
3264 HAL_SD_DriveTransceiver_1_8V_Callback(SET);
3265#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
3266
3267 /* Switch ready */
3268 hsd->Instance->POWER |= SDMMC_POWER_VSWITCH;
3269
3270 /* Check VSWEND Flag */
3271 while ((hsd->Instance->STA & SDMMC_FLAG_VSWEND) != SDMMC_FLAG_VSWEND)
3272 {
3273 if ((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
3274 {
3275 return HAL_SD_ERROR_TIMEOUT;
3276 }
3277 }
3278
3279 /* Clear VSWEND Flag */
3280 hsd->Instance->ICR = SDMMC_FLAG_VSWEND;
3281
3282 /* Check BusyD0 status */
3283 if ((hsd->Instance->STA & SDMMC_FLAG_BUSYD0) == SDMMC_FLAG_BUSYD0)
3284 {
3285 /* Error when enabling 1.8V mode */
3286 return HAL_SD_ERROR_INVALID_VOLTRANGE;
3287 }
3288 /* Switch to 1.8V OK */
3289
3290 /* Disable VSWITCH FLAG from SDMMC Peripheral */
3291 hsd->Instance->POWER = 0x13U;
3292
3293 /* Clean Status flags */
3294 hsd->Instance->ICR = 0xFFFFFFFFU;
3295 }
3296 }
3297 }
3298#endif /* USE_SD_TRANSCEIVER */
3299 }
3300
3301 return HAL_SD_ERROR_NONE;
3302}
3303
3304/**
3305 * @brief Turns the SDMMC output signals off.
3306 * @param hsd: Pointer to SD handle
3307 * @retval None
3308 */
3309static void SD_PowerOFF(SD_HandleTypeDef *hsd)
3310{
3311 /* Set Power State to OFF */
3312 (void)SDMMC_PowerState_OFF(hsd->Instance);
3313}
3314
3315/**
3316 * @brief Send Status info command.
3317 * @param hsd: pointer to SD handle
3318 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
3319 * SD Status register)
3320 * @retval error state
3321 */
3322static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
3323{
3324 SDMMC_DataInitTypeDef config;
3325 uint32_t errorstate;
3326 uint32_t tickstart = HAL_GetTick();
3327 uint32_t count;
3328 uint32_t *pData = pSDstatus;
3329
3330 /* Check SD response */
3331 if ((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3332 {
3333 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3334 }
3335
3336 /* Set block size for card if it is not equal to current block size for card */
3337 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
3338 if (errorstate != HAL_SD_ERROR_NONE)
3339 {
3340 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3341 return errorstate;
3342 }
3343
3344 /* Send CMD55 */
3345 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3346 if (errorstate != HAL_SD_ERROR_NONE)
3347 {
3348 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3349 return errorstate;
3350 }
3351
3352 /* Configure the SD DPSM (Data Path State Machine) */
3353 config.DataTimeOut = SDMMC_DATATIMEOUT;
3354 config.DataLength = 64U;
3355 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B;
3356 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3357 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3358 config.DPSM = SDMMC_DPSM_ENABLE;
3359 (void)SDMMC_ConfigData(hsd->Instance, &config);
3360
3361 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
3362 errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
3363 if (errorstate != HAL_SD_ERROR_NONE)
3364 {
3365 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
3366 return errorstate;
3367 }
3368
3369 /* Get status data */
3370 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))
3371 {
3372 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3373 {
3374 for (count = 0U; count < 8U; count++)
3375 {
3376 *pData = SDMMC_ReadFIFO(hsd->Instance);
3377 pData++;
3378 }
3379 }
3380
3381 if ((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
3382 {
3383 return HAL_SD_ERROR_TIMEOUT;
3384 }
3385 }
3386
3387 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3388 {
3389 return HAL_SD_ERROR_DATA_TIMEOUT;
3390 }
3391 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3392 {
3393 return HAL_SD_ERROR_DATA_CRC_FAIL;
3394 }
3395 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3396 {
3397 return HAL_SD_ERROR_RX_OVERRUN;
3398 }
3399 else
3400 {
3401 /* Nothing to do */
3402 }
3403
3404 while ((__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DPSMACT)))
3405 {
3406 *pData = SDMMC_ReadFIFO(hsd->Instance);
3407 pData++;
3408
3409 if ((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
3410 {
3411 return HAL_SD_ERROR_TIMEOUT;
3412 }
3413 }
3414
3415 /* Clear all the static status flags*/
3416 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3417
3418 return HAL_SD_ERROR_NONE;
3419}
3420
3421/**
3422 * @brief Returns the current card's status.
3423 * @param hsd: Pointer to SD handle
3424 * @param pCardStatus: pointer to the buffer that will contain the SD card
3425 * status (Card Status register)
3426 * @retval error state
3427 */
3428static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
3429{
3430 uint32_t errorstate;
3431
3432 if (pCardStatus == NULL)
3433 {
3434 return HAL_SD_ERROR_PARAM;
3435 }
3436
3437 /* Send Status command */
3438 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3439 if (errorstate != HAL_SD_ERROR_NONE)
3440 {
3441 return errorstate;
3442 }
3443
3444 /* Get SD card status */
3445 *pCardStatus = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);
3446
3447 return HAL_SD_ERROR_NONE;
3448}
3449
3450/**
3451 * @brief Enables the SDMMC wide bus mode.
3452 * @param hsd: pointer to SD handle
3453 * @retval error state
3454 */
3455static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
3456{
3457 uint32_t scr[2U] = {0UL, 0UL};
3458 uint32_t errorstate;
3459
3460 if ((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3461 {
3462 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3463 }
3464
3465 /* Get SCR Register */
3466 errorstate = SD_FindSCR(hsd, scr);
3467 if (errorstate != HAL_SD_ERROR_NONE)
3468 {
3469 return errorstate;
3470 }
3471
3472 /* If requested card supports wide bus operation */
3473 if ((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
3474 {
3475 /* Send CMD55 APP_CMD with argument as card's RCA.*/
3476 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3477 if (errorstate != HAL_SD_ERROR_NONE)
3478 {
3479 return errorstate;
3480 }
3481
3482 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3483 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
3484 if (errorstate != HAL_SD_ERROR_NONE)
3485 {
3486 return errorstate;
3487 }
3488
3489 return HAL_SD_ERROR_NONE;
3490 }
3491 else
3492 {
3493 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3494 }
3495}
3496
3497/**
3498 * @brief Disables the SDMMC wide bus mode.
3499 * @param hsd: Pointer to SD handle
3500 * @retval error state
3501 */
3502static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3503{
3504 uint32_t scr[2U] = {0UL, 0UL};
3505 uint32_t errorstate;
3506
3507 if ((SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3508 {
3509 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3510 }
3511
3512 /* Get SCR Register */
3513 errorstate = SD_FindSCR(hsd, scr);
3514 if (errorstate != HAL_SD_ERROR_NONE)
3515 {
3516 return errorstate;
3517 }
3518
3519 /* If requested card supports 1 bit mode operation */
3520 if ((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
3521 {
3522 /* Send CMD55 APP_CMD with argument as card's RCA */
3523 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3524 if (errorstate != HAL_SD_ERROR_NONE)
3525 {
3526 return errorstate;
3527 }
3528
3529 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3530 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
3531 if (errorstate != HAL_SD_ERROR_NONE)
3532 {
3533 return errorstate;
3534 }
3535
3536 return HAL_SD_ERROR_NONE;
3537 }
3538 else
3539 {
3540 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3541 }
3542}
3543
3544/**
3545 * @brief Finds the SD card SCR register value.
3546 * @param hsd: Pointer to SD handle
3547 * @param pSCR: pointer to the buffer that will contain the SCR value
3548 * @retval error state
3549 */
3550static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
3551{
3552 SDMMC_DataInitTypeDef config;
3553 uint32_t errorstate;
3554 uint32_t tickstart = HAL_GetTick();
3555 uint32_t index = 0U;
3556 uint32_t tempscr[2U] = {0UL, 0UL};
3557 uint32_t *scr = pSCR;
3558
3559 /* Set Block Size To 8 Bytes */
3560 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
3561 if (errorstate != HAL_SD_ERROR_NONE)
3562 {
3563 return errorstate;
3564 }
3565
3566 /* Send CMD55 APP_CMD with argument as card's RCA */
3567 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
3568 if (errorstate != HAL_SD_ERROR_NONE)
3569 {
3570 return errorstate;
3571 }
3572
3573 config.DataTimeOut = SDMMC_DATATIMEOUT;
3574 config.DataLength = 8U;
3575 config.DataBlockSize = SDMMC_DATABLOCK_SIZE_8B;
3576 config.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3577 config.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3578 config.DPSM = SDMMC_DPSM_ENABLE;
3579 (void)SDMMC_ConfigData(hsd->Instance, &config);
3580
3581 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3582 errorstate = SDMMC_CmdSendSCR(hsd->Instance);
3583 if (errorstate != HAL_SD_ERROR_NONE)
3584 {
3585 return errorstate;
3586 }
3587
3588 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND |
3589 SDMMC_FLAG_DATAEND))
3590 {
3591 if ((!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOE)) && (index == 0U))
3592 {
3593 tempscr[0] = SDMMC_ReadFIFO(hsd->Instance);
3594 tempscr[1] = SDMMC_ReadFIFO(hsd->Instance);
3595 index++;
3596 }
3597
3598 if ((HAL_GetTick() - tickstart) >= SDMMC_SWDATATIMEOUT)
3599 {
3600 return HAL_SD_ERROR_TIMEOUT;
3601 }
3602 }
3603
3604 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3605 {
3606 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3607
3608 return HAL_SD_ERROR_DATA_TIMEOUT;
3609 }
3610 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3611 {
3612 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3613
3614 return HAL_SD_ERROR_DATA_CRC_FAIL;
3615 }
3616 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3617 {
3618 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3619
3620 return HAL_SD_ERROR_RX_OVERRUN;
3621 }
3622 else
3623 {
3624 /* No error flag set */
3625 /* Clear all the static flags */
3626 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3627
3628 *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24U) | ((tempscr[1] & SDMMC_8TO15BITS) << 8U) | \
3629 ((tempscr[1] & SDMMC_16TO23BITS) >> 8U) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24U));
3630 scr++;
3631 *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24U) | ((tempscr[0] & SDMMC_8TO15BITS) << 8U) | \
3632 ((tempscr[0] & SDMMC_16TO23BITS) >> 8U) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24U));
3633
3634 }
3635
3636 return HAL_SD_ERROR_NONE;
3637}
3638
3639/**
3640 * @brief Wrap up reading in non-blocking mode.
3641 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3642 * the configuration information.
3643 * @retval None
3644 */
3645static void SD_Read_IT(SD_HandleTypeDef *hsd)
3646{
3647 uint32_t count;
3648 uint32_t data;
3649 uint8_t *tmp;
3650
3651 tmp = hsd->pRxBuffPtr;
3652
3653 if (hsd->RxXferSize >= SDMMC_FIFO_SIZE)
3654 {
3655 /* Read data from SDMMC Rx FIFO */
3656 for (count = 0U; count < (SDMMC_FIFO_SIZE / 4U); count++)
3657 {
3658 data = SDMMC_ReadFIFO(hsd->Instance);
3659 *tmp = (uint8_t)(data & 0xFFU);
3660 tmp++;
3661 *tmp = (uint8_t)((data >> 8U) & 0xFFU);
3662 tmp++;
3663 *tmp = (uint8_t)((data >> 16U) & 0xFFU);
3664 tmp++;
3665 *tmp = (uint8_t)((data >> 24U) & 0xFFU);
3666 tmp++;
3667 }
3668
3669 hsd->pRxBuffPtr = tmp;
3670 hsd->RxXferSize -= SDMMC_FIFO_SIZE;
3671 }
3672}
3673
3674/**
3675 * @brief Wrap up writing in non-blocking mode.
3676 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3677 * the configuration information.
3678 * @retval None
3679 */
3680static void SD_Write_IT(SD_HandleTypeDef *hsd)
3681{
3682 uint32_t count;
3683 uint32_t data;
3684 const uint8_t *tmp;
3685
3686 tmp = hsd->pTxBuffPtr;
3687
3688 if (hsd->TxXferSize >= SDMMC_FIFO_SIZE)
3689 {
3690 /* Write data to SDMMC Tx FIFO */
3691 for (count = 0U; count < (SDMMC_FIFO_SIZE / 4U); count++)
3692 {
3693 data = (uint32_t)(*tmp);
3694 tmp++;
3695 data |= ((uint32_t)(*tmp) << 8U);
3696 tmp++;
3697 data |= ((uint32_t)(*tmp) << 16U);
3698 tmp++;
3699 data |= ((uint32_t)(*tmp) << 24U);
3700 tmp++;
3701 (void)SDMMC_WriteFIFO(hsd->Instance, &data);
3702 }
3703
3704 hsd->pTxBuffPtr = tmp;
3705 hsd->TxXferSize -= SDMMC_FIFO_SIZE;
3706 }
3707}
3708
3709/**
3710 * @brief Switches the SD card to High Speed mode.
3711 * This API must be used after "Transfer State"
3712 * @note This operation should be followed by the configuration
3713 * of PLL to have SDMMCCK clock between 25 and 50 MHz
3714 * @param hsd: SD handle
3715 * @param SwitchSpeedMode: SD speed mode( SDMMC_SDR12_SWITCH_PATTERN, SDMMC_SDR25_SWITCH_PATTERN)
3716 * @retval SD Card error state
3717 */
3718uint32_t SD_SwitchSpeed(SD_HandleTypeDef *hsd, uint32_t SwitchSpeedMode)
3719{
3720 uint32_t errorstate = HAL_SD_ERROR_NONE;
3721 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
3722 uint32_t SD_hs[16] = {0};
3723 uint32_t count;
3724 uint32_t loop = 0 ;
3725 uint32_t Timeout = HAL_GetTick();
3726
3727 if (hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
3728 {
3729 /* Standard Speed Card <= 12.5Mhz */
3730 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3731 }
3732
3733 if (hsd->SdCard.CardSpeed >= CARD_HIGH_SPEED)
3734 {
3735 /* Initialize the Data control register */
3736 hsd->Instance->DCTRL = 0;
3737 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
3738
3739 if (errorstate != HAL_SD_ERROR_NONE)
3740 {
3741 return errorstate;
3742 }
3743
3744 /* Configure the SD DPSM (Data Path State Machine) */
3745 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
3746 sdmmc_datainitstructure.DataLength = 64U;
3747 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
3748 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3749 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3750 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
3751
3752 (void)SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure);
3753
3754 errorstate = SDMMC_CmdSwitch(hsd->Instance, SwitchSpeedMode);
3755 if (errorstate != HAL_SD_ERROR_NONE)
3756 {
3757 return errorstate;
3758 }
3759
3760 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND |
3761 SDMMC_FLAG_DATAEND))
3762 {
3763 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3764 {
3765 for (count = 0U; count < 8U; count++)
3766 {
3767 SD_hs[(8U * loop) + count] = SDMMC_ReadFIFO(hsd->Instance);
3768 }
3769 loop ++;
3770 }
3771 if ((HAL_GetTick() - Timeout) >= SDMMC_SWDATATIMEOUT)
3772 {
3773 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
3774 hsd->State = HAL_SD_STATE_READY;
3775 return HAL_SD_ERROR_TIMEOUT;
3776 }
3777 }
3778
3779 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3780 {
3781 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3782
3783 return errorstate;
3784 }
3785 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3786 {
3787 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3788
3789 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
3790
3791 return errorstate;
3792 }
3793 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3794 {
3795 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3796
3797 errorstate = SDMMC_ERROR_RX_OVERRUN;
3798
3799 return errorstate;
3800 }
3801 else
3802 {
3803 /* No error flag set */
3804 }
3805
3806 /* Clear all the static flags */
3807 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3808
3809 /* Test if the switch mode HS is ok */
3810 if ((((uint8_t *)SD_hs)[13] & 2U) != 2U)
3811 {
3812 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
3813 }
3814
3815 }
3816
3817 return errorstate;
3818}
3819
3820#if (USE_SD_TRANSCEIVER != 0U)
3821/**
3822 * @brief Switches the SD card to Ultra High Speed mode.
3823 * This API must be used after "Transfer State"
3824 * @note This operation should be followed by the configuration
3825 * of PLL to have SDMMCCK clock between 50 and 120 MHz
3826 * @param hsd: SD handle
3827 * @param UltraHighSpeedMode: SD speed mode( SDMMC_SDR50_SWITCH_PATTERN, SDMMC_SDR104_SWITCH_PATTERN)
3828 * @retval SD Card error state
3829 */
3830static uint32_t SD_UltraHighSpeed(SD_HandleTypeDef *hsd, uint32_t UltraHighSpeedMode)
3831{
3832 uint32_t errorstate = HAL_SD_ERROR_NONE;
3833 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
3834 uint32_t SD_hs[16] = {0};
3835 uint32_t count;
3836 uint32_t loop = 0 ;
3837 uint32_t Timeout = HAL_GetTick();
3838
3839 if (hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
3840 {
3841 /* Standard Speed Card <= 12.5Mhz */
3842 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3843 }
3844
3845 if (hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
3846 {
3847 /* Initialize the Data control register */
3848 hsd->Instance->DCTRL = 0;
3849 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
3850
3851 if (errorstate != HAL_SD_ERROR_NONE)
3852 {
3853 return errorstate;
3854 }
3855
3856 /* Configure the SD DPSM (Data Path State Machine) */
3857 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
3858 sdmmc_datainitstructure.DataLength = 64U;
3859 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
3860 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3861 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3862 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
3863
3864 if (SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK)
3865 {
3866 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3867 }
3868
3869 errorstate = SDMMC_CmdSwitch(hsd->Instance, UltraHighSpeedMode);
3870 if (errorstate != HAL_SD_ERROR_NONE)
3871 {
3872 return errorstate;
3873 }
3874
3875 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND |
3876 SDMMC_FLAG_DATAEND))
3877 {
3878 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
3879 {
3880 for (count = 0U; count < 8U; count++)
3881 {
3882 SD_hs[(8U * loop) + count] = SDMMC_ReadFIFO(hsd->Instance);
3883 }
3884 loop ++;
3885 }
3886
3887 if ((HAL_GetTick() - Timeout) >= SDMMC_SWDATATIMEOUT)
3888 {
3889 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
3890 hsd->State = HAL_SD_STATE_READY;
3891 return HAL_SD_ERROR_TIMEOUT;
3892 }
3893 }
3894
3895 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
3896 {
3897 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
3898
3899 return errorstate;
3900 }
3901 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
3902 {
3903 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
3904
3905 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
3906
3907 return errorstate;
3908 }
3909 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
3910 {
3911 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
3912
3913 errorstate = SDMMC_ERROR_RX_OVERRUN;
3914
3915 return errorstate;
3916 }
3917 else
3918 {
3919 /* No error flag set */
3920 }
3921
3922 /* Clear all the static flags */
3923 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
3924
3925 /* Test if the switch mode HS is ok */
3926 if ((((uint8_t *)SD_hs)[13] & 2U) != 2U)
3927 {
3928 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
3929 }
3930 else
3931 {
3932#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
3933 hsd->DriveTransceiver_1_8V_Callback(SET);
3934#else
3935 HAL_SD_DriveTransceiver_1_8V_Callback(SET);
3936#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
3937#if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
3938 /* Enable DelayBlock Peripheral */
3939 /* SDMMC_FB_CLK tuned feedback clock selected as receive clock, for SDR104 */
3941 if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
3942 {
3943 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3944 }
3945#endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
3946 }
3947 }
3948
3949 return errorstate;
3950}
3951
3952/**
3953 * @brief Switches the SD card to Double Data Rate (DDR) mode.
3954 * This API must be used after "Transfer State"
3955 * @note This operation should be followed by the configuration
3956 * of PLL to have SDMMCCK clock less than 50MHz
3957 * @param hsd: SD handle
3958 * @retval SD Card error state
3959 */
3960static uint32_t SD_DDR_Mode(SD_HandleTypeDef *hsd)
3961{
3962 uint32_t errorstate = HAL_SD_ERROR_NONE;
3963 SDMMC_DataInitTypeDef sdmmc_datainitstructure;
3964 uint32_t SD_hs[16] = {0};
3965 uint32_t count;
3966 uint32_t loop = 0 ;
3967 uint32_t Timeout = HAL_GetTick();
3968
3969 if (hsd->SdCard.CardSpeed == CARD_NORMAL_SPEED)
3970 {
3971 /* Standard Speed Card <= 12.5Mhz */
3972 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3973 }
3974
3975 if (hsd->SdCard.CardSpeed == CARD_ULTRA_HIGH_SPEED)
3976 {
3977 /* Initialize the Data control register */
3978 hsd->Instance->DCTRL = 0;
3979 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
3980
3981 if (errorstate != HAL_SD_ERROR_NONE)
3982 {
3983 return errorstate;
3984 }
3985
3986 /* Configure the SD DPSM (Data Path State Machine) */
3987 sdmmc_datainitstructure.DataTimeOut = SDMMC_DATATIMEOUT;
3988 sdmmc_datainitstructure.DataLength = 64U;
3989 sdmmc_datainitstructure.DataBlockSize = SDMMC_DATABLOCK_SIZE_64B ;
3990 sdmmc_datainitstructure.TransferDir = SDMMC_TRANSFER_DIR_TO_SDMMC;
3991 sdmmc_datainitstructure.TransferMode = SDMMC_TRANSFER_MODE_BLOCK;
3992 sdmmc_datainitstructure.DPSM = SDMMC_DPSM_ENABLE;
3993
3994 if (SDMMC_ConfigData(hsd->Instance, &sdmmc_datainitstructure) != HAL_OK)
3995 {
3996 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
3997 }
3998
3999 errorstate = SDMMC_CmdSwitch(hsd->Instance, SDMMC_DDR50_SWITCH_PATTERN);
4000 if (errorstate != HAL_SD_ERROR_NONE)
4001 {
4002 return errorstate;
4003 }
4004
4005 while (!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DBCKEND |
4006 SDMMC_FLAG_DATAEND))
4007 {
4008 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXFIFOHF))
4009 {
4010 for (count = 0U; count < 8U; count++)
4011 {
4012 SD_hs[(8U * loop) + count] = SDMMC_ReadFIFO(hsd->Instance);
4013 }
4014 loop ++;
4015 }
4016
4017 if ((HAL_GetTick() - Timeout) >= SDMMC_SWDATATIMEOUT)
4018 {
4019 hsd->ErrorCode = HAL_SD_ERROR_TIMEOUT;
4020 hsd->State = HAL_SD_STATE_READY;
4021 return HAL_SD_ERROR_TIMEOUT;
4022 }
4023 }
4024
4025 if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DTIMEOUT))
4026 {
4027 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DTIMEOUT);
4028
4029 return errorstate;
4030 }
4031 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_DCRCFAIL))
4032 {
4033 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_DCRCFAIL);
4034
4035 errorstate = SDMMC_ERROR_DATA_CRC_FAIL;
4036
4037 return errorstate;
4038 }
4039 else if (__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_RXOVERR))
4040 {
4041 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_FLAG_RXOVERR);
4042
4043 errorstate = SDMMC_ERROR_RX_OVERRUN;
4044
4045 return errorstate;
4046 }
4047 else
4048 {
4049 /* No error flag set */
4050 }
4051
4052 /* Clear all the static flags */
4053 __HAL_SD_CLEAR_FLAG(hsd, SDMMC_STATIC_DATA_FLAGS);
4054
4055 /* Test if the switch mode is ok */
4056 if ((((uint8_t *)SD_hs)[13] & 2U) != 2U)
4057 {
4058 errorstate = SDMMC_ERROR_UNSUPPORTED_FEATURE;
4059 }
4060 else
4061 {
4062#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
4063 hsd->DriveTransceiver_1_8V_Callback(SET);
4064#else
4065 HAL_SD_DriveTransceiver_1_8V_Callback(SET);
4066#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
4067#if defined (DLYB_SDMMC1) || defined (DLYB_SDMMC2)
4068 /* Enable DelayBlock Peripheral */
4069 /* SDMMC_CKin feedback clock selected as receive clock, for DDR50 */
4071 if (DelayBlock_Enable(SD_GET_DLYB_INSTANCE(hsd->Instance)) != HAL_OK)
4072 {
4073 return (HAL_SD_ERROR_GENERAL_UNKNOWN_ERR);
4074 }
4075#endif /* (DLYB_SDMMC1) || (DLYB_SDMMC2) */
4076 }
4077 }
4078
4079 return errorstate;
4080}
4081
4082#endif /* USE_SD_TRANSCEIVER */
4083
4084/**
4085 * @brief Read DMA Buffer 0 Transfer completed callbacks
4086 * @param hsd: SD handle
4087 * @retval None
4088 */
4089__weak void HAL_SDEx_Read_DMADoubleBuf0CpltCallback(SD_HandleTypeDef *hsd)
4090{
4091 /* Prevent unused argument(s) compilation warning */
4092 UNUSED(hsd);
4093
4094 /* NOTE : This function should not be modified, when the callback is needed,
4095 the HAL_SDEx_Read_DMADoubleBuf0CpltCallback can be implemented in the user file
4096 */
4097}
4098
4099/**
4100 * @brief Read DMA Buffer 1 Transfer completed callbacks
4101 * @param hsd: SD handle
4102 * @retval None
4103 */
4104__weak void HAL_SDEx_Read_DMADoubleBuf1CpltCallback(SD_HandleTypeDef *hsd)
4105{
4106 /* Prevent unused argument(s) compilation warning */
4107 UNUSED(hsd);
4108
4109 /* NOTE : This function should not be modified, when the callback is needed,
4110 the HAL_SDEx_Read_DMADoubleBuf1CpltCallback can be implemented in the user file
4111 */
4112}
4113
4114/**
4115 * @brief Write DMA Buffer 0 Transfer completed callbacks
4116 * @param hsd: SD handle
4117 * @retval None
4118 */
4119__weak void HAL_SDEx_Write_DMADoubleBuf0CpltCallback(SD_HandleTypeDef *hsd)
4120{
4121 /* Prevent unused argument(s) compilation warning */
4122 UNUSED(hsd);
4123
4124 /* NOTE : This function should not be modified, when the callback is needed,
4125 the HAL_SDEx_Write_DMADoubleBuf0CpltCallback can be implemented in the user file
4126 */
4127}
4128
4129/**
4130 * @brief Write DMA Buffer 1 Transfer completed callbacks
4131 * @param hsd: SD handle
4132 * @retval None
4133 */
4134__weak void HAL_SDEx_Write_DMADoubleBuf1CpltCallback(SD_HandleTypeDef *hsd)
4135{
4136 /* Prevent unused argument(s) compilation warning */
4137 UNUSED(hsd);
4138
4139 /* NOTE : This function should not be modified, when the callback is needed,
4140 the HAL_SDEx_Write_DMADoubleBuf1CpltCallback can be implemented in the user file
4141 */
4142}
4143
4144/**
4145 * @}
4146 */
4147
4148#endif /* HAL_SD_MODULE_ENABLED */
4149#endif /* SDMMC1 || SDMMC2 */
4150
4151/**
4152 * @}
4153 */
4154
4155/**
4156 * @}
4157 */
#define __IO
Defines 'read / write' permissions.
void loop()
Definition main.cpp:621
#define MODIFY_REG(REG, CLEARMASK, SETMASK)
Definition stm32h7xx.h:219
#define READ_BIT(REG, BIT)
Definition stm32h7xx.h:211
#define IS_SDMMC_ALL_INSTANCE(_INSTANCE_)
FlagStatus
Definition stm32h7xx.h:181
@ RESET
Definition stm32h7xx.h:182
@ SET
Definition stm32h7xx.h:183
HAL_StatusTypeDef DelayBlock_Enable(DLYB_TypeDef *DLYBx)
void HAL_Delay(uint32_t Delay)
This function provides minimum delay (in milliseconds) based on variable incremented.
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
#define SDMMC_CLKCR_DDR
Data rate signaling selection.
#define SDMMC_CLKCR_SELCLKRX
SELCLKRX[1:0] bits (Receive clock selection).
#define SDMMC_CLKCR_SELCLKRX_0
0x00100000
#define SDMMC_POWER_VSWITCHEN
Voltage switch procedure enable.
#define SDMMC_POWER_DIRPOL
Data and Command direction signals polarity selection.
#define SDMMC_POWER_VSWITCH
Voltage switch sequence start.
#define SDMMC_CLKCR_BUSSPEED
Bus speed mode selection.
#define SDMMC_DCTRL_DTDIR
Data transfer direction selection.
#define SDMMC_IDMA_IDMABACT
Uses buffer 1 when double buffer mode is selected.
#define SDMMC_CLKCR_SELCLKRX_1
0x00200000
#define SDMMC_CMD_CMDSTOP
CPSM Treats command as a Stop.
#define SDMMC_DCTRL_FIFORST
FIFO reset.
uint32_t HAL_RCCEx_GetPeriphCLKFreq(uint64_t PeriphClk)
#define RCC_PERIPHCLK_SDMMC
#define NULL
Definition usbd_def.h:44
static const idle_config_t config
Definition idle_task.c:17
size_type count(const key_type &key) const
Definition json.hpp:19264
This file contains all the functions prototypes for the HAL module driver.
#define assert_param(expr)
Uncomment the line below to expanse the "assert_param" macro in the HAL drivers code.
HAL_StatusTypeDef
HAL Status structures definition.
@ HAL_TIMEOUT
@ HAL_ERROR
@ HAL_OK
@ HAL_BUSY
#define __HAL_UNLOCK(__HANDLE__)
#define UNUSED(x)
@ HAL_UNLOCKED
#define __HAL_LOCK(__HANDLE__)