stm32l1xx_ll_sdmmc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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. @verbatim
  15. ==============================================================================
  16. ##### SDMMC peripheral features #####
  17. ==============================================================================
  18. [..] The SD/SDIO MMC card host interface (SDIO) provides an interface between the APB2
  19. peripheral bus and MultiMedia cards (MMCs), SD memory cards, SDIO cards and CE-ATA
  20. devices.
  21. [..] The SDIO features include the following:
  22. (+) Full compliance with MultiMedia Card System Specification Version 4.2. Card support
  23. for three different databus modes: 1-bit (default), 4-bit and 8-bit
  24. (+) Full compatibility with previous versions of MultiMedia Cards (forward compatibility)
  25. (+) Full compliance with SD Memory Card Specifications Version 2.0
  26. (+) Full compliance with SD I/O Card Specification Version 2.0: card support for two
  27. different data bus modes: 1-bit (default) and 4-bit
  28. (+) Full support of the CE-ATA features (full compliance with CE-ATA digital protocol
  29. Rev1.1)
  30. (+) Data transfer up to 48 MHz for the 8 bit mode
  31. (+) Data and command output enable signals to control external bidirectional drivers.
  32. ##### How to use this driver #####
  33. ==============================================================================
  34. [..]
  35. This driver is a considered as a driver of service for external devices drivers
  36. that interfaces with the SDIO peripheral.
  37. According to the device used (SD card/ MMC card / SDIO card ...), a set of APIs
  38. is used in the device's driver to perform SDIO operations and functionalities.
  39. This driver is almost transparent for the final user, it is only used to implement other
  40. functionalities of the external device.
  41. [..]
  42. (+) The SDIO clock (SDIOCLK = 48 MHz) is coming from the PLL. Before start working with SDIO peripheral make sure that the
  43. PLL is well configured.
  44. The SDIO peripheral uses two clock signals:
  45. (++) SDIO adapter clock (SDIOCLK = 48 MHz)
  46. (++) APB2 bus clock (PCLK2)
  47. -@@- PCLK2 and SDIO_CK clock frequencies must respect the following condition:
  48. Frequency(PCLK2) >= (3 / 8 x Frequency(SDIO_CK))
  49. (+) Enable/Disable peripheral clock using RCC peripheral macros related to SDIO
  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(SDIOx),
  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 SDIO 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_DataConfig(), 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 SDIO)
  89. (++) Data Transfer mode
  90. (++) DPSM Status (Enable or Disable)
  91. (#) Configure the SDIO resources to receive the data from the card
  92. according to selected transfer mode.
  93. (#) Send the selected Read command.
  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 SDIO 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. @endverbatim
  112. ******************************************************************************
  113. * @attention
  114. *
  115. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  116. *
  117. * Redistribution and use in source and binary forms, with or without modification,
  118. * are permitted provided that the following conditions are met:
  119. * 1. Redistributions of source code must retain the above copyright notice,
  120. * this list of conditions and the following disclaimer.
  121. * 2. Redistributions in binary form must reproduce the above copyright notice,
  122. * this list of conditions and the following disclaimer in the documentation
  123. * and/or other materials provided with the distribution.
  124. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  125. * may be used to endorse or promote products derived from this software
  126. * without specific prior written permission.
  127. *
  128. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  129. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  130. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  131. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  132. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  133. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  134. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  135. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  136. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  137. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  138. *
  139. ******************************************************************************
  140. */
  141. /* Includes ------------------------------------------------------------------*/
  142. #include "stm32l1xx_hal.h"
  143. /** @addtogroup STM32L1xx_HAL_Driver
  144. * @{
  145. */
  146. /** @defgroup SDMMC_LL SDMMC_LL
  147. * @brief Low layer module for SD and MMC driver
  148. * @{
  149. */
  150. #if defined (HAL_SD_MODULE_ENABLED) || defined(HAL_MMC_MODULE_ENABLED)
  151. #if defined(STM32L151xD) || defined(STM32L152xD) || defined(STM32L162xD)
  152. /* Private typedef -----------------------------------------------------------*/
  153. /* Private define ------------------------------------------------------------*/
  154. /* Private macro -------------------------------------------------------------*/
  155. /* Private variables ---------------------------------------------------------*/
  156. /* Private function prototypes -----------------------------------------------*/
  157. /* Private functions ---------------------------------------------------------*/
  158. /** @defgroup SDMMC_LL_Exported_Functions SDMMC_LL Exported Functions
  159. * @{
  160. */
  161. /** @defgroup HAL_SDMMC_LL_Group1 Initialization/de-initialization functions
  162. * @brief Initialization and Configuration functions
  163. *
  164. @verbatim
  165. ===============================================================================
  166. ##### Initialization/de-initialization functions #####
  167. ===============================================================================
  168. [..] This section provides functions allowing to:
  169. @endverbatim
  170. * @{
  171. */
  172. /**
  173. * @brief Initializes the SDIO according to the specified
  174. * parameters in the SDIO_InitTypeDef and create the associated handle.
  175. * @param SDIOx: Pointer to SDIO register base
  176. * @param Init: SDIO initialization structure
  177. * @retval HAL status
  178. */
  179. HAL_StatusTypeDef SDIO_Init(SDIO_TypeDef *SDIOx, SDIO_InitTypeDef Init)
  180. {
  181. uint32_t tmpreg = 0;
  182. /* Check the parameters */
  183. assert_param(IS_SDIO_ALL_INSTANCE(SDIOx));
  184. assert_param(IS_SDIO_CLOCK_EDGE(Init.ClockEdge));
  185. assert_param(IS_SDIO_CLOCK_BYPASS(Init.ClockBypass));
  186. assert_param(IS_SDIO_CLOCK_POWER_SAVE(Init.ClockPowerSave));
  187. assert_param(IS_SDIO_BUS_WIDE(Init.BusWide));
  188. assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(Init.HardwareFlowControl));
  189. assert_param(IS_SDIO_CLKDIV(Init.ClockDiv));
  190. /* Set SDIO configuration parameters */
  191. tmpreg |= (Init.ClockEdge |\
  192. Init.ClockBypass |\
  193. Init.ClockPowerSave |\
  194. Init.BusWide |\
  195. Init.HardwareFlowControl |\
  196. Init.ClockDiv
  197. );
  198. /* Write to SDIO CLKCR */
  199. MODIFY_REG(SDIOx->CLKCR, CLKCR_CLEAR_MASK, tmpreg);
  200. return HAL_OK;
  201. }
  202. /**
  203. * @}
  204. */
  205. /** @defgroup HAL_SDMMC_LL_Group2 I/O operation functions
  206. * @brief Data transfers functions
  207. *
  208. @verbatim
  209. ===============================================================================
  210. ##### I/O operation functions #####
  211. ===============================================================================
  212. [..]
  213. This subsection provides a set of functions allowing to manage the SDIO data
  214. transfers.
  215. @endverbatim
  216. * @{
  217. */
  218. /**
  219. * @brief Read data (word) from Rx FIFO in blocking mode (polling)
  220. * @param SDIOx: Pointer to SDIO register base
  221. * @retval HAL status
  222. */
  223. uint32_t SDIO_ReadFIFO(SDIO_TypeDef *SDIOx)
  224. {
  225. /* Read data from Rx FIFO */
  226. return (SDIOx->FIFO);
  227. }
  228. /**
  229. * @brief Write data (word) to Tx FIFO in blocking mode (polling)
  230. * @param SDIOx: Pointer to SDIO register base
  231. * @param pWriteData: pointer to data to write
  232. * @retval HAL status
  233. */
  234. HAL_StatusTypeDef SDIO_WriteFIFO(SDIO_TypeDef *SDIOx, uint32_t *pWriteData)
  235. {
  236. /* Write data to FIFO */
  237. SDIOx->FIFO = *pWriteData;
  238. return HAL_OK;
  239. }
  240. /**
  241. * @}
  242. */
  243. /** @defgroup HAL_SDMMC_LL_Group3 Peripheral Control functions
  244. * @brief management functions
  245. *
  246. @verbatim
  247. ===============================================================================
  248. ##### Peripheral Control functions #####
  249. ===============================================================================
  250. [..]
  251. This subsection provides a set of functions allowing to control the SDIO data
  252. transfers.
  253. @endverbatim
  254. * @{
  255. */
  256. /**
  257. * @brief Set SDIO Power state to ON.
  258. * @param SDIOx: Pointer to SDIO register base
  259. * @retval HAL status
  260. */
  261. HAL_StatusTypeDef SDIO_PowerState_ON(SDIO_TypeDef *SDIOx)
  262. {
  263. /* Set power state to ON */
  264. SDIOx->POWER = SDIO_POWER_PWRCTRL;
  265. return HAL_OK;
  266. }
  267. /**
  268. * @brief Set SDIO Power state to OFF.
  269. * @param SDIOx: Pointer to SDIO register base
  270. * @retval HAL status
  271. */
  272. HAL_StatusTypeDef SDIO_PowerState_OFF(SDIO_TypeDef *SDIOx)
  273. {
  274. /* Set power state to OFF */
  275. SDIOx->POWER = 0x00000000U;
  276. return HAL_OK;
  277. }
  278. /**
  279. * @brief Get SDIO Power state.
  280. * @param SDIOx: Pointer to SDIO register base
  281. * @retval Power status of the controller. The returned value can be one of the
  282. * following values:
  283. * - 0x00: Power OFF
  284. * - 0x02: Power UP
  285. * - 0x03: Power ON
  286. */
  287. uint32_t SDIO_GetPowerState(SDIO_TypeDef *SDIOx)
  288. {
  289. return (SDIOx->POWER & SDIO_POWER_PWRCTRL);
  290. }
  291. /**
  292. * @brief Configure the SDIO command path according to the specified parameters in
  293. * SDIO_CmdInitTypeDef structure and send the command
  294. * @param SDIOx: Pointer to SDIO register base
  295. * @param SDIO_CmdInitStruct: pointer to a SDIO_CmdInitTypeDef structure that contains
  296. * the configuration information for the SDIO command
  297. * @retval HAL status
  298. */
  299. HAL_StatusTypeDef SDIO_SendCommand(SDIO_TypeDef *SDIOx, SDIO_CmdInitTypeDef *SDIO_CmdInitStruct)
  300. {
  301. uint32_t tmpreg = 0;
  302. /* Check the parameters */
  303. assert_param(IS_SDIO_CMD_INDEX(SDIO_CmdInitStruct->CmdIndex));
  304. assert_param(IS_SDIO_RESPONSE(SDIO_CmdInitStruct->Response));
  305. assert_param(IS_SDIO_WAIT(SDIO_CmdInitStruct->WaitForInterrupt));
  306. assert_param(IS_SDIO_CPSM(SDIO_CmdInitStruct->CPSM));
  307. /* Set the SDIO Argument value */
  308. SDIOx->ARG = SDIO_CmdInitStruct->Argument;
  309. /* Set SDIO command parameters */
  310. tmpreg |= (uint32_t)(SDIO_CmdInitStruct->CmdIndex |\
  311. SDIO_CmdInitStruct->Response |\
  312. SDIO_CmdInitStruct->WaitForInterrupt |\
  313. SDIO_CmdInitStruct->CPSM);
  314. /* Write to SDIO CMD register */
  315. MODIFY_REG(SDIOx->CMD, CMD_CLEAR_MASK, tmpreg);
  316. return HAL_OK;
  317. }
  318. /**
  319. * @brief Return the command index of last command for which response received
  320. * @param SDIOx: Pointer to SDIO register base
  321. * @retval Command index of the last command response received
  322. */
  323. uint8_t SDIO_GetCommandResponse(SDIO_TypeDef *SDIOx)
  324. {
  325. return (uint8_t)(SDIOx->RESPCMD);
  326. }
  327. /**
  328. * @brief Return the response received from the card for the last command
  329. * @param SDIO_RESP: Specifies the SDIO response register.
  330. * This parameter can be one of the following values:
  331. * @arg SDIO_RESP1: Response Register 1
  332. * @arg SDIO_RESP2: Response Register 2
  333. * @arg SDIO_RESP3: Response Register 3
  334. * @arg SDIO_RESP4: Response Register 4
  335. * @retval The Corresponding response register value
  336. */
  337. uint32_t SDIO_GetResponse(uint32_t SDIO_RESP)
  338. {
  339. __IO uint32_t tmp = 0;
  340. /* Check the parameters */
  341. assert_param(IS_SDIO_RESP(SDIO_RESP));
  342. /* Get the response */
  343. tmp = SDIO_RESP_ADDR + SDIO_RESP;
  344. return (*(__IO uint32_t *) tmp);
  345. }
  346. /**
  347. * @brief Configure the SDIO data path according to the specified
  348. * parameters in the SDIO_DataInitTypeDef.
  349. * @param SDIOx: Pointer to SDIO register base
  350. * @param SDIO_DataInitStruct : pointer to a SDIO_DataInitTypeDef structure
  351. * that contains the configuration information for the SDIO command.
  352. * @retval HAL status
  353. */
  354. HAL_StatusTypeDef SDIO_DataConfig(SDIO_TypeDef *SDIOx, SDIO_DataInitTypeDef* SDIO_DataInitStruct)
  355. {
  356. uint32_t tmpreg = 0;
  357. /* Check the parameters */
  358. assert_param(IS_SDIO_DATA_LENGTH(SDIO_DataInitStruct->DataLength));
  359. assert_param(IS_SDIO_BLOCK_SIZE(SDIO_DataInitStruct->DataBlockSize));
  360. assert_param(IS_SDIO_TRANSFER_DIR(SDIO_DataInitStruct->TransferDir));
  361. assert_param(IS_SDIO_TRANSFER_MODE(SDIO_DataInitStruct->TransferMode));
  362. assert_param(IS_SDIO_DPSM(SDIO_DataInitStruct->DPSM));
  363. /* Set the SDIO Data Timeout value */
  364. SDIOx->DTIMER = SDIO_DataInitStruct->DataTimeOut;
  365. /* Set the SDIO DataLength value */
  366. SDIOx->DLEN = SDIO_DataInitStruct->DataLength;
  367. /* Set the SDIO data configuration parameters */
  368. tmpreg |= (uint32_t)(SDIO_DataInitStruct->DataBlockSize |\
  369. SDIO_DataInitStruct->TransferDir |\
  370. SDIO_DataInitStruct->TransferMode |\
  371. SDIO_DataInitStruct->DPSM);
  372. /* Write to SDIO DCTRL */
  373. MODIFY_REG(SDIOx->DCTRL, DCTRL_CLEAR_MASK, tmpreg);
  374. return HAL_OK;
  375. }
  376. /**
  377. * @brief Returns number of remaining data bytes to be transferred.
  378. * @param SDIOx: Pointer to SDIO register base
  379. * @retval Number of remaining data bytes to be transferred
  380. */
  381. uint32_t SDIO_GetDataCounter(SDIO_TypeDef *SDIOx)
  382. {
  383. return (SDIOx->DCOUNT);
  384. }
  385. /**
  386. * @brief Get the FIFO data
  387. * @param SDIOx: Pointer to SDIO register base
  388. * @retval Data received
  389. */
  390. uint32_t SDIO_GetFIFOCount(SDIO_TypeDef *SDIOx)
  391. {
  392. return (SDIOx->FIFO);
  393. }
  394. /**
  395. * @brief Sets one of the two options of inserting read wait interval.
  396. * @param SDIO_ReadWaitMode: SD I/O Read Wait operation mode.
  397. * This parameter can be:
  398. * @arg SDIO_READ_WAIT_MODE_CLK: Read Wait control by stopping SDIOCLK
  399. * @arg SDIO_READ_WAIT_MODE_DATA2: Read Wait control using SDIO_DATA2
  400. * @retval None
  401. */
  402. HAL_StatusTypeDef SDIO_SetSDIOReadWaitMode(uint32_t SDIO_ReadWaitMode)
  403. {
  404. /* Check the parameters */
  405. assert_param(IS_SDIO_READWAIT_MODE(SDIO_ReadWaitMode));
  406. *(__IO uint32_t *)DCTRL_RWMOD_BB = SDIO_ReadWaitMode;
  407. return HAL_OK;
  408. }
  409. /**
  410. * @}
  411. */
  412. /**
  413. * @}
  414. */
  415. #endif /* STM32L151xD || STM32L152xD || STM32L162xD */
  416. #endif /* (HAL_SD_MODULE_ENABLED) || (HAL_MMC_MODULE_ENABLED) */
  417. /**
  418. * @}
  419. */
  420. /**
  421. * @}
  422. */
  423. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/