stm32f4xx_ll_sdmmc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_ll_sdmmc.c
  4. * @author MCD Application Team
  5. * @version V1.6.0
  6. * @date 04-November-2016
  7. * @brief SDMMC Low Layer HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the SDMMC peripheral:
  11. * + Initialization/de-initialization functions
  12. * + I/O operation functions
  13. * + Peripheral Control functions
  14. * + Peripheral State functions
  15. *
  16. @verbatim
  17. ==============================================================================
  18. ##### SDMMC peripheral features #####
  19. ==============================================================================
  20. [..] The SD/SDIO MMC card host interface (SDIO) provides an interface between the APB2
  21. peripheral bus and MultiMedia cards (MMCs), SD memory cards, SDIO cards and CE-ATA
  22. devices.
  23. [..] The SDIO 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 SDIO peripheral.
  39. According to the device used (SD card/ MMC card / SDIO card ...), a set of APIs
  40. is used in the device's driver to perform SDIO 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 clock (SDIOCLK = 48 MHz) is coming from a specific output of PLL
  45. (PLL48CLK). Before start working with SDIO peripheral make sure that the
  46. PLL is well configured.
  47. The SDIO peripheral uses two clock signals:
  48. (++) SDIO adapter clock (SDIOCLK = 48 MHz)
  49. (++) APB2 bus clock (PCLK2)
  50. -@@- PCLK2 and SDIO_CK clock frequencies must respect the following condition:
  51. Frequency(PCLK2) >= (3 / 8 x Frequency(SDIO_CK))
  52. (+) Enable/Disable peripheral clock using RCC peripheral macros related to SDIO
  53. peripheral.
  54. (+) Enable the Power ON State using the SDIO_PowerState_ON(SDIOx)
  55. function and disable it using the function SDIO_PowerState_OFF(SDIOx).
  56. (+) Enable/Disable the clock using the __SDIO_ENABLE()/__SDIO_DISABLE() macros.
  57. (+) Enable/Disable the peripheral interrupts using the macros __SDIO_ENABLE_IT(hsdio, IT)
  58. and __SDIO_DISABLE_IT(hsdio, IT) if you need to use interrupt mode.
  59. (+) When using the DMA mode
  60. (++) Configure the DMA in the MSP layer of the external device
  61. (++) Active the needed channel Request
  62. (++) Enable the DMA using __SDIO_DMA_ENABLE() macro or Disable it using the macro
  63. __SDIO_DMA_DISABLE().
  64. (+) To control the CPSM (Command Path State Machine) and send
  65. commands to the card use the SDIO_SendCommand(SDIOx),
  66. SDIO_GetCommandResponse() and SDIO_GetResponse() functions. First, user has
  67. to fill the command structure (pointer to SDIO_CmdInitTypeDef) according
  68. to the selected command to be sent.
  69. The parameters that should be filled are:
  70. (++) Command Argument
  71. (++) Command Index
  72. (++) Command Response type
  73. (++) Command Wait
  74. (++) CPSM Status (Enable or Disable).
  75. -@@- To check if the command is well received, read the SDIO_CMDRESP
  76. register using the SDIO_GetCommandResponse().
  77. The SDIO responses registers (SDIO_RESP1 to SDIO_RESP2), use the
  78. SDIO_GetResponse() function.
  79. (+) To control the DPSM (Data Path State Machine) and send/receive
  80. data to/from the card use the SDIO_DataConfig(), SDIO_GetDataCounter(),
  81. SDIO_ReadFIFO(), DIO_WriteFIFO() and SDIO_GetFIFOCount() functions.
  82. *** Read Operations ***
  83. =======================
  84. [..]
  85. (#) First, user has to fill the data structure (pointer to
  86. SDIO_DataInitTypeDef) according to the selected data type to be received.
  87. The parameters that should be filled are:
  88. (++) Data Timeout
  89. (++) Data Length
  90. (++) Data Block size
  91. (++) Data Transfer direction: should be from card (To SDIO)
  92. (++) Data Transfer mode
  93. (++) DPSM Status (Enable or Disable)
  94. (#) Configure the SDIO resources to receive the data from the card
  95. according to selected transfer mode (Refer to Step 8, 9 and 10).
  96. (#) Send the selected Read command (refer to step 11).
  97. (#) Use the SDIO flags/interrupts to check the transfer status.
  98. *** Write Operations ***
  99. ========================
  100. [..]
  101. (#) First, user has to fill the data structure (pointer to
  102. SDIO_DataInitTypeDef) according to the selected data type to be received.
  103. The parameters that should be filled are:
  104. (++) Data Timeout
  105. (++) Data Length
  106. (++) Data Block size
  107. (++) Data Transfer direction: should be to card (To CARD)
  108. (++) Data Transfer mode
  109. (++) DPSM Status (Enable or Disable)
  110. (#) Configure the SDIO resources to send the data to the card according to
  111. selected transfer mode.
  112. (#) Send the selected Write command.
  113. (#) Use the SDIO flags/interrupts to check the transfer status.
  114. @endverbatim
  115. ******************************************************************************
  116. * @attention
  117. *
  118. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  119. *
  120. * Redistribution and use in source and binary forms, with or without modification,
  121. * are permitted provided that the following conditions are met:
  122. * 1. Redistributions of source code must retain the above copyright notice,
  123. * this list of conditions and the following disclaimer.
  124. * 2. Redistributions in binary form must reproduce the above copyright notice,
  125. * this list of conditions and the following disclaimer in the documentation
  126. * and/or other materials provided with the distribution.
  127. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  128. * may be used to endorse or promote products derived from this software
  129. * without specific prior written permission.
  130. *
  131. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  132. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  133. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  134. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  135. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  136. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  137. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  138. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  139. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  140. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  141. *
  142. ******************************************************************************
  143. */
  144. /* Includes ------------------------------------------------------------------*/
  145. #include "stm32f4xx_hal.h"
  146. /** @addtogroup STM32F4xx_HAL_Driver
  147. * @{
  148. */
  149. /** @defgroup SDMMC_LL SDMMC Low Layer
  150. * @brief Low layer module for SD and MMC driver
  151. * @{
  152. */
  153. #if defined(HAL_SD_MODULE_ENABLED) || defined(HAL_MMC_MODULE_ENABLED)
  154. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \
  155. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
  156. defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \
  157. defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
  158. defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  159. /* Private typedef -----------------------------------------------------------*/
  160. /* Private define ------------------------------------------------------------*/
  161. /* Private macro -------------------------------------------------------------*/
  162. /* Private variables ---------------------------------------------------------*/
  163. /* Private function prototypes -----------------------------------------------*/
  164. /* Private functions ---------------------------------------------------------*/
  165. /** @defgroup SDMMC_LL_Exported_Functions SDMMC_LL Exported Functions
  166. * @{
  167. */
  168. /** @defgroup HAL_SDMMC_LL_Group1 Initialization/de-initialization functions
  169. * @brief Initialization and Configuration functions
  170. *
  171. @verbatim
  172. ===============================================================================
  173. ##### Initialization/de-initialization functions #####
  174. ===============================================================================
  175. [..] This section provides functions allowing to:
  176. @endverbatim
  177. * @{
  178. */
  179. /**
  180. * @brief Initializes the SDIO according to the specified
  181. * parameters in the SDIO_InitTypeDef and create the associated handle.
  182. * @param SDIOx: Pointer to SDIO register base
  183. * @param Init: SDIO initialization structure
  184. * @retval HAL status
  185. */
  186. HAL_StatusTypeDef SDIO_Init(SDIO_TypeDef *SDIOx, SDIO_InitTypeDef Init)
  187. {
  188. uint32_t tmpreg = 0U;
  189. /* Check the parameters */
  190. assert_param(IS_SDIO_ALL_INSTANCE(SDIOx));
  191. assert_param(IS_SDIO_CLOCK_EDGE(Init.ClockEdge));
  192. assert_param(IS_SDIO_CLOCK_BYPASS(Init.ClockBypass));
  193. assert_param(IS_SDIO_CLOCK_POWER_SAVE(Init.ClockPowerSave));
  194. assert_param(IS_SDIO_BUS_WIDE(Init.BusWide));
  195. assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(Init.HardwareFlowControl));
  196. assert_param(IS_SDIO_CLKDIV(Init.ClockDiv));
  197. /* Set SDIO configuration parameters */
  198. tmpreg |= (Init.ClockEdge |\
  199. Init.ClockBypass |\
  200. Init.ClockPowerSave |\
  201. Init.BusWide |\
  202. Init.HardwareFlowControl |\
  203. Init.ClockDiv
  204. );
  205. /* Write to SDIO CLKCR */
  206. MODIFY_REG(SDIOx->CLKCR, CLKCR_CLEAR_MASK, tmpreg);
  207. return HAL_OK;
  208. }
  209. /**
  210. * @}
  211. */
  212. /** @defgroup HAL_SDMMC_LL_Group2 I/O operation functions
  213. * @brief Data transfers functions
  214. *
  215. @verbatim
  216. ===============================================================================
  217. ##### I/O operation functions #####
  218. ===============================================================================
  219. [..]
  220. This subsection provides a set of functions allowing to manage the SDIO data
  221. transfers.
  222. @endverbatim
  223. * @{
  224. */
  225. /**
  226. * @brief Read data (word) from Rx FIFO in blocking mode (polling)
  227. * @param SDIOx: Pointer to SDIO register base
  228. * @retval HAL status
  229. */
  230. uint32_t SDIO_ReadFIFO(SDIO_TypeDef *SDIOx)
  231. {
  232. /* Read data from Rx FIFO */
  233. return (SDIOx->FIFO);
  234. }
  235. /**
  236. * @brief Write data (word) to Tx FIFO in blocking mode (polling)
  237. * @param SDIOx: Pointer to SDIO register base
  238. * @param pWriteData: pointer to data to write
  239. * @retval HAL status
  240. */
  241. HAL_StatusTypeDef SDIO_WriteFIFO(SDIO_TypeDef *SDIOx, uint32_t *pWriteData)
  242. {
  243. /* Write data to FIFO */
  244. SDIOx->FIFO = *pWriteData;
  245. return HAL_OK;
  246. }
  247. /**
  248. * @}
  249. */
  250. /** @defgroup HAL_SDMMC_LL_Group3 Peripheral Control functions
  251. * @brief management functions
  252. *
  253. @verbatim
  254. ===============================================================================
  255. ##### Peripheral Control functions #####
  256. ===============================================================================
  257. [..]
  258. This subsection provides a set of functions allowing to control the SDIO data
  259. transfers.
  260. @endverbatim
  261. * @{
  262. */
  263. /**
  264. * @brief Set SDIO Power state to ON.
  265. * @param SDIOx: Pointer to SDIO register base
  266. * @retval HAL status
  267. */
  268. HAL_StatusTypeDef SDIO_PowerState_ON(SDIO_TypeDef *SDIOx)
  269. {
  270. /* Set power state to ON */
  271. SDIOx->POWER = SDIO_POWER_PWRCTRL;
  272. return HAL_OK;
  273. }
  274. /**
  275. * @brief Set SDIO Power state to OFF.
  276. * @param SDIOx: Pointer to SDIO register base
  277. * @retval HAL status
  278. */
  279. HAL_StatusTypeDef SDIO_PowerState_OFF(SDIO_TypeDef *SDIOx)
  280. {
  281. /* Set power state to OFF */
  282. SDIOx->POWER = (uint32_t)0x00000000U;
  283. return HAL_OK;
  284. }
  285. /**
  286. * @brief Get SDIO Power state.
  287. * @param SDIOx: Pointer to SDIO register base
  288. * @retval Power status of the controller. The returned value can be one of the
  289. * following values:
  290. * - 0x00: Power OFF
  291. * - 0x02: Power UP
  292. * - 0x03: Power ON
  293. */
  294. uint32_t SDIO_GetPowerState(SDIO_TypeDef *SDIOx)
  295. {
  296. return (SDIOx->POWER & SDIO_POWER_PWRCTRL);
  297. }
  298. /**
  299. * @brief Configure the SDIO command path according to the specified parameters in
  300. * SDIO_CmdInitTypeDef structure and send the command
  301. * @param SDIOx: Pointer to SDIO register base
  302. * @param SDIO_CmdInitStruct: pointer to a SDIO_CmdInitTypeDef structure that contains
  303. * the configuration information for the SDIO command
  304. * @retval HAL status
  305. */
  306. HAL_StatusTypeDef SDIO_SendCommand(SDIO_TypeDef *SDIOx, SDIO_CmdInitTypeDef *SDIO_CmdInitStruct)
  307. {
  308. uint32_t tmpreg = 0U;
  309. /* Check the parameters */
  310. assert_param(IS_SDIO_CMD_INDEX(SDIO_CmdInitStruct->CmdIndex));
  311. assert_param(IS_SDIO_RESPONSE(SDIO_CmdInitStruct->Response));
  312. assert_param(IS_SDIO_WAIT(SDIO_CmdInitStruct->WaitForInterrupt));
  313. assert_param(IS_SDIO_CPSM(SDIO_CmdInitStruct->CPSM));
  314. /* Set the SDIO Argument value */
  315. SDIOx->ARG = SDIO_CmdInitStruct->Argument;
  316. /* Set SDIO command parameters */
  317. tmpreg |= (uint32_t)(SDIO_CmdInitStruct->CmdIndex |\
  318. SDIO_CmdInitStruct->Response |\
  319. SDIO_CmdInitStruct->WaitForInterrupt |\
  320. SDIO_CmdInitStruct->CPSM);
  321. /* Write to SDIO CMD register */
  322. MODIFY_REG(SDIOx->CMD, CMD_CLEAR_MASK, tmpreg);
  323. return HAL_OK;
  324. }
  325. /**
  326. * @brief Return the command index of last command for which response received
  327. * @param SDIOx: Pointer to SDIO register base
  328. * @retval Command index of the last command response received
  329. */
  330. uint8_t SDIO_GetCommandResponse(SDIO_TypeDef *SDIOx)
  331. {
  332. return (uint8_t)(SDIOx->RESPCMD);
  333. }
  334. /**
  335. * @brief Return the response received from the card for the last command
  336. * @param SDIO_RESP: Specifies the SDIO response register.
  337. * This parameter can be one of the following values:
  338. * @arg SDIO_RESP1: Response Register 1
  339. * @arg SDIO_RESP2: Response Register 2
  340. * @arg SDIO_RESP3: Response Register 3
  341. * @arg SDIO_RESP4: Response Register 4
  342. * @retval The Corresponding response register value
  343. */
  344. uint32_t SDIO_GetResponse(uint32_t SDIO_RESP)
  345. {
  346. __IO uint32_t tmp = 0U;
  347. /* Check the parameters */
  348. assert_param(IS_SDIO_RESP(SDIO_RESP));
  349. /* Get the response */
  350. tmp = SDIO_RESP_ADDR + SDIO_RESP;
  351. return (*(__IO uint32_t *) tmp);
  352. }
  353. /**
  354. * @brief Configure the SDIO data path according to the specified
  355. * parameters in the SDIO_DataInitTypeDef.
  356. * @param SDIOx: Pointer to SDIO register base
  357. * @param SDIO_DataInitStruct : pointer to a SDIO_DataInitTypeDef structure
  358. * that contains the configuration information for the SDIO command.
  359. * @retval HAL status
  360. */
  361. HAL_StatusTypeDef SDIO_DataConfig(SDIO_TypeDef *SDIOx, SDIO_DataInitTypeDef* SDIO_DataInitStruct)
  362. {
  363. uint32_t tmpreg = 0U;
  364. /* Check the parameters */
  365. assert_param(IS_SDIO_DATA_LENGTH(SDIO_DataInitStruct->DataLength));
  366. assert_param(IS_SDIO_BLOCK_SIZE(SDIO_DataInitStruct->DataBlockSize));
  367. assert_param(IS_SDIO_TRANSFER_DIR(SDIO_DataInitStruct->TransferDir));
  368. assert_param(IS_SDIO_TRANSFER_MODE(SDIO_DataInitStruct->TransferMode));
  369. assert_param(IS_SDIO_DPSM(SDIO_DataInitStruct->DPSM));
  370. /* Set the SDIO Data Timeout value */
  371. SDIOx->DTIMER = SDIO_DataInitStruct->DataTimeOut;
  372. /* Set the SDIO DataLength value */
  373. SDIOx->DLEN = SDIO_DataInitStruct->DataLength;
  374. /* Set the SDIO data configuration parameters */
  375. tmpreg |= (uint32_t)(SDIO_DataInitStruct->DataBlockSize |\
  376. SDIO_DataInitStruct->TransferDir |\
  377. SDIO_DataInitStruct->TransferMode |\
  378. SDIO_DataInitStruct->DPSM);
  379. /* Write to SDIO DCTRL */
  380. MODIFY_REG(SDIOx->DCTRL, DCTRL_CLEAR_MASK, tmpreg);
  381. return HAL_OK;
  382. }
  383. /**
  384. * @brief Returns number of remaining data bytes to be transferred.
  385. * @param SDIOx: Pointer to SDIO register base
  386. * @retval Number of remaining data bytes to be transferred
  387. */
  388. uint32_t SDIO_GetDataCounter(SDIO_TypeDef *SDIOx)
  389. {
  390. return (SDIOx->DCOUNT);
  391. }
  392. /**
  393. * @brief Get the FIFO data
  394. * @param SDIOx: Pointer to SDIO register base
  395. * @retval Data received
  396. */
  397. uint32_t SDIO_GetFIFOCount(SDIO_TypeDef *SDIOx)
  398. {
  399. return (SDIOx->FIFO);
  400. }
  401. /**
  402. * @brief Sets one of the two options of inserting read wait interval.
  403. * @param SDIO_ReadWaitMode: SD I/O Read Wait operation mode.
  404. * This parameter can be:
  405. * @arg SDIO_READ_WAIT_MODE_CLK: Read Wait control by stopping SDIOCLK
  406. * @arg SDIO_READ_WAIT_MODE_DATA2: Read Wait control using SDIO_DATA2
  407. * @retval None
  408. */
  409. HAL_StatusTypeDef SDIO_SetSDIOReadWaitMode(uint32_t SDIO_ReadWaitMode)
  410. {
  411. /* Check the parameters */
  412. assert_param(IS_SDIO_READWAIT_MODE(SDIO_ReadWaitMode));
  413. *(__IO uint32_t *)DCTRL_RWMOD_BB = SDIO_ReadWaitMode;
  414. return HAL_OK;
  415. }
  416. /**
  417. * @}
  418. */
  419. /**
  420. * @}
  421. */
  422. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||
  423. STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx ||
  424. STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
  425. #endif /* (HAL_SD_MODULE_ENABLED) || (HAL_MMC_MODULE_ENABLED) */
  426. /**
  427. * @}
  428. */
  429. /**
  430. * @}
  431. */
  432. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/