stm32l4xx_ll_sdmmc.c 49 KB

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