stm32f1xx_ll_sdmmc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_ll_sdmmc.c
  4. * @author MCD Application Team
  5. * @version V1.0.4
  6. * @date 29-April-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 peripheral uses two clock signals:
  45. (++) SDIO adapter clock (SDIOCLK = HCLK)
  46. (++) AHB bus clock (HCLK/2)
  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(),
  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) 2016 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 "stm32f1xx_hal.h"
  143. #if defined (HAL_SD_MODULE_ENABLED) || defined(HAL_MMC_MODULE_ENABLED)
  144. #if defined(STM32F103xE) || defined(STM32F103xG)
  145. /** @addtogroup STM32F1xx_HAL_Driver
  146. * @{
  147. */
  148. /** @defgroup SDMMC_LL SDMMC Low Layer
  149. * @brief Low layer module for SD and MMC driver
  150. * @{
  151. */
  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 and 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. /* Check the parameters */
  182. assert_param(IS_SDIO_ALL_INSTANCE(SDIOx));
  183. assert_param(IS_SDIO_CLOCK_EDGE(Init.ClockEdge));
  184. assert_param(IS_SDIO_CLOCK_BYPASS(Init.ClockBypass));
  185. assert_param(IS_SDIO_CLOCK_POWER_SAVE(Init.ClockPowerSave));
  186. assert_param(IS_SDIO_BUS_WIDE(Init.BusWide));
  187. assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(Init.HardwareFlowControl));
  188. assert_param(IS_SDIO_CLKDIV(Init.ClockDiv));
  189. /* Set SDIO configuration parameters */
  190. /* Write to SDIO CLKCR */
  191. MODIFY_REG(SDIOx->CLKCR, CLKCR_CLEAR_MASK, Init.ClockEdge |\
  192. Init.ClockBypass |\
  193. Init.ClockPowerSave |\
  194. Init.BusWide |\
  195. Init.HardwareFlowControl |\
  196. Init.ClockDiv);
  197. return HAL_OK;
  198. }
  199. /**
  200. * @}
  201. */
  202. /** @defgroup HAL_SDMMC_LL_Group2 IO operation functions
  203. * @brief Data transfers functions
  204. *
  205. @verbatim
  206. ===============================================================================
  207. ##### IO operation functions #####
  208. ===============================================================================
  209. [..]
  210. This subsection provides a set of functions allowing to manage the SDIO data
  211. transfers.
  212. @endverbatim
  213. * @{
  214. */
  215. /**
  216. * @brief Read data (word) from Rx FIFO in blocking mode (polling)
  217. * @param SDIOx: Pointer to SDIO register base
  218. * @retval HAL status
  219. */
  220. uint32_t SDIO_ReadFIFO(SDIO_TypeDef *SDIOx)
  221. {
  222. /* Read data from Rx FIFO */
  223. return (SDIOx->FIFO);
  224. }
  225. /**
  226. * @brief Write data (word) to Tx FIFO in blocking mode (polling)
  227. * @param SDIOx: Pointer to SDIO register base
  228. * @param pWriteData: pointer to data to write
  229. * @retval HAL status
  230. */
  231. HAL_StatusTypeDef SDIO_WriteFIFO(SDIO_TypeDef *SDIOx, uint32_t *pWriteData)
  232. {
  233. /* Write data to FIFO */
  234. SDIOx->FIFO = *pWriteData;
  235. return HAL_OK;
  236. }
  237. /**
  238. * @}
  239. */
  240. /** @defgroup HAL_SDMMC_LL_Group3 Peripheral Control functions
  241. * @brief management functions
  242. *
  243. @verbatim
  244. ===============================================================================
  245. ##### Peripheral Control functions #####
  246. ===============================================================================
  247. [..]
  248. This subsection provides a set of functions allowing to control the SDIO data
  249. transfers.
  250. @endverbatim
  251. * @{
  252. */
  253. /**
  254. * @brief Set SDIO Power state to ON.
  255. * @param SDIOx: Pointer to SDIO register base
  256. * @retval HAL status
  257. */
  258. HAL_StatusTypeDef SDIO_PowerState_ON(SDIO_TypeDef *SDIOx)
  259. {
  260. /* Set power state to ON */
  261. SDIOx->POWER = SDIO_POWER_PWRCTRL;
  262. return HAL_OK;
  263. }
  264. /**
  265. * @brief Set SDIO Power state to OFF.
  266. * @param SDIOx: Pointer to SDIO register base
  267. * @retval HAL status
  268. */
  269. HAL_StatusTypeDef SDIO_PowerState_OFF(SDIO_TypeDef *SDIOx)
  270. {
  271. /* Set power state to OFF */
  272. SDIOx->POWER = (uint32_t)0x00000000;
  273. return HAL_OK;
  274. }
  275. /**
  276. * @brief Get SDIO Power state.
  277. * @param SDIOx: Pointer to SDIO register base
  278. * @retval Power status of the controller. The returned value can be one of the
  279. * following values:
  280. * - 0x00: Power OFF
  281. * - 0x02: Power UP
  282. * - 0x03: Power ON
  283. */
  284. uint32_t SDIO_GetPowerState(SDIO_TypeDef *SDIOx)
  285. {
  286. return (SDIOx->POWER & SDIO_POWER_PWRCTRL);
  287. }
  288. /**
  289. * @brief Configure the SDIO command path according to the specified parameters in
  290. * SDIO_CmdInitTypeDef structure and send the command
  291. * @param SDIOx: Pointer to SDIO register base
  292. * @param Command: pointer to a SDIO_CmdInitTypeDef structure that contains
  293. * the configuration information for the SDIO command
  294. * @retval HAL status
  295. */
  296. HAL_StatusTypeDef SDIO_SendCommand(SDIO_TypeDef *SDIOx, SDIO_CmdInitTypeDef *Command)
  297. {
  298. /* Check the parameters */
  299. assert_param(IS_SDIO_CMD_INDEX(Command->CmdIndex));
  300. assert_param(IS_SDIO_RESPONSE(Command->Response));
  301. assert_param(IS_SDIO_WAIT(Command->WaitForInterrupt));
  302. assert_param(IS_SDIO_CPSM(Command->CPSM));
  303. /* Set the SDIO Argument value */
  304. SDIOx->ARG = Command->Argument;
  305. /* Set SDIO command parameters */
  306. /* Write to SDIO CMD register */
  307. MODIFY_REG(SDIOx->CMD, CMD_CLEAR_MASK, Command->CmdIndex |\
  308. Command->Response |\
  309. Command->WaitForInterrupt |\
  310. Command->CPSM);
  311. return HAL_OK;
  312. }
  313. /**
  314. * @brief Return the command index of last command for which response received
  315. * @param SDIOx: Pointer to SDIO register base
  316. * @retval Command index of the last command response received
  317. */
  318. uint8_t SDIO_GetCommandResponse(SDIO_TypeDef *SDIOx)
  319. {
  320. return (uint8_t)(SDIOx->RESPCMD);
  321. }
  322. /**
  323. * @brief Return the response received from the card for the last command
  324. * @param SDIO_RESP: Specifies the SDIO response register.
  325. * This parameter can be one of the following values:
  326. * @arg SDIO_RESP1: Response Register 1
  327. * @arg SDIO_RESP2: Response Register 2
  328. * @arg SDIO_RESP3: Response Register 3
  329. * @arg SDIO_RESP4: Response Register 4
  330. * @retval The Corresponding response register value
  331. */
  332. uint32_t SDIO_GetResponse(SDIO_TypeDef *SDIOx, uint32_t Response)
  333. {
  334. __IO uint32_t tmp = 0;
  335. /* Check the parameters */
  336. assert_param(IS_SDIO_RESP(Response));
  337. /* Get the response */
  338. tmp = SDIO_RESP_ADDR + Response;
  339. return (*(__IO uint32_t *) tmp);
  340. }
  341. /**
  342. * @brief Configure the SDIO data path according to the specified
  343. * parameters in the SDIO_DataInitTypeDef.
  344. * @param SDIOx: Pointer to SDIO register base
  345. * @param Data : pointer to a SDIO_DataInitTypeDef structure
  346. * that contains the configuration information for the SDIO data.
  347. * @retval HAL status
  348. */
  349. HAL_StatusTypeDef SDIO_DataConfig(SDIO_TypeDef *SDIOx, SDIO_DataInitTypeDef* Data)
  350. {
  351. /* Check the parameters */
  352. assert_param(IS_SDIO_DATA_LENGTH(Data->DataLength));
  353. assert_param(IS_SDIO_BLOCK_SIZE(Data->DataBlockSize));
  354. assert_param(IS_SDIO_TRANSFER_DIR(Data->TransferDir));
  355. assert_param(IS_SDIO_TRANSFER_MODE(Data->TransferMode));
  356. assert_param(IS_SDIO_DPSM(Data->DPSM));
  357. /* Set the SDIO Data TimeOut value */
  358. SDIOx->DTIMER = Data->DataTimeOut;
  359. /* Set the SDIO DataLength value */
  360. SDIOx->DLEN = Data->DataLength;
  361. /* Set the SDIO data configuration parameters */
  362. /* Write to SDIO DCTRL */
  363. MODIFY_REG(SDIOx->DCTRL, DCTRL_CLEAR_MASK, Data->DataBlockSize |\
  364. Data->TransferDir |\
  365. Data->TransferMode |\
  366. Data->DPSM);
  367. return HAL_OK;
  368. }
  369. /**
  370. * @brief Returns number of remaining data bytes to be transferred.
  371. * @param SDIOx: Pointer to SDIO register base
  372. * @retval Number of remaining data bytes to be transferred
  373. */
  374. uint32_t SDIO_GetDataCounter(SDIO_TypeDef *SDIOx)
  375. {
  376. return (SDIOx->DCOUNT);
  377. }
  378. /**
  379. * @brief Get the FIFO data
  380. * @param SDIOx: Pointer to SDIO register base
  381. * @retval Data received
  382. */
  383. uint32_t SDIO_GetFIFOCount(SDIO_TypeDef *SDIOx)
  384. {
  385. return (SDIOx->FIFO);
  386. }
  387. /**
  388. * @brief Sets one of the two options of inserting read wait interval.
  389. * @param SDIO_ReadWaitMode: SD I/O Read Wait operation mode.
  390. * This parameter can be:
  391. * @arg SDIO_READ_WAIT_MODE_CLK: Read Wait control by stopping SDIOCLK
  392. * @arg SDIO_READ_WAIT_MODE_DATA2: Read Wait control using SDIO_DATA2
  393. * @retval None
  394. */
  395. HAL_StatusTypeDef SDIO_SetSDIOReadWaitMode(SDIO_TypeDef *SDIOx, uint32_t SDIO_ReadWaitMode)
  396. {
  397. /* Check the parameters */
  398. assert_param(IS_SDIO_READWAIT_MODE(SDIO_ReadWaitMode));
  399. /* Set SDIO read wait mode */
  400. MODIFY_REG(SDIO->DCTRL, SDIO_DCTRL_RWMOD, SDIO_ReadWaitMode);
  401. return HAL_OK;
  402. }
  403. /**
  404. * @}
  405. */
  406. /**
  407. * @}
  408. */
  409. #endif /* STM32F103xE || STM32F103xG */
  410. #endif /* (HAL_SD_MODULE_ENABLED) || (HAL_MMC_MODULE_ENABLED) */
  411. /**
  412. * @}
  413. */
  414. /**
  415. * @}
  416. */
  417. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/