You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

511 lines
18 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_ll_sdmmc.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  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/SDMMC MMC card host interface (SDMMC) provides an interface between the APB2
  21. peripheral bus and MultiMedia cards (MMCs), SD memory cards, SDMMC cards and CE-ATA
  22. devices.
  23. [..] The SDMMC features include the following:
  24. (+) Full compliance with MultiMedia Card System Specification Version 4.2. Card support
  25. for three different databus modes: 1-bit (default), 4-bit and 8-bit
  26. (+) Full compatibility with previous versions of MultiMedia Cards (forward compatibility)
  27. (+) Full compliance with SD Memory Card Specifications Version 2.0
  28. (+) Full compliance with SD I/O Card Specification Version 2.0: card support for two
  29. different data bus modes: 1-bit (default) and 4-bit
  30. (+) Full support of the CE-ATA features (full compliance with CE-ATA digital protocol
  31. Rev1.1)
  32. (+) Data transfer up to 48 MHz for the 8 bit mode
  33. (+) Data and command output enable signals to control external bidirectional drivers.
  34. ##### How to use this driver #####
  35. ==============================================================================
  36. [..]
  37. This driver is a considered as a driver of service for external devices drivers
  38. that interfaces with the SDMMC peripheral.
  39. According to the device used (SD card/ MMC card / SDMMC card ...), a set of APIs
  40. is used in the device's driver to perform SDMMC operations and functionalities.
  41. This driver is almost transparent for the final user, it is only used to implement other
  42. functionalities of the external device.
  43. [..]
  44. (+) The SDMMC clock (SDMMCCLK = 48 MHz) is coming from a specific output of PLL
  45. (PLL48CLK). Before start working with SDMMC peripheral make sure that the
  46. PLL is well configured.
  47. The SDMMC peripheral uses two clock signals:
  48. (++) SDMMC adapter clock (SDMMCCLK = 48 MHz)
  49. (++) APB2 bus clock (PCLK2)
  50. -@@- PCLK2 and SDMMC_CK clock frequencies must respect the following condition:
  51. Frequency(PCLK2) >= (3 / 8 x Frequency(SDMMC_CK))
  52. (+) Enable/Disable peripheral clock using RCC peripheral macros related to SDMMC
  53. peripheral.
  54. (+) Enable the Power ON State using the SDMMC_PowerState_ON(SDMMCx)
  55. function and disable it using the function SDMMC_PowerState_OFF(SDMMCx).
  56. (+) Enable/Disable the clock using the __SDMMC_ENABLE()/__SDMMC_DISABLE() macros.
  57. (+) Enable/Disable the peripheral interrupts using the macros __SDMMC_ENABLE_IT(hSDMMC, IT)
  58. and __SDMMC_DISABLE_IT(hSDMMC, 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 __SDMMC_DMA_ENABLE() macro or Disable it using the macro
  63. __SDMMC_DMA_DISABLE().
  64. (+) To control the CPSM (Command Path State Machine) and send
  65. commands to the card use the SDMMC_SendCommand(SDMMCx),
  66. SDMMC_GetCommandResponse() and SDMMC_GetResponse() functions. First, user has
  67. to fill the command structure (pointer to SDMMC_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 SDMMC_CMDRESP
  76. register using the SDMMC_GetCommandResponse().
  77. The SDMMC responses registers (SDMMC_RESP1 to SDMMC_RESP2), use the
  78. SDMMC_GetResponse() function.
  79. (+) To control the DPSM (Data Path State Machine) and send/receive
  80. data to/from the card use the SDMMC_DataConfig(), SDMMC_GetDataCounter(),
  81. SDMMC_ReadFIFO(), DIO_WriteFIFO() and SDMMC_GetFIFOCount() functions.
  82. *** Read Operations ***
  83. =======================
  84. [..]
  85. (#) First, user has to fill the data structure (pointer to
  86. SDMMC_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 SDMMC)
  92. (++) Data Transfer mode
  93. (++) DPSM Status (Enable or Disable)
  94. (#) Configure the SDMMC 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 SDMMC flags/interrupts to check the transfer status.
  98. *** Write Operations ***
  99. ========================
  100. [..]
  101. (#) First, user has to fill the data structure (pointer to
  102. SDMMC_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 SDMMC resources to send the data to the card according to
  111. selected transfer mode.
  112. (#) Send the selected Write command.
  113. (#) Use the SDMMC flags/interrupts to check the transfer status.
  114. @endverbatim
  115. ******************************************************************************
  116. * @attention
  117. *
  118. * <h2><center>&copy; COPYRIGHT(c) 2015 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 "stm32f7xx_hal.h"
  146. /** @addtogroup STM32F7xx_HAL_Driver
  147. * @{
  148. */
  149. /** @defgroup SDMMC_LL SDMMC Low Layer
  150. * @brief Low layer module for SD
  151. * @{
  152. */
  153. #if defined (HAL_SD_MODULE_ENABLED) || defined(HAL_MMC_MODULE_ENABLED)
  154. /* Private typedef -----------------------------------------------------------*/
  155. /* Private define ------------------------------------------------------------*/
  156. /* Private macro -------------------------------------------------------------*/
  157. /* Private variables ---------------------------------------------------------*/
  158. /* Private function prototypes -----------------------------------------------*/
  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. assert_param(IS_SDMMC_CLOCK_BYPASS(Init.ClockBypass));
  188. assert_param(IS_SDMMC_CLOCK_POWER_SAVE(Init.ClockPowerSave));
  189. assert_param(IS_SDMMC_BUS_WIDE(Init.BusWide));
  190. assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(Init.HardwareFlowControl));
  191. assert_param(IS_SDMMC_CLKDIV(Init.ClockDiv));
  192. /* Set SDMMC configuration parameters */
  193. tmpreg |= (Init.ClockEdge |\
  194. Init.ClockBypass |\
  195. Init.ClockPowerSave |\
  196. Init.BusWide |\
  197. Init.HardwareFlowControl |\
  198. Init.ClockDiv
  199. );
  200. /* Write to SDMMC CLKCR */
  201. MODIFY_REG(SDMMCx->CLKCR, CLKCR_CLEAR_MASK, tmpreg);
  202. return HAL_OK;
  203. }
  204. /**
  205. * @}
  206. */
  207. /** @defgroup HAL_SDMMC_LL_Group2 IO operation functions
  208. * @brief Data transfers functions
  209. *
  210. @verbatim
  211. ===============================================================================
  212. ##### I/O operation functions #####
  213. ===============================================================================
  214. [..]
  215. This subsection provides a set of functions allowing to manage the SDMMC data
  216. transfers.
  217. @endverbatim
  218. * @{
  219. */
  220. /**
  221. * @brief Read data (word) from Rx FIFO in blocking mode (polling)
  222. * @param SDMMCx: Pointer to SDMMC register base
  223. * @retval HAL status
  224. */
  225. uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)
  226. {
  227. /* Read data from Rx FIFO */
  228. return (SDMMCx->FIFO);
  229. }
  230. /**
  231. * @brief Write data (word) to Tx FIFO in blocking mode (polling)
  232. * @param SDMMCx: Pointer to SDMMC register base
  233. * @param pWriteData: pointer to data to write
  234. * @retval HAL status
  235. */
  236. HAL_StatusTypeDef SDMMC_WriteFIFO(SDMMC_TypeDef *SDMMCx, uint32_t *pWriteData)
  237. {
  238. /* Write data to FIFO */
  239. SDMMCx->FIFO = *pWriteData;
  240. return HAL_OK;
  241. }
  242. /**
  243. * @}
  244. */
  245. /** @defgroup HAL_SDMMC_LL_Group3 Peripheral Control functions
  246. * @brief management functions
  247. *
  248. @verbatim
  249. ===============================================================================
  250. ##### Peripheral Control functions #####
  251. ===============================================================================
  252. [..]
  253. This subsection provides a set of functions allowing to control the SDMMC data
  254. transfers.
  255. @endverbatim
  256. * @{
  257. */
  258. /**
  259. * @brief Set SDMMC Power state to ON.
  260. * @param SDMMCx: Pointer to SDMMC register base
  261. * @retval HAL status
  262. */
  263. HAL_StatusTypeDef SDMMC_PowerState_ON(SDMMC_TypeDef *SDMMCx)
  264. {
  265. /* Set power state to ON */
  266. SDMMCx->POWER = SDMMC_POWER_PWRCTRL;
  267. return HAL_OK;
  268. }
  269. /**
  270. * @brief Set SDMMC Power state to OFF.
  271. * @param SDMMCx: Pointer to SDMMC register base
  272. * @retval HAL status
  273. */
  274. HAL_StatusTypeDef SDMMC_PowerState_OFF(SDMMC_TypeDef *SDMMCx)
  275. {
  276. /* Set power state to OFF */
  277. SDMMCx->POWER = (uint32_t)0x00000000;
  278. return HAL_OK;
  279. }
  280. /**
  281. * @brief Get SDMMC Power state.
  282. * @param SDMMCx: Pointer to SDMMC register base
  283. * @retval Power status of the controller. The returned value can be one of the
  284. * following values:
  285. * - 0x00: Power OFF
  286. * - 0x02: Power UP
  287. * - 0x03: Power ON
  288. */
  289. uint32_t SDMMC_GetPowerState(SDMMC_TypeDef *SDMMCx)
  290. {
  291. return (SDMMCx->POWER & SDMMC_POWER_PWRCTRL);
  292. }
  293. /**
  294. * @brief Configure the SDMMC command path according to the specified parameters in
  295. * SDMMC_CmdInitTypeDef structure and send the command
  296. * @param SDMMCx: Pointer to SDMMC register base
  297. * @param Command: pointer to a SDMMC_CmdInitTypeDef structure that contains
  298. * the configuration information for the SDMMC command
  299. * @retval HAL status
  300. */
  301. HAL_StatusTypeDef SDMMC_SendCommand(SDMMC_TypeDef *SDMMCx, SDMMC_CmdInitTypeDef *Command)
  302. {
  303. uint32_t tmpreg = 0;
  304. /* Check the parameters */
  305. assert_param(IS_SDMMC_CMD_INDEX(Command->CmdIndex));
  306. assert_param(IS_SDMMC_RESPONSE(Command->Response));
  307. assert_param(IS_SDMMC_WAIT(Command->WaitForInterrupt));
  308. assert_param(IS_SDMMC_CPSM(Command->CPSM));
  309. /* Set the SDMMC Argument value */
  310. SDMMCx->ARG = Command->Argument;
  311. /* Set SDMMC command parameters */
  312. tmpreg |= (uint32_t)(Command->CmdIndex |\
  313. Command->Response |\
  314. Command->WaitForInterrupt |\
  315. Command->CPSM);
  316. /* Write to SDMMC CMD register */
  317. MODIFY_REG(SDMMCx->CMD, CMD_CLEAR_MASK, tmpreg);
  318. return HAL_OK;
  319. }
  320. /**
  321. * @brief Return the command index of last command for which response received
  322. * @param SDMMCx: Pointer to SDMMC register base
  323. * @retval Command index of the last command response received
  324. */
  325. uint8_t SDMMC_GetCommandResponse(SDMMC_TypeDef *SDMMCx)
  326. {
  327. return (uint8_t)(SDMMCx->RESPCMD);
  328. }
  329. /**
  330. * @brief Return the response received from the card for the last command
  331. * @param SDMMCx: Pointer to SDMMC register base
  332. * @param Response: Specifies the SDMMC response register.
  333. * This parameter can be one of the following values:
  334. * @arg SDMMC_RESP1: Response Register 1
  335. * @arg SDMMC_RESP2: Response Register 2
  336. * @arg SDMMC_RESP3: Response Register 3
  337. * @arg SDMMC_RESP4: Response Register 4
  338. * @retval The Corresponding response register value
  339. */
  340. uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)
  341. {
  342. __IO uint32_t tmp = 0;
  343. /* Check the parameters */
  344. assert_param(IS_SDMMC_RESP(Response));
  345. /* Get the response */
  346. tmp = (uint32_t)&(SDMMCx->RESP1) + Response;
  347. return (*(__IO uint32_t *) tmp);
  348. }
  349. /**
  350. * @brief Configure the SDMMC data path according to the specified
  351. * parameters in the SDMMC_DataInitTypeDef.
  352. * @param SDMMCx: Pointer to SDMMC register base
  353. * @param Data : pointer to a SDMMC_DataInitTypeDef structure
  354. * that contains the configuration information for the SDMMC data.
  355. * @retval HAL status
  356. */
  357. HAL_StatusTypeDef SDMMC_DataConfig(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef* Data)
  358. {
  359. uint32_t tmpreg = 0;
  360. /* Check the parameters */
  361. assert_param(IS_SDMMC_DATA_LENGTH(Data->DataLength));
  362. assert_param(IS_SDMMC_BLOCK_SIZE(Data->DataBlockSize));
  363. assert_param(IS_SDMMC_TRANSFER_DIR(Data->TransferDir));
  364. assert_param(IS_SDMMC_TRANSFER_MODE(Data->TransferMode));
  365. assert_param(IS_SDMMC_DPSM(Data->DPSM));
  366. /* Set the SDMMC Data TimeOut value */
  367. SDMMCx->DTIMER = Data->DataTimeOut;
  368. /* Set the SDMMC DataLength value */
  369. SDMMCx->DLEN = Data->DataLength;
  370. /* Set the SDMMC data configuration parameters */
  371. tmpreg |= (uint32_t)(Data->DataBlockSize |\
  372. Data->TransferDir |\
  373. Data->TransferMode |\
  374. Data->DPSM);
  375. /* Write to SDMMC DCTRL */
  376. MODIFY_REG(SDMMCx->DCTRL, DCTRL_CLEAR_MASK, tmpreg);
  377. return HAL_OK;
  378. }
  379. /**
  380. * @brief Returns number of remaining data bytes to be transferred.
  381. * @param SDMMCx: Pointer to SDMMC register base
  382. * @retval Number of remaining data bytes to be transferred
  383. */
  384. uint32_t SDMMC_GetDataCounter(SDMMC_TypeDef *SDMMCx)
  385. {
  386. return (SDMMCx->DCOUNT);
  387. }
  388. /**
  389. * @brief Get the FIFO data
  390. * @param SDMMCx: Pointer to SDMMC register base
  391. * @retval Data received
  392. */
  393. uint32_t SDMMC_GetFIFOCount(SDMMC_TypeDef *SDMMCx)
  394. {
  395. return (SDMMCx->FIFO);
  396. }
  397. /**
  398. * @brief Sets one of the two options of inserting read wait interval.
  399. * @param SDMMCx: Pointer to SDMMC register base
  400. * @param SDMMC_ReadWaitMode: SDMMC Read Wait operation mode.
  401. * This parameter can be:
  402. * @arg SDMMC_READ_WAIT_MODE_CLK: Read Wait control by stopping SDMMCCLK
  403. * @arg SDMMC_READ_WAIT_MODE_DATA2: Read Wait control using SDMMC_DATA2
  404. * @retval None
  405. */
  406. HAL_StatusTypeDef SDMMC_SetSDMMCReadWaitMode(SDMMC_TypeDef *SDMMCx, uint32_t SDMMC_ReadWaitMode)
  407. {
  408. /* Check the parameters */
  409. assert_param(IS_SDMMC_READWAIT_MODE(SDMMC_ReadWaitMode));
  410. /* Set SDMMC read wait mode */
  411. SDMMCx->DCTRL |= SDMMC_ReadWaitMode;
  412. return HAL_OK;
  413. }
  414. /**
  415. * @}
  416. */
  417. /**
  418. * @}
  419. */
  420. #endif /* (HAL_SD_MODULE_ENABLED) || (HAL_MMC_MODULE_ENABLED) */
  421. /**
  422. * @}
  423. */
  424. /**
  425. * @}
  426. */
  427. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/