2 ******************************************************************************
3 * @file stm32h7xx_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 AHB
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 MultiMediaCard System Specification Version 4.51. Card support
24 for three different databus modes: 1-bit (default), 4-bit and 8-bit.
25 (+) Full compatibility with previous versions of MultiMediaCards (backward compatibility).
26 (+) Full compliance with SD memory card specifications version 4.1.
27 (SDR104 SDMMC_CK speed limited to maximum allowed IO speed, SPI mode and
28 UHS-II mode not supported).
29 (+) Full compliance with SDIO card specification version 4.0. Card support
30 for two different databus modes: 1-bit (default) and 4-bit.
31 (SDR104 SDMMC_CK speed limited to maximum allowed IO speed, SPI mode and
32 UHS-II mode not supported).
33 (+) Data transfer up to 208 Mbyte/s for the 8 bit mode. (depending maximum allowed IO speed).
34 (+) Data and command output enable signals to control external bidirectional drivers
36 ##### How to use this driver #####
37 ==============================================================================
39 This driver is a considered as a driver of service for external devices drivers
40 that interfaces with the SDMMC peripheral.
41 According to the device used (SD card/ MMC card / SDMMC card ...), a set of APIs
42 is used in the device's driver to perform SDMMC operations and functionalities.
44 This driver is almost transparent for the final user, it is only used to implement other
45 functionalities of the external device.
48 (+) The SDMMC clock is coming from output of PLL1_Q or PLL2_R.
49 Before start working with SDMMC peripheral make sure that the PLL is well configured.
50 The SDMMC peripheral uses two clock signals:
51 (++) PLL1_Q bus clock (default after reset)
54 (+) Enable/Disable peripheral clock using RCC peripheral macros related to SDMMC
57 (+) Enable the Power ON State using the SDMMC_PowerState_ON(SDMMCx)
58 function and disable it using the function SDMMC_PowerState_OFF(SDMMCx).
60 (+) Enable/Disable the peripheral interrupts using the macros __SDMMC_ENABLE_IT(hSDMMC, IT)
61 and __SDMMC_DISABLE_IT(hSDMMC, IT) if you need to use interrupt mode.
63 (+) When using the DMA mode
64 (++) Configure the IDMA mode (Single buffer or double)
65 (++) Configure the buffer address
66 (++) Configure Data Path State Machine
68 (+) To control the CPSM (Command Path State Machine) and send
69 commands to the card use the SDMMC_SendCommand(SDMMCx),
70 SDMMC_GetCommandResponse() and SDMMC_GetResponse() functions. First, user has
71 to fill the command structure (pointer to SDMMC_CmdInitTypeDef) according
72 to the selected command to be sent.
73 The parameters that should be filled are:
76 (++) Command Response type
78 (++) CPSM Status (Enable or Disable).
80 -@@- To check if the command is well received, read the SDMMC_CMDRESP
81 register using the SDMMC_GetCommandResponse().
82 The SDMMC responses registers (SDMMC_RESP1 to SDMMC_RESP2), use the
83 SDMMC_GetResponse() function.
85 (+) To control the DPSM (Data Path State Machine) and send/receive
86 data to/from the card use the SDMMC_DataConfig(), SDMMC_GetDataCounter(),
87 SDMMC_ReadFIFO(), SDMMC_WriteFIFO() and SDMMC_GetFIFOCount() functions.
89 *** Read Operations ***
90 =======================
92 (#) First, user has to fill the data structure (pointer to
93 SDMMC_DataInitTypeDef) according to the selected data type to be received.
94 The parameters that should be filled are:
98 (++) Data Transfer direction: should be from card (To SDMMC)
99 (++) Data Transfer mode
100 (++) DPSM Status (Enable or Disable)
102 (#) Configure the SDMMC resources to receive the data from the card
103 according to selected transfer mode (Refer to Step 8, 9 and 10).
105 (#) Send the selected Read command (refer to step 11).
107 (#) Use the SDMMC flags/interrupts to check the transfer status.
109 *** Write Operations ***
110 ========================
112 (#) First, user has to fill the data structure (pointer to
113 SDMMC_DataInitTypeDef) according to the selected data type to be received.
114 The parameters that should be filled are:
118 (++) Data Transfer direction: should be to card (To CARD)
119 (++) Data Transfer mode
120 (++) DPSM Status (Enable or Disable)
122 (#) Configure the SDMMC resources to send the data to the card according to
123 selected transfer mode.
125 (#) Send the selected Write command.
127 (#) Use the SDMMC flags/interrupts to check the transfer status.
129 *** Command management operations ***
130 =====================================
132 (#) The commands used for Read/Write/Erase operations are managed in
134 Each function allows to send the needed command with the related argument,
135 then check the response.
136 By the same approach, you could implement a command and check the response.
139 ******************************************************************************
142 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
143 * All rights reserved.</center></h2>
145 * This software component is licensed by ST under BSD 3-Clause license,
146 * the "License"; You may not use this file except in compliance with the
147 * License. You may obtain a copy of the License at:
148 * opensource.org/licenses/BSD-3-Clause
150 ******************************************************************************
153 /* Includes ------------------------------------------------------------------*/
154 #include "stm32h7xx_hal.h"
156 /** @addtogroup STM32H7xx_HAL_Driver
160 /** @defgroup SDMMC_LL SDMMC Low Layer
161 * @brief Low layer module for SD
165 #if defined (HAL_SD_MODULE_ENABLED) || defined (HAL_MMC_MODULE_ENABLED)
167 /* Private typedef -----------------------------------------------------------*/
168 /* Private define ------------------------------------------------------------*/
169 /* Private macro -------------------------------------------------------------*/
170 /* Private variables ---------------------------------------------------------*/
171 /* Private function prototypes -----------------------------------------------*/
172 static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef
*SDMMCx
);
173 static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef
*SDMMCx
, uint8_t SD_CMD
, uint32_t Timeout
);
174 static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef
*SDMMCx
);
175 static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef
*SDMMCx
);
176 static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef
*SDMMCx
);
177 static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef
*SDMMCx
, uint8_t SD_CMD
, uint16_t *pRCA
);
179 /* Exported functions --------------------------------------------------------*/
181 /** @defgroup SDMMC_LL_Exported_Functions SDMMC Low Layer Exported Functions
185 /** @defgroup HAL_SDMMC_LL_Group1 Initialization de-initialization functions
186 * @brief Initialization and Configuration functions
189 ===============================================================================
190 ##### Initialization/de-initialization functions #####
191 ===============================================================================
192 [..] This section provides functions allowing to:
199 * @brief Initializes the SDMMC according to the specified
200 * parameters in the SDMMC_InitTypeDef and create the associated handle.
201 * @param SDMMCx: Pointer to SDMMC register base
202 * @param Init: SDMMC initialization structure
205 HAL_StatusTypeDef
SDMMC_Init(SDMMC_TypeDef
*SDMMCx
, SDMMC_InitTypeDef Init
)
209 /* Check the parameters */
210 assert_param(IS_SDMMC_ALL_INSTANCE(SDMMCx
));
211 assert_param(IS_SDMMC_CLOCK_EDGE(Init
.ClockEdge
));
212 assert_param(IS_SDMMC_CLOCK_POWER_SAVE(Init
.ClockPowerSave
));
213 assert_param(IS_SDMMC_BUS_WIDE(Init
.BusWide
));
214 assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(Init
.HardwareFlowControl
));
215 assert_param(IS_SDMMC_CLKDIV(Init
.ClockDiv
));
217 /* Set SDMMC configuration parameters */
218 tmpreg
|= (Init
.ClockEdge
|\
219 Init
.ClockPowerSave
|\
221 Init
.HardwareFlowControl
|\
225 /* Write to SDMMC CLKCR */
226 MODIFY_REG(SDMMCx
->CLKCR
, CLKCR_CLEAR_MASK
, tmpreg
);
236 /** @defgroup HAL_SDMMC_LL_Group2 IO operation functions
237 * @brief Data transfers functions
240 ===============================================================================
241 ##### I/O operation functions #####
242 ===============================================================================
244 This subsection provides a set of functions allowing to manage the SDMMC data
252 * @brief Read data (word) from Rx FIFO in blocking mode (polling)
253 * @param SDMMCx: Pointer to SDMMC register base
256 uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef
*SDMMCx
)
258 /* Read data from Rx FIFO */
259 return (SDMMCx
->FIFO
);
263 * @brief Write data (word) to Tx FIFO in blocking mode (polling)
264 * @param SDMMCx: Pointer to SDMMC register base
265 * @param pWriteData: pointer to data to write
268 HAL_StatusTypeDef
SDMMC_WriteFIFO(SDMMC_TypeDef
*SDMMCx
, uint32_t *pWriteData
)
270 /* Write data to FIFO */
271 SDMMCx
->FIFO
= *pWriteData
;
280 /** @defgroup HAL_SDMMC_LL_Group3 Peripheral Control functions
281 * @brief management functions
284 ===============================================================================
285 ##### Peripheral Control functions #####
286 ===============================================================================
288 This subsection provides a set of functions allowing to control the SDMMC data
296 * @brief Set SDMMC Power state to ON.
297 * @param SDMMCx: Pointer to SDMMC register base
300 HAL_StatusTypeDef
SDMMC_PowerState_ON(SDMMC_TypeDef
*SDMMCx
)
302 /* Set power state to ON */
303 SDMMCx
->POWER
|= SDMMC_POWER_PWRCTRL
;
309 * @brief Set SDMMC Power state to Power-Cycle.
310 * @param SDMMCx: Pointer to SDMMC register base
313 HAL_StatusTypeDef
SDMMC_PowerState_Cycle(SDMMC_TypeDef
*SDMMCx
)
315 /* Set power state to Power Cycle*/
316 SDMMCx
->POWER
|= SDMMC_POWER_PWRCTRL_1
;
322 * @brief Set SDMMC Power state to OFF.
323 * @param SDMMCx: Pointer to SDMMC register base
326 HAL_StatusTypeDef
SDMMC_PowerState_OFF(SDMMC_TypeDef
*SDMMCx
)
328 /* Set power state to OFF */
329 SDMMCx
->POWER
&= ~(SDMMC_POWER_PWRCTRL
);
335 * @brief Get SDMMC Power state.
336 * @param SDMMCx: Pointer to SDMMC register base
337 * @retval Power status of the controller. The returned value can be one of the
343 uint32_t SDMMC_GetPowerState(SDMMC_TypeDef
*SDMMCx
)
345 return (SDMMCx
->POWER
& SDMMC_POWER_PWRCTRL
);
349 * @brief Configure the SDMMC command path according to the specified parameters in
350 * SDMMC_CmdInitTypeDef structure and send the command
351 * @param SDMMCx: Pointer to SDMMC register base
352 * @param Command: pointer to a SDMMC_CmdInitTypeDef structure that contains
353 * the configuration information for the SDMMC command
356 HAL_StatusTypeDef
SDMMC_SendCommand(SDMMC_TypeDef
*SDMMCx
, SDMMC_CmdInitTypeDef
*Command
)
360 /* Check the parameters */
361 assert_param(IS_SDMMC_CMD_INDEX(Command
->CmdIndex
));
362 assert_param(IS_SDMMC_RESPONSE(Command
->Response
));
363 assert_param(IS_SDMMC_WAIT(Command
->WaitForInterrupt
));
364 assert_param(IS_SDMMC_CPSM(Command
->CPSM
));
366 /* Set the SDMMC Argument value */
367 SDMMCx
->ARG
= Command
->Argument
;
369 /* Set SDMMC command parameters */
370 tmpreg
|= (uint32_t)(Command
->CmdIndex
|\
372 Command
->WaitForInterrupt
|\
375 /* Write to SDMMC CMD register */
376 MODIFY_REG(SDMMCx
->CMD
, CMD_CLEAR_MASK
, tmpreg
);
382 * @brief Return the command index of last command for which response received
383 * @param SDMMCx: Pointer to SDMMC register base
384 * @retval Command index of the last command response received
386 uint8_t SDMMC_GetCommandResponse(SDMMC_TypeDef
*SDMMCx
)
388 return (uint8_t)(SDMMCx
->RESPCMD
);
393 * @brief Return the response received from the card for the last command
394 * @param SDMMCx: Pointer to SDMMC register base
395 * @param Response: Specifies the SDMMC response register.
396 * This parameter can be one of the following values:
397 * @arg SDMMC_RESP1: Response Register 1
398 * @arg SDMMC_RESP2: Response Register 2
399 * @arg SDMMC_RESP3: Response Register 3
400 * @arg SDMMC_RESP4: Response Register 4
401 * @retval The Corresponding response register value
403 uint32_t SDMMC_GetResponse(SDMMC_TypeDef
*SDMMCx
, uint32_t Response
)
407 /* Check the parameters */
408 assert_param(IS_SDMMC_RESP(Response
));
410 /* Get the response */
411 tmp
= (uint32_t)(&(SDMMCx
->RESP1
)) + Response
;
413 return (*(__IO
uint32_t *) tmp
);
417 * @brief Configure the SDMMC data path according to the specified
418 * parameters in the SDMMC_DataInitTypeDef.
419 * @param SDMMCx: Pointer to SDMMC register base
420 * @param Data : pointer to a SDMMC_DataInitTypeDef structure
421 * that contains the configuration information for the SDMMC data.
424 HAL_StatusTypeDef
SDMMC_ConfigData(SDMMC_TypeDef
*SDMMCx
, SDMMC_DataInitTypeDef
* Data
)
428 /* Check the parameters */
429 assert_param(IS_SDMMC_DATA_LENGTH(Data
->DataLength
));
430 assert_param(IS_SDMMC_BLOCK_SIZE(Data
->DataBlockSize
));
431 assert_param(IS_SDMMC_TRANSFER_DIR(Data
->TransferDir
));
432 assert_param(IS_SDMMC_TRANSFER_MODE(Data
->TransferMode
));
433 assert_param(IS_SDMMC_DPSM(Data
->DPSM
));
435 /* Set the SDMMC Data TimeOut value */
436 SDMMCx
->DTIMER
= Data
->DataTimeOut
;
438 /* Set the SDMMC DataLength value */
439 SDMMCx
->DLEN
= Data
->DataLength
;
441 /* Set the SDMMC data configuration parameters */
442 tmpreg
|= (uint32_t)(Data
->DataBlockSize
|\
444 Data
->TransferMode
|\
447 // DC - See errata 2.11.4 - 8 SDMMC clock cycles must elapse before DTEN can be set.
448 // 32U below is used as a VERY rough guess that the SDMMC clock is 1/4 of the sytem clock, 8 * 4 = 32 and that the
449 // loop code below only takes 2 CPU cycles to run. All of which will likely be wrong, but right enough most of the time.
450 // It's important that the code isn't optimized-out by the compiler or linker too, see
451 // https://stackoverflow.com/questions/7083482/how-to-prevent-gcc-from-optimizing-out-a-busy-wait-loop
452 register uint32_t count
= 32U;
453 for (unsigned i
= 0; i
< count
; i
++) {
454 __asm__
volatile("" : "+g" (i
) : :);
456 // DC - See errata 2.11.4
458 /* Write to SDMMC DCTRL */
459 MODIFY_REG(SDMMCx
->DCTRL
, DCTRL_CLEAR_MASK
, tmpreg
);
466 * @brief Returns number of remaining data bytes to be transferred.
467 * @param SDMMCx: Pointer to SDMMC register base
468 * @retval Number of remaining data bytes to be transferred
470 uint32_t SDMMC_GetDataCounter(SDMMC_TypeDef
*SDMMCx
)
472 return (SDMMCx
->DCOUNT
);
476 * @brief Get the FIFO data
477 * @param SDMMCx: Pointer to SDMMC register base
478 * @retval Data received
480 uint32_t SDMMC_GetFIFOCount(SDMMC_TypeDef
*SDMMCx
)
482 return (SDMMCx
->FIFO
);
486 * @brief Sets one of the two options of inserting read wait interval.
487 * @param SDMMCx: Pointer to SDMMC register base
488 * @param SDMMC_ReadWaitMode: SDMMC Read Wait operation mode.
489 * This parameter can be:
490 * @arg SDMMC_READ_WAIT_MODE_CLK: Read Wait control by stopping SDMMCCLK
491 * @arg SDMMC_READ_WAIT_MODE_DATA2: Read Wait control using SDMMC_DATA2
494 HAL_StatusTypeDef
SDMMC_SetSDMMCReadWaitMode(SDMMC_TypeDef
*SDMMCx
, uint32_t SDMMC_ReadWaitMode
)
496 /* Check the parameters */
497 assert_param(IS_SDMMC_READWAIT_MODE(SDMMC_ReadWaitMode
));
499 /* Set SDMMC read wait mode */
500 MODIFY_REG(SDMMCx
->DCTRL
, SDMMC_DCTRL_RWMOD
, SDMMC_ReadWaitMode
);
510 /** @defgroup HAL_SDMMC_LL_Group4 Command management functions
511 * @brief Data transfers functions
514 ===============================================================================
515 ##### Commands management functions #####
516 ===============================================================================
518 This subsection provides a set of functions allowing to manage the needed commands.
525 * @brief Send the Data Block Lenght command and check the response
526 * @param SDMMCx: Pointer to SDMMC register base
529 uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef
*SDMMCx
, uint32_t BlockSize
)
531 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
534 /* Set Block Size for Card */
535 sdmmc_cmdinit
.Argument
= (uint32_t)BlockSize
;
536 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SET_BLOCKLEN
;
537 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
538 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
539 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
540 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
542 /* Check for error conditions */
543 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SET_BLOCKLEN
, SDMMC_CMDTIMEOUT
);
549 * @brief Send the Read Single Block command and check the response
550 * @param SDMMCx: Pointer to SDMMC register base
553 uint32_t SDMMC_CmdReadSingleBlock(SDMMC_TypeDef
*SDMMCx
, uint32_t ReadAdd
)
555 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
558 /* Set Block Size for Card */
559 sdmmc_cmdinit
.Argument
= (uint32_t)ReadAdd
;
560 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_READ_SINGLE_BLOCK
;
561 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
562 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
563 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
564 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
566 /* Check for error conditions */
567 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_READ_SINGLE_BLOCK
, SDMMC_CMDTIMEOUT
);
573 * @brief Send the Read Multi Block command and check the response
574 * @param SDMMCx: Pointer to SDMMC register base
577 uint32_t SDMMC_CmdReadMultiBlock(SDMMC_TypeDef
*SDMMCx
, uint32_t ReadAdd
)
579 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
582 /* Set Block Size for Card */
583 sdmmc_cmdinit
.Argument
= (uint32_t)ReadAdd
;
584 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_READ_MULT_BLOCK
;
585 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
586 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
587 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
588 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
590 /* Check for error conditions */
591 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_READ_MULT_BLOCK
, SDMMC_CMDTIMEOUT
);
597 * @brief Send the Write Single Block command and check the response
598 * @param SDMMCx: Pointer to SDMMC register base
601 uint32_t SDMMC_CmdWriteSingleBlock(SDMMC_TypeDef
*SDMMCx
, uint32_t WriteAdd
)
603 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
606 /* Set Block Size for Card */
607 sdmmc_cmdinit
.Argument
= (uint32_t)WriteAdd
;
608 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_WRITE_SINGLE_BLOCK
;
609 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
610 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
611 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
612 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
614 /* Check for error conditions */
615 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_WRITE_SINGLE_BLOCK
, SDMMC_CMDTIMEOUT
);
621 * @brief Send the Write Multi Block command and check the response
622 * @param SDMMCx: Pointer to SDMMC register base
625 uint32_t SDMMC_CmdWriteMultiBlock(SDMMC_TypeDef
*SDMMCx
, uint32_t WriteAdd
)
627 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
630 /* Set Block Size for Card */
631 sdmmc_cmdinit
.Argument
= (uint32_t)WriteAdd
;
632 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_WRITE_MULT_BLOCK
;
633 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
634 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
635 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
636 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
638 /* Check for error conditions */
639 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_WRITE_MULT_BLOCK
, SDMMC_CMDTIMEOUT
);
645 * @brief Send the Start Address Erase command for SD and check the response
646 * @param SDMMCx: Pointer to SDMMC register base
649 uint32_t SDMMC_CmdSDEraseStartAdd(SDMMC_TypeDef
*SDMMCx
, uint32_t StartAdd
)
651 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
654 /* Set Block Size for Card */
655 sdmmc_cmdinit
.Argument
= (uint32_t)StartAdd
;
656 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SD_ERASE_GRP_START
;
657 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
658 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
659 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
660 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
662 /* Check for error conditions */
663 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SD_ERASE_GRP_START
, SDMMC_CMDTIMEOUT
);
669 * @brief Send the End Address Erase command for SD and check the response
670 * @param SDMMCx: Pointer to SDMMC register base
673 uint32_t SDMMC_CmdSDEraseEndAdd(SDMMC_TypeDef
*SDMMCx
, uint32_t EndAdd
)
675 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
678 /* Set Block Size for Card */
679 sdmmc_cmdinit
.Argument
= (uint32_t)EndAdd
;
680 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SD_ERASE_GRP_END
;
681 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
682 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
683 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
684 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
686 /* Check for error conditions */
687 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SD_ERASE_GRP_END
, SDMMC_CMDTIMEOUT
);
693 * @brief Send the Start Address Erase command and check the response
694 * @param SDMMCx: Pointer to SDMMC register base
697 uint32_t SDMMC_CmdEraseStartAdd(SDMMC_TypeDef
*SDMMCx
, uint32_t StartAdd
)
699 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
702 /* Set Block Size for Card */
703 sdmmc_cmdinit
.Argument
= (uint32_t)StartAdd
;
704 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_ERASE_GRP_START
;
705 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
706 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
707 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
708 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
710 /* Check for error conditions */
711 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_ERASE_GRP_START
, SDMMC_CMDTIMEOUT
);
717 * @brief Send the End Address Erase command and check the response
718 * @param SDMMCx: Pointer to SDMMC register base
721 uint32_t SDMMC_CmdEraseEndAdd(SDMMC_TypeDef
*SDMMCx
, uint32_t EndAdd
)
723 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
726 /* Set Block Size for Card */
727 sdmmc_cmdinit
.Argument
= (uint32_t)EndAdd
;
728 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_ERASE_GRP_END
;
729 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
730 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
731 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
732 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
734 /* Check for error conditions */
735 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_ERASE_GRP_END
, SDMMC_CMDTIMEOUT
);
741 * @brief Send the Erase command and check the response
742 * @param SDMMCx Pointer to SDMMC register base
743 * @param EraseType Type of erase to be performed
746 uint32_t SDMMC_CmdErase(SDMMC_TypeDef
*SDMMCx
, uint32_t EraseType
)
748 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
751 /* Set Block Size for Card */
752 sdmmc_cmdinit
.Argument
= EraseType
;
753 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_ERASE
;
754 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
755 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
756 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
757 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
759 /* Check for error conditions */
760 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_ERASE
, SDMMC_MAXERASETIMEOUT
);
766 * @brief Send the Stop Transfer command and check the response.
767 * @param SDMMCx: Pointer to SDMMC register base
770 uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef
*SDMMCx
)
772 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
775 /* Send CMD12 STOP_TRANSMISSION */
776 sdmmc_cmdinit
.Argument
= 0U;
777 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_STOP_TRANSMISSION
;
778 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
779 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
780 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
782 __SDMMC_CMDSTOP_ENABLE(SDMMCx
);
783 __SDMMC_CMDTRANS_DISABLE(SDMMCx
);
785 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
787 /* Check for error conditions */
788 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_STOP_TRANSMISSION
, SDMMC_STOPTRANSFERTIMEOUT
);
790 __SDMMC_CMDSTOP_DISABLE(SDMMCx
);
792 /* Ignore Address Out Of Range Error, Not relevant at end of memory */
793 if (errorstate
== SDMMC_ERROR_ADDR_OUT_OF_RANGE
)
795 errorstate
= SDMMC_ERROR_NONE
;
802 * @brief Send the Select Deselect command and check the response.
803 * @param SDMMCx: Pointer to SDMMC register base
804 * @param addr: Address of the card to be selected
807 uint32_t SDMMC_CmdSelDesel(SDMMC_TypeDef
*SDMMCx
, uint64_t Addr
)
809 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
812 /* Send CMD7 SDMMC_SEL_DESEL_CARD */
813 sdmmc_cmdinit
.Argument
= (uint32_t)Addr
;
814 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SEL_DESEL_CARD
;
815 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
816 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
817 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
818 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
820 /* Check for error conditions */
821 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SEL_DESEL_CARD
, SDMMC_CMDTIMEOUT
);
827 * @brief Send the Go Idle State command and check the response.
828 * @param SDMMCx: Pointer to SDMMC register base
831 uint32_t SDMMC_CmdGoIdleState(SDMMC_TypeDef
*SDMMCx
)
833 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
836 sdmmc_cmdinit
.Argument
= 0U;
837 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_GO_IDLE_STATE
;
838 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_NO
;
839 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
840 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
841 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
843 /* Check for error conditions */
844 errorstate
= SDMMC_GetCmdError(SDMMCx
);
850 * @brief Send the Operating Condition command and check the response.
851 * @param SDMMCx: Pointer to SDMMC register base
854 uint32_t SDMMC_CmdOperCond(SDMMC_TypeDef
*SDMMCx
)
856 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
859 /* Send CMD8 to verify SD card interface operating condition */
860 /* Argument: - [31:12]: Reserved (shall be set to '0')
861 - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
862 - [7:0]: Check Pattern (recommended 0xAA) */
863 /* CMD Response: R7 */
864 sdmmc_cmdinit
.Argument
= SDMMC_CHECK_PATTERN
;
865 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_HS_SEND_EXT_CSD
;
866 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
867 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
868 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
869 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
871 /* Check for error conditions */
872 errorstate
= SDMMC_GetCmdResp7(SDMMCx
);
878 * @brief Send the Application command to verify that that the next command
879 * is an application specific com-mand rather than a standard command
880 * and check the response.
881 * @param SDMMCx: Pointer to SDMMC register base
882 * @param Argument: Command Argument
885 uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
887 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
890 sdmmc_cmdinit
.Argument
= (uint32_t)Argument
;
891 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_APP_CMD
;
892 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
893 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
894 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
895 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
897 /* Check for error conditions */
898 /* If there is a HAL_ERROR, it is a MMC card, else
899 it is a SD card: SD card 2.0 (voltage range mismatch)
901 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_APP_CMD
, SDMMC_CMDTIMEOUT
);
907 * @brief Send the command asking the accessed card to send its operating
908 * condition register (OCR)
909 * @param SDMMCx: Pointer to SDMMC register base
910 * @param Argument: Command Argument
913 uint32_t SDMMC_CmdAppOperCommand(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
915 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
918 sdmmc_cmdinit
.Argument
= Argument
;
919 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SD_APP_OP_COND
;
920 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
921 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
922 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
923 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
925 /* Check for error conditions */
926 errorstate
= SDMMC_GetCmdResp3(SDMMCx
);
932 * @brief Send the Bus Width command and check the response.
933 * @param SDMMCx: Pointer to SDMMC register base
934 * @param BusWidth: BusWidth
937 uint32_t SDMMC_CmdBusWidth(SDMMC_TypeDef
*SDMMCx
, uint32_t BusWidth
)
939 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
942 sdmmc_cmdinit
.Argument
= (uint32_t)BusWidth
;
943 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_APP_SD_SET_BUSWIDTH
;
944 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
945 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
946 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
947 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
949 /* Check for error conditions */
950 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_APP_SD_SET_BUSWIDTH
, SDMMC_CMDTIMEOUT
);
956 * @brief Send the Send SCR command and check the response.
957 * @param SDMMCx: Pointer to SDMMC register base
960 uint32_t SDMMC_CmdSendSCR(SDMMC_TypeDef
*SDMMCx
)
962 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
965 /* Send CMD51 SD_APP_SEND_SCR */
966 sdmmc_cmdinit
.Argument
= 0U;
967 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SD_APP_SEND_SCR
;
968 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
969 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
970 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
971 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
973 /* Check for error conditions */
974 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SD_APP_SEND_SCR
, SDMMC_CMDTIMEOUT
);
980 * @brief Send the Send CID command and check the response.
981 * @param SDMMCx: Pointer to SDMMC register base
984 uint32_t SDMMC_CmdSendCID(SDMMC_TypeDef
*SDMMCx
)
986 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
989 /* Send CMD2 ALL_SEND_CID */
990 sdmmc_cmdinit
.Argument
= 0U;
991 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_ALL_SEND_CID
;
992 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_LONG
;
993 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
994 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
995 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
997 /* Check for error conditions */
998 errorstate
= SDMMC_GetCmdResp2(SDMMCx
);
1004 * @brief Send the Send CSD command and check the response.
1005 * @param SDMMCx: Pointer to SDMMC register base
1006 * @param Argument: Command Argument
1007 * @retval HAL status
1009 uint32_t SDMMC_CmdSendCSD(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
1011 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1012 uint32_t errorstate
;
1014 /* Send CMD9 SEND_CSD */
1015 sdmmc_cmdinit
.Argument
= Argument
;
1016 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SEND_CSD
;
1017 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_LONG
;
1018 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1019 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1020 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1022 /* Check for error conditions */
1023 errorstate
= SDMMC_GetCmdResp2(SDMMCx
);
1029 * @brief Send the Send CSD command and check the response.
1030 * @param SDMMCx: Pointer to SDMMC register base
1031 * @param pRCA: Card RCA
1032 * @retval HAL status
1034 uint32_t SDMMC_CmdSetRelAdd(SDMMC_TypeDef
*SDMMCx
, uint16_t *pRCA
)
1036 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1037 uint32_t errorstate
;
1039 /* Send CMD3 SD_CMD_SET_REL_ADDR */
1040 sdmmc_cmdinit
.Argument
= 0U;
1041 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SET_REL_ADDR
;
1042 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1043 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1044 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1045 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1047 /* Check for error conditions */
1048 errorstate
= SDMMC_GetCmdResp6(SDMMCx
, SDMMC_CMD_SET_REL_ADDR
, pRCA
);
1054 * @brief Send the Status command and check the response.
1055 * @param SDMMCx: Pointer to SDMMC register base
1056 * @param Argument: Command Argument
1057 * @retval HAL status
1059 uint32_t SDMMC_CmdSendStatus(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
1061 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1062 uint32_t errorstate
;
1064 sdmmc_cmdinit
.Argument
= Argument
;
1065 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SEND_STATUS
;
1066 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1067 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1068 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1069 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1071 /* Check for error conditions */
1072 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SEND_STATUS
, SDMMC_CMDTIMEOUT
);
1078 * @brief Send the Status register command and check the response.
1079 * @param SDMMCx: Pointer to SDMMC register base
1080 * @retval HAL status
1082 uint32_t SDMMC_CmdStatusRegister(SDMMC_TypeDef
*SDMMCx
)
1084 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1085 uint32_t errorstate
;
1087 sdmmc_cmdinit
.Argument
= 0U;
1088 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SD_APP_STATUS
;
1089 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1090 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1091 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1092 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1094 /* Check for error conditions */
1095 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_SD_APP_STATUS
, SDMMC_CMDTIMEOUT
);
1101 * @brief Sends host capacity support information and activates the card's
1102 * initialization process. Send SDMMC_CMD_SEND_OP_COND command
1103 * @param SDMMCx: Pointer to SDMMC register base
1104 * @parame Argument: Argument used for the command
1105 * @retval HAL status
1107 uint32_t SDMMC_CmdOpCondition(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
1109 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1110 uint32_t errorstate
;
1112 sdmmc_cmdinit
.Argument
= Argument
;
1113 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_SEND_OP_COND
;
1114 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1115 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1116 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1117 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1119 /* Check for error conditions */
1120 errorstate
= SDMMC_GetCmdResp3(SDMMCx
);
1126 * @brief Checks switchable function and switch card function. SDMMC_CMD_HS_SWITCH comand
1127 * @param SDMMCx: Pointer to SDMMC register base
1128 * @parame Argument: Argument used for the command
1129 * @retval HAL status
1131 uint32_t SDMMC_CmdSwitch(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
1133 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1134 uint32_t errorstate
;
1136 /* Send CMD6 to activate SDR50 Mode and Power Limit 1.44W */
1137 /* CMD Response: R1 */
1138 sdmmc_cmdinit
.Argument
= Argument
; /* SDMMC_SDR25_SWITCH_PATTERN;*/
1139 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_HS_SWITCH
;
1140 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1141 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1142 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1143 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1145 /* Check for error conditions */
1146 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_HS_SWITCH
, SDMMC_CMDTIMEOUT
);
1152 * @brief Send the command asking the accessed card to send its operating
1153 * condition register (OCR)
1155 * @retval HAL status
1157 uint32_t SDMMC_CmdVoltageSwitch(SDMMC_TypeDef
*SDMMCx
)
1159 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1160 uint32_t errorstate
;
1162 sdmmc_cmdinit
.Argument
= 0x00000000;
1163 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_VOLTAGE_SWITCH
;
1164 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1165 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1166 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1167 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1169 /* Check for error conditions */
1170 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_VOLTAGE_SWITCH
, SDMMC_CMDTIMEOUT
);
1176 * @brief Send the Send EXT_CSD command and check the response.
1177 * @param SDMMCx: Pointer to SDMMC register base
1178 * @param Argument: Command Argument
1179 * @retval HAL status
1181 uint32_t SDMMC_CmdSendEXTCSD(SDMMC_TypeDef
*SDMMCx
, uint32_t Argument
)
1183 SDMMC_CmdInitTypeDef sdmmc_cmdinit
;
1184 uint32_t errorstate
;
1186 /* Send CMD9 SEND_CSD */
1187 sdmmc_cmdinit
.Argument
= Argument
;
1188 sdmmc_cmdinit
.CmdIndex
= SDMMC_CMD_HS_SEND_EXT_CSD
;
1189 sdmmc_cmdinit
.Response
= SDMMC_RESPONSE_SHORT
;
1190 sdmmc_cmdinit
.WaitForInterrupt
= SDMMC_WAIT_NO
;
1191 sdmmc_cmdinit
.CPSM
= SDMMC_CPSM_ENABLE
;
1192 (void)SDMMC_SendCommand(SDMMCx
, &sdmmc_cmdinit
);
1194 /* Check for error conditions */
1195 errorstate
= SDMMC_GetCmdResp1(SDMMCx
, SDMMC_CMD_HS_SEND_EXT_CSD
,SDMMC_CMDTIMEOUT
);
1205 /* Private function ----------------------------------------------------------*/
1206 /** @addtogroup SD_Private_Functions
1211 * @brief Checks for error conditions for CMD0.
1212 * @param hsd: SD handle
1213 * @retval SD Card error state
1215 static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef
*SDMMCx
)
1217 /* 8 is the number of required instructions cycles for the below loop statement.
1218 The SDMMC_CMDTIMEOUT is expressed in ms */
1219 uint32_t count
= SDMMC_CMDTIMEOUT
* (SystemCoreClock
/ 8U /1000U);
1225 return SDMMC_ERROR_TIMEOUT
;
1228 }while(!__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CMDSENT
));
1230 /* Clear all the static flags */
1231 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_STATIC_CMD_FLAGS
);
1233 return SDMMC_ERROR_NONE
;
1237 * @brief Checks for error conditions for R1 response.
1238 * @param hsd: SD handle
1239 * @param SD_CMD: The sent command index
1240 * @retval SD Card error state
1242 static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef
*SDMMCx
, uint8_t SD_CMD
, uint32_t Timeout
)
1244 uint32_t response_r1
;
1247 /* 8 is the number of required instructions cycles for the below loop statement.
1248 The Timeout is expressed in ms */
1249 uint32_t count
= Timeout
* (SystemCoreClock
/ 8U /1000U);
1255 return SDMMC_ERROR_TIMEOUT
;
1257 sta_reg
= SDMMCx
->STA
;
1258 }while(((sta_reg
& (SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
| SDMMC_FLAG_BUSYD0END
)) == 0U) ||
1259 ((sta_reg
& SDMMC_FLAG_CMDACT
) != 0U ));
1261 if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
))
1263 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
);
1265 return SDMMC_ERROR_CMD_RSP_TIMEOUT
;
1267 else if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
))
1269 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
);
1271 return SDMMC_ERROR_CMD_CRC_FAIL
;
1278 /* Clear all the static flags */
1279 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_STATIC_CMD_FLAGS
);
1281 /* Check response received is of desired command */
1282 if(SDMMC_GetCommandResponse(SDMMCx
) != SD_CMD
)
1284 return SDMMC_ERROR_CMD_CRC_FAIL
;
1287 /* We have received response, retrieve it for analysis */
1288 response_r1
= SDMMC_GetResponse(SDMMCx
, SDMMC_RESP1
);
1290 if((response_r1
& SDMMC_OCR_ERRORBITS
) == SDMMC_ALLZERO
)
1292 return SDMMC_ERROR_NONE
;
1294 else if((response_r1
& SDMMC_OCR_ADDR_OUT_OF_RANGE
) == SDMMC_OCR_ADDR_OUT_OF_RANGE
)
1296 return SDMMC_ERROR_ADDR_OUT_OF_RANGE
;
1298 else if((response_r1
& SDMMC_OCR_ADDR_MISALIGNED
) == SDMMC_OCR_ADDR_MISALIGNED
)
1300 return SDMMC_ERROR_ADDR_MISALIGNED
;
1302 else if((response_r1
& SDMMC_OCR_BLOCK_LEN_ERR
) == SDMMC_OCR_BLOCK_LEN_ERR
)
1304 return SDMMC_ERROR_BLOCK_LEN_ERR
;
1306 else if((response_r1
& SDMMC_OCR_ERASE_SEQ_ERR
) == SDMMC_OCR_ERASE_SEQ_ERR
)
1308 return SDMMC_ERROR_ERASE_SEQ_ERR
;
1310 else if((response_r1
& SDMMC_OCR_BAD_ERASE_PARAM
) == SDMMC_OCR_BAD_ERASE_PARAM
)
1312 return SDMMC_ERROR_BAD_ERASE_PARAM
;
1314 else if((response_r1
& SDMMC_OCR_WRITE_PROT_VIOLATION
) == SDMMC_OCR_WRITE_PROT_VIOLATION
)
1316 return SDMMC_ERROR_WRITE_PROT_VIOLATION
;
1318 else if((response_r1
& SDMMC_OCR_LOCK_UNLOCK_FAILED
) == SDMMC_OCR_LOCK_UNLOCK_FAILED
)
1320 return SDMMC_ERROR_LOCK_UNLOCK_FAILED
;
1322 else if((response_r1
& SDMMC_OCR_COM_CRC_FAILED
) == SDMMC_OCR_COM_CRC_FAILED
)
1324 return SDMMC_ERROR_COM_CRC_FAILED
;
1326 else if((response_r1
& SDMMC_OCR_ILLEGAL_CMD
) == SDMMC_OCR_ILLEGAL_CMD
)
1328 return SDMMC_ERROR_ILLEGAL_CMD
;
1330 else if((response_r1
& SDMMC_OCR_CARD_ECC_FAILED
) == SDMMC_OCR_CARD_ECC_FAILED
)
1332 return SDMMC_ERROR_CARD_ECC_FAILED
;
1334 else if((response_r1
& SDMMC_OCR_CC_ERROR
) == SDMMC_OCR_CC_ERROR
)
1336 return SDMMC_ERROR_CC_ERR
;
1338 else if((response_r1
& SDMMC_OCR_STREAM_READ_UNDERRUN
) == SDMMC_OCR_STREAM_READ_UNDERRUN
)
1340 return SDMMC_ERROR_STREAM_READ_UNDERRUN
;
1342 else if((response_r1
& SDMMC_OCR_STREAM_WRITE_OVERRUN
) == SDMMC_OCR_STREAM_WRITE_OVERRUN
)
1344 return SDMMC_ERROR_STREAM_WRITE_OVERRUN
;
1346 else if((response_r1
& SDMMC_OCR_CID_CSD_OVERWRITE
) == SDMMC_OCR_CID_CSD_OVERWRITE
)
1348 return SDMMC_ERROR_CID_CSD_OVERWRITE
;
1350 else if((response_r1
& SDMMC_OCR_WP_ERASE_SKIP
) == SDMMC_OCR_WP_ERASE_SKIP
)
1352 return SDMMC_ERROR_WP_ERASE_SKIP
;
1354 else if((response_r1
& SDMMC_OCR_CARD_ECC_DISABLED
) == SDMMC_OCR_CARD_ECC_DISABLED
)
1356 return SDMMC_ERROR_CARD_ECC_DISABLED
;
1358 else if((response_r1
& SDMMC_OCR_ERASE_RESET
) == SDMMC_OCR_ERASE_RESET
)
1360 return SDMMC_ERROR_ERASE_RESET
;
1362 else if((response_r1
& SDMMC_OCR_AKE_SEQ_ERROR
) == SDMMC_OCR_AKE_SEQ_ERROR
)
1364 return SDMMC_ERROR_AKE_SEQ_ERR
;
1368 return SDMMC_ERROR_GENERAL_UNKNOWN_ERR
;
1373 * @brief Checks for error conditions for R2 (CID or CSD) response.
1374 * @param hsd: SD handle
1375 * @retval SD Card error state
1377 static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef
*SDMMCx
)
1380 /* 8 is the number of required instructions cycles for the below loop statement.
1381 The SDMMC_CMDTIMEOUT is expressed in ms */
1382 uint32_t count
= SDMMC_CMDTIMEOUT
* (SystemCoreClock
/ 8U /1000U);
1388 return SDMMC_ERROR_TIMEOUT
;
1390 sta_reg
= SDMMCx
->STA
;
1391 }while(((sta_reg
& (SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
)) == 0U) ||
1392 ((sta_reg
& SDMMC_FLAG_CMDACT
) != 0U ));
1394 if (__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
))
1396 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
);
1398 return SDMMC_ERROR_CMD_RSP_TIMEOUT
;
1400 else if (__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
))
1402 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
);
1404 return SDMMC_ERROR_CMD_CRC_FAIL
;
1408 /* No error flag set */
1409 /* Clear all the static flags */
1410 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_STATIC_CMD_FLAGS
);
1413 return SDMMC_ERROR_NONE
;
1417 * @brief Checks for error conditions for R3 (OCR) response.
1418 * @param hsd: SD handle
1419 * @retval SD Card error state
1421 static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef
*SDMMCx
)
1424 /* 8 is the number of required instructions cycles for the below loop statement.
1425 The SDMMC_CMDTIMEOUT is expressed in ms */
1426 uint32_t count
= SDMMC_CMDTIMEOUT
* (SystemCoreClock
/ 8U /1000U);
1432 return SDMMC_ERROR_TIMEOUT
;
1434 sta_reg
= SDMMCx
->STA
;
1435 }while(((sta_reg
& (SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
)) == 0U) ||
1436 ((sta_reg
& SDMMC_FLAG_CMDACT
) != 0U ));
1438 if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
))
1440 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
);
1442 return SDMMC_ERROR_CMD_RSP_TIMEOUT
;
1446 /* Clear all the static flags */
1447 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_STATIC_CMD_FLAGS
);
1450 return SDMMC_ERROR_NONE
;
1454 * @brief Checks for error conditions for R6 (RCA) response.
1455 * @param hsd: SD handle
1456 * @param SD_CMD: The sent command index
1457 * @param pRCA: Pointer to the variable that will contain the SD card relative
1459 * @retval SD Card error state
1461 static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef
*SDMMCx
, uint8_t SD_CMD
, uint16_t *pRCA
)
1463 uint32_t response_r1
;
1466 /* 8 is the number of required instructions cycles for the below loop statement.
1467 The SDMMC_CMDTIMEOUT is expressed in ms */
1468 uint32_t count
= SDMMC_CMDTIMEOUT
* (SystemCoreClock
/ 8U /1000U);
1474 return SDMMC_ERROR_TIMEOUT
;
1476 sta_reg
= SDMMCx
->STA
;
1477 }while(((sta_reg
& (SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
)) == 0U) ||
1478 ((sta_reg
& SDMMC_FLAG_CMDACT
) != 0U ));
1480 if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
))
1482 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
);
1484 return SDMMC_ERROR_CMD_RSP_TIMEOUT
;
1486 else if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
))
1488 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
);
1490 return SDMMC_ERROR_CMD_CRC_FAIL
;
1497 /* Check response received is of desired command */
1498 if(SDMMC_GetCommandResponse(SDMMCx
) != SD_CMD
)
1500 return SDMMC_ERROR_CMD_CRC_FAIL
;
1503 /* Clear all the static flags */
1504 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_STATIC_CMD_FLAGS
);
1506 /* We have received response, retrieve it. */
1507 response_r1
= SDMMC_GetResponse(SDMMCx
, SDMMC_RESP1
);
1509 if((response_r1
& (SDMMC_R6_GENERAL_UNKNOWN_ERROR
| SDMMC_R6_ILLEGAL_CMD
| SDMMC_R6_COM_CRC_FAILED
)) == SDMMC_ALLZERO
)
1511 *pRCA
= (uint16_t) (response_r1
>> 16);
1513 return SDMMC_ERROR_NONE
;
1515 else if((response_r1
& SDMMC_R6_ILLEGAL_CMD
) == SDMMC_R6_ILLEGAL_CMD
)
1517 return SDMMC_ERROR_ILLEGAL_CMD
;
1519 else if((response_r1
& SDMMC_R6_COM_CRC_FAILED
) == SDMMC_R6_COM_CRC_FAILED
)
1521 return SDMMC_ERROR_COM_CRC_FAILED
;
1525 return SDMMC_ERROR_GENERAL_UNKNOWN_ERR
;
1530 * @brief Checks for error conditions for R7 response.
1531 * @param hsd: SD handle
1532 * @retval SD Card error state
1534 static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef
*SDMMCx
)
1537 /* 8 is the number of required instructions cycles for the below loop statement.
1538 The SDMMC_CMDTIMEOUT is expressed in ms */
1539 uint32_t count
= SDMMC_CMDTIMEOUT
* (SystemCoreClock
/ 8U /1000U);
1545 return SDMMC_ERROR_TIMEOUT
;
1547 sta_reg
= SDMMCx
->STA
;
1548 }while(((sta_reg
& (SDMMC_FLAG_CCRCFAIL
| SDMMC_FLAG_CMDREND
| SDMMC_FLAG_CTIMEOUT
)) == 0U) ||
1549 ((sta_reg
& SDMMC_FLAG_CMDACT
) != 0U ));
1551 if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
))
1553 /* Card is SD V2.0 compliant */
1554 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CTIMEOUT
);
1556 return SDMMC_ERROR_CMD_RSP_TIMEOUT
;
1559 else if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
))
1561 /* Card is SD V2.0 compliant */
1562 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CCRCFAIL
);
1564 return SDMMC_ERROR_CMD_CRC_FAIL
;
1571 if(__SDMMC_GET_FLAG(SDMMCx
, SDMMC_FLAG_CMDREND
))
1573 /* Card is SD V2.0 compliant */
1574 __SDMMC_CLEAR_FLAG(SDMMCx
, SDMMC_FLAG_CMDREND
);
1577 return SDMMC_ERROR_NONE
;
1585 #endif /* HAL_SD_MODULE_ENABLED || HAL_MMC_MODULE_ENABLED */
1594 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/