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