Merge pull request #10558 from iNavFlight/MrD_Correct-comments-on-OSD-symbols
[inav.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_ll_sdmmc.c
blob10860289a6407a3c3e65b1828865dbdb74003676
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_ll_sdmmc.c
4 * @author MCD Application Team
5 * @version V1.2.2
6 * @date 14-April-2017
7 * @brief SDMMC Low Layer HAL module driver.
8 *
9 * This file provides firmware functions to manage the following
10 * functionalities of the SDMMC peripheral:
11 * + Initialization/de-initialization functions
12 * + I/O operation functions
13 * + Peripheral Control functions
14 * + Peripheral State functions
16 @verbatim
17 ==============================================================================
18 ##### SDMMC peripheral features #####
19 ==============================================================================
20 [..] The SD/SDMMC MMC card host interface (SDMMC) provides an interface between the APB2
21 peripheral bus and MultiMedia cards (MMCs), SD memory cards, SDMMC cards and CE-ATA
22 devices.
24 [..] The SDMMC features include the following:
25 (+) Full compliance with MultiMedia Card System Specification Version 4.2. Card support
26 for three different databus modes: 1-bit (default), 4-bit and 8-bit
27 (+) Full compatibility with previous versions of MultiMedia Cards (forward compatibility)
28 (+) Full compliance with SD Memory Card Specifications Version 2.0
29 (+) Full compliance with SD I/O Card Specification Version 2.0: card support for two
30 different data bus modes: 1-bit (default) and 4-bit
31 (+) Full support of the CE-ATA features (full compliance with CE-ATA digital protocol
32 Rev1.1)
33 (+) Data transfer up to 48 MHz for the 8 bit mode
34 (+) Data and command output enable signals to control external bidirectional drivers.
37 ##### How to use this driver #####
38 ==============================================================================
39 [..]
40 This driver is a considered as a driver of service for external devices drivers
41 that interfaces with the SDMMC peripheral.
42 According to the device used (SD card/ MMC card / SDMMC card ...), a set of APIs
43 is used in the device's driver to perform SDMMC operations and functionalities.
45 This driver is almost transparent for the final user, it is only used to implement other
46 functionalities of the external device.
48 [..]
49 (+) The SDMMC clock (SDMMCCLK = 48 MHz) is coming from a specific output of PLL
50 (PLL48CLK). Before start working with SDMMC peripheral make sure that the
51 PLL is well configured.
52 The SDMMC peripheral uses two clock signals:
53 (++) SDMMC adapter clock (SDMMCCLK = 48 MHz)
54 (++) APB2 bus clock (PCLK2)
56 -@@- PCLK2 and SDMMC_CK clock frequencies must respect the following condition:
57 Frequency(PCLK2) >= (3 / 8 x Frequency(SDMMC_CK))
59 (+) Enable/Disable peripheral clock using RCC peripheral macros related to SDMMC
60 peripheral.
62 (+) Enable the Power ON State using the SDMMC_PowerState_ON(SDMMCx)
63 function and disable it using the function SDMMC_PowerState_OFF(SDMMCx).
65 (+) Enable/Disable the clock using the __SDMMC_ENABLE()/__SDMMC_DISABLE() macros.
67 (+) Enable/Disable the peripheral interrupts using the macros __SDMMC_ENABLE_IT(hSDMMC, IT)
68 and __SDMMC_DISABLE_IT(hSDMMC, IT) if you need to use interrupt mode.
70 (+) When using the DMA mode
71 (++) Configure the DMA in the MSP layer of the external device
72 (++) Active the needed channel Request
73 (++) Enable the DMA using __SDMMC_DMA_ENABLE() macro or Disable it using the macro
74 __SDMMC_DMA_DISABLE().
76 (+) To control the CPSM (Command Path State Machine) and send
77 commands to the card use the SDMMC_SendCommand(SDMMCx),
78 SDMMC_GetCommandResponse() and SDMMC_GetResponse() functions. First, user has
79 to fill the command structure (pointer to SDMMC_CmdInitTypeDef) according
80 to the selected command to be sent.
81 The parameters that should be filled are:
82 (++) Command Argument
83 (++) Command Index
84 (++) Command Response type
85 (++) Command Wait
86 (++) CPSM Status (Enable or Disable).
88 -@@- To check if the command is well received, read the SDMMC_CMDRESP
89 register using the SDMMC_GetCommandResponse().
90 The SDMMC responses registers (SDMMC_RESP1 to SDMMC_RESP2), use the
91 SDMMC_GetResponse() function.
93 (+) To control the DPSM (Data Path State Machine) and send/receive
94 data to/from the card use the SDMMC_DataConfig(), SDMMC_GetDataCounter(),
95 SDMMC_ReadFIFO(), SDMMC_WriteFIFO() and SDMMC_GetFIFOCount() functions.
97 *** Read Operations ***
98 =======================
99 [..]
100 (#) First, user has to fill the data structure (pointer to
101 SDMMC_DataInitTypeDef) according to the selected data type to be received.
102 The parameters that should be filled are:
103 (++) Data TimeOut
104 (++) Data Length
105 (++) Data Block size
106 (++) Data Transfer direction: should be from card (To SDMMC)
107 (++) Data Transfer mode
108 (++) DPSM Status (Enable or Disable)
110 (#) Configure the SDMMC resources to receive the data from the card
111 according to selected transfer mode (Refer to Step 8, 9 and 10).
113 (#) Send the selected Read command (refer to step 11).
115 (#) Use the SDMMC flags/interrupts to check the transfer status.
117 *** Write Operations ***
118 ========================
119 [..]
120 (#) First, user has to fill the data structure (pointer to
121 SDMMC_DataInitTypeDef) according to the selected data type to be received.
122 The parameters that should be filled are:
123 (++) Data TimeOut
124 (++) Data Length
125 (++) Data Block size
126 (++) Data Transfer direction: should be to card (To CARD)
127 (++) Data Transfer mode
128 (++) DPSM Status (Enable or Disable)
130 (#) Configure the SDMMC resources to send the data to the card according to
131 selected transfer mode.
133 (#) Send the selected Write command.
135 (#) Use the SDMMC flags/interrupts to check the transfer status.
137 *** Command management operations ***
138 =====================================
139 [..]
140 (#) The commands used for Read/Write//Erase operations are managed in
141 separate functions.
142 Each function allows to send the needed command with the related argument,
143 then check the response.
144 By the same approach, you could implement a command and check the response.
146 @endverbatim
147 ******************************************************************************
148 * @attention
150 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
152 * Redistribution and use in source and binary forms, with or without modification,
153 * are permitted provided that the following conditions are met:
154 * 1. Redistributions of source code must retain the above copyright notice,
155 * this list of conditions and the following disclaimer.
156 * 2. Redistributions in binary form must reproduce the above copyright notice,
157 * this list of conditions and the following disclaimer in the documentation
158 * and/or other materials provided with the distribution.
159 * 3. Neither the name of STMicroelectronics nor the names of its contributors
160 * may be used to endorse or promote products derived from this software
161 * without specific prior written permission.
163 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
164 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
165 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
166 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
167 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
168 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
169 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
170 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
171 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
172 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
174 ******************************************************************************
177 /* Includes ------------------------------------------------------------------*/
178 #include "stm32f7xx_hal.h"
180 /** @addtogroup STM32F7xx_HAL_Driver
181 * @{
184 /** @defgroup SDMMC_LL SDMMC Low Layer
185 * @brief Low layer module for SD
186 * @{
189 #if defined (HAL_SD_MODULE_ENABLED) || defined(HAL_MMC_MODULE_ENABLED)
191 /* Private typedef -----------------------------------------------------------*/
192 /* Private define ------------------------------------------------------------*/
193 /* Private macro -------------------------------------------------------------*/
194 /* Private variables ---------------------------------------------------------*/
195 /* Private function prototypes -----------------------------------------------*/
196 static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef *SDMMCx);
197 static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint32_t Timeout);
198 static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef *SDMMCx);
199 static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef *SDMMCx);
200 static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef *SDMMCx);
201 static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint16_t *pRCA);
203 /* Exported functions --------------------------------------------------------*/
205 /** @defgroup SDMMC_LL_Exported_Functions SDMMC Low Layer Exported Functions
206 * @{
209 /** @defgroup HAL_SDMMC_LL_Group1 Initialization de-initialization functions
210 * @brief Initialization and Configuration functions
212 @verbatim
213 ===============================================================================
214 ##### Initialization/de-initialization functions #####
215 ===============================================================================
216 [..] This section provides functions allowing to:
218 @endverbatim
219 * @{
223 * @brief Initializes the SDMMC according to the specified
224 * parameters in the SDMMC_InitTypeDef and create the associated handle.
225 * @param SDMMCx: Pointer to SDMMC register base
226 * @param Init: SDMMC initialization structure
227 * @retval HAL status
229 HAL_StatusTypeDef SDMMC_Init(SDMMC_TypeDef *SDMMCx, SDMMC_InitTypeDef Init)
231 uint32_t tmpreg = 0;
233 /* Check the parameters */
234 assert_param(IS_SDMMC_ALL_INSTANCE(SDMMCx));
235 assert_param(IS_SDMMC_CLOCK_EDGE(Init.ClockEdge));
236 assert_param(IS_SDMMC_CLOCK_BYPASS(Init.ClockBypass));
237 assert_param(IS_SDMMC_CLOCK_POWER_SAVE(Init.ClockPowerSave));
238 assert_param(IS_SDMMC_BUS_WIDE(Init.BusWide));
239 assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(Init.HardwareFlowControl));
240 assert_param(IS_SDMMC_CLKDIV(Init.ClockDiv));
242 /* Set SDMMC configuration parameters */
243 tmpreg |= (Init.ClockEdge |\
244 Init.ClockBypass |\
245 Init.ClockPowerSave |\
246 Init.BusWide |\
247 Init.HardwareFlowControl |\
248 Init.ClockDiv
251 /* Write to SDMMC CLKCR */
252 MODIFY_REG(SDMMCx->CLKCR, CLKCR_CLEAR_MASK, tmpreg);
254 return HAL_OK;
259 * @}
262 /** @defgroup HAL_SDMMC_LL_Group2 IO operation functions
263 * @brief Data transfers functions
265 @verbatim
266 ===============================================================================
267 ##### I/O operation functions #####
268 ===============================================================================
269 [..]
270 This subsection provides a set of functions allowing to manage the SDMMC data
271 transfers.
273 @endverbatim
274 * @{
278 * @brief Read data (word) from Rx FIFO in blocking mode (polling)
279 * @param SDMMCx: Pointer to SDMMC register base
280 * @retval HAL status
282 uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
284 /* Read data from Rx FIFO */
285 return (SDMMCx->FIFO);
289 * @brief Write data (word) to Tx FIFO in blocking mode (polling)
290 * @param SDMMCx: Pointer to SDMMC register base
291 * @param pWriteData: pointer to data to write
292 * @retval HAL status
294 HAL_StatusTypeDef SDMMC_WriteFIFO(SDMMC_TypeDef *SDMMCx, uint32_t *pWriteData)
296 /* Write data to FIFO */
297 SDMMCx->FIFO = *pWriteData;
299 return HAL_OK;
303 * @}
306 /** @defgroup HAL_SDMMC_LL_Group3 Peripheral Control functions
307 * @brief management functions
309 @verbatim
310 ===============================================================================
311 ##### Peripheral Control functions #####
312 ===============================================================================
313 [..]
314 This subsection provides a set of functions allowing to control the SDMMC data
315 transfers.
317 @endverbatim
318 * @{
322 * @brief Set SDMMC Power state to ON.
323 * @param SDMMCx: Pointer to SDMMC register base
324 * @retval HAL status
326 HAL_StatusTypeDef SDMMC_PowerState_ON(SDMMC_TypeDef *SDMMCx)
328 /* Set power state to ON */
329 SDMMCx->POWER = SDMMC_POWER_PWRCTRL;
331 return HAL_OK;
335 * @brief Set SDMMC Power state to OFF.
336 * @param SDMMCx: Pointer to SDMMC register base
337 * @retval HAL status
339 HAL_StatusTypeDef SDMMC_PowerState_OFF(SDMMC_TypeDef *SDMMCx)
341 /* Set power state to OFF */
342 SDMMCx->POWER = (uint32_t)0x00000000;
344 return HAL_OK;
348 * @brief Get SDMMC Power state.
349 * @param SDMMCx: Pointer to SDMMC register base
350 * @retval Power status of the controller. The returned value can be one of the
351 * following values:
352 * - 0x00: Power OFF
353 * - 0x02: Power UP
354 * - 0x03: Power ON
356 uint32_t SDMMC_GetPowerState(SDMMC_TypeDef *SDMMCx)
358 return (SDMMCx->POWER & SDMMC_POWER_PWRCTRL);
362 * @brief Configure the SDMMC command path according to the specified parameters in
363 * SDMMC_CmdInitTypeDef structure and send the command
364 * @param SDMMCx: Pointer to SDMMC register base
365 * @param Command: pointer to a SDMMC_CmdInitTypeDef structure that contains
366 * the configuration information for the SDMMC command
367 * @retval HAL status
369 HAL_StatusTypeDef SDMMC_SendCommand(SDMMC_TypeDef *SDMMCx, SDMMC_CmdInitTypeDef *Command)
371 uint32_t tmpreg = 0;
373 /* Check the parameters */
374 assert_param(IS_SDMMC_CMD_INDEX(Command->CmdIndex));
375 assert_param(IS_SDMMC_RESPONSE(Command->Response));
376 assert_param(IS_SDMMC_WAIT(Command->WaitForInterrupt));
377 assert_param(IS_SDMMC_CPSM(Command->CPSM));
379 /* Set the SDMMC Argument value */
380 SDMMCx->ARG = Command->Argument;
382 /* Set SDMMC command parameters */
383 tmpreg |= (uint32_t)(Command->CmdIndex |\
384 Command->Response |\
385 Command->WaitForInterrupt |\
386 Command->CPSM);
388 /* Write to SDMMC CMD register */
389 MODIFY_REG(SDMMCx->CMD, CMD_CLEAR_MASK, tmpreg);
391 return HAL_OK;
395 * @brief Return the command index of last command for which response received
396 * @param SDMMCx: Pointer to SDMMC register base
397 * @retval Command index of the last command response received
399 uint8_t SDMMC_GetCommandResponse(SDMMC_TypeDef *SDMMCx)
401 return (uint8_t)(SDMMCx->RESPCMD);
406 * @brief Return the response received from the card for the last command
407 * @param SDMMCx: Pointer to SDMMC register base
408 * @param Response: Specifies the SDMMC response register.
409 * This parameter can be one of the following values:
410 * @arg SDMMC_RESP1: Response Register 1
411 * @arg SDMMC_RESP2: Response Register 2
412 * @arg SDMMC_RESP3: Response Register 3
413 * @arg SDMMC_RESP4: Response Register 4
414 * @retval The Corresponding response register value
416 uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
418 __IO uint32_t tmp = 0;
420 /* Check the parameters */
421 assert_param(IS_SDMMC_RESP(Response));
423 /* Get the response */
424 tmp = (uint32_t)&(SDMMCx->RESP1) + Response;
426 return (*(__IO uint32_t *) tmp);
430 * @brief Configure the SDMMC data path according to the specified
431 * parameters in the SDMMC_DataInitTypeDef.
432 * @param SDMMCx: Pointer to SDMMC register base
433 * @param Data : pointer to a SDMMC_DataInitTypeDef structure
434 * that contains the configuration information for the SDMMC data.
435 * @retval HAL status
437 HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef* Data)
439 uint32_t tmpreg = 0;
441 /* Check the parameters */
442 assert_param(IS_SDMMC_DATA_LENGTH(Data->DataLength));
443 assert_param(IS_SDMMC_BLOCK_SIZE(Data->DataBlockSize));
444 assert_param(IS_SDMMC_TRANSFER_DIR(Data->TransferDir));
445 assert_param(IS_SDMMC_TRANSFER_MODE(Data->TransferMode));
446 assert_param(IS_SDMMC_DPSM(Data->DPSM));
448 /* Set the SDMMC Data TimeOut value */
449 SDMMCx->DTIMER = Data->DataTimeOut;
451 /* Set the SDMMC DataLength value */
452 SDMMCx->DLEN = Data->DataLength;
454 /* Set the SDMMC data configuration parameters */
455 tmpreg |= (uint32_t)(Data->DataBlockSize |\
456 Data->TransferDir |\
457 Data->TransferMode |\
458 Data->DPSM);
460 /* Write to SDMMC DCTRL */
461 MODIFY_REG(SDMMCx->DCTRL, DCTRL_CLEAR_MASK, tmpreg);
463 return HAL_OK;
468 * @brief Returns number of remaining data bytes to be transferred.
469 * @param SDMMCx: Pointer to SDMMC register base
470 * @retval Number of remaining data bytes to be transferred
472 uint32_t SDMMC_GetDataCounter(SDMMC_TypeDef *SDMMCx)
474 return (SDMMCx->DCOUNT);
478 * @brief Get the FIFO data
479 * @param SDMMCx: Pointer to SDMMC register base
480 * @retval Data received
482 uint32_t SDMMC_GetFIFOCount(SDMMC_TypeDef *SDMMCx)
484 return (SDMMCx->FIFO);
488 * @brief Sets one of the two options of inserting read wait interval.
489 * @param SDMMCx: Pointer to SDMMC register base
490 * @param SDMMC_ReadWaitMode: SDMMC Read Wait operation mode.
491 * This parameter can be:
492 * @arg SDMMC_READ_WAIT_MODE_CLK: Read Wait control by stopping SDMMCCLK
493 * @arg SDMMC_READ_WAIT_MODE_DATA2: Read Wait control using SDMMC_DATA2
494 * @retval None
496 HAL_StatusTypeDef SDMMC_SetSDMMCReadWaitMode(SDMMC_TypeDef *SDMMCx, uint32_t SDMMC_ReadWaitMode)
498 /* Check the parameters */
499 assert_param(IS_SDMMC_READWAIT_MODE(SDMMC_ReadWaitMode));
501 /* Set SDMMC read wait mode */
502 MODIFY_REG(SDMMCx->DCTRL, SDMMC_DCTRL_RWMOD, SDMMC_ReadWaitMode);
504 return HAL_OK;
508 * @}
512 /** @defgroup HAL_SDMMC_LL_Group4 Command management functions
513 * @brief Data transfers functions
515 @verbatim
516 ===============================================================================
517 ##### Commands management functions #####
518 ===============================================================================
519 [..]
520 This subsection provides a set of functions allowing to manage the needed commands.
522 @endverbatim
523 * @{
527 * @brief Send the Data Block Lenght command and check the response
528 * @param SDMMCx: Pointer to SDMMC register base
529 * @retval HAL status
531 uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
533 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
534 uint32_t errorstate = SDMMC_ERROR_NONE;
536 /* Set Block Size for Card */
537 sdmmc_cmdinit.Argument = (uint32_t)BlockSize;
538 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_BLOCKLEN;
539 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
540 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
541 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
542 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
544 /* Check for error conditions */
545 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SET_BLOCKLEN, SDMMC_CMDTIMEOUT);
547 return errorstate;
551 * @brief Send the Read Single Block command and check the response
552 * @param SDMMCx: Pointer to SDMMC register base
553 * @retval HAL status
555 uint32_t SDMMC_CmdReadSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
557 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
558 uint32_t errorstate = SDMMC_ERROR_NONE;
560 /* Set Block Size for Card */
561 sdmmc_cmdinit.Argument = (uint32_t)ReadAdd;
562 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_READ_SINGLE_BLOCK;
563 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
564 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
565 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
566 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
568 /* Check for error conditions */
569 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_READ_SINGLE_BLOCK, SDMMC_CMDTIMEOUT);
571 return errorstate;
575 * @brief Send the Read Multi Block command and check the response
576 * @param SDMMCx: Pointer to SDMMC register base
577 * @retval HAL status
579 uint32_t SDMMC_CmdReadMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
581 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
582 uint32_t errorstate = SDMMC_ERROR_NONE;
584 /* Set Block Size for Card */
585 sdmmc_cmdinit.Argument = (uint32_t)ReadAdd;
586 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_READ_MULT_BLOCK;
587 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
588 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
589 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
590 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
592 /* Check for error conditions */
593 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_READ_MULT_BLOCK, SDMMC_CMDTIMEOUT);
595 return errorstate;
599 * @brief Send the Write Single Block command and check the response
600 * @param SDMMCx: Pointer to SDMMC register base
601 * @retval HAL status
603 uint32_t SDMMC_CmdWriteSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
605 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
606 uint32_t errorstate = SDMMC_ERROR_NONE;
608 /* Set Block Size for Card */
609 sdmmc_cmdinit.Argument = (uint32_t)WriteAdd;
610 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_WRITE_SINGLE_BLOCK;
611 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
612 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
613 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
614 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
616 /* Check for error conditions */
617 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_WRITE_SINGLE_BLOCK, SDMMC_CMDTIMEOUT);
619 return errorstate;
623 * @brief Send the Write Multi Block command and check the response
624 * @param SDMMCx: Pointer to SDMMC register base
625 * @retval HAL status
627 uint32_t SDMMC_CmdWriteMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
629 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
630 uint32_t errorstate = SDMMC_ERROR_NONE;
632 /* Set Block Size for Card */
633 sdmmc_cmdinit.Argument = (uint32_t)WriteAdd;
634 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_WRITE_MULT_BLOCK;
635 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
636 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
637 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
638 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
640 /* Check for error conditions */
641 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_WRITE_MULT_BLOCK, SDMMC_CMDTIMEOUT);
643 return errorstate;
647 * @brief Send the Start Address Erase command for SD and check the response
648 * @param SDMMCx: Pointer to SDMMC register base
649 * @retval HAL status
651 uint32_t SDMMC_CmdSDEraseStartAdd(SDMMC_TypeDef *SDMMCx, uint32_t StartAdd)
653 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
654 uint32_t errorstate = SDMMC_ERROR_NONE;
656 /* Set Block Size for Card */
657 sdmmc_cmdinit.Argument = (uint32_t)StartAdd;
658 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_ERASE_GRP_START;
659 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
660 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
661 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
662 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
664 /* Check for error conditions */
665 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_ERASE_GRP_START, SDMMC_CMDTIMEOUT);
667 return errorstate;
671 * @brief Send the End Address Erase command for SD and check the response
672 * @param SDMMCx: Pointer to SDMMC register base
673 * @retval HAL status
675 uint32_t SDMMC_CmdSDEraseEndAdd(SDMMC_TypeDef *SDMMCx, uint32_t EndAdd)
677 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
678 uint32_t errorstate = SDMMC_ERROR_NONE;
680 /* Set Block Size for Card */
681 sdmmc_cmdinit.Argument = (uint32_t)EndAdd;
682 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_ERASE_GRP_END;
683 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
684 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
685 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
686 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
688 /* Check for error conditions */
689 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_ERASE_GRP_END, SDMMC_CMDTIMEOUT);
691 return errorstate;
695 * @brief Send the Start Address Erase command and check the response
696 * @param SDMMCx: Pointer to SDMMC register base
697 * @retval HAL status
699 uint32_t SDMMC_CmdEraseStartAdd(SDMMC_TypeDef *SDMMCx, uint32_t StartAdd)
701 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
702 uint32_t errorstate = SDMMC_ERROR_NONE;
704 /* Set Block Size for Card */
705 sdmmc_cmdinit.Argument = (uint32_t)StartAdd;
706 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE_GRP_START;
707 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
708 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
709 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
710 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
712 /* Check for error conditions */
713 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE_GRP_START, SDMMC_CMDTIMEOUT);
715 return errorstate;
719 * @brief Send the End Address Erase command and check the response
720 * @param SDMMCx: Pointer to SDMMC register base
721 * @retval HAL status
723 uint32_t SDMMC_CmdEraseEndAdd(SDMMC_TypeDef *SDMMCx, uint32_t EndAdd)
725 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
726 uint32_t errorstate = SDMMC_ERROR_NONE;
728 /* Set Block Size for Card */
729 sdmmc_cmdinit.Argument = (uint32_t)EndAdd;
730 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE_GRP_END;
731 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
732 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
733 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
734 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
736 /* Check for error conditions */
737 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE_GRP_END, SDMMC_CMDTIMEOUT);
739 return errorstate;
743 * @brief Send the Erase command and check the response
744 * @param SDMMCx: Pointer to SDMMC register base
745 * @retval HAL status
747 uint32_t SDMMC_CmdErase(SDMMC_TypeDef *SDMMCx)
749 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
750 uint32_t errorstate = SDMMC_ERROR_NONE;
752 /* Set Block Size for Card */
753 sdmmc_cmdinit.Argument = 0;
754 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE;
755 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
756 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
757 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
758 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
760 /* Check for error conditions */
761 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE, SDMMC_MAXERASETIMEOUT);
763 return errorstate;
767 * @brief Send the Stop Transfer command and check the response.
768 * @param SDMMCx: Pointer to SDMMC register base
769 * @retval HAL status
771 uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
773 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
774 uint32_t errorstate = SDMMC_ERROR_NONE;
776 /* Send CMD12 STOP_TRANSMISSION */
777 sdmmc_cmdinit.Argument = 0;
778 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_STOP_TRANSMISSION;
779 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
780 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
781 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
782 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
784 /* Check for error conditions */
785 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_STOP_TRANSMISSION, 100000000/*SDMMC_CMDTIMEOUT*/);
787 return errorstate;
791 * @brief Send the Select Deselect command and check the response.
792 * @param SDMMCx: Pointer to SDMMC register base
793 * @param addr: Address of the card to be selected
794 * @retval HAL status
796 uint32_t SDMMC_CmdSelDesel(SDMMC_TypeDef *SDMMCx, uint64_t Addr)
798 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
799 uint32_t errorstate = SDMMC_ERROR_NONE;
801 /* Send CMD7 SDMMC_SEL_DESEL_CARD */
802 sdmmc_cmdinit.Argument = (uint32_t)Addr;
803 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEL_DESEL_CARD;
804 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
805 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
806 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
807 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
809 /* Check for error conditions */
810 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SEL_DESEL_CARD, SDMMC_CMDTIMEOUT);
812 return errorstate;
816 * @brief Send the Go Idle State command and check the response.
817 * @param SDMMCx: Pointer to SDMMC register base
818 * @retval HAL status
820 uint32_t SDMMC_CmdGoIdleState(SDMMC_TypeDef *SDMMCx)
822 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
823 uint32_t errorstate = SDMMC_ERROR_NONE;
825 sdmmc_cmdinit.Argument = 0;
826 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_GO_IDLE_STATE;
827 sdmmc_cmdinit.Response = SDMMC_RESPONSE_NO;
828 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
829 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
830 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
832 /* Check for error conditions */
833 errorstate = SDMMC_GetCmdError(SDMMCx);
835 return errorstate;
839 * @brief Send the Operating Condition command and check the response.
840 * @param SDMMCx: Pointer to SDMMC register base
841 * @retval HAL status
843 uint32_t SDMMC_CmdOperCond(SDMMC_TypeDef *SDMMCx)
845 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
846 uint32_t errorstate = SDMMC_ERROR_NONE;
848 /* Send CMD8 to verify SD card interface operating condition */
849 /* Argument: - [31:12]: Reserved (shall be set to '0')
850 - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
851 - [7:0]: Check Pattern (recommended 0xAA) */
852 /* CMD Response: R7 */
853 sdmmc_cmdinit.Argument = SDMMC_CHECK_PATTERN;
854 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SEND_EXT_CSD;
855 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
856 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
857 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
858 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
860 /* Check for error conditions */
861 errorstate = SDMMC_GetCmdResp7(SDMMCx);
863 return errorstate;
867 * @brief Send the Application command to verify that that the next command
868 * is an application specific com-mand rather than a standard command
869 * and check the response.
870 * @param SDMMCx: Pointer to SDMMC register base
871 * @retval HAL status
873 uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
875 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
876 uint32_t errorstate = SDMMC_ERROR_NONE;
878 sdmmc_cmdinit.Argument = (uint32_t)Argument;
879 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_APP_CMD;
880 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
881 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
882 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
883 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
885 /* Check for error conditions */
886 /* If there is a HAL_ERROR, it is a MMC card, else
887 it is a SD card: SD card 2.0 (voltage range mismatch)
888 or SD card 1.x */
889 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_APP_CMD, SDMMC_CMDTIMEOUT);
891 return errorstate;
895 * @brief Send the command asking the accessed card to send its operating
896 * condition register (OCR)
897 * @param SDMMCx: Pointer to SDMMC register base
898 * @retval HAL status
900 uint32_t SDMMC_CmdAppOperCommand(SDMMC_TypeDef *SDMMCx, uint32_t SdType)
902 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
903 uint32_t errorstate = SDMMC_ERROR_NONE;
905 sdmmc_cmdinit.Argument = SDMMC_VOLTAGE_WINDOW_SD | SdType;
906 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_OP_COND;
907 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
908 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
909 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
910 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
912 /* Check for error conditions */
913 errorstate = SDMMC_GetCmdResp3(SDMMCx);
915 return errorstate;
919 * @brief Send the Bus Width command and check the response.
920 * @param SDMMCx: Pointer to SDMMC register base
921 * @retval HAL status
923 uint32_t SDMMC_CmdBusWidth(SDMMC_TypeDef *SDMMCx, uint32_t BusWidth)
925 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
926 uint32_t errorstate = SDMMC_ERROR_NONE;
928 sdmmc_cmdinit.Argument = (uint32_t)BusWidth;
929 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_APP_SD_SET_BUSWIDTH;
930 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
931 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
932 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
933 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
935 /* Check for error conditions */
936 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_APP_SD_SET_BUSWIDTH, SDMMC_CMDTIMEOUT);
938 return errorstate;
942 * @brief Send the Send SCR command and check the response.
943 * @param SDMMCx: Pointer to SDMMC register base
944 * @retval HAL status
946 uint32_t SDMMC_CmdSendSCR(SDMMC_TypeDef *SDMMCx)
948 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
949 uint32_t errorstate = SDMMC_ERROR_NONE;
951 /* Send CMD51 SD_APP_SEND_SCR */
952 sdmmc_cmdinit.Argument = 0;
953 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_SEND_SCR;
954 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
955 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
956 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
957 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
959 /* Check for error conditions */
960 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_APP_SEND_SCR, SDMMC_CMDTIMEOUT);
962 return errorstate;
966 * @brief Send the Send CID command and check the response.
967 * @param SDMMCx: Pointer to SDMMC register base
968 * @retval HAL status
970 uint32_t SDMMC_CmdSendCID(SDMMC_TypeDef *SDMMCx)
972 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
973 uint32_t errorstate = SDMMC_ERROR_NONE;
975 /* Send CMD2 ALL_SEND_CID */
976 sdmmc_cmdinit.Argument = 0;
977 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ALL_SEND_CID;
978 sdmmc_cmdinit.Response = SDMMC_RESPONSE_LONG;
979 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
980 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
981 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
983 /* Check for error conditions */
984 errorstate = SDMMC_GetCmdResp2(SDMMCx);
986 return errorstate;
990 * @brief Send the Send CSD command and check the response.
991 * @param SDMMCx: Pointer to SDMMC register base
992 * @retval HAL status
994 uint32_t SDMMC_CmdSendCSD(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
996 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
997 uint32_t errorstate = SDMMC_ERROR_NONE;
999 /* Send CMD9 SEND_CSD */
1000 sdmmc_cmdinit.Argument = (uint32_t)Argument;
1001 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_CSD;
1002 sdmmc_cmdinit.Response = SDMMC_RESPONSE_LONG;
1003 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1004 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1005 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1007 /* Check for error conditions */
1008 errorstate = SDMMC_GetCmdResp2(SDMMCx);
1010 return errorstate;
1014 * @brief Send the Send CSD command and check the response.
1015 * @param SDMMCx: Pointer to SDMMC register base
1016 * @retval HAL status
1018 uint32_t SDMMC_CmdSetRelAdd(SDMMC_TypeDef *SDMMCx, uint16_t *pRCA)
1020 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1021 uint32_t errorstate = SDMMC_ERROR_NONE;
1023 /* Send CMD3 SD_CMD_SET_REL_ADDR */
1024 sdmmc_cmdinit.Argument = 0;
1025 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_REL_ADDR;
1026 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1027 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1028 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1029 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1031 /* Check for error conditions */
1032 errorstate = SDMMC_GetCmdResp6(SDMMCx, SDMMC_CMD_SET_REL_ADDR, pRCA);
1034 return errorstate;
1038 * @brief Send the Status command and check the response.
1039 * @param SDMMCx: Pointer to SDMMC register base
1040 * @retval HAL status
1042 uint32_t SDMMC_CmdSendStatus(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1044 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1045 uint32_t errorstate = SDMMC_ERROR_NONE;
1047 sdmmc_cmdinit.Argument = (uint32_t)Argument;
1048 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_STATUS;
1049 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1050 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1051 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1052 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1054 /* Check for error conditions */
1055 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SEND_STATUS, SDMMC_CMDTIMEOUT);
1057 return errorstate;
1061 * @brief Send the Status register command and check the response.
1062 * @param SDMMCx: Pointer to SDMMC register base
1063 * @retval HAL status
1065 uint32_t SDMMC_CmdStatusRegister(SDMMC_TypeDef *SDMMCx)
1067 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1068 uint32_t errorstate = SDMMC_ERROR_NONE;
1070 sdmmc_cmdinit.Argument = 0;
1071 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_STATUS;
1072 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1073 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1074 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1075 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1077 /* Check for error conditions */
1078 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_APP_STATUS, SDMMC_CMDTIMEOUT);
1080 return errorstate;
1084 * @brief Sends host capacity support information and activates the card's
1085 * initialization process. Send SDMMC_CMD_SEND_OP_COND command
1086 * @param SDIOx: Pointer to SDIO register base
1087 * @parame Argument: Argument used for the command
1088 * @retval HAL status
1090 uint32_t SDMMC_CmdOpCondition(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1092 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1093 uint32_t errorstate = SDMMC_ERROR_NONE;
1095 sdmmc_cmdinit.Argument = Argument;
1096 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_OP_COND;
1097 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1098 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1099 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1100 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1102 /* Check for error conditions */
1103 errorstate = SDMMC_GetCmdResp3(SDMMCx);
1105 return errorstate;
1109 * @brief Checks switchable function and switch card function. SDMMC_CMD_HS_SWITCH comand
1110 * @param SDIOx: Pointer to SDIO register base
1111 * @parame Argument: Argument used for the command
1112 * @retval HAL status
1114 uint32_t SDMMC_CmdSwitch(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
1116 SDMMC_CmdInitTypeDef sdmmc_cmdinit;
1117 uint32_t errorstate = SDMMC_ERROR_NONE;
1119 sdmmc_cmdinit.Argument = Argument;
1120 sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SWITCH;
1121 sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
1122 sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
1123 sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
1124 SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
1126 /* Check for error conditions */
1127 errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_HS_SWITCH, SDMMC_CMDTIMEOUT);
1129 return errorstate;
1133 * @}
1136 /* Private function ----------------------------------------------------------*/
1137 /** @addtogroup SD_Private_Functions
1138 * @{
1142 * @brief Checks for error conditions for CMD0.
1143 * @param hsd: SD handle
1144 * @retval SD Card error state
1146 static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef *SDMMCx)
1148 /* 8 is the number of required instructions cycles for the below loop statement.
1149 The SDMMC_CMDTIMEOUT is expressed in ms */
1150 register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
1154 if (count-- == 0)
1156 return SDMMC_ERROR_TIMEOUT;
1159 }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDSENT));
1161 /* Clear all the static flags */
1162 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
1164 return SDMMC_ERROR_NONE;
1168 * @brief Checks for error conditions for R1 response.
1169 * @param hsd: SD handle
1170 * @param SD_CMD: The sent command index
1171 * @retval SD Card error state
1173 static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint32_t Timeout)
1175 uint32_t response_r1;
1177 /* 8 is the number of required instructions cycles for the below loop statement.
1178 The Timeout is expressed in ms */
1179 register uint32_t count = Timeout * (SystemCoreClock / 8 /1000);
1183 if (count-- == 0)
1185 return SDMMC_ERROR_TIMEOUT;
1188 }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
1190 if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1192 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1194 return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1196 else if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1198 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1200 return SDMMC_ERROR_CMD_CRC_FAIL;
1203 /* Check response received is of desired command */
1204 if(SDMMC_GetCommandResponse(SDMMCx) != SD_CMD)
1206 return SDMMC_ERROR_CMD_CRC_FAIL;
1209 /* Clear all the static flags */
1210 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
1212 /* We have received response, retrieve it for analysis */
1213 response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1);
1215 if((response_r1 & SDMMC_OCR_ERRORBITS) == SDMMC_ALLZERO)
1217 return SDMMC_ERROR_NONE;
1219 else if((response_r1 & SDMMC_OCR_ADDR_OUT_OF_RANGE) == SDMMC_OCR_ADDR_OUT_OF_RANGE)
1221 return SDMMC_ERROR_ADDR_OUT_OF_RANGE;
1223 else if((response_r1 & SDMMC_OCR_ADDR_MISALIGNED) == SDMMC_OCR_ADDR_MISALIGNED)
1225 return SDMMC_ERROR_ADDR_MISALIGNED;
1227 else if((response_r1 & SDMMC_OCR_BLOCK_LEN_ERR) == SDMMC_OCR_BLOCK_LEN_ERR)
1229 return SDMMC_ERROR_BLOCK_LEN_ERR;
1231 else if((response_r1 & SDMMC_OCR_ERASE_SEQ_ERR) == SDMMC_OCR_ERASE_SEQ_ERR)
1233 return SDMMC_ERROR_ERASE_SEQ_ERR;
1235 else if((response_r1 & SDMMC_OCR_BAD_ERASE_PARAM) == SDMMC_OCR_BAD_ERASE_PARAM)
1237 return SDMMC_ERROR_BAD_ERASE_PARAM;
1239 else if((response_r1 & SDMMC_OCR_WRITE_PROT_VIOLATION) == SDMMC_OCR_WRITE_PROT_VIOLATION)
1241 return SDMMC_ERROR_WRITE_PROT_VIOLATION;
1243 else if((response_r1 & SDMMC_OCR_LOCK_UNLOCK_FAILED) == SDMMC_OCR_LOCK_UNLOCK_FAILED)
1245 return SDMMC_ERROR_LOCK_UNLOCK_FAILED;
1247 else if((response_r1 & SDMMC_OCR_COM_CRC_FAILED) == SDMMC_OCR_COM_CRC_FAILED)
1249 return SDMMC_ERROR_COM_CRC_FAILED;
1251 else if((response_r1 & SDMMC_OCR_ILLEGAL_CMD) == SDMMC_OCR_ILLEGAL_CMD)
1253 return SDMMC_ERROR_ILLEGAL_CMD;
1255 else if((response_r1 & SDMMC_OCR_CARD_ECC_FAILED) == SDMMC_OCR_CARD_ECC_FAILED)
1257 return SDMMC_ERROR_CARD_ECC_FAILED;
1259 else if((response_r1 & SDMMC_OCR_CC_ERROR) == SDMMC_OCR_CC_ERROR)
1261 return SDMMC_ERROR_CC_ERR;
1263 else if((response_r1 & SDMMC_OCR_STREAM_READ_UNDERRUN) == SDMMC_OCR_STREAM_READ_UNDERRUN)
1265 return SDMMC_ERROR_STREAM_READ_UNDERRUN;
1267 else if((response_r1 & SDMMC_OCR_STREAM_WRITE_OVERRUN) == SDMMC_OCR_STREAM_WRITE_OVERRUN)
1269 return SDMMC_ERROR_STREAM_WRITE_OVERRUN;
1271 else if((response_r1 & SDMMC_OCR_CID_CSD_OVERWRITE) == SDMMC_OCR_CID_CSD_OVERWRITE)
1273 return SDMMC_ERROR_CID_CSD_OVERWRITE;
1275 else if((response_r1 & SDMMC_OCR_WP_ERASE_SKIP) == SDMMC_OCR_WP_ERASE_SKIP)
1277 return SDMMC_ERROR_WP_ERASE_SKIP;
1279 else if((response_r1 & SDMMC_OCR_CARD_ECC_DISABLED) == SDMMC_OCR_CARD_ECC_DISABLED)
1281 return SDMMC_ERROR_CARD_ECC_DISABLED;
1283 else if((response_r1 & SDMMC_OCR_ERASE_RESET) == SDMMC_OCR_ERASE_RESET)
1285 return SDMMC_ERROR_ERASE_RESET;
1287 else if((response_r1 & SDMMC_OCR_AKE_SEQ_ERROR) == SDMMC_OCR_AKE_SEQ_ERROR)
1289 return SDMMC_ERROR_AKE_SEQ_ERR;
1291 else
1293 return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
1298 * @brief Checks for error conditions for R2 (CID or CSD) response.
1299 * @param hsd: SD handle
1300 * @retval SD Card error state
1302 static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef *SDMMCx)
1304 /* 8 is the number of required instructions cycles for the below loop statement.
1305 The SDMMC_CMDTIMEOUT is expressed in ms */
1306 register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
1310 if (count-- == 0)
1312 return SDMMC_ERROR_TIMEOUT;
1315 }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
1317 if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1319 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1321 return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1323 else if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1325 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1327 return SDMMC_ERROR_CMD_CRC_FAIL;
1329 else
1331 /* No error flag set */
1332 /* Clear all the static flags */
1333 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
1336 return SDMMC_ERROR_NONE;
1340 * @brief Checks for error conditions for R3 (OCR) response.
1341 * @param hsd: SD handle
1342 * @retval SD Card error state
1344 static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef *SDMMCx)
1346 /* 8 is the number of required instructions cycles for the below loop statement.
1347 The SDMMC_CMDTIMEOUT is expressed in ms */
1348 register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
1352 if (count-- == 0)
1354 return SDMMC_ERROR_TIMEOUT;
1357 }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
1359 if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1361 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1363 return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1365 else
1368 /* Clear all the static flags */
1369 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
1372 return SDMMC_ERROR_NONE;
1376 * @brief Checks for error conditions for R6 (RCA) response.
1377 * @param hsd: SD handle
1378 * @param SD_CMD: The sent command index
1379 * @param pRCA: Pointer to the variable that will contain the SD card relative
1380 * address RCA
1381 * @retval SD Card error state
1383 static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint16_t *pRCA)
1385 uint32_t response_r1;
1387 /* 8 is the number of required instructions cycles for the below loop statement.
1388 The SDMMC_CMDTIMEOUT is expressed in ms */
1389 register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
1393 if (count-- == 0)
1395 return SDMMC_ERROR_TIMEOUT;
1398 }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
1400 if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1402 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
1404 return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1406 else if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
1408 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
1410 return SDMMC_ERROR_CMD_CRC_FAIL;
1413 /* Check response received is of desired command */
1414 if(SDMMC_GetCommandResponse(SDMMCx) != SD_CMD)
1416 return SDMMC_ERROR_CMD_CRC_FAIL;
1419 /* Clear all the static flags */
1420 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
1422 /* We have received response, retrieve it. */
1423 response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1);
1425 if((response_r1 & (SDMMC_R6_GENERAL_UNKNOWN_ERROR | SDMMC_R6_ILLEGAL_CMD | SDMMC_R6_COM_CRC_FAILED)) == SDMMC_ALLZERO)
1427 *pRCA = (uint16_t) (response_r1 >> 16);
1429 return SDMMC_ERROR_NONE;
1431 else if((response_r1 & SDMMC_R6_ILLEGAL_CMD) == SDMMC_R6_ILLEGAL_CMD)
1433 return SDMMC_ERROR_ILLEGAL_CMD;
1435 else if((response_r1 & SDMMC_R6_COM_CRC_FAILED) == SDMMC_R6_COM_CRC_FAILED)
1437 return SDMMC_ERROR_COM_CRC_FAILED;
1439 else
1441 return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
1446 * @brief Checks for error conditions for R7 response.
1447 * @param hsd: SD handle
1448 * @retval SD Card error state
1450 static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef *SDMMCx)
1452 /* 8 is the number of required instructions cycles for the below loop statement.
1453 The SDMMC_CMDTIMEOUT is expressed in ms */
1454 register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
1458 if (count-- == 0)
1460 return SDMMC_ERROR_TIMEOUT;
1463 }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
1465 if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
1467 /* Card is SD V2.0 compliant */
1468 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CMDREND);
1470 return SDMMC_ERROR_CMD_RSP_TIMEOUT;
1473 if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDREND))
1475 /* Card is SD V2.0 compliant */
1476 __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CMDREND);
1479 return SDMMC_ERROR_NONE;
1484 * @}
1488 * @}
1491 #endif /* (HAL_SD_MODULE_ENABLED) */
1493 * @}
1497 * @}
1500 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/