stm32l4xx_ll_sdmmc.c 55 KB

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