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