Increment eeprom version
[betaflight.git] / lib / main / STM32F1 / Drivers / STM32F1xx_HAL_Driver / Src / stm32f1xx_hal_sd.c
blob40d91834281648ee64b319697d9ccd1f8e6e6e8d
1 /**
2 ******************************************************************************
3 * @file stm32f1xx_hal_sd.c
4 * @author MCD Application Team
5 * @version V1.1.1
6 * @date 12-May-2017
7 * @brief SD card HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Secure Digital (SD) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + SD card Control functions
15 @verbatim
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
19 [..]
20 This driver implements a high level communication layer for read and write from/to
21 this memory. The needed STM32 hardware resources (SDIO and GPIO) are performed by
22 the user in HAL_SD_MspInit() function (MSP layer).
23 Basically, the MSP layer configuration should be the same as we provide in the
24 examples.
25 You can easily tailor this configuration according to hardware resources.
27 [..]
28 This driver is a generic layered driver for SDIO memories which uses the HAL
29 SDIO driver functions to interface with SD and uSD cards devices.
30 It is used as follows:
32 (#)Initialize the SDIO low level resources by implement the HAL_SD_MspInit() API:
33 (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE();
34 (##) SDIO pins configuration for SD card
35 (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
36 (+++) Configure these SDIO pins as alternate function pull-up using HAL_GPIO_Init()
37 and according to your pin assignment;
38 (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
39 and HAL_SD_WriteBlocks_DMA() APIs).
40 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
41 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
42 (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
43 (+++) Configure the SDIO and DMA interrupt priorities using functions
44 HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority
45 (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ()
46 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
47 and __HAL_SD_DISABLE_IT() inside the communication process.
48 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
49 and __HAL_SD_CLEAR_IT()
50 (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
51 and HAL_SD_WriteBlocks_IT() APIs).
52 (+++) Configure the SDIO interrupt priorities using function
53 HAL_NVIC_SetPriority();
54 (+++) Enable the NVIC SDIO IRQs using function HAL_NVIC_EnableIRQ()
55 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
56 and __HAL_SD_DISABLE_IT() inside the communication process.
57 (+++) SDIO interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
58 and __HAL_SD_CLEAR_IT()
59 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
62 *** SD Card Initialization and configuration ***
63 ================================================
64 [..]
65 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
66 SDIO IP(STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
67 This function provide the following operations:
69 (#) Initialize the SDIO peripheral interface with defaullt configuration.
70 The initialization process is done at 400KHz. You can change or adapt
71 this frequency by adjusting the "ClockDiv" field.
72 The SD Card frequency (SDIO_CK) is computed as follows:
74 SDIO_CK = SDIOCLK / (ClockDiv + 2)
76 In initialization mode and according to the SD Card standard,
77 make sure that the SDIO_CK frequency doesn't exceed 400KHz.
79 This phase of initialization is done through SDIO_Init() and
80 SDIO_PowerState_ON() SDIO low level APIs.
82 (#) Initialize the SD card. The API used is HAL_SD_InitCard().
83 This phase allows the card initialization and identification
84 and check the SD Card type (Standard Capacity or High Capacity)
85 The initialization flow is compatible with SD standard.
87 This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
88 of plug-off plug-in.
90 (#) Configure the SD Card Data transfer frequency. By Default, the card transfer
91 frequency is set to 24MHz. You can change or adapt this frequency by adjusting
92 the "ClockDiv" field.
93 In transfer mode and according to the SD Card standard, make sure that the
94 SDIO_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
95 To be able to use a frequency higher than 24MHz, you should use the SDIO
96 peripheral in bypass mode. Refer to the corresponding reference manual
97 for more details.
99 (#) Select the corresponding SD Card according to the address read with the step 2.
101 (#) Configure the SD Card in wide bus mode: 4-bits data.
103 *** SD Card Read operation ***
104 ==============================
105 [..]
106 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
107 This function allows the read of 512 bytes blocks.
108 You can choose either one block read operation or multiple block read operation
109 by adjusting the "NumberOfBlocks" parameter.
110 After this, you have to ensure that the transfer is done correctly. The check is done
111 through HAL_SD_GetCardState() function for SD card state.
113 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
114 This function allows the read of 512 bytes blocks.
115 You can choose either one block read operation or multiple block read operation
116 by adjusting the "NumberOfBlocks" parameter.
117 After this, you have to ensure that the transfer is done correctly. The check is done
118 through HAL_SD_GetCardState() function for SD card state.
119 You could also check the DMA transfer process through the SD Rx interrupt event.
121 (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
122 This function allows the read of 512 bytes blocks.
123 You can choose either one block read operation or multiple block read operation
124 by adjusting the "NumberOfBlocks" parameter.
125 After this, you have to ensure that the transfer is done correctly. The check is done
126 through HAL_SD_GetCardState() function for SD card state.
127 You could also check the IT transfer process through the SD Rx interrupt event.
129 *** SD Card Write operation ***
130 ===============================
131 [..]
132 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
133 This function allows the read of 512 bytes blocks.
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.
139 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
140 This function allows the read of 512 bytes blocks.
141 You can choose either one block read operation or multiple block read operation
142 by adjusting the "NumberOfBlocks" parameter.
143 After this, you have to ensure that the transfer is done correctly. The check is done
144 through HAL_SD_GetCardState() function for SD card state.
145 You could also check the DMA transfer process through the SD Tx interrupt event.
147 (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
148 This function allows the read of 512 bytes blocks.
149 You can choose either one block read operation or multiple block read operation
150 by adjusting the "NumberOfBlocks" parameter.
151 After this, you have to ensure that the transfer is done correctly. The check is done
152 through HAL_SD_GetCardState() function for SD card state.
153 You could also check the IT transfer process through the SD Tx interrupt event.
155 *** SD card status ***
156 ======================
157 [..]
158 (+) The SD Status contains status bits that are related to the SD Memory
159 Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
161 *** SD card information ***
162 ===========================
163 [..]
164 (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
165 It returns useful information about the SD card such as block size, card type,
166 block number ...
168 *** SD card CSD register ***
169 ============================
170 [..]
171 (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
172 Some of the CSD parameters are useful for card initialization and identification.
174 *** SD card CID register ***
175 ============================
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.
180 *** SD HAL driver macros list ***
181 ==================================
182 [..]
183 Below the list of most used macros in SD HAL driver.
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
194 [..]
195 (@) You can refer to the SD HAL driver header file for more useful macros
197 @endverbatim
198 ******************************************************************************
199 * @attention
201 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
203 * Redistribution and use in source and binary forms, with or without modification,
204 * are permitted provided that the following conditions are met:
205 * 1. Redistributions of source code must retain the above copyright notice,
206 * this list of conditions and the following disclaimer.
207 * 2. Redistributions in binary form must reproduce the above copyright notice,
208 * this list of conditions and the following disclaimer in the documentation
209 * and/or other materials provided with the distribution.
210 * 3. Neither the name of STMicroelectronics nor the names of its contributors
211 * may be used to endorse or promote products derived from this software
212 * without specific prior written permission.
214 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
215 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
216 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
217 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
218 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
220 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
221 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
222 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
223 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
225 ******************************************************************************
228 /* Includes ------------------------------------------------------------------*/
229 #include "stm32f1xx_hal.h"
231 #if defined(STM32F103xE) || defined(STM32F103xG)
233 /** @addtogroup STM32F1xx_HAL_Driver
234 * @{
237 /** @addtogroup SD
238 * @{
240 #ifdef HAL_SD_MODULE_ENABLED
242 /* Private typedef -----------------------------------------------------------*/
243 /* Private define ------------------------------------------------------------*/
244 /** @addtogroup SD_Private_Defines
245 * @{
249 * @}
252 /* Private macro -------------------------------------------------------------*/
253 /* Private variables ---------------------------------------------------------*/
254 /* Private function prototypes -----------------------------------------------*/
255 /* Private functions ---------------------------------------------------------*/
256 /** @defgroup SD_Private_Functions SD Private Functions
257 * @{
259 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd);
260 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd);
261 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus);
262 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus);
263 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd);
264 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd);
265 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR);
266 static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd);
267 static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd);
268 static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd);
269 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
270 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
271 static void SD_DMAError(DMA_HandleTypeDef *hdma);
272 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma);
273 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma);
275 * @}
278 /* Exported functions --------------------------------------------------------*/
279 /** @addtogroup SD_Exported_Functions
280 * @{
283 /** @addtogroup SD_Exported_Functions_Group1
284 * @brief Initialization and de-initialization functions
286 @verbatim
287 ==============================================================================
288 ##### Initialization and de-initialization functions #####
289 ==============================================================================
290 [..]
291 This section provides functions allowing to initialize/de-initialize the SD
292 card device to be ready for use.
294 @endverbatim
295 * @{
299 * @brief Initializes the SD according to the specified parameters in the
300 SD_HandleTypeDef and create the associated handle.
301 * @param hsd: Pointer to the SD handle
302 * @retval HAL status
304 HAL_StatusTypeDef HAL_SD_Init(SD_HandleTypeDef *hsd)
306 /* Check the SD handle allocation */
307 if(hsd == NULL)
309 return HAL_ERROR;
312 /* Check the parameters */
313 assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance));
314 assert_param(IS_SDIO_CLOCK_EDGE(hsd->Init.ClockEdge));
315 assert_param(IS_SDIO_CLOCK_BYPASS(hsd->Init.ClockBypass));
316 assert_param(IS_SDIO_CLOCK_POWER_SAVE(hsd->Init.ClockPowerSave));
317 assert_param(IS_SDIO_BUS_WIDE(hsd->Init.BusWide));
318 assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(hsd->Init.HardwareFlowControl));
319 assert_param(IS_SDIO_CLKDIV(hsd->Init.ClockDiv));
321 if(hsd->State == HAL_SD_STATE_RESET)
323 /* Allocate lock resource and initialize it */
324 hsd->Lock = HAL_UNLOCKED;
325 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
326 HAL_SD_MspInit(hsd);
329 hsd->State = HAL_SD_STATE_BUSY;
331 /* Initialize the Card parameters */
332 HAL_SD_InitCard(hsd);
334 /* Initialize the error code */
335 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
337 /* Initialize the SD operation */
338 hsd->Context = SD_CONTEXT_NONE;
340 /* Initialize the SD state */
341 hsd->State = HAL_SD_STATE_READY;
343 return HAL_OK;
347 * @brief Initializes the SD Card.
348 * @param hsd: Pointer to SD handle
349 * @note This function initializes the SD card. It could be used when a card
350 re-initialization is needed.
351 * @retval HAL status
353 HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
355 uint32_t errorstate = HAL_SD_ERROR_NONE;
356 SD_InitTypeDef Init;
358 /* Default SDIO peripheral configuration for SD card initialization */
359 Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
360 Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
361 Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
362 Init.BusWide = SDIO_BUS_WIDE_1B;
363 Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
364 Init.ClockDiv = SDIO_INIT_CLK_DIV;
366 /* Initialize SDIO peripheral interface with default configuration */
367 SDIO_Init(hsd->Instance, Init);
369 /* Disable SDIO Clock */
370 __HAL_SD_DISABLE(hsd);
372 /* Set Power State to ON */
373 SDIO_PowerState_ON(hsd->Instance);
375 /* Enable SDIO Clock */
376 __HAL_SD_ENABLE(hsd);
378 /* Required power up waiting time before starting the SD initialization
379 sequence */
380 HAL_Delay(2U);
382 /* Identify card operating voltage */
383 errorstate = SD_PowerON(hsd);
384 if(errorstate != HAL_SD_ERROR_NONE)
386 hsd->State = HAL_SD_STATE_READY;
387 hsd->ErrorCode |= errorstate;
388 return HAL_ERROR;
391 /* Card initialization */
392 errorstate = SD_InitCard(hsd);
393 if(errorstate != HAL_SD_ERROR_NONE)
395 hsd->State = HAL_SD_STATE_READY;
396 hsd->ErrorCode |= errorstate;
397 return HAL_ERROR;
400 return HAL_OK;
404 * @brief De-Initializes the SD card.
405 * @param hsd: Pointer to SD handle
406 * @retval HAL status
408 HAL_StatusTypeDef HAL_SD_DeInit(SD_HandleTypeDef *hsd)
410 /* Check the SD handle allocation */
411 if(hsd == NULL)
413 return HAL_ERROR;
416 /* Check the parameters */
417 assert_param(IS_SDIO_ALL_INSTANCE(hsd->Instance));
419 hsd->State = HAL_SD_STATE_BUSY;
421 /* Set SD power state to off */
422 SD_PowerOFF(hsd);
424 /* De-Initialize the MSP layer */
425 HAL_SD_MspDeInit(hsd);
427 hsd->ErrorCode = HAL_SD_ERROR_NONE;
428 hsd->State = HAL_SD_STATE_RESET;
430 return HAL_OK;
435 * @brief Initializes the SD MSP.
436 * @param hsd: Pointer to SD handle
437 * @retval None
439 __weak void HAL_SD_MspInit(SD_HandleTypeDef *hsd)
441 /* Prevent unused argument(s) compilation warning */
442 UNUSED(hsd);
444 /* NOTE : This function Should not be modified, when the callback is needed,
445 the HAL_SD_MspInit could be implemented in the user file
450 * @brief De-Initialize SD MSP.
451 * @param hsd: Pointer to SD handle
452 * @retval None
454 __weak void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd)
456 /* Prevent unused argument(s) compilation warning */
457 UNUSED(hsd);
459 /* NOTE : This function Should not be modified, when the callback is needed,
460 the HAL_SD_MspDeInit could be implemented in the user file
465 * @}
468 /** @addtogroup SD_Exported_Functions_Group2
469 * @brief Data transfer functions
471 @verbatim
472 ==============================================================================
473 ##### IO operation functions #####
474 ==============================================================================
475 [..]
476 This subsection provides a set of functions allowing to manage the data
477 transfer from/to SD card.
479 @endverbatim
480 * @{
484 * @brief Reads block(s) from a specified address in a card. The Data transfer
485 * is managed by polling mode.
486 * @note This API should be followed by a check on the card state through
487 * HAL_SD_GetCardState().
488 * @param hsd: Pointer to SD handle
489 * @param pData: pointer to the buffer that will contain the received data
490 * @param BlockAdd: Block Address from where data is to be read
491 * @param NumberOfBlocks: Number of SD blocks to read
492 * @param Timeout: Specify timeout value
493 * @retval HAL status
495 HAL_StatusTypeDef HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
497 SDIO_DataInitTypeDef config;
498 uint32_t errorstate = HAL_SD_ERROR_NONE;
499 uint32_t tickstart = HAL_GetTick();
500 uint32_t count = 0U, *tempbuff = (uint32_t *)pData;
502 if(NULL == pData)
504 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
505 return HAL_ERROR;
508 if(hsd->State == HAL_SD_STATE_READY)
510 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
512 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
514 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
515 return HAL_ERROR;
518 hsd->State = HAL_SD_STATE_BUSY;
520 /* Initialize data control register */
521 hsd->Instance->DCTRL = 0U;
523 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
525 BlockAdd *= 512U;
528 /* Set Block Size for Card */
529 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
530 if(errorstate != HAL_SD_ERROR_NONE)
532 /* Clear all the static flags */
533 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
534 hsd->ErrorCode |= errorstate;
535 hsd->State = HAL_SD_STATE_READY;
536 return HAL_ERROR;
539 /* Configure the SD DPSM (Data Path State Machine) */
540 config.DataTimeOut = SDMMC_DATATIMEOUT;
541 config.DataLength = NumberOfBlocks * BLOCKSIZE;
542 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
543 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
544 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
545 config.DPSM = SDIO_DPSM_ENABLE;
546 SDIO_ConfigData(hsd->Instance, &config);
548 /* Read block(s) in polling mode */
549 if(NumberOfBlocks > 1U)
551 hsd->Context = SD_CONTEXT_READ_MULTIPLE_BLOCK;
553 /* Read Multi Block command */
554 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
556 else
558 hsd->Context = SD_CONTEXT_READ_SINGLE_BLOCK;
560 /* Read Single Block command */
561 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
563 if(errorstate != HAL_SD_ERROR_NONE)
565 /* Clear all the static flags */
566 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
567 hsd->ErrorCode |= errorstate;
568 hsd->State = HAL_SD_STATE_READY;
569 return HAL_ERROR;
572 /* Poll on SDIO flags */
573 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_STA_STBITERR))
575 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
577 /* Read data from SDIO Rx FIFO */
578 for(count = 0U; count < 8U; count++)
580 *(tempbuff + count) = SDIO_ReadFIFO(hsd->Instance);
582 tempbuff += 8U;
585 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
587 /* Clear all the static flags */
588 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
589 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
590 hsd->State= HAL_SD_STATE_READY;
591 return HAL_TIMEOUT;
595 /* Send stop transmission command in case of multiblock read */
596 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
598 if(hsd->SdCard.CardType != CARD_SECURED)
600 /* Send stop transmission command */
601 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
602 if(errorstate != HAL_SD_ERROR_NONE)
604 /* Clear all the static flags */
605 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
606 hsd->ErrorCode |= errorstate;
607 hsd->State = HAL_SD_STATE_READY;
608 return HAL_ERROR;
613 /* Get error state */
614 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
616 /* Clear all the static flags */
617 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
618 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
619 hsd->State = HAL_SD_STATE_READY;
620 return HAL_ERROR;
622 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
624 /* Clear all the static flags */
625 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
626 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
627 hsd->State = HAL_SD_STATE_READY;
628 return HAL_ERROR;
630 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
632 /* Clear all the static flags */
633 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
634 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
635 hsd->State = HAL_SD_STATE_READY;
636 return HAL_ERROR;
639 /* Empty FIFO if there is still any data */
640 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)))
642 *tempbuff = SDIO_ReadFIFO(hsd->Instance);
643 tempbuff++;
645 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
647 /* Clear all the static flags */
648 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
649 hsd->ErrorCode |= HAL_SD_ERROR_TIMEOUT;
650 hsd->State= HAL_SD_STATE_READY;
651 return HAL_ERROR;
655 /* Clear all the static flags */
656 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
658 hsd->State = HAL_SD_STATE_READY;
660 return HAL_OK;
662 else
664 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
665 return HAL_ERROR;
670 * @brief Allows to write block(s) to a specified address in a card. The Data
671 * transfer is managed by polling mode.
672 * @note This API should be followed by a check on the card state through
673 * HAL_SD_GetCardState().
674 * @param hsd: Pointer to SD handle
675 * @param pData: pointer to the buffer that will contain the data to transmit
676 * @param BlockAdd: Block Address where data will be written
677 * @param NumberOfBlocks: Number of SD blocks to write
678 * @param Timeout: Specify timeout value
679 * @retval HAL status
681 HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
683 SDIO_DataInitTypeDef config;
684 uint32_t errorstate = HAL_SD_ERROR_NONE;
685 uint32_t tickstart = HAL_GetTick();
686 uint32_t count = 0U;
687 uint32_t *tempbuff = (uint32_t *)pData;
689 if(NULL == pData)
691 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
692 return HAL_ERROR;
695 if(hsd->State == HAL_SD_STATE_READY)
697 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
699 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
701 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
702 return HAL_ERROR;
705 hsd->State = HAL_SD_STATE_BUSY;
707 /* Initialize data control register */
708 hsd->Instance->DCTRL = 0U;
710 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
712 BlockAdd *= 512U;
715 /* Set Block Size for Card */
716 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
717 if(errorstate != HAL_SD_ERROR_NONE)
719 /* Clear all the static flags */
720 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
721 hsd->ErrorCode |= errorstate;
722 hsd->State = HAL_SD_STATE_READY;
723 return HAL_ERROR;
726 /* Write Blocks in Polling mode */
727 if(NumberOfBlocks > 1U)
729 hsd->Context = SD_CONTEXT_WRITE_MULTIPLE_BLOCK;
731 /* Write Multi Block command */
732 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
734 else
736 hsd->Context = SD_CONTEXT_WRITE_SINGLE_BLOCK;
738 /* Write Single Block command */
739 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
741 if(errorstate != HAL_SD_ERROR_NONE)
743 /* Clear all the static flags */
744 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
745 hsd->ErrorCode |= errorstate;
746 hsd->State = HAL_SD_STATE_READY;
747 return HAL_ERROR;
750 /* Configure the SD DPSM (Data Path State Machine) */
751 config.DataTimeOut = SDMMC_DATATIMEOUT;
752 config.DataLength = NumberOfBlocks * BLOCKSIZE;
753 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
754 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
755 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
756 config.DPSM = SDIO_DPSM_ENABLE;
757 SDIO_ConfigData(hsd->Instance, &config);
759 /* Write block(s) in polling mode */
760 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DATAEND | SDIO_FLAG_STBITERR))
762 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXFIFOHE))
764 /* Write data to SDIO Tx FIFO */
765 for(count = 0U; count < 8U; count++)
767 SDIO_WriteFIFO(hsd->Instance, (tempbuff + count));
769 tempbuff += 8U;
772 if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))
774 /* Clear all the static flags */
775 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
776 hsd->ErrorCode |= errorstate;
777 hsd->State = HAL_SD_STATE_READY;
778 return HAL_TIMEOUT;
782 /* Send stop transmission command in case of multiblock write */
783 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DATAEND) && (NumberOfBlocks > 1U))
785 if(hsd->SdCard.CardType != CARD_SECURED)
787 /* Send stop transmission command */
788 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
789 if(errorstate != HAL_SD_ERROR_NONE)
791 /* Clear all the static flags */
792 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
793 hsd->ErrorCode |= errorstate;
794 hsd->State = HAL_SD_STATE_READY;
795 return HAL_ERROR;
800 /* Get error state */
801 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
803 /* Clear all the static flags */
804 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
805 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
806 hsd->State = HAL_SD_STATE_READY;
807 return HAL_ERROR;
809 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
811 /* Clear all the static flags */
812 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
813 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
814 hsd->State = HAL_SD_STATE_READY;
815 return HAL_ERROR;
817 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_TXUNDERR))
819 /* Clear all the static flags */
820 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
821 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
822 hsd->State = HAL_SD_STATE_READY;
823 return HAL_ERROR;
826 /* Clear all the static flags */
827 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
829 hsd->State = HAL_SD_STATE_READY;
831 return HAL_OK;
833 else
835 hsd->ErrorCode |= HAL_SD_ERROR_BUSY;
836 return HAL_ERROR;
841 * @brief Reads block(s) from a specified address in a card. The Data transfer
842 * is managed in interrupt mode.
843 * @note This API should be followed by a check on the card state through
844 * HAL_SD_GetCardState().
845 * @note You could also check the IT transfer process through the SD Rx
846 * interrupt event.
847 * @param hsd: Pointer to SD handle
848 * @param pData: Pointer to the buffer that will contain the received data
849 * @param BlockAdd: Block Address from where data is to be read
850 * @param NumberOfBlocks: Number of blocks to read.
851 * @retval HAL status
853 HAL_StatusTypeDef HAL_SD_ReadBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
855 SDIO_DataInitTypeDef config;
856 uint32_t errorstate = HAL_SD_ERROR_NONE;
858 if(NULL == pData)
860 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
861 return HAL_ERROR;
864 if(hsd->State == HAL_SD_STATE_READY)
866 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
868 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
870 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
871 return HAL_ERROR;
874 hsd->State = HAL_SD_STATE_BUSY;
876 /* Initialize data control register */
877 hsd->Instance->DCTRL = 0U;
879 hsd->pRxBuffPtr = (uint32_t *)pData;
880 hsd->RxXferSize = BLOCKSIZE * NumberOfBlocks;
882 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_FLAG_RXFIFOHF | SDIO_IT_STBITERR));
884 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
886 BlockAdd *= 512U;
889 /* Configure the SD DPSM (Data Path State Machine) */
890 config.DataTimeOut = SDMMC_DATATIMEOUT;
891 config.DataLength = BLOCKSIZE * NumberOfBlocks;
892 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
893 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
894 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
895 config.DPSM = SDIO_DPSM_ENABLE;
896 SDIO_ConfigData(hsd->Instance, &config);
898 /* Set Block Size for Card */
899 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
900 if(errorstate != HAL_SD_ERROR_NONE)
902 /* Clear all the static flags */
903 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
904 hsd->ErrorCode |= errorstate;
905 hsd->State = HAL_SD_STATE_READY;
906 return HAL_ERROR;
909 /* Read Blocks in IT mode */
910 if(NumberOfBlocks > 1U)
912 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_IT);
914 /* Read Multi Block command */
915 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
917 else
919 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_IT);
921 /* Read Single Block command */
922 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
924 if(errorstate != HAL_SD_ERROR_NONE)
926 /* Clear all the static flags */
927 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
928 hsd->ErrorCode |= errorstate;
929 hsd->State = HAL_SD_STATE_READY;
930 return HAL_ERROR;
933 return HAL_OK;
935 else
937 return HAL_BUSY;
942 * @brief Writes block(s) to a specified address in a card. The Data transfer
943 * is managed in interrupt mode.
944 * @note This API should be followed by a check on the card state through
945 * HAL_SD_GetCardState().
946 * @note You could also check the IT transfer process through the SD Tx
947 * interrupt event.
948 * @param hsd: Pointer to SD handle
949 * @param pData: Pointer to the buffer that will contain the data to transmit
950 * @param BlockAdd: Block Address where data will be written
951 * @param NumberOfBlocks: Number of blocks to write
952 * @retval HAL status
954 HAL_StatusTypeDef HAL_SD_WriteBlocks_IT(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
956 SDIO_DataInitTypeDef config;
957 uint32_t errorstate = HAL_SD_ERROR_NONE;
959 if(NULL == pData)
961 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
962 return HAL_ERROR;
965 if(hsd->State == HAL_SD_STATE_READY)
967 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
969 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
971 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
972 return HAL_ERROR;
975 hsd->State = HAL_SD_STATE_BUSY;
977 /* Initialize data control register */
978 hsd->Instance->DCTRL = 0U;
980 hsd->pTxBuffPtr = (uint32_t *)pData;
981 hsd->TxXferSize = BLOCKSIZE * NumberOfBlocks;
983 /* Enable transfer interrupts */
984 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_DATAEND | SDIO_FLAG_TXFIFOHE | SDIO_IT_STBITERR));
986 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
988 BlockAdd *= 512U;
991 /* Set Block Size for Card */
992 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
993 if(errorstate != HAL_SD_ERROR_NONE)
995 /* Clear all the static flags */
996 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
997 hsd->ErrorCode |= errorstate;
998 hsd->State = HAL_SD_STATE_READY;
999 return HAL_ERROR;
1002 /* Write Blocks in Polling mode */
1003 if(NumberOfBlocks > 1U)
1005 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK| SD_CONTEXT_IT);
1007 /* Write Multi Block command */
1008 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
1010 else
1012 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_IT);
1014 /* Write Single Block command */
1015 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
1017 if(errorstate != HAL_SD_ERROR_NONE)
1019 /* Clear all the static flags */
1020 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1021 hsd->ErrorCode |= errorstate;
1022 hsd->State = HAL_SD_STATE_READY;
1023 return HAL_ERROR;
1026 /* Configure the SD DPSM (Data Path State Machine) */
1027 config.DataTimeOut = SDMMC_DATATIMEOUT;
1028 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1029 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1030 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
1031 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1032 config.DPSM = SDIO_DPSM_ENABLE;
1033 SDIO_ConfigData(hsd->Instance, &config);
1035 return HAL_OK;
1037 else
1039 return HAL_BUSY;
1044 * @brief Reads block(s) from a specified address in a card. The Data transfer
1045 * is managed by DMA mode.
1046 * @note This API should be followed by a check on the card state through
1047 * HAL_SD_GetCardState().
1048 * @note You could also check the DMA transfer process through the SD Rx
1049 * interrupt event.
1050 * @param hsd: Pointer SD handle
1051 * @param pData: Pointer to the buffer that will contain the received data
1052 * @param BlockAdd: Block Address from where data is to be read
1053 * @param NumberOfBlocks: Number of blocks to read.
1054 * @retval HAL status
1056 HAL_StatusTypeDef HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1058 SDIO_DataInitTypeDef config;
1059 uint32_t errorstate = HAL_SD_ERROR_NONE;
1061 if(NULL == pData)
1063 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1064 return HAL_ERROR;
1067 if(hsd->State == HAL_SD_STATE_READY)
1069 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
1071 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1073 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1074 return HAL_ERROR;
1077 hsd->State = HAL_SD_STATE_BUSY;
1079 /* Initialize data control register */
1080 hsd->Instance->DCTRL = 0U;
1082 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_DATAEND | SDIO_IT_STBITERR));
1084 /* Set the DMA transfer complete callback */
1085 hsd->hdmarx->XferCpltCallback = SD_DMAReceiveCplt;
1087 /* Set the DMA error callback */
1088 hsd->hdmarx->XferErrorCallback = SD_DMAError;
1090 /* Set the DMA Abort callback */
1091 hsd->hdmarx->XferAbortCallback = NULL;
1093 /* Enable the DMA Channel */
1094 HAL_DMA_Start_IT(hsd->hdmarx, (uint32_t)&hsd->Instance->FIFO, (uint32_t)pData, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
1096 /* Enable SD DMA transfer */
1097 __HAL_SD_DMA_ENABLE(hsd);
1099 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1101 BlockAdd *= 512U;
1104 /* Configure the SD DPSM (Data Path State Machine) */
1105 config.DataTimeOut = SDMMC_DATATIMEOUT;
1106 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1107 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1108 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
1109 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1110 config.DPSM = SDIO_DPSM_ENABLE;
1111 SDIO_ConfigData(hsd->Instance, &config);
1113 /* Set Block Size for Card */
1114 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1115 if(errorstate != HAL_SD_ERROR_NONE)
1117 /* Clear all the static flags */
1118 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1119 hsd->ErrorCode |= errorstate;
1120 hsd->State = HAL_SD_STATE_READY;
1121 return HAL_ERROR;
1124 /* Read Blocks in DMA mode */
1125 if(NumberOfBlocks > 1U)
1127 hsd->Context = (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1129 /* Read Multi Block command */
1130 errorstate = SDMMC_CmdReadMultiBlock(hsd->Instance, BlockAdd);
1132 else
1134 hsd->Context = (SD_CONTEXT_READ_SINGLE_BLOCK | SD_CONTEXT_DMA);
1136 /* Read Single Block command */
1137 errorstate = SDMMC_CmdReadSingleBlock(hsd->Instance, BlockAdd);
1139 if(errorstate != HAL_SD_ERROR_NONE)
1141 /* Clear all the static flags */
1142 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1143 hsd->ErrorCode |= errorstate;
1144 hsd->State = HAL_SD_STATE_READY;
1145 return HAL_ERROR;
1148 return HAL_OK;
1150 else
1152 return HAL_BUSY;
1157 * @brief Writes block(s) to a specified address in a card. The Data transfer
1158 * is managed by DMA mode.
1159 * @note This API should be followed by a check on the card state through
1160 * HAL_SD_GetCardState().
1161 * @note You could also check the DMA transfer process through the SD Tx
1162 * interrupt event.
1163 * @param hsd: Pointer to SD handle
1164 * @param pData: Pointer to the buffer that will contain the data to transmit
1165 * @param BlockAdd: Block Address where data will be written
1166 * @param NumberOfBlocks: Number of blocks to write
1167 * @retval HAL status
1169 HAL_StatusTypeDef HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
1171 SDIO_DataInitTypeDef config;
1172 uint32_t errorstate = HAL_SD_ERROR_NONE;
1174 if(NULL == pData)
1176 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1177 return HAL_ERROR;
1180 if(hsd->State == HAL_SD_STATE_READY)
1182 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
1184 if((BlockAdd + NumberOfBlocks) > (hsd->SdCard.LogBlockNbr))
1186 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1187 return HAL_ERROR;
1190 hsd->State = HAL_SD_STATE_BUSY;
1192 /* Initialize data control register */
1193 hsd->Instance->DCTRL = 0U;
1195 /* Enable SD Error interrupts */
1196 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR));
1198 /* Set the DMA transfer complete callback */
1199 hsd->hdmatx->XferCpltCallback = SD_DMATransmitCplt;
1201 /* Set the DMA error callback */
1202 hsd->hdmatx->XferErrorCallback = SD_DMAError;
1204 /* Set the DMA Abort callback */
1205 hsd->hdmatx->XferAbortCallback = NULL;
1207 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1209 BlockAdd *= 512U;
1212 /* Set Block Size for Card */
1213 errorstate = SDMMC_CmdBlockLength(hsd->Instance, BLOCKSIZE);
1214 if(errorstate != HAL_SD_ERROR_NONE)
1216 /* Clear all the static flags */
1217 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1218 hsd->ErrorCode |= errorstate;
1219 hsd->State = HAL_SD_STATE_READY;
1220 return HAL_ERROR;
1223 /* Write Blocks in Polling mode */
1224 if(NumberOfBlocks > 1U)
1226 hsd->Context = (SD_CONTEXT_WRITE_MULTIPLE_BLOCK | SD_CONTEXT_DMA);
1228 /* Write Multi Block command */
1229 errorstate = SDMMC_CmdWriteMultiBlock(hsd->Instance, BlockAdd);
1231 else
1233 hsd->Context = (SD_CONTEXT_WRITE_SINGLE_BLOCK | SD_CONTEXT_DMA);
1235 /* Write Single Block command */
1236 errorstate = SDMMC_CmdWriteSingleBlock(hsd->Instance, BlockAdd);
1238 if(errorstate != HAL_SD_ERROR_NONE)
1240 /* Clear all the static flags */
1241 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1242 hsd->ErrorCode |= errorstate;
1243 hsd->State = HAL_SD_STATE_READY;
1244 return HAL_ERROR;
1247 /* Enable SDIO DMA transfer */
1248 __HAL_SD_DMA_ENABLE(hsd);
1250 /* Enable the DMA Channel */
1251 HAL_DMA_Start_IT(hsd->hdmatx, (uint32_t)pData, (uint32_t)&hsd->Instance->FIFO, (uint32_t)(BLOCKSIZE * NumberOfBlocks)/4);
1253 /* Configure the SD DPSM (Data Path State Machine) */
1254 config.DataTimeOut = SDMMC_DATATIMEOUT;
1255 config.DataLength = BLOCKSIZE * NumberOfBlocks;
1256 config.DataBlockSize = SDIO_DATABLOCK_SIZE_512B;
1257 config.TransferDir = SDIO_TRANSFER_DIR_TO_CARD;
1258 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
1259 config.DPSM = SDIO_DPSM_ENABLE;
1260 SDIO_ConfigData(hsd->Instance, &config);
1262 return HAL_OK;
1264 else
1266 return HAL_BUSY;
1271 * @brief Erases the specified memory area of the given SD card.
1272 * @note This API should be followed by a check on the card state through
1273 * HAL_SD_GetCardState().
1274 * @param hsd: Pointer to SD handle
1275 * @param BlockStartAdd: Start Block address
1276 * @param BlockEndAdd: End Block address
1277 * @retval HAL status
1279 HAL_StatusTypeDef HAL_SD_Erase(SD_HandleTypeDef *hsd, uint32_t BlockStartAdd, uint32_t BlockEndAdd)
1281 uint32_t errorstate = HAL_SD_ERROR_NONE;
1283 if(hsd->State == HAL_SD_STATE_READY)
1285 hsd->ErrorCode = HAL_DMA_ERROR_NONE;
1287 if(BlockEndAdd < BlockStartAdd)
1289 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
1290 return HAL_ERROR;
1293 if(BlockEndAdd > (hsd->SdCard.LogBlockNbr))
1295 hsd->ErrorCode |= HAL_SD_ERROR_ADDR_OUT_OF_RANGE;
1296 return HAL_ERROR;
1299 hsd->State = HAL_SD_STATE_BUSY;
1301 /* Check if the card command class supports erase command */
1302 if(((hsd->SdCard.Class) & SDIO_CCCC_ERASE) == 0U)
1304 /* Clear all the static flags */
1305 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1306 hsd->ErrorCode |= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
1307 hsd->State = HAL_SD_STATE_READY;
1308 return HAL_ERROR;
1311 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
1313 /* Clear all the static flags */
1314 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1315 hsd->ErrorCode |= HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
1316 hsd->State = HAL_SD_STATE_READY;
1317 return HAL_ERROR;
1320 /* Get start and end block for high capacity cards */
1321 if(hsd->SdCard.CardType != CARD_SDHC_SDXC)
1323 BlockStartAdd *= 512U;
1324 BlockEndAdd *= 512U;
1327 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1328 if(hsd->SdCard.CardType != CARD_SECURED)
1330 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1331 errorstate = SDMMC_CmdSDEraseStartAdd(hsd->Instance, BlockStartAdd);
1332 if(errorstate != HAL_SD_ERROR_NONE)
1334 /* Clear all the static flags */
1335 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1336 hsd->ErrorCode |= errorstate;
1337 hsd->State = HAL_SD_STATE_READY;
1338 return HAL_ERROR;
1341 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1342 errorstate = SDMMC_CmdSDEraseEndAdd(hsd->Instance, BlockEndAdd);
1343 if(errorstate != HAL_SD_ERROR_NONE)
1345 /* Clear all the static flags */
1346 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1347 hsd->ErrorCode |= errorstate;
1348 hsd->State = HAL_SD_STATE_READY;
1349 return HAL_ERROR;
1353 /* Send CMD38 ERASE */
1354 errorstate = SDMMC_CmdErase(hsd->Instance);
1355 if(errorstate != HAL_SD_ERROR_NONE)
1357 /* Clear all the static flags */
1358 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1359 hsd->ErrorCode |= errorstate;
1360 hsd->State = HAL_SD_STATE_READY;
1361 return HAL_ERROR;
1364 hsd->State = HAL_SD_STATE_READY;
1366 return HAL_OK;
1368 else
1370 return HAL_BUSY;
1375 * @brief This function handles SD card interrupt request.
1376 * @param hsd: Pointer to SD handle
1377 * @retval None
1379 void HAL_SD_IRQHandler(SD_HandleTypeDef *hsd)
1381 uint32_t errorstate = HAL_SD_ERROR_NONE;
1383 /* Check for SDIO interrupt flags */
1384 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DATAEND) != RESET)
1386 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DATAEND);
1388 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1389 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR | SDIO_IT_STBITERR);
1391 if((hsd->Context & SD_CONTEXT_IT) != RESET)
1393 if(((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET))
1395 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1396 if(errorstate != HAL_SD_ERROR_NONE)
1398 hsd->ErrorCode |= errorstate;
1399 HAL_SD_ErrorCallback(hsd);
1403 /* Clear all the static flags */
1404 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1406 hsd->State = HAL_SD_STATE_READY;
1407 if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) != RESET) || ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) != RESET))
1409 HAL_SD_RxCpltCallback(hsd);
1411 else
1413 HAL_SD_TxCpltCallback(hsd);
1416 else if((hsd->Context & SD_CONTEXT_DMA) != RESET)
1418 if((hsd->Context & SD_CONTEXT_WRITE_MULTIPLE_BLOCK) != RESET)
1420 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
1421 if(errorstate != HAL_SD_ERROR_NONE)
1423 hsd->ErrorCode |= errorstate;
1424 HAL_SD_ErrorCallback(hsd);
1427 if(((hsd->Context & SD_CONTEXT_READ_SINGLE_BLOCK) == RESET) && ((hsd->Context & SD_CONTEXT_READ_MULTIPLE_BLOCK) == RESET))
1429 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1430 in the SD DCTRL register */
1431 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
1433 hsd->State = HAL_SD_STATE_READY;
1435 HAL_SD_TxCpltCallback(hsd);
1440 else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_TXFIFOHE) != RESET)
1442 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_TXFIFOHE);
1444 SD_Write_IT(hsd);
1447 else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_RXFIFOHF) != RESET)
1449 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXFIFOHF);
1451 SD_Read_IT(hsd);
1454 else if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_RXOVERR | SDIO_IT_TXUNDERR | SDIO_IT_STBITERR) != RESET)
1456 /* Set Error code */
1457 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DCRCFAIL) != RESET)
1459 hsd->ErrorCode |= HAL_SD_ERROR_DATA_CRC_FAIL;
1461 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_DTIMEOUT) != RESET)
1463 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1465 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_RXOVERR) != RESET)
1467 hsd->ErrorCode |= HAL_SD_ERROR_RX_OVERRUN;
1469 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_TXUNDERR) != RESET)
1471 hsd->ErrorCode |= HAL_SD_ERROR_TX_UNDERRUN;
1473 if(__HAL_SD_GET_FLAG(hsd, SDIO_IT_STBITERR) != RESET)
1475 hsd->ErrorCode |= HAL_SD_ERROR_DATA_TIMEOUT;
1478 /* Clear All flags */
1479 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS | SDIO_FLAG_STBITERR);
1481 /* Disable all interrupts */
1482 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
1483 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR |SDIO_IT_STBITERR);
1485 if((hsd->Context & SD_CONTEXT_DMA) != RESET)
1487 /* Abort the SD DMA Streams */
1488 if(hsd->hdmatx != NULL)
1490 /* Set the DMA Tx abort callback */
1491 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
1492 /* Abort DMA in IT mode */
1493 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
1495 SD_DMATxAbort(hsd->hdmatx);
1498 else if(hsd->hdmarx != NULL)
1500 /* Set the DMA Rx abort callback */
1501 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
1502 /* Abort DMA in IT mode */
1503 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
1505 SD_DMARxAbort(hsd->hdmarx);
1508 else
1510 hsd->ErrorCode = HAL_SD_ERROR_NONE;
1511 hsd->State = HAL_SD_STATE_READY;
1512 HAL_SD_AbortCallback(hsd);
1515 else if((hsd->Context & SD_CONTEXT_IT) != RESET)
1517 /* Set the SD state to ready to be able to start again the process */
1518 hsd->State = HAL_SD_STATE_READY;
1519 HAL_SD_ErrorCallback(hsd);
1525 * @brief return the SD state
1526 * @param hsd: Pointer to sd handle
1527 * @retval HAL state
1529 HAL_SD_StateTypeDef HAL_SD_GetState(SD_HandleTypeDef *hsd)
1531 return hsd->State;
1535 * @brief Return the SD error code
1536 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
1537 * the configuration information.
1538 * @retval SD Error Code
1540 uint32_t HAL_SD_GetError(SD_HandleTypeDef *hsd)
1542 return hsd->ErrorCode;
1546 * @brief Tx Transfer completed callbacks
1547 * @param hsd: Pointer to SD handle
1548 * @retval None
1550 __weak void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
1552 /* Prevent unused argument(s) compilation warning */
1553 UNUSED(hsd);
1555 /* NOTE : This function should not be modified, when the callback is needed,
1556 the HAL_SD_TxCpltCallback can be implemented in the user file
1561 * @brief Rx Transfer completed callbacks
1562 * @param hsd: Pointer SD handle
1563 * @retval None
1565 __weak void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
1567 /* Prevent unused argument(s) compilation warning */
1568 UNUSED(hsd);
1570 /* NOTE : This function should not be modified, when the callback is needed,
1571 the HAL_SD_RxCpltCallback can be implemented in the user file
1576 * @brief SD error callbacks
1577 * @param hsd: Pointer SD handle
1578 * @retval None
1580 __weak void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
1582 /* Prevent unused argument(s) compilation warning */
1583 UNUSED(hsd);
1585 /* NOTE : This function should not be modified, when the callback is needed,
1586 the HAL_SD_ErrorCallback can be implemented in the user file
1591 * @brief SD Abort callbacks
1592 * @param hsd: Pointer SD handle
1593 * @retval None
1595 __weak void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
1597 /* Prevent unused argument(s) compilation warning */
1598 UNUSED(hsd);
1600 /* NOTE : This function should not be modified, when the callback is needed,
1601 the HAL_SD_ErrorCallback can be implemented in the user file
1607 * @}
1610 /** @addtogroup SD_Exported_Functions_Group3
1611 * @brief management functions
1613 @verbatim
1614 ==============================================================================
1615 ##### Peripheral Control functions #####
1616 ==============================================================================
1617 [..]
1618 This subsection provides a set of functions allowing to control the SD card
1619 operations and get the related information
1621 @endverbatim
1622 * @{
1626 * @brief Returns information the information of the card which are stored on
1627 * the CID register.
1628 * @param hsd: Pointer to SD handle
1629 * @param pCID: Pointer to a HAL_SD_CIDTypeDef structure that
1630 * contains all CID register parameters
1631 * @retval HAL status
1633 HAL_StatusTypeDef HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypeDef *pCID)
1635 uint32_t tmp = 0U;
1637 /* Byte 0 */
1638 tmp = (uint8_t)((hsd->CID[0U] & 0xFF000000U) >> 24U);
1639 pCID->ManufacturerID = tmp;
1641 /* Byte 1 */
1642 tmp = (uint8_t)((hsd->CID[0U] & 0x00FF0000U) >> 16U);
1643 pCID->OEM_AppliID = tmp << 8U;
1645 /* Byte 2 */
1646 tmp = (uint8_t)((hsd->CID[0U] & 0x000000FF00U) >> 8U);
1647 pCID->OEM_AppliID |= tmp;
1649 /* Byte 3 */
1650 tmp = (uint8_t)(hsd->CID[0U] & 0x000000FFU);
1651 pCID->ProdName1 = tmp << 24U;
1653 /* Byte 4 */
1654 tmp = (uint8_t)((hsd->CID[1U] & 0xFF000000U) >> 24U);
1655 pCID->ProdName1 |= tmp << 16;
1657 /* Byte 5 */
1658 tmp = (uint8_t)((hsd->CID[1U] & 0x00FF0000U) >> 16U);
1659 pCID->ProdName1 |= tmp << 8U;
1661 /* Byte 6 */
1662 tmp = (uint8_t)((hsd->CID[1U] & 0x0000FF00U) >> 8U);
1663 pCID->ProdName1 |= tmp;
1665 /* Byte 7 */
1666 tmp = (uint8_t)(hsd->CID[1U] & 0x000000FFU);
1667 pCID->ProdName2 = tmp;
1669 /* Byte 8 */
1670 tmp = (uint8_t)((hsd->CID[2U] & 0xFF000000U) >> 24U);
1671 pCID->ProdRev = tmp;
1673 /* Byte 9 */
1674 tmp = (uint8_t)((hsd->CID[2U] & 0x00FF0000U) >> 16U);
1675 pCID->ProdSN = tmp << 24U;
1677 /* Byte 10 */
1678 tmp = (uint8_t)((hsd->CID[2U] & 0x0000FF00U) >> 8U);
1679 pCID->ProdSN |= tmp << 16U;
1681 /* Byte 11 */
1682 tmp = (uint8_t)(hsd->CID[2U] & 0x000000FFU);
1683 pCID->ProdSN |= tmp << 8U;
1685 /* Byte 12 */
1686 tmp = (uint8_t)((hsd->CID[3U] & 0xFF000000U) >> 24U);
1687 pCID->ProdSN |= tmp;
1689 /* Byte 13 */
1690 tmp = (uint8_t)((hsd->CID[3U] & 0x00FF0000U) >> 16U);
1691 pCID->Reserved1 |= (tmp & 0xF0U) >> 4U;
1692 pCID->ManufactDate = (tmp & 0x0FU) << 8U;
1694 /* Byte 14 */
1695 tmp = (uint8_t)((hsd->CID[3U] & 0x0000FF00U) >> 8U);
1696 pCID->ManufactDate |= tmp;
1698 /* Byte 15 */
1699 tmp = (uint8_t)(hsd->CID[3U] & 0x000000FFU);
1700 pCID->CID_CRC = (tmp & 0xFEU) >> 1U;
1701 pCID->Reserved2 = 1U;
1703 return HAL_OK;
1707 * @brief Returns information the information of the card which are stored on
1708 * the CSD register.
1709 * @param hsd: Pointer to SD handle
1710 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
1711 * contains all CSD register parameters
1712 * @retval HAL status
1714 HAL_StatusTypeDef HAL_SD_GetCardCSD(SD_HandleTypeDef *hsd, HAL_SD_CardCSDTypeDef *pCSD)
1716 uint32_t tmp = 0U;
1718 /* Byte 0 */
1719 tmp = (hsd->CSD[0U] & 0xFF000000U) >> 24U;
1720 pCSD->CSDStruct = (uint8_t)((tmp & 0xC0U) >> 6U);
1721 pCSD->SysSpecVersion = (uint8_t)((tmp & 0x3CU) >> 2U);
1722 pCSD->Reserved1 = tmp & 0x03U;
1724 /* Byte 1 */
1725 tmp = (hsd->CSD[0U] & 0x00FF0000U) >> 16U;
1726 pCSD->TAAC = (uint8_t)tmp;
1728 /* Byte 2 */
1729 tmp = (hsd->CSD[0U] & 0x0000FF00U) >> 8U;
1730 pCSD->NSAC = (uint8_t)tmp;
1732 /* Byte 3 */
1733 tmp = hsd->CSD[0U] & 0x000000FFU;
1734 pCSD->MaxBusClkFrec = (uint8_t)tmp;
1736 /* Byte 4 */
1737 tmp = (hsd->CSD[1U] & 0xFF000000U) >> 24U;
1738 pCSD->CardComdClasses = (uint16_t)(tmp << 4U);
1740 /* Byte 5 */
1741 tmp = (hsd->CSD[1U] & 0x00FF0000U) >> 16U;
1742 pCSD->CardComdClasses |= (uint16_t)((tmp & 0xF0U) >> 4U);
1743 pCSD->RdBlockLen = (uint8_t)(tmp & 0x0FU);
1745 /* Byte 6 */
1746 tmp = (hsd->CSD[1U] & 0x0000FF00U) >> 8U;
1747 pCSD->PartBlockRead = (uint8_t)((tmp & 0x80U) >> 7U);
1748 pCSD->WrBlockMisalign = (uint8_t)((tmp & 0x40U) >> 6U);
1749 pCSD->RdBlockMisalign = (uint8_t)((tmp & 0x20U) >> 5U);
1750 pCSD->DSRImpl = (uint8_t)((tmp & 0x10U) >> 4U);
1751 pCSD->Reserved2 = 0U; /*!< Reserved */
1753 if(hsd->SdCard.CardType == CARD_SDSC)
1755 pCSD->DeviceSize = (tmp & 0x03U) << 10U;
1757 /* Byte 7 */
1758 tmp = (uint8_t)(hsd->CSD[1U] & 0x000000FFU);
1759 pCSD->DeviceSize |= (tmp) << 2U;
1761 /* Byte 8 */
1762 tmp = (uint8_t)((hsd->CSD[2U] & 0xFF000000U) >> 24U);
1763 pCSD->DeviceSize |= (tmp & 0xC0U) >> 6U;
1765 pCSD->MaxRdCurrentVDDMin = (tmp & 0x38U) >> 3U;
1766 pCSD->MaxRdCurrentVDDMax = (tmp & 0x07U);
1768 /* Byte 9 */
1769 tmp = (uint8_t)((hsd->CSD[2U] & 0x00FF0000U) >> 16U);
1770 pCSD->MaxWrCurrentVDDMin = (tmp & 0xE0U) >> 5U;
1771 pCSD->MaxWrCurrentVDDMax = (tmp & 0x1CU) >> 2U;
1772 pCSD->DeviceSizeMul = (tmp & 0x03U) << 1U;
1773 /* Byte 10 */
1774 tmp = (uint8_t)((hsd->CSD[2U] & 0x0000FF00U) >> 8U);
1775 pCSD->DeviceSizeMul |= (tmp & 0x80U) >> 7U;
1777 hsd->SdCard.BlockNbr = (pCSD->DeviceSize + 1U) ;
1778 hsd->SdCard.BlockNbr *= (1U << (pCSD->DeviceSizeMul + 2U));
1779 hsd->SdCard.BlockSize = 1U << (pCSD->RdBlockLen);
1781 hsd->SdCard.LogBlockNbr = (hsd->SdCard.BlockNbr) * ((hsd->SdCard.BlockSize) / 512U);
1782 hsd->SdCard.LogBlockSize = 512U;
1784 else if(hsd->SdCard.CardType == CARD_SDHC_SDXC)
1786 /* Byte 7 */
1787 tmp = (uint8_t)(hsd->CSD[1U] & 0x000000FFU);
1788 pCSD->DeviceSize = (tmp & 0x3FU) << 16U;
1790 /* Byte 8 */
1791 tmp = (uint8_t)((hsd->CSD[2U] & 0xFF000000U) >> 24U);
1793 pCSD->DeviceSize |= (tmp << 8U);
1795 /* Byte 9 */
1796 tmp = (uint8_t)((hsd->CSD[2U] & 0x00FF0000U) >> 16U);
1798 pCSD->DeviceSize |= (tmp);
1800 /* Byte 10 */
1801 tmp = (uint8_t)((hsd->CSD[2U] & 0x0000FF00U) >> 8U);
1803 hsd->SdCard.LogBlockNbr = hsd->SdCard.BlockNbr = (((uint64_t)pCSD->DeviceSize + 1U) * 1024U);
1804 hsd->SdCard.LogBlockSize = hsd->SdCard.BlockSize = 512U;
1806 else
1808 /* Clear all the static flags */
1809 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1810 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
1811 hsd->State = HAL_SD_STATE_READY;
1812 return HAL_ERROR;
1815 pCSD->EraseGrSize = (tmp & 0x40U) >> 6U;
1816 pCSD->EraseGrMul = (tmp & 0x3FU) << 1U;
1818 /* Byte 11 */
1819 tmp = (uint8_t)(hsd->CSD[2U] & 0x000000FFU);
1820 pCSD->EraseGrMul |= (tmp & 0x80U) >> 7U;
1821 pCSD->WrProtectGrSize = (tmp & 0x7FU);
1823 /* Byte 12 */
1824 tmp = (uint8_t)((hsd->CSD[3U] & 0xFF000000U) >> 24U);
1825 pCSD->WrProtectGrEnable = (tmp & 0x80U) >> 7U;
1826 pCSD->ManDeflECC = (tmp & 0x60U) >> 5U;
1827 pCSD->WrSpeedFact = (tmp & 0x1CU) >> 2U;
1828 pCSD->MaxWrBlockLen = (tmp & 0x03U) << 2U;
1830 /* Byte 13 */
1831 tmp = (uint8_t)((hsd->CSD[3U] & 0x00FF0000U) >> 16U);
1832 pCSD->MaxWrBlockLen |= (tmp & 0xC0U) >> 6U;
1833 pCSD->WriteBlockPaPartial = (tmp & 0x20U) >> 5U;
1834 pCSD->Reserved3 = 0U;
1835 pCSD->ContentProtectAppli = (tmp & 0x01U);
1837 /* Byte 14 */
1838 tmp = (uint8_t)((hsd->CSD[3U] & 0x0000FF00U) >> 8U);
1839 pCSD->FileFormatGrouop = (tmp & 0x80U) >> 7U;
1840 pCSD->CopyFlag = (tmp & 0x40U) >> 6U;
1841 pCSD->PermWrProtect = (tmp & 0x20U) >> 5U;
1842 pCSD->TempWrProtect = (tmp & 0x10U) >> 4U;
1843 pCSD->FileFormat = (tmp & 0x0CU) >> 2U;
1844 pCSD->ECC = (tmp & 0x03U);
1846 /* Byte 15 */
1847 tmp = (uint8_t)(hsd->CSD[3U] & 0x000000FFU);
1848 pCSD->CSD_CRC = (tmp & 0xFEU) >> 1U;
1849 pCSD->Reserved4 = 1U;
1851 return HAL_OK;
1855 * @brief Gets the SD status info.
1856 * @param hsd: Pointer to SD handle
1857 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
1858 * will contain the SD card status information
1859 * @retval HAL status
1861 HAL_StatusTypeDef HAL_SD_GetCardStatus(SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypeDef *pStatus)
1863 uint32_t tmp = 0U;
1864 uint32_t sd_status[16U];
1865 uint32_t errorstate = HAL_SD_ERROR_NONE;
1867 errorstate = SD_SendSDStatus(hsd, sd_status);
1868 if(errorstate != HAL_OK)
1870 /* Clear all the static flags */
1871 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
1872 hsd->ErrorCode |= errorstate;
1873 hsd->State = HAL_SD_STATE_READY;
1874 return HAL_ERROR;
1876 else
1878 /* Byte 0 */
1879 tmp = (sd_status[0U] & 0xC0U) >> 6U;
1880 pStatus->DataBusWidth = (uint8_t)tmp;
1882 /* Byte 0 */
1883 tmp = (sd_status[0U] & 0x20U) >> 5U;
1884 pStatus->SecuredMode = (uint8_t)tmp;
1886 /* Byte 2 */
1887 tmp = (sd_status[0U] & 0x00FF0000U) >> 16U;
1888 pStatus->CardType = (uint16_t)(tmp << 8U);
1890 /* Byte 3 */
1891 tmp = (sd_status[0U] & 0xFF000000U) >> 24U;
1892 pStatus->CardType |= (uint16_t)tmp;
1894 /* Byte 4 */
1895 tmp = (sd_status[1U] & 0xFFU);
1896 pStatus->ProtectedAreaSize = (uint32_t)(tmp << 24U);
1898 /* Byte 5 */
1899 tmp = (sd_status[1U] & 0xFF00U) >> 8U;
1900 pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 16U);
1902 /* Byte 6 */
1903 tmp = (sd_status[1U] & 0xFF0000U) >> 16U;
1904 pStatus->ProtectedAreaSize |= (uint32_t)(tmp << 8U);
1906 /* Byte 7 */
1907 tmp = (sd_status[1U] & 0xFF000000U) >> 24U;
1908 pStatus->ProtectedAreaSize |= (uint32_t)tmp;
1910 /* Byte 8 */
1911 tmp = (sd_status[2U] & 0xFFU);
1912 pStatus->SpeedClass = (uint8_t)tmp;
1914 /* Byte 9 */
1915 tmp = (sd_status[2U] & 0xFF00U) >> 8U;
1916 pStatus->PerformanceMove = (uint8_t)tmp;
1918 /* Byte 10 */
1919 tmp = (sd_status[2U] & 0xF00000U) >> 20U;
1920 pStatus->AllocationUnitSize = (uint8_t)tmp;
1922 /* Byte 11 */
1923 tmp = (sd_status[2U] & 0xFF000000U) >> 24U;
1924 pStatus->EraseSize = (uint16_t)(tmp << 8U);
1926 /* Byte 12 */
1927 tmp = (sd_status[3U] & 0xFFU);
1928 pStatus->EraseSize |= (uint16_t)tmp;
1930 /* Byte 13 */
1931 tmp = (sd_status[3U] & 0xFC00U) >> 10U;
1932 pStatus->EraseTimeout = (uint8_t)tmp;
1934 /* Byte 13 */
1935 tmp = (sd_status[3U] & 0x0300U) >> 8U;
1936 pStatus->EraseOffset = (uint8_t)tmp;
1939 return HAL_OK;
1943 * @brief Gets the SD card info.
1944 * @param hsd: Pointer to SD handle
1945 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
1946 * will contain the SD card status information
1947 * @retval HAL status
1949 HAL_StatusTypeDef HAL_SD_GetCardInfo(SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypeDef *pCardInfo)
1951 pCardInfo->CardType = (uint32_t)(hsd->SdCard.CardType);
1952 pCardInfo->CardVersion = (uint32_t)(hsd->SdCard.CardVersion);
1953 pCardInfo->Class = (uint32_t)(hsd->SdCard.Class);
1954 pCardInfo->RelCardAdd = (uint32_t)(hsd->SdCard.RelCardAdd);
1955 pCardInfo->BlockNbr = (uint32_t)(hsd->SdCard.BlockNbr);
1956 pCardInfo->BlockSize = (uint32_t)(hsd->SdCard.BlockSize);
1957 pCardInfo->LogBlockNbr = (uint32_t)(hsd->SdCard.LogBlockNbr);
1958 pCardInfo->LogBlockSize = (uint32_t)(hsd->SdCard.LogBlockSize);
1960 return HAL_OK;
1964 * @brief Enables wide bus operation for the requested card if supported by
1965 * card.
1966 * @param hsd: Pointer to SD handle
1967 * @param WideMode: Specifies the SD card wide bus mode
1968 * This parameter can be one of the following values:
1969 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
1970 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
1971 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
1972 * @retval HAL status
1974 HAL_StatusTypeDef HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef *hsd, uint32_t WideMode)
1976 SDIO_InitTypeDef Init;
1977 uint32_t errorstate = HAL_SD_ERROR_NONE;
1979 /* Check the parameters */
1980 assert_param(IS_SDIO_BUS_WIDE(WideMode));
1982 /* Chnage Satte */
1983 hsd->State = HAL_SD_STATE_BUSY;
1985 if(hsd->SdCard.CardType != CARD_SECURED)
1987 if(WideMode == SDIO_BUS_WIDE_8B)
1989 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
1991 else if(WideMode == SDIO_BUS_WIDE_4B)
1993 errorstate = SD_WideBus_Enable(hsd);
1995 hsd->ErrorCode |= errorstate;
1997 else if(WideMode == SDIO_BUS_WIDE_1B)
1999 errorstate = SD_WideBus_Disable(hsd);
2001 hsd->ErrorCode |= errorstate;
2003 else
2005 /* WideMode is not a valid argument*/
2006 hsd->ErrorCode |= HAL_SD_ERROR_PARAM;
2009 else
2011 /* MMC Card does not support this feature */
2012 hsd->ErrorCode |= HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2015 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2017 /* Clear all the static flags */
2018 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2019 hsd->State = HAL_SD_STATE_READY;
2020 return HAL_ERROR;
2022 else
2024 /* Configure the SDIO peripheral */
2025 Init.ClockEdge = hsd->Init.ClockEdge;
2026 Init.ClockBypass = hsd->Init.ClockBypass;
2027 Init.ClockPowerSave = hsd->Init.ClockPowerSave;
2028 Init.BusWide = WideMode;
2029 Init.HardwareFlowControl = hsd->Init.HardwareFlowControl;
2030 Init.ClockDiv = hsd->Init.ClockDiv;
2031 SDIO_Init(hsd->Instance, Init);
2034 /* Change State */
2035 hsd->State = HAL_SD_STATE_READY;
2037 return HAL_OK;
2042 * @brief Gets the current sd card data state.
2043 * @param hsd: pointer to SD handle
2044 * @retval Card state
2046 HAL_SD_CardStateTypeDef HAL_SD_GetCardState(SD_HandleTypeDef *hsd)
2048 HAL_SD_CardStateTypeDef cardstate = HAL_SD_CARD_TRANSFER;
2049 uint32_t errorstate = HAL_SD_ERROR_NONE;
2050 uint32_t resp1 = 0;
2052 errorstate = SD_SendStatus(hsd, &resp1);
2053 if(errorstate != HAL_OK)
2055 hsd->ErrorCode |= errorstate;
2058 cardstate = (HAL_SD_CardStateTypeDef)((resp1 >> 9U) & 0x0FU);
2060 return cardstate;
2064 * @brief Abort the current transfer and disable the SD.
2065 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2066 * the configuration information for SD module.
2067 * @retval HAL status
2069 HAL_StatusTypeDef HAL_SD_Abort(SD_HandleTypeDef *hsd)
2071 HAL_SD_CardStateTypeDef CardState;
2073 /* DIsable All interrupts */
2074 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2075 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2077 /* Clear All flags */
2078 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2080 if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL))
2082 /* Disable the SD DMA request */
2083 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2085 /* Abort the SD DMA Tx Stream */
2086 if(hsd->hdmatx != NULL)
2088 HAL_DMA_Abort(hsd->hdmatx);
2090 /* Abort the SD DMA Rx Stream */
2091 if(hsd->hdmarx != NULL)
2093 HAL_DMA_Abort(hsd->hdmarx);
2097 hsd->State = HAL_SD_STATE_READY;
2098 CardState = HAL_SD_GetCardState(hsd);
2099 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2101 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2103 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2105 return HAL_ERROR;
2107 return HAL_OK;
2111 * @brief Abort the current transfer and disable the SD (IT mode).
2112 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2113 * the configuration information for SD module.
2114 * @retval HAL status
2116 HAL_StatusTypeDef HAL_SD_Abort_IT(SD_HandleTypeDef *hsd)
2118 HAL_SD_CardStateTypeDef CardState;
2120 /* DIsable All interrupts */
2121 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2122 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2124 /* Clear All flags */
2125 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2127 if((hsd->hdmatx != NULL) || (hsd->hdmarx != NULL))
2129 /* Disable the SD DMA request */
2130 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2132 /* Abort the SD DMA Tx Stream */
2133 if(hsd->hdmatx != NULL)
2135 hsd->hdmatx->XferAbortCallback = SD_DMATxAbort;
2136 if(HAL_DMA_Abort_IT(hsd->hdmatx) != HAL_OK)
2138 hsd->hdmatx = NULL;
2141 /* Abort the SD DMA Rx Stream */
2142 if(hsd->hdmarx != NULL)
2144 hsd->hdmarx->XferAbortCallback = SD_DMARxAbort;
2145 if(HAL_DMA_Abort_IT(hsd->hdmarx) != HAL_OK)
2147 hsd->hdmarx = NULL;
2152 /* No transfer ongoing on both DMA channels*/
2153 if((hsd->hdmatx == NULL) && (hsd->hdmarx == NULL))
2155 CardState = HAL_SD_GetCardState(hsd);
2156 hsd->State = HAL_SD_STATE_READY;
2157 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2159 hsd->ErrorCode = SDMMC_CmdStopTransfer(hsd->Instance);
2161 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2163 return HAL_ERROR;
2165 else
2167 HAL_SD_AbortCallback(hsd);
2171 return HAL_OK;
2175 * @}
2179 * @}
2182 /* Private function ----------------------------------------------------------*/
2183 /** @addtogroup SD_Private_Functions
2184 * @{
2188 * @brief DMA SD transmit process complete callback
2189 * @param hdma: DMA handle
2190 * @retval None
2192 static void SD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2194 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2196 /* Enable DATAEND Interrupt */
2197 __HAL_SD_ENABLE_IT(hsd, (SDIO_IT_DATAEND));
2201 * @brief DMA SD receive process complete callback
2202 * @param hdma: DMA handle
2203 * @retval None
2205 static void SD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2207 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2208 uint32_t errorstate = HAL_SD_ERROR_NONE;
2210 /* Send stop command in multiblock write */
2211 if(hsd->Context == (SD_CONTEXT_READ_MULTIPLE_BLOCK | SD_CONTEXT_DMA))
2213 errorstate = SDMMC_CmdStopTransfer(hsd->Instance);
2214 if(errorstate != HAL_SD_ERROR_NONE)
2216 hsd->ErrorCode |= errorstate;
2217 HAL_SD_ErrorCallback(hsd);
2221 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2222 in the SD DCTRL register */
2223 hsd->Instance->DCTRL &= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN);
2225 /* Clear all the static flags */
2226 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2228 hsd->State = HAL_SD_STATE_READY;
2230 HAL_SD_RxCpltCallback(hsd);
2234 * @brief DMA SD communication error callback
2235 * @param hdma: DMA handle
2236 * @retval None
2238 static void SD_DMAError(DMA_HandleTypeDef *hdma)
2240 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2241 HAL_SD_CardStateTypeDef CardState;
2243 if((hsd->hdmarx->ErrorCode == HAL_DMA_ERROR_TE) || (hsd->hdmatx->ErrorCode == HAL_DMA_ERROR_TE))
2245 /* Clear All flags */
2246 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2248 /* Disable All interrupts */
2249 __HAL_SD_DISABLE_IT(hsd, SDIO_IT_DATAEND | SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT|\
2250 SDIO_IT_TXUNDERR| SDIO_IT_RXOVERR);
2252 hsd->ErrorCode |= HAL_SD_ERROR_DMA;
2253 CardState = HAL_SD_GetCardState(hsd);
2254 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2256 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2259 hsd->State= HAL_SD_STATE_READY;
2262 HAL_SD_ErrorCallback(hsd);
2266 * @brief DMA SD Tx Abort callback
2267 * @param hdma: DMA handle
2268 * @retval None
2270 static void SD_DMATxAbort(DMA_HandleTypeDef *hdma)
2272 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2273 HAL_SD_CardStateTypeDef CardState;
2275 if(hsd->hdmatx != NULL)
2277 hsd->hdmatx = NULL;
2280 /* All DMA channels are aborted */
2281 if(hsd->hdmarx == NULL)
2283 CardState = HAL_SD_GetCardState(hsd);
2284 hsd->ErrorCode = HAL_SD_ERROR_NONE;
2285 hsd->State = HAL_SD_STATE_READY;
2286 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2288 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2290 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2292 HAL_SD_AbortCallback(hsd);
2294 else
2296 HAL_SD_ErrorCallback(hsd);
2303 * @brief DMA SD Rx Abort callback
2304 * @param hdma: DMA handle
2305 * @retval None
2307 static void SD_DMARxAbort(DMA_HandleTypeDef *hdma)
2309 SD_HandleTypeDef* hsd = (SD_HandleTypeDef* )(hdma->Parent);
2310 HAL_SD_CardStateTypeDef CardState;
2312 if(hsd->hdmarx != NULL)
2314 hsd->hdmarx = NULL;
2317 /* All DMA channels are aborted */
2318 if(hsd->hdmatx == NULL)
2320 CardState = HAL_SD_GetCardState(hsd);
2321 hsd->ErrorCode = HAL_SD_ERROR_NONE;
2322 hsd->State = HAL_SD_STATE_READY;
2323 if((CardState == HAL_SD_CARD_RECEIVING) || (CardState == HAL_SD_CARD_SENDING))
2325 hsd->ErrorCode |= SDMMC_CmdStopTransfer(hsd->Instance);
2327 if(hsd->ErrorCode != HAL_SD_ERROR_NONE)
2329 HAL_SD_AbortCallback(hsd);
2331 else
2333 HAL_SD_ErrorCallback(hsd);
2341 * @brief Initializes the sd card.
2342 * @param hsd: Pointer to SD handle
2343 * @retval SD Card error state
2345 static uint32_t SD_InitCard(SD_HandleTypeDef *hsd)
2347 HAL_SD_CardCSDTypeDef CSD;
2348 uint32_t errorstate = HAL_SD_ERROR_NONE;
2349 uint16_t sd_rca = 1U;
2351 /* Check the power State */
2352 if(SDIO_GetPowerState(hsd->Instance) == 0U)
2354 /* Power off */
2355 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2358 if(hsd->SdCard.CardType != CARD_SECURED)
2360 /* Send CMD2 ALL_SEND_CID */
2361 errorstate = SDMMC_CmdSendCID(hsd->Instance);
2362 if(errorstate != HAL_SD_ERROR_NONE)
2364 return errorstate;
2366 else
2368 /* Get Card identification number data */
2369 hsd->CID[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2370 hsd->CID[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2371 hsd->CID[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2372 hsd->CID[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2376 if(hsd->SdCard.CardType != CARD_SECURED)
2378 /* Send CMD3 SET_REL_ADDR with argument 0 */
2379 /* SD Card publishes its RCA. */
2380 errorstate = SDMMC_CmdSetRelAdd(hsd->Instance, &sd_rca);
2381 if(errorstate != HAL_SD_ERROR_NONE)
2383 return errorstate;
2386 if(hsd->SdCard.CardType != CARD_SECURED)
2388 /* Get the SD card RCA */
2389 hsd->SdCard.RelCardAdd = sd_rca;
2391 /* Send CMD9 SEND_CSD with argument as card's RCA */
2392 errorstate = SDMMC_CmdSendCSD(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2393 if(errorstate != HAL_SD_ERROR_NONE)
2395 return errorstate;
2397 else
2399 /* Get Card Specific Data */
2400 hsd->CSD[0U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2401 hsd->CSD[1U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP2);
2402 hsd->CSD[2U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP3);
2403 hsd->CSD[3U] = SDIO_GetResponse(hsd->Instance, SDIO_RESP4);
2407 /* Get the Card Class */
2408 hsd->SdCard.Class = (SDIO_GetResponse(hsd->Instance, SDIO_RESP2) >> 20U);
2410 /* Get CSD parameters */
2411 HAL_SD_GetCardCSD(hsd, &CSD);
2413 /* Select the Card */
2414 errorstate = SDMMC_CmdSelDesel(hsd->Instance, (uint32_t)(((uint32_t)hsd->SdCard.RelCardAdd) << 16U));
2415 if(errorstate != HAL_SD_ERROR_NONE)
2417 return errorstate;
2420 /* Configure SDIO peripheral interface */
2421 SDIO_Init(hsd->Instance, hsd->Init);
2423 /* All cards are initialized */
2424 return HAL_SD_ERROR_NONE;
2428 * @brief Enquires cards about their operating voltage and configures clock
2429 * controls and stores SD information that will be needed in future
2430 * in the SD handle.
2431 * @param hsd: Pointer to SD handle
2432 * @retval error state
2434 static uint32_t SD_PowerON(SD_HandleTypeDef *hsd)
2436 __IO uint32_t count = 0U;
2437 uint32_t response = 0U, validvoltage = 0U;
2438 uint32_t errorstate = HAL_SD_ERROR_NONE;
2440 /* CMD0: GO_IDLE_STATE */
2441 errorstate = SDMMC_CmdGoIdleState(hsd->Instance);
2442 if(errorstate != HAL_SD_ERROR_NONE)
2444 return errorstate;
2447 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2448 errorstate = SDMMC_CmdOperCond(hsd->Instance);
2449 if(errorstate != HAL_SD_ERROR_NONE)
2451 hsd->SdCard.CardVersion = CARD_V1_X;
2453 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2454 while(validvoltage == 0U)
2456 if(count++ == SDMMC_MAX_VOLT_TRIAL)
2458 return HAL_SD_ERROR_INVALID_VOLTRANGE;
2461 /* SEND CMD55 APP_CMD with RCA as 0 */
2462 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0U);
2463 if(errorstate != HAL_SD_ERROR_NONE)
2465 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2468 /* Send CMD41 */
2469 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_STD_CAPACITY);
2470 if(errorstate != HAL_SD_ERROR_NONE)
2472 return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
2475 /* Get command response */
2476 response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2478 /* Get operating voltage*/
2479 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
2481 /* Card type is SDSC */
2482 hsd->SdCard.CardType = CARD_SDSC;
2484 else
2486 hsd->SdCard.CardVersion = CARD_V2_X;
2488 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2489 while(validvoltage == 0U)
2491 if(count++ == SDMMC_MAX_VOLT_TRIAL)
2493 return HAL_SD_ERROR_INVALID_VOLTRANGE;
2496 /* SEND CMD55 APP_CMD with RCA as 0 */
2497 errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0U);
2498 if(errorstate != HAL_SD_ERROR_NONE)
2500 return errorstate;
2503 /* Send CMD41 */
2504 errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_HIGH_CAPACITY);
2505 if(errorstate != HAL_SD_ERROR_NONE)
2507 return errorstate;
2510 /* Get command response */
2511 response = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2513 /* Get operating voltage*/
2514 validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);
2517 if((response & SDMMC_HIGH_CAPACITY) == SDMMC_HIGH_CAPACITY) /* (response &= SD_HIGH_CAPACITY) */
2519 hsd->SdCard.CardType = CARD_SDHC_SDXC;
2521 else
2523 hsd->SdCard.CardType = CARD_SDSC;
2527 return HAL_SD_ERROR_NONE;
2531 * @brief Turns the SDIO output signals off.
2532 * @param hsd: Pointer to SD handle
2533 * @retval HAL status
2535 static HAL_StatusTypeDef SD_PowerOFF(SD_HandleTypeDef *hsd)
2537 /* Set Power State to OFF */
2538 SDIO_PowerState_OFF(hsd->Instance);
2540 return HAL_OK;
2544 * @brief Send Status info command.
2545 * @param hsd: pointer to SD handle
2546 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
2547 * SD Status register)
2548 * @retval error state
2550 static uint32_t SD_SendSDStatus(SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
2552 SDIO_DataInitTypeDef config;
2553 uint32_t errorstate = HAL_SD_ERROR_NONE;
2554 uint32_t tickstart = HAL_GetTick();
2555 uint32_t count = 0U;
2557 /* Check SD response */
2558 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2560 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2563 /* Set block size for card if it is not equal to current block size for card */
2564 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 64U);
2565 if(errorstate != HAL_SD_ERROR_NONE)
2567 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2568 return errorstate;
2571 /* Send CMD55 */
2572 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2573 if(errorstate != HAL_SD_ERROR_NONE)
2575 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2576 return errorstate;
2579 /* Configure the SD DPSM (Data Path State Machine) */
2580 config.DataTimeOut = SDMMC_DATATIMEOUT;
2581 config.DataLength = 64U;
2582 config.DataBlockSize = SDIO_DATABLOCK_SIZE_64B;
2583 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
2584 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
2585 config.DPSM = SDIO_DPSM_ENABLE;
2586 SDIO_ConfigData(hsd->Instance, &config);
2588 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
2589 errorstate = SDMMC_CmdStatusRegister(hsd->Instance);
2590 if(errorstate != HAL_SD_ERROR_NONE)
2592 hsd->ErrorCode |= HAL_SD_ERROR_NONE;
2593 return errorstate;
2596 /* Get status data */
2597 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
2599 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXFIFOHF))
2601 for(count = 0U; count < 8U; count++)
2603 *(pSDstatus + count) = SDIO_ReadFIFO(hsd->Instance);
2606 pSDstatus += 8U;
2609 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2611 return HAL_SD_ERROR_TIMEOUT;
2615 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
2617 return HAL_SD_ERROR_DATA_TIMEOUT;
2619 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
2621 return HAL_SD_ERROR_DATA_CRC_FAIL;
2623 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
2625 return HAL_SD_ERROR_RX_OVERRUN;
2628 while ((__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL)))
2630 *pSDstatus = SDIO_ReadFIFO(hsd->Instance);
2631 pSDstatus++;
2633 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2635 return HAL_SD_ERROR_TIMEOUT;
2639 /* Clear all the static status flags*/
2640 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2642 return HAL_SD_ERROR_NONE;
2646 * @brief Returns the current card's status.
2647 * @param hsd: Pointer to SD handle
2648 * @param pCardStatus: pointer to the buffer that will contain the SD card
2649 * status (Card Status register)
2650 * @retval error state
2652 static uint32_t SD_SendStatus(SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
2654 uint32_t errorstate = HAL_SD_ERROR_NONE;
2656 if(pCardStatus == NULL)
2658 return HAL_SD_ERROR_PARAM;
2661 /* Send Status command */
2662 errorstate = SDMMC_CmdSendStatus(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2663 if(errorstate != HAL_OK)
2665 return errorstate;
2668 /* Get SD card status */
2669 *pCardStatus = SDIO_GetResponse(hsd->Instance, SDIO_RESP1);
2671 return HAL_SD_ERROR_NONE;
2675 * @brief Enables the SDIO wide bus mode.
2676 * @param hsd: pointer to SD handle
2677 * @retval error state
2679 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef *hsd)
2681 uint32_t scr[2U] = {0U, 0U};
2682 uint32_t errorstate = HAL_SD_ERROR_NONE;
2684 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2686 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2689 /* Get SCR Register */
2690 errorstate = SD_FindSCR(hsd, scr);
2691 if(errorstate != HAL_OK)
2693 return errorstate;
2696 /* If requested card supports wide bus operation */
2697 if((scr[1U] & SDMMC_WIDE_BUS_SUPPORT) != SDMMC_ALLZERO)
2699 /* Send CMD55 APP_CMD with argument as card's RCA.*/
2700 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2701 if(errorstate != HAL_OK)
2703 return errorstate;
2706 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
2707 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 2U);
2708 if(errorstate != HAL_OK)
2710 return errorstate;
2713 return HAL_SD_ERROR_NONE;
2715 else
2717 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2722 * @brief Disables the SDIO wide bus mode.
2723 * @param hsd: Pointer to SD handle
2724 * @retval error state
2726 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef *hsd)
2728 uint32_t scr[2U] = {0U, 0U};
2729 uint32_t errorstate = HAL_SD_ERROR_NONE;
2731 if((SDIO_GetResponse(hsd->Instance, SDIO_RESP1) & SDMMC_CARD_LOCKED) == SDMMC_CARD_LOCKED)
2733 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED;
2736 /* Get SCR Register */
2737 errorstate = SD_FindSCR(hsd, scr);
2738 if(errorstate != HAL_OK)
2740 return errorstate;
2743 /* If requested card supports 1 bit mode operation */
2744 if((scr[1U] & SDMMC_SINGLE_BUS_SUPPORT) != SDMMC_ALLZERO)
2746 /* Send CMD55 APP_CMD with argument as card's RCA */
2747 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)(hsd->SdCard.RelCardAdd << 16U));
2748 if(errorstate != HAL_OK)
2750 return errorstate;
2753 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
2754 errorstate = SDMMC_CmdBusWidth(hsd->Instance, 0U);
2755 if(errorstate != HAL_OK)
2757 return errorstate;
2760 return HAL_SD_ERROR_NONE;
2762 else
2764 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE;
2770 * @brief Finds the SD card SCR register value.
2771 * @param hsd: Pointer to SD handle
2772 * @param pSCR: pointer to the buffer that will contain the SCR value
2773 * @retval error state
2775 static uint32_t SD_FindSCR(SD_HandleTypeDef *hsd, uint32_t *pSCR)
2777 SDIO_DataInitTypeDef config;
2778 uint32_t errorstate = HAL_SD_ERROR_NONE;
2779 uint32_t tickstart = HAL_GetTick();
2780 uint32_t index = 0U;
2781 uint32_t tempscr[2U] = {0U, 0U};
2783 /* Set Block Size To 8 Bytes */
2784 errorstate = SDMMC_CmdBlockLength(hsd->Instance, 8U);
2785 if(errorstate != HAL_OK)
2787 return errorstate;
2790 /* Send CMD55 APP_CMD with argument as card's RCA */
2791 errorstate = SDMMC_CmdAppCommand(hsd->Instance, (uint32_t)((hsd->SdCard.RelCardAdd) << 16U));
2792 if(errorstate != HAL_OK)
2794 return errorstate;
2797 config.DataTimeOut = SDMMC_DATATIMEOUT;
2798 config.DataLength = 8U;
2799 config.DataBlockSize = SDIO_DATABLOCK_SIZE_8B;
2800 config.TransferDir = SDIO_TRANSFER_DIR_TO_SDIO;
2801 config.TransferMode = SDIO_TRANSFER_MODE_BLOCK;
2802 config.DPSM = SDIO_DPSM_ENABLE;
2803 SDIO_ConfigData(hsd->Instance, &config);
2805 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
2806 errorstate = SDMMC_CmdSendSCR(hsd->Instance);
2807 if(errorstate != HAL_OK)
2809 return errorstate;
2812 while(!__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND))
2814 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
2816 *(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
2817 index++;
2820 if((HAL_GetTick() - tickstart) >= SDMMC_DATATIMEOUT)
2822 return HAL_SD_ERROR_TIMEOUT;
2826 if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DTIMEOUT))
2828 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DTIMEOUT);
2830 return HAL_SD_ERROR_DATA_TIMEOUT;
2832 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_DCRCFAIL))
2834 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_DCRCFAIL);
2836 return HAL_SD_ERROR_DATA_CRC_FAIL;
2838 else if(__HAL_SD_GET_FLAG(hsd, SDIO_FLAG_RXOVERR))
2840 __HAL_SD_CLEAR_FLAG(hsd, SDIO_FLAG_RXOVERR);
2842 return HAL_SD_ERROR_RX_OVERRUN;
2844 else
2846 /* No error flag set */
2847 /* Clear all the static flags */
2848 __HAL_SD_CLEAR_FLAG(hsd, SDIO_STATIC_FLAGS);
2850 *(pSCR + 1U) = ((tempscr[0U] & SDMMC_0TO7BITS) << 24U) | ((tempscr[0U] & SDMMC_8TO15BITS) << 8U) |\
2851 ((tempscr[0U] & SDMMC_16TO23BITS) >> 8U) | ((tempscr[0U] & SDMMC_24TO31BITS) >> 24U);
2853 *(pSCR) = ((tempscr[1U] & SDMMC_0TO7BITS) << 24U) | ((tempscr[1U] & SDMMC_8TO15BITS) << 8U) |\
2854 ((tempscr[1U] & SDMMC_16TO23BITS) >> 8U) | ((tempscr[1U] & SDMMC_24TO31BITS) >> 24U);
2857 return HAL_SD_ERROR_NONE;
2861 * @brief Wrap up reading in non-blocking mode.
2862 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2863 * the configuration information.
2864 * @retval HAL status
2866 static HAL_StatusTypeDef SD_Read_IT(SD_HandleTypeDef *hsd)
2868 uint32_t count = 0U;
2869 uint32_t* tmp;
2871 tmp = (uint32_t*)hsd->pRxBuffPtr;
2873 /* Read data from SDIO Rx FIFO */
2874 for(count = 0U; count < 8U; count++)
2876 *(tmp + count) = SDIO_ReadFIFO(hsd->Instance);
2879 hsd->pRxBuffPtr += 8U;
2881 return HAL_OK;
2885 * @brief Wrap up writing in non-blocking mode.
2886 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2887 * the configuration information.
2888 * @retval HAL status
2890 static HAL_StatusTypeDef SD_Write_IT(SD_HandleTypeDef *hsd)
2892 uint32_t count = 0U;
2893 uint32_t* tmp;
2895 tmp = (uint32_t*)hsd->pTxBuffPtr;
2897 /* Write data to SDIO Tx FIFO */
2898 for(count = 0U; count < 8U; count++)
2900 SDIO_WriteFIFO(hsd->Instance, (tmp + count));
2903 hsd->pTxBuffPtr += 8U;
2905 return HAL_OK;
2909 * @}
2912 #endif /* STM32F103xE || STM32F103xG */
2914 #endif /* HAL_SD_MODULE_ENABLED */
2917 * @}
2921 * @}
2924 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/