2 ******************************************************************************
3 * @file stm32f7xx_ll_sdmmc.c
4 * @author MCD Application Team
5 * @brief SDMMC Low Layer HAL module driver.
7 * This file provides firmware functions to manage the following
8 * functionalities of the SDMMC peripheral:
9 * + Initialization/de-initialization functions
10 * + I/O operation functions
11 * + Peripheral Control functions
12 * + Peripheral State functions
15 ==============================================================================
16 ##### SDMMC peripheral features #####
17 ==============================================================================
18 [..] The SD/SDMMC MMC card host interface (SDMMC) provides an interface between the APB2
19 peripheral bus and MultiMedia cards (MMCs), SD memory cards, SDMMC cards and CE-ATA
22 [..] The SDMMC features include the following:
23 (+) Full compliance with MultiMedia Card System Specification Version 4.2. Card support
24 for three different databus modes: 1-bit (default), 4-bit and 8-bit
25 (+) Full compatibility with previous versions of MultiMedia Cards (forward compatibility)
26 (+) Full compliance with SD Memory Card Specifications Version 2.0
27 (+) Full compliance with SD I/O Card Specification Version 2.0: card support for two
28 different data bus modes: 1-bit (default) and 4-bit
29 (+) Full support of the CE-ATA features (full compliance with CE-ATA digital protocol
31 (+) Data transfer up to 48 MHz for the 8 bit mode
32 (+) Data and command output enable signals to control external bidirectional drivers.
35 ##### How to use this driver #####
36 ==============================================================================
38 This driver is a considered as a driver of service for external devices drivers
39 that interfaces with the SDMMC peripheral.
40 According to the device used (SD card/ MMC card / SDMMC card ...), a set of APIs
41 is used in the device's driver to perform SDMMC operations and functionalities.
43 This driver is almost transparent for the final user, it is only used to implement other
44 functionalities of the external device.
47 (+) The SDMMC clock (SDMMCCLK = 48 MHz) is coming from a specific output of PLL
48 (PLL48CLK). Before start working with SDMMC peripheral make sure that the
49 PLL is well configured.
50 The SDMMC peripheral uses two clock signals:
51 (++) SDMMC adapter clock (SDMMCCLK = 48 MHz)
52 (++) APB2 bus clock (PCLK2)
54 -@@- PCLK2 and SDMMC_CK clock frequencies must respect the following condition:
55 Frequency(PCLK2) >= (3 / 8 x Frequency(SDMMC_CK))
57 (+) Enable/Disable peripheral clock using RCC peripheral macros related to SDMMC
60 (+) Enable the Power ON State using the SDMMC_PowerState_ON(SDMMCx)
61 function and disable it using the function SDMMC_PowerState_OFF(SDMMCx).
63 (+) Enable/Disable the clock using the __SDMMC_ENABLE()/__SDMMC_DISABLE() macros.
65 (+) Enable/Disable the peripheral interrupts using the macros __SDMMC_ENABLE_IT(hSDMMC, IT)
66 and __SDMMC_DISABLE_IT(hSDMMC, IT) if you need to use interrupt mode.
68 (+) When using the DMA mode
69 (++) Configure the DMA in the MSP layer of the external device
70 (++) Active the needed channel Request
71 (++) Enable the DMA using __SDMMC_DMA_ENABLE() macro or Disable it using the macro
72 __SDMMC_DMA_DISABLE().
74 (+) To control the CPSM (Command Path State Machine) and send
75 commands to the card use the SDMMC_SendCommand(SDMMCx),
76 SDMMC_GetCommandResponse() and SDMMC_GetResponse() functions. First, user has
77 to fill the command structure (pointer to SDMMC_CmdInitTypeDef) according
78 to the selected command to be sent.
79 The parameters that should be filled are:
82 (++) Command Response type
84 (++) CPSM Status (Enable or Disable).
86 -@@- To check if the command is well received, read the SDMMC_CMDRESP
87 register using the SDMMC_GetCommandResponse().
88 The SDMMC responses registers (SDMMC_RESP1 to SDMMC_RESP2), use the
89 SDMMC_GetResponse() function.
91 (+) To control the DPSM (Data Path State Machine) and send/receive
92 data to/from the card use the SDMMC_DataConfig(), SDMMC_GetDataCounter(),
93 SDMMC_ReadFIFO(), SDMMC_WriteFIFO() and SDMMC_GetFIFOCount() functions.
95 *** Read Operations ***
96 =======================
98 (#) First, user has to fill the data structure (pointer to
99 SDMMC_DataInitTypeDef) according to the selected data type to be received.
100 The parameters that should be filled are:
104 (++) Data Transfer direction: should be from card (To SDMMC)
105 (++) Data Transfer mode
106 (++) DPSM Status (Enable or Disable)
108 (#) Configure the SDMMC resources to receive the data from the card
109 according to selected transfer mode (Refer to Step 8, 9 and 10).
111 (#) Send the selected Read command (refer to step 11).
113 (#) Use the SDMMC flags/interrupts to check the transfer status.
115 *** Write Operations ***
116 ========================
118 (#) First, user has to fill the data structure (pointer to
119 SDMMC_DataInitTypeDef) according to the selected data type to be received.
120 The parameters that should be filled are:
124 (++) Data Transfer direction: should be to card (To CARD)
125 (++) Data Transfer mode
126 (++) DPSM Status (Enable or Disable)
128 (#) Configure the SDMMC resources to send the data to the card according to
129 selected transfer mode.
131 (#) Send the selected Write command.
133 (#) Use the SDMMC flags/interrupts to check the transfer status.
135 *** Command management operations ***
136 =====================================
138 (#) The commands used for Read/Write//Erase operations are managed in
140 Each function allows to send the needed command with the related argument,
141 then check the response.
142 By the same approach, you could implement a command and check the response.
145 ******************************************************************************
148 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
149 * All rights reserved.</center></h2>
151 * This software component is licensed by ST under BSD 3-Clause license,
152 * the "License"; You may not use this file except in compliance with the
153 * License. You may obtain a copy of the License at:
154 * opensource.org/licenses/BSD-3-Clause
156 ******************************************************************************
159 /* Includes ------------------------------------------------------------------*/
160 #include "stm32f7xx_hal.h"
162 /** @addtogroup STM32F7xx_HAL_Driver
166 /** @defgroup SDMMC_LL SDMMC Low Layer
167 * @brief Low layer module for SD
171 #if defined (HAL_SD_MODULE_ENABLED) || defined(HAL_MMC_MODULE_ENABLED)
173 /* Private typedef -----------------------------------------------------------*/
174 /* Private define ------------------------------------------------------------*/
175 /* Private macro -------------------------------------------------------------*/
176 /* Private variables ---------------------------------------------------------*/
177 /* Private function prototypes -----------------------------------------------*/
178 static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef
*SDMMCx
);
179 static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef
*SDMMCx
, uint8_t SD_CMD
, uint32_t Timeout
);
180 static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef
*SDMMCx
);
181 static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef
*SDMMCx
);
182 static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef
*SDMMCx
);
183 static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef
*SDMMCx
, uint8_t SD_CMD
, uint16_t *pRCA
);
185 /* Exported functions --------------------------------------------------------*/
187 /** @defgroup SDMMC_LL_Exported_Functions SDMMC Low Layer Exported Functions
191 /** @defgroup HAL_SDMMC_LL_Group1 Initialization de-initialization functions
192 * @brief Initialization and Configuration functions
195 ===============================================================================
196 ##### Initialization/de-initialization functions #####
197 ===============================================================================
198 [..] This section provides functions allowing to:
205 * @brief Initializes the SDMMC according to the specified
206 * parameters in the SDMMC_InitTypeDef and create the associated handle.
207 * @param SDMMCx Pointer to SDMMC register base
208 * @param Init SDMMC initialization structure
211 HAL_StatusTypeDef
SDMMC_Init(SDMMC_TypeDef
*SDMMCx
, SDMMC_InitTypeDef Init
)
215 /* Check the parameters */
216 assert_param(IS_SDMMC_ALL_INSTANCE(SDMMCx
));
217 assert_param(IS_SDMMC_CLOCK_EDGE(Init
.ClockEdge
));
218 assert_param(IS_SDMMC_CLOCK_BYPASS(Init
.ClockBypass
));
219 assert_param(IS_SDMMC_CLOCK_POWER_SAVE(Init
.ClockPowerSave
));
220 assert_param(IS_SDMMC_BUS_WIDE(Init
.BusWide
));
221 assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(Init
.HardwareFlowControl
));
222 assert_param(IS_SDMMC_CLKDIV(Init
.ClockDiv
));
224 /* Set SDMMC configuration parameters */
225 tmpreg
|= (Init
.ClockEdge
|\
227 Init
.ClockPowerSave
|\
229 Init
.HardwareFlowControl
|\
233 /* Write to SDMMC CLKCR */
234 MODIFY_REG(SDMMCx
->CLKCR
, CLKCR_CLEAR_MASK
, tmpreg
);
244 /** @defgroup HAL_SDMMC_LL_Group2 IO operation functions
245 * @brief Data transfers functions
248 ===============================================================================
249 ##### I/O operation functions #####
250 ===============================================================================
252 This subsection provides a set of functions allowing to manage the SDMMC data
260 * @brief Read data (word) from Rx FIFO in blocking mode (polling)
261 * @param SDMMCx Pointer to SDMMC register base
264 uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef
*SDMMCx
)
266 /* Read data from Rx FIFO */
267 return (SDMMCx
->FIFO
);
271 * @brief Write data (word) to Tx FIFO in blocking mode (polling)
272 * @param SDMMCx Pointer to SDMMC register base
273 * @param pWriteData pointer to data to write
276 HAL_StatusTypeDef
SDMMC_WriteFIFO(SDMMC_TypeDef
*SDMMCx
, uint32_t *pWriteData
)
278 /* Write data to FIFO */
279 SDMMCx
->FIFO
= *pWriteData
;
288 /** @defgroup HAL_SDMMC_LL_Group3 Peripheral Control functions
289 * @brief management functions
292 ===============================================================================
293 ##### Peripheral Control functions #####
294 ===============================================================================
296 This subsection provides a set of functions allowing to control the SDMMC data
304 * @brief Set SDMMC Power state to ON.
305 * @param SDMMCx Pointer to SDMMC register base
308 HAL_StatusTypeDef
SDMMC_PowerState_ON(SDMMC_TypeDef
*SDMMCx
)
310 /* Set power state to ON */
311 SDMMCx
->POWER
= SDMMC_POWER_PWRCTRL
;
317 * @brief Set SDMMC Power state to OFF.
318 * @param SDMMCx Pointer to SDMMC register base
321 HAL_StatusTypeDef
SDMMC_PowerState_OFF(SDMMC_TypeDef
*SDMMCx
)
323 /* Set power state to OFF */
324 SDMMCx
->POWER
= (uint32_t)0x00000000;
330 * @brief Get SDMMC Power state.
331 * @param SDMMCx Pointer to SDMMC register base
332 * @retval Power status of the controller. The returned value can be one of the
338 uint32_t SDMMC_GetPowerState(SDMMC_TypeDef
*SDMMCx
)
340 return (SDMMCx
->POWER
& SDMMC_POWER_PWRCTRL
);
344 * @brief Configure the SDMMC command path according to the specified parameters in
345 * SDMMC_CmdInitTypeDef structure and send the command
346 * @param SDMMCx Pointer to SDMMC register base
347 * @param Command pointer to a SDMMC_CmdInitTypeDef structure that contains
348 * the configuration information for the SDMMC command
351 HAL_StatusTypeDef
SDMMC_SendCommand(SDMMC_TypeDef
*SDMMCx
, SDMMC_CmdInitTypeDef
*Command
)
355 /* Check the parameters */
356 assert_param(IS_SDMMC_CMD_INDEX(Command
->CmdIndex
));
357 assert_param(IS_SDMMC_RESPONSE(Command
->Response
));
358 assert_param(IS_SDMMC_WAIT(Command
->WaitForInterrupt
));
359 assert_param(IS_SDMMC_CPSM(Command
->CPSM
));
361 /* Set the SDMMC Argument value */
362 SDMMCx
->ARG
= Command
->Argument
;
364 /* Set SDMMC command parameters */
365 tmpreg
|= (uint32_t)(Command
->CmdIndex
|\
367 Command
->WaitForInterrupt
|\
370 /* Write to SDMMC CMD register */
371 MODIFY_REG(SDMMCx
->CMD
, CMD_CLEAR_MASK
, tmpreg
);
377 * @brief Return the command index of last command for which response received
378 * @param SDMMCx Pointer to SDMMC register base
379 * @retval Command index of the last command response received
381 uint8_t SDMMC_GetCommandResponse(SDMMC_TypeDef
*SDMMCx
)
383 return (uint8_t)(SDMMCx
->RESPCMD
);
388 * @brief Return the response received from the card for the last command
389 * @param SDMMCx Pointer to SDMMC register base
390 * @param Response Specifies the SDMMC response register.
391 * This parameter can be one of the following values:
392 * @arg SDMMC_RESP1: Response Register 1
393 * @arg SDMMC_RESP2: Response Register 2
394 * @arg SDMMC_RESP3: Response Register 3
395 * @arg SDMMC_RESP4: Response Register 4
396 * @retval The Corresponding response register value
398 uint32_t SDMMC_GetResponse(SDMMC_TypeDef
*SDMMCx
, uint32_t Response
)
400 __IO
uint32_t tmp
= 0;
402 /* Check the parameters */
403 assert_param(IS_SDMMC_RESP(Response
));
405 /* Get the response */
406 tmp
= (uint32_t)&(SDMMCx
->RESP1
) + Response
;
408 return (*(__IO
uint32_t *) tmp
);
412 * @brief Configure the SDMMC data path according to the specified
413 * parameters in the SDMMC_DataInitTypeDef.
414 * @param SDMMCx Pointer to SDMMC register base
415 * @param Data pointer to a SDMMC_DataInitTypeDef structure
416 * that contains the configuration information for the SDMMC data.
419 HAL_StatusTypeDef
SDMMC_ConfigData(SDMMC_TypeDef
*SDMMCx
, SDMMC_DataInitTypeDef
* Data
)
423 /* Check the parameters */
424 assert_param(IS_SDMMC_DATA_LENGTH(Data
->DataLength
));
425 assert_param(IS_SDMMC_BLOCK_SIZE(Data
->DataBlockSize
));
426 assert_param(IS_SDMMC_TRANSFER_DIR(Data
->TransferDir
));
427 assert_param(IS_SDMMC_TRANSFER_MODE(Data
->TransferMode
));
428 assert_param(IS_SDMMC_DPSM(Data
->DPSM
));
430 /* Set the SDMMC Data TimeOut value */
431 SDMMCx
->DTIMER
= Data
->DataTimeOut
;
433 /* Set the SDMMC DataLength value */
434 SDMMCx
->DLEN
= Data
->DataLength
;
436 /* Set the SDMMC data configuration parameters */
437 tmpreg
|= (uint32_t)(Data
->DataBlockSize
|\
439 Data
->TransferMode
|\
442 /* Write to SDMMC DCTRL */
443 MODIFY_REG(SDMMCx
->DCTRL
, DCTRL_CLEAR_MASK
, tmpreg
);
450 * @brief Returns number of remaining data bytes to be transferred.
451 * @param SDMMCx Pointer to SDMMC register base
452 * @retval Number of remaining data bytes to be transferred
454 uint32_t SDMMC_GetDataCounter(SDMMC_TypeDef
*SDMMCx
)
456 return (SDMMCx
->DCOUNT
);
460 * @brief Get the FIFO data
461 * @param SDMMCx Pointer to SDMMC register base
462 * @retval Data received
464 uint32_t SDMMC_GetFIFOCount(SDMMC_TypeDef
*SDMMCx
)
466 return (SDMMCx
->FIFO
);
470 * @brief Sets one of the two options of inserting read wait interval.
471 * @param SDMMCx Pointer to SDMMC register base
472 * @param SDMMC_ReadWaitMode SDMMC Read Wait operation mode.
473 * This parameter can be:
474 * @arg SDMMC_READ_WAIT_MODE_CLK: Read Wait control by stopping SDMMCCLK
475 * @arg SDMMC_READ_WAIT_MODE_DATA2: Read Wait control using SDMMC_DATA2
478 HAL_StatusTypeDef
SDMMC_SetSDMMCReadWaitMode(SDMMC_TypeDef
*SDMMCx
, uint32_t SDMMC_ReadWaitMode
)
480 /* Check the parameters */
481 assert_param(IS_SDMMC_READWAIT_MODE(SDMMC_ReadWaitMode
));
483 /* Set SDMMC read wait mode */
484 MODIFY_REG(SDMMCx
->DCTRL
, SDMMC_DCTRL_RWMOD
, SDMMC_ReadWaitMode
);
494 /** @defgroup HAL_SDMMC_LL_Group4 Command management functions
495 * @brief Data transfers functions
498 ===============================================================================
499 ##### Commands management functions #####
500 ===============================================================================
502 This subsection provides a set of functions allowing to manage the needed commands.
509 * @brief Send the Data Block Lenght command and check the response
510 * @param SDMMCx Pointer to SDMMC register base
513 uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef
*SDMMCx
, uint32_t BlockSize
)
515 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
516 uint32_t errorstate
= SDMMC_ERROR_NONE
;
518 /* Set Block Size for Card */
519 sdmmc_cmdinit
.Argument
= (uint32_t)BlockSize
;
520 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SET_BLOCKLEN
;
521 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
522 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
523 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
524 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
526 /* Check for error conditions */
527 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SET_BLOCKLEN
, SDMMC_CMDTIMEOUT
);
533 * @brief Send the Read Single Block command and check the response
534 * @param SDMMCx Pointer to SDMMC register base
537 uint32_t SDMMC_CmdReadSingleBlock(SDMMC_TypeDef
*SDMMCx
, uint32_t ReadAdd
)
539 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
540 uint32_t errorstate
= SDMMC_ERROR_NONE
;
542 /* Set Block Size for Card */
543 sdmmc_cmdinit
.Argument
= (uint32_t)ReadAdd
;
544 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_READ_SINGLE_BLOCK
;
545 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
546 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
547 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
548 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
550 /* Check for error conditions */
551 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_READ_SINGLE_BLOCK
, SDMMC_CMDTIMEOUT
);
557 * @brief Send the Read Multi Block command and check the response
558 * @param SDMMCx Pointer to SDMMC register base
561 uint32_t SDMMC_CmdReadMultiBlock(SDMMC_TypeDef
*SDMMCx
, uint32_t ReadAdd
)
563 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
564 uint32_t errorstate
= SDMMC_ERROR_NONE
;
566 /* Set Block Size for Card */
567 sdmmc_cmdinit
.Argument
= (uint32_t)ReadAdd
;
568 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_READ_MULT_BLOCK
;
569 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
570 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
571 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
572 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
574 /* Check for error conditions */
575 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_READ_MULT_BLOCK
, SDMMC_CMDTIMEOUT
);
581 * @brief Send the Write Single Block command and check the response
582 * @param SDMMCx Pointer to SDMMC register base
585 uint32_t SDMMC_CmdWriteSingleBlock(SDMMC_TypeDef
*SDMMCx
, uint32_t WriteAdd
)
587 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
588 uint32_t errorstate
= SDMMC_ERROR_NONE
;
590 /* Set Block Size for Card */
591 sdmmc_cmdinit
.Argument
= (uint32_t)WriteAdd
;
592 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_WRITE_SINGLE_BLOCK
;
593 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
594 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
595 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
596 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
598 /* Check for error conditions */
599 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_WRITE_SINGLE_BLOCK
, SDMMC_CMDTIMEOUT
);
605 * @brief Send the Write Multi Block command and check the response
606 * @param SDMMCx Pointer to SDMMC register base
609 uint32_t SDMMC_CmdWriteMultiBlock(SDMMC_TypeDef
*SDMMCx
, uint32_t WriteAdd
)
611 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
612 uint32_t errorstate
= SDMMC_ERROR_NONE
;
614 /* Set Block Size for Card */
615 sdmmc_cmdinit
.Argument
= (uint32_t)WriteAdd
;
616 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_WRITE_MULT_BLOCK
;
617 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
618 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
619 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
620 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
622 /* Check for error conditions */
623 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_WRITE_MULT_BLOCK
, SDMMC_CMDTIMEOUT
);
629 * @brief Send the Start Address Erase command for SD and check the response
630 * @param SDMMCx Pointer to SDMMC register base
633 uint32_t SDMMC_CmdSDEraseStartAdd(SDMMC_TypeDef
*SDMMCx
, uint32_t StartAdd
)
635 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
636 uint32_t errorstate
= SDMMC_ERROR_NONE
;
638 /* Set Block Size for Card */
639 sdmmc_cmdinit
.Argument
= (uint32_t)StartAdd
;
640 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SD_ERASE_GRP_START
;
641 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
642 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
643 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
644 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
646 /* Check for error conditions */
647 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SD_ERASE_GRP_START
, SDMMC_CMDTIMEOUT
);
653 * @brief Send the End Address Erase command for SD and check the response
654 * @param SDMMCx Pointer to SDMMC register base
657 uint32_t SDMMC_CmdSDEraseEndAdd(SDMMC_TypeDef
*SDMMCx
, uint32_t EndAdd
)
659 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
660 uint32_t errorstate
= SDMMC_ERROR_NONE
;
662 /* Set Block Size for Card */
663 sdmmc_cmdinit
.Argument
= (uint32_t)EndAdd
;
664 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SD_ERASE_GRP_END
;
665 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
666 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
667 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
668 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
670 /* Check for error conditions */
671 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SD_ERASE_GRP_END
, SDMMC_CMDTIMEOUT
);
677 * @brief Send the Start Address Erase command and check the response
678 * @param SDMMCx Pointer to SDMMC register base
681 uint32_t SDMMC_CmdEraseStartAdd(SDMMC_TypeDef
*SDMMCx
, uint32_t StartAdd
)
683 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
684 uint32_t errorstate
= SDMMC_ERROR_NONE
;
686 /* Set Block Size for Card */
687 sdmmc_cmdinit
.Argument
= (uint32_t)StartAdd
;
688 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_ERASE_GRP_START
;
689 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
690 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
691 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
692 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
694 /* Check for error conditions */
695 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_ERASE_GRP_START
, SDMMC_CMDTIMEOUT
);
701 * @brief Send the End Address Erase command and check the response
702 * @param SDMMCx Pointer to SDMMC register base
705 uint32_t SDMMC_CmdEraseEndAdd(SDMMC_TypeDef
*SDMMCx
, uint32_t EndAdd
)
707 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
708 uint32_t errorstate
= SDMMC_ERROR_NONE
;
710 /* Set Block Size for Card */
711 sdmmc_cmdinit
.Argument
= (uint32_t)EndAdd
;
712 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_ERASE_GRP_END
;
713 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
714 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
715 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
716 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
718 /* Check for error conditions */
719 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_ERASE_GRP_END
, SDMMC_CMDTIMEOUT
);
725 * @brief Send the Erase command and check the response
726 * @param SDMMCx Pointer to SDMMC register base
729 uint32_t SDMMC_CmdErase(SDMMC_TypeDef
*SDMMCx
)
731 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
732 uint32_t errorstate
= SDMMC_ERROR_NONE
;
734 /* Set Block Size for Card */
735 sdmmc_cmdinit
.Argument
= 0;
736 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_ERASE
;
737 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
738 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
739 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
740 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
742 /* Check for error conditions */
743 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_ERASE
, SDMMC_MAXERASETIMEOUT
);
749 * @brief Send the Stop Transfer command and check the response.
750 * @param SDMMCx Pointer to SDMMC register base
753 uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef
*SDMMCx
)
755 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
756 uint32_t errorstate
= SDMMC_ERROR_NONE
;
758 /* Send CMD12 STOP_TRANSMISSION */
759 sdmmc_cmdinit
.Argument
= 0;
760 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_STOP_TRANSMISSION
;
761 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
762 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
763 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
764 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
766 /* Check for error conditions */
767 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_STOP_TRANSMISSION
, 100000000/*SDMMC_CMDTIMEOUT*/);
773 * @brief Send the Select Deselect command and check the response.
774 * @param SDMMCx Pointer to SDMMC register base
775 * @param addr Address of the card to be selected
778 uint32_t SDMMC_CmdSelDesel(SDMMC_TypeDef
*SDMMCx
, uint64_t Addr
)
780 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
781 uint32_t errorstate
= SDMMC_ERROR_NONE
;
783 /* Send CMD7 SDMMC_SEL_DESEL_CARD */
784 sdmmc_cmdinit
.Argument
= (uint32_t)Addr
;
785 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SEL_DESEL_CARD
;
786 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
787 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
788 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
789 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
791 /* Check for error conditions */
792 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SEL_DESEL_CARD
, SDMMC_CMDTIMEOUT
);
798 * @brief Send the Go Idle State command and check the response.
799 * @param SDMMCx Pointer to SDMMC register base
802 uint32_t SDMMC_CmdGoIdleState(SDMMC_TypeDef
*SDMMCx
)
804 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
805 uint32_t errorstate
= SDMMC_ERROR_NONE
;
807 sdmmc_cmdinit
.Argument
= 0;
808 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_GO_IDLE_STATE
;
809 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_NO
;
810 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
811 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
812 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
814 /* Check for error conditions */
815 errorstate
= SDMMC_GetCmdError(SDMMCx
);
821 * @brief Send the Operating Condition command and check the response.
822 * @param SDMMCx Pointer to SDMMC register base
825 uint32_t SDMMC_CmdOperCond(SDMMC_TypeDef
*SDMMCx
)
827 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
828 uint32_t errorstate
= SDMMC_ERROR_NONE
;
830 /* Send CMD8 to verify SD card interface operating condition */
831 /* Argument: - [31:12]: Reserved (shall be set to '0')
832 - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
833 - [7:0]: Check Pattern (recommended 0xAA) */
834 /* CMD Response: R7 */
835 sdmmc_cmdinit
.Argument
= SDMMC_CHECK_PATTERN
;
836 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_HS_SEND_EXT_CSD
;
837 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
838 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
839 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
840 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
842 /* Check for error conditions */
843 errorstate
= SDMMC_GetCmdResp7(SDMMCx
);
849 * @brief Send the Application command to verify that that the next command
850 * is an application specific com-mand rather than a standard command
851 * and check the response.
852 * @param SDMMCx Pointer to SDMMC register base
855 uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
857 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
858 uint32_t errorstate
= SDMMC_ERROR_NONE
;
860 sdmmc_cmdinit
.Argument
= (uint32_t)Argument
;
861 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_APP_CMD
;
862 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
863 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
864 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
865 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
867 /* Check for error conditions */
868 /* If there is a HAL_ERROR, it is a MMC card, else
869 it is a SD card: SD card 2.0 (voltage range mismatch)
871 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_APP_CMD
, SDMMC_CMDTIMEOUT
);
877 * @brief Send the command asking the accessed card to send its operating
878 * condition register (OCR)
879 * @param SDMMCx Pointer to SDMMC register base
882 uint32_t SDMMC_CmdAppOperCommand(SDMMC_TypeDef
*SDMMCx
, uint32_t SdType
)
884 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
885 uint32_t errorstate
= SDMMC_ERROR_NONE
;
887 sdmmc_cmdinit
.Argument
= SDMMC_VOLTAGE_WINDOW_SD
| SdType
;
888 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SD_APP_OP_COND
;
889 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
890 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
891 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
892 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
894 /* Check for error conditions */
895 errorstate
= SDMMC_GetCmdResp3(SDMMCx
);
901 * @brief Send the Bus Width command and check the response.
902 * @param SDMMCx Pointer to SDMMC register base
905 uint32_t SDMMC_CmdBusWidth(SDMMC_TypeDef
*SDMMCx
, uint32_t BusWidth
)
907 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
908 uint32_t errorstate
= SDMMC_ERROR_NONE
;
910 sdmmc_cmdinit
.Argument
= (uint32_t)BusWidth
;
911 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_APP_SD_SET_BUSWIDTH
;
912 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
913 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
914 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
915 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
917 /* Check for error conditions */
918 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_APP_SD_SET_BUSWIDTH
, SDMMC_CMDTIMEOUT
);
924 * @brief Send the Send SCR command and check the response.
925 * @param SDMMCx Pointer to SDMMC register base
928 uint32_t SDMMC_CmdSendSCR(SDMMC_TypeDef
*SDMMCx
)
930 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
931 uint32_t errorstate
= SDMMC_ERROR_NONE
;
933 /* Send CMD51 SD_APP_SEND_SCR */
934 sdmmc_cmdinit
.Argument
= 0;
935 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SD_APP_SEND_SCR
;
936 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
937 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
938 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
939 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
941 /* Check for error conditions */
942 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SD_APP_SEND_SCR
, SDMMC_CMDTIMEOUT
);
948 * @brief Send the Send CID command and check the response.
949 * @param SDMMCx Pointer to SDMMC register base
952 uint32_t SDMMC_CmdSendCID(SDMMC_TypeDef
*SDMMCx
)
954 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
955 uint32_t errorstate
= SDMMC_ERROR_NONE
;
957 /* Send CMD2 ALL_SEND_CID */
958 sdmmc_cmdinit
.Argument
= 0;
959 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_ALL_SEND_CID
;
960 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_LONG
;
961 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
962 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
963 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
965 /* Check for error conditions */
966 errorstate
= SDMMC_GetCmdResp2(SDMMCx
);
972 * @brief Send the Send CSD command and check the response.
973 * @param SDMMCx Pointer to SDMMC register base
976 uint32_t SDMMC_CmdSendCSD(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
978 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
979 uint32_t errorstate
= SDMMC_ERROR_NONE
;
981 /* Send CMD9 SEND_CSD */
982 sdmmc_cmdinit
.Argument
= (uint32_t)Argument
;
983 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SEND_CSD
;
984 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_LONG
;
985 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
986 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
987 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
989 /* Check for error conditions */
990 errorstate
= SDMMC_GetCmdResp2(SDMMCx
);
996 * @brief Send the Send CSD command and check the response.
997 * @param SDMMCx Pointer to SDMMC register base
1000 uint32_t SDMMC_CmdSetRelAdd(SDMMC_TypeDef
*SDMMCx
, uint16_t *pRCA
)
1002 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1003 uint32_t errorstate
= SDMMC_ERROR_NONE
;
1005 /* Send CMD3 SD_CMD_SET_REL_ADDR */
1006 sdmmc_cmdinit
.Argument
= 0;
1007 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SET_REL_ADDR
;
1008 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1009 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1010 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1011 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1013 /* Check for error conditions */
1014 errorstate
= SDMMC_GetCmdResp6(SDMMCx
, SDMMC_CMD_SET_REL_ADDR
, pRCA
);
1020 * @brief Send the Status command and check the response.
1021 * @param SDMMCx Pointer to SDMMC register base
1022 * @retval HAL status
1024 uint32_t SDMMC_CmdSendStatus(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
1026 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1027 uint32_t errorstate
= SDMMC_ERROR_NONE
;
1029 sdmmc_cmdinit
.Argument
= (uint32_t)Argument
;
1030 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SEND_STATUS
;
1031 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1032 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1033 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1034 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1036 /* Check for error conditions */
1037 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SEND_STATUS
, SDMMC_CMDTIMEOUT
);
1043 * @brief Send the Status register command and check the response.
1044 * @param SDMMCx Pointer to SDMMC register base
1045 * @retval HAL status
1047 uint32_t SDMMC_CmdStatusRegister(SDMMC_TypeDef
*SDMMCx
)
1049 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1050 uint32_t errorstate
= SDMMC_ERROR_NONE
;
1052 sdmmc_cmdinit
.Argument
= 0;
1053 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SD_APP_STATUS
;
1054 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1055 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1056 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1057 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1059 /* Check for error conditions */
1060 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SD_APP_STATUS
, SDMMC_CMDTIMEOUT
);
1066 * @brief Sends host capacity support information and activates the card's
1067 * initialization process. Send SDMMC_CMD_SEND_OP_COND command
1068 * @param SDIOx Pointer to SDIO register base
1069 * @parame Argument Argument used for the command
1070 * @retval HAL status
1072 uint32_t SDMMC_CmdOpCondition(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
1074 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1075 uint32_t errorstate
= SDMMC_ERROR_NONE
;
1077 sdmmc_cmdinit
.Argument
= Argument
;
1078 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SEND_OP_COND
;
1079 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1080 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1081 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1082 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1084 /* Check for error conditions */
1085 errorstate
= SDMMC_GetCmdResp3(SDMMCx
);
1091 * @brief Checks switchable function and switch card function. SDMMC_CMD_HS_SWITCH comand
1092 * @param SDIOx Pointer to SDIO register base
1093 * @parame Argument Argument used for the command
1094 * @retval HAL status
1096 uint32_t SDMMC_CmdSwitch(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
1098 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1099 uint32_t errorstate
= SDMMC_ERROR_NONE
;
1101 sdmmc_cmdinit
.Argument
= Argument
;
1102 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_HS_SWITCH
;
1103 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1104 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1105 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1106 SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1108 /* Check for error conditions */
1109 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_HS_SWITCH
, SDMMC_CMDTIMEOUT
);
1118 /* Private function ----------------------------------------------------------*/
1119 /** @addtogroup SD_Private_Functions
1124 * @brief Checks for error conditions for CMD0.
1125 * @param hsd SD handle
1126 * @retval SD Card error state
1128 static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef
*SDMMCx
)
1130 /* 8 is the number of required instructions cycles for the below loop statement.
1131 The SDMMC_CMDTIMEOUT is expressed in ms */
1132 register uint32_t count
= SDMMC_CMDTIMEOUT
* (SystemCoreClock
/ 8 /1000);
1138 return SDMMC_ERROR_TIMEOUT
;
1141 }while(!__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CMDSENT
));
1143 /* Clear all the static flags */
1144 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_STATIC_FLAGS
);
1146 return SDMMC_ERROR_NONE
;
1150 * @brief Checks for error conditions for R1 response.
1151 * @param hsd SD handle
1152 * @param SD_CMD The sent command index
1153 * @retval SD Card error state
1155 static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef
*SDMMCx
, uint8_t SD_CMD
, uint32_t Timeout
)
1157 uint32_t response_r1
;
1159 /* 8 is the number of required instructions cycles for the below loop statement.
1160 The Timeout is expressed in ms */
1161 register uint32_t count
= Timeout
* (SystemCoreClock
/ 8 /1000);
1167 return SDMMC_ERROR_TIMEOUT
;
1170 }while(!__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
));
1172 if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
))
1174 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
);
1176 return SDMMC_ERROR_CMD_RSP_TIMEOUT
;
1178 else if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
))
1180 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
);
1182 return SDMMC_ERROR_CMD_CRC_FAIL
;
1185 /* Check response received is of desired command */
1186 if(SDMMC_GetCommandResponse(SDMMCx
) != SD_CMD
)
1188 return SDMMC_ERROR_CMD_CRC_FAIL
;
1191 /* Clear all the static flags */
1192 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_STATIC_FLAGS
);
1194 /* We have received response, retrieve it for analysis */
1195 response_r1
= SDMMC_GetResponse(SDMMCx
, SDMMC_RESP1
);
1197 if((response_r1
& SDMMC_OCR_ERRORBITS
) == SDMMC_ALLZERO
)
1199 return SDMMC_ERROR_NONE
;
1201 else if((response_r1
& SDMMC_OCR_ADDR_OUT_OF_RANGE
) == SDMMC_OCR_ADDR_OUT_OF_RANGE
)
1203 return SDMMC_ERROR_ADDR_OUT_OF_RANGE
;
1205 else if((response_r1
& SDMMC_OCR_ADDR_MISALIGNED
) == SDMMC_OCR_ADDR_MISALIGNED
)
1207 return SDMMC_ERROR_ADDR_MISALIGNED
;
1209 else if((response_r1
& SDMMC_OCR_BLOCK_LEN_ERR
) == SDMMC_OCR_BLOCK_LEN_ERR
)
1211 return SDMMC_ERROR_BLOCK_LEN_ERR
;
1213 else if((response_r1
& SDMMC_OCR_ERASE_SEQ_ERR
) == SDMMC_OCR_ERASE_SEQ_ERR
)
1215 return SDMMC_ERROR_ERASE_SEQ_ERR
;
1217 else if((response_r1
& SDMMC_OCR_BAD_ERASE_PARAM
) == SDMMC_OCR_BAD_ERASE_PARAM
)
1219 return SDMMC_ERROR_BAD_ERASE_PARAM
;
1221 else if((response_r1
& SDMMC_OCR_WRITE_PROT_VIOLATION
) == SDMMC_OCR_WRITE_PROT_VIOLATION
)
1223 return SDMMC_ERROR_WRITE_PROT_VIOLATION
;
1225 else if((response_r1
& SDMMC_OCR_LOCK_UNLOCK_FAILED
) == SDMMC_OCR_LOCK_UNLOCK_FAILED
)
1227 return SDMMC_ERROR_LOCK_UNLOCK_FAILED
;
1229 else if((response_r1
& SDMMC_OCR_COM_CRC_FAILED
) == SDMMC_OCR_COM_CRC_FAILED
)
1231 return SDMMC_ERROR_COM_CRC_FAILED
;
1233 else if((response_r1
& SDMMC_OCR_ILLEGAL_CMD
) == SDMMC_OCR_ILLEGAL_CMD
)
1235 return SDMMC_ERROR_ILLEGAL_CMD
;
1237 else if((response_r1
& SDMMC_OCR_CARD_ECC_FAILED
) == SDMMC_OCR_CARD_ECC_FAILED
)
1239 return SDMMC_ERROR_CARD_ECC_FAILED
;
1241 else if((response_r1
& SDMMC_OCR_CC_ERROR
) == SDMMC_OCR_CC_ERROR
)
1243 return SDMMC_ERROR_CC_ERR
;
1245 else if((response_r1
& SDMMC_OCR_STREAM_READ_UNDERRUN
) == SDMMC_OCR_STREAM_READ_UNDERRUN
)
1247 return SDMMC_ERROR_STREAM_READ_UNDERRUN
;
1249 else if((response_r1
& SDMMC_OCR_STREAM_WRITE_OVERRUN
) == SDMMC_OCR_STREAM_WRITE_OVERRUN
)
1251 return SDMMC_ERROR_STREAM_WRITE_OVERRUN
;
1253 else if((response_r1
& SDMMC_OCR_CID_CSD_OVERWRITE
) == SDMMC_OCR_CID_CSD_OVERWRITE
)
1255 return SDMMC_ERROR_CID_CSD_OVERWRITE
;
1257 else if((response_r1
& SDMMC_OCR_WP_ERASE_SKIP
) == SDMMC_OCR_WP_ERASE_SKIP
)
1259 return SDMMC_ERROR_WP_ERASE_SKIP
;
1261 else if((response_r1
& SDMMC_OCR_CARD_ECC_DISABLED
) == SDMMC_OCR_CARD_ECC_DISABLED
)
1263 return SDMMC_ERROR_CARD_ECC_DISABLED
;
1265 else if((response_r1
& SDMMC_OCR_ERASE_RESET
) == SDMMC_OCR_ERASE_RESET
)
1267 return SDMMC_ERROR_ERASE_RESET
;
1269 else if((response_r1
& SDMMC_OCR_AKE_SEQ_ERROR
) == SDMMC_OCR_AKE_SEQ_ERROR
)
1271 return SDMMC_ERROR_AKE_SEQ_ERR
;
1275 return SDMMC_ERROR_GENERAL_UNKNOWN_ERR
;
1280 * @brief Checks for error conditions for R2 (CID or CSD) response.
1281 * @param hsd SD handle
1282 * @retval SD Card error state
1284 static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef
*SDMMCx
)
1286 /* 8 is the number of required instructions cycles for the below loop statement.
1287 The SDMMC_CMDTIMEOUT is expressed in ms */
1288 register uint32_t count
= SDMMC_CMDTIMEOUT
* (SystemCoreClock
/ 8 /1000);
1294 return SDMMC_ERROR_TIMEOUT
;
1297 }while(!__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
));
1299 if (__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
))
1301 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
);
1303 return SDMMC_ERROR_CMD_RSP_TIMEOUT
;
1305 else if (__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
))
1307 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
);
1309 return SDMMC_ERROR_CMD_CRC_FAIL
;
1313 /* No error flag set */
1314 /* Clear all the static flags */
1315 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_STATIC_FLAGS
);
1318 return SDMMC_ERROR_NONE
;
1322 * @brief Checks for error conditions for R3 (OCR) response.
1323 * @param hsd SD handle
1324 * @retval SD Card error state
1326 static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef
*SDMMCx
)
1328 /* 8 is the number of required instructions cycles for the below loop statement.
1329 The SDMMC_CMDTIMEOUT is expressed in ms */
1330 register uint32_t count
= SDMMC_CMDTIMEOUT
* (SystemCoreClock
/ 8 /1000);
1336 return SDMMC_ERROR_TIMEOUT
;
1339 }while(!__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
));
1341 if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
))
1343 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
);
1345 return SDMMC_ERROR_CMD_RSP_TIMEOUT
;
1350 /* Clear all the static flags */
1351 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_STATIC_FLAGS
);
1354 return SDMMC_ERROR_NONE
;
1358 * @brief Checks for error conditions for R6 (RCA) response.
1359 * @param hsd SD handle
1360 * @param SD_CMD The sent command index
1361 * @param pRCA Pointer to the variable that will contain the SD card relative
1363 * @retval SD Card error state
1365 static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef
*SDMMCx
, uint8_t SD_CMD
, uint16_t *pRCA
)
1367 uint32_t response_r1
;
1369 /* 8 is the number of required instructions cycles for the below loop statement.
1370 The SDMMC_CMDTIMEOUT is expressed in ms */
1371 register uint32_t count
= SDMMC_CMDTIMEOUT
* (SystemCoreClock
/ 8 /1000);
1377 return SDMMC_ERROR_TIMEOUT
;
1380 }while(!__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
));
1382 if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
))
1384 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
);
1386 return SDMMC_ERROR_CMD_RSP_TIMEOUT
;
1388 else if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
))
1390 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
);
1392 return SDMMC_ERROR_CMD_CRC_FAIL
;
1395 /* Check response received is of desired command */
1396 if(SDMMC_GetCommandResponse(SDMMCx
) != SD_CMD
)
1398 return SDMMC_ERROR_CMD_CRC_FAIL
;
1401 /* Clear all the static flags */
1402 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_STATIC_FLAGS
);
1404 /* We have received response, retrieve it. */
1405 response_r1
= SDMMC_GetResponse(SDMMCx
, SDMMC_RESP1
);
1407 if((response_r1
& (SDMMC_R6_GENERAL_UNKNOWN_ERROR
| SDMMC_R6_ILLEGAL_CMD
| SDMMC_R6_COM_CRC_FAILED
)) == SDMMC_ALLZERO
)
1409 *pRCA
= (uint16_t) (response_r1
>> 16);
1411 return SDMMC_ERROR_NONE
;
1413 else if((response_r1
& SDMMC_R6_ILLEGAL_CMD
) == SDMMC_R6_ILLEGAL_CMD
)
1415 return SDMMC_ERROR_ILLEGAL_CMD
;
1417 else if((response_r1
& SDMMC_R6_COM_CRC_FAILED
) == SDMMC_R6_COM_CRC_FAILED
)
1419 return SDMMC_ERROR_COM_CRC_FAILED
;
1423 return SDMMC_ERROR_GENERAL_UNKNOWN_ERR
;
1428 * @brief Checks for error conditions for R7 response.
1429 * @param hsd SD handle
1430 * @retval SD Card error state
1432 static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef
*SDMMCx
)
1434 /* 8 is the number of required instructions cycles for the below loop statement.
1435 The SDMMC_CMDTIMEOUT is expressed in ms */
1436 register uint32_t count
= SDMMC_CMDTIMEOUT
* (SystemCoreClock
/ 8 /1000);
1442 return SDMMC_ERROR_TIMEOUT
;
1445 }while(!__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
));
1447 if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
))
1449 /* Card is SD V2.0 compliant */
1450 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CMDREND
);
1452 return SDMMC_ERROR_CMD_RSP_TIMEOUT
;
1455 if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CMDREND
))
1457 /* Card is SD V2.0 compliant */
1458 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CMDREND
);
1461 return SDMMC_ERROR_NONE
;
1473 #endif /* (HAL_SD_MODULE_ENABLED) */
1482 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/