cd27bd7418747c8ab303408de7c480e7430d624f
[SCSI2SD-V6.git] / STM32CubeMX / 2020c / Drivers / STM32F2xx_HAL_Driver / Src / stm32f2xx_hal_sd.c
1 /**
2 ******************************************************************************
3 * @file stm32f2xx_hal_sd.c
4 * @author MCD Application Team
5 * @brief SD card HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Secure Digital (SD) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral Control functions
11 * + Peripheral State functions
12 *
13 @verbatim
14 ==============================================================================
15 ##### How to use this driver #####
16 ==============================================================================
17 [..]
18 This driver implements a high level communication layer for read and write from/to
19 this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by
20 the user in HAL_SD_MspInit() function (MSP layer).
21 Basically, the MSP layer configuration should be the same as we provide in the
22 examples.
23 You can easily tailor this configuration according to hardware resources.
24
25 [..]
26 This driver is a generic layered driver for SDIO memories which uses the HAL
27 SDIO driver functions to interface with SD and uSD cards devices.
28 It is used as follows:
29
30 (#)Initialize the SDIO low level resources by implementing the HAL_SD_MspInit() API:
31 (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE();
32 (##) SDIO pins configuration for SD card
33 (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
34 (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init()
35 and according to your pin assignment;
36 (##) DMA configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
37 and HAL_SD_WriteBlocks_DMA() APIs).
38 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
39 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
40 (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
41 (+++) Configure the SDIO and DMA interrupt priorities using functions
42 HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority
43 (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ()
44 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
45 and __HAL_SD_DISABLE_IT() inside the communication process.
46 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
47 and __HAL_SD_CLEAR_IT()
48 (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
49 and HAL_SD_WriteBlocks_IT() APIs).
50 (+++) Configure the SDIO interrupt priorities using function HAL_NVIC_SetPriority();
51 (+++) Enable the NVIC SDIO IRQs using function HAL_NVIC_EnableIRQ()
52 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
53 and __HAL_SD_DISABLE_IT() inside the communication process.
54 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
55 and __HAL_SD_CLEAR_IT()
56 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
57
58
59 *** SD Card Initialization and configuration ***
60 ================================================
61 [..]
62 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
63 SDIO Peripheral(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
64 This function provide the following operations:
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 (SDIO_CK) is computed as follows:
70
71 SDIO_CK = SDIOCLK / (ClockDiv + 2)
72
73 In initialization mode and according to the SD Card standard,
74 make sure that the SDIO_CK frequency doesn't exceed 400KHz.
75
76 This phase of initialization is done through SDIO_Init() and
77 SDIO_PowerState_ON() SDIO 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 SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
91 To be able to use a frequency higher than 24MHz, you should use the SDIO
92 peripheral in bypass mode. Refer to the corresponding reference manual
93 for more details.
94
95 (#) Select the corresponding SD Card according to the address read with the step 2.
96
97 (#) Configure the SD Card in wide bus mode: 4-bits data.
98
99 *** SD Card Read operation ***
100 ==============================
101 [..]
102 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
103 This function support only 512-bytes block length (the block size should be
104 chosen as 512 bytes).
105 You can choose either one block read operation or multiple block read operation
106 by adjusting the "NumberOfBlocks" parameter.
107 After this, you have to ensure that the transfer is done correctly. The check is done
108 through HAL_SD_GetCardState() function for SD card state.
109
110 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
111 This function support only 512-bytes block length (the block size should be
112 chosen as 512 bytes).
113 You can choose either one block read operation or multiple block read operation
114 by adjusting the "NumberOfBlocks" parameter.
115 After this, you have to ensure that the transfer is done correctly. The check is done
116 through HAL_SD_GetCardState() function for SD card state.
117 You could also check the DMA transfer process through the SD Rx interrupt event.
118
119 (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
120 This function support only 512-bytes block length (the block size should be
121 chosen as 512 bytes).
122 You can choose either one block read operation or multiple block read operation
123 by adjusting the "NumberOfBlocks" parameter.
124 After this, you have to ensure that the transfer is done correctly. The check is done
125 through HAL_SD_GetCardState() function for SD card state.
126 You could also check the IT transfer process through the SD Rx interrupt event.
127
128 *** SD Card Write operation ***
129 ===============================
130 [..]
131 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
132 This function support only 512-bytes block length (the block size should be
133 chosen as 512 bytes).
134 You can choose either one block read operation or multiple block read operation
135 by adjusting the "NumberOfBlocks" parameter.
136 After this, you have to ensure that the transfer is done correctly. The check is done
137 through HAL_SD_GetCardState() function for SD card state.
138
139 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
140 This function support only 512-bytes block length (the block size should be
141 chosen as 512 bytes).
142 You can choose either one block read operation or multiple block read operation
143 by adjusting the "NumberOfBlocks" parameter.
144 After this, you have to ensure that the transfer is done correctly. The check is done
145 through HAL_SD_GetCardState() function for SD card state.
146 You could also check the DMA transfer process through the SD Tx interrupt event.
147
148 (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
149 This function support only 512-bytes block length (the block size should be
150 chosen as 512 bytes).
151 You can choose either one block read operation or multiple block read operation
152 by adjusting the "NumberOfBlocks" parameter.
153 After this, you have to ensure that the transfer is done correctly. The check is done
154 through HAL_SD_GetCardState() function for SD card state.
155 You could also check the IT transfer process through the SD Tx interrupt event.
156
157 *** SD card status ***
158 ======================
159 [..]
160 (+) The SD Status contains status bits that are related to the SD Memory
161 Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
162
163 *** SD card information ***
164 ===========================
165 [..]
166 (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
167 It returns useful information about the SD card such as block size, card type,
168 block number ...
169
170 *** SD card CSD register ***
171 ============================
172 (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
173 Some of the CSD parameters are useful for card initialization and identification.
174
175 *** SD card CID register ***
176 ============================
177 (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
178 Some of the CSD parameters are useful for card initialization and identification.
179
180 *** SD HAL driver macros list ***
181 ==================================
182 [..]
183 Below the list of most used macros in SD HAL driver.
184
185 (+) __HAL_SD_ENABLE : Enable the SD device
186 (+) __HAL_SD_DISABLE : Disable the SD device
187 (+) __HAL_SD_DMA_ENABLE: Enable the SDIO DMA transfer
188 (+) __HAL_SD_DMA_DISABLE: Disable the SDIO DMA transfer
189 (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
190 (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
191 (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
192 (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
193
194 (@) You can refer to the SD HAL driver header file for more useful macros
195
196 *** Callback registration ***
197 =============================================
198 [..]
199 The compilation define USE_HAL_SD_REGISTER_CALLBACKS when set to 1
200 allows the user to configure dynamically the driver callbacks.
201
202 Use Functions @ref HAL_SD_RegisterCallback() to register a user callback,
203 it allows to register following callbacks:
204 (+) TxCpltCallback : callback when a transmission transfer is completed.
205 (+) RxCpltCallback : callback when a reception transfer is completed.
206 (+) ErrorCallback : callback when error occurs.
207 (+) AbortCpltCallback : callback when abort is completed.
208 (+) MspInitCallback : SD MspInit.
209 (+) MspDeInitCallback : SD MspDeInit.
210 This function takes as parameters the HAL peripheral handle, the Callback ID
211 and a pointer to the user callback function.
212
213 Use function @ref HAL_SD_UnRegisterCallback() to reset a callback to the default
214 weak (surcharged) function. It allows to reset following callbacks:
215 (+) TxCpltCallback : callback when a transmission transfer is completed.
216 (+) RxCpltCallback : callback when a reception transfer is completed.
217 (+) ErrorCallback : callback when error occurs.
218 (+) AbortCpltCallback : callback when abort is completed.
219 (+) MspInitCallback : SD MspInit.
220 (+) MspDeInitCallback : SD MspDeInit.
221 This function) takes as parameters the HAL peripheral handle and the Callback ID.
222
223 By default, after the @ref HAL_SD_Init and if the state is HAL_SD_STATE_RESET
224 all callbacks are reset to the corresponding legacy weak (surcharged) functions.
225 Exception done for MspInit and MspDeInit callbacks that are respectively
226 reset to the legacy weak (surcharged) functions in the @ref HAL_SD_Init
227 and @ref HAL_SD_DeInit only when these callbacks are null (not registered beforehand).
228 If not, MspInit or MspDeInit are not null, the @ref HAL_SD_Init and @ref HAL_SD_DeInit
229 keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
230
231 Callbacks can be registered/unregistered in READY state only.
232 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
233 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
234 during the Init/DeInit.
235 In that case first register the MspInit/MspDeInit user callbacks
236 using @ref HAL_SD_RegisterCallback before calling @ref HAL_SD_DeInit
237 or @ref HAL_SD_Init function.
238
239 When The compilation define USE_HAL_SD_REGISTER_CALLBACKS is set to 0 or
240 not defined, the callback registering feature is not available
241 and weak (surcharged) callbacks are used.
242
243 @endverbatim
244 ******************************************************************************
245 * @attention
246 *
247 * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
248 * All rights reserved.</center></h2>
249 *
250 * This software component is licensed by ST under BSD 3-Clause license,
251 * the "License"; You may not use this file except in compliance with the
252 * License. You may obtain a copy of the License at:
253 * opensource.org/licenses/BSD-3-Clause
254 *
255 ******************************************************************************
256 */
257
258 /* Includes ------------------------------------------------------------------*/
259 #include "stm32f2xx_hal.h"
260
261 #if defined(SDIO)
262
263 /** @addtogroup STM32F2xx_HAL_Driver
264 * @{
265 */
266
267 /** @addtogroup SD
268 * @{
269 */
270
271 #ifdef HAL_SD_MODULE_ENABLED
272
273 /* Private typedef -----------------------------------------------------------*/
274 /* Private define ------------------------------------------------------------*/
275 /** @addtogroup SD_Private_Defines
276 * @{
277 */
278
279 /**
280 * @}
281 */
282
283 /* Private macro -------------------------------------------------------------*/
284 /* Private variables ---------------------------------------------------------*/
285 /* Private function prototypes -----------------------------------------------*/
286 /* Private functions ---------------------------------------------------------*/
287 /** @defgroup SD_Private_Functions SD Private Functions
288 * @{
289 */
290 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd);
291 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd);
292 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
293 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
294 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd);
295 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
296 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
297 static void SD_PowerOFF(SD_HandleTypeDef *hsd);
298 static void SD_Write_IT(SD_HandleTypeDef *hsd);
299 static void SD_Read_IT(SD_HandleTypeDef *hsd);
300 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
301 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
302 static void SD_DMAError(DMA_HandleTypeDef *hdma);
303 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma);
304 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma);
305 /**
306 * @}
307 */
308
309 /* Exported functions --------------------------------------------------------*/
310 /** @addtogroup SD_Exported_Functions
311 * @{
312 */
313
314 /** @addtogroup SD_Exported_Functions_Group1
315 * @brief Initialization and de-initialization functions
316 *
317 @verbatim
318 ==============================================================================
319 ##### Initialization and de-initialization functions #####
320 ==============================================================================
321 [..]
322 This section provides functions allowing to initialize/de-initialize the SD
323 card device to be ready for use.
324
325 @endverbatim
326 * @{
327 */
328
329 /**
330 * @brief Initializes the SD according to the specified parameters in the
331 SD_HandleTypeDef and create the associated handle.
332 * @param hsd: Pointer to the SD handle
333 * @retval HAL status
334 */
335 HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
336 {
337 /* Check the SD handle allocation */
338 if(hsd == NULL)
339 {
340 return HAL_ERROR;
341 }
342
343 /* Check the parameters */
344 assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance));
345 assert_param(IS_SDIO_CLOCK_EDGE(hsd->Init.ClockEdge));
346 assert_param(IS_SDIO_CLOCK_BYPASS(hsd->Init.ClockBypass));
347 assert_param(IS_SDIO_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
348 assert_param(IS_SDIO_BUS_WIDE(hsd->Init.BusWide));
349 assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
350 assert_param(IS_SDIO_CLKDIV(hsd->Init.ClockDiv));
351
352 if(hsd->State == HAL_SD_STATE_RESET)
353 {
354 /* Allocate lock resource and initialize it */
355 hsd->Lock = HAL_UNLOCKED;
356 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
357 /* Reset Callback pointers in HAL_SD_STATE_RESET only */
358 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
359 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
360 hsd->ErrorCallback = HAL_SD_ErrorCallback;
361 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
362
363 if(hsd->MspInitCallback == NULL)
364 {
365 hsd->MspInitCallback = HAL_SD_MspInit;
366 }
367
368 /* Init the low level hardware */
369 hsd->MspInitCallback(hsd);
370 #else
371 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
372 HAL_SD_MspInit(hsd);
373 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
374 }
375
376 hsd->State = HAL_SD_STATE_BUSY;
377
378 /* Initialize the Card parameters */
379 if (HAL_SD_InitCard(hsd) != HAL_OK)
380 {
381 return HAL_ERROR;
382 }
383
384 /* Initialize the error code */
385 hsd->ErrorCode = HAL_SD_ERROR_NONE;
386
387 /* Initialize the SD operation */
388 hsd->Context = SD_CONTEXT_NONE;
389
390 /* Initialize the SD state */
391 hsd->State = HAL_SD_STATE_READY;
392
393 return HAL_OK;
394 }
395
396 /**
397 * @brief Initializes the SD Card.
398 * @param hsd: Pointer to SD handle
399 * @note This function initializes the SD card. It could be used when a card
400 re-initialization is needed.
401 * @retval HAL status
402 */
403 HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
404 {
405 uint32_t errorstate;
406 HAL_StatusTypeDef status;
407 SD_InitTypeDef Init;
408
409 /* Default SDIO peripheral configuration for SD card initialization */
410 Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
411 Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
412 Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
413 Init.BusWide = SDIO_BUS_WIDE_1B;
414 Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
415 Init.ClockDiv = SDIO_INIT_CLK_DIV;
416
417 /* Initialize SDIO peripheral interface with default configuration */
418 status = SDIO_Init(hsd->Instance, Init);
419 if(status != HAL_OK)
420 {
421 return HAL_ERROR;
422 }
423
424 /* Disable SDIO Clock */
425 __HAL_SD_DISABLE(hsd);
426
427 /* Set Power State to ON */
428 (void)SDIO_PowerState_ON(hsd->Instance);
429
430 /* Enable SDIO Clock */
431 __HAL_SD_ENABLE(hsd);
432
433 /* 1ms: required power up waiting time before starting the SD initialization
434 sequence */
435 HAL_Delay(1);
436
437 /* Identify card operating voltage */
438 errorstate = SD_PowerON(hsd);
439 if(errorstate != HAL_SD_ERROR_NONE)
440 {
441 hsd->State = HAL_SD_STATE_READY;
442 hsd->ErrorCode |= errorstate;
443 return HAL_ERROR;
444 }
445
446 /* Card initialization */
447 errorstate = SD_InitCard(hsd);
448 if(errorstate != HAL_SD_ERROR_NONE)
449 {
450 hsd->State = HAL_SD_STATE_READY;
451 hsd->ErrorCode |= errorstate;
452 return HAL_ERROR;
453 }
454
455 return HAL_OK;
456 }
457
458 /**
459 * @brief De-Initializes the SD card.
460 * @param hsd: Pointer to SD handle
461 * @retval HAL status
462 */
463 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
464 {
465 /* Check the SD handle allocation */
466 if(hsd == NULL)
467 {
468 return HAL_ERROR;
469 }
470
471 /* Check the parameters */
472 assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance));
473
474 hsd->State = HAL_SD_STATE_BUSY;
475
476 /* Set SD power state to off */
477 SD_PowerOFF(hsd);
478
479 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
480 if(hsd->MspDeInitCallback == NULL)
481 {
482 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
483 }
484
485 /* DeInit the low level hardware */
486 hsd->MspDeInitCallback(hsd);
487 #else
488 /* De-Initialize the MSP layer */
489 HAL_SD_MspDeInit(hsd);
490 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
491
492 hsd->ErrorCode = HAL_SD_ERROR_NONE;
493 hsd->State = HAL_SD_STATE_RESET;
494
495 return HAL_OK;
496 }
497
498
499 /**
500 * @brief Initializes the SD MSP.
501 * @param hsd: Pointer to SD handle
502 * @retval None
503 */
504 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
505 {
506 /* Prevent unused argument(s) compilation warning */
507 UNUSED(hsd);
508
509 /* NOTE : This function should not be modified, when the callback is needed,
510 the HAL_SD_MspInit could be implemented in the user file
511 */
512 }
513
514 /**
515 * @brief De-Initialize SD MSP.
516 * @param hsd: Pointer to SD handle
517 * @retval None
518 */
519 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
520 {
521 /* Prevent unused argument(s) compilation warning */
522 UNUSED(hsd);
523
524 /* NOTE : This function should not be modified, when the callback is needed,
525 the HAL_SD_MspDeInit could be implemented in the user file
526 */
527 }
528
529 /**
530 * @}
531 */
532
533 /** @addtogroup SD_Exported_Functions_Group2
534 * @brief Data transfer functions
535 *
536 @verbatim
537 ==============================================================================
538 ##### IO operation functions #####
539 ==============================================================================
540 [..]
541 This subsection provides a set of functions allowing to manage the data
542 transfer from/to SD card.
543
544 @endverbatim
545 * @{
546 */
547
548 /**
549 * @brief Reads block(s) from a specified address in a card. The Data transfer
550 * is managed by polling mode.
551 * @note This API should be followed by a check on the card state through
552 * HAL_SD_GetCardState().
553 * @param hsd: Pointer to SD handle
554 * @param pData: pointer to the buffer that will contain the received data
555 * @param BlockAdd: Block Address from where data is to be read
556 * @param NumberOfBlocks: Number of SD blocks to read
557 * @param Timeout: Specify timeout value
558 * @retval HAL status
559 */
560 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
561 {
562 SDIO_DataInitTypeDef config;
563 uint32_t errorstate;
564 uint32_t tickstart = HAL_GetTick();
565 uint32_t count, data, dataremaining;
566 uint32_t add = BlockAdd;
567 uint8_t *tempbuff = pData;
568
569 if(NULL == pData)
570 {
571 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
572 return HAL_ERROR;
573 }
574
575 if(hsd->State == HAL_SD_STATE_READY)
576 {
577 hsd->ErrorCode = HAL_SD_ERROR_NONE;
578
579 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
580 {
581 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
582 return HAL_ERROR;
583 }
584
585 hsd->State = HAL_SD_STATE_BUSY;
586
587 /* Initialize data control register */
588 hsd->Instance->DCTRL = 0U;
589
590 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
591 {
592 add *= 512U;
593 }
594
595 /* Set Block Size for Card */
596 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
597 if(errorstate != HAL_SD_ERROR_NONE)
598 {
599 /* Clear all the static flags */
600 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
601 hsd->ErrorCode |= errorstate;
602 hsd->State = HAL_SD_STATE_READY;
603 return HAL_ERROR;
604 }
605
606 /* Configure the SD DPSM (Data Path State Machine) */
607 config.DataTimeOut = SDMMC_DATATIMEOUT;
608 config.DataLength = NumberOfBlocks * BLOCKSIZE;
609 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
610 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
611 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
612 config.DPSM = SDIO_DPSM_ENABLE;
613 (void)SDIO_ConfigData(hsd->Instance, &config);
614
615 /* Read block(s) in polling mode */
616 if(NumberOfBlocks > 1U)
617 {
618 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
619
620 /* Read Multi Block command */
621 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
622 }
623 else
624 {
625 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
626
627 /* Read Single Block command */
628 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
629 }
630 if(errorstate != HAL_SD_ERROR_NONE)
631 {
632 /* Clear all the static flags */
633 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
634 hsd->ErrorCode |= errorstate;
635 hsd->State = HAL_SD_STATE_READY;
636 hsd->Context = SD_CONTEXT_NONE;
637 return HAL_ERROR;
638 }
639
640 /* Poll on SDIO flags */
641 dataremaining = config.DataLength;
642 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
643 {
644 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) && (dataremaining > 0U))
645 {
646 /* Read data from SDIO Rx FIFO */
647 for(count = 0U; count < 8U; count++)
648 {
649 data = SDIO_ReadFIFO(hsd->Instance);
650 *tempbuff = (uint8_t)(data & 0xFFU);
651 tempbuff++;
652 dataremaining--;
653 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
654 tempbuff++;
655 dataremaining--;
656 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
657 tempbuff++;
658 dataremaining--;
659 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
660 tempbuff++;
661 dataremaining--;
662 }
663 }
664
665 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
666 {
667 /* Clear all the static flags */
668 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
669 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
670 hsd->State= HAL_SD_STATE_READY;
671 hsd->Context = SD_CONTEXT_NONE;
672 return HAL_TIMEOUT;
673 }
674 }
675
676 /* Send stop transmission command in case of multiblock read */
677 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
678 {
679 if(hsd->SdCard.CardType != CARD_SECURED)
680 {
681 /* Send stop transmission command */
682 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
683 if(errorstate != HAL_SD_ERROR_NONE)
684 {
685 /* Clear all the static flags */
686 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
687 hsd->ErrorCode |= errorstate;
688 hsd->State = HAL_SD_STATE_READY;
689 hsd->Context = SD_CONTEXT_NONE;
690 return HAL_ERROR;
691 }
692 }
693 }
694
695 /* Get error state */
696 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
697 {
698 /* Clear all the static flags */
699 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
700 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
701 hsd->State = HAL_SD_STATE_READY;
702 hsd->Context = SD_CONTEXT_NONE;
703 return HAL_ERROR;
704 }
705 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
706 {
707 /* Clear all the static flags */
708 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
709 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
710 hsd->State = HAL_SD_STATE_READY;
711 hsd->Context = SD_CONTEXT_NONE;
712 return HAL_ERROR;
713 }
714 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
715 {
716 /* Clear all the static flags */
717 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
718 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
719 hsd->State = HAL_SD_STATE_READY;
720 hsd->Context = SD_CONTEXT_NONE;
721 return HAL_ERROR;
722 }
723 else
724 {
725 /* Nothing to do */
726 }
727
728 /* Empty FIFO if there is still any data */
729 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)) && (dataremaining > 0U))
730 {
731 data = SDIO_ReadFIFO(hsd->Instance);
732 *tempbuff = (uint8_t)(data & 0xFFU);
733 tempbuff++;
734 dataremaining--;
735 *tempbuff = (uint8_t)((data >> 8U) & 0xFFU);
736 tempbuff++;
737 dataremaining--;
738 *tempbuff = (uint8_t)((data >> 16U) & 0xFFU);
739 tempbuff++;
740 dataremaining--;
741 *tempbuff = (uint8_t)((data >> 24U) & 0xFFU);
742 tempbuff++;
743 dataremaining--;
744
745 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
746 {
747 /* Clear all the static flags */
748 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
749 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
750 hsd->State= HAL_SD_STATE_READY;
751 hsd->Context = SD_CONTEXT_NONE;
752 return HAL_ERROR;
753 }
754 }
755
756 /* Clear all the static flags */
757 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
758
759 hsd->State = HAL_SD_STATE_READY;
760
761 return HAL_OK;
762 }
763 else
764 {
765 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
766 return HAL_ERROR;
767 }
768 }
769
770 /**
771 * @brief Allows to write block(s) to a specified address in a card. The Data
772 * transfer is managed by polling mode.
773 * @note This API should be followed by a check on the card state through
774 * HAL_SD_GetCardState().
775 * @param hsd: Pointer to SD handle
776 * @param pData: pointer to the buffer that will contain the data to transmit
777 * @param BlockAdd: Block Address where data will be written
778 * @param NumberOfBlocks: Number of SD blocks to write
779 * @param Timeout: Specify timeout value
780 * @retval HAL status
781 */
782 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
783 {
784 SDIO_DataInitTypeDef config;
785 uint32_t errorstate;
786 uint32_t tickstart = HAL_GetTick();
787 uint32_t count, data, dataremaining;
788 uint32_t add = BlockAdd;
789 uint8_t *tempbuff = pData;
790
791 if(NULL == pData)
792 {
793 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
794 return HAL_ERROR;
795 }
796
797 if(hsd->State == HAL_SD_STATE_READY)
798 {
799 hsd->ErrorCode = HAL_SD_ERROR_NONE;
800
801 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
802 {
803 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
804 return HAL_ERROR;
805 }
806
807 hsd->State = HAL_SD_STATE_BUSY;
808
809 /* Initialize data control register */
810 hsd->Instance->DCTRL = 0U;
811
812 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
813 {
814 add *= 512U;
815 }
816
817 /* Set Block Size for Card */
818 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
819 if(errorstate != HAL_SD_ERROR_NONE)
820 {
821 /* Clear all the static flags */
822 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
823 hsd->ErrorCode |= errorstate;
824 hsd->State = HAL_SD_STATE_READY;
825 return HAL_ERROR;
826 }
827
828 /* Configure the SD DPSM (Data Path State Machine) */
829 config.DataTimeOut = SDMMC_DATATIMEOUT;
830 config.DataLength = NumberOfBlocks * BLOCKSIZE;
831 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
832 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
833 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
834 config.DPSM = SDIO_DPSM_ENABLE;
835 (void)SDIO_ConfigData(hsd->Instance, &config);
836
837 /* Write Blocks in Polling mode */
838 if(NumberOfBlocks > 1U)
839 {
840 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
841
842 /* Write Multi Block command */
843 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
844 }
845 else
846 {
847 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
848
849 /* Write Single Block command */
850 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
851 }
852 if(errorstate != HAL_SD_ERROR_NONE)
853 {
854 /* Clear all the static flags */
855 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
856 hsd->ErrorCode |= errorstate;
857 hsd->State = HAL_SD_STATE_READY;
858 hsd->Context = SD_CONTEXT_NONE;
859 return HAL_ERROR;
860 }
861
862 /* Write block(s) in polling mode */
863 dataremaining = config.DataLength;
864 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
865 {
866 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) && (dataremaining > 0U))
867 {
868 /* Write data to SDIO Tx FIFO */
869 for(count = 0U; count < 8U; count++)
870 {
871 data = (uint32_t)(*tempbuff);
872 tempbuff++;
873 dataremaining--;
874 data |= ((uint32_t)(*tempbuff) << 8U);
875 tempbuff++;
876 dataremaining--;
877 data |= ((uint32_t)(*tempbuff) << 16U);
878 tempbuff++;
879 dataremaining--;
880 data |= ((uint32_t)(*tempbuff) << 24U);
881 tempbuff++;
882 dataremaining--;
883 (void)SDIO_WriteFIFO(hsd->Instance, &data);
884 }
885 }
886
887 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
888 {
889 /* Clear all the static flags */
890 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
891 hsd->ErrorCode |= errorstate;
892 hsd->State = HAL_SD_STATE_READY;
893 hsd->Context = SD_CONTEXT_NONE;
894 return HAL_TIMEOUT;
895 }
896 }
897
898 /* Send stop transmission command in case of multiblock write */
899 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
900 {
901 if(hsd->SdCard.CardType != CARD_SECURED)
902 {
903 /* Send stop transmission command */
904 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
905 if(errorstate != HAL_SD_ERROR_NONE)
906 {
907 /* Clear all the static flags */
908 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
909 hsd->ErrorCode |= errorstate;
910 hsd->State = HAL_SD_STATE_READY;
911 hsd->Context = SD_CONTEXT_NONE;
912 return HAL_ERROR;
913 }
914 }
915 }
916
917 /* Get error state */
918 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
919 {
920 /* Clear all the static flags */
921 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
922 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
923 hsd->State = HAL_SD_STATE_READY;
924 hsd->Context = SD_CONTEXT_NONE;
925 return HAL_ERROR;
926 }
927 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
928 {
929 /* Clear all the static flags */
930 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
931 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
932 hsd->State = HAL_SD_STATE_READY;
933 hsd->Context = SD_CONTEXT_NONE;
934 return HAL_ERROR;
935 }
936 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
937 {
938 /* Clear all the static flags */
939 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
940 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
941 hsd->State = HAL_SD_STATE_READY;
942 hsd->Context = SD_CONTEXT_NONE;
943 return HAL_ERROR;
944 }
945 else
946 {
947 /* Nothing to do */
948 }
949
950 /* Clear all the static flags */
951 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
952
953 hsd->State = HAL_SD_STATE_READY;
954
955 return HAL_OK;
956 }
957 else
958 {
959 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
960 return HAL_ERROR;
961 }
962 }
963
964 /**
965 * @brief Reads block(s) from a specified address in a card. The Data transfer
966 * is managed in interrupt mode.
967 * @note This API should be followed by a check on the card state through
968 * HAL_SD_GetCardState().
969 * @note You could also check the IT transfer process through the SD Rx
970 * interrupt event.
971 * @param hsd: Pointer to SD handle
972 * @param pData: Pointer to the buffer that will contain the received data
973 * @param BlockAdd: Block Address from where data is to be read
974 * @param NumberOfBlocks: Number of blocks to read.
975 * @retval HAL status
976 */
977 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
978 {
979 SDIO_DataInitTypeDef config;
980 uint32_t errorstate;
981 uint32_t add = BlockAdd;
982
983 if(NULL == pData)
984 {
985 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
986 return HAL_ERROR;
987 }
988
989 if(hsd->State == HAL_SD_STATE_READY)
990 {
991 hsd->ErrorCode = HAL_SD_ERROR_NONE;
992
993 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
994 {
995 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
996 return HAL_ERROR;
997 }
998
999 hsd->State = HAL_SD_STATE_BUSY;
1000
1001 /* Initialize data control register */
1002 hsd->Instance->DCTRL = 0U;
1003
1004 hsd->pRxBuffPtr = pData;
1005 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
1006
1007 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF));
1008
1009 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1010 {
1011 add *= 512U;
1012 }
1013
1014 /* Set Block Size for Card */
1015 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1016 if(errorstate != HAL_SD_ERROR_NONE)
1017 {
1018 /* Clear all the static flags */
1019 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1020 hsd->ErrorCode |= errorstate;
1021 hsd->State = HAL_SD_STATE_READY;
1022 return HAL_ERROR;
1023 }
1024
1025 /* Configure the SD DPSM (Data Path State Machine) */
1026 config.DataTimeOut = SDMMC_DATATIMEOUT;
1027 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1028 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1029 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
1030 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1031 config.DPSM = SDIO_DPSM_ENABLE;
1032 (void)SDIO_ConfigData(hsd->Instance, &config);
1033
1034 /* Read Blocks in IT mode */
1035 if(NumberOfBlocks > 1U)
1036 {
1037 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1038
1039 /* Read Multi Block command */
1040 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1041 }
1042 else
1043 {
1044 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
1045
1046 /* Read Single Block command */
1047 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1048 }
1049 if(errorstate != HAL_SD_ERROR_NONE)
1050 {
1051 /* Clear all the static flags */
1052 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1053 hsd->ErrorCode |= errorstate;
1054 hsd->State = HAL_SD_STATE_READY;
1055 hsd->Context = SD_CONTEXT_NONE;
1056 return HAL_ERROR;
1057 }
1058
1059 return HAL_OK;
1060 }
1061 else
1062 {
1063 return HAL_BUSY;
1064 }
1065 }
1066
1067 /**
1068 * @brief Writes block(s) to a specified address in a card. The Data transfer
1069 * is managed in interrupt mode.
1070 * @note This API should be followed by a check on the card state through
1071 * HAL_SD_GetCardState().
1072 * @note You could also check the IT transfer process through the SD Tx
1073 * interrupt event.
1074 * @param hsd: Pointer to SD handle
1075 * @param pData: Pointer to the buffer that will contain the data to transmit
1076 * @param BlockAdd: Block Address where data will be written
1077 * @param NumberOfBlocks: Number of blocks to write
1078 * @retval HAL status
1079 */
1080 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1081 {
1082 SDIO_DataInitTypeDef config;
1083 uint32_t errorstate;
1084 uint32_t add = BlockAdd;
1085
1086 if(NULL == pData)
1087 {
1088 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1089 return HAL_ERROR;
1090 }
1091
1092 if(hsd->State == HAL_SD_STATE_READY)
1093 {
1094 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1095
1096 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1097 {
1098 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1099 return HAL_ERROR;
1100 }
1101
1102 hsd->State = HAL_SD_STATE_BUSY;
1103
1104 /* Initialize data control register */
1105 hsd->Instance->DCTRL = 0U;
1106
1107 hsd->pTxBuffPtr = pData;
1108 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1109
1110 /* Enable transfer interrupts */
1111 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE));
1112
1113 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1114 {
1115 add *= 512U;
1116 }
1117
1118 /* Set Block Size for Card */
1119 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1120 if(errorstate != HAL_SD_ERROR_NONE)
1121 {
1122 /* Clear all the static flags */
1123 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1124 hsd->ErrorCode |= errorstate;
1125 hsd->State = HAL_SD_STATE_READY;
1126 return HAL_ERROR;
1127 }
1128
1129 /* Write Blocks in Polling mode */
1130 if(NumberOfBlocks > 1U)
1131 {
1132 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1133
1134 /* Write Multi Block command */
1135 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1136 }
1137 else
1138 {
1139 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1140
1141 /* Write Single Block command */
1142 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1143 }
1144 if(errorstate != HAL_SD_ERROR_NONE)
1145 {
1146 /* Clear all the static flags */
1147 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1148 hsd->ErrorCode |= errorstate;
1149 hsd->State = HAL_SD_STATE_READY;
1150 hsd->Context = SD_CONTEXT_NONE;
1151 return HAL_ERROR;
1152 }
1153
1154 /* Configure the SD DPSM (Data Path State Machine) */
1155 config.DataTimeOut = SDMMC_DATATIMEOUT;
1156 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1157 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1158 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
1159 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1160 config.DPSM = SDIO_DPSM_ENABLE;
1161 (void)SDIO_ConfigData(hsd->Instance, &config);
1162
1163 return HAL_OK;
1164 }
1165 else
1166 {
1167 return HAL_BUSY;
1168 }
1169 }
1170
1171 /**
1172 * @brief Reads block(s) from a specified address in a card. The Data transfer
1173 * is managed by DMA mode.
1174 * @note This API should be followed by a check on the card state through
1175 * HAL_SD_GetCardState().
1176 * @note You could also check the DMA transfer process through the SD Rx
1177 * interrupt event.
1178 * @param hsd: Pointer SD handle
1179 * @param pData: Pointer to the buffer that will contain the received data
1180 * @param BlockAdd: Block Address from where data is to be read
1181 * @param NumberOfBlocks: Number of blocks to read.
1182 * @retval HAL status
1183 */
1184 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1185 {
1186 SDIO_DataInitTypeDef config;
1187 uint32_t errorstate;
1188 uint32_t add = BlockAdd;
1189
1190 if(NULL == pData)
1191 {
1192 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1193 return HAL_ERROR;
1194 }
1195
1196 if(hsd->State == HAL_SD_STATE_READY)
1197 {
1198 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1199
1200 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1201 {
1202 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1203 return HAL_ERROR;
1204 }
1205
1206 hsd->State = HAL_SD_STATE_BUSY;
1207
1208 /* Initialize data control register */
1209 hsd->Instance->DCTRL = 0U;
1210
1211 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
1212
1213 /* Set the DMA transfer complete callback */
1214 hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
1215
1216 /* Set the DMA error callback */
1217 hsd->hdmarx->XferErrorCallback = SD_DMAError;
1218
1219 /* Set the DMA Abort callback */
1220 hsd->hdmarx->XferAbortCallback = NULL;
1221
1222 /* Enable the DMA Channel */
1223 if(HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1224 {
1225 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
1226 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1227 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1228 hsd->State = HAL_SD_STATE_READY;
1229 return HAL_ERROR;
1230 }
1231 else
1232 {
1233 /* Enable SD DMA transfer */
1234 // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
1235
1236 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1237 {
1238 add *= 512U;
1239
1240 /* Set Block Size for Card */
1241 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1242 if(errorstate != HAL_SD_ERROR_NONE)
1243 {
1244 /* Clear all the static flags */
1245 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1246 hsd->ErrorCode |= errorstate;
1247 hsd->State = HAL_SD_STATE_READY;
1248 return HAL_ERROR;
1249 }
1250 }
1251
1252 /* Configure the SD DPSM (Data Path State Machine) */
1253 config.DataTimeOut = SDMMC_DATATIMEOUT;
1254 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1255 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1256 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
1257 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1258 config.DPSM = SDIO_DPSM_ENABLE;
1259
1260 // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
1261 // data is just discarded before the dpsm is started.
1262 __HAL_SD_DMA_ENABLE(hsd);
1263
1264 (void)SDIO_ConfigData(hsd->Instance, &config);
1265
1266 /* Read Blocks in DMA mode */
1267 if(NumberOfBlocks > 1U)
1268 {
1269 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1270
1271 /* Read Multi Block command */
1272 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1273 }
1274 else
1275 {
1276 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1277
1278 /* Read Single Block command */
1279 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1280 }
1281 if(errorstate != HAL_SD_ERROR_NONE)
1282 {
1283 /* Clear all the static flags */
1284 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1285 hsd->ErrorCode |= errorstate;
1286 hsd->State = HAL_SD_STATE_READY;
1287 hsd->Context = SD_CONTEXT_NONE;
1288 return HAL_ERROR;
1289 }
1290
1291 return HAL_OK;
1292 }
1293 }
1294 else
1295 {
1296 return HAL_BUSY;
1297 }
1298 }
1299
1300 /**
1301 * @brief Writes block(s) to a specified address in a card. The Data transfer
1302 * is managed by DMA mode.
1303 * @note This API should be followed by a check on the card state through
1304 * HAL_SD_GetCardState().
1305 * @note You could also check the DMA transfer process through the SD Tx
1306 * interrupt event.
1307 * @param hsd: Pointer to SD handle
1308 * @param pData: Pointer to the buffer that will contain the data to transmit
1309 * @param BlockAdd: Block Address where data will be written
1310 * @param NumberOfBlocks: Number of blocks to write
1311 * @retval HAL status
1312 */
1313 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1314 {
1315 SDIO_DataInitTypeDef config;
1316 uint32_t errorstate;
1317 uint32_t add = BlockAdd;
1318
1319 if(NULL == pData)
1320 {
1321 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1322 return HAL_ERROR;
1323 }
1324
1325 if(hsd->State == HAL_SD_STATE_READY)
1326 {
1327 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1328
1329 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1330 {
1331 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1332 return HAL_ERROR;
1333 }
1334
1335 hsd->State = HAL_SD_STATE_BUSY;
1336
1337 /* Initialize data control register */
1338 hsd->Instance->DCTRL = 0U;
1339
1340 /* Enable SD Error interrupts */
1341 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
1342
1343 /* Set the DMA transfer complete callback */
1344 hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
1345
1346 /* Set the DMA error callback */
1347 hsd->hdmatx->XferErrorCallback = SD_DMAError;
1348
1349 /* Set the DMA Abort callback */
1350 hsd->hdmatx->XferAbortCallback = NULL;
1351
1352 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1353 {
1354 add *= 512U;
1355
1356 /* Set Block Size for Card */
1357 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1358 if(errorstate != HAL_SD_ERROR_NONE)
1359 {
1360 /* Clear all the static flags */
1361 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1362 hsd->ErrorCode |= errorstate;
1363 hsd->State = HAL_SD_STATE_READY;
1364 return HAL_ERROR;
1365 }
1366 }
1367
1368 /* Write Blocks in Polling mode */
1369 if(NumberOfBlocks > 1U)
1370 {
1371 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1372
1373 /* MM: Prepare for write */
1374 /* TODO
1375 SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->RCA << 16));
1376 SDIO_CmdInitTypeDef mm_cmdinit;
1377 mm_cmdinit.Argument = (uint32_t)NumberOfBlocks;
1378 mm_cmdinit.CmdIndex = SDMMC_CMD_SET_BLOCK_COUNT;
1379 mm_cmdinit.Response = SDIO_RESPONSE_SHORT;
1380 mm_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
1381 mm_cmdinit.CPSM = SDIO_CPSM_ENABLE;
1382 (void)SDIO_SendCommand(hsd->Instance, &mm_cmdinit);
1383 SDMMC_GetCmdResp1(hsd->Instance, SDMMC_CMD_SET_BLOCK_COUNT, SDIO_CMDTIMEOUT);*/
1384
1385 /* Write Multi Block command */
1386 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1387 }
1388 else
1389 {
1390 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1391
1392 /* Write Single Block command */
1393 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1394 }
1395 if(errorstate != HAL_SD_ERROR_NONE)
1396 {
1397 /* Clear all the static flags */
1398 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1399 hsd->ErrorCode |= errorstate;
1400 hsd->State = HAL_SD_STATE_READY;
1401 hsd->Context = SD_CONTEXT_NONE;
1402 return HAL_ERROR;
1403 }
1404
1405 /* Enable SDIO DMA transfer */
1406 // MM disabled, as this fails on fast cards. __HAL_SD_DMA_ENABLE(hsd);
1407
1408 /* Enable the DMA Channel */
1409 if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1410 {
1411 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
1412 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1413 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1414 hsd->State = HAL_SD_STATE_READY;
1415 hsd->Context = SD_CONTEXT_NONE;
1416 return HAL_ERROR;
1417 }
1418 else
1419 {
1420 /* Configure the SD DPSM (Data Path State Machine) */
1421 config.DataTimeOut = SDMMC_DATATIMEOUT;
1422 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1423 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1424 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
1425 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1426 config.DPSM = SDIO_DPSM_ENABLE;
1427
1428 // We cannot enable DMA too early on UHS-I class 3 SD cards, or else the
1429 // data is just discarded before the dpsm is started.
1430 __HAL_SD_DMA_ENABLE();
1431
1432 (void)SDIO_ConfigData(hsd->Instance, &config);
1433
1434 return HAL_OK;
1435 }
1436 }
1437 else
1438 {
1439 return HAL_BUSY;
1440 }
1441 }
1442
1443 /**
1444 * @brief Erases the specified memory area of the given SD card.
1445 * @note This API should be followed by a check on the card state through
1446 * HAL_SD_GetCardState().
1447 * @param hsd: Pointer to SD handle
1448 * @param BlockStartAdd: Start Block address
1449 * @param BlockEndAdd: End Block address
1450 * @retval HAL status
1451 */
1452 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1453 {
1454 uint32_t errorstate;
1455 uint32_t start_add = BlockStartAdd;
1456 uint32_t end_add = BlockEndAdd;
1457
1458 if(hsd->State == HAL_SD_STATE_READY)
1459 {
1460 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1461
1462 if(end_add < start_add)
1463 {
1464 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1465 return HAL_ERROR;
1466 }
1467
1468 if(end_add > (hsd->SdCard.LogBlockNbr))
1469 {
1470 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1471 return HAL_ERROR;
1472 }
1473
1474 hsd->State = HAL_SD_STATE_BUSY;
1475
1476 /* Check if the card command class supports erase command */
1477 if(((hsd->SdCard.Class) & SDIO_CCCC_ERASE) == 0U)
1478 {
1479 /* Clear all the static flags */
1480 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1481 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1482 hsd->State = HAL_SD_STATE_READY;
1483 return HAL_ERROR;
1484 }
1485
1486 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1487 {
1488 /* Clear all the static flags */
1489 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1490 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1491 hsd->State = HAL_SD_STATE_READY;
1492 return HAL_ERROR;
1493 }
1494
1495 /* Get start and end block for high capacity cards */
1496 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1497 {
1498 start_add *= 512U;
1499 end_add *= 512U;
1500 }
1501
1502 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1503 if(hsd->SdCard.CardType != CARD_SECURED)
1504 {
1505 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1506 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
1507 if(errorstate != HAL_SD_ERROR_NONE)
1508 {
1509 /* Clear all the static flags */
1510 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1511 hsd->ErrorCode |= errorstate;
1512 hsd->State = HAL_SD_STATE_READY;
1513 return HAL_ERROR;
1514 }
1515
1516 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1517 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
1518 if(errorstate != HAL_SD_ERROR_NONE)
1519 {
1520 /* Clear all the static flags */
1521 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1522 hsd->ErrorCode |= errorstate;
1523 hsd->State = HAL_SD_STATE_READY;
1524 return HAL_ERROR;
1525 }
1526 }
1527
1528 /* Send CMD38 ERASE */
1529 errorstate = SDMMC_CmdErase(hsd->Instance);
1530 if(errorstate != HAL_SD_ERROR_NONE)
1531 {
1532 /* Clear all the static flags */
1533 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1534 hsd->ErrorCode |= errorstate;
1535 hsd->State = HAL_SD_STATE_READY;
1536 return HAL_ERROR;
1537 }
1538
1539 hsd->State = HAL_SD_STATE_READY;
1540
1541 return HAL_OK;
1542 }
1543 else
1544 {
1545 return HAL_BUSY;
1546 }
1547 }
1548
1549 /**
1550 * @brief This function handles SD card interrupt request.
1551 * @param hsd: Pointer to SD handle
1552 * @retval None
1553 */
1554 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1555 {
1556 uint32_t errorstate;
1557 uint32_t context = hsd->Context;
1558
1559 /* Check for SDIO interrupt flags */
1560 if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1561 {
1562 SD_Read_IT(hsd);
1563 }
1564
1565 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) != RESET)
1566 {
1567 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DATAEND);
1568
1569 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1570 SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR | SDIO_IT_TXFIFOHE |\
1571 SDIO_IT_RXFIFOHF);
1572
1573 hsd->Instance->DCTRL &= ~(SDIO_DCTRL_DTEN);
1574
1575 if((context & SD_CONTEXT_IT) != 0U)
1576 {
1577 if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1578 {
1579 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1580 if(errorstate != HAL_SD_ERROR_NONE)
1581 {
1582 hsd->ErrorCode |= errorstate;
1583 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1584 hsd->ErrorCallback(hsd);
1585 #else
1586 HAL_SD_ErrorCallback(hsd);
1587 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1588 }
1589 }
1590
1591 /* Clear all the static flags */
1592 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
1593
1594 hsd->State = HAL_SD_STATE_READY;
1595 hsd->Context = SD_CONTEXT_NONE;
1596 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1597 {
1598 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1599 hsd->RxCpltCallback(hsd);
1600 #else
1601 HAL_SD_RxCpltCallback(hsd);
1602 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1603 }
1604 else
1605 {
1606 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1607 hsd->TxCpltCallback(hsd);
1608 #else
1609 HAL_SD_TxCpltCallback(hsd);
1610 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1611 }
1612 }
1613 else if((context & SD_CONTEXT_DMA) != 0U)
1614 {
1615 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1616 {
1617 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1618 if(errorstate != HAL_SD_ERROR_NONE)
1619 {
1620 hsd->ErrorCode |= errorstate;
1621 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1622 hsd->ErrorCallback(hsd);
1623 #else
1624 HAL_SD_ErrorCallback(hsd);
1625 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1626 }
1627 }
1628 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
1629 {
1630 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1631 in the SD DCTRL register */
1632 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
1633
1634 hsd->State = HAL_SD_STATE_READY;
1635
1636 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1637 hsd->TxCpltCallback(hsd);
1638 #else
1639 HAL_SD_TxCpltCallback(hsd);
1640 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1641 }
1642 }
1643 else
1644 {
1645 /* Nothing to do */
1646 }
1647 }
1648
1649 else if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1650 {
1651 SD_Write_IT(hsd);
1652 }
1653
1654 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR) != RESET)
1655 {
1656 /* Set Error code */
1657 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL) != RESET)
1658 {
1659 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1660 }
1661 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT) != RESET)
1662 {
1663 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1664 }
1665 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR) != RESET)
1666 {
1667 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1668 }
1669 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR) != RESET)
1670 {
1671 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1672 }
1673
1674 /* Clear All flags */
1675 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS | SDIO_FLAG_STBITERR);
1676
1677 /* Disable all interrupts */
1678 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1679 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
1680
1681 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
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 /* Abort the SD DMA channel */
1697 if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1698 {
1699 /* Set the DMA Tx abort callback */
1700 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
1701 /* Abort DMA in IT mode */
1702 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
1703 {
1704 SD_DMATxAbort(hsd->hdmatx);
1705 }
1706 }
1707 else if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1708 {
1709 /* Set the DMA Rx abort callback */
1710 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
1711 /* Abort DMA in IT mode */
1712 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
1713 {
1714 SD_DMARxAbort(hsd->hdmarx);
1715 }
1716 }
1717 else
1718 {
1719 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1720 hsd->State = HAL_SD_STATE_READY;
1721 hsd->Context = SD_CONTEXT_NONE;
1722 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1723 hsd->AbortCpltCallback(hsd);
1724 #else
1725 HAL_SD_AbortCallback(hsd);
1726 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1727 }
1728 }
1729 else
1730 {
1731 /* Nothing to do */
1732 }
1733 }
1734 else
1735 {
1736 /* Nothing to do */
1737 }
1738 }
1739
1740 /**
1741 * @brief return the SD state
1742 * @param hsd: Pointer to sd handle
1743 * @retval HAL state
1744 */
1745 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
1746 {
1747 return hsd->State;
1748 }
1749
1750 /**
1751 * @brief Return the SD error code
1752 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
1753 * the configuration information.
1754 * @retval SD Error Code
1755 */
1756 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
1757 {
1758 return hsd->ErrorCode;
1759 }
1760
1761 /**
1762 * @brief Tx Transfer completed callbacks
1763 * @param hsd: Pointer to SD handle
1764 * @retval None
1765 */
1766 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
1767 {
1768 /* Prevent unused argument(s) compilation warning */
1769 UNUSED(hsd);
1770
1771 /* NOTE : This function should not be modified, when the callback is needed,
1772 the HAL_SD_TxCpltCallback can be implemented in the user file
1773 */
1774 }
1775
1776 /**
1777 * @brief Rx Transfer completed callbacks
1778 * @param hsd: Pointer SD handle
1779 * @retval None
1780 */
1781 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
1782 {
1783 /* Prevent unused argument(s) compilation warning */
1784 UNUSED(hsd);
1785
1786 /* NOTE : This function should not be modified, when the callback is needed,
1787 the HAL_SD_RxCpltCallback can be implemented in the user file
1788 */
1789 }
1790
1791 /**
1792 * @brief SD error callbacks
1793 * @param hsd: Pointer SD handle
1794 * @retval None
1795 */
1796 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1797 {
1798 /* Prevent unused argument(s) compilation warning */
1799 UNUSED(hsd);
1800
1801 /* NOTE : This function should not be modified, when the callback is needed,
1802 the HAL_SD_ErrorCallback can be implemented in the user file
1803 */
1804 }
1805
1806 /**
1807 * @brief SD Abort callbacks
1808 * @param hsd: Pointer SD handle
1809 * @retval None
1810 */
1811 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
1812 {
1813 /* Prevent unused argument(s) compilation warning */
1814 UNUSED(hsd);
1815
1816 /* NOTE : This function should not be modified, when the callback is needed,
1817 the HAL_SD_AbortCallback can be implemented in the user file
1818 */
1819 }
1820
1821 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1822 /**
1823 * @brief Register a User SD Callback
1824 * To be used instead of the weak (surcharged) predefined callback
1825 * @param hsd : SD handle
1826 * @param CallbackID : ID of the callback to be registered
1827 * This parameter can be one of the following values:
1828 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1829 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1830 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1831 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1832 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1833 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1834 * @param pCallback : pointer to the Callback function
1835 * @retval status
1836 */
1837 HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
1838 {
1839 HAL_StatusTypeDef status = HAL_OK;
1840
1841 if(pCallback == NULL)
1842 {
1843 /* Update the error code */
1844 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1845 return HAL_ERROR;
1846 }
1847
1848 /* Process locked */
1849 __HAL_LOCK(hsd);
1850
1851 if(hsd->State == HAL_SD_STATE_READY)
1852 {
1853 switch (CallbackID)
1854 {
1855 case HAL_SD_TX_CPLT_CB_ID :
1856 hsd->TxCpltCallback = pCallback;
1857 break;
1858 case HAL_SD_RX_CPLT_CB_ID :
1859 hsd->RxCpltCallback = pCallback;
1860 break;
1861 case HAL_SD_ERROR_CB_ID :
1862 hsd->ErrorCallback = pCallback;
1863 break;
1864 case HAL_SD_ABORT_CB_ID :
1865 hsd->AbortCpltCallback = pCallback;
1866 break;
1867 case HAL_SD_MSP_INIT_CB_ID :
1868 hsd->MspInitCallback = pCallback;
1869 break;
1870 case HAL_SD_MSP_DEINIT_CB_ID :
1871 hsd->MspDeInitCallback = pCallback;
1872 break;
1873 default :
1874 /* Update the error code */
1875 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1876 /* update return status */
1877 status = HAL_ERROR;
1878 break;
1879 }
1880 }
1881 else if (hsd->State == HAL_SD_STATE_RESET)
1882 {
1883 switch (CallbackID)
1884 {
1885 case HAL_SD_MSP_INIT_CB_ID :
1886 hsd->MspInitCallback = pCallback;
1887 break;
1888 case HAL_SD_MSP_DEINIT_CB_ID :
1889 hsd->MspDeInitCallback = pCallback;
1890 break;
1891 default :
1892 /* Update the error code */
1893 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1894 /* update return status */
1895 status = HAL_ERROR;
1896 break;
1897 }
1898 }
1899 else
1900 {
1901 /* Update the error code */
1902 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1903 /* update return status */
1904 status = HAL_ERROR;
1905 }
1906
1907 /* Release Lock */
1908 __HAL_UNLOCK(hsd);
1909 return status;
1910 }
1911
1912 /**
1913 * @brief Unregister a User SD Callback
1914 * SD Callback is redirected to the weak (surcharged) predefined callback
1915 * @param hsd : SD handle
1916 * @param CallbackID : ID of the callback to be unregistered
1917 * This parameter can be one of the following values:
1918 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1919 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1920 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1921 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1922 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1923 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1924 * @retval status
1925 */
1926 HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
1927 {
1928 HAL_StatusTypeDef status = HAL_OK;
1929
1930 /* Process locked */
1931 __HAL_LOCK(hsd);
1932
1933 if(hsd->State == HAL_SD_STATE_READY)
1934 {
1935 switch (CallbackID)
1936 {
1937 case HAL_SD_TX_CPLT_CB_ID :
1938 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
1939 break;
1940 case HAL_SD_RX_CPLT_CB_ID :
1941 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
1942 break;
1943 case HAL_SD_ERROR_CB_ID :
1944 hsd->ErrorCallback = HAL_SD_ErrorCallback;
1945 break;
1946 case HAL_SD_ABORT_CB_ID :
1947 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
1948 break;
1949 case HAL_SD_MSP_INIT_CB_ID :
1950 hsd->MspInitCallback = HAL_SD_MspInit;
1951 break;
1952 case HAL_SD_MSP_DEINIT_CB_ID :
1953 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1954 break;
1955 default :
1956 /* Update the error code */
1957 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1958 /* update return status */
1959 status = HAL_ERROR;
1960 break;
1961 }
1962 }
1963 else if (hsd->State == HAL_SD_STATE_RESET)
1964 {
1965 switch (CallbackID)
1966 {
1967 case HAL_SD_MSP_INIT_CB_ID :
1968 hsd->MspInitCallback = HAL_SD_MspInit;
1969 break;
1970 case HAL_SD_MSP_DEINIT_CB_ID :
1971 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1972 break;
1973 default :
1974 /* Update the error code */
1975 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1976 /* update return status */
1977 status = HAL_ERROR;
1978 break;
1979 }
1980 }
1981 else
1982 {
1983 /* Update the error code */
1984 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1985 /* update return status */
1986 status = HAL_ERROR;
1987 }
1988
1989 /* Release Lock */
1990 __HAL_UNLOCK(hsd);
1991 return status;
1992 }
1993 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1994
1995 /**
1996 * @}
1997 */
1998
1999 /** @addtogroup SD_Exported_Functions_Group3
2000 * @brief management functions
2001 *
2002 @verbatim
2003 ==============================================================================
2004 ##### Peripheral Control functions #####
2005 ==============================================================================
2006 [..]
2007 This subsection provides a set of functions allowing to control the SD card
2008 operations and get the related information
2009
2010 @endverbatim
2011 * @{
2012 */
2013
2014 /**
2015 * @brief Returns information the information of the card which are stored on
2016 * the CID register.
2017 * @param hsd: Pointer to SD handle
2018 * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that
2019 * contains all CID register parameters
2020 * @retval HAL status
2021 */
2022 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
2023 {
2024 pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
2025
2026 pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
2027
2028 pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
2029
2030 pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
2031
2032 pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
2033
2034 pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
2035
2036 pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
2037
2038 pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
2039
2040 pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
2041
2042 pCID->Reserved2 = 1U;
2043
2044 return HAL_OK;
2045 }
2046
2047 /**
2048 * @brief Returns information the information of the card which are stored on
2049 * the CSD register.
2050 * @param hsd: Pointer to SD handle
2051 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
2052 * contains all CSD register parameters
2053 * @retval HAL status
2054 */
2055 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
2056 {
2057 pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
2058
2059 pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
2060
2061 pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
2062
2063 pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
2064
2065 pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
2066
2067 pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
2068
2069 pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
2070
2071 pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
2072
2073 pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
2074
2075 pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
2076
2077 pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
2078
2079 pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
2080
2081 pCSD->Reserved2 = 0U; /*!< Reserved */
2082
2083 if(hsd->SdCard.CardType == CARD_SDSC)
2084 {
2085 pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
2086
2087 pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
2088
2089 pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
2090
2091 pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
2092
2093 pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
2094
2095 pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
2096
2097 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2098 hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2099 hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2100
2101 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
2102 hsd->SdCard.LogBlockSize = 512U;
2103 }
2104 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
2105 {
2106 /* Byte 7 */
2107 pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
2108
2109 hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
2110 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
2111 hsd->SdCard.BlockSize = 512U;
2112 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
2113 }
2114 else
2115 {
2116 /* Clear all the static flags */
2117 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2118 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2119 hsd->State = HAL_SD_STATE_READY;
2120 return HAL_ERROR;
2121 }
2122
2123 pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
2124
2125 pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
2126
2127 pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
2128
2129 pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
2130
2131 pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
2132
2133 pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
2134
2135 pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
2136
2137 pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
2138
2139 pCSD->Reserved3 = 0;
2140
2141 pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
2142
2143 pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
2144
2145 pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
2146
2147 pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
2148
2149 pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
2150
2151 pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
2152
2153 pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
2154
2155 pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
2156
2157 pCSD->Reserved4 = 1;
2158
2159 return HAL_OK;
2160 }
2161
2162 /**
2163 * @brief Gets the SD status info.
2164 * @param hsd: Pointer to SD handle
2165 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
2166 * will contain the SD card status information
2167 * @retval HAL status
2168 */
2169 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
2170 {
2171 uint32_t sd_status[16];
2172 uint32_t errorstate;
2173
2174 errorstate = SD_SendSDStatus(hsd, sd_status);
2175 if(errorstate != HAL_SD_ERROR_NONE)
2176 {
2177 /* Clear all the static flags */
2178 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2179 hsd->ErrorCode |= errorstate;
2180 hsd->State = HAL_SD_STATE_READY;
2181 return HAL_ERROR;
2182 }
2183 else
2184 {
2185 pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
2186
2187 pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
2188
2189 pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
2190
2191 pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
2192 ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
2193
2194 pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
2195
2196 pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
2197
2198 pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
2199
2200 pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
2201
2202 pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
2203
2204 pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
2205 }
2206
2207 return HAL_OK;
2208 }
2209
2210 /**
2211 * @brief Gets the SD card info.
2212 * @param hsd: Pointer to SD handle
2213 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
2214 * will contain the SD card status information
2215 * @retval HAL status
2216 */
2217 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
2218 {
2219 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
2220 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
2221 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
2222 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
2223 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
2224 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
2225 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
2226 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
2227
2228 return HAL_OK;
2229 }
2230
2231 /**
2232 * @brief Enables wide bus operation for the requested card if supported by
2233 * card.
2234 * @param hsd: Pointer to SD handle
2235 * @param WideMode: Specifies the SD card wide bus mode
2236 * This parameter can be one of the following values:
2237 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
2238 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
2239 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
2240 * @retval HAL status
2241 */
2242 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
2243 {
2244 SDIO_InitTypeDef Init;
2245 uint32_t errorstate;
2246
2247 /* Check the parameters */
2248 assert_param(IS_SDIO_BUS_WIDE(WideMode));
2249
2250 /* Change State */
2251 hsd->State = HAL_SD_STATE_BUSY;
2252
2253 if(hsd->SdCard.CardType != CARD_SECURED)
2254 {
2255 if(WideMode == SDIO_BUS_WIDE_8B)
2256 {
2257 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2258 }
2259 else if(WideMode == SDIO_BUS_WIDE_4B)
2260 {
2261 errorstate = SD_WideBus_Enable(hsd);
2262
2263 hsd->ErrorCode |= errorstate;
2264 }
2265 else if(WideMode == SDIO_BUS_WIDE_1B)
2266 {
2267 errorstate = SD_WideBus_Disable(hsd);
2268
2269 hsd->ErrorCode |= errorstate;
2270 }
2271 else
2272 {
2273 /* WideMode is not a valid argument*/
2274 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2275 }
2276 }
2277 else
2278 {
2279 /* MMC Card does not support this feature */
2280 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2281 }
2282
2283 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2284 {
2285 /* Clear all the static flags */
2286 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2287 hsd->State = HAL_SD_STATE_READY;
2288 return HAL_ERROR;
2289 }
2290 else
2291 {
2292 /* Configure the SDIO peripheral */
2293 Init.ClockEdge = hsd->Init.ClockEdge;
2294 Init.ClockBypass = hsd->Init.ClockBypass;
2295 Init.ClockPowerSave = hsd->Init.ClockPowerSave;
2296 Init.BusWide = WideMode;
2297 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2298 Init.ClockDiv = hsd->Init.ClockDiv;
2299 (void)SDIO_Init(hsd->Instance, Init);
2300 }
2301
2302 /* Change State */
2303 hsd->State = HAL_SD_STATE_READY;
2304
2305 return HAL_OK;
2306 }
2307
2308 /**
2309 * @brief Gets the current sd card data state.
2310 * @param hsd: pointer to SD handle
2311 * @retval Card state
2312 */
2313 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2314 {
2315 uint32_t cardstate;
2316 uint32_t errorstate;
2317 uint32_t resp1 = 0;
2318
2319 errorstate = SD_SendStatus(hsd, &resp1);
2320 if(errorstate != HAL_SD_ERROR_NONE)
2321 {
2322 hsd->ErrorCode |= errorstate;
2323 }
2324
2325 cardstate = ((resp1 >> 9U) & 0x0FU);
2326
2327 return (HAL_SD_CardStateTypeDef)cardstate;
2328 }
2329
2330 /**
2331 * @brief Abort the current transfer and disable the SD.
2332 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2333 * the configuration information for SD module.
2334 * @retval HAL status
2335 */
2336 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
2337 {
2338 HAL_SD_CardStateTypeDef CardState;
2339 uint32_t context = hsd->Context;
2340
2341 /* DIsable All interrupts */
2342 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2343 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2344
2345 /* Clear All flags */
2346 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2347
2348 CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
2349
2350 if ((context & SD_CONTEXT_DMA) != 0U)
2351 {
2352 /* Disable the SD DMA request */
2353 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2354
2355 /* Abort the SD DMA Tx channel */
2356 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2357 {
2358 if(HAL_DMA_Abort(hsd->hdmatx) != HAL_OK)
2359 {
2360 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2361 }
2362 }
2363 /* Abort the SD DMA Rx channel */
2364 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2365 {
2366 if(HAL_DMA_Abort(hsd->hdmarx) != HAL_OK)
2367 {
2368 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2369 }
2370 }
2371 else
2372 {
2373 /* Nothing to do */
2374 }
2375 }
2376
2377 hsd->State = HAL_SD_STATE_READY;
2378
2379 /* Initialize the SD operation */
2380 hsd->Context = SD_CONTEXT_NONE;
2381
2382 CardState = HAL_SD_GetCardState(hsd);
2383 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2384 {
2385 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2386 }
2387 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2388 {
2389 return HAL_ERROR;
2390 }
2391 return HAL_OK;
2392 }
2393
2394 /**
2395 * @brief Abort the current transfer and disable the SD (IT mode).
2396 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2397 * the configuration information for SD module.
2398 * @retval HAL status
2399 */
2400 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
2401 {
2402 HAL_SD_CardStateTypeDef CardState;
2403 uint32_t context = hsd->Context;
2404
2405 /* Disable All interrupts */
2406 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2407 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2408
2409 CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
2410
2411 if ((context & SD_CONTEXT_DMA) != 0U)
2412 {
2413 /* Disable the SD DMA request */
2414 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2415
2416 /* Abort the SD DMA Tx channel */
2417 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2418 {
2419 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
2420 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
2421 {
2422 hsd->hdmatx = NULL;
2423 }
2424 }
2425 /* Abort the SD DMA Rx channel */
2426 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2427 {
2428 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
2429 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
2430 {
2431 hsd->hdmarx = NULL;
2432 }
2433 }
2434 else
2435 {
2436 /* Nothing to do */
2437 }
2438 }
2439 /* No transfer ongoing on both DMA channels*/
2440 else
2441 {
2442 /* Clear All flags */
2443 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2444
2445 CardState = HAL_SD_GetCardState(hsd);
2446 hsd->State = HAL_SD_STATE_READY;
2447 hsd->Context = SD_CONTEXT_NONE;
2448 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2449 {
2450 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2451 }
2452 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2453 {
2454 return HAL_ERROR;
2455 }
2456 else
2457 {
2458 #if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2459 hsd->AbortCpltCallback(hsd);
2460 #else
2461 HAL_SD_AbortCallback(hsd);
2462 #endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2463 }
2464 }
2465
2466 return HAL_OK;
2467 }
2468
2469 /**
2470 * @}
2471 */
2472
2473 /**
2474 * @}
2475 */
2476
2477 /* Private function ----------------------------------------------------------*/
2478 /** @addtogroup SD_Private_Functions
2479 * @{
2480 */
2481
2482 /**
2483 * @brief DMA SD transmit process complete callback
2484 * @param hdma: DMA handle
2485 * @retval None
2486 */
2487 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2488 {
2489 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2490
2491 /* Enable DATAEND Interrupt */
2492 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND));
2493 }
2494
2495 /**
2496 * @brief DMA SD receive process complete callback
2497 * @param hdma: DMA handle
2498 * @retval None
2499 */
2500 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2501 {
2502 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2503 uint32_t errorstate;
2504
2505 /* Send stop command in multiblock write */
2506 if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
2507 {
2508 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
2509 if(errorstate != HAL_SD_ERROR_NONE)
2510 {
2511 hsd->ErrorCode |= errorstate;
2512 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2513 hsd->ErrorCallback(hsd);
2514 #else
2515 HAL_SD_ErrorCallback(hsd);
2516 #endif
2517 }
2518 }
2519
2520 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2521 in the SD DCTRL register */
2522 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2523
2524 /* Clear all the static flags */
2525 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2526
2527 hsd->State = HAL_SD_STATE_READY;
2528 hsd->Context = SD_CONTEXT_NONE;
2529
2530 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2531 hsd->RxCpltCallback(hsd);
2532 #else
2533 HAL_SD_RxCpltCallback(hsd);
2534 #endif
2535 }
2536
2537 /**
2538 * @brief DMA SD communication error callback
2539 * @param hdma: DMA handle
2540 * @retval None
2541 */
2542 static void SD_DMAError(DMA_HandleTypeDef *hdma)
2543 {
2544 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2545 HAL_SD_CardStateTypeDef CardState;
2546 uint32_t RxErrorCode, TxErrorCode;
2547
2548 /* if DMA error is FIFO error ignore it */
2549 if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
2550 {
2551 RxErrorCode = hsd->hdmarx->ErrorCode;
2552 TxErrorCode = hsd->hdmatx->ErrorCode;
2553 if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
2554 {
2555 /* Clear All flags */
2556 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2557
2558 /* Disable All interrupts */
2559 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2560 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2561
2562 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2563 CardState = HAL_SD_GetCardState(hsd);
2564 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2565 {
2566 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2567 }
2568
2569 hsd->State= HAL_SD_STATE_READY;
2570 hsd->Context = SD_CONTEXT_NONE;
2571 }
2572
2573 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2574 hsd->ErrorCallback(hsd);
2575 #else
2576 HAL_SD_ErrorCallback(hsd);
2577 #endif
2578 }
2579 }
2580
2581 /**
2582 * @brief DMA SD Tx Abort callback
2583 * @param hdma: DMA handle
2584 * @retval None
2585 */
2586 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
2587 {
2588 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2589 HAL_SD_CardStateTypeDef CardState;
2590
2591 /* Clear All flags */
2592 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2593
2594 CardState = HAL_SD_GetCardState(hsd);
2595 hsd->State = HAL_SD_STATE_READY;
2596 hsd->Context = SD_CONTEXT_NONE;
2597 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2598 {
2599 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2600 }
2601
2602 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2603 {
2604 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2605 hsd->AbortCpltCallback(hsd);
2606 #else
2607 HAL_SD_AbortCallback(hsd);
2608 #endif
2609 }
2610 else
2611 {
2612 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2613 hsd->ErrorCallback(hsd);
2614 #else
2615 HAL_SD_ErrorCallback(hsd);
2616 #endif
2617 }
2618 }
2619
2620 /**
2621 * @brief DMA SD Rx Abort callback
2622 * @param hdma: DMA handle
2623 * @retval None
2624 */
2625 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
2626 {
2627 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2628 HAL_SD_CardStateTypeDef CardState;
2629
2630 /* Clear All flags */
2631 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2632
2633 CardState = HAL_SD_GetCardState(hsd);
2634 hsd->State = HAL_SD_STATE_READY;
2635 hsd->Context = SD_CONTEXT_NONE;
2636 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2637 {
2638 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2639 }
2640
2641 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2642 {
2643 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2644 hsd->AbortCpltCallback(hsd);
2645 #else
2646 HAL_SD_AbortCallback(hsd);
2647 #endif
2648 }
2649 else
2650 {
2651 #if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2652 hsd->ErrorCallback(hsd);
2653 #else
2654 HAL_SD_ErrorCallback(hsd);
2655 #endif
2656 }
2657 }
2658
2659 /**
2660 * @brief Initializes the sd card.
2661 * @param hsd: Pointer to SD handle
2662 * @retval SD Card error state
2663 */
2664 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
2665 {
2666 HAL_SD_CardCSDTypeDef CSD;
2667 uint32_t errorstate;
2668 uint16_t sd_rca = 1U;
2669
2670 /* Check the power State */
2671 if(SDIO_GetPowerState(hsd->Instance) == 0U)
2672 {
2673 /* Power off */
2674 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2675 }
2676
2677 if(hsd->SdCard.CardType != CARD_SECURED)
2678 {
2679 /* Send CMD2 ALL_SEND_CID */
2680 errorstate = SDMMC_CmdSendCID(hsd->Instance);
2681 if(errorstate != HAL_SD_ERROR_NONE)
2682 {
2683 return errorstate;
2684 }
2685 else
2686 {
2687 /* Get Card identification number data */
2688 hsd->CID[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2689 hsd->CID[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2690 hsd->CID[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2691 hsd->CID[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2692 }
2693 }
2694
2695 if(hsd->SdCard.CardType != CARD_SECURED)
2696 {
2697 /* Send CMD3 SET_REL_ADDR with argument 0 */
2698 /* SD Card publishes its RCA. */
2699 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
2700 if(errorstate != HAL_SD_ERROR_NONE)
2701 {
2702 return errorstate;
2703 }
2704 }
2705 if(hsd->SdCard.CardType != CARD_SECURED)
2706 {
2707 /* Get the SD card RCA */
2708 hsd->SdCard.RelCardAdd = sd_rca;
2709
2710 /* Send CMD9 SEND_CSD with argument as card's RCA */
2711 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2712 if(errorstate != HAL_SD_ERROR_NONE)
2713 {
2714 return errorstate;
2715 }
2716 else
2717 {
2718 /* Get Card Specific Data */
2719 hsd->CSD[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2720 hsd->CSD[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2721 hsd->CSD[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2722 hsd->CSD[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2723 }
2724 }
2725
2726 /* Get the Card Class */
2727 hsd->SdCard.Class = (SDIO_GetResponse(hsd->Instance, SDIO_RESP2) >> 20U);
2728
2729 /* Get CSD parameters */
2730 if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
2731 {
2732 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2733 }
2734
2735 /* Select the Card */
2736 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
2737 if(errorstate != HAL_SD_ERROR_NONE)
2738 {
2739 return errorstate;
2740 }
2741
2742 /* Configure SDIO peripheral interface */
2743 (void)SDIO_Init(hsd->Instance, hsd->Init);
2744
2745 /* All cards are initialized */
2746 return HAL_SD_ERROR_NONE;
2747 }
2748
2749 /**
2750 * @brief Enquires cards about their operating voltage and configures clock
2751 * controls and stores SD information that will be needed in future
2752 * in the SD handle.
2753 * @param hsd: Pointer to SD handle
2754 * @retval error state
2755 */
2756 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
2757 {
2758 __IO uint32_t count = 0U;
2759 uint32_t response = 0U, validvoltage = 0U;
2760 uint32_t errorstate;
2761
2762 /* CMD0: GO_IDLE_STATE */
2763 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2764 if(errorstate != HAL_SD_ERROR_NONE)
2765 {
2766 return errorstate;
2767 }
2768
2769 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2770 errorstate = SDMMC_CmdOperCond(hsd->Instance);
2771 if(errorstate != HAL_SD_ERROR_NONE)
2772 {
2773 hsd->SdCard.CardVersion = CARD_V1_X;
2774 /* CMD0: GO_IDLE_STATE */
2775 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2776 if(errorstate != HAL_SD_ERROR_NONE)
2777 {
2778 return errorstate;
2779 }
2780
2781 }
2782 else
2783 {
2784 hsd->SdCard.CardVersion = CARD_V2_X;
2785 }
2786
2787 if( hsd->SdCard.CardVersion == CARD_V2_X)
2788 {
2789 /* SEND CMD55 APP_CMD with RCA as 0 */
2790 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2791 if(errorstate != HAL_SD_ERROR_NONE)
2792 {
2793 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2794 }
2795 }
2796 /* SD CARD */
2797 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2798 while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
2799 {
2800 /* SEND CMD55 APP_CMD with RCA as 0 */
2801 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2802 if(errorstate != HAL_SD_ERROR_NONE)
2803 {
2804 return errorstate;
2805 }
2806
2807 /* Send CMD41 */
2808 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
2809 if(errorstate != HAL_SD_ERROR_NONE)
2810 {
2811 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2812 }
2813
2814 /* Get command response */
2815 response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2816
2817 /* Get operating voltage*/
2818 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
2819
2820 count++;
2821 }
2822
2823 if(count >= SDMMC_MAX_VOLT_TRIAL)
2824 {
2825 return HAL_SD_ERROR_INVALID_VOLTRANGE;
2826 }
2827
2828 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
2829 {
2830 hsd->SdCard.CardType = CARD_SDHC_SDXC;
2831 }
2832 else
2833 {
2834 hsd->SdCard.CardType = CARD_SDSC;
2835 }
2836
2837
2838 return HAL_SD_ERROR_NONE;
2839 }
2840
2841 /**
2842 * @brief Turns the SDIO output signals off.
2843 * @param hsd: Pointer to SD handle
2844 * @retval None
2845 */
2846 static void SD_PowerOFF(SD_HandleTypeDef *hsd)
2847 {
2848 /* Set Power State to OFF */
2849 (void)SDIO_PowerState_OFF(hsd->Instance);
2850 }
2851
2852 /**
2853 * @brief Send Status info command.
2854 * @param hsd: pointer to SD handle
2855 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
2856 * SD Status register)
2857 * @retval error state
2858 */
2859 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
2860 {
2861 SDIO_DataInitTypeDef config;
2862 uint32_t errorstate;
2863 uint32_t tickstart = HAL_GetTick();
2864 uint32_t count;
2865 uint32_t *pData = pSDstatus;
2866
2867 /* Check SD response */
2868 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2869 {
2870 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2871 }
2872
2873 /* Set block size for card if it is not equal to current block size for card */
2874 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
2875 if(errorstate != HAL_SD_ERROR_NONE)
2876 {
2877 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2878 return errorstate;
2879 }
2880
2881 /* Send CMD55 */
2882 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2883 if(errorstate != HAL_SD_ERROR_NONE)
2884 {
2885 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2886 return errorstate;
2887 }
2888
2889 /* Configure the SD DPSM (Data Path State Machine) */
2890 config.DataTimeOut = SDMMC_DATATIMEOUT;
2891 config.DataLength = 64U;
2892 config.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
2893 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
2894 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
2895 config.DPSM = SDIO_DPSM_ENABLE;
2896 (void)SDIO_ConfigData(hsd->Instance, &config);
2897
2898 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
2899 errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
2900 if(errorstate != HAL_SD_ERROR_NONE)
2901 {
2902 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2903 return errorstate;
2904 }
2905
2906 /* Get status data */
2907 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
2908 {
2909 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
2910 {
2911 for(count = 0U; count < 8U; count++)
2912 {
2913 *pData = SDIO_ReadFIFO(hsd->Instance);
2914 pData++;
2915 }
2916 }
2917
2918 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2919 {
2920 return HAL_SD_ERROR_TIMEOUT;
2921 }
2922 }
2923
2924 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
2925 {
2926 return HAL_SD_ERROR_DATA_TIMEOUT;
2927 }
2928 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
2929 {
2930 return HAL_SD_ERROR_DATA_CRC_FAIL;
2931 }
2932 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
2933 {
2934 return HAL_SD_ERROR_RX_OVERRUN;
2935 }
2936 else
2937 {
2938 /* Nothing to do */
2939 }
2940
2941 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)))
2942 {
2943 *pData = SDIO_ReadFIFO(hsd->Instance);
2944 pData++;
2945
2946 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2947 {
2948 return HAL_SD_ERROR_TIMEOUT;
2949 }
2950 }
2951
2952 /* Clear all the static status flags*/
2953 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2954
2955 return HAL_SD_ERROR_NONE;
2956 }
2957
2958 /**
2959 * @brief Returns the current card's status.
2960 * @param hsd: Pointer to SD handle
2961 * @param pCardStatus: pointer to the buffer that will contain the SD card
2962 * status (Card Status register)
2963 * @retval error state
2964 */
2965 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
2966 {
2967 uint32_t errorstate;
2968
2969 if(pCardStatus == NULL)
2970 {
2971 return HAL_SD_ERROR_PARAM;
2972 }
2973
2974 /* Send Status command */
2975 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2976 if(errorstate != HAL_SD_ERROR_NONE)
2977 {
2978 return errorstate;
2979 }
2980
2981 /* Get SD card status */
2982 *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2983
2984 return HAL_SD_ERROR_NONE;
2985 }
2986
2987 /**
2988 * @brief Enables the SDIO wide bus mode.
2989 * @param hsd: pointer to SD handle
2990 * @retval error state
2991 */
2992 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
2993 {
2994 uint32_t scr[2U] = {0U, 0U};
2995 uint32_t errorstate;
2996
2997 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2998 {
2999 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3000 }
3001
3002 /* Get SCR Register */
3003 errorstate = SD_FindSCR(hsd, scr);
3004 if(errorstate != HAL_SD_ERROR_NONE)
3005 {
3006 return errorstate;
3007 }
3008
3009 /* If requested card supports wide bus operation */
3010 if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
3011 {
3012 /* Send CMD55 APP_CMD with argument as card's RCA.*/
3013 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3014 if(errorstate != HAL_SD_ERROR_NONE)
3015 {
3016 return errorstate;
3017 }
3018
3019 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3020 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
3021 if(errorstate != HAL_SD_ERROR_NONE)
3022 {
3023 return errorstate;
3024 }
3025
3026 return HAL_SD_ERROR_NONE;
3027 }
3028 else
3029 {
3030 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3031 }
3032 }
3033
3034 /**
3035 * @brief Disables the SDIO wide bus mode.
3036 * @param hsd: Pointer to SD handle
3037 * @retval error state
3038 */
3039 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3040 {
3041 uint32_t scr[2U] = {0U, 0U};
3042 uint32_t errorstate;
3043
3044 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3045 {
3046 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3047 }
3048
3049 /* Get SCR Register */
3050 errorstate = SD_FindSCR(hsd, scr);
3051 if(errorstate != HAL_SD_ERROR_NONE)
3052 {
3053 return errorstate;
3054 }
3055
3056 /* If requested card supports 1 bit mode operation */
3057 if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
3058 {
3059 /* Send CMD55 APP_CMD with argument as card's RCA */
3060 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3061 if(errorstate != HAL_SD_ERROR_NONE)
3062 {
3063 return errorstate;
3064 }
3065
3066 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3067 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
3068 if(errorstate != HAL_SD_ERROR_NONE)
3069 {
3070 return errorstate;
3071 }
3072
3073 return HAL_SD_ERROR_NONE;
3074 }
3075 else
3076 {
3077 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3078 }
3079 }
3080
3081
3082 /**
3083 * @brief Finds the SD card SCR register value.
3084 * @param hsd: Pointer to SD handle
3085 * @param pSCR: pointer to the buffer that will contain the SCR value
3086 * @retval error state
3087 */
3088 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
3089 {
3090 SDIO_DataInitTypeDef config;
3091 uint32_t errorstate;
3092 uint32_t tickstart = HAL_GetTick();
3093 uint32_t index = 0U;
3094 uint32_t tempscr[2U] = {0U, 0U};
3095 uint32_t *scr = pSCR;
3096
3097 /* Set Block Size To 8 Bytes */
3098 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
3099 if(errorstate != HAL_SD_ERROR_NONE)
3100 {
3101 return errorstate;
3102 }
3103
3104 /* Send CMD55 APP_CMD with argument as card's RCA */
3105 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
3106 if(errorstate != HAL_SD_ERROR_NONE)
3107 {
3108 return errorstate;
3109 }
3110
3111 config.DataTimeOut = SDMMC_DATATIMEOUT;
3112 config.DataLength = 8U;
3113 config.DataBlockSize = SDIO_DATABLOCK_SIZE_8B;
3114 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
3115 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
3116 config.DPSM = SDIO_DPSM_ENABLE;
3117 (void)SDIO_ConfigData(hsd->Instance, &config);
3118
3119 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3120 errorstate = SDMMC_CmdSendSCR(hsd->Instance);
3121 if(errorstate != HAL_SD_ERROR_NONE)
3122 {
3123 return errorstate;
3124 }
3125
3126 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
3127 {
3128 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
3129 {
3130 *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
3131 index++;
3132 }
3133
3134 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3135 {
3136 return HAL_SD_ERROR_TIMEOUT;
3137 }
3138 }
3139
3140 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
3141 {
3142 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
3143
3144 return HAL_SD_ERROR_DATA_TIMEOUT;
3145 }
3146 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
3147 {
3148 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
3149
3150 return HAL_SD_ERROR_DATA_CRC_FAIL;
3151 }
3152 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
3153 {
3154 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
3155
3156 return HAL_SD_ERROR_RX_OVERRUN;
3157 }
3158 else
3159 {
3160 /* No error flag set */
3161 /* Clear all the static flags */
3162 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
3163
3164 *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
3165 ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24));
3166 scr++;
3167 *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
3168 ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24));
3169
3170 }
3171
3172 return HAL_SD_ERROR_NONE;
3173 }
3174
3175 /**
3176 * @brief Wrap up reading in non-blocking mode.
3177 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3178 * the configuration information.
3179 * @retval None
3180 */
3181 static void SD_Read_IT(SD_HandleTypeDef *hsd)
3182 {
3183 uint32_t count, data, dataremaining;
3184 uint8_t* tmp;
3185
3186 tmp = hsd->pRxBuffPtr;
3187 dataremaining = hsd->RxXferSize;
3188
3189 if (dataremaining > 0U)
3190 {
3191 /* Read data from SDIO Rx FIFO */
3192 for(count = 0U; count < 8U; count++)
3193 {
3194 data = SDIO_ReadFIFO(hsd->Instance);
3195 *tmp = (uint8_t)(data & 0xFFU);
3196 tmp++;
3197 dataremaining--;
3198 *tmp = (uint8_t)((data >> 8U) & 0xFFU);
3199 tmp++;
3200 dataremaining--;
3201 *tmp = (uint8_t)((data >> 16U) & 0xFFU);
3202 tmp++;
3203 dataremaining--;
3204 *tmp = (uint8_t)((data >> 24U) & 0xFFU);
3205 tmp++;
3206 dataremaining--;
3207 }
3208
3209 hsd->pRxBuffPtr = tmp;
3210 hsd->RxXferSize = dataremaining;
3211 }
3212 }
3213
3214 /**
3215 * @brief Wrap up writing in non-blocking mode.
3216 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3217 * the configuration information.
3218 * @retval None
3219 */
3220 static void SD_Write_IT(SD_HandleTypeDef *hsd)
3221 {
3222 uint32_t count, data, dataremaining;
3223 uint8_t* tmp;
3224
3225 tmp = hsd->pTxBuffPtr;
3226 dataremaining = hsd->TxXferSize;
3227
3228 if (dataremaining > 0U)
3229 {
3230 /* Write data to SDIO Tx FIFO */
3231 for(count = 0U; count < 8U; count++)
3232 {
3233 data = (uint32_t)(*tmp);
3234 tmp++;
3235 dataremaining--;
3236 data |= ((uint32_t)(*tmp) << 8U);
3237 tmp++;
3238 dataremaining--;
3239 data |= ((uint32_t)(*tmp) << 16U);
3240 tmp++;
3241 dataremaining--;
3242 data |= ((uint32_t)(*tmp) << 24U);
3243 tmp++;
3244 dataremaining--;
3245 (void)SDIO_WriteFIFO(hsd->Instance, &data);
3246 }
3247
3248 hsd->pTxBuffPtr = tmp;
3249 hsd->TxXferSize = dataremaining;
3250 }
3251 }
3252
3253 /**
3254 * @}
3255 */
3256
3257 #endif /* HAL_SD_MODULE_ENABLED */
3258
3259 /**
3260 * @}
3261 */
3262
3263 /**
3264 * @}
3265 */
3266
3267 #endif /* SDIO */
3268
3269 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/