source: S-port/trunk/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 102.4 KB
Line 
1/**
2 ******************************************************************************
3 * @file stm32f4xx_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) 2017 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 "stm32f4xx_hal.h"
260
261#if defined(SDIO)
262
263/** @addtogroup STM32F4xx_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 */
290static uint32_t SD_InitCard(SD_HandleTypeDef *hsd);
291static uint32_t SD_PowerON(SD_HandleTypeDef *hsd);
292static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
293static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
294static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd);
295static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
296static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
297static void SD_PowerOFF(SD_HandleTypeDef *hsd);
298static void SD_Write_IT(SD_HandleTypeDef *hsd);
299static void SD_Read_IT(SD_HandleTypeDef *hsd);
300static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
301static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
302static void SD_DMAError(DMA_HandleTypeDef *hdma);
303static void SD_DMATxAbort(DMA_HandleTypeDef *hdma);
304static 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 */
335HAL_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 */
403HAL_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 /* Identify card operating voltage */
434 errorstate = SD_PowerON(hsd);
435 if(errorstate != HAL_SD_ERROR_NONE)
436 {
437 hsd->State = HAL_SD_STATE_READY;
438 hsd->ErrorCode |= errorstate;
439 return HAL_ERROR;
440 }
441
442 /* Card initialization */
443 errorstate = SD_InitCard(hsd);
444 if(errorstate != HAL_SD_ERROR_NONE)
445 {
446 hsd->State = HAL_SD_STATE_READY;
447 hsd->ErrorCode |= errorstate;
448 return HAL_ERROR;
449 }
450
451 /* Set Block Size for Card */
452 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
453 if(errorstate != HAL_SD_ERROR_NONE)
454 {
455 /* Clear all the static flags */
456 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
457 hsd->ErrorCode |= errorstate;
458 hsd->State = HAL_SD_STATE_READY;
459 return HAL_ERROR;
460 }
461
462 return HAL_OK;
463}
464
465/**
466 * @brief De-Initializes the SD card.
467 * @param hsd: Pointer to SD handle
468 * @retval HAL status
469 */
470HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
471{
472 /* Check the SD handle allocation */
473 if(hsd == NULL)
474 {
475 return HAL_ERROR;
476 }
477
478 /* Check the parameters */
479 assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance));
480
481 hsd->State = HAL_SD_STATE_BUSY;
482
483 /* Set SD power state to off */
484 SD_PowerOFF(hsd);
485
486#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
487 if(hsd->MspDeInitCallback == NULL)
488 {
489 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
490 }
491
492 /* DeInit the low level hardware */
493 hsd->MspDeInitCallback(hsd);
494#else
495 /* De-Initialize the MSP layer */
496 HAL_SD_MspDeInit(hsd);
497#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
498
499 hsd->ErrorCode = HAL_SD_ERROR_NONE;
500 hsd->State = HAL_SD_STATE_RESET;
501
502 return HAL_OK;
503}
504
505
506/**
507 * @brief Initializes the SD MSP.
508 * @param hsd: Pointer to SD handle
509 * @retval None
510 */
511__weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
512{
513 /* Prevent unused argument(s) compilation warning */
514 UNUSED(hsd);
515
516 /* NOTE : This function should not be modified, when the callback is needed,
517 the HAL_SD_MspInit could be implemented in the user file
518 */
519}
520
521/**
522 * @brief De-Initialize SD MSP.
523 * @param hsd: Pointer to SD handle
524 * @retval None
525 */
526__weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
527{
528 /* Prevent unused argument(s) compilation warning */
529 UNUSED(hsd);
530
531 /* NOTE : This function should not be modified, when the callback is needed,
532 the HAL_SD_MspDeInit could be implemented in the user file
533 */
534}
535
536/**
537 * @}
538 */
539
540/** @addtogroup SD_Exported_Functions_Group2
541 * @brief Data transfer functions
542 *
543@verbatim
544 ==============================================================================
545 ##### IO operation functions #####
546 ==============================================================================
547 [..]
548 This subsection provides a set of functions allowing to manage the data
549 transfer from/to SD card.
550
551@endverbatim
552 * @{
553 */
554
555/**
556 * @brief Reads block(s) from a specified address in a card. The Data transfer
557 * is managed by polling mode.
558 * @note This API should be followed by a check on the card state through
559 * HAL_SD_GetCardState().
560 * @param hsd: Pointer to SD handle
561 * @param pData: pointer to the buffer that will contain the received data
562 * @param BlockAdd: Block Address from where data is to be read
563 * @param NumberOfBlocks: Number of SD blocks to read
564 * @param Timeout: Specify timeout value
565 * @retval HAL status
566 */
567HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
568{
569 SDIO_DataInitTypeDef config;
570 uint32_t errorstate;
571 uint32_t tickstart = HAL_GetTick();
572 uint32_t count, data, dataremaining;
573 uint32_t add = BlockAdd;
574 uint8_t *tempbuff = pData;
575
576 if(NULL == pData)
577 {
578 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
579 return HAL_ERROR;
580 }
581
582 if(hsd->State == HAL_SD_STATE_READY)
583 {
584 hsd->ErrorCode = HAL_SD_ERROR_NONE;
585
586 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
587 {
588 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
589 return HAL_ERROR;
590 }
591
592 hsd->State = HAL_SD_STATE_BUSY;
593
594 /* Initialize data control register */
595 hsd->Instance->DCTRL = 0U;
596
597 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
598 {
599 add *= 512U;
600 }
601
602 /* Configure the SD DPSM (Data Path State Machine) */
603 config.DataTimeOut = SDMMC_DATATIMEOUT;
604 config.DataLength = NumberOfBlocks * BLOCKSIZE;
605 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
606 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
607 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
608 config.DPSM = SDIO_DPSM_ENABLE;
609 (void)SDIO_ConfigData(hsd->Instance, &config);
610
611 /* Read block(s) in polling mode */
612 if(NumberOfBlocks > 1U)
613 {
614 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
615
616 /* Read Multi Block command */
617 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
618 }
619 else
620 {
621 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
622
623 /* Read Single Block command */
624 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
625 }
626 if(errorstate != HAL_SD_ERROR_NONE)
627 {
628 /* Clear all the static flags */
629 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
630 hsd->ErrorCode |= errorstate;
631 hsd->State = HAL_SD_STATE_READY;
632 hsd->Context = SD_CONTEXT_NONE;
633 return HAL_ERROR;
634 }
635
636 /* Poll on SDIO flags */
637 dataremaining = config.DataLength;
638#if defined(SDIO_STA_STBITERR)
639 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
640#else /* SDIO_STA_STBITERR not defined */
641 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
642#endif /* SDIO_STA_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 */
782HAL_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 /* Configure the SD DPSM (Data Path State Machine) */
818 config.DataTimeOut = SDMMC_DATATIMEOUT;
819 config.DataLength = NumberOfBlocks * BLOCKSIZE;
820 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
821 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
822 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
823 config.DPSM = SDIO_DPSM_ENABLE;
824 (void)SDIO_ConfigData(hsd->Instance, &config);
825
826 /* Write Blocks in Polling mode */
827 if(NumberOfBlocks > 1U)
828 {
829 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
830
831 /* Write Multi Block command */
832 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
833 }
834 else
835 {
836 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
837
838 /* Write Single Block command */
839 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
840 }
841 if(errorstate != HAL_SD_ERROR_NONE)
842 {
843 /* Clear all the static flags */
844 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
845 hsd->ErrorCode |= errorstate;
846 hsd->State = HAL_SD_STATE_READY;
847 hsd->Context = SD_CONTEXT_NONE;
848 return HAL_ERROR;
849 }
850
851 /* Write block(s) in polling mode */
852 dataremaining = config.DataLength;
853#if defined(SDIO_STA_STBITERR)
854 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
855#else /* SDIO_STA_STBITERR not defined */
856 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND))
857#endif /* SDIO_STA_STBITERR */
858 {
859 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) && (dataremaining > 0U))
860 {
861 /* Write data to SDIO Tx FIFO */
862 for(count = 0U; count < 8U; count++)
863 {
864 data = (uint32_t)(*tempbuff);
865 tempbuff++;
866 dataremaining--;
867 data |= ((uint32_t)(*tempbuff) << 8U);
868 tempbuff++;
869 dataremaining--;
870 data |= ((uint32_t)(*tempbuff) << 16U);
871 tempbuff++;
872 dataremaining--;
873 data |= ((uint32_t)(*tempbuff) << 24U);
874 tempbuff++;
875 dataremaining--;
876 (void)SDIO_WriteFIFO(hsd->Instance, &data);
877 }
878 }
879
880 if(((HAL_GetTick()-tickstart) >= Timeout) || (Timeout == 0U))
881 {
882 /* Clear all the static flags */
883 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
884 hsd->ErrorCode |= errorstate;
885 hsd->State = HAL_SD_STATE_READY;
886 hsd->Context = SD_CONTEXT_NONE;
887 return HAL_TIMEOUT;
888 }
889 }
890
891 /* Send stop transmission command in case of multiblock write */
892 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
893 {
894 if(hsd->SdCard.CardType != CARD_SECURED)
895 {
896 /* Send stop transmission command */
897 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
898 if(errorstate != HAL_SD_ERROR_NONE)
899 {
900 /* Clear all the static flags */
901 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
902 hsd->ErrorCode |= errorstate;
903 hsd->State = HAL_SD_STATE_READY;
904 hsd->Context = SD_CONTEXT_NONE;
905 return HAL_ERROR;
906 }
907 }
908 }
909
910 /* Get error state */
911 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
912 {
913 /* Clear all the static flags */
914 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
915 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
916 hsd->State = HAL_SD_STATE_READY;
917 hsd->Context = SD_CONTEXT_NONE;
918 return HAL_ERROR;
919 }
920 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
921 {
922 /* Clear all the static flags */
923 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
924 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
925 hsd->State = HAL_SD_STATE_READY;
926 hsd->Context = SD_CONTEXT_NONE;
927 return HAL_ERROR;
928 }
929 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
930 {
931 /* Clear all the static flags */
932 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
933 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
934 hsd->State = HAL_SD_STATE_READY;
935 hsd->Context = SD_CONTEXT_NONE;
936 return HAL_ERROR;
937 }
938 else
939 {
940 /* Nothing to do */
941 }
942
943 /* Clear all the static flags */
944 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
945
946 hsd->State = HAL_SD_STATE_READY;
947
948 return HAL_OK;
949 }
950 else
951 {
952 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
953 return HAL_ERROR;
954 }
955}
956
957/**
958 * @brief Reads block(s) from a specified address in a card. The Data transfer
959 * is managed in interrupt mode.
960 * @note This API should be followed by a check on the card state through
961 * HAL_SD_GetCardState().
962 * @note You could also check the IT transfer process through the SD Rx
963 * interrupt event.
964 * @param hsd: Pointer to SD handle
965 * @param pData: Pointer to the buffer that will contain the received data
966 * @param BlockAdd: Block Address from where data is to be read
967 * @param NumberOfBlocks: Number of blocks to read.
968 * @retval HAL status
969 */
970HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
971{
972 SDIO_DataInitTypeDef config;
973 uint32_t errorstate;
974 uint32_t add = BlockAdd;
975
976 if(NULL == pData)
977 {
978 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
979 return HAL_ERROR;
980 }
981
982 if(hsd->State == HAL_SD_STATE_READY)
983 {
984 hsd->ErrorCode = HAL_SD_ERROR_NONE;
985
986 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
987 {
988 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
989 return HAL_ERROR;
990 }
991
992 hsd->State = HAL_SD_STATE_BUSY;
993
994 /* Initialize data control register */
995 hsd->Instance->DCTRL = 0U;
996
997 hsd->pRxBuffPtr = pData;
998 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
999
1000#if defined(SDIO_STA_STBITERR)
1001 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF | SDIO_IT_STBITERR));
1002#else /* SDIO_STA_STBITERR not defined */
1003 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF));
1004#endif /* SDIO_STA_STBITERR */
1005
1006 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1007 {
1008 add *= 512U;
1009 }
1010
1011 /* Configure the SD DPSM (Data Path State Machine) */
1012 config.DataTimeOut = SDMMC_DATATIMEOUT;
1013 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1014 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1015 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
1016 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1017 config.DPSM = SDIO_DPSM_ENABLE;
1018 (void)SDIO_ConfigData(hsd->Instance, &config);
1019
1020 /* Read Blocks in IT mode */
1021 if(NumberOfBlocks > 1U)
1022 {
1023 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
1024
1025 /* Read Multi Block command */
1026 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1027 }
1028 else
1029 {
1030 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
1031
1032 /* Read Single Block command */
1033 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1034 }
1035 if(errorstate != HAL_SD_ERROR_NONE)
1036 {
1037 /* Clear all the static flags */
1038 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1039 hsd->ErrorCode |= errorstate;
1040 hsd->State = HAL_SD_STATE_READY;
1041 hsd->Context = SD_CONTEXT_NONE;
1042 return HAL_ERROR;
1043 }
1044
1045 return HAL_OK;
1046 }
1047 else
1048 {
1049 return HAL_BUSY;
1050 }
1051}
1052
1053/**
1054 * @brief Writes block(s) to a specified address in a card. The Data transfer
1055 * is managed in interrupt mode.
1056 * @note This API should be followed by a check on the card state through
1057 * HAL_SD_GetCardState().
1058 * @note You could also check the IT transfer process through the SD Tx
1059 * interrupt event.
1060 * @param hsd: Pointer to SD handle
1061 * @param pData: Pointer to the buffer that will contain the data to transmit
1062 * @param BlockAdd: Block Address where data will be written
1063 * @param NumberOfBlocks: Number of blocks to write
1064 * @retval HAL status
1065 */
1066HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1067{
1068 SDIO_DataInitTypeDef config;
1069 uint32_t errorstate;
1070 uint32_t add = BlockAdd;
1071
1072 if(NULL == pData)
1073 {
1074 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1075 return HAL_ERROR;
1076 }
1077
1078 if(hsd->State == HAL_SD_STATE_READY)
1079 {
1080 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1081
1082 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1083 {
1084 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1085 return HAL_ERROR;
1086 }
1087
1088 hsd->State = HAL_SD_STATE_BUSY;
1089
1090 /* Initialize data control register */
1091 hsd->Instance->DCTRL = 0U;
1092
1093 hsd->pTxBuffPtr = pData;
1094 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
1095
1096 /* Enable transfer interrupts */
1097#if defined(SDIO_STA_STBITERR)
1098 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_STBITERR));
1099#else /* SDIO_STA_STBITERR not defined */
1100 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE));
1101#endif /* SDIO_STA_STBITERR */
1102
1103 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1104 {
1105 add *= 512U;
1106 }
1107
1108 /* Write Blocks in Polling mode */
1109 if(NumberOfBlocks > 1U)
1110 {
1111 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1112
1113 /* Write Multi Block command */
1114 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1115 }
1116 else
1117 {
1118 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1119
1120 /* Write Single Block command */
1121 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1122 }
1123 if(errorstate != HAL_SD_ERROR_NONE)
1124 {
1125 /* Clear all the static flags */
1126 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1127 hsd->ErrorCode |= errorstate;
1128 hsd->State = HAL_SD_STATE_READY;
1129 hsd->Context = SD_CONTEXT_NONE;
1130 return HAL_ERROR;
1131 }
1132
1133 /* Configure the SD DPSM (Data Path State Machine) */
1134 config.DataTimeOut = SDMMC_DATATIMEOUT;
1135 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1136 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1137 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
1138 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1139 config.DPSM = SDIO_DPSM_ENABLE;
1140 (void)SDIO_ConfigData(hsd->Instance, &config);
1141
1142 return HAL_OK;
1143 }
1144 else
1145 {
1146 return HAL_BUSY;
1147 }
1148}
1149
1150/**
1151 * @brief Reads block(s) from a specified address in a card. The Data transfer
1152 * is managed by DMA mode.
1153 * @note This API should be followed by a check on the card state through
1154 * HAL_SD_GetCardState().
1155 * @note You could also check the DMA transfer process through the SD Rx
1156 * interrupt event.
1157 * @param hsd: Pointer SD handle
1158 * @param pData: Pointer to the buffer that will contain the received data
1159 * @param BlockAdd: Block Address from where data is to be read
1160 * @param NumberOfBlocks: Number of blocks to read.
1161 * @retval HAL status
1162 */
1163HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1164{
1165 SDIO_DataInitTypeDef config;
1166 uint32_t errorstate;
1167 uint32_t add = BlockAdd;
1168
1169 if(NULL == pData)
1170 {
1171 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1172 return HAL_ERROR;
1173 }
1174
1175 if(hsd->State == HAL_SD_STATE_READY)
1176 {
1177 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1178
1179 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1180 {
1181 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1182 return HAL_ERROR;
1183 }
1184
1185 hsd->State = HAL_SD_STATE_BUSY;
1186
1187 /* Initialize data control register */
1188 hsd->Instance->DCTRL = 0U;
1189
1190#if defined(SDIO_STA_STBITERR)
1191 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_IT_STBITERR));
1192#else /* SDIO_STA_STBITERR not defined */
1193 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
1194#endif /* SDIO_STA_STBITERR */
1195
1196 /* Set the DMA transfer complete callback */
1197 hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
1198
1199 /* Set the DMA error callback */
1200 hsd->hdmarx->XferErrorCallback = SD_DMAError;
1201
1202 /* Set the DMA Abort callback */
1203 hsd->hdmarx->XferAbortCallback = NULL;
1204
1205 /* Force DMA Direction */
1206 hsd->hdmarx->Init.Direction = DMA_PERIPH_TO_MEMORY;
1207 MODIFY_REG(hsd->hdmarx->Instance->CR, DMA_SxCR_DIR, hsd->hdmarx->Init.Direction);
1208
1209 /* Enable the DMA Channel */
1210 if(HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1211 {
1212 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND));
1213 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1214 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1215 hsd->State = HAL_SD_STATE_READY;
1216 return HAL_ERROR;
1217 }
1218 else
1219 {
1220 /* Enable SD DMA transfer */
1221 __HAL_SD_DMA_ENABLE(hsd);
1222
1223 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1224 {
1225 add *= 512U;
1226 }
1227
1228 /* Configure the SD DPSM (Data Path State Machine) */
1229 config.DataTimeOut = SDMMC_DATATIMEOUT;
1230 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1231 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1232 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
1233 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1234 config.DPSM = SDIO_DPSM_ENABLE;
1235 (void)SDIO_ConfigData(hsd->Instance, &config);
1236
1237 /* Read Blocks in DMA mode */
1238 if(NumberOfBlocks > 1U)
1239 {
1240 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1241
1242 /* Read Multi Block command */
1243 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, add);
1244 }
1245 else
1246 {
1247 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1248
1249 /* Read Single Block command */
1250 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, add);
1251 }
1252 if(errorstate != HAL_SD_ERROR_NONE)
1253 {
1254 /* Clear all the static flags */
1255 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1256 hsd->ErrorCode |= errorstate;
1257 hsd->State = HAL_SD_STATE_READY;
1258 hsd->Context = SD_CONTEXT_NONE;
1259 return HAL_ERROR;
1260 }
1261
1262 return HAL_OK;
1263 }
1264 }
1265 else
1266 {
1267 return HAL_BUSY;
1268 }
1269}
1270
1271/**
1272 * @brief Writes block(s) to a specified address in a card. The Data transfer
1273 * is managed by DMA mode.
1274 * @note This API should be followed by a check on the card state through
1275 * HAL_SD_GetCardState().
1276 * @note You could also check the DMA transfer process through the SD Tx
1277 * interrupt event.
1278 * @param hsd: Pointer to SD handle
1279 * @param pData: Pointer to the buffer that will contain the data to transmit
1280 * @param BlockAdd: Block Address where data will be written
1281 * @param NumberOfBlocks: Number of blocks to write
1282 * @retval HAL status
1283 */
1284HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1285{
1286 SDIO_DataInitTypeDef config;
1287 uint32_t errorstate;
1288 uint32_t add = BlockAdd;
1289
1290 if(NULL == pData)
1291 {
1292 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1293 return HAL_ERROR;
1294 }
1295
1296 if(hsd->State == HAL_SD_STATE_READY)
1297 {
1298 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1299
1300 if((add + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1301 {
1302 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1303 return HAL_ERROR;
1304 }
1305
1306 hsd->State = HAL_SD_STATE_BUSY;
1307
1308 /* Initialize data control register */
1309 hsd->Instance->DCTRL = 0U;
1310
1311 /* Enable SD Error interrupts */
1312#if defined(SDIO_STA_STBITERR)
1313 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));
1314#else /* SDIO_STA_STBITERR not defined */
1315 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
1316#endif /* SDIO_STA_STBITERR */
1317
1318 /* Set the DMA transfer complete callback */
1319 hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
1320
1321 /* Set the DMA error callback */
1322 hsd->hdmatx->XferErrorCallback = SD_DMAError;
1323
1324 /* Set the DMA Abort callback */
1325 hsd->hdmatx->XferAbortCallback = NULL;
1326
1327 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1328 {
1329 add *= 512U;
1330 }
1331
1332 /* Write Blocks in Polling mode */
1333 if(NumberOfBlocks > 1U)
1334 {
1335 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1336
1337 /* Write Multi Block command */
1338 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, add);
1339 }
1340 else
1341 {
1342 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1343
1344 /* Write Single Block command */
1345 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, add);
1346 }
1347 if(errorstate != HAL_SD_ERROR_NONE)
1348 {
1349 /* Clear all the static flags */
1350 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1351 hsd->ErrorCode |= errorstate;
1352 hsd->State = HAL_SD_STATE_READY;
1353 hsd->Context = SD_CONTEXT_NONE;
1354 return HAL_ERROR;
1355 }
1356
1357 /* Enable SDIO DMA transfer */
1358 __HAL_SD_DMA_ENABLE(hsd);
1359
1360 /* Force DMA Direction */
1361 hsd->hdmatx->Init.Direction = DMA_MEMORY_TO_PERIPH;
1362 MODIFY_REG(hsd->hdmatx->Instance->CR, DMA_SxCR_DIR, hsd->hdmatx->Init.Direction);
1363
1364 /* Enable the DMA Channel */
1365 if(HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4U) != HAL_OK)
1366 {
1367#if defined(SDIO_STA_STBITERR)
1368 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));
1369#else /* SDIO_STA_STBITERR not defined */
1370 __HAL_SD_DISABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR));
1371#endif /* SDIO_STA_STBITERR */
1372 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1373 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
1374 hsd->State = HAL_SD_STATE_READY;
1375 hsd->Context = SD_CONTEXT_NONE;
1376 return HAL_ERROR;
1377 }
1378 else
1379 {
1380 /* Configure the SD DPSM (Data Path State Machine) */
1381 config.DataTimeOut = SDMMC_DATATIMEOUT;
1382 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1383 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1384 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
1385 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1386 config.DPSM = SDIO_DPSM_ENABLE;
1387 (void)SDIO_ConfigData(hsd->Instance, &config);
1388
1389 return HAL_OK;
1390 }
1391 }
1392 else
1393 {
1394 return HAL_BUSY;
1395 }
1396}
1397
1398/**
1399 * @brief Erases the specified memory area of the given SD card.
1400 * @note This API should be followed by a check on the card state through
1401 * HAL_SD_GetCardState().
1402 * @param hsd: Pointer to SD handle
1403 * @param BlockStartAdd: Start Block address
1404 * @param BlockEndAdd: End Block address
1405 * @retval HAL status
1406 */
1407HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1408{
1409 uint32_t errorstate;
1410 uint32_t start_add = BlockStartAdd;
1411 uint32_t end_add = BlockEndAdd;
1412
1413 if(hsd->State == HAL_SD_STATE_READY)
1414 {
1415 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1416
1417 if(end_add < start_add)
1418 {
1419 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1420 return HAL_ERROR;
1421 }
1422
1423 if(end_add > (hsd->SdCard.LogBlockNbr))
1424 {
1425 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1426 return HAL_ERROR;
1427 }
1428
1429 hsd->State = HAL_SD_STATE_BUSY;
1430
1431 /* Check if the card command class supports erase command */
1432 if(((hsd->SdCard.Class) & SDIO_CCCC_ERASE) == 0U)
1433 {
1434 /* Clear all the static flags */
1435 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1436 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1437 hsd->State = HAL_SD_STATE_READY;
1438 return HAL_ERROR;
1439 }
1440
1441 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1442 {
1443 /* Clear all the static flags */
1444 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1445 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1446 hsd->State = HAL_SD_STATE_READY;
1447 return HAL_ERROR;
1448 }
1449
1450 /* Get start and end block for high capacity cards */
1451 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1452 {
1453 start_add *= 512U;
1454 end_add *= 512U;
1455 }
1456
1457 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1458 if(hsd->SdCard.CardType != CARD_SECURED)
1459 {
1460 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1461 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, start_add);
1462 if(errorstate != HAL_SD_ERROR_NONE)
1463 {
1464 /* Clear all the static flags */
1465 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1466 hsd->ErrorCode |= errorstate;
1467 hsd->State = HAL_SD_STATE_READY;
1468 return HAL_ERROR;
1469 }
1470
1471 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1472 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, end_add);
1473 if(errorstate != HAL_SD_ERROR_NONE)
1474 {
1475 /* Clear all the static flags */
1476 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1477 hsd->ErrorCode |= errorstate;
1478 hsd->State = HAL_SD_STATE_READY;
1479 return HAL_ERROR;
1480 }
1481 }
1482
1483 /* Send CMD38 ERASE */
1484 errorstate = SDMMC_CmdErase(hsd->Instance);
1485 if(errorstate != HAL_SD_ERROR_NONE)
1486 {
1487 /* Clear all the static flags */
1488 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1489 hsd->ErrorCode |= errorstate;
1490 hsd->State = HAL_SD_STATE_READY;
1491 return HAL_ERROR;
1492 }
1493
1494 hsd->State = HAL_SD_STATE_READY;
1495
1496 return HAL_OK;
1497 }
1498 else
1499 {
1500 return HAL_BUSY;
1501 }
1502}
1503
1504/**
1505 * @brief This function handles SD card interrupt request.
1506 * @param hsd: Pointer to SD handle
1507 * @retval None
1508 */
1509void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1510{
1511 uint32_t errorstate;
1512 uint32_t context = hsd->Context;
1513
1514 /* Check for SDIO interrupt flags */
1515 if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1516 {
1517 SD_Read_IT(hsd);
1518 }
1519
1520 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) != RESET)
1521 {
1522 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DATAEND);
1523
1524#if defined(SDIO_STA_STBITERR)
1525 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1526 SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR | SDIO_IT_TXFIFOHE |\
1527 SDIO_IT_RXFIFOHF | SDIO_IT_STBITERR);
1528#else /* SDIO_STA_STBITERR not defined */
1529 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1530 SDIO_IT_TXUNDERR | SDIO_IT_RXOVERR | SDIO_IT_TXFIFOHE |\
1531 SDIO_IT_RXFIFOHF);
1532#endif /* SDIO_STA_STBITERR */
1533
1534 hsd->Instance->DCTRL &= ~(SDIO_DCTRL_DTEN);
1535
1536 if((context & SD_CONTEXT_IT) != 0U)
1537 {
1538 if(((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1539 {
1540 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1541 if(errorstate != HAL_SD_ERROR_NONE)
1542 {
1543 hsd->ErrorCode |= errorstate;
1544#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1545 hsd->ErrorCallback(hsd);
1546#else
1547 HAL_SD_ErrorCallback(hsd);
1548#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1549 }
1550 }
1551
1552 /* Clear all the static flags */
1553 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
1554
1555 hsd->State = HAL_SD_STATE_READY;
1556 hsd->Context = SD_CONTEXT_NONE;
1557 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1558 {
1559#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1560 hsd->RxCpltCallback(hsd);
1561#else
1562 HAL_SD_RxCpltCallback(hsd);
1563#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1564 }
1565 else
1566 {
1567#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1568 hsd->TxCpltCallback(hsd);
1569#else
1570 HAL_SD_TxCpltCallback(hsd);
1571#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1572 }
1573 }
1574 else if((context & SD_CONTEXT_DMA) != 0U)
1575 {
1576 if((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U)
1577 {
1578 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1579 if(errorstate != HAL_SD_ERROR_NONE)
1580 {
1581 hsd->ErrorCode |= errorstate;
1582#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1583 hsd->ErrorCallback(hsd);
1584#else
1585 HAL_SD_ErrorCallback(hsd);
1586#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1587 }
1588 }
1589 if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) == 0U) && ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == 0U))
1590 {
1591 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1592 in the SD DCTRL register */
1593 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
1594
1595 hsd->State = HAL_SD_STATE_READY;
1596
1597#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1598 hsd->TxCpltCallback(hsd);
1599#else
1600 HAL_SD_TxCpltCallback(hsd);
1601#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1602 }
1603 }
1604 else
1605 {
1606 /* Nothing to do */
1607 }
1608 }
1609
1610 else if((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE) != RESET) && ((context & SD_CONTEXT_IT) != 0U))
1611 {
1612 SD_Write_IT(hsd);
1613 }
1614
1615#if defined(SDIO_STA_STBITERR)
1616 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR | SDIO_FLAG_STBITERR) != RESET)
1617#else /* SDIO_STA_STBITERR not defined */
1618 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_RXOVERR | SDIO_FLAG_TXUNDERR) != RESET)
1619#endif /* SDIO_STA_STBITERR */
1620 {
1621 /* Set Error code */
1622 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL) != RESET)
1623 {
1624 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1625 }
1626 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT) != RESET)
1627 {
1628 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1629 }
1630 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR) != RESET)
1631 {
1632 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1633 }
1634 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR) != RESET)
1635 {
1636 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1637 }
1638#if defined(SDIO_STA_STBITERR)
1639 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_STBITERR) != RESET)
1640 {
1641 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1642 }
1643#endif /* SDIO_STA_STBITERR */
1644
1645#if defined(SDIO_STA_STBITERR)
1646 /* Clear All flags */
1647 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS | SDIO_FLAG_STBITERR);
1648
1649 /* Disable all interrupts */
1650 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1651 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
1652#else /* SDIO_STA_STBITERR not defined */
1653 /* Clear All flags */
1654 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
1655
1656 /* Disable all interrupts */
1657 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1658 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
1659#endif /* SDIO_STA_STBITERR */
1660
1661 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
1662
1663 if((context & SD_CONTEXT_IT) != 0U)
1664 {
1665 /* Set the SD state to ready to be able to start again the process */
1666 hsd->State = HAL_SD_STATE_READY;
1667 hsd->Context = SD_CONTEXT_NONE;
1668#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1669 hsd->ErrorCallback(hsd);
1670#else
1671 HAL_SD_ErrorCallback(hsd);
1672#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1673 }
1674 else if((context & SD_CONTEXT_DMA) != 0U)
1675 {
1676 /* Abort the SD DMA channel */
1677 if(((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
1678 {
1679 /* Set the DMA Tx abort callback */
1680 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
1681 /* Abort DMA in IT mode */
1682 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
1683 {
1684 SD_DMATxAbort(hsd->hdmatx);
1685 }
1686 }
1687 else if(((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
1688 {
1689 /* Set the DMA Rx abort callback */
1690 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
1691 /* Abort DMA in IT mode */
1692 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
1693 {
1694 SD_DMARxAbort(hsd->hdmarx);
1695 }
1696 }
1697 else
1698 {
1699 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1700 hsd->State = HAL_SD_STATE_READY;
1701 hsd->Context = SD_CONTEXT_NONE;
1702#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1703 hsd->AbortCpltCallback(hsd);
1704#else
1705 HAL_SD_AbortCallback(hsd);
1706#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1707 }
1708 }
1709 else
1710 {
1711 /* Nothing to do */
1712 }
1713 }
1714 else
1715 {
1716 /* Nothing to do */
1717 }
1718}
1719
1720/**
1721 * @brief return the SD state
1722 * @param hsd: Pointer to sd handle
1723 * @retval HAL state
1724 */
1725HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
1726{
1727 return hsd->State;
1728}
1729
1730/**
1731* @brief Return the SD error code
1732* @param hsd : Pointer to a SD_HandleTypeDef structure that contains
1733 * the configuration information.
1734* @retval SD Error Code
1735*/
1736uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
1737{
1738 return hsd->ErrorCode;
1739}
1740
1741/**
1742 * @brief Tx Transfer completed callbacks
1743 * @param hsd: Pointer to SD handle
1744 * @retval None
1745 */
1746__weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
1747{
1748 /* Prevent unused argument(s) compilation warning */
1749 UNUSED(hsd);
1750
1751 /* NOTE : This function should not be modified, when the callback is needed,
1752 the HAL_SD_TxCpltCallback can be implemented in the user file
1753 */
1754}
1755
1756/**
1757 * @brief Rx Transfer completed callbacks
1758 * @param hsd: Pointer SD handle
1759 * @retval None
1760 */
1761__weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
1762{
1763 /* Prevent unused argument(s) compilation warning */
1764 UNUSED(hsd);
1765
1766 /* NOTE : This function should not be modified, when the callback is needed,
1767 the HAL_SD_RxCpltCallback can be implemented in the user file
1768 */
1769}
1770
1771/**
1772 * @brief SD error callbacks
1773 * @param hsd: Pointer SD handle
1774 * @retval None
1775 */
1776__weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1777{
1778 /* Prevent unused argument(s) compilation warning */
1779 UNUSED(hsd);
1780
1781 /* NOTE : This function should not be modified, when the callback is needed,
1782 the HAL_SD_ErrorCallback can be implemented in the user file
1783 */
1784}
1785
1786/**
1787 * @brief SD Abort callbacks
1788 * @param hsd: Pointer SD handle
1789 * @retval None
1790 */
1791__weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
1792{
1793 /* Prevent unused argument(s) compilation warning */
1794 UNUSED(hsd);
1795
1796 /* NOTE : This function should not be modified, when the callback is needed,
1797 the HAL_SD_AbortCallback can be implemented in the user file
1798 */
1799}
1800
1801#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
1802/**
1803 * @brief Register a User SD Callback
1804 * To be used instead of the weak (surcharged) predefined callback
1805 * @param hsd : SD handle
1806 * @param CallbackID : ID of the callback to be registered
1807 * This parameter can be one of the following values:
1808 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1809 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1810 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1811 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1812 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1813 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1814 * @param pCallback : pointer to the Callback function
1815 * @retval status
1816 */
1817HAL_StatusTypeDef HAL_SD_RegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID, pSD_CallbackTypeDef pCallback)
1818{
1819 HAL_StatusTypeDef status = HAL_OK;
1820
1821 if(pCallback == NULL)
1822 {
1823 /* Update the error code */
1824 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1825 return HAL_ERROR;
1826 }
1827
1828 /* Process locked */
1829 __HAL_LOCK(hsd);
1830
1831 if(hsd->State == HAL_SD_STATE_READY)
1832 {
1833 switch (CallbackID)
1834 {
1835 case HAL_SD_TX_CPLT_CB_ID :
1836 hsd->TxCpltCallback = pCallback;
1837 break;
1838 case HAL_SD_RX_CPLT_CB_ID :
1839 hsd->RxCpltCallback = pCallback;
1840 break;
1841 case HAL_SD_ERROR_CB_ID :
1842 hsd->ErrorCallback = pCallback;
1843 break;
1844 case HAL_SD_ABORT_CB_ID :
1845 hsd->AbortCpltCallback = pCallback;
1846 break;
1847 case HAL_SD_MSP_INIT_CB_ID :
1848 hsd->MspInitCallback = pCallback;
1849 break;
1850 case HAL_SD_MSP_DEINIT_CB_ID :
1851 hsd->MspDeInitCallback = pCallback;
1852 break;
1853 default :
1854 /* Update the error code */
1855 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1856 /* update return status */
1857 status = HAL_ERROR;
1858 break;
1859 }
1860 }
1861 else if (hsd->State == HAL_SD_STATE_RESET)
1862 {
1863 switch (CallbackID)
1864 {
1865 case HAL_SD_MSP_INIT_CB_ID :
1866 hsd->MspInitCallback = pCallback;
1867 break;
1868 case HAL_SD_MSP_DEINIT_CB_ID :
1869 hsd->MspDeInitCallback = pCallback;
1870 break;
1871 default :
1872 /* Update the error code */
1873 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1874 /* update return status */
1875 status = HAL_ERROR;
1876 break;
1877 }
1878 }
1879 else
1880 {
1881 /* Update the error code */
1882 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1883 /* update return status */
1884 status = HAL_ERROR;
1885 }
1886
1887 /* Release Lock */
1888 __HAL_UNLOCK(hsd);
1889 return status;
1890}
1891
1892/**
1893 * @brief Unregister a User SD Callback
1894 * SD Callback is redirected to the weak (surcharged) predefined callback
1895 * @param hsd : SD handle
1896 * @param CallbackID : ID of the callback to be unregistered
1897 * This parameter can be one of the following values:
1898 * @arg @ref HAL_SD_TX_CPLT_CB_ID SD Tx Complete Callback ID
1899 * @arg @ref HAL_SD_RX_CPLT_CB_ID SD Rx Complete Callback ID
1900 * @arg @ref HAL_SD_ERROR_CB_ID SD Error Callback ID
1901 * @arg @ref HAL_SD_ABORT_CB_ID SD Abort Callback ID
1902 * @arg @ref HAL_SD_MSP_INIT_CB_ID SD MspInit Callback ID
1903 * @arg @ref HAL_SD_MSP_DEINIT_CB_ID SD MspDeInit Callback ID
1904 * @retval status
1905 */
1906HAL_StatusTypeDef HAL_SD_UnRegisterCallback(SD_HandleTypeDef *hsd, HAL_SD_CallbackIDTypeDef CallbackID)
1907{
1908 HAL_StatusTypeDef status = HAL_OK;
1909
1910 /* Process locked */
1911 __HAL_LOCK(hsd);
1912
1913 if(hsd->State == HAL_SD_STATE_READY)
1914 {
1915 switch (CallbackID)
1916 {
1917 case HAL_SD_TX_CPLT_CB_ID :
1918 hsd->TxCpltCallback = HAL_SD_TxCpltCallback;
1919 break;
1920 case HAL_SD_RX_CPLT_CB_ID :
1921 hsd->RxCpltCallback = HAL_SD_RxCpltCallback;
1922 break;
1923 case HAL_SD_ERROR_CB_ID :
1924 hsd->ErrorCallback = HAL_SD_ErrorCallback;
1925 break;
1926 case HAL_SD_ABORT_CB_ID :
1927 hsd->AbortCpltCallback = HAL_SD_AbortCallback;
1928 break;
1929 case HAL_SD_MSP_INIT_CB_ID :
1930 hsd->MspInitCallback = HAL_SD_MspInit;
1931 break;
1932 case HAL_SD_MSP_DEINIT_CB_ID :
1933 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1934 break;
1935 default :
1936 /* Update the error code */
1937 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1938 /* update return status */
1939 status = HAL_ERROR;
1940 break;
1941 }
1942 }
1943 else if (hsd->State == HAL_SD_STATE_RESET)
1944 {
1945 switch (CallbackID)
1946 {
1947 case HAL_SD_MSP_INIT_CB_ID :
1948 hsd->MspInitCallback = HAL_SD_MspInit;
1949 break;
1950 case HAL_SD_MSP_DEINIT_CB_ID :
1951 hsd->MspDeInitCallback = HAL_SD_MspDeInit;
1952 break;
1953 default :
1954 /* Update the error code */
1955 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1956 /* update return status */
1957 status = HAL_ERROR;
1958 break;
1959 }
1960 }
1961 else
1962 {
1963 /* Update the error code */
1964 hsd->ErrorCode |= HAL_SD_ERROR_INVALID_CALLBACK;
1965 /* update return status */
1966 status = HAL_ERROR;
1967 }
1968
1969 /* Release Lock */
1970 __HAL_UNLOCK(hsd);
1971 return status;
1972}
1973#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
1974
1975/**
1976 * @}
1977 */
1978
1979/** @addtogroup SD_Exported_Functions_Group3
1980 * @brief management functions
1981 *
1982@verbatim
1983 ==============================================================================
1984 ##### Peripheral Control functions #####
1985 ==============================================================================
1986 [..]
1987 This subsection provides a set of functions allowing to control the SD card
1988 operations and get the related information
1989
1990@endverbatim
1991 * @{
1992 */
1993
1994/**
1995 * @brief Returns information the information of the card which are stored on
1996 * the CID register.
1997 * @param hsd: Pointer to SD handle
1998 * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that
1999 * contains all CID register parameters
2000 * @retval HAL status
2001 */
2002HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
2003{
2004 pCID->ManufacturerID = (uint8_t)((hsd->CID[0] & 0xFF000000U) >> 24U);
2005
2006 pCID->OEM_AppliID = (uint16_t)((hsd->CID[0] & 0x00FFFF00U) >> 8U);
2007
2008 pCID->ProdName1 = (((hsd->CID[0] & 0x000000FFU) << 24U) | ((hsd->CID[1] & 0xFFFFFF00U) >> 8U));
2009
2010 pCID->ProdName2 = (uint8_t)(hsd->CID[1] & 0x000000FFU);
2011
2012 pCID->ProdRev = (uint8_t)((hsd->CID[2] & 0xFF000000U) >> 24U);
2013
2014 pCID->ProdSN = (((hsd->CID[2] & 0x00FFFFFFU) << 8U) | ((hsd->CID[3] & 0xFF000000U) >> 24U));
2015
2016 pCID->Reserved1 = (uint8_t)((hsd->CID[3] & 0x00F00000U) >> 20U);
2017
2018 pCID->ManufactDate = (uint16_t)((hsd->CID[3] & 0x000FFF00U) >> 8U);
2019
2020 pCID->CID_CRC = (uint8_t)((hsd->CID[3] & 0x000000FEU) >> 1U);
2021
2022 pCID->Reserved2 = 1U;
2023
2024 return HAL_OK;
2025}
2026
2027/**
2028 * @brief Returns information the information of the card which are stored on
2029 * the CSD register.
2030 * @param hsd: Pointer to SD handle
2031 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
2032 * contains all CSD register parameters
2033 * @retval HAL status
2034 */
2035HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
2036{
2037 pCSD->CSDStruct = (uint8_t)((hsd->CSD[0] & 0xC0000000U) >> 30U);
2038
2039 pCSD->SysSpecVersion = (uint8_t)((hsd->CSD[0] & 0x3C000000U) >> 26U);
2040
2041 pCSD->Reserved1 = (uint8_t)((hsd->CSD[0] & 0x03000000U) >> 24U);
2042
2043 pCSD->TAAC = (uint8_t)((hsd->CSD[0] & 0x00FF0000U) >> 16U);
2044
2045 pCSD->NSAC = (uint8_t)((hsd->CSD[0] & 0x0000FF00U) >> 8U);
2046
2047 pCSD->MaxBusClkFrec = (uint8_t)(hsd->CSD[0] & 0x000000FFU);
2048
2049 pCSD->CardComdClasses = (uint16_t)((hsd->CSD[1] & 0xFFF00000U) >> 20U);
2050
2051 pCSD->RdBlockLen = (uint8_t)((hsd->CSD[1] & 0x000F0000U) >> 16U);
2052
2053 pCSD->PartBlockRead = (uint8_t)((hsd->CSD[1] & 0x00008000U) >> 15U);
2054
2055 pCSD->WrBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00004000U) >> 14U);
2056
2057 pCSD->RdBlockMisalign = (uint8_t)((hsd->CSD[1] & 0x00002000U) >> 13U);
2058
2059 pCSD->DSRImpl = (uint8_t)((hsd->CSD[1] & 0x00001000U) >> 12U);
2060
2061 pCSD->Reserved2 = 0U; /*!< Reserved */
2062
2063 if(hsd->SdCard.CardType == CARD_SDSC)
2064 {
2065 pCSD->DeviceSize = (((hsd->CSD[1] & 0x000003FFU) << 2U) | ((hsd->CSD[2] & 0xC0000000U) >> 30U));
2066
2067 pCSD->MaxRdCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x38000000U) >> 27U);
2068
2069 pCSD->MaxRdCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x07000000U) >> 24U);
2070
2071 pCSD->MaxWrCurrentVDDMin = (uint8_t)((hsd->CSD[2] & 0x00E00000U) >> 21U);
2072
2073 pCSD->MaxWrCurrentVDDMax = (uint8_t)((hsd->CSD[2] & 0x001C0000U) >> 18U);
2074
2075 pCSD->DeviceSizeMul = (uint8_t)((hsd->CSD[2] & 0x00038000U) >> 15U);
2076
2077 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
2078 hsd->SdCard.BlockNbr *= (1UL << ((pCSD->DeviceSizeMul & 0x07U) + 2U));
2079 hsd->SdCard.BlockSize = (1UL << (pCSD->RdBlockLen & 0x0FU));
2080
2081 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
2082 hsd->SdCard.LogBlockSize = 512U;
2083 }
2084 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
2085 {
2086 /* Byte 7 */
2087 pCSD->DeviceSize = (((hsd->CSD[1] & 0x0000003FU) << 16U) | ((hsd->CSD[2] & 0xFFFF0000U) >> 16U));
2088
2089 hsd->SdCard.BlockNbr = ((pCSD->DeviceSize + 1U) * 1024U);
2090 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr;
2091 hsd->SdCard.BlockSize = 512U;
2092 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize;
2093 }
2094 else
2095 {
2096 /* Clear all the static flags */
2097 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2098 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2099 hsd->State = HAL_SD_STATE_READY;
2100 return HAL_ERROR;
2101 }
2102
2103 pCSD->EraseGrSize = (uint8_t)((hsd->CSD[2] & 0x00004000U) >> 14U);
2104
2105 pCSD->EraseGrMul = (uint8_t)((hsd->CSD[2] & 0x00003F80U) >> 7U);
2106
2107 pCSD->WrProtectGrSize = (uint8_t)(hsd->CSD[2] & 0x0000007FU);
2108
2109 pCSD->WrProtectGrEnable = (uint8_t)((hsd->CSD[3] & 0x80000000U) >> 31U);
2110
2111 pCSD->ManDeflECC = (uint8_t)((hsd->CSD[3] & 0x60000000U) >> 29U);
2112
2113 pCSD->WrSpeedFact = (uint8_t)((hsd->CSD[3] & 0x1C000000U) >> 26U);
2114
2115 pCSD->MaxWrBlockLen= (uint8_t)((hsd->CSD[3] & 0x03C00000U) >> 22U);
2116
2117 pCSD->WriteBlockPaPartial = (uint8_t)((hsd->CSD[3] & 0x00200000U) >> 21U);
2118
2119 pCSD->Reserved3 = 0;
2120
2121 pCSD->ContentProtectAppli = (uint8_t)((hsd->CSD[3] & 0x00010000U) >> 16U);
2122
2123 pCSD->FileFormatGroup = (uint8_t)((hsd->CSD[3] & 0x00008000U) >> 15U);
2124
2125 pCSD->CopyFlag = (uint8_t)((hsd->CSD[3] & 0x00004000U) >> 14U);
2126
2127 pCSD->PermWrProtect = (uint8_t)((hsd->CSD[3] & 0x00002000U) >> 13U);
2128
2129 pCSD->TempWrProtect = (uint8_t)((hsd->CSD[3] & 0x00001000U) >> 12U);
2130
2131 pCSD->FileFormat = (uint8_t)((hsd->CSD[3] & 0x00000C00U) >> 10U);
2132
2133 pCSD->ECC= (uint8_t)((hsd->CSD[3] & 0x00000300U) >> 8U);
2134
2135 pCSD->CSD_CRC = (uint8_t)((hsd->CSD[3] & 0x000000FEU) >> 1U);
2136
2137 pCSD->Reserved4 = 1;
2138
2139 return HAL_OK;
2140}
2141
2142/**
2143 * @brief Gets the SD status info.
2144 * @param hsd: Pointer to SD handle
2145 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
2146 * will contain the SD card status information
2147 * @retval HAL status
2148 */
2149HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
2150{
2151 uint32_t sd_status[16];
2152 uint32_t errorstate;
2153 HAL_StatusTypeDef status = HAL_OK;
2154
2155 errorstate = SD_SendSDStatus(hsd, sd_status);
2156 if(errorstate != HAL_SD_ERROR_NONE)
2157 {
2158 /* Clear all the static flags */
2159 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2160 hsd->ErrorCode |= errorstate;
2161 hsd->State = HAL_SD_STATE_READY;
2162 status = HAL_ERROR;
2163 }
2164 else
2165 {
2166 pStatus->DataBusWidth = (uint8_t)((sd_status[0] & 0xC0U) >> 6U);
2167
2168 pStatus->SecuredMode = (uint8_t)((sd_status[0] & 0x20U) >> 5U);
2169
2170 pStatus->CardType = (uint16_t)(((sd_status[0] & 0x00FF0000U) >> 8U) | ((sd_status[0] & 0xFF000000U) >> 24U));
2171
2172 pStatus->ProtectedAreaSize = (((sd_status[1] & 0xFFU) << 24U) | ((sd_status[1] & 0xFF00U) << 8U) |
2173 ((sd_status[1] & 0xFF0000U) >> 8U) | ((sd_status[1] & 0xFF000000U) >> 24U));
2174
2175 pStatus->SpeedClass = (uint8_t)(sd_status[2] & 0xFFU);
2176
2177 pStatus->PerformanceMove = (uint8_t)((sd_status[2] & 0xFF00U) >> 8U);
2178
2179 pStatus->AllocationUnitSize = (uint8_t)((sd_status[2] & 0xF00000U) >> 20U);
2180
2181 pStatus->EraseSize = (uint16_t)(((sd_status[2] & 0xFF000000U) >> 16U) | (sd_status[3] & 0xFFU));
2182
2183 pStatus->EraseTimeout = (uint8_t)((sd_status[3] & 0xFC00U) >> 10U);
2184
2185 pStatus->EraseOffset = (uint8_t)((sd_status[3] & 0x0300U) >> 8U);
2186 }
2187
2188 /* Set Block Size for Card */
2189 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
2190 if(errorstate != HAL_SD_ERROR_NONE)
2191 {
2192 /* Clear all the static flags */
2193 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2194 hsd->ErrorCode = errorstate;
2195 hsd->State = HAL_SD_STATE_READY;
2196 status = HAL_ERROR;
2197 }
2198
2199 return status;
2200}
2201
2202/**
2203 * @brief Gets the SD card info.
2204 * @param hsd: Pointer to SD handle
2205 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
2206 * will contain the SD card status information
2207 * @retval HAL status
2208 */
2209HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
2210{
2211 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
2212 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
2213 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
2214 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
2215 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
2216 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
2217 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
2218 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
2219
2220 return HAL_OK;
2221}
2222
2223/**
2224 * @brief Enables wide bus operation for the requested card if supported by
2225 * card.
2226 * @param hsd: Pointer to SD handle
2227 * @param WideMode: Specifies the SD card wide bus mode
2228 * This parameter can be one of the following values:
2229 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
2230 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
2231 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
2232 * @retval HAL status
2233 */
2234HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
2235{
2236 SDIO_InitTypeDef Init;
2237 uint32_t errorstate;
2238 HAL_StatusTypeDef status = HAL_OK;
2239
2240 /* Check the parameters */
2241 assert_param(IS_SDIO_BUS_WIDE(WideMode));
2242
2243 /* Change State */
2244 hsd->State = HAL_SD_STATE_BUSY;
2245
2246 if(hsd->SdCard.CardType != CARD_SECURED)
2247 {
2248 if(WideMode == SDIO_BUS_WIDE_8B)
2249 {
2250 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2251 }
2252 else if(WideMode == SDIO_BUS_WIDE_4B)
2253 {
2254 errorstate = SD_WideBus_Enable(hsd);
2255
2256 hsd->ErrorCode |= errorstate;
2257 }
2258 else if(WideMode == SDIO_BUS_WIDE_1B)
2259 {
2260 errorstate = SD_WideBus_Disable(hsd);
2261
2262 hsd->ErrorCode |= errorstate;
2263 }
2264 else
2265 {
2266 /* WideMode is not a valid argument*/
2267 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2268 }
2269 }
2270 else
2271 {
2272 /* MMC Card does not support this feature */
2273 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2274 }
2275
2276 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2277 {
2278 /* Clear all the static flags */
2279 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2280 hsd->State = HAL_SD_STATE_READY;
2281 status = HAL_ERROR;
2282 }
2283 else
2284 {
2285 /* Configure the SDIO peripheral */
2286 Init.ClockEdge = hsd->Init.ClockEdge;
2287 Init.ClockBypass = hsd->Init.ClockBypass;
2288 Init.ClockPowerSave = hsd->Init.ClockPowerSave;
2289 Init.BusWide = WideMode;
2290 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2291 Init.ClockDiv = hsd->Init.ClockDiv;
2292 (void)SDIO_Init(hsd->Instance, Init);
2293 }
2294
2295 /* Set Block Size for Card */
2296 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
2297 if(errorstate != HAL_SD_ERROR_NONE)
2298 {
2299 /* Clear all the static flags */
2300 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2301 hsd->ErrorCode |= errorstate;
2302 status = HAL_ERROR;
2303 }
2304
2305 /* Change State */
2306 hsd->State = HAL_SD_STATE_READY;
2307
2308 return status;
2309}
2310
2311/**
2312 * @brief Gets the current sd card data state.
2313 * @param hsd: pointer to SD handle
2314 * @retval Card state
2315 */
2316HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2317{
2318 uint32_t cardstate;
2319 uint32_t errorstate;
2320 uint32_t resp1 = 0;
2321
2322 errorstate = SD_SendStatus(hsd, &resp1);
2323 if(errorstate != HAL_SD_ERROR_NONE)
2324 {
2325 hsd->ErrorCode |= errorstate;
2326 }
2327
2328 cardstate = ((resp1 >> 9U) & 0x0FU);
2329
2330 return (HAL_SD_CardStateTypeDef)cardstate;
2331}
2332
2333/**
2334 * @brief Abort the current transfer and disable the SD.
2335 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2336 * the configuration information for SD module.
2337 * @retval HAL status
2338 */
2339HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
2340{
2341 HAL_SD_CardStateTypeDef CardState;
2342 uint32_t context = hsd->Context;
2343
2344 /* DIsable All interrupts */
2345 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2346 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2347
2348 /* Clear All flags */
2349 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2350
2351 CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
2352
2353 if ((context & SD_CONTEXT_DMA) != 0U)
2354 {
2355 /* Disable the SD DMA request */
2356 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2357
2358 /* Abort the SD DMA Tx channel */
2359 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2360 {
2361 if(HAL_DMA_Abort(hsd->hdmatx) != HAL_OK)
2362 {
2363 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2364 }
2365 }
2366 /* Abort the SD DMA Rx channel */
2367 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2368 {
2369 if(HAL_DMA_Abort(hsd->hdmarx) != HAL_OK)
2370 {
2371 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2372 }
2373 }
2374 else
2375 {
2376 /* Nothing to do */
2377 }
2378 }
2379
2380 hsd->State = HAL_SD_STATE_READY;
2381
2382 /* Initialize the SD operation */
2383 hsd->Context = SD_CONTEXT_NONE;
2384
2385 CardState = HAL_SD_GetCardState(hsd);
2386 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2387 {
2388 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2389 }
2390 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2391 {
2392 return HAL_ERROR;
2393 }
2394 return HAL_OK;
2395}
2396
2397/**
2398 * @brief Abort the current transfer and disable the SD (IT mode).
2399 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2400 * the configuration information for SD module.
2401 * @retval HAL status
2402 */
2403HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
2404{
2405 HAL_SD_CardStateTypeDef CardState;
2406 uint32_t context = hsd->Context;
2407
2408 /* Disable All interrupts */
2409 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2410 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2411
2412 CLEAR_BIT(hsd->Instance->DCTRL, SDIO_DCTRL_DTEN);
2413
2414 if ((context & SD_CONTEXT_DMA) != 0U)
2415 {
2416 /* Disable the SD DMA request */
2417 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2418
2419 /* Abort the SD DMA Tx channel */
2420 if (((context & SD_CONTEXT_WRITE_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != 0U))
2421 {
2422 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
2423 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
2424 {
2425 hsd->hdmatx = NULL;
2426 }
2427 }
2428 /* Abort the SD DMA Rx channel */
2429 else if (((context & SD_CONTEXT_READ_SINGLE_BLOCK) != 0U) || ((context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != 0U))
2430 {
2431 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
2432 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
2433 {
2434 hsd->hdmarx = NULL;
2435 }
2436 }
2437 else
2438 {
2439 /* Nothing to do */
2440 }
2441 }
2442 /* No transfer ongoing on both DMA channels*/
2443 else
2444 {
2445 /* Clear All flags */
2446 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2447
2448 CardState = HAL_SD_GetCardState(hsd);
2449 hsd->State = HAL_SD_STATE_READY;
2450 hsd->Context = SD_CONTEXT_NONE;
2451 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2452 {
2453 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2454 }
2455 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2456 {
2457 return HAL_ERROR;
2458 }
2459 else
2460 {
2461#if defined (USE_HAL_SD_REGISTER_CALLBACKS) && (USE_HAL_SD_REGISTER_CALLBACKS == 1U)
2462 hsd->AbortCpltCallback(hsd);
2463#else
2464 HAL_SD_AbortCallback(hsd);
2465#endif /* USE_HAL_SD_REGISTER_CALLBACKS */
2466 }
2467 }
2468
2469 return HAL_OK;
2470}
2471
2472/**
2473 * @}
2474 */
2475
2476/**
2477 * @}
2478 */
2479
2480/* Private function ----------------------------------------------------------*/
2481/** @addtogroup SD_Private_Functions
2482 * @{
2483 */
2484
2485/**
2486 * @brief DMA SD transmit process complete callback
2487 * @param hdma: DMA handle
2488 * @retval None
2489 */
2490static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2491{
2492 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2493
2494 /* Enable DATAEND Interrupt */
2495 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND));
2496}
2497
2498/**
2499 * @brief DMA SD receive process complete callback
2500 * @param hdma: DMA handle
2501 * @retval None
2502 */
2503static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2504{
2505 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2506 uint32_t errorstate;
2507
2508 /* Send stop command in multiblock write */
2509 if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
2510 {
2511 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
2512 if(errorstate != HAL_SD_ERROR_NONE)
2513 {
2514 hsd->ErrorCode |= errorstate;
2515#if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2516 hsd->ErrorCallback(hsd);
2517#else
2518 HAL_SD_ErrorCallback(hsd);
2519#endif
2520 }
2521 }
2522
2523 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2524 in the SD DCTRL register */
2525 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2526
2527 /* Clear all the static flags */
2528 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2529
2530 hsd->State = HAL_SD_STATE_READY;
2531 hsd->Context = SD_CONTEXT_NONE;
2532
2533#if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2534 hsd->RxCpltCallback(hsd);
2535#else
2536 HAL_SD_RxCpltCallback(hsd);
2537#endif
2538}
2539
2540/**
2541 * @brief DMA SD communication error callback
2542 * @param hdma: DMA handle
2543 * @retval None
2544 */
2545static void SD_DMAError(DMA_HandleTypeDef *hdma)
2546{
2547 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2548 HAL_SD_CardStateTypeDef CardState;
2549 uint32_t RxErrorCode, TxErrorCode;
2550
2551 /* if DMA error is FIFO error ignore it */
2552 if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
2553 {
2554 RxErrorCode = hsd->hdmarx->ErrorCode;
2555 TxErrorCode = hsd->hdmatx->ErrorCode;
2556 if((RxErrorCode == HAL_DMA_ERROR_TE) || (TxErrorCode == HAL_DMA_ERROR_TE))
2557 {
2558 /* Clear All flags */
2559 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2560
2561 /* Disable All interrupts */
2562 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2563 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2564
2565 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2566 CardState = HAL_SD_GetCardState(hsd);
2567 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2568 {
2569 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2570 }
2571
2572 hsd->State= HAL_SD_STATE_READY;
2573 hsd->Context = SD_CONTEXT_NONE;
2574 }
2575
2576#if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2577 hsd->ErrorCallback(hsd);
2578#else
2579 HAL_SD_ErrorCallback(hsd);
2580#endif
2581 }
2582}
2583
2584/**
2585 * @brief DMA SD Tx Abort callback
2586 * @param hdma: DMA handle
2587 * @retval None
2588 */
2589static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
2590{
2591 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2592 HAL_SD_CardStateTypeDef CardState;
2593
2594 /* Clear All flags */
2595 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2596
2597 CardState = HAL_SD_GetCardState(hsd);
2598 hsd->State = HAL_SD_STATE_READY;
2599 hsd->Context = SD_CONTEXT_NONE;
2600 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2601 {
2602 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2603 }
2604
2605 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2606 {
2607#if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2608 hsd->AbortCpltCallback(hsd);
2609#else
2610 HAL_SD_AbortCallback(hsd);
2611#endif
2612 }
2613 else
2614 {
2615#if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2616 hsd->ErrorCallback(hsd);
2617#else
2618 HAL_SD_ErrorCallback(hsd);
2619#endif
2620 }
2621}
2622
2623/**
2624 * @brief DMA SD Rx Abort callback
2625 * @param hdma: DMA handle
2626 * @retval None
2627 */
2628static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
2629{
2630 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2631 HAL_SD_CardStateTypeDef CardState;
2632
2633 /* Clear All flags */
2634 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2635
2636 CardState = HAL_SD_GetCardState(hsd);
2637 hsd->State = HAL_SD_STATE_READY;
2638 hsd->Context = SD_CONTEXT_NONE;
2639 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2640 {
2641 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2642 }
2643
2644 if(hsd->ErrorCode == HAL_SD_ERROR_NONE)
2645 {
2646#if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2647 hsd->AbortCpltCallback(hsd);
2648#else
2649 HAL_SD_AbortCallback(hsd);
2650#endif
2651 }
2652 else
2653 {
2654#if (USE_HAL_SD_REGISTER_CALLBACKS == 1)
2655 hsd->ErrorCallback(hsd);
2656#else
2657 HAL_SD_ErrorCallback(hsd);
2658#endif
2659 }
2660}
2661
2662/**
2663 * @brief Initializes the sd card.
2664 * @param hsd: Pointer to SD handle
2665 * @retval SD Card error state
2666 */
2667static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
2668{
2669 HAL_SD_CardCSDTypeDef CSD;
2670 uint32_t errorstate;
2671 uint16_t sd_rca = 1U;
2672
2673 /* Check the power State */
2674 if(SDIO_GetPowerState(hsd->Instance) == 0U)
2675 {
2676 /* Power off */
2677 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2678 }
2679
2680 if(hsd->SdCard.CardType != CARD_SECURED)
2681 {
2682 /* Send CMD2 ALL_SEND_CID */
2683 errorstate = SDMMC_CmdSendCID(hsd->Instance);
2684 if(errorstate != HAL_SD_ERROR_NONE)
2685 {
2686 return errorstate;
2687 }
2688 else
2689 {
2690 /* Get Card identification number data */
2691 hsd->CID[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2692 hsd->CID[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2693 hsd->CID[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2694 hsd->CID[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2695 }
2696 }
2697
2698 if(hsd->SdCard.CardType != CARD_SECURED)
2699 {
2700 /* Send CMD3 SET_REL_ADDR with argument 0 */
2701 /* SD Card publishes its RCA. */
2702 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
2703 if(errorstate != HAL_SD_ERROR_NONE)
2704 {
2705 return errorstate;
2706 }
2707 }
2708 if(hsd->SdCard.CardType != CARD_SECURED)
2709 {
2710 /* Get the SD card RCA */
2711 hsd->SdCard.RelCardAdd = sd_rca;
2712
2713 /* Send CMD9 SEND_CSD with argument as card's RCA */
2714 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2715 if(errorstate != HAL_SD_ERROR_NONE)
2716 {
2717 return errorstate;
2718 }
2719 else
2720 {
2721 /* Get Card Specific Data */
2722 hsd->CSD[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2723 hsd->CSD[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2724 hsd->CSD[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2725 hsd->CSD[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2726 }
2727 }
2728
2729 /* Get the Card Class */
2730 hsd->SdCard.Class = (SDIO_GetResponse(hsd->Instance, SDIO_RESP2) >> 20U);
2731
2732 /* Get CSD parameters */
2733 if (HAL_SD_GetCardCSD(hsd, &CSD) != HAL_OK)
2734 {
2735 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2736 }
2737
2738 /* Select the Card */
2739 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
2740 if(errorstate != HAL_SD_ERROR_NONE)
2741 {
2742 return errorstate;
2743 }
2744
2745 /* Configure SDIO peripheral interface */
2746 (void)SDIO_Init(hsd->Instance, hsd->Init);
2747
2748 /* All cards are initialized */
2749 return HAL_SD_ERROR_NONE;
2750}
2751
2752/**
2753 * @brief Enquires cards about their operating voltage and configures clock
2754 * controls and stores SD information that will be needed in future
2755 * in the SD handle.
2756 * @param hsd: Pointer to SD handle
2757 * @retval error state
2758 */
2759static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
2760{
2761 __IO uint32_t count = 0U;
2762 uint32_t response = 0U, validvoltage = 0U;
2763 uint32_t errorstate;
2764
2765 /* CMD0: GO_IDLE_STATE */
2766 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2767 if(errorstate != HAL_SD_ERROR_NONE)
2768 {
2769 return errorstate;
2770 }
2771
2772 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2773 errorstate = SDMMC_CmdOperCond(hsd->Instance);
2774 if(errorstate != HAL_SD_ERROR_NONE)
2775 {
2776 hsd->SdCard.CardVersion = CARD_V1_X;
2777 /* CMD0: GO_IDLE_STATE */
2778 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2779 if(errorstate != HAL_SD_ERROR_NONE)
2780 {
2781 return errorstate;
2782 }
2783
2784 }
2785 else
2786 {
2787 hsd->SdCard.CardVersion = CARD_V2_X;
2788 }
2789
2790 if( hsd->SdCard.CardVersion == CARD_V2_X)
2791 {
2792 /* SEND CMD55 APP_CMD with RCA as 0 */
2793 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2794 if(errorstate != HAL_SD_ERROR_NONE)
2795 {
2796 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2797 }
2798 }
2799 /* SD CARD */
2800 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2801 while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
2802 {
2803 /* SEND CMD55 APP_CMD with RCA as 0 */
2804 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
2805 if(errorstate != HAL_SD_ERROR_NONE)
2806 {
2807 return errorstate;
2808 }
2809
2810 /* Send CMD41 */
2811 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
2812 if(errorstate != HAL_SD_ERROR_NONE)
2813 {
2814 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2815 }
2816
2817 /* Get command response */
2818 response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2819
2820 /* Get operating voltage*/
2821 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
2822
2823 count++;
2824 }
2825
2826 if(count >= SDMMC_MAX_VOLT_TRIAL)
2827 {
2828 return HAL_SD_ERROR_INVALID_VOLTRANGE;
2829 }
2830
2831 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
2832 {
2833 hsd->SdCard.CardType = CARD_SDHC_SDXC;
2834 }
2835 else
2836 {
2837 hsd->SdCard.CardType = CARD_SDSC;
2838 }
2839
2840
2841 return HAL_SD_ERROR_NONE;
2842}
2843
2844/**
2845 * @brief Turns the SDIO output signals off.
2846 * @param hsd: Pointer to SD handle
2847 * @retval None
2848 */
2849static void SD_PowerOFF(SD_HandleTypeDef *hsd)
2850{
2851 /* Set Power State to OFF */
2852 (void)SDIO_PowerState_OFF(hsd->Instance);
2853}
2854
2855/**
2856 * @brief Send Status info command.
2857 * @param hsd: pointer to SD handle
2858 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
2859 * SD Status register)
2860 * @retval error state
2861 */
2862static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
2863{
2864 SDIO_DataInitTypeDef config;
2865 uint32_t errorstate;
2866 uint32_t tickstart = HAL_GetTick();
2867 uint32_t count;
2868 uint32_t *pData = pSDstatus;
2869
2870 /* Check SD response */
2871 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2872 {
2873 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2874 }
2875
2876 /* Set block size for card if it is not equal to current block size for card */
2877 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
2878 if(errorstate != HAL_SD_ERROR_NONE)
2879 {
2880 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2881 return errorstate;
2882 }
2883
2884 /* Send CMD55 */
2885 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2886 if(errorstate != HAL_SD_ERROR_NONE)
2887 {
2888 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2889 return errorstate;
2890 }
2891
2892 /* Configure the SD DPSM (Data Path State Machine) */
2893 config.DataTimeOut = SDMMC_DATATIMEOUT;
2894 config.DataLength = 64U;
2895 config.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
2896 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
2897 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
2898 config.DPSM = SDIO_DPSM_ENABLE;
2899 (void)SDIO_ConfigData(hsd->Instance, &config);
2900
2901 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
2902 errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
2903 if(errorstate != HAL_SD_ERROR_NONE)
2904 {
2905 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2906 return errorstate;
2907 }
2908
2909 /* Get status data */
2910 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
2911 {
2912 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
2913 {
2914 for(count = 0U; count < 8U; count++)
2915 {
2916 *pData = SDIO_ReadFIFO(hsd->Instance);
2917 pData++;
2918 }
2919 }
2920
2921 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2922 {
2923 return HAL_SD_ERROR_TIMEOUT;
2924 }
2925 }
2926
2927 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
2928 {
2929 return HAL_SD_ERROR_DATA_TIMEOUT;
2930 }
2931 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
2932 {
2933 return HAL_SD_ERROR_DATA_CRC_FAIL;
2934 }
2935 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
2936 {
2937 return HAL_SD_ERROR_RX_OVERRUN;
2938 }
2939 else
2940 {
2941 /* Nothing to do */
2942 }
2943
2944 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)))
2945 {
2946 *pData = SDIO_ReadFIFO(hsd->Instance);
2947 pData++;
2948
2949 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2950 {
2951 return HAL_SD_ERROR_TIMEOUT;
2952 }
2953 }
2954
2955 /* Clear all the static status flags*/
2956 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
2957
2958 return HAL_SD_ERROR_NONE;
2959}
2960
2961/**
2962 * @brief Returns the current card's status.
2963 * @param hsd: Pointer to SD handle
2964 * @param pCardStatus: pointer to the buffer that will contain the SD card
2965 * status (Card Status register)
2966 * @retval error state
2967 */
2968static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
2969{
2970 uint32_t errorstate;
2971
2972 if(pCardStatus == NULL)
2973 {
2974 return HAL_SD_ERROR_PARAM;
2975 }
2976
2977 /* Send Status command */
2978 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2979 if(errorstate != HAL_SD_ERROR_NONE)
2980 {
2981 return errorstate;
2982 }
2983
2984 /* Get SD card status */
2985 *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2986
2987 return HAL_SD_ERROR_NONE;
2988}
2989
2990/**
2991 * @brief Enables the SDIO wide bus mode.
2992 * @param hsd: pointer to SD handle
2993 * @retval error state
2994 */
2995static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
2996{
2997 uint32_t scr[2U] = {0U, 0U};
2998 uint32_t errorstate;
2999
3000 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3001 {
3002 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3003 }
3004
3005 /* Get SCR Register */
3006 errorstate = SD_FindSCR(hsd, scr);
3007 if(errorstate != HAL_SD_ERROR_NONE)
3008 {
3009 return errorstate;
3010 }
3011
3012 /* If requested card supports wide bus operation */
3013 if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
3014 {
3015 /* Send CMD55 APP_CMD with argument as card's RCA.*/
3016 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3017 if(errorstate != HAL_SD_ERROR_NONE)
3018 {
3019 return errorstate;
3020 }
3021
3022 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
3023 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
3024 if(errorstate != HAL_SD_ERROR_NONE)
3025 {
3026 return errorstate;
3027 }
3028
3029 return HAL_SD_ERROR_NONE;
3030 }
3031 else
3032 {
3033 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3034 }
3035}
3036
3037/**
3038 * @brief Disables the SDIO wide bus mode.
3039 * @param hsd: Pointer to SD handle
3040 * @retval error state
3041 */
3042static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
3043{
3044 uint32_t scr[2U] = {0U, 0U};
3045 uint32_t errorstate;
3046
3047 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
3048 {
3049 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
3050 }
3051
3052 /* Get SCR Register */
3053 errorstate = SD_FindSCR(hsd, scr);
3054 if(errorstate != HAL_SD_ERROR_NONE)
3055 {
3056 return errorstate;
3057 }
3058
3059 /* If requested card supports 1 bit mode operation */
3060 if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
3061 {
3062 /* Send CMD55 APP_CMD with argument as card's RCA */
3063 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
3064 if(errorstate != HAL_SD_ERROR_NONE)
3065 {
3066 return errorstate;
3067 }
3068
3069 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
3070 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
3071 if(errorstate != HAL_SD_ERROR_NONE)
3072 {
3073 return errorstate;
3074 }
3075
3076 return HAL_SD_ERROR_NONE;
3077 }
3078 else
3079 {
3080 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
3081 }
3082}
3083
3084
3085/**
3086 * @brief Finds the SD card SCR register value.
3087 * @param hsd: Pointer to SD handle
3088 * @param pSCR: pointer to the buffer that will contain the SCR value
3089 * @retval error state
3090 */
3091static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
3092{
3093 SDIO_DataInitTypeDef config;
3094 uint32_t errorstate;
3095 uint32_t tickstart = HAL_GetTick();
3096 uint32_t index = 0U;
3097 uint32_t tempscr[2U] = {0U, 0U};
3098 uint32_t *scr = pSCR;
3099
3100 /* Set Block Size To 8 Bytes */
3101 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
3102 if(errorstate != HAL_SD_ERROR_NONE)
3103 {
3104 return errorstate;
3105 }
3106
3107 /* Send CMD55 APP_CMD with argument as card's RCA */
3108 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
3109 if(errorstate != HAL_SD_ERROR_NONE)
3110 {
3111 return errorstate;
3112 }
3113
3114 config.DataTimeOut = SDMMC_DATATIMEOUT;
3115 config.DataLength = 8U;
3116 config.DataBlockSize = SDIO_DATABLOCK_SIZE_8B;
3117 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
3118 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
3119 config.DPSM = SDIO_DPSM_ENABLE;
3120 (void)SDIO_ConfigData(hsd->Instance, &config);
3121
3122 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
3123 errorstate = SDMMC_CmdSendSCR(hsd->Instance);
3124 if(errorstate != HAL_SD_ERROR_NONE)
3125 {
3126 return errorstate;
3127 }
3128
3129 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT))
3130 {
3131 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
3132 {
3133 *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
3134 index++;
3135 }
3136 else if(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXACT))
3137 {
3138 break;
3139 }
3140
3141 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
3142 {
3143 return HAL_SD_ERROR_TIMEOUT;
3144 }
3145 }
3146
3147 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
3148 {
3149 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
3150
3151 return HAL_SD_ERROR_DATA_TIMEOUT;
3152 }
3153 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
3154 {
3155 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
3156
3157 return HAL_SD_ERROR_DATA_CRC_FAIL;
3158 }
3159 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
3160 {
3161 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
3162
3163 return HAL_SD_ERROR_RX_OVERRUN;
3164 }
3165 else
3166 {
3167 /* No error flag set */
3168 /* Clear all the static flags */
3169 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_DATA_FLAGS);
3170
3171 *scr = (((tempscr[1] & SDMMC_0TO7BITS) << 24) | ((tempscr[1] & SDMMC_8TO15BITS) << 8) |\
3172 ((tempscr[1] & SDMMC_16TO23BITS) >> 8) | ((tempscr[1] & SDMMC_24TO31BITS) >> 24));
3173 scr++;
3174 *scr = (((tempscr[0] & SDMMC_0TO7BITS) << 24) | ((tempscr[0] & SDMMC_8TO15BITS) << 8) |\
3175 ((tempscr[0] & SDMMC_16TO23BITS) >> 8) | ((tempscr[0] & SDMMC_24TO31BITS) >> 24));
3176
3177 }
3178
3179 return HAL_SD_ERROR_NONE;
3180}
3181
3182/**
3183 * @brief Wrap up reading in non-blocking mode.
3184 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3185 * the configuration information.
3186 * @retval None
3187 */
3188static void SD_Read_IT(SD_HandleTypeDef *hsd)
3189{
3190 uint32_t count, data, dataremaining;
3191 uint8_t* tmp;
3192
3193 tmp = hsd->pRxBuffPtr;
3194 dataremaining = hsd->RxXferSize;
3195
3196 if (dataremaining > 0U)
3197 {
3198 /* Read data from SDIO Rx FIFO */
3199 for(count = 0U; count < 8U; count++)
3200 {
3201 data = SDIO_ReadFIFO(hsd->Instance);
3202 *tmp = (uint8_t)(data & 0xFFU);
3203 tmp++;
3204 dataremaining--;
3205 *tmp = (uint8_t)((data >> 8U) & 0xFFU);
3206 tmp++;
3207 dataremaining--;
3208 *tmp = (uint8_t)((data >> 16U) & 0xFFU);
3209 tmp++;
3210 dataremaining--;
3211 *tmp = (uint8_t)((data >> 24U) & 0xFFU);
3212 tmp++;
3213 dataremaining--;
3214 }
3215
3216 hsd->pRxBuffPtr = tmp;
3217 hsd->RxXferSize = dataremaining;
3218 }
3219}
3220
3221/**
3222 * @brief Wrap up writing in non-blocking mode.
3223 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
3224 * the configuration information.
3225 * @retval None
3226 */
3227static void SD_Write_IT(SD_HandleTypeDef *hsd)
3228{
3229 uint32_t count, data, dataremaining;
3230 uint8_t* tmp;
3231
3232 tmp = hsd->pTxBuffPtr;
3233 dataremaining = hsd->TxXferSize;
3234
3235 if (dataremaining > 0U)
3236 {
3237 /* Write data to SDIO Tx FIFO */
3238 for(count = 0U; count < 8U; count++)
3239 {
3240 data = (uint32_t)(*tmp);
3241 tmp++;
3242 dataremaining--;
3243 data |= ((uint32_t)(*tmp) << 8U);
3244 tmp++;
3245 dataremaining--;
3246 data |= ((uint32_t)(*tmp) << 16U);
3247 tmp++;
3248 dataremaining--;
3249 data |= ((uint32_t)(*tmp) << 24U);
3250 tmp++;
3251 dataremaining--;
3252 (void)SDIO_WriteFIFO(hsd->Instance, &data);
3253 }
3254
3255 hsd->pTxBuffPtr = tmp;
3256 hsd->TxXferSize = dataremaining;
3257 }
3258}
3259
3260/**
3261 * @}
3262 */
3263
3264#endif /* HAL_SD_MODULE_ENABLED */
3265
3266/**
3267 * @}
3268 */
3269
3270/**
3271 * @}
3272 */
3273
3274#endif /* SDIO */
3275
3276/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.