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