stm32h7xx_ll_sdmmc.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_ll_sdmmc.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 21-April-2017
  7. * @brief SDMMC Low Layer HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the SDMMC peripheral:
  11. * + Initialization/de-initialization functions
  12. * + I/O operation functions
  13. * + Peripheral Control functions
  14. * + Peripheral State functions
  15. *
  16. @verbatim
  17. ==============================================================================
  18. ##### SDMMC peripheral features #####
  19. ==============================================================================
  20. [..] The SD/SDMMC MMC card host interface (SDMMC) provides an interface between the AHB
  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 MultiMediaCard System Specification Version 4.51. Card support
  25. for three different databus modes: 1-bit (default), 4-bit and 8-bit.
  26. (+) Full compatibility with previous versions of MultiMediaCards (backward compatibility).
  27. (+) Full compliance with SD memory card specifications version 4.1.
  28. (SDR104 SDMMC_CK speed limited to maximum allowed IO speed, SPI mode and
  29. UHS-II mode not supported).
  30. (+) Full compliance with SDIO card specification version 4.0. Card support
  31. for two different databus modes: 1-bit (default) and 4-bit.
  32. (SDR104 SDMMC_CK speed limited to maximum allowed IO speed, SPI mode and
  33. UHS-II mode not supported).
  34. (+) Data transfer up to 208 Mbyte/s for the 8 bit mode. (depending maximum allowed IO speed).
  35. (+) Data and command output enable signals to control external bidirectional drivers
  36. ##### How to use this driver #####
  37. ==============================================================================
  38. [..]
  39. This driver is a considered as a driver of service for external devices drivers
  40. that interfaces with the SDMMC peripheral.
  41. According to the device used (SD card/ MMC card / SDMMC card ...), a set of APIs
  42. is used in the device's driver to perform SDMMC operations and functionalities.
  43. This driver is almost transparent for the final user, it is only used to implement other
  44. functionalities of the external device.
  45. [..]
  46. (+) The SDMMC clock is coming from output of PLL1_Q or PLL2_R.
  47. Before start working with SDMMC peripheral make sure that the PLL is well configured.
  48. The SDMMC peripheral uses two clock signals:
  49. (++) PLL1_Q bus clock (default after reset)
  50. (++) PLL2_R bus clock
  51. (+) Enable/Disable peripheral clock using RCC peripheral macros related to SDMMC
  52. peripheral.
  53. (+) Enable the Power ON State using the SDMMC_PowerState_ON(SDMMCx)
  54. function and disable it using the function SDMMC_PowerState_OFF(SDMMCx).
  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 IDMA mode (Single buffer or double)
  59. (++) Configure the buffer address
  60. (++) Configure Data Path State Machine
  61. (+) To control the CPSM (Command Path State Machine) and send
  62. commands to the card use the SDMMC_SendCommand(SDMMCx),
  63. SDMMC_GetCommandResponse() and SDMMC_GetResponse() functions. First, user has
  64. to fill the command structure (pointer to SDMMC_CmdInitTypeDef) according
  65. to the selected command to be sent.
  66. The parameters that should be filled are:
  67. (++) Command Argument
  68. (++) Command Index
  69. (++) Command Response type
  70. (++) Command Wait
  71. (++) CPSM Status (Enable or Disable).
  72. -@@- To check if the command is well received, read the SDMMC_CMDRESP
  73. register using the SDMMC_GetCommandResponse().
  74. The SDMMC responses registers (SDMMC_RESP1 to SDMMC_RESP2), use the
  75. SDMMC_GetResponse() function.
  76. (+) To control the DPSM (Data Path State Machine) and send/receive
  77. data to/from the card use the SDMMC_DataConfig(), SDMMC_GetDataCounter(),
  78. SDMMC_ReadFIFO(), SDMMC_WriteFIFO() and SDMMC_GetFIFOCount() functions.
  79. *** Read Operations ***
  80. =======================
  81. [..]
  82. (#) First, user has to fill the data structure (pointer to
  83. SDMMC_DataInitTypeDef) according to the selected data type to be received.
  84. The parameters that should be filled are:
  85. (++) Data TimeOut
  86. (++) Data Length
  87. (++) Data Block size
  88. (++) Data Transfer direction: should be from card (To SDMMC)
  89. (++) Data Transfer mode
  90. (++) DPSM Status (Enable or Disable)
  91. (#) Configure the SDMMC resources to receive the data from the card
  92. according to selected transfer mode (Refer to Step 8, 9 and 10).
  93. (#) Send the selected Read command (refer to step 11).
  94. (#) Use the SDMMC flags/interrupts to check the transfer status.
  95. *** Write Operations ***
  96. ========================
  97. [..]
  98. (#) First, user has to fill the data structure (pointer to
  99. SDMMC_DataInitTypeDef) according to the selected data type to be received.
  100. The parameters that should be filled are:
  101. (++) Data TimeOut
  102. (++) Data Length
  103. (++) Data Block size
  104. (++) Data Transfer direction: should be to card (To CARD)
  105. (++) Data Transfer mode
  106. (++) DPSM Status (Enable or Disable)
  107. (#) Configure the SDMMC resources to send the data to the card according to
  108. selected transfer mode.
  109. (#) Send the selected Write command.
  110. (#) Use the SDMMC flags/interrupts to check the transfer status.
  111. *** Command management operations ***
  112. =====================================
  113. [..]
  114. (#) The commands used for Read/Write/Erase operations are managed in
  115. separate functions.
  116. Each function allows to send the needed command with the related argument,
  117. then check the response.
  118. By the same approach, you could implement a command and check the response.
  119. @endverbatim
  120. ******************************************************************************
  121. * @attention
  122. *
  123. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  124. *
  125. * Redistribution and use in source and binary forms, with or without modification,
  126. * are permitted provided that the following conditions are met:
  127. * 1. Redistributions of source code must retain the above copyright notice,
  128. * this list of conditions and the following disclaimer.
  129. * 2. Redistributions in binary form must reproduce the above copyright notice,
  130. * this list of conditions and the following disclaimer in the documentation
  131. * and/or other materials provided with the distribution.
  132. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  133. * may be used to endorse or promote products derived from this software
  134. * without specific prior written permission.
  135. *
  136. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  137. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  138. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  139. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  140. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  141. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  142. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  143. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  144. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  145. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  146. *
  147. ******************************************************************************
  148. */
  149. /* Includes ------------------------------------------------------------------*/
  150. #include "stm32h7xx_hal.h"
  151. /** @addtogroup STM32H7xx_HAL_Driver
  152. * @{
  153. */
  154. /** @defgroup SDMMC_LL SDMMC Low Layer
  155. * @brief Low layer module for SD
  156. * @{
  157. */
  158. #if defined (HAL_SD_MODULE_ENABLED) || defined (HAL_MMC_MODULE_ENABLED)
  159. /* Private typedef -----------------------------------------------------------*/
  160. /* Private define ------------------------------------------------------------*/
  161. /* Private macro -------------------------------------------------------------*/
  162. /* Private variables ---------------------------------------------------------*/
  163. /* Private function prototypes -----------------------------------------------*/
  164. static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef *SDMMCx);
  165. static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint32_t Timeout);
  166. static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef *SDMMCx);
  167. static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef *SDMMCx);
  168. static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef *SDMMCx);
  169. static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint16_t *pRCA);
  170. /* Exported functions --------------------------------------------------------*/
  171. /** @defgroup SDMMC_LL_Exported_Functions SDMMC Low Layer Exported Functions
  172. * @{
  173. */
  174. /** @defgroup HAL_SDMMC_LL_Group1 Initialization de-initialization functions
  175. * @brief Initialization and Configuration functions
  176. *
  177. @verbatim
  178. ===============================================================================
  179. ##### Initialization/de-initialization functions #####
  180. ===============================================================================
  181. [..] This section provides functions allowing to:
  182. @endverbatim
  183. * @{
  184. */
  185. /**
  186. * @brief Initializes the SDMMC according to the specified
  187. * parameters in the SDMMC_InitTypeDef and create the associated handle.
  188. * @param SDMMCx: Pointer to SDMMC register base
  189. * @param Init: SDMMC initialization structure
  190. * @retval HAL status
  191. */
  192. HAL_StatusTypeDef SDMMC_Init(SDMMC_TypeDef *SDMMCx, SDMMC_InitTypeDef Init)
  193. {
  194. uint32_t tmpreg = 0;
  195. /* Check the parameters */
  196. assert_param(IS_SDMMC_ALL_INSTANCE(SDMMCx));
  197. assert_param(IS_SDMMC_CLOCK_EDGE(Init.ClockEdge));
  198. assert_param(IS_SDMMC_CLOCK_POWER_SAVE(Init.ClockPowerSave));
  199. assert_param(IS_SDMMC_BUS_WIDE(Init.BusWide));
  200. assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(Init.HardwareFlowControl));
  201. assert_param(IS_SDMMC_CLKDIV(Init.ClockDiv));
  202. /* Set SDMMC configuration parameters */
  203. tmpreg |= (Init.ClockEdge |\
  204. Init.ClockPowerSave |\
  205. Init.BusWide |\
  206. Init.HardwareFlowControl |\
  207. Init.ClockDiv
  208. );
  209. /* Write to SDMMC CLKCR */
  210. MODIFY_REG(SDMMCx->CLKCR, CLKCR_CLEAR_MASK, tmpreg);
  211. return HAL_OK;
  212. }
  213. /**
  214. * @}
  215. */
  216. /** @defgroup HAL_SDMMC_LL_Group2 IO operation functions
  217. * @brief Data transfers functions
  218. *
  219. @verbatim
  220. ===============================================================================
  221. ##### I/O operation functions #####
  222. ===============================================================================
  223. [..]
  224. This subsection provides a set of functions allowing to manage the SDMMC data
  225. transfers.
  226. @endverbatim
  227. * @{
  228. */
  229. /**
  230. * @brief Read data (word) from Rx FIFO in blocking mode (polling)
  231. * @param SDMMCx: Pointer to SDMMC register base
  232. * @retval HAL status
  233. */
  234. uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
  235. {
  236. /* Read data from Rx FIFO */
  237. return (SDMMCx->FIFO);
  238. }
  239. /**
  240. * @brief Write data (word) to Tx FIFO in blocking mode (polling)
  241. * @param SDMMCx: Pointer to SDMMC register base
  242. * @param pWriteData: pointer to data to write
  243. * @retval HAL status
  244. */
  245. HAL_StatusTypeDef SDMMC_WriteFIFO(SDMMC_TypeDef *SDMMCx, uint32_t *pWriteData)
  246. {
  247. /* Write data to FIFO */
  248. SDMMCx->FIFO = *pWriteData;
  249. return HAL_OK;
  250. }
  251. /**
  252. * @}
  253. */
  254. /** @defgroup HAL_SDMMC_LL_Group3 Peripheral Control functions
  255. * @brief management functions
  256. *
  257. @verbatim
  258. ===============================================================================
  259. ##### Peripheral Control functions #####
  260. ===============================================================================
  261. [..]
  262. This subsection provides a set of functions allowing to control the SDMMC data
  263. transfers.
  264. @endverbatim
  265. * @{
  266. */
  267. /**
  268. * @brief Set SDMMC Power state to ON.
  269. * @param SDMMCx: Pointer to SDMMC register base
  270. * @retval HAL status
  271. */
  272. HAL_StatusTypeDef SDMMC_PowerState_ON(SDMMC_TypeDef *SDMMCx)
  273. {
  274. /* Set power state to ON */
  275. SDMMCx->POWER |= SDMMC_POWER_PWRCTRL;
  276. /* 2ms: required power up waiting time before starting the SD initialization
  277. sequence */
  278. HAL_Delay(2);
  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. * @param Argument: Command Argument
  731. * @retval HAL status
  732. */
  733. uint32_t SDMMC_CmdAppCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  734. {
  735. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  736. uint32_t errorstate = SDMMC_ERROR_NONE;
  737. sdmmc_cmdinit.Argument = (uint32_t)Argument;
  738. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_APP_CMD;
  739. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  740. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  741. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  742. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  743. /* Check for error conditions */
  744. /* If there is a HAL_ERROR, it is a MMC card, else
  745. it is a SD card: SD card 2.0 (voltage range mismatch)
  746. or SD card 1.x */
  747. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_APP_CMD, SDMMC_CMDTIMEOUT);
  748. return errorstate;
  749. }
  750. /**
  751. * @brief Send the command asking the accessed card to send its operating
  752. * condition register (OCR)
  753. * @param SDMMCx: Pointer to SDMMC register base
  754. * @param Argument: Command Argument
  755. * @retval HAL status
  756. */
  757. uint32_t SDMMC_CmdAppOperCommand(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  758. {
  759. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  760. uint32_t errorstate = SDMMC_ERROR_NONE;
  761. sdmmc_cmdinit.Argument = Argument;
  762. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_OP_COND;
  763. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  764. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  765. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  766. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  767. /* Check for error conditions */
  768. errorstate = SDMMC_GetCmdResp3(SDMMCx);
  769. return errorstate;
  770. }
  771. /**
  772. * @brief Send the Bus Width command and check the response.
  773. * @param SDMMCx: Pointer to SDMMC register base
  774. * @param BusWidth: BusWidth
  775. * @retval HAL status
  776. */
  777. uint32_t SDMMC_CmdBusWidth(SDMMC_TypeDef *SDMMCx, uint32_t BusWidth)
  778. {
  779. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  780. uint32_t errorstate = SDMMC_ERROR_NONE;
  781. sdmmc_cmdinit.Argument = (uint32_t)BusWidth;
  782. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_APP_SD_SET_BUSWIDTH;
  783. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  784. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  785. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  786. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  787. /* Check for error conditions */
  788. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_APP_SD_SET_BUSWIDTH, SDMMC_CMDTIMEOUT);
  789. return errorstate;
  790. }
  791. /**
  792. * @brief Send the Send SCR command and check the response.
  793. * @param SDMMCx: Pointer to SDMMC register base
  794. * @retval HAL status
  795. */
  796. uint32_t SDMMC_CmdSendSCR(SDMMC_TypeDef *SDMMCx)
  797. {
  798. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  799. uint32_t errorstate = SDMMC_ERROR_NONE;
  800. /* Send CMD51 SD_APP_SEND_SCR */
  801. sdmmc_cmdinit.Argument = 0;
  802. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_SEND_SCR;
  803. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  804. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  805. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  806. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  807. /* Check for error conditions */
  808. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_APP_SEND_SCR, SDMMC_CMDTIMEOUT);
  809. return errorstate;
  810. }
  811. /**
  812. * @brief Send the Send CID command and check the response.
  813. * @param SDMMCx: Pointer to SDMMC register base
  814. * @retval HAL status
  815. */
  816. uint32_t SDMMC_CmdSendCID(SDMMC_TypeDef *SDMMCx)
  817. {
  818. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  819. uint32_t errorstate = SDMMC_ERROR_NONE;
  820. /* Send CMD2 ALL_SEND_CID */
  821. sdmmc_cmdinit.Argument = 0;
  822. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ALL_SEND_CID;
  823. sdmmc_cmdinit.Response = SDMMC_RESPONSE_LONG;
  824. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  825. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  826. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  827. /* Check for error conditions */
  828. errorstate = SDMMC_GetCmdResp2(SDMMCx);
  829. return errorstate;
  830. }
  831. /**
  832. * @brief Send the Send CSD command and check the response.
  833. * @param SDMMCx: Pointer to SDMMC register base
  834. * @param Argument: Command Argument
  835. * @retval HAL status
  836. */
  837. uint32_t SDMMC_CmdSendCSD(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  838. {
  839. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  840. uint32_t errorstate = SDMMC_ERROR_NONE;
  841. /* Send CMD9 SEND_CSD */
  842. sdmmc_cmdinit.Argument = (uint32_t)Argument;
  843. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_CSD;
  844. sdmmc_cmdinit.Response = SDMMC_RESPONSE_LONG;
  845. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  846. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  847. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  848. /* Check for error conditions */
  849. errorstate = SDMMC_GetCmdResp2(SDMMCx);
  850. return errorstate;
  851. }
  852. /**
  853. * @brief Send the Send CSD command and check the response.
  854. * @param SDMMCx: Pointer to SDMMC register base
  855. * @param pRCA: Card RCA
  856. * @retval HAL status
  857. */
  858. uint32_t SDMMC_CmdSetRelAdd(SDMMC_TypeDef *SDMMCx, uint16_t *pRCA)
  859. {
  860. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  861. uint32_t errorstate = SDMMC_ERROR_NONE;
  862. /* Send CMD3 SD_CMD_SET_REL_ADDR */
  863. sdmmc_cmdinit.Argument = 0;
  864. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_REL_ADDR;
  865. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  866. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  867. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  868. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  869. /* Check for error conditions */
  870. errorstate = SDMMC_GetCmdResp6(SDMMCx, SDMMC_CMD_SET_REL_ADDR, pRCA);
  871. return errorstate;
  872. }
  873. /**
  874. * @brief Send the Status command and check the response.
  875. * @param SDMMCx: Pointer to SDMMC register base
  876. * @param Argument: Command Argument
  877. * @retval HAL status
  878. */
  879. uint32_t SDMMC_CmdSendStatus(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  880. {
  881. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  882. uint32_t errorstate = SDMMC_ERROR_NONE;
  883. sdmmc_cmdinit.Argument = (uint32_t)Argument;
  884. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_STATUS;
  885. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  886. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  887. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  888. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  889. /* Check for error conditions */
  890. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SEND_STATUS, SDMMC_CMDTIMEOUT);
  891. return errorstate;
  892. }
  893. /**
  894. * @brief Send the Status register command and check the response.
  895. * @param SDMMCx: Pointer to SDMMC register base
  896. * @retval HAL status
  897. */
  898. uint32_t SDMMC_CmdStatusRegister(SDMMC_TypeDef *SDMMCx)
  899. {
  900. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  901. uint32_t errorstate = SDMMC_ERROR_NONE;
  902. sdmmc_cmdinit.Argument = 0;
  903. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_STATUS;
  904. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  905. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  906. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  907. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  908. /* Check for error conditions */
  909. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_SD_APP_STATUS, SDMMC_CMDTIMEOUT);
  910. return errorstate;
  911. }
  912. /**
  913. * @brief Sends host capacity support information and activates the card's
  914. * initialization process. Send SDMMC_CMD_SEND_OP_COND command
  915. * @param SDIOx: Pointer to SDIO register base
  916. * @parame Argument: Argument used for the command
  917. * @retval HAL status
  918. */
  919. uint32_t SDMMC_CmdOpCondition(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  920. {
  921. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  922. uint32_t errorstate = SDMMC_ERROR_NONE;
  923. sdmmc_cmdinit.Argument = Argument;
  924. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_OP_COND;
  925. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  926. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  927. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  928. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  929. /* Check for error conditions */
  930. errorstate = SDMMC_GetCmdResp3(SDMMCx);
  931. return errorstate;
  932. }
  933. /**
  934. * @brief Checks switchable function and switch card function. SDMMC_CMD_HS_SWITCH comand
  935. * @param SDIOx: Pointer to SDIO register base
  936. * @parame Argument: Argument used for the command
  937. * @retval HAL status
  938. */
  939. uint32_t SDMMC_CmdSwitch(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  940. {
  941. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  942. uint32_t errorstate = SDMMC_ERROR_NONE;
  943. /* Send CMD6 to activate SDR50 Mode and Power Limit 1.44W */
  944. /* CMD Response: R1 */
  945. sdmmc_cmdinit.Argument = Argument;
  946. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SWITCH;
  947. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  948. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  949. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  950. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  951. /* Check for error conditions */
  952. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_HS_SWITCH, SDMMC_CMDTIMEOUT);
  953. return errorstate;
  954. }
  955. /**
  956. * @brief Send the command asking the accessed card to send its operating
  957. * condition register (OCR)
  958. * @param None
  959. * @retval HAL status
  960. */
  961. uint32_t SDMMC_CmdVoltageSwitch(SDMMC_TypeDef *SDMMCx)
  962. {
  963. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  964. uint32_t errorstate = SDMMC_ERROR_NONE;
  965. sdmmc_cmdinit.Argument = 0x00000000;
  966. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_VOLTAGE_SWITCH;
  967. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  968. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  969. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  970. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  971. /* Check for error conditions */
  972. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_VOLTAGE_SWITCH, SDMMC_CMDTIMEOUT);
  973. return errorstate;
  974. }
  975. /**
  976. * @brief Send the Send EXT_CSD command and check the response.
  977. * @param SDMMCx: Pointer to SDMMC register base
  978. * @param Argument: Command Argument
  979. * @retval HAL status
  980. */
  981. uint32_t SDMMC_CmdSendEXTCSD(SDMMC_TypeDef *SDMMCx, uint32_t Argument)
  982. {
  983. SDMMC_CmdInitTypeDef sdmmc_cmdinit;
  984. uint32_t errorstate = SDMMC_ERROR_NONE;
  985. /* Send CMD9 SEND_CSD */
  986. sdmmc_cmdinit.Argument = (uint32_t)Argument;
  987. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SEND_EXT_CSD;
  988. sdmmc_cmdinit.Response = SDMMC_RESPONSE_SHORT;
  989. sdmmc_cmdinit.WaitForInterrupt = SDMMC_WAIT_NO;
  990. sdmmc_cmdinit.CPSM = SDMMC_CPSM_ENABLE;
  991. SDMMC_SendCommand(SDMMCx, &sdmmc_cmdinit);
  992. /* Check for error conditions */
  993. errorstate = SDMMC_GetCmdResp1(SDMMCx, SDMMC_CMD_HS_SEND_EXT_CSD,SDMMC_CMDTIMEOUT);
  994. return errorstate;
  995. }
  996. /**
  997. * @}
  998. */
  999. /* Private function ----------------------------------------------------------*/
  1000. /** @addtogroup SD_Private_Functions
  1001. * @{
  1002. */
  1003. /**
  1004. * @brief Checks for error conditions for CMD0.
  1005. * @param hsd: SD handle
  1006. * @retval SD Card error state
  1007. */
  1008. static uint32_t SDMMC_GetCmdError(SDMMC_TypeDef *SDMMCx)
  1009. {
  1010. /* 8 is the number of required instructions cycles for the below loop statement.
  1011. The SDMMC_CMDTIMEOUT is expressed in ms */
  1012. register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
  1013. do
  1014. {
  1015. if (count-- == 0)
  1016. {
  1017. return SDMMC_ERROR_TIMEOUT;
  1018. }
  1019. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDSENT));
  1020. /* Clear all the static flags */
  1021. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
  1022. return SDMMC_ERROR_NONE;
  1023. }
  1024. /**
  1025. * @brief Checks for error conditions for R1 response.
  1026. * @param hsd: SD handle
  1027. * @param SD_CMD: The sent command index
  1028. * @retval SD Card error state
  1029. */
  1030. static uint32_t SDMMC_GetCmdResp1(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint32_t Timeout)
  1031. {
  1032. uint32_t response_r1;
  1033. /* 8 is the number of required instructions cycles for the below loop statement.
  1034. The Timeout is expressed in ms */
  1035. register uint32_t count = Timeout * (SystemCoreClock / 8 /1000);
  1036. do
  1037. {
  1038. if (count-- == 0)
  1039. {
  1040. return SDMMC_ERROR_TIMEOUT;
  1041. }
  1042. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT | SDMMC_FLAG_BUSYD0END));
  1043. if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
  1044. {
  1045. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
  1046. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1047. }
  1048. else if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
  1049. {
  1050. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
  1051. return SDMMC_ERROR_CMD_CRC_FAIL;
  1052. }
  1053. /* Clear all the static flags */
  1054. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
  1055. /* We have received response, retrieve it for analysis */
  1056. response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1);
  1057. if((response_r1 & SDMMC_OCR_ERRORBITS) == SDMMC_ALLZERO)
  1058. {
  1059. return SDMMC_ERROR_NONE;
  1060. }
  1061. else if((response_r1 & SDMMC_OCR_ADDR_OUT_OF_RANGE) == SDMMC_OCR_ADDR_OUT_OF_RANGE)
  1062. {
  1063. return SDMMC_ERROR_ADDR_OUT_OF_RANGE;
  1064. }
  1065. else if((response_r1 & SDMMC_OCR_ADDR_MISALIGNED) == SDMMC_OCR_ADDR_MISALIGNED)
  1066. {
  1067. return SDMMC_ERROR_ADDR_MISALIGNED;
  1068. }
  1069. else if((response_r1 & SDMMC_OCR_BLOCK_LEN_ERR) == SDMMC_OCR_BLOCK_LEN_ERR)
  1070. {
  1071. return SDMMC_ERROR_BLOCK_LEN_ERR;
  1072. }
  1073. else if((response_r1 & SDMMC_OCR_ERASE_SEQ_ERR) == SDMMC_OCR_ERASE_SEQ_ERR)
  1074. {
  1075. return SDMMC_ERROR_ERASE_SEQ_ERR;
  1076. }
  1077. else if((response_r1 & SDMMC_OCR_BAD_ERASE_PARAM) == SDMMC_OCR_BAD_ERASE_PARAM)
  1078. {
  1079. return SDMMC_ERROR_BAD_ERASE_PARAM;
  1080. }
  1081. else if((response_r1 & SDMMC_OCR_WRITE_PROT_VIOLATION) == SDMMC_OCR_WRITE_PROT_VIOLATION)
  1082. {
  1083. return SDMMC_ERROR_WRITE_PROT_VIOLATION;
  1084. }
  1085. else if((response_r1 & SDMMC_OCR_LOCK_UNLOCK_FAILED) == SDMMC_OCR_LOCK_UNLOCK_FAILED)
  1086. {
  1087. return SDMMC_ERROR_LOCK_UNLOCK_FAILED;
  1088. }
  1089. else if((response_r1 & SDMMC_OCR_COM_CRC_FAILED) == SDMMC_OCR_COM_CRC_FAILED)
  1090. {
  1091. return SDMMC_ERROR_COM_CRC_FAILED;
  1092. }
  1093. else if((response_r1 & SDMMC_OCR_ILLEGAL_CMD) == SDMMC_OCR_ILLEGAL_CMD)
  1094. {
  1095. return SDMMC_ERROR_ILLEGAL_CMD;
  1096. }
  1097. else if((response_r1 & SDMMC_OCR_CARD_ECC_FAILED) == SDMMC_OCR_CARD_ECC_FAILED)
  1098. {
  1099. return SDMMC_ERROR_CARD_ECC_FAILED;
  1100. }
  1101. else if((response_r1 & SDMMC_OCR_CC_ERROR) == SDMMC_OCR_CC_ERROR)
  1102. {
  1103. return SDMMC_ERROR_CC_ERR;
  1104. }
  1105. else if((response_r1 & SDMMC_OCR_STREAM_READ_UNDERRUN) == SDMMC_OCR_STREAM_READ_UNDERRUN)
  1106. {
  1107. return SDMMC_ERROR_STREAM_READ_UNDERRUN;
  1108. }
  1109. else if((response_r1 & SDMMC_OCR_STREAM_WRITE_OVERRUN) == SDMMC_OCR_STREAM_WRITE_OVERRUN)
  1110. {
  1111. return SDMMC_ERROR_STREAM_WRITE_OVERRUN;
  1112. }
  1113. else if((response_r1 & SDMMC_OCR_CID_CSD_OVERWRITE) == SDMMC_OCR_CID_CSD_OVERWRITE)
  1114. {
  1115. return SDMMC_ERROR_CID_CSD_OVERWRITE;
  1116. }
  1117. else if((response_r1 & SDMMC_OCR_WP_ERASE_SKIP) == SDMMC_OCR_WP_ERASE_SKIP)
  1118. {
  1119. return SDMMC_ERROR_WP_ERASE_SKIP;
  1120. }
  1121. else if((response_r1 & SDMMC_OCR_CARD_ECC_DISABLED) == SDMMC_OCR_CARD_ECC_DISABLED)
  1122. {
  1123. return SDMMC_ERROR_CARD_ECC_DISABLED;
  1124. }
  1125. else if((response_r1 & SDMMC_OCR_ERASE_RESET) == SDMMC_OCR_ERASE_RESET)
  1126. {
  1127. return SDMMC_ERROR_ERASE_RESET;
  1128. }
  1129. else if((response_r1 & SDMMC_OCR_AKE_SEQ_ERROR) == SDMMC_OCR_AKE_SEQ_ERROR)
  1130. {
  1131. return SDMMC_ERROR_AKE_SEQ_ERR;
  1132. }
  1133. else
  1134. {
  1135. return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  1136. }
  1137. }
  1138. /**
  1139. * @brief Checks for error conditions for R2 (CID or CSD) response.
  1140. * @param hsd: SD handle
  1141. * @retval SD Card error state
  1142. */
  1143. static uint32_t SDMMC_GetCmdResp2(SDMMC_TypeDef *SDMMCx)
  1144. {
  1145. /* 8 is the number of required instructions cycles for the below loop statement.
  1146. The SDMMC_CMDTIMEOUT is expressed in ms */
  1147. register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
  1148. do
  1149. {
  1150. if (count-- == 0)
  1151. {
  1152. return SDMMC_ERROR_TIMEOUT;
  1153. }
  1154. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
  1155. if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
  1156. {
  1157. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
  1158. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1159. }
  1160. else if (__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
  1161. {
  1162. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
  1163. return SDMMC_ERROR_CMD_CRC_FAIL;
  1164. }
  1165. else
  1166. {
  1167. /* No error flag set */
  1168. /* Clear all the static flags */
  1169. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
  1170. }
  1171. return SDMMC_ERROR_NONE;
  1172. }
  1173. /**
  1174. * @brief Checks for error conditions for R3 (OCR) response.
  1175. * @param hsd: SD handle
  1176. * @retval SD Card error state
  1177. */
  1178. static uint32_t SDMMC_GetCmdResp3(SDMMC_TypeDef *SDMMCx)
  1179. {
  1180. /* 8 is the number of required instructions cycles for the below loop statement.
  1181. The SDMMC_CMDTIMEOUT is expressed in ms */
  1182. register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
  1183. do
  1184. {
  1185. if (count-- == 0)
  1186. {
  1187. return SDMMC_ERROR_TIMEOUT;
  1188. }
  1189. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
  1190. if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
  1191. {
  1192. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
  1193. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1194. }
  1195. else
  1196. {
  1197. /* Clear all the static flags */
  1198. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
  1199. }
  1200. return SDMMC_ERROR_NONE;
  1201. }
  1202. /**
  1203. * @brief Checks for error conditions for R6 (RCA) response.
  1204. * @param hsd: SD handle
  1205. * @param SD_CMD: The sent command index
  1206. * @param pRCA: Pointer to the variable that will contain the SD card relative
  1207. * address RCA
  1208. * @retval SD Card error state
  1209. */
  1210. static uint32_t SDMMC_GetCmdResp6(SDMMC_TypeDef *SDMMCx, uint8_t SD_CMD, uint16_t *pRCA)
  1211. {
  1212. uint32_t response_r1;
  1213. /* 8 is the number of required instructions cycles for the below loop statement.
  1214. The SDMMC_CMDTIMEOUT is expressed in ms */
  1215. register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
  1216. do
  1217. {
  1218. if (count-- == 0)
  1219. {
  1220. return SDMMC_ERROR_TIMEOUT;
  1221. }
  1222. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
  1223. if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
  1224. {
  1225. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
  1226. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1227. }
  1228. else if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL))
  1229. {
  1230. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL);
  1231. return SDMMC_ERROR_CMD_CRC_FAIL;
  1232. }
  1233. /* Check response received is of desired command */
  1234. if(SDMMC_GetCommandResponse(SDMMCx) != SD_CMD)
  1235. {
  1236. return SDMMC_ERROR_CMD_CRC_FAIL;
  1237. }
  1238. /* Clear all the static flags */
  1239. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_STATIC_FLAGS);
  1240. /* We have received response, retrieve it. */
  1241. response_r1 = SDMMC_GetResponse(SDMMCx, SDMMC_RESP1);
  1242. if((response_r1 & (SDMMC_R6_GENERAL_UNKNOWN_ERROR | SDMMC_R6_ILLEGAL_CMD | SDMMC_R6_COM_CRC_FAILED)) == SDMMC_ALLZERO)
  1243. {
  1244. *pRCA = (uint16_t) (response_r1 >> 16);
  1245. return SDMMC_ERROR_NONE;
  1246. }
  1247. else if((response_r1 & SDMMC_R6_ILLEGAL_CMD) == SDMMC_R6_ILLEGAL_CMD)
  1248. {
  1249. return SDMMC_ERROR_ILLEGAL_CMD;
  1250. }
  1251. else if((response_r1 & SDMMC_R6_COM_CRC_FAILED) == SDMMC_R6_COM_CRC_FAILED)
  1252. {
  1253. return SDMMC_ERROR_COM_CRC_FAILED;
  1254. }
  1255. else
  1256. {
  1257. return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  1258. }
  1259. }
  1260. /**
  1261. * @brief Checks for error conditions for R7 response.
  1262. * @param hsd: SD handle
  1263. * @retval SD Card error state
  1264. */
  1265. static uint32_t SDMMC_GetCmdResp7(SDMMC_TypeDef *SDMMCx)
  1266. {
  1267. /* 8 is the number of required instructions cycles for the below loop statement.
  1268. The SDMMC_CMDTIMEOUT is expressed in ms */
  1269. register uint32_t count = SDMMC_CMDTIMEOUT * (SystemCoreClock / 8 /1000);
  1270. do
  1271. {
  1272. if (count-- == 0)
  1273. {
  1274. return SDMMC_ERROR_TIMEOUT;
  1275. }
  1276. }while(!__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CCRCFAIL | SDMMC_FLAG_CMDREND | SDMMC_FLAG_CTIMEOUT));
  1277. if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT))
  1278. {
  1279. /* Card is SD V2.0 compliant */
  1280. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CTIMEOUT);
  1281. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1282. }
  1283. if(__SDMMC_GET_FLAG(SDMMCx, SDMMC_FLAG_CMDREND))
  1284. {
  1285. /* Card is SD V2.0 compliant */
  1286. __SDMMC_CLEAR_FLAG(SDMMCx, SDMMC_FLAG_CMDREND);
  1287. }
  1288. return SDMMC_ERROR_NONE;
  1289. }
  1290. /**
  1291. * @}
  1292. */
  1293. #endif /* (HAL_SD_MODULE_ENABLED) */
  1294. /**
  1295. * @}
  1296. */
  1297. /**
  1298. * @}
  1299. */
  1300. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/