stm32f1xx_ll_sdmmc.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_ll_sdmmc.c
  4. * @author MCD Application Team
  5. * @version V1.1.1
  6. * @date 12-May-2017
  7. * @brief SDIO Low Layer HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the SDIO 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 databus 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 SDIO peripheral uses two clock signals:
  45. (++) SDIO adapter clock (SDIOCLK = HCLK)
  46. (++) AHB bus clock (HCLK/2)
  47. -@@- PCLK2 and SDMMC_CK clock frequencies must respect the following condition:
  48. Frequency(PCLK2) >= (3 / 8 x Frequency(SDMMC_CK))
  49. (+) Enable/Disable peripheral clock using RCC peripheral macros related to SDMMC
  50. peripheral.
  51. (+) Enable the Power ON State using the SDIO_PowerState_ON(SDIOx)
  52. function and disable it using the function SDIO_PowerState_OFF(SDIOx).
  53. (+) Enable/Disable the clock using the __SDIO_ENABLE()/__SDIO_DISABLE() macros.
  54. (+) Enable/Disable the peripheral interrupts using the macros __SDIO_ENABLE_IT(hsdio, IT)
  55. and __SDIO_DISABLE_IT(hsdio, IT) if you need to use interrupt mode.
  56. (+) When using the DMA mode
  57. (++) Configure the DMA in the MSP layer of the external device
  58. (++) Active the needed channel Request
  59. (++) Enable the DMA using __SDIO_DMA_ENABLE() macro or Disable it using the macro
  60. __SDIO_DMA_DISABLE().
  61. (+) To control the CPSM (Command Path State Machine) and send
  62. commands to the card use the SDIO_SendCommand(),
  63. SDIO_GetCommandResponse() and SDIO_GetResponse() functions. First, user has
  64. to fill the command structure (pointer to SDIO_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 SDIO_CMDRESP
  73. register using the SDIO_GetCommandResponse().
  74. The SDMMC responses registers (SDIO_RESP1 to SDIO_RESP2), use the
  75. SDIO_GetResponse() function.
  76. (+) To control the DPSM (Data Path State Machine) and send/receive
  77. data to/from the card use the SDIO_ConfigData(), SDIO_GetDataCounter(),
  78. SDIO_ReadFIFO(), SDIO_WriteFIFO() and SDIO_GetFIFOCount() functions.
  79. *** Read Operations ***
  80. =======================
  81. [..]
  82. (#) First, user has to fill the data structure (pointer to
  83. SDIO_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 SDIO flags/interrupts to check the transfer status.
  95. *** Write Operations ***
  96. ========================
  97. [..]
  98. (#) First, user has to fill the data structure (pointer to
  99. SDIO_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 SDIO 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 "stm32f1xx_hal.h"
  151. #if defined(STM32F103xE) || defined(STM32F103xG)
  152. /** @addtogroup STM32F1xx_HAL_Driver
  153. * @{
  154. */
  155. /** @defgroup SDMMC_LL SDMMC Low Layer
  156. * @brief Low layer module for SD
  157. * @{
  158. */
  159. #if defined (HAL_SD_MODULE_ENABLED) || defined(HAL_MMC_MODULE_ENABLED)
  160. /* Private typedef -----------------------------------------------------------*/
  161. /* Private define ------------------------------------------------------------*/
  162. /* Private macro -------------------------------------------------------------*/
  163. /* Private variables ---------------------------------------------------------*/
  164. /* Private function prototypes -----------------------------------------------*/
  165. static uint32_t SDMMC_GetCmdError(SDIO_TypeDef *SDIOx);
  166. static uint32_t SDMMC_GetCmdResp1(SDIO_TypeDef *SDIOx, uint8_t SD_CMD, uint32_t Timeout);
  167. static uint32_t SDMMC_GetCmdResp2(SDIO_TypeDef *SDIOx);
  168. static uint32_t SDMMC_GetCmdResp3(SDIO_TypeDef *SDIOx);
  169. static uint32_t SDMMC_GetCmdResp7(SDIO_TypeDef *SDIOx);
  170. static uint32_t SDMMC_GetCmdResp6(SDIO_TypeDef *SDIOx, uint8_t SD_CMD, uint16_t *pRCA);
  171. /* Exported functions --------------------------------------------------------*/
  172. /** @defgroup SDMMC_LL_Exported_Functions SDMMC Low Layer Exported Functions
  173. * @{
  174. */
  175. /** @defgroup HAL_SDMMC_LL_Group1 Initialization de-initialization functions
  176. * @brief Initialization and Configuration functions
  177. *
  178. @verbatim
  179. ===============================================================================
  180. ##### Initialization/de-initialization functions #####
  181. ===============================================================================
  182. [..] This section provides functions allowing to:
  183. @endverbatim
  184. * @{
  185. */
  186. /**
  187. * @brief Initializes the SDMMC according to the specified
  188. * parameters in the SDMMC_InitTypeDef and create the associated handle.
  189. * @param SDIOx: Pointer to SDMMC register base
  190. * @param Init: SDMMC initialization structure
  191. * @retval HAL status
  192. */
  193. HAL_StatusTypeDef SDIO_Init(SDIO_TypeDef *SDIOx, SDIO_InitTypeDef Init)
  194. {
  195. uint32_t tmpreg = 0U;
  196. /* Check the parameters */
  197. assert_param(IS_SDIO_ALL_INSTANCE(SDIOx));
  198. assert_param(IS_SDIO_CLOCK_EDGE(Init.ClockEdge));
  199. assert_param(IS_SDIO_CLOCK_BYPASS(Init.ClockBypass));
  200. assert_param(IS_SDIO_CLOCK_POWER_SAVE(Init.ClockPowerSave));
  201. assert_param(IS_SDIO_BUS_WIDE(Init.BusWide));
  202. assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(Init.HardwareFlowControl));
  203. assert_param(IS_SDIO_CLKDIV(Init.ClockDiv));
  204. /* Set SDMMC configuration parameters */
  205. tmpreg |= (Init.ClockEdge |\
  206. Init.ClockBypass |\
  207. Init.ClockPowerSave |\
  208. Init.BusWide |\
  209. Init.HardwareFlowControl |\
  210. Init.ClockDiv
  211. );
  212. /* Write to SDMMC CLKCR */
  213. MODIFY_REG(SDIOx->CLKCR, CLKCR_CLEAR_MASK, tmpreg);
  214. return HAL_OK;
  215. }
  216. /**
  217. * @}
  218. */
  219. /** @defgroup HAL_SDMMC_LL_Group2 IO operation functions
  220. * @brief Data transfers functions
  221. *
  222. @verbatim
  223. ===============================================================================
  224. ##### I/O operation functions #####
  225. ===============================================================================
  226. [..]
  227. This subsection provides a set of functions allowing to manage the SDMMC data
  228. transfers.
  229. @endverbatim
  230. * @{
  231. */
  232. /**
  233. * @brief Read data (word) from Rx FIFO in blocking mode (polling)
  234. * @param SDIOx: Pointer to SDMMC register base
  235. * @retval HAL status
  236. */
  237. uint32_t SDIO_ReadFIFO(SDIO_TypeDef *SDIOx)
  238. {
  239. /* Read data from Rx FIFO */
  240. return (SDIOx->FIFO);
  241. }
  242. /**
  243. * @brief Write data (word) to Tx FIFO in blocking mode (polling)
  244. * @param SDIOx: Pointer to SDMMC register base
  245. * @param pWriteData: pointer to data to write
  246. * @retval HAL status
  247. */
  248. HAL_StatusTypeDef SDIO_WriteFIFO(SDIO_TypeDef *SDIOx, uint32_t *pWriteData)
  249. {
  250. /* Write data to FIFO */
  251. SDIOx->FIFO = *pWriteData;
  252. return HAL_OK;
  253. }
  254. /**
  255. * @}
  256. */
  257. /** @defgroup HAL_SDMMC_LL_Group3 Peripheral Control functions
  258. * @brief management functions
  259. *
  260. @verbatim
  261. ===============================================================================
  262. ##### Peripheral Control functions #####
  263. ===============================================================================
  264. [..]
  265. This subsection provides a set of functions allowing to control the SDMMC data
  266. transfers.
  267. @endverbatim
  268. * @{
  269. */
  270. /**
  271. * @brief Set SDMMC Power state to ON.
  272. * @param SDIOx: Pointer to SDMMC register base
  273. * @retval HAL status
  274. */
  275. HAL_StatusTypeDef SDIO_PowerState_ON(SDIO_TypeDef *SDIOx)
  276. {
  277. /* Set power state to ON */
  278. SDIOx->POWER = SDIO_POWER_PWRCTRL;
  279. return HAL_OK;
  280. }
  281. /**
  282. * @brief Set SDMMC Power state to OFF.
  283. * @param SDIOx: Pointer to SDMMC register base
  284. * @retval HAL status
  285. */
  286. HAL_StatusTypeDef SDIO_PowerState_OFF(SDIO_TypeDef *SDIOx)
  287. {
  288. /* Set power state to OFF */
  289. SDIOx->POWER = 0x00000000U;
  290. return HAL_OK;
  291. }
  292. /**
  293. * @brief Get SDMMC Power state.
  294. * @param SDIOx: 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 SDIO_GetPowerState(SDIO_TypeDef *SDIOx)
  302. {
  303. return (SDIOx->POWER & SDIO_POWER_PWRCTRL);
  304. }
  305. /**
  306. * @brief Configure the SDMMC command path according to the specified parameters in
  307. * SDIO_CmdInitTypeDef structure and send the command
  308. * @param SDIOx: Pointer to SDMMC register base
  309. * @param Command: pointer to a SDIO_CmdInitTypeDef structure that contains
  310. * the configuration information for the SDMMC command
  311. * @retval HAL status
  312. */
  313. HAL_StatusTypeDef SDIO_SendCommand(SDIO_TypeDef *SDIOx, SDIO_CmdInitTypeDef *Command)
  314. {
  315. uint32_t tmpreg = 0U;
  316. /* Check the parameters */
  317. assert_param(IS_SDIO_CMD_INDEX(Command->CmdIndex));
  318. assert_param(IS_SDIO_RESPONSE(Command->Response));
  319. assert_param(IS_SDIO_WAIT(Command->WaitForInterrupt));
  320. assert_param(IS_SDIO_CPSM(Command->CPSM));
  321. /* Set the SDMMC Argument value */
  322. SDIOx->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(SDIOx->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 SDIOx: Pointer to SDMMC register base
  335. * @retval Command index of the last command response received
  336. */
  337. uint8_t SDIO_GetCommandResponse(SDIO_TypeDef *SDIOx)
  338. {
  339. return (uint8_t)(SDIOx->RESPCMD);
  340. }
  341. /**
  342. * @brief Return the response received from the card for the last command
  343. * @param SDIOx: 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 SDIO_RESP1: Response Register 1
  347. * @arg SDIO_RESP2: Response Register 2
  348. * @arg SDIO_RESP3: Response Register 3
  349. * @arg SDIO_RESP4: Response Register 4
  350. * @retval The Corresponding response register value
  351. */
  352. uint32_t SDIO_GetResponse(SDIO_TypeDef *SDIOx, uint32_t Response)
  353. {
  354. __IO uint32_t tmp = 0U;
  355. /* Check the parameters */
  356. assert_param(IS_SDIO_RESP(Response));
  357. /* Get the response */
  358. tmp = (uint32_t)&(SDIOx->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 SDIO_DataInitTypeDef.
  364. * @param SDIOx: Pointer to SDMMC register base
  365. * @param Data : pointer to a SDIO_DataInitTypeDef structure
  366. * that contains the configuration information for the SDMMC data.
  367. * @retval HAL status
  368. */
  369. HAL_StatusTypeDef SDIO_ConfigData(SDIO_TypeDef *SDIOx, SDIO_DataInitTypeDef* Data)
  370. {
  371. uint32_t tmpreg = 0U;
  372. /* Check the parameters */
  373. assert_param(IS_SDIO_DATA_LENGTH(Data->DataLength));
  374. assert_param(IS_SDIO_BLOCK_SIZE(Data->DataBlockSize));
  375. assert_param(IS_SDIO_TRANSFER_DIR(Data->TransferDir));
  376. assert_param(IS_SDIO_TRANSFER_MODE(Data->TransferMode));
  377. assert_param(IS_SDIO_DPSM(Data->DPSM));
  378. /* Set the SDMMC Data TimeOut value */
  379. SDIOx->DTIMER = Data->DataTimeOut;
  380. /* Set the SDMMC DataLength value */
  381. SDIOx->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(SDIOx->DCTRL, DCTRL_CLEAR_MASK, tmpreg);
  389. return HAL_OK;
  390. }
  391. /**
  392. * @brief Returns number of remaining data bytes to be transferred.
  393. * @param SDIOx: Pointer to SDMMC register base
  394. * @retval Number of remaining data bytes to be transferred
  395. */
  396. uint32_t SDIO_GetDataCounter(SDIO_TypeDef *SDIOx)
  397. {
  398. return (SDIOx->DCOUNT);
  399. }
  400. /**
  401. * @brief Get the FIFO data
  402. * @param SDIOx: Pointer to SDMMC register base
  403. * @retval Data received
  404. */
  405. uint32_t SDIO_GetFIFOCount(SDIO_TypeDef *SDIOx)
  406. {
  407. return (SDIOx->FIFO);
  408. }
  409. /**
  410. * @brief Sets one of the two options of inserting read wait interval.
  411. * @param SDIOx: Pointer to SDMMC register base
  412. * @param SDIO_ReadWaitMode: SDMMC Read Wait operation mode.
  413. * This parameter can be:
  414. * @arg SDIO_READ_WAIT_MODE_CLK: Read Wait control by stopping SDMMCCLK
  415. * @arg SDIO_READ_WAIT_MODE_DATA2: Read Wait control using SDMMC_DATA2
  416. * @retval None
  417. */
  418. HAL_StatusTypeDef SDIO_SetSDMMCReadWaitMode(SDIO_TypeDef *SDIOx, uint32_t SDIO_ReadWaitMode)
  419. {
  420. /* Check the parameters */
  421. assert_param(IS_SDIO_READWAIT_MODE(SDIO_ReadWaitMode));
  422. /* Set SDMMC read wait mode */
  423. MODIFY_REG(SDIOx->DCTRL, SDIO_DCTRL_RWMOD, SDIO_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 SDIOx: Pointer to SDMMC register base
  444. * @retval HAL status
  445. */
  446. uint32_t SDMMC_CmdBlockLength(SDIO_TypeDef *SDIOx, uint32_t BlockSize)
  447. {
  448. SDIO_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 = SDIO_RESPONSE_SHORT;
  454. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  455. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  456. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  457. /* Check for error conditions */
  458. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_SET_BLOCKLEN, SDIO_CMDTIMEOUT);
  459. return errorstate;
  460. }
  461. /**
  462. * @brief Send the Read Single Block command and check the response
  463. * @param SDIOx: Pointer to SDMMC register base
  464. * @retval HAL status
  465. */
  466. uint32_t SDMMC_CmdReadSingleBlock(SDIO_TypeDef *SDIOx, uint32_t ReadAdd)
  467. {
  468. SDIO_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 = SDIO_RESPONSE_SHORT;
  474. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  475. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  476. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  477. /* Check for error conditions */
  478. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_READ_SINGLE_BLOCK, SDIO_CMDTIMEOUT);
  479. return errorstate;
  480. }
  481. /**
  482. * @brief Send the Read Multi Block command and check the response
  483. * @param SDIOx: Pointer to SDIO register base
  484. * @retval HAL status
  485. */
  486. uint32_t SDMMC_CmdReadMultiBlock(SDIO_TypeDef *SDIOx, uint32_t ReadAdd)
  487. {
  488. SDIO_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 = SDIO_RESPONSE_SHORT;
  494. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  495. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  496. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  497. /* Check for error conditions */
  498. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_READ_MULT_BLOCK, SDIO_CMDTIMEOUT);
  499. return errorstate;
  500. }
  501. /**
  502. * @brief Send the Write Single Block command and check the response
  503. * @param SDIOx: Pointer to SDIO register base
  504. * @retval HAL status
  505. */
  506. uint32_t SDMMC_CmdWriteSingleBlock(SDIO_TypeDef *SDIOx, uint32_t WriteAdd)
  507. {
  508. SDIO_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 = SDIO_RESPONSE_SHORT;
  514. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  515. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  516. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  517. /* Check for error conditions */
  518. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_WRITE_SINGLE_BLOCK, SDIO_CMDTIMEOUT);
  519. return errorstate;
  520. }
  521. /**
  522. * @brief Send the Write Multi Block command and check the response
  523. * @param SDIOx: Pointer to SDIO register base
  524. * @retval HAL status
  525. */
  526. uint32_t SDMMC_CmdWriteMultiBlock(SDIO_TypeDef *SDIOx, uint32_t WriteAdd)
  527. {
  528. SDIO_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 = SDIO_RESPONSE_SHORT;
  534. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  535. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  536. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  537. /* Check for error conditions */
  538. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_WRITE_MULT_BLOCK, SDIO_CMDTIMEOUT);
  539. return errorstate;
  540. }
  541. /**
  542. * @brief Send the Start Address Erase command for SD and check the response
  543. * @param SDIOx: Pointer to SDIO register base
  544. * @retval HAL status
  545. */
  546. uint32_t SDMMC_CmdSDEraseStartAdd(SDIO_TypeDef *SDIOx, uint32_t StartAdd)
  547. {
  548. SDIO_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 = SDIO_RESPONSE_SHORT;
  554. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  555. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  556. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  557. /* Check for error conditions */
  558. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_SD_ERASE_GRP_START, SDIO_CMDTIMEOUT);
  559. return errorstate;
  560. }
  561. /**
  562. * @brief Send the End Address Erase command for SD and check the response
  563. * @param SDIOx: Pointer to SDIO register base
  564. * @retval HAL status
  565. */
  566. uint32_t SDMMC_CmdSDEraseEndAdd(SDIO_TypeDef *SDIOx, uint32_t EndAdd)
  567. {
  568. SDIO_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 = SDIO_RESPONSE_SHORT;
  574. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  575. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  576. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  577. /* Check for error conditions */
  578. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_SD_ERASE_GRP_END, SDIO_CMDTIMEOUT);
  579. return errorstate;
  580. }
  581. /**
  582. * @brief Send the Start Address Erase command and check the response
  583. * @param SDIOx: Pointer to SDIO register base
  584. * @retval HAL status
  585. */
  586. uint32_t SDMMC_CmdEraseStartAdd(SDIO_TypeDef *SDIOx, uint32_t StartAdd)
  587. {
  588. SDIO_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 = SDIO_RESPONSE_SHORT;
  594. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  595. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  596. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  597. /* Check for error conditions */
  598. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_ERASE_GRP_START, SDIO_CMDTIMEOUT);
  599. return errorstate;
  600. }
  601. /**
  602. * @brief Send the End Address Erase command and check the response
  603. * @param SDIOx: Pointer to SDIO register base
  604. * @retval HAL status
  605. */
  606. uint32_t SDMMC_CmdEraseEndAdd(SDIO_TypeDef *SDIOx, uint32_t EndAdd)
  607. {
  608. SDIO_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 = SDIO_RESPONSE_SHORT;
  614. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  615. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  616. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  617. /* Check for error conditions */
  618. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_ERASE_GRP_END, SDIO_CMDTIMEOUT);
  619. return errorstate;
  620. }
  621. /**
  622. * @brief Send the Erase command and check the response
  623. * @param SDIOx: Pointer to SDIO register base
  624. * @retval HAL status
  625. */
  626. uint32_t SDMMC_CmdErase(SDIO_TypeDef *SDIOx)
  627. {
  628. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  629. uint32_t errorstate = SDMMC_ERROR_NONE;
  630. /* Set Block Size for Card */
  631. sdmmc_cmdinit.Argument = 0U;
  632. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ERASE;
  633. sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  634. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  635. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  636. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  637. /* Check for error conditions */
  638. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_ERASE, SDIO_MAXERASETIMEOUT);
  639. return errorstate;
  640. }
  641. /**
  642. * @brief Send the Stop Transfer command and check the response.
  643. * @param SDIOx: Pointer to SDIO register base
  644. * @retval HAL status
  645. */
  646. uint32_t SDMMC_CmdStopTransfer(SDIO_TypeDef *SDIOx)
  647. {
  648. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  649. uint32_t errorstate = SDMMC_ERROR_NONE;
  650. /* Send CMD12 STOP_TRANSMISSION */
  651. sdmmc_cmdinit.Argument = 0U;
  652. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_STOP_TRANSMISSION;
  653. sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  654. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  655. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  656. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  657. /* Check for error conditions */
  658. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_STOP_TRANSMISSION, 100000000U);
  659. return errorstate;
  660. }
  661. /**
  662. * @brief Send the Select Deselect command and check the response.
  663. * @param SDIOx: Pointer to SDIO register base
  664. * @param addr: Address of the card to be selected
  665. * @retval HAL status
  666. */
  667. uint32_t SDMMC_CmdSelDesel(SDIO_TypeDef *SDIOx, uint64_t Addr)
  668. {
  669. SDIO_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 = SDIO_RESPONSE_SHORT;
  675. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  676. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  677. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  678. /* Check for error conditions */
  679. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_SEL_DESEL_CARD, SDIO_CMDTIMEOUT);
  680. return errorstate;
  681. }
  682. /**
  683. * @brief Send the Go Idle State command and check the response.
  684. * @param SDIOx: Pointer to SDIO register base
  685. * @retval HAL status
  686. */
  687. uint32_t SDMMC_CmdGoIdleState(SDIO_TypeDef *SDIOx)
  688. {
  689. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  690. uint32_t errorstate = SDMMC_ERROR_NONE;
  691. sdmmc_cmdinit.Argument = 0U;
  692. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_GO_IDLE_STATE;
  693. sdmmc_cmdinit.Response = SDIO_RESPONSE_NO;
  694. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  695. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  696. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  697. /* Check for error conditions */
  698. errorstate = SDMMC_GetCmdError(SDIOx);
  699. return errorstate;
  700. }
  701. /**
  702. * @brief Send the Operating Condition command and check the response.
  703. * @param SDIOx: Pointer to SDIO register base
  704. * @retval HAL status
  705. */
  706. uint32_t SDMMC_CmdOperCond(SDIO_TypeDef *SDIOx)
  707. {
  708. SDIO_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 = SDIO_RESPONSE_SHORT;
  718. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  719. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  720. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  721. /* Check for error conditions */
  722. errorstate = SDMMC_GetCmdResp7(SDIOx);
  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 SDIOx: Pointer to SDIO register base
  730. * @retval HAL status
  731. */
  732. uint32_t SDMMC_CmdAppCommand(SDIO_TypeDef *SDIOx, uint32_t Argument)
  733. {
  734. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  735. uint32_t errorstate = SDMMC_ERROR_NONE;
  736. sdmmc_cmdinit.Argument = (uint32_t)Argument;
  737. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_APP_CMD;
  738. sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  739. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  740. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  741. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  742. /* Check for error conditions */
  743. /* If there is a HAL_ERROR, it is a MMC card, else
  744. it is a SD card: SD card 2.0 (voltage range mismatch)
  745. or SD card 1.x */
  746. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_APP_CMD, SDIO_CMDTIMEOUT);
  747. return errorstate;
  748. }
  749. /**
  750. * @brief Send the command asking the accessed card to send its operating
  751. * condition register (OCR)
  752. * @param SDIOx: Pointer to SDIO register base
  753. * @retval HAL status
  754. */
  755. uint32_t SDMMC_CmdAppOperCommand(SDIO_TypeDef *SDIOx, uint32_t SdType)
  756. {
  757. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  758. uint32_t errorstate = SDMMC_ERROR_NONE;
  759. sdmmc_cmdinit.Argument = SDMMC_VOLTAGE_WINDOW_SD | SdType;
  760. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_OP_COND;
  761. sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  762. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  763. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  764. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  765. /* Check for error conditions */
  766. errorstate = SDMMC_GetCmdResp3(SDIOx);
  767. return errorstate;
  768. }
  769. /**
  770. * @brief Send the Bus Width command and check the response.
  771. * @param SDIOx: Pointer to SDIO register base
  772. * @retval HAL status
  773. */
  774. uint32_t SDMMC_CmdBusWidth(SDIO_TypeDef *SDIOx, uint32_t BusWidth)
  775. {
  776. SDIO_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 = SDIO_RESPONSE_SHORT;
  781. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  782. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  783. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  784. /* Check for error conditions */
  785. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_APP_SD_SET_BUSWIDTH, SDIO_CMDTIMEOUT);
  786. return errorstate;
  787. }
  788. /**
  789. * @brief Send the Send SCR command and check the response.
  790. * @param SDIOx: Pointer to SDMMC register base
  791. * @retval HAL status
  792. */
  793. uint32_t SDMMC_CmdSendSCR(SDIO_TypeDef *SDIOx)
  794. {
  795. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  796. uint32_t errorstate = SDMMC_ERROR_NONE;
  797. /* Send CMD51 SD_APP_SEND_SCR */
  798. sdmmc_cmdinit.Argument = 0U;
  799. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_SEND_SCR;
  800. sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  801. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  802. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  803. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  804. /* Check for error conditions */
  805. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_SD_APP_SEND_SCR, SDIO_CMDTIMEOUT);
  806. return errorstate;
  807. }
  808. /**
  809. * @brief Send the Send CID command and check the response.
  810. * @param SDIOx: Pointer to SDIO register base
  811. * @retval HAL status
  812. */
  813. uint32_t SDMMC_CmdSendCID(SDIO_TypeDef *SDIOx)
  814. {
  815. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  816. uint32_t errorstate = SDMMC_ERROR_NONE;
  817. /* Send CMD2 ALL_SEND_CID */
  818. sdmmc_cmdinit.Argument = 0U;
  819. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_ALL_SEND_CID;
  820. sdmmc_cmdinit.Response = SDIO_RESPONSE_LONG;
  821. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  822. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  823. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  824. /* Check for error conditions */
  825. errorstate = SDMMC_GetCmdResp2(SDIOx);
  826. return errorstate;
  827. }
  828. /**
  829. * @brief Send the Send CSD command and check the response.
  830. * @param SDIOx: Pointer to SDIO register base
  831. * @retval HAL status
  832. */
  833. uint32_t SDMMC_CmdSendCSD(SDIO_TypeDef *SDIOx, uint32_t Argument)
  834. {
  835. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  836. uint32_t errorstate = SDMMC_ERROR_NONE;
  837. /* Send CMD9 SEND_CSD */
  838. sdmmc_cmdinit.Argument = (uint32_t)Argument;
  839. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_CSD;
  840. sdmmc_cmdinit.Response = SDIO_RESPONSE_LONG;
  841. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  842. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  843. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  844. /* Check for error conditions */
  845. errorstate = SDMMC_GetCmdResp2(SDIOx);
  846. return errorstate;
  847. }
  848. /**
  849. * @brief Send the Send CSD command and check the response.
  850. * @param SDIOx: Pointer to SDIO register base
  851. * @retval HAL status
  852. */
  853. uint32_t SDMMC_CmdSetRelAdd(SDIO_TypeDef *SDIOx, uint16_t *pRCA)
  854. {
  855. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  856. uint32_t errorstate = SDMMC_ERROR_NONE;
  857. /* Send CMD3 SD_CMD_SET_REL_ADDR */
  858. sdmmc_cmdinit.Argument = 0U;
  859. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SET_REL_ADDR;
  860. sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  861. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  862. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  863. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  864. /* Check for error conditions */
  865. errorstate = SDMMC_GetCmdResp6(SDIOx, SDMMC_CMD_SET_REL_ADDR, pRCA);
  866. return errorstate;
  867. }
  868. /**
  869. * @brief Send the Status command and check the response.
  870. * @param SDIOx: Pointer to SDIO register base
  871. * @retval HAL status
  872. */
  873. uint32_t SDMMC_CmdSendStatus(SDIO_TypeDef *SDIOx, uint32_t Argument)
  874. {
  875. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  876. uint32_t errorstate = SDMMC_ERROR_NONE;
  877. sdmmc_cmdinit.Argument = (uint32_t)Argument;
  878. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_STATUS;
  879. sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  880. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  881. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  882. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  883. /* Check for error conditions */
  884. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_SEND_STATUS, SDIO_CMDTIMEOUT);
  885. return errorstate;
  886. }
  887. /**
  888. * @brief Send the Status register command and check the response.
  889. * @param SDIOx: Pointer to SDIO register base
  890. * @retval HAL status
  891. */
  892. uint32_t SDMMC_CmdStatusRegister(SDIO_TypeDef *SDIOx)
  893. {
  894. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  895. uint32_t errorstate = SDMMC_ERROR_NONE;
  896. sdmmc_cmdinit.Argument = 0U;
  897. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SD_APP_STATUS;
  898. sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  899. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  900. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  901. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  902. /* Check for error conditions */
  903. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_SD_APP_STATUS, SDIO_CMDTIMEOUT);
  904. return errorstate;
  905. }
  906. /**
  907. * @brief Sends host capacity support information and activates the card's
  908. * initialization process. Send SDMMC_CMD_SEND_OP_COND command
  909. * @param SDIOx: Pointer to SDIO register base
  910. * @parame Argument: Argument used for the command
  911. * @retval HAL status
  912. */
  913. uint32_t SDMMC_CmdOpCondition(SDIO_TypeDef *SDIOx, uint32_t Argument)
  914. {
  915. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  916. uint32_t errorstate = SDMMC_ERROR_NONE;
  917. sdmmc_cmdinit.Argument = Argument;
  918. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_SEND_OP_COND;
  919. sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  920. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  921. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  922. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  923. /* Check for error conditions */
  924. errorstate = SDMMC_GetCmdResp3(SDIOx);
  925. return errorstate;
  926. }
  927. /**
  928. * @brief Checks switchable function and switch card function. SDMMC_CMD_HS_SWITCH comand
  929. * @param SDIOx: Pointer to SDIO register base
  930. * @parame Argument: Argument used for the command
  931. * @retval HAL status
  932. */
  933. uint32_t SDMMC_CmdSwitch(SDIO_TypeDef *SDIOx, uint32_t Argument)
  934. {
  935. SDIO_CmdInitTypeDef sdmmc_cmdinit;
  936. uint32_t errorstate = SDMMC_ERROR_NONE;
  937. sdmmc_cmdinit.Argument = Argument;
  938. sdmmc_cmdinit.CmdIndex = SDMMC_CMD_HS_SWITCH;
  939. sdmmc_cmdinit.Response = SDIO_RESPONSE_SHORT;
  940. sdmmc_cmdinit.WaitForInterrupt = SDIO_WAIT_NO;
  941. sdmmc_cmdinit.CPSM = SDIO_CPSM_ENABLE;
  942. SDIO_SendCommand(SDIOx, &sdmmc_cmdinit);
  943. /* Check for error conditions */
  944. errorstate = SDMMC_GetCmdResp1(SDIOx, SDMMC_CMD_HS_SWITCH, SDIO_CMDTIMEOUT);
  945. return errorstate;
  946. }
  947. /**
  948. * @}
  949. */
  950. /* Private function ----------------------------------------------------------*/
  951. /** @addtogroup SD_Private_Functions
  952. * @{
  953. */
  954. /**
  955. * @brief Checks for error conditions for CMD0.
  956. * @param hsd: SD handle
  957. * @retval SD Card error state
  958. */
  959. static uint32_t SDMMC_GetCmdError(SDIO_TypeDef *SDIOx)
  960. {
  961. /* 8 is the number of required instructions cycles for the below loop statement.
  962. The SDMMC_CMDTIMEOUT is expressed in ms */
  963. register uint32_t count = SDIO_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
  964. do
  965. {
  966. if (count-- == 0U)
  967. {
  968. return SDMMC_ERROR_TIMEOUT;
  969. }
  970. }while(!__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CMDSENT));
  971. /* Clear all the static flags */
  972. __SDIO_CLEAR_FLAG(SDIOx, SDIO_STATIC_FLAGS);
  973. return SDMMC_ERROR_NONE;
  974. }
  975. /**
  976. * @brief Checks for error conditions for R1 response.
  977. * @param hsd: SD handle
  978. * @param SD_CMD: The sent command index
  979. * @retval SD Card error state
  980. */
  981. static uint32_t SDMMC_GetCmdResp1(SDIO_TypeDef *SDIOx, uint8_t SD_CMD, uint32_t Timeout)
  982. {
  983. uint32_t response_r1;
  984. /* 8 is the number of required instructions cycles for the below loop statement.
  985. The Timeout is expressed in ms */
  986. register uint32_t count = Timeout * (SystemCoreClock / 8U /1000U);
  987. do
  988. {
  989. if (count-- == 0U)
  990. {
  991. return SDMMC_ERROR_TIMEOUT;
  992. }
  993. }while(!__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT));
  994. if(__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CTIMEOUT))
  995. {
  996. __SDIO_CLEAR_FLAG(SDIOx, SDIO_FLAG_CTIMEOUT);
  997. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  998. }
  999. else if(__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL))
  1000. {
  1001. __SDIO_CLEAR_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL);
  1002. return SDMMC_ERROR_CMD_CRC_FAIL;
  1003. }
  1004. /* Check response received is of desired command */
  1005. if(SDIO_GetCommandResponse(SDIOx) != SD_CMD)
  1006. {
  1007. return SDMMC_ERROR_CMD_CRC_FAIL;
  1008. }
  1009. /* Clear all the static flags */
  1010. __SDIO_CLEAR_FLAG(SDIOx, SDIO_STATIC_FLAGS);
  1011. /* We have received response, retrieve it for analysis */
  1012. response_r1 = SDIO_GetResponse(SDIOx, SDIO_RESP1);
  1013. if((response_r1 & SDMMC_OCR_ERRORBITS) == SDMMC_ALLZERO)
  1014. {
  1015. return SDMMC_ERROR_NONE;
  1016. }
  1017. else if((response_r1 & SDMMC_OCR_ADDR_OUT_OF_RANGE) == SDMMC_OCR_ADDR_OUT_OF_RANGE)
  1018. {
  1019. return SDMMC_ERROR_ADDR_OUT_OF_RANGE;
  1020. }
  1021. else if((response_r1 & SDMMC_OCR_ADDR_MISALIGNED) == SDMMC_OCR_ADDR_MISALIGNED)
  1022. {
  1023. return SDMMC_ERROR_ADDR_MISALIGNED;
  1024. }
  1025. else if((response_r1 & SDMMC_OCR_BLOCK_LEN_ERR) == SDMMC_OCR_BLOCK_LEN_ERR)
  1026. {
  1027. return SDMMC_ERROR_BLOCK_LEN_ERR;
  1028. }
  1029. else if((response_r1 & SDMMC_OCR_ERASE_SEQ_ERR) == SDMMC_OCR_ERASE_SEQ_ERR)
  1030. {
  1031. return SDMMC_ERROR_ERASE_SEQ_ERR;
  1032. }
  1033. else if((response_r1 & SDMMC_OCR_BAD_ERASE_PARAM) == SDMMC_OCR_BAD_ERASE_PARAM)
  1034. {
  1035. return SDMMC_ERROR_BAD_ERASE_PARAM;
  1036. }
  1037. else if((response_r1 & SDMMC_OCR_WRITE_PROT_VIOLATION) == SDMMC_OCR_WRITE_PROT_VIOLATION)
  1038. {
  1039. return SDMMC_ERROR_WRITE_PROT_VIOLATION;
  1040. }
  1041. else if((response_r1 & SDMMC_OCR_LOCK_UNLOCK_FAILED) == SDMMC_OCR_LOCK_UNLOCK_FAILED)
  1042. {
  1043. return SDMMC_ERROR_LOCK_UNLOCK_FAILED;
  1044. }
  1045. else if((response_r1 & SDMMC_OCR_COM_CRC_FAILED) == SDMMC_OCR_COM_CRC_FAILED)
  1046. {
  1047. return SDMMC_ERROR_COM_CRC_FAILED;
  1048. }
  1049. else if((response_r1 & SDMMC_OCR_ILLEGAL_CMD) == SDMMC_OCR_ILLEGAL_CMD)
  1050. {
  1051. return SDMMC_ERROR_ILLEGAL_CMD;
  1052. }
  1053. else if((response_r1 & SDMMC_OCR_CARD_ECC_FAILED) == SDMMC_OCR_CARD_ECC_FAILED)
  1054. {
  1055. return SDMMC_ERROR_CARD_ECC_FAILED;
  1056. }
  1057. else if((response_r1 & SDMMC_OCR_CC_ERROR) == SDMMC_OCR_CC_ERROR)
  1058. {
  1059. return SDMMC_ERROR_CC_ERR;
  1060. }
  1061. else if((response_r1 & SDMMC_OCR_STREAM_READ_UNDERRUN) == SDMMC_OCR_STREAM_READ_UNDERRUN)
  1062. {
  1063. return SDMMC_ERROR_STREAM_READ_UNDERRUN;
  1064. }
  1065. else if((response_r1 & SDMMC_OCR_STREAM_WRITE_OVERRUN) == SDMMC_OCR_STREAM_WRITE_OVERRUN)
  1066. {
  1067. return SDMMC_ERROR_STREAM_WRITE_OVERRUN;
  1068. }
  1069. else if((response_r1 & SDMMC_OCR_CID_CSD_OVERWRITE) == SDMMC_OCR_CID_CSD_OVERWRITE)
  1070. {
  1071. return SDMMC_ERROR_CID_CSD_OVERWRITE;
  1072. }
  1073. else if((response_r1 & SDMMC_OCR_WP_ERASE_SKIP) == SDMMC_OCR_WP_ERASE_SKIP)
  1074. {
  1075. return SDMMC_ERROR_WP_ERASE_SKIP;
  1076. }
  1077. else if((response_r1 & SDMMC_OCR_CARD_ECC_DISABLED) == SDMMC_OCR_CARD_ECC_DISABLED)
  1078. {
  1079. return SDMMC_ERROR_CARD_ECC_DISABLED;
  1080. }
  1081. else if((response_r1 & SDMMC_OCR_ERASE_RESET) == SDMMC_OCR_ERASE_RESET)
  1082. {
  1083. return SDMMC_ERROR_ERASE_RESET;
  1084. }
  1085. else if((response_r1 & SDMMC_OCR_AKE_SEQ_ERROR) == SDMMC_OCR_AKE_SEQ_ERROR)
  1086. {
  1087. return SDMMC_ERROR_AKE_SEQ_ERR;
  1088. }
  1089. else
  1090. {
  1091. return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  1092. }
  1093. }
  1094. /**
  1095. * @brief Checks for error conditions for R2 (CID or CSD) response.
  1096. * @param hsd: SD handle
  1097. * @retval SD Card error state
  1098. */
  1099. static uint32_t SDMMC_GetCmdResp2(SDIO_TypeDef *SDIOx)
  1100. {
  1101. /* 8 is the number of required instructions cycles for the below loop statement.
  1102. The SDMMC_CMDTIMEOUT is expressed in ms */
  1103. register uint32_t count = SDIO_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
  1104. do
  1105. {
  1106. if (count-- == 0U)
  1107. {
  1108. return SDMMC_ERROR_TIMEOUT;
  1109. }
  1110. }while(!__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT));
  1111. if (__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CTIMEOUT))
  1112. {
  1113. __SDIO_CLEAR_FLAG(SDIOx, SDIO_FLAG_CTIMEOUT);
  1114. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1115. }
  1116. else if (__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL))
  1117. {
  1118. __SDIO_CLEAR_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL);
  1119. return SDMMC_ERROR_CMD_CRC_FAIL;
  1120. }
  1121. else
  1122. {
  1123. /* No error flag set */
  1124. /* Clear all the static flags */
  1125. __SDIO_CLEAR_FLAG(SDIOx, SDIO_STATIC_FLAGS);
  1126. }
  1127. return SDMMC_ERROR_NONE;
  1128. }
  1129. /**
  1130. * @brief Checks for error conditions for R3 (OCR) response.
  1131. * @param hsd: SD handle
  1132. * @retval SD Card error state
  1133. */
  1134. static uint32_t SDMMC_GetCmdResp3(SDIO_TypeDef *SDIOx)
  1135. {
  1136. /* 8 is the number of required instructions cycles for the below loop statement.
  1137. The SDMMC_CMDTIMEOUT is expressed in ms */
  1138. register uint32_t count = SDIO_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
  1139. do
  1140. {
  1141. if (count-- == 0U)
  1142. {
  1143. return SDMMC_ERROR_TIMEOUT;
  1144. }
  1145. }while(!__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT));
  1146. if(__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CTIMEOUT))
  1147. {
  1148. __SDIO_CLEAR_FLAG(SDIOx, SDIO_FLAG_CTIMEOUT);
  1149. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1150. }
  1151. else
  1152. {
  1153. /* Clear all the static flags */
  1154. __SDIO_CLEAR_FLAG(SDIOx, SDIO_STATIC_FLAGS);
  1155. }
  1156. return SDMMC_ERROR_NONE;
  1157. }
  1158. /**
  1159. * @brief Checks for error conditions for R6 (RCA) response.
  1160. * @param hsd: SD handle
  1161. * @param SD_CMD: The sent command index
  1162. * @param pRCA: Pointer to the variable that will contain the SD card relative
  1163. * address RCA
  1164. * @retval SD Card error state
  1165. */
  1166. static uint32_t SDMMC_GetCmdResp6(SDIO_TypeDef *SDIOx, uint8_t SD_CMD, uint16_t *pRCA)
  1167. {
  1168. uint32_t response_r1;
  1169. /* 8 is the number of required instructions cycles for the below loop statement.
  1170. The SDMMC_CMDTIMEOUT is expressed in ms */
  1171. register uint32_t count = SDIO_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
  1172. do
  1173. {
  1174. if (count-- == 0U)
  1175. {
  1176. return SDMMC_ERROR_TIMEOUT;
  1177. }
  1178. }while(!__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT));
  1179. if(__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CTIMEOUT))
  1180. {
  1181. __SDIO_CLEAR_FLAG(SDIOx, SDIO_FLAG_CTIMEOUT);
  1182. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1183. }
  1184. else if(__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL))
  1185. {
  1186. __SDIO_CLEAR_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL);
  1187. return SDMMC_ERROR_CMD_CRC_FAIL;
  1188. }
  1189. /* Check response received is of desired command */
  1190. if(SDIO_GetCommandResponse(SDIOx) != SD_CMD)
  1191. {
  1192. return SDMMC_ERROR_CMD_CRC_FAIL;
  1193. }
  1194. /* Clear all the static flags */
  1195. __SDIO_CLEAR_FLAG(SDIOx, SDIO_STATIC_FLAGS);
  1196. /* We have received response, retrieve it. */
  1197. response_r1 = SDIO_GetResponse(SDIOx, SDIO_RESP1);
  1198. if((response_r1 & (SDMMC_R6_GENERAL_UNKNOWN_ERROR | SDMMC_R6_ILLEGAL_CMD | SDMMC_R6_COM_CRC_FAILED)) == SDMMC_ALLZERO)
  1199. {
  1200. *pRCA = (uint16_t) (response_r1 >> 16);
  1201. return SDMMC_ERROR_NONE;
  1202. }
  1203. else if((response_r1 & SDMMC_R6_ILLEGAL_CMD) == SDMMC_R6_ILLEGAL_CMD)
  1204. {
  1205. return SDMMC_ERROR_ILLEGAL_CMD;
  1206. }
  1207. else if((response_r1 & SDMMC_R6_COM_CRC_FAILED) == SDMMC_R6_COM_CRC_FAILED)
  1208. {
  1209. return SDMMC_ERROR_COM_CRC_FAILED;
  1210. }
  1211. else
  1212. {
  1213. return SDMMC_ERROR_GENERAL_UNKNOWN_ERR;
  1214. }
  1215. }
  1216. /**
  1217. * @brief Checks for error conditions for R7 response.
  1218. * @param hsd: SD handle
  1219. * @retval SD Card error state
  1220. */
  1221. static uint32_t SDMMC_GetCmdResp7(SDIO_TypeDef *SDIOx)
  1222. {
  1223. /* 8 is the number of required instructions cycles for the below loop statement.
  1224. The SDIO_CMDTIMEOUT is expressed in ms */
  1225. register uint32_t count = SDIO_CMDTIMEOUT * (SystemCoreClock / 8U /1000U);
  1226. do
  1227. {
  1228. if (count-- == 0U)
  1229. {
  1230. return SDMMC_ERROR_TIMEOUT;
  1231. }
  1232. }while(!__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT));
  1233. if(__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CTIMEOUT))
  1234. {
  1235. /* Card is SD V2.0 compliant */
  1236. __SDIO_CLEAR_FLAG(SDIOx, SDIO_FLAG_CMDREND);
  1237. return SDMMC_ERROR_CMD_RSP_TIMEOUT;
  1238. }
  1239. if(__SDIO_GET_FLAG(SDIOx, SDIO_FLAG_CMDREND))
  1240. {
  1241. /* Card is SD V2.0 compliant */
  1242. __SDIO_CLEAR_FLAG(SDIOx, SDIO_FLAG_CMDREND);
  1243. }
  1244. return SDMMC_ERROR_NONE;
  1245. }
  1246. /**
  1247. * @}
  1248. */
  1249. /**
  1250. * @}
  1251. */
  1252. #endif /* STM32F103xE || STM32F103xG */
  1253. #endif /* (HAL_SD_MODULE_ENABLED) || (HAL_MMC_MODULE_ENABLED) */
  1254. /**
  1255. * @}
  1256. */
  1257. /**
  1258. * @}
  1259. */
  1260. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/