stm32f7xx_ll_sdmmc.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_ll_sdmmc.c
  4. * @author MCD Application Team
  5. * @brief SDMMC Low Layer HAL module driver.
  6. *
  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
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### SDMMC peripheral features #####
  17. ==============================================================================
  18. [..] The SD/SDMMC MMC card host interface (SDMMC) provides an interface between the APB2
  19. peripheral bus and MultiMedia cards (MMCs), SD memory cards, SDMMC cards and CE-ATA
  20. devices.
  21. [..] The SDMMC features include the following:
  22. (+) Full compliance with MultiMedia Card System Specification Version 4.2. Card support
  23. for three different databus modes: 1-bit (default), 4-bit and 8-bit
  24. (+) Full compatibility with previous versions of MultiMedia Cards (forward compatibility)
  25. (+) Full compliance with SD Memory Card Specifications Version 2.0
  26. (+) Full compliance with SD I/O Card Specification Version 2.0: card support for two
  27. different data bus modes: 1-bit (default) and 4-bit
  28. (+) Full support of the CE-ATA features (full compliance with CE-ATA digital protocol
  29. Rev1.1)
  30. (+) Data transfer up to 48 MHz for the 8 bit mode
  31. (+) Data and command output enable signals to control external bidirectional drivers.
  32. ##### How to use this driver #####
  33. ==============================================================================
  34. [..]
  35. This driver is a considered as a driver of service for external devices drivers
  36. that interfaces with the SDMMC peripheral.
  37. According to the device used (SD card/ MMC card / SDMMC card ...), a set of APIs
  38. is used in the device's driver to perform SDMMC operations and functionalities.
  39. This driver is almost transparent for the final user, it is only used to implement other
  40. functionalities of the external device.
  41. [..]
  42. (+) The SDMMC clock (SDMMCCLK = 48 MHz) is coming from a specific output of PLL
  43. (PLL48CLK). Before start working with SDMMC peripheral make sure that the
  44. PLL is well configured.
  45. The SDMMC peripheral uses two clock signals:
  46. (++) SDMMC adapter clock (SDMMCCLK = 48 MHz)
  47. (++) APB2 bus clock (PCLK2)
  48. -@@- PCLK2 and SDMMC_CK clock frequencies must respect the following condition:
  49. Frequency(PCLK2) >= (3 / 8 x Frequency(SDMMC_CK))
  50. (+) Enable/Disable peripheral clock using RCC peripheral macros related to SDMMC
  51. peripheral.
  52. (+) Enable the Power ON State using the SDMMC_PowerState_ON(SDMMCx)
  53. function and disable it using the function SDMMC_PowerState_OFF(SDMMCx).
  54. (+) Enable/Disable the clock using the __SDMMC_ENABLE()/__SDMMC_DISABLE() macros.
  55. (+) Enable/Disable the peripheral interrupts using the macros __SDMMC_ENABLE_IT(hSDMMC, IT)
  56. and __SDMMC_DISABLE_IT(hSDMMC, IT) if you need to use interrupt mode.
  57. (+) When using the DMA mode
  58. (++) Configure the DMA in the MSP layer of the external device
  59. (++) Active the needed channel Request
  60. (++) Enable the DMA using __SDMMC_DMA_ENABLE() macro or Disable it using the macro
  61. __SDMMC_DMA_DISABLE().
  62. (+) To control the CPSM (Command Path State Machine) and send
  63. commands to the card use the SDMMC_SendCommand(SDMMCx),
  64. SDMMC_GetCommandResponse() and SDMMC_GetResponse() functions. First, user has
  65. to fill the command structure (pointer to SDMMC_CmdInitTypeDef) according
  66. to the selected command to be sent.
  67. The parameters that should be filled are:
  68. (++) Command Argument
  69. (++) Command Index
  70. (++) Command Response type
  71. (++) Command Wait
  72. (++) CPSM Status (Enable or Disable).
  73. -@@- To check if the command is well received, read the SDMMC_CMDRESP
  74. register using the SDMMC_GetCommandResponse().
  75. The SDMMC responses registers (SDMMC_RESP1 to SDMMC_RESP2), use the
  76. SDMMC_GetResponse() function.
  77. (+) To control the DPSM (Data Path State Machine) and send/receive
  78. data to/from the card use the SDMMC_DataConfig(), SDMMC_GetDataCounter(),
  79. SDMMC_ReadFIFO(), SDMMC_WriteFIFO() and SDMMC_GetFIFOCount() functions.
  80. *** Read Operations ***
  81. =======================
  82. [..]
  83. (#) First, user has to fill the data structure (pointer to
  84. SDMMC_DataInitTypeDef) according to the selected data type to be received.
  85. The parameters that should be filled are:
  86. (++) Data TimeOut
  87. (++) Data Length
  88. (++) Data Block size
  89. (++) Data Transfer direction: should be from card (To SDMMC)
  90. (++) Data Transfer mode
  91. (++) DPSM Status (Enable or Disable)
  92. (#) Configure the SDMMC resources to receive the data from the card
  93. according to selected transfer mode (Refer to Step 8, 9 and 10).
  94. (#) Send the selected Read command (refer to step 11).
  95. (#) Use the SDMMC flags/interrupts to check the transfer status.
  96. *** Write Operations ***
  97. ========================
  98. [..]
  99. (#) First, user has to fill the data structure (pointer to
  100. SDMMC_DataInitTypeDef) according to the selected data type to be received.
  101. The parameters that should be filled are:
  102. (++) Data TimeOut
  103. (++) Data Length
  104. (++) Data Block size
  105. (++) Data Transfer direction: should be to card (To CARD)
  106. (++) Data Transfer mode
  107. (++) DPSM Status (Enable or Disable)
  108. (#) Configure the SDMMC resources to send the data to the card according to
  109. selected transfer mode.
  110. (#) Send the selected Write command.
  111. (#) Use the SDMMC flags/interrupts to check the transfer status.
  112. *** Command management operations ***
  113. =====================================
  114. [..]
  115. (#) The commands used for Read/Write//Erase operations are managed in
  116. separate functions.
  117. Each function allows to send the needed command with the related argument,
  118. then check the response.
  119. By the same approach, you could implement a command and check the response.
  120. @endverbatim
  121. ******************************************************************************
  122. * @attention
  123. *
  124. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  125. *
  126. * Redistribution and use in source and binary forms, with or without modification,
  127. * are permitted provided that the following conditions are met:
  128. * 1. Redistributions of source code must retain the above copyright notice,
  129. * this list of conditions and the following disclaimer.
  130. * 2. Redistributions in binary form must reproduce the above copyright notice,
  131. * this list of conditions and the following disclaimer in the documentation
  132. * and/or other materials provided with the distribution.
  133. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  134. * may be used to endorse or promote products derived from this software
  135. * without specific prior written permission.
  136. *
  137. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  138. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  139. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  140. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  141. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  142. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  143. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  144. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  145. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  146. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  147. *
  148. ******************************************************************************
  149. */
  150. /* Includes ------------------------------------------------------------------*/
  151. #include "stm32f7xx_hal.h"
  152. /** @addtogroup STM32F7xx_HAL_Driver
  153. * @{
  154. */
  155. /** @defgroup SDMMC_LL SDMMC Low Layer
  156. * @brief Low layer module for SD
  157. * @{
  158. */
  159. #if defined (HAL_SD_MODULE_ENABLED) || defined(HAL_MMC_MODULE_ENABLED)
  160. /* Private typedef -----------------------------------------------------------*/
  161. /* Private define ------------------------------------------------------------*/
  162. /* Private macro -------------------------------------------------------------*/
  163. /* Private variables ---------------------------------------------------------*/
  164. /* Private function prototypes -----------------------------------------------*/
  165. static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef *SDMMCx);
  166. static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint32_t Timeout);
  167. static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef *SDMMCx);
  168. static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef *SDMMCx);
  169. static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef *SDMMCx);
  170. static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint16_t *pRCA);
  171. /* Exported functions --------------------------------------------------------*/
  172. /** @defgroup SDMMC_LL_Exported_Functions SDMMC Low Layer Exported Functions
  173. * @{
  174. */
  175. /** @defgroup HAL_SDMMC_LL_Group1 Initialization de-initialization functions
  176. * @brief Initialization and Configuration functions
  177. *
  178. @verbatim
  179. ===============================================================================
  180. ##### Initialization/de-initialization functions #####
  181. ===============================================================================
  182. [..] This section provides functions allowing to:
  183. @endverbatim
  184. * @{
  185. */
  186. /**
  187. * @brief Initializes the SDMMC according to the specified
  188. * parameters in the SDMMC_InitTypeDef and create the associated handle.
  189. * @param SDMMCx Pointer to SDMMC register base
  190. * @param Init SDMMC initialization structure
  191. * @retval HAL status
  192. */
  193. HAL_StatusTypeDef SDMMC_Init(SDMMC_TypeDef *SDMMCx, SDMMC_InitTypeDef Init)
  194. {
  195. uint32_t tmpreg = 0;
  196. /* Check the parameters */
  197. assert_param(IS_SDMMC_ALL_INSTANCE(SDMMCx));
  198. assert_param(IS_SDMMC_CLOCK_EDGE(Init.ClockEdge));
  199. assert_param(IS_SDMMC_CLOCK_BYPASS(Init.ClockBypass));
  200. assert_param(IS_SDMMC_CLOCK_POWER_SAVE(Init.ClockPowerSave));
  201. assert_param(IS_SDMMC_BUS_WIDE(Init.BusWide));
  202. assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(Init.HardwareFlowControl));
  203. assert_param(IS_SDMMC_CLKDIV(Init.ClockDiv));
  204. /* Set SDMMC configuration parameters */
  205. tmpreg |= (Init.ClockEdge |\
  206. Init.ClockBypass |\
  207. Init.ClockPowerSave |\
  208. Init.BusWide |\
  209. Init.HardwareFlowControl |\
  210. Init.ClockDiv
  211. );
  212. /* Write to SDMMC CLKCR */
  213. MODIFY_REG(SDMMCx->CLKCR, CLKCR_CLEAR_MASK, tmpreg);
  214. return HAL_OK;
  215. }
  216. /**
  217. * @}
  218. */
  219. /** @defgroup HAL_SDMMC_LL_Group2 IO operation functions
  220. * @brief Data transfers functions
  221. *
  222. @verbatim
  223. ===============================================================================
  224. ##### I/O operation functions #####
  225. ===============================================================================
  226. [..]
  227. This subsection provides a set of functions allowing to manage the SDMMC data
  228. transfers.
  229. @endverbatim
  230. * @{
  231. */
  232. /**
  233. * @brief Read data (word) from Rx FIFO in blocking mode (polling)
  234. * @param SDMMCx Pointer to SDMMC register base
  235. * @retval HAL status
  236. */
  237. uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
  238. {
  239. /* Read data from Rx FIFO */
  240. return (SDMMCx->FIFO);
  241. }
  242. /**
  243. * @brief Write data (word) to Tx FIFO in blocking mode (polling)
  244. * @param SDMMCx Pointer to SDMMC register base
  245. * @param pWriteData pointer to data to write
  246. * @retval HAL status
  247. */
  248. HAL_StatusTypeDef SDMMC_WriteFIFO(SDMMC_TypeDef *SDMMCx, uint32_t *pWriteData)
  249. {
  250. /* Write data to FIFO */
  251. SDMMCx->FIFO = *pWriteData;
  252. return HAL_OK;
  253. }
  254. /**
  255. * @}
  256. */
  257. /** @defgroup HAL_SDMMC_LL_Group3 Peripheral Control functions
  258. * @brief management functions
  259. *
  260. @verbatim
  261. ===============================================================================
  262. ##### Peripheral Control functions #####
  263. ===============================================================================
  264. [..]
  265. This subsection provides a set of functions allowing to control the SDMMC data
  266. transfers.
  267. @endverbatim
  268. * @{
  269. */
  270. /**
  271. * @brief Set SDMMC Power state to ON.
  272. * @param SDMMCx Pointer to SDMMC register base
  273. * @retval HAL status
  274. */
  275. HAL_StatusTypeDef SDMMC_PowerState_ON(SDMMC_TypeDef *SDMMCx)
  276. {
  277. /* Set power state to ON */
  278. SDMMCx->POWER = SDMMC_POWER_PWRCTRL;
  279. return HAL_OK;
  280. }
  281. /**
  282. * @brief Set SDMMC Power state to OFF.
  283. * @param SDMMCx Pointer to SDMMC register base
  284. * @retval HAL status
  285. */
  286. HAL_StatusTypeDef SDMMC_PowerState_OFF(SDMMC_TypeDef *SDMMCx)
  287. {
  288. /* Set power state to OFF */
  289. SDMMCx->POWER = (uint32_t)0x00000000;
  290. return HAL_OK;
  291. }
  292. /**
  293. * @brief Get SDMMC Power state.
  294. * @param SDMMCx Pointer to SDMMC register base
  295. * @retval Power status of the controller. The returned value can be one of the
  296. * following values:
  297. * - 0x00: Power OFF
  298. * - 0x02: Power UP
  299. * - 0x03: Power ON
  300. */
  301. uint32_t SDMMC_GetPowerState(SDMMC_TypeDef *SDMMCx)
  302. {
  303. return (SDMMCx->POWER & SDMMC_POWER_PWRCTRL);
  304. }
  305. /**
  306. * @brief Configure the SDMMC command path according to the specified parameters in
  307. * SDMMC_CmdInitTypeDef structure and send the command
  308. * @param SDMMCx Pointer to SDMMC register base
  309. * @param Command pointer to a SDMMC_CmdInitTypeDef structure that contains
  310. * the configuration information for the SDMMC command
  311. * @retval HAL status
  312. */
  313. HAL_StatusTypeDef SDMMC_SendCommand(SDMMC_TypeDef *SDMMCx, SDMMC_CmdInitTypeDef *Command)
  314. {
  315. uint32_t tmpreg = 0;
  316. /* Check the parameters */
  317. assert_param(IS_SDMMC_CMD_INDEX(Command->CmdIndex));
  318. assert_param(IS_SDMMC_RESPONSE(Command->Response));
  319. assert_param(IS_SDMMC_WAIT(Command->WaitForInterrupt));
  320. assert_param(IS_SDMMC_CPSM(Command->CPSM));
  321. /* Set the SDMMC Argument value */
  322. SDMMCx->ARG = Command->Argument;
  323. /* Set SDMMC command parameters */
  324. tmpreg |= (uint32_t)(Command->CmdIndex |\
  325. Command->Response |\
  326. Command->WaitForInterrupt |\
  327. Command->CPSM);
  328. /* Write to SDMMC CMD register */
  329. MODIFY_REG(SDMMCx->CMD, CMD_CLEAR_MASK, tmpreg);
  330. return HAL_OK;
  331. }
  332. /**
  333. * @brief Return the command index of last command for which response received
  334. * @param SDMMCx Pointer to SDMMC register base
  335. * @retval Command index of the last command response received
  336. */
  337. uint8_t SDMMC_GetCommandResponse(SDMMC_TypeDef *SDMMCx)
  338. {
  339. return (uint8_t)(SDMMCx->RESPCMD);
  340. }
  341. /**
  342. * @brief Return the response received from the card for the last command
  343. * @param SDMMCx Pointer to SDMMC register base
  344. * @param Response Specifies the SDMMC response register.
  345. * This parameter can be one of the following values:
  346. * @arg SDMMC_RESP1: Response Register 1
  347. * @arg SDMMC_RESP2: Response Register 2
  348. * @arg SDMMC_RESP3: Response Register 3
  349. * @arg SDMMC_RESP4: Response Register 4
  350. * @retval The Corresponding response register value
  351. */
  352. uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
  353. {
  354. __IO uint32_t tmp = 0;
  355. /* Check the parameters */
  356. assert_param(IS_SDMMC_RESP(Response));
  357. /* Get the response */
  358. tmp = (uint32_t)&(SDMMCx->RESP1) + Response;
  359. return (*(__IO uint32_t *) tmp);
  360. }
  361. /**
  362. * @brief Configure the SDMMC data path according to the specified
  363. * parameters in the SDMMC_DataInitTypeDef.
  364. * @param SDMMCx Pointer to SDMMC register base
  365. * @param Data pointer to a SDMMC_DataInitTypeDef structure
  366. * that contains the configuration information for the SDMMC data.
  367. * @retval HAL status
  368. */
  369. HAL_StatusTypeDef SDMMC_ConfigData(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef* Data)
  370. {
  371. uint32_t tmpreg = 0;
  372. /* Check the parameters */
  373. assert_param(IS_SDMMC_DATA_LENGTH(Data->DataLength));
  374. assert_param(IS_SDMMC_BLOCK_SIZE(Data->DataBlockSize));
  375. assert_param(IS_SDMMC_TRANSFER_DIR(Data->TransferDir));
  376. assert_param(IS_SDMMC_TRANSFER_MODE(Data->TransferMode));
  377. assert_param(IS_SDMMC_DPSM(Data->DPSM));
  378. /* Set the SDMMC Data TimeOut value */
  379. SDMMCx->DTIMER = Data->DataTimeOut;
  380. /* Set the SDMMC DataLength value */
  381. SDMMCx->DLEN = Data->DataLength;
  382. /* Set the SDMMC data configuration parameters */
  383. tmpreg |= (uint32_t)(Data->DataBlockSize |\
  384. Data->TransferDir |\
  385. Data->TransferMode |\
  386. Data->DPSM);
  387. /* Write to SDMMC DCTRL */
  388. MODIFY_REG(SDMMCx->DCTRL, DCTRL_CLEAR_MASK, tmpreg);
  389. return HAL_OK;
  390. }
  391. /**
  392. * @brief Returns number of remaining data bytes to be transferred.
  393. * @param SDMMCx Pointer to SDMMC register base
  394. * @retval Number of remaining data bytes to be transferred
  395. */
  396. uint32_t SDMMC_GetDataCounter(SDMMC_TypeDef *SDMMCx)
  397. {
  398. return (SDMMCx->DCOUNT);
  399. }
  400. /**
  401. * @brief Get the FIFO data
  402. * @param SDMMCx Pointer to SDMMC register base
  403. * @retval Data received
  404. */
  405. uint32_t SDMMC_GetFIFOCount(SDMMC_TypeDef *SDMMCx)
  406. {
  407. return (SDMMCx->FIFO);
  408. }
  409. /**
  410. * @brief Sets one of the two options of inserting read wait interval.
  411. * @param SDMMCx Pointer to SDMMC register base
  412. * @param SDMMC_ReadWaitMode SDMMC Read Wait operation mode.
  413. * This parameter can be:
  414. * @arg SDMMC_READ_WAIT_MODE_CLK: Read Wait control by stopping SDMMCCLK
  415. * @arg SDMMC_READ_WAIT_MODE_DATA2: Read Wait control using SDMMC_DATA2
  416. * @retval None
  417. */
  418. HAL_StatusTypeDef SDMMC_SetSDMMCReadWaitMode(SDMMC_TypeDef *SDMMCx, uint32_t SDMMC_ReadWaitMode)
  419. {
  420. /* Check the parameters */
  421. assert_param(IS_SDMMC_READWAIT_MODE(SDMMC_ReadWaitMode));
  422. /* Set SDMMC read wait mode */
  423. MODIFY_REG(SDMMCx->DCTRL, SDMMC_DCTRL_RWMOD, SDMMC_ReadWaitMode);
  424. return HAL_OK;
  425. }
  426. /**
  427. * @}
  428. */
  429. /** @defgroup HAL_SDMMC_LL_Group4 Command management functions
  430. * @brief Data transfers functions
  431. *
  432. @verbatim
  433. ===============================================================================
  434. ##### Commands management functions #####
  435. ===============================================================================
  436. [..]
  437. This subsection provides a set of functions allowing to manage the needed commands.
  438. @endverbatim
  439. * @{
  440. */
  441. /**
  442. * @brief Send the Data Block Lenght command and check the response
  443. * @param SDMMCx Pointer to SDMMC register base
  444. * @retval HAL status
  445. */
  446. uint32_t SDMMC_CmdBlockLength(SDMMC_TypeDef *SDMMCx, uint32_t BlockSize)
  447. {
  448. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  449. uint32_t errorstate = SDMMC_ERROR_NONE;
  450. /* Set Block Size for Card */
  451. sdmmc_cmdinit.Argument = (uint32_t)BlockSize;
  452. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_BLOCKLEN;
  453. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  454. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  455. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  456. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  457. /* Check for error conditions */
  458. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SET_BLOCKLEN, SDMMC_CMDTIMEOUT);
  459. return errorstate;
  460. }
  461. /**
  462. * @brief Send the Read Single Block command and check the response
  463. * @param SDMMCx Pointer to SDMMC register base
  464. * @retval HAL status
  465. */
  466. uint32_t SDMMC_CmdReadSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
  467. {
  468. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  469. uint32_t errorstate = SDMMC_ERROR_NONE;
  470. /* Set Block Size for Card */
  471. sdmmc_cmdinit.Argument = (uint32_t)ReadAdd;
  472. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_READ_SINGLE_BLOCK;
  473. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  474. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  475. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  476. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  477. /* Check for error conditions */
  478. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_READ_SINGLE_BLOCK, SDMMC_CMDTIMEOUT);
  479. return errorstate;
  480. }
  481. /**
  482. * @brief Send the Read Multi Block command and check the response
  483. * @param SDMMCx Pointer to SDMMC register base
  484. * @retval HAL status
  485. */
  486. uint32_t SDMMC_CmdReadMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t ReadAdd)
  487. {
  488. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  489. uint32_t errorstate = SDMMC_ERROR_NONE;
  490. /* Set Block Size for Card */
  491. sdmmc_cmdinit.Argument = (uint32_t)ReadAdd;
  492. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_READ_MULT_BLOCK;
  493. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  494. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  495. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  496. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  497. /* Check for error conditions */
  498. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_READ_MULT_BLOCK, SDMMC_CMDTIMEOUT);
  499. return errorstate;
  500. }
  501. /**
  502. * @brief Send the Write Single Block command and check the response
  503. * @param SDMMCx Pointer to SDMMC register base
  504. * @retval HAL status
  505. */
  506. uint32_t SDMMC_CmdWriteSingleBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
  507. {
  508. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  509. uint32_t errorstate = SDMMC_ERROR_NONE;
  510. /* Set Block Size for Card */
  511. sdmmc_cmdinit.Argument = (uint32_t)WriteAdd;
  512. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_WRITE_SINGLE_BLOCK;
  513. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  514. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  515. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  516. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  517. /* Check for error conditions */
  518. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_WRITE_SINGLE_BLOCK, SDMMC_CMDTIMEOUT);
  519. return errorstate;
  520. }
  521. /**
  522. * @brief Send the Write Multi Block command and check the response
  523. * @param SDMMCx Pointer to SDMMC register base
  524. * @retval HAL status
  525. */
  526. uint32_t SDMMC_CmdWriteMultiBlock(SDMMC_TypeDef *SDMMCx, uint32_t WriteAdd)
  527. {
  528. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  529. uint32_t errorstate = SDMMC_ERROR_NONE;
  530. /* Set Block Size for Card */
  531. sdmmc_cmdinit.Argument = (uint32_t)WriteAdd;
  532. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_WRITE_MULT_BLOCK;
  533. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  534. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  535. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  536. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  537. /* Check for error conditions */
  538. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_WRITE_MULT_BLOCK, SDMMC_CMDTIMEOUT);
  539. return errorstate;
  540. }
  541. /**
  542. * @brief Send the Start Address Erase command for SD and check the response
  543. * @param SDMMCx Pointer to SDMMC register base
  544. * @retval HAL status
  545. */
  546. uint32_t SDMMC_CmdSDEraseStartAdd(SDMMC_TypeDef *SDMMCx, uint32_t StartAdd)
  547. {
  548. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  549. uint32_t errorstate = SDMMC_ERROR_NONE;
  550. /* Set Block Size for Card */
  551. sdmmc_cmdinit.Argument = (uint32_t)StartAdd;
  552. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_ERASE_GRP_START;
  553. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  554. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  555. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  556. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  557. /* Check for error conditions */
  558. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_ERASE_GRP_START, SDMMC_CMDTIMEOUT);
  559. return errorstate;
  560. }
  561. /**
  562. * @brief Send the End Address Erase command for SD and check the response
  563. * @param SDMMCx Pointer to SDMMC register base
  564. * @retval HAL status
  565. */
  566. uint32_t SDMMC_CmdSDEraseEndAdd(SDMMC_TypeDef *SDMMCx, uint32_t EndAdd)
  567. {
  568. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  569. uint32_t errorstate = SDMMC_ERROR_NONE;
  570. /* Set Block Size for Card */
  571. sdmmc_cmdinit.Argument = (uint32_t)EndAdd;
  572. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_ERASE_GRP_END;
  573. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  574. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  575. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  576. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  577. /* Check for error conditions */
  578. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_ERASE_GRP_END, SDMMC_CMDTIMEOUT);
  579. return errorstate;
  580. }
  581. /**
  582. * @brief Send the Start Address Erase command and check the response
  583. * @param SDMMCx Pointer to SDMMC register base
  584. * @retval HAL status
  585. */
  586. uint32_t SDMMC_CmdEraseStartAdd(SDMMC_TypeDef *SDMMCx, uint32_t StartAdd)
  587. {
  588. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  589. uint32_t errorstate = SDMMC_ERROR_NONE;
  590. /* Set Block Size for Card */
  591. sdmmc_cmdinit.Argument = (uint32_t)StartAdd;
  592. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE_GRP_START;
  593. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  594. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  595. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  596. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  597. /* Check for error conditions */
  598. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE_GRP_START, SDMMC_CMDTIMEOUT);
  599. return errorstate;
  600. }
  601. /**
  602. * @brief Send the End Address Erase command and check the response
  603. * @param SDMMCx Pointer to SDMMC register base
  604. * @retval HAL status
  605. */
  606. uint32_t SDMMC_CmdEraseEndAdd(SDMMC_TypeDef *SDMMCx, uint32_t EndAdd)
  607. {
  608. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  609. uint32_t errorstate = SDMMC_ERROR_NONE;
  610. /* Set Block Size for Card */
  611. sdmmc_cmdinit.Argument = (uint32_t)EndAdd;
  612. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE_GRP_END;
  613. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  614. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  615. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  616. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  617. /* Check for error conditions */
  618. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE_GRP_END, SDMMC_CMDTIMEOUT);
  619. return errorstate;
  620. }
  621. /**
  622. * @brief Send the Erase command and check the response
  623. * @param SDMMCx Pointer to SDMMC register base
  624. * @retval HAL status
  625. */
  626. uint32_t SDMMC_CmdErase(SDMMC_TypeDef *SDMMCx)
  627. {
  628. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  629. uint32_t errorstate = SDMMC_ERROR_NONE;
  630. /* Set Block Size for Card */
  631. sdmmc_cmdinit.Argument = 0;
  632. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE;
  633. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  634. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  635. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  636. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  637. /* Check for error conditions */
  638. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_ERASE, SDMMC_MAXERASETIMEOUT);
  639. return errorstate;
  640. }
  641. /**
  642. * @brief Send the Stop Transfer command and check the response.
  643. * @param SDMMCx Pointer to SDMMC register base
  644. * @retval HAL status
  645. */
  646. uint32_t SDMMC_CmdStopTransfer(SDMMC_TypeDef *SDMMCx)
  647. {
  648. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  649. uint32_t errorstate = SDMMC_ERROR_NONE;
  650. /* Send CMD12 STOP_TRANSMISSION */
  651. sdmmc_cmdinit.Argument = 0;
  652. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_STOP_TRANSMISSION;
  653. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  654. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  655. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  656. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  657. /* Check for error conditions */
  658. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_STOP_TRANSMISSION, 100000000/*SDMMC_CMDTIMEOUT*/);
  659. return errorstate;
  660. }
  661. /**
  662. * @brief Send the Select Deselect command and check the response.
  663. * @param SDMMCx Pointer to SDMMC register base
  664. * @param addr Address of the card to be selected
  665. * @retval HAL status
  666. */
  667. uint32_t SDMMC_CmdSelDesel(SDMMC_TypeDef *SDMMCx, uint64_t Addr)
  668. {
  669. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  670. uint32_t errorstate = SDMMC_ERROR_NONE;
  671. /* Send CMD7 SDMMC_SEL_DESEL_CARD */
  672. sdmmc_cmdinit.Argument = (uint32_t)Addr;
  673. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEL_DESEL_CARD;
  674. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  675. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  676. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  677. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  678. /* Check for error conditions */
  679. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SEL_DESEL_CARD, SDMMC_CMDTIMEOUT);
  680. return errorstate;
  681. }
  682. /**
  683. * @brief Send the Go Idle State command and check the response.
  684. * @param SDMMCx Pointer to SDMMC register base
  685. * @retval HAL status
  686. */
  687. uint32_t SDMMC_CmdGoIdleState(SDMMC_TypeDef *SDMMCx)
  688. {
  689. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  690. uint32_t errorstate = SDMMC_ERROR_NONE;
  691. sdmmc_cmdinit.Argument = 0;
  692. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_GO_IDLE_STATE;
  693. sdmmc_cmdinit.Response = SDMMC_RESPONSE_NO;
  694. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  695. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  696. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  697. /* Check for error conditions */
  698. errorstate = SDMMC_GetCmdError(SDMMCx);
  699. return errorstate;
  700. }
  701. /**
  702. * @brief Send the Operating Condition command and check the response.
  703. * @param SDMMCx Pointer to SDMMC register base
  704. * @retval HAL status
  705. */
  706. uint32_t SDMMC_CmdOperCond(SDMMC_TypeDef *SDMMCx)
  707. {
  708. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  709. uint32_t errorstate = SDMMC_ERROR_NONE;
  710. /* Send CMD8 to verify SD card interface operating condition */
  711. /* Argument: - [31:12]: Reserved (shall be set to '0')
  712. - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
  713. - [7:0]: Check Pattern (recommended 0xAA) */
  714. /* CMD Response: R7 */
  715. sdmmc_cmdinit.Argument = SDMMC_CHECK_PATTERN;
  716. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SEND_EXT_CSD;
  717. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  718. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  719. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  720. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  721. /* Check for error conditions */
  722. errorstate = SDMMC_GetCmdResp7(SDMMCx);
  723. return errorstate;
  724. }
  725. /**
  726. * @brief Send the Application command to verify that that the next command
  727. * is an application specific com-mand rather than a standard command
  728. * and check the response.
  729. * @param SDMMCx Pointer to SDMMC register base
  730. * @retval HAL status
  731. */
  732. uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  733. {
  734. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  735. uint32_t errorstate = SDMMC_ERROR_NONE;
  736. sdmmc_cmdinit.Argument = (uint32_t)Argument;
  737. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_APP_CMD;
  738. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  739. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  740. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  741. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  742. /* Check for error conditions */
  743. /* If there is a HAL_ERROR, it is a MMC card, else
  744. it is a SD card: SD card 2.0 (voltage range mismatch)
  745. or SD card 1.x */
  746. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_APP_CMD, SDMMC_CMDTIMEOUT);
  747. return errorstate;
  748. }
  749. /**
  750. * @brief Send the command asking the accessed card to send its operating
  751. * condition register (OCR)
  752. * @param SDMMCx Pointer to SDMMC register base
  753. * @retval HAL status
  754. */
  755. uint32_t SDMMC_CmdAppOperCommand(SDMMC_TypeDef *SDMMCx, uint32_t SdType)
  756. {
  757. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  758. uint32_t errorstate = SDMMC_ERROR_NONE;
  759. sdmmc_cmdinit.Argument = SDMMC_VOLTAGE_WINDOW_SD | SdType;
  760. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_OP_COND;
  761. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  762. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  763. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  764. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  765. /* Check for error conditions */
  766. errorstate = SDMMC_GetCmdResp3(SDMMCx);
  767. return errorstate;
  768. }
  769. /**
  770. * @brief Send the Bus Width command and check the response.
  771. * @param SDMMCx Pointer to SDMMC register base
  772. * @retval HAL status
  773. */
  774. uint32_t SDMMC_CmdBusWidth(SDMMC_TypeDef *SDMMCx, uint32_t BusWidth)
  775. {
  776. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  777. uint32_t errorstate = SDMMC_ERROR_NONE;
  778. sdmmc_cmdinit.Argument = (uint32_t)BusWidth;
  779. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_APP_SD_SET_BUSWIDTH;
  780. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  781. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  782. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  783. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  784. /* Check for error conditions */
  785. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_APP_SD_SET_BUSWIDTH, SDMMC_CMDTIMEOUT);
  786. return errorstate;
  787. }
  788. /**
  789. * @brief Send the Send SCR command and check the response.
  790. * @param SDMMCx Pointer to SDMMC register base
  791. * @retval HAL status
  792. */
  793. uint32_t SDMMC_CmdSendSCR(SDMMC_TypeDef *SDMMCx)
  794. {
  795. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  796. uint32_t errorstate = SDMMC_ERROR_NONE;
  797. /* Send CMD51 SD_APP_SEND_SCR */
  798. sdmmc_cmdinit.Argument = 0;
  799. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_SEND_SCR;
  800. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  801. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  802. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  803. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  804. /* Check for error conditions */
  805. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_APP_SEND_SCR, SDMMC_CMDTIMEOUT);
  806. return errorstate;
  807. }
  808. /**
  809. * @brief Send the Send CID command and check the response.
  810. * @param SDMMCx Pointer to SDMMC register base
  811. * @retval HAL status
  812. */
  813. uint32_t SDMMC_CmdSendCID(SDMMC_TypeDef *SDMMCx)
  814. {
  815. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  816. uint32_t errorstate = SDMMC_ERROR_NONE;
  817. /* Send CMD2 ALL_SEND_CID */
  818. sdmmc_cmdinit.Argument = 0;
  819. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ALL_SEND_CID;
  820. sdmmc_cmdinit.Response = SDMMC_RESPONSE_LONG;
  821. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  822. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  823. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  824. /* Check for error conditions */
  825. errorstate = SDMMC_GetCmdResp2(SDMMCx);
  826. return errorstate;
  827. }
  828. /**
  829. * @brief Send the Send CSD command and check the response.
  830. * @param SDMMCx Pointer to SDMMC register base
  831. * @retval HAL status
  832. */
  833. uint32_t SDMMC_CmdSendCSD(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  834. {
  835. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  836. uint32_t errorstate = SDMMC_ERROR_NONE;
  837. /* Send CMD9 SEND_CSD */
  838. sdmmc_cmdinit.Argument = (uint32_t)Argument;
  839. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_CSD;
  840. sdmmc_cmdinit.Response = SDMMC_RESPONSE_LONG;
  841. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  842. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  843. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  844. /* Check for error conditions */
  845. errorstate = SDMMC_GetCmdResp2(SDMMCx);
  846. return errorstate;
  847. }
  848. /**
  849. * @brief Send the Send CSD command and check the response.
  850. * @param SDMMCx Pointer to SDMMC register base
  851. * @retval HAL status
  852. */
  853. uint32_t SDMMC_CmdSetRelAdd(SDMMC_TypeDef *SDMMCx, uint16_t *pRCA)
  854. {
  855. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  856. uint32_t errorstate = SDMMC_ERROR_NONE;
  857. /* Send CMD3 SD_CMD_SET_REL_ADDR */
  858. sdmmc_cmdinit.Argument = 0;
  859. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_REL_ADDR;
  860. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  861. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  862. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  863. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  864. /* Check for error conditions */
  865. errorstate = SDMMC_GetCmdResp6(SDMMCx, SDMMC_CMD_SET_REL_ADDR, pRCA);
  866. return errorstate;
  867. }
  868. /**
  869. * @brief Send the Status command and check the response.
  870. * @param SDMMCx Pointer to SDMMC register base
  871. * @retval HAL status
  872. */
  873. uint32_t SDMMC_CmdSendStatus(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  874. {
  875. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  876. uint32_t errorstate = SDMMC_ERROR_NONE;
  877. sdmmc_cmdinit.Argument = (uint32_t)Argument;
  878. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_STATUS;
  879. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  880. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  881. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  882. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  883. /* Check for error conditions */
  884. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SEND_STATUS, SDMMC_CMDTIMEOUT);
  885. return errorstate;
  886. }
  887. /**
  888. * @brief Send the Status register command and check the response.
  889. * @param SDMMCx Pointer to SDMMC register base
  890. * @retval HAL status
  891. */
  892. uint32_t SDMMC_CmdStatusRegister(SDMMC_TypeDef *SDMMCx)
  893. {
  894. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  895. uint32_t errorstate = SDMMC_ERROR_NONE;
  896. sdmmc_cmdinit.Argument = 0;
  897. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_STATUS;
  898. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  899. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  900. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  901. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  902. /* Check for error conditions */
  903. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_APP_STATUS, SDMMC_CMDTIMEOUT);
  904. return errorstate;
  905. }
  906. /**
  907. * @brief Sends host capacity support information and activates the card's
  908. * initialization process. Send SDMMC_CMD_SEND_OP_COND command
  909. * @param SDIOx Pointer to SDIO register base
  910. * @parame Argument Argument used for the command
  911. * @retval HAL status
  912. */
  913. uint32_t SDMMC_CmdOpCondition(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  914. {
  915. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  916. uint32_t errorstate = SDMMC_ERROR_NONE;
  917. sdmmc_cmdinit.Argument = Argument;
  918. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_OP_COND;
  919. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  920. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  921. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  922. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  923. /* Check for error conditions */
  924. errorstate = SDMMC_GetCmdResp3(SDMMCx);
  925. return errorstate;
  926. }
  927. /**
  928. * @brief Checks switchable function and switch card function. SDMMC_CMD_HS_SWITCH comand
  929. * @param SDIOx Pointer to SDIO register base
  930. * @parame Argument Argument used for the command
  931. * @retval HAL status
  932. */
  933. uint32_t SDMMC_CmdSwitch(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  934. {
  935. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  936. uint32_t errorstate = SDMMC_ERROR_NONE;
  937. sdmmc_cmdinit.Argument = Argument;
  938. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SWITCH;
  939. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  940. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  941. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  942. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  943. /* Check for error conditions */
  944. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_HS_SWITCH, SDMMC_CMDTIMEOUT);
  945. return errorstate;
  946. }
  947. /**
  948. * @}
  949. */
  950. /* Private function ----------------------------------------------------------*/
  951. /** @addtogroup SD_Private_Functions
  952. * @{
  953. */
  954. /**
  955. * @brief Checks for error conditions for CMD0.
  956. * @param hsd SD handle
  957. * @retval SD Card error state
  958. */
  959. static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef *SDMMCx)
  960. {
  961. /* 8 is the number of required instructions cycles for the below loop statement.
  962. The SDMMC_CMDTIMEOUT is expressed in ms */
  963. register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
  964. do
  965. {
  966. if (count-- == 0)
  967. {
  968. return SDMMC_ERROR_TIMEOUT;
  969. }
  970. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDSENT));
  971. /* Clear all the static flags */
  972. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
  973. return SDMMC_ERROR_NONE;
  974. }
  975. /**
  976. * @brief Checks for error conditions for R1 response.
  977. * @param hsd SD handle
  978. * @param SD_CMD The sent command index
  979. * @retval SD Card error state
  980. */
  981. static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint32_t Timeout)
  982. {
  983. uint32_t response_r1;
  984. /* 8 is the number of required instructions cycles for the below loop statement.
  985. The Timeout is expressed in ms */
  986. register uint32_t count = Timeout * (SystemCoreClock / 8 /1000);
  987. do
  988. {
  989. if (count-- == 0)
  990. {
  991. return SDMMC_ERROR_TIMEOUT;
  992. }
  993. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
  994. if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
  995. {
  996. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
  997. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  998. }
  999. else if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
  1000. {
  1001. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
  1002. return SDMMC_ERROR_CMD_CRC_FAIL;
  1003. }
  1004. /* Check response received is of desired command */
  1005. if(SDMMC_GetCommandResponse(SDMMCx) != SD_CMD)
  1006. {
  1007. return SDMMC_ERROR_CMD_CRC_FAIL;
  1008. }
  1009. /* Clear all the static flags */
  1010. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
  1011. /* We have received response, retrieve it for analysis */
  1012. response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1);
  1013. if((response_r1 & SDMMC_OCR_ERRORBITS) == SDMMC_ALLZERO)
  1014. {
  1015. return SDMMC_ERROR_NONE;
  1016. }
  1017. else if((response_r1 & SDMMC_OCR_ADDR_OUT_OF_RANGE) == SDMMC_OCR_ADDR_OUT_OF_RANGE)
  1018. {
  1019. return SDMMC_ERROR_ADDR_OUT_OF_RANGE;
  1020. }
  1021. else if((response_r1 & SDMMC_OCR_ADDR_MISALIGNED) == SDMMC_OCR_ADDR_MISALIGNED)
  1022. {
  1023. return SDMMC_ERROR_ADDR_MISALIGNED;
  1024. }
  1025. else if((response_r1 & SDMMC_OCR_BLOCK_LEN_ERR) == SDMMC_OCR_BLOCK_LEN_ERR)
  1026. {
  1027. return SDMMC_ERROR_BLOCK_LEN_ERR;
  1028. }
  1029. else if((response_r1 & SDMMC_OCR_ERASE_SEQ_ERR) == SDMMC_OCR_ERASE_SEQ_ERR)
  1030. {
  1031. return SDMMC_ERROR_ERASE_SEQ_ERR;
  1032. }
  1033. else if((response_r1 & SDMMC_OCR_BAD_ERASE_PARAM) == SDMMC_OCR_BAD_ERASE_PARAM)
  1034. {
  1035. return SDMMC_ERROR_BAD_ERASE_PARAM;
  1036. }
  1037. else if((response_r1 & SDMMC_OCR_WRITE_PROT_VIOLATION) == SDMMC_OCR_WRITE_PROT_VIOLATION)
  1038. {
  1039. return SDMMC_ERROR_WRITE_PROT_VIOLATION;
  1040. }
  1041. else if((response_r1 & SDMMC_OCR_LOCK_UNLOCK_FAILED) == SDMMC_OCR_LOCK_UNLOCK_FAILED)
  1042. {
  1043. return SDMMC_ERROR_LOCK_UNLOCK_FAILED;
  1044. }
  1045. else if((response_r1 & SDMMC_OCR_COM_CRC_FAILED) == SDMMC_OCR_COM_CRC_FAILED)
  1046. {
  1047. return SDMMC_ERROR_COM_CRC_FAILED;
  1048. }
  1049. else if((response_r1 & SDMMC_OCR_ILLEGAL_CMD) == SDMMC_OCR_ILLEGAL_CMD)
  1050. {
  1051. return SDMMC_ERROR_ILLEGAL_CMD;
  1052. }
  1053. else if((response_r1 & SDMMC_OCR_CARD_ECC_FAILED) == SDMMC_OCR_CARD_ECC_FAILED)
  1054. {
  1055. return SDMMC_ERROR_CARD_ECC_FAILED;
  1056. }
  1057. else if((response_r1 & SDMMC_OCR_CC_ERROR) == SDMMC_OCR_CC_ERROR)
  1058. {
  1059. return SDMMC_ERROR_CC_ERR;
  1060. }
  1061. else if((response_r1 & SDMMC_OCR_STREAM_READ_UNDERRUN) == SDMMC_OCR_STREAM_READ_UNDERRUN)
  1062. {
  1063. return SDMMC_ERROR_STREAM_READ_UNDERRUN;
  1064. }
  1065. else if((response_r1 & SDMMC_OCR_STREAM_WRITE_OVERRUN) == SDMMC_OCR_STREAM_WRITE_OVERRUN)
  1066. {
  1067. return SDMMC_ERROR_STREAM_WRITE_OVERRUN;
  1068. }
  1069. else if((response_r1 & SDMMC_OCR_CID_CSD_OVERWRITE) == SDMMC_OCR_CID_CSD_OVERWRITE)
  1070. {
  1071. return SDMMC_ERROR_CID_CSD_OVERWRITE;
  1072. }
  1073. else if((response_r1 & SDMMC_OCR_WP_ERASE_SKIP) == SDMMC_OCR_WP_ERASE_SKIP)
  1074. {
  1075. return SDMMC_ERROR_WP_ERASE_SKIP;
  1076. }
  1077. else if((response_r1 & SDMMC_OCR_CARD_ECC_DISABLED) == SDMMC_OCR_CARD_ECC_DISABLED)
  1078. {
  1079. return SDMMC_ERROR_CARD_ECC_DISABLED;
  1080. }
  1081. else if((response_r1 & SDMMC_OCR_ERASE_RESET) == SDMMC_OCR_ERASE_RESET)
  1082. {
  1083. return SDMMC_ERROR_ERASE_RESET;
  1084. }
  1085. else if((response_r1 & SDMMC_OCR_AKE_SEQ_ERROR) == SDMMC_OCR_AKE_SEQ_ERROR)
  1086. {
  1087. return SDMMC_ERROR_AKE_SEQ_ERR;
  1088. }
  1089. else
  1090. {
  1091. return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  1092. }
  1093. }
  1094. /**
  1095. * @brief Checks for error conditions for R2 (CID or CSD) response.
  1096. * @param hsd SD handle
  1097. * @retval SD Card error state
  1098. */
  1099. static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef *SDMMCx)
  1100. {
  1101. /* 8 is the number of required instructions cycles for the below loop statement.
  1102. The SDMMC_CMDTIMEOUT is expressed in ms */
  1103. register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
  1104. do
  1105. {
  1106. if (count-- == 0)
  1107. {
  1108. return SDMMC_ERROR_TIMEOUT;
  1109. }
  1110. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
  1111. if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
  1112. {
  1113. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
  1114. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1115. }
  1116. else if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
  1117. {
  1118. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
  1119. return SDMMC_ERROR_CMD_CRC_FAIL;
  1120. }
  1121. else
  1122. {
  1123. /* No error flag set */
  1124. /* Clear all the static flags */
  1125. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
  1126. }
  1127. return SDMMC_ERROR_NONE;
  1128. }
  1129. /**
  1130. * @brief Checks for error conditions for R3 (OCR) response.
  1131. * @param hsd SD handle
  1132. * @retval SD Card error state
  1133. */
  1134. static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef *SDMMCx)
  1135. {
  1136. /* 8 is the number of required instructions cycles for the below loop statement.
  1137. The SDMMC_CMDTIMEOUT is expressed in ms */
  1138. register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
  1139. do
  1140. {
  1141. if (count-- == 0)
  1142. {
  1143. return SDMMC_ERROR_TIMEOUT;
  1144. }
  1145. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
  1146. if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
  1147. {
  1148. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
  1149. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1150. }
  1151. else
  1152. {
  1153. /* Clear all the static flags */
  1154. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
  1155. }
  1156. return SDMMC_ERROR_NONE;
  1157. }
  1158. /**
  1159. * @brief Checks for error conditions for R6 (RCA) response.
  1160. * @param hsd SD handle
  1161. * @param SD_CMD The sent command index
  1162. * @param pRCA Pointer to the variable that will contain the SD card relative
  1163. * address RCA
  1164. * @retval SD Card error state
  1165. */
  1166. static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint16_t *pRCA)
  1167. {
  1168. uint32_t response_r1;
  1169. /* 8 is the number of required instructions cycles for the below loop statement.
  1170. The SDMMC_CMDTIMEOUT is expressed in ms */
  1171. register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
  1172. do
  1173. {
  1174. if (count-- == 0)
  1175. {
  1176. return SDMMC_ERROR_TIMEOUT;
  1177. }
  1178. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
  1179. if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
  1180. {
  1181. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
  1182. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1183. }
  1184. else if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
  1185. {
  1186. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
  1187. return SDMMC_ERROR_CMD_CRC_FAIL;
  1188. }
  1189. /* Check response received is of desired command */
  1190. if(SDMMC_GetCommandResponse(SDMMCx) != SD_CMD)
  1191. {
  1192. return SDMMC_ERROR_CMD_CRC_FAIL;
  1193. }
  1194. /* Clear all the static flags */
  1195. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
  1196. /* We have received response, retrieve it. */
  1197. response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1);
  1198. if((response_r1 & (SDMMC_R6_GENERAL_UNKNOWN_ERROR | SDMMC_R6_ILLEGAL_CMD | SDMMC_R6_COM_CRC_FAILED)) == SDMMC_ALLZERO)
  1199. {
  1200. *pRCA = (uint16_t) (response_r1 >> 16);
  1201. return SDMMC_ERROR_NONE;
  1202. }
  1203. else if((response_r1 & SDMMC_R6_ILLEGAL_CMD) == SDMMC_R6_ILLEGAL_CMD)
  1204. {
  1205. return SDMMC_ERROR_ILLEGAL_CMD;
  1206. }
  1207. else if((response_r1 & SDMMC_R6_COM_CRC_FAILED) == SDMMC_R6_COM_CRC_FAILED)
  1208. {
  1209. return SDMMC_ERROR_COM_CRC_FAILED;
  1210. }
  1211. else
  1212. {
  1213. return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  1214. }
  1215. }
  1216. /**
  1217. * @brief Checks for error conditions for R7 response.
  1218. * @param hsd SD handle
  1219. * @retval SD Card error state
  1220. */
  1221. static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef *SDMMCx)
  1222. {
  1223. /* 8 is the number of required instructions cycles for the below loop statement.
  1224. The SDMMC_CMDTIMEOUT is expressed in ms */
  1225. register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
  1226. do
  1227. {
  1228. if (count-- == 0)
  1229. {
  1230. return SDMMC_ERROR_TIMEOUT;
  1231. }
  1232. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
  1233. if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
  1234. {
  1235. /* Card is SD V2.0 compliant */
  1236. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CMDREND);
  1237. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1238. }
  1239. if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDREND))
  1240. {
  1241. /* Card is SD V2.0 compliant */
  1242. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CMDREND);
  1243. }
  1244. return SDMMC_ERROR_NONE;
  1245. }
  1246. /**
  1247. * @}
  1248. */
  1249. /**
  1250. * @}
  1251. */
  1252. #endif /* (HAL_SD_MODULE_ENABLED) */
  1253. /**
  1254. * @}
  1255. */
  1256. /**
  1257. * @}
  1258. */
  1259. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/