stm32f4xx_sdio.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_sdio.c
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Secure digital input/output interface (SDIO)
  9. * peripheral:
  10. * + Initialization and Configuration
  11. * + Command path state machine (CPSM) management
  12. * + Data path state machine (DPSM) management
  13. * + SDIO IO Cards mode management
  14. * + CE-ATA mode management
  15. * + DMA transfers management
  16. * + Interrupts and flags management
  17. *
  18. @verbatim
  19. ===================================================================
  20. ##### How to use this driver #####
  21. ===================================================================
  22. [..]
  23. (#) The SDIO clock (SDIOCLK = 48 MHz) is coming from a specific output of PLL
  24. (PLL48CLK). Before to start working with SDIO peripheral make sure that the
  25. PLL is well configured.
  26. The SDIO peripheral uses two clock signals:
  27. (++) SDIO adapter clock (SDIOCLK = 48 MHz)
  28. (++) APB2 bus clock (PCLK2)
  29. -@@- PCLK2 and SDIO_CK clock frequencies must respect the following condition:
  30. Frequency(PCLK2) >= (3 / 8 x Frequency(SDIO_CK))
  31. (#) Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDIO, ENABLE).
  32. (#) According to the SDIO mode, enable the GPIO clocks using
  33. RCC_AHB1PeriphClockCmd() function.
  34. The I/O can be one of the following configurations:
  35. (++) 1-bit data length: SDIO_CMD, SDIO_CK and D0.
  36. (++) 4-bit data length: SDIO_CMD, SDIO_CK and D[3:0].
  37. (++) 8-bit data length: SDIO_CMD, SDIO_CK and D[7:0].
  38. (#) Peripheral alternate function:
  39. (++) Connect the pin to the desired peripherals' Alternate Function (AF)
  40. using GPIO_PinAFConfig() function
  41. (++) Configure the desired pin in alternate function by:
  42. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  43. (++) Select the type, pull-up/pull-down and output speed via GPIO_PuPd,
  44. GPIO_OType and GPIO_Speed members
  45. (++) Call GPIO_Init() function
  46. (#) Program the Clock Edge, Clock Bypass, Clock Power Save, Bus Wide,
  47. hardware, flow control and the Clock Divider using the SDIO_Init()
  48. function.
  49. (#) Enable the Power ON State using the SDIO_SetPowerState(SDIO_PowerState_ON)
  50. function.
  51. (#) Enable the clock using the SDIO_ClockCmd() function.
  52. (#) Enable the NVIC and the corresponding interrupt using the function
  53. SDIO_ITConfig() if you need to use interrupt mode.
  54. (#) When using the DMA mode
  55. (++) Configure the DMA using DMA_Init() function
  56. (++) Active the needed channel Request using SDIO_DMACmd() function
  57. (#) Enable the DMA using the DMA_Cmd() function, when using DMA mode.
  58. (#) To control the CPSM (Command Path State Machine) and send
  59. commands to the card use the SDIO_SendCommand(),
  60. SDIO_GetCommandResponse() and SDIO_GetResponse() functions. First, user has
  61. to fill the command structure (pointer to SDIO_CmdInitTypeDef) according
  62. to the selected command to be sent.
  63. The parameters that should be filled are:
  64. (++) Command Argument
  65. (++) Command Index
  66. (++) Command Response type
  67. (++) Command Wait
  68. (++) CPSM Status (Enable or Disable).
  69. -@@- To check if the command is well received, read the SDIO_CMDRESP
  70. register using the SDIO_GetCommandResponse().
  71. The SDIO responses registers (SDIO_RESP1 to SDIO_RESP2), use the
  72. SDIO_GetResponse() function.
  73. (#) To control the DPSM (Data Path State Machine) and send/receive
  74. data to/from the card use the SDIO_DataConfig(), SDIO_GetDataCounter(),
  75. SDIO_ReadData(), SDIO_WriteData() and SDIO_GetFIFOCount() functions.
  76. *** Read Operations ***
  77. =======================
  78. [..]
  79. (#) First, user has to fill the data structure (pointer to
  80. SDIO_DataInitTypeDef) according to the selected data type to be received.
  81. The parameters that should be filled are:
  82. (++) Data TimeOut
  83. (++) Data Length
  84. (++) Data Block size
  85. (++) Data Transfer direction: should be from card (To SDIO)
  86. (++) Data Transfer mode
  87. (++) DPSM Status (Enable or Disable)
  88. (#) Configure the SDIO resources to receive the data from the card
  89. according to selected transfer mode (Refer to Step 8, 9 and 10).
  90. (#) Send the selected Read command (refer to step 11).
  91. (#) Use the SDIO flags/interrupts to check the transfer status.
  92. *** Write Operations ***
  93. ========================
  94. [..]
  95. (#) First, user has to fill the data structure (pointer to
  96. SDIO_DataInitTypeDef) according to the selected data type to be received.
  97. The parameters that should be filled are:
  98. (++) Data TimeOut
  99. (++) Data Length
  100. (++) Data Block size
  101. (++) Data Transfer direction: should be to card (To CARD)
  102. (++) Data Transfer mode
  103. (++) DPSM Status (Enable or Disable)
  104. (#) Configure the SDIO resources to send the data to the card according to
  105. selected transfer mode (Refer to Step 8, 9 and 10).
  106. (#) Send the selected Write command (refer to step 11).
  107. (#) Use the SDIO flags/interrupts to check the transfer status.
  108. @endverbatim
  109. *
  110. *
  111. ******************************************************************************
  112. * @attention
  113. *
  114. * Copyright (c) 2016 STMicroelectronics.
  115. * All rights reserved.
  116. *
  117. * This software is licensed under terms that can be found in the LICENSE file
  118. * in the root directory of this software component.
  119. * If no LICENSE file comes with this software, it is provided AS-IS.
  120. *
  121. ******************************************************************************
  122. */
  123. /* Includes ------------------------------------------------------------------*/
  124. #include "stm32f4xx_sdio.h"
  125. #include "stm32f4xx_rcc.h"
  126. /** @addtogroup STM32F4xx_StdPeriph_Driver
  127. * @{
  128. */
  129. /** @defgroup SDIO
  130. * @brief SDIO driver modules
  131. * @{
  132. */
  133. /* Private typedef -----------------------------------------------------------*/
  134. /* Private define ------------------------------------------------------------*/
  135. /* ------------ SDIO registers bit address in the alias region ----------- */
  136. #define SDIO_OFFSET (SDIO_BASE - PERIPH_BASE)
  137. /* --- CLKCR Register ---*/
  138. /* Alias word address of CLKEN bit */
  139. #define CLKCR_OFFSET (SDIO_OFFSET + 0x04)
  140. #define CLKEN_BitNumber 0x08
  141. #define CLKCR_CLKEN_BB (PERIPH_BB_BASE + (CLKCR_OFFSET * 32) + (CLKEN_BitNumber * 4))
  142. /* --- CMD Register ---*/
  143. /* Alias word address of SDIOSUSPEND bit */
  144. #define CMD_OFFSET (SDIO_OFFSET + 0x0C)
  145. #define SDIOSUSPEND_BitNumber 0x0B
  146. #define CMD_SDIOSUSPEND_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (SDIOSUSPEND_BitNumber * 4))
  147. /* Alias word address of ENCMDCOMPL bit */
  148. #define ENCMDCOMPL_BitNumber 0x0C
  149. #define CMD_ENCMDCOMPL_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (ENCMDCOMPL_BitNumber * 4))
  150. /* Alias word address of NIEN bit */
  151. #define NIEN_BitNumber 0x0D
  152. #define CMD_NIEN_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (NIEN_BitNumber * 4))
  153. /* Alias word address of ATACMD bit */
  154. #define ATACMD_BitNumber 0x0E
  155. #define CMD_ATACMD_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (ATACMD_BitNumber * 4))
  156. /* --- DCTRL Register ---*/
  157. /* Alias word address of DMAEN bit */
  158. #define DCTRL_OFFSET (SDIO_OFFSET + 0x2C)
  159. #define DMAEN_BitNumber 0x03
  160. #define DCTRL_DMAEN_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (DMAEN_BitNumber * 4))
  161. /* Alias word address of RWSTART bit */
  162. #define RWSTART_BitNumber 0x08
  163. #define DCTRL_RWSTART_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTART_BitNumber * 4))
  164. /* Alias word address of RWSTOP bit */
  165. #define RWSTOP_BitNumber 0x09
  166. #define DCTRL_RWSTOP_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTOP_BitNumber * 4))
  167. /* Alias word address of RWMOD bit */
  168. #define RWMOD_BitNumber 0x0A
  169. #define DCTRL_RWMOD_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWMOD_BitNumber * 4))
  170. /* Alias word address of SDIOEN bit */
  171. #define SDIOEN_BitNumber 0x0B
  172. #define DCTRL_SDIOEN_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (SDIOEN_BitNumber * 4))
  173. /* ---------------------- SDIO registers bit mask ------------------------ */
  174. /* --- CLKCR Register ---*/
  175. /* CLKCR register clear mask */
  176. #define CLKCR_CLEAR_MASK ((uint32_t)0xFFFF8100)
  177. /* --- PWRCTRL Register ---*/
  178. /* SDIO PWRCTRL Mask */
  179. #define PWR_PWRCTRL_MASK ((uint32_t)0xFFFFFFFC)
  180. /* --- DCTRL Register ---*/
  181. /* SDIO DCTRL Clear Mask */
  182. #define DCTRL_CLEAR_MASK ((uint32_t)0xFFFFFF08)
  183. /* --- CMD Register ---*/
  184. /* CMD Register clear mask */
  185. #define CMD_CLEAR_MASK ((uint32_t)0xFFFFF800)
  186. /* SDIO RESP Registers Address */
  187. #define SDIO_RESP_ADDR ((uint32_t)(SDIO_BASE + 0x14))
  188. /* Private macro -------------------------------------------------------------*/
  189. /* Private variables ---------------------------------------------------------*/
  190. /* Private function prototypes -----------------------------------------------*/
  191. /* Private functions ---------------------------------------------------------*/
  192. /** @defgroup SDIO_Private_Functions
  193. * @{
  194. */
  195. /** @defgroup SDIO_Group1 Initialization and Configuration functions
  196. * @brief Initialization and Configuration functions
  197. *
  198. @verbatim
  199. ===============================================================================
  200. ##### Initialization and Configuration functions #####
  201. ===============================================================================
  202. @endverbatim
  203. * @{
  204. */
  205. /**
  206. * @brief Deinitializes the SDIO peripheral registers to their default reset values.
  207. * @param None
  208. * @retval None
  209. */
  210. void SDIO_DeInit(void)
  211. {
  212. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SDIO, ENABLE);
  213. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SDIO, DISABLE);
  214. }
  215. /**
  216. * @brief Initializes the SDIO peripheral according to the specified
  217. * parameters in the SDIO_InitStruct.
  218. * @param SDIO_InitStruct : pointer to a SDIO_InitTypeDef structure
  219. * that contains the configuration information for the SDIO peripheral.
  220. * @retval None
  221. */
  222. void SDIO_Init(SDIO_InitTypeDef* SDIO_InitStruct)
  223. {
  224. uint32_t tmpreg = 0;
  225. /* Check the parameters */
  226. assert_param(IS_SDIO_CLOCK_EDGE(SDIO_InitStruct->SDIO_ClockEdge));
  227. assert_param(IS_SDIO_CLOCK_BYPASS(SDIO_InitStruct->SDIO_ClockBypass));
  228. assert_param(IS_SDIO_CLOCK_POWER_SAVE(SDIO_InitStruct->SDIO_ClockPowerSave));
  229. assert_param(IS_SDIO_BUS_WIDE(SDIO_InitStruct->SDIO_BusWide));
  230. assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(SDIO_InitStruct->SDIO_HardwareFlowControl));
  231. /*---------------------------- SDIO CLKCR Configuration ------------------------*/
  232. /* Get the SDIO CLKCR value */
  233. tmpreg = SDIO->CLKCR;
  234. /* Clear CLKDIV, PWRSAV, BYPASS, WIDBUS, NEGEDGE, HWFC_EN bits */
  235. tmpreg &= CLKCR_CLEAR_MASK;
  236. /* Set CLKDIV bits according to SDIO_ClockDiv value */
  237. /* Set PWRSAV bit according to SDIO_ClockPowerSave value */
  238. /* Set BYPASS bit according to SDIO_ClockBypass value */
  239. /* Set WIDBUS bits according to SDIO_BusWide value */
  240. /* Set NEGEDGE bits according to SDIO_ClockEdge value */
  241. /* Set HWFC_EN bits according to SDIO_HardwareFlowControl value */
  242. tmpreg |= (SDIO_InitStruct->SDIO_ClockDiv | SDIO_InitStruct->SDIO_ClockPowerSave |
  243. SDIO_InitStruct->SDIO_ClockBypass | SDIO_InitStruct->SDIO_BusWide |
  244. SDIO_InitStruct->SDIO_ClockEdge | SDIO_InitStruct->SDIO_HardwareFlowControl);
  245. /* Write to SDIO CLKCR */
  246. SDIO->CLKCR = tmpreg;
  247. }
  248. /**
  249. * @brief Fills each SDIO_InitStruct member with its default value.
  250. * @param SDIO_InitStruct: pointer to an SDIO_InitTypeDef structure which
  251. * will be initialized.
  252. * @retval None
  253. */
  254. void SDIO_StructInit(SDIO_InitTypeDef* SDIO_InitStruct)
  255. {
  256. /* SDIO_InitStruct members default value */
  257. SDIO_InitStruct->SDIO_ClockDiv = 0x00;
  258. SDIO_InitStruct->SDIO_ClockEdge = SDIO_ClockEdge_Rising;
  259. SDIO_InitStruct->SDIO_ClockBypass = SDIO_ClockBypass_Disable;
  260. SDIO_InitStruct->SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
  261. SDIO_InitStruct->SDIO_BusWide = SDIO_BusWide_1b;
  262. SDIO_InitStruct->SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
  263. }
  264. /**
  265. * @brief Enables or disables the SDIO Clock.
  266. * @param NewState: new state of the SDIO Clock.
  267. * This parameter can be: ENABLE or DISABLE.
  268. * @retval None
  269. */
  270. void SDIO_ClockCmd(FunctionalState NewState)
  271. {
  272. /* Check the parameters */
  273. assert_param(IS_FUNCTIONAL_STATE(NewState));
  274. *(__IO uint32_t *) CLKCR_CLKEN_BB = (uint32_t)NewState;
  275. }
  276. /**
  277. * @brief Sets the power status of the controller.
  278. * @param SDIO_PowerState: new state of the Power state.
  279. * This parameter can be one of the following values:
  280. * @arg SDIO_PowerState_OFF: SDIO Power OFF
  281. * @arg SDIO_PowerState_ON: SDIO Power ON
  282. * @retval None
  283. */
  284. void SDIO_SetPowerState(uint32_t SDIO_PowerState)
  285. {
  286. /* Check the parameters */
  287. assert_param(IS_SDIO_POWER_STATE(SDIO_PowerState));
  288. SDIO->POWER = SDIO_PowerState;
  289. }
  290. /**
  291. * @brief Gets the power status of the controller.
  292. * @param None
  293. * @retval Power status of the controller. The returned value can be one of the
  294. * following values:
  295. * - 0x00: Power OFF
  296. * - 0x02: Power UP
  297. * - 0x03: Power ON
  298. */
  299. uint32_t SDIO_GetPowerState(void)
  300. {
  301. return (SDIO->POWER & (~PWR_PWRCTRL_MASK));
  302. }
  303. /**
  304. * @}
  305. */
  306. /** @defgroup SDIO_Group2 Command path state machine (CPSM) management functions
  307. * @brief Command path state machine (CPSM) management functions
  308. *
  309. @verbatim
  310. ===============================================================================
  311. ##### Command path state machine (CPSM) management functions #####
  312. ===============================================================================
  313. This section provide functions allowing to program and read the Command path
  314. state machine (CPSM).
  315. @endverbatim
  316. * @{
  317. */
  318. /**
  319. * @brief Initializes the SDIO Command according to the specified
  320. * parameters in the SDIO_CmdInitStruct and send the command.
  321. * @param SDIO_CmdInitStruct : pointer to a SDIO_CmdInitTypeDef
  322. * structure that contains the configuration information for the SDIO
  323. * command.
  324. * @retval None
  325. */
  326. void SDIO_SendCommand(SDIO_CmdInitTypeDef *SDIO_CmdInitStruct)
  327. {
  328. uint32_t tmpreg = 0;
  329. /* Check the parameters */
  330. assert_param(IS_SDIO_CMD_INDEX(SDIO_CmdInitStruct->SDIO_CmdIndex));
  331. assert_param(IS_SDIO_RESPONSE(SDIO_CmdInitStruct->SDIO_Response));
  332. assert_param(IS_SDIO_WAIT(SDIO_CmdInitStruct->SDIO_Wait));
  333. assert_param(IS_SDIO_CPSM(SDIO_CmdInitStruct->SDIO_CPSM));
  334. /*---------------------------- SDIO ARG Configuration ------------------------*/
  335. /* Set the SDIO Argument value */
  336. SDIO->ARG = SDIO_CmdInitStruct->SDIO_Argument;
  337. /*---------------------------- SDIO CMD Configuration ------------------------*/
  338. /* Get the SDIO CMD value */
  339. tmpreg = SDIO->CMD;
  340. /* Clear CMDINDEX, WAITRESP, WAITINT, WAITPEND, CPSMEN bits */
  341. tmpreg &= CMD_CLEAR_MASK;
  342. /* Set CMDINDEX bits according to SDIO_CmdIndex value */
  343. /* Set WAITRESP bits according to SDIO_Response value */
  344. /* Set WAITINT and WAITPEND bits according to SDIO_Wait value */
  345. /* Set CPSMEN bits according to SDIO_CPSM value */
  346. tmpreg |= (uint32_t)SDIO_CmdInitStruct->SDIO_CmdIndex | SDIO_CmdInitStruct->SDIO_Response
  347. | SDIO_CmdInitStruct->SDIO_Wait | SDIO_CmdInitStruct->SDIO_CPSM;
  348. /* Write to SDIO CMD */
  349. SDIO->CMD = tmpreg;
  350. }
  351. /**
  352. * @brief Fills each SDIO_CmdInitStruct member with its default value.
  353. * @param SDIO_CmdInitStruct: pointer to an SDIO_CmdInitTypeDef
  354. * structure which will be initialized.
  355. * @retval None
  356. */
  357. void SDIO_CmdStructInit(SDIO_CmdInitTypeDef* SDIO_CmdInitStruct)
  358. {
  359. /* SDIO_CmdInitStruct members default value */
  360. SDIO_CmdInitStruct->SDIO_Argument = 0x00;
  361. SDIO_CmdInitStruct->SDIO_CmdIndex = 0x00;
  362. SDIO_CmdInitStruct->SDIO_Response = SDIO_Response_No;
  363. SDIO_CmdInitStruct->SDIO_Wait = SDIO_Wait_No;
  364. SDIO_CmdInitStruct->SDIO_CPSM = SDIO_CPSM_Disable;
  365. }
  366. /**
  367. * @brief Returns command index of last command for which response received.
  368. * @param None
  369. * @retval Returns the command index of the last command response received.
  370. */
  371. uint8_t SDIO_GetCommandResponse(void)
  372. {
  373. return (uint8_t)(SDIO->RESPCMD);
  374. }
  375. /**
  376. * @brief Returns response received from the card for the last command.
  377. * @param SDIO_RESP: Specifies the SDIO response register.
  378. * This parameter can be one of the following values:
  379. * @arg SDIO_RESP1: Response Register 1
  380. * @arg SDIO_RESP2: Response Register 2
  381. * @arg SDIO_RESP3: Response Register 3
  382. * @arg SDIO_RESP4: Response Register 4
  383. * @retval The Corresponding response register value.
  384. */
  385. uint32_t SDIO_GetResponse(uint32_t SDIO_RESP)
  386. {
  387. __IO uint32_t tmp = 0;
  388. /* Check the parameters */
  389. assert_param(IS_SDIO_RESP(SDIO_RESP));
  390. tmp = SDIO_RESP_ADDR + SDIO_RESP;
  391. return (*(__IO uint32_t *) tmp);
  392. }
  393. /**
  394. * @}
  395. */
  396. /** @defgroup SDIO_Group3 Data path state machine (DPSM) management functions
  397. * @brief Data path state machine (DPSM) management functions
  398. *
  399. @verbatim
  400. ===============================================================================
  401. ##### Data path state machine (DPSM) management functions #####
  402. ===============================================================================
  403. This section provide functions allowing to program and read the Data path
  404. state machine (DPSM).
  405. @endverbatim
  406. * @{
  407. */
  408. /**
  409. * @brief Initializes the SDIO data path according to the specified
  410. * parameters in the SDIO_DataInitStruct.
  411. * @param SDIO_DataInitStruct : pointer to a SDIO_DataInitTypeDef structure
  412. * that contains the configuration information for the SDIO command.
  413. * @retval None
  414. */
  415. void SDIO_DataConfig(SDIO_DataInitTypeDef* SDIO_DataInitStruct)
  416. {
  417. uint32_t tmpreg = 0;
  418. /* Check the parameters */
  419. assert_param(IS_SDIO_DATA_LENGTH(SDIO_DataInitStruct->SDIO_DataLength));
  420. assert_param(IS_SDIO_BLOCK_SIZE(SDIO_DataInitStruct->SDIO_DataBlockSize));
  421. assert_param(IS_SDIO_TRANSFER_DIR(SDIO_DataInitStruct->SDIO_TransferDir));
  422. assert_param(IS_SDIO_TRANSFER_MODE(SDIO_DataInitStruct->SDIO_TransferMode));
  423. assert_param(IS_SDIO_DPSM(SDIO_DataInitStruct->SDIO_DPSM));
  424. /*---------------------------- SDIO DTIMER Configuration ---------------------*/
  425. /* Set the SDIO Data TimeOut value */
  426. SDIO->DTIMER = SDIO_DataInitStruct->SDIO_DataTimeOut;
  427. /*---------------------------- SDIO DLEN Configuration -----------------------*/
  428. /* Set the SDIO DataLength value */
  429. SDIO->DLEN = SDIO_DataInitStruct->SDIO_DataLength;
  430. /*---------------------------- SDIO DCTRL Configuration ----------------------*/
  431. /* Get the SDIO DCTRL value */
  432. tmpreg = SDIO->DCTRL;
  433. /* Clear DEN, DTMODE, DTDIR and DBCKSIZE bits */
  434. tmpreg &= DCTRL_CLEAR_MASK;
  435. /* Set DEN bit according to SDIO_DPSM value */
  436. /* Set DTMODE bit according to SDIO_TransferMode value */
  437. /* Set DTDIR bit according to SDIO_TransferDir value */
  438. /* Set DBCKSIZE bits according to SDIO_DataBlockSize value */
  439. tmpreg |= (uint32_t)SDIO_DataInitStruct->SDIO_DataBlockSize | SDIO_DataInitStruct->SDIO_TransferDir
  440. | SDIO_DataInitStruct->SDIO_TransferMode | SDIO_DataInitStruct->SDIO_DPSM;
  441. /* Write to SDIO DCTRL */
  442. SDIO->DCTRL = tmpreg;
  443. }
  444. /**
  445. * @brief Fills each SDIO_DataInitStruct member with its default value.
  446. * @param SDIO_DataInitStruct: pointer to an SDIO_DataInitTypeDef structure
  447. * which will be initialized.
  448. * @retval None
  449. */
  450. void SDIO_DataStructInit(SDIO_DataInitTypeDef* SDIO_DataInitStruct)
  451. {
  452. /* SDIO_DataInitStruct members default value */
  453. SDIO_DataInitStruct->SDIO_DataTimeOut = 0xFFFFFFFF;
  454. SDIO_DataInitStruct->SDIO_DataLength = 0x00;
  455. SDIO_DataInitStruct->SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
  456. SDIO_DataInitStruct->SDIO_TransferDir = SDIO_TransferDir_ToCard;
  457. SDIO_DataInitStruct->SDIO_TransferMode = SDIO_TransferMode_Block;
  458. SDIO_DataInitStruct->SDIO_DPSM = SDIO_DPSM_Disable;
  459. }
  460. /**
  461. * @brief Returns number of remaining data bytes to be transferred.
  462. * @param None
  463. * @retval Number of remaining data bytes to be transferred
  464. */
  465. uint32_t SDIO_GetDataCounter(void)
  466. {
  467. return SDIO->DCOUNT;
  468. }
  469. /**
  470. * @brief Read one data word from Rx FIFO.
  471. * @param None
  472. * @retval Data received
  473. */
  474. uint32_t SDIO_ReadData(void)
  475. {
  476. return SDIO->FIFO;
  477. }
  478. /**
  479. * @brief Write one data word to Tx FIFO.
  480. * @param Data: 32-bit data word to write.
  481. * @retval None
  482. */
  483. void SDIO_WriteData(uint32_t Data)
  484. {
  485. SDIO->FIFO = Data;
  486. }
  487. /**
  488. * @brief Returns the number of words left to be written to or read from FIFO.
  489. * @param None
  490. * @retval Remaining number of words.
  491. */
  492. uint32_t SDIO_GetFIFOCount(void)
  493. {
  494. return SDIO->FIFOCNT;
  495. }
  496. /**
  497. * @}
  498. */
  499. /** @defgroup SDIO_Group4 SDIO IO Cards mode management functions
  500. * @brief SDIO IO Cards mode management functions
  501. *
  502. @verbatim
  503. ===============================================================================
  504. ##### SDIO IO Cards mode management functions #####
  505. ===============================================================================
  506. This section provide functions allowing to program and read the SDIO IO Cards.
  507. @endverbatim
  508. * @{
  509. */
  510. /**
  511. * @brief Starts the SD I/O Read Wait operation.
  512. * @param NewState: new state of the Start SDIO Read Wait operation.
  513. * This parameter can be: ENABLE or DISABLE.
  514. * @retval None
  515. */
  516. void SDIO_StartSDIOReadWait(FunctionalState NewState)
  517. {
  518. /* Check the parameters */
  519. assert_param(IS_FUNCTIONAL_STATE(NewState));
  520. *(__IO uint32_t *) DCTRL_RWSTART_BB = (uint32_t) NewState;
  521. }
  522. /**
  523. * @brief Stops the SD I/O Read Wait operation.
  524. * @param NewState: new state of the Stop SDIO Read Wait operation.
  525. * This parameter can be: ENABLE or DISABLE.
  526. * @retval None
  527. */
  528. void SDIO_StopSDIOReadWait(FunctionalState NewState)
  529. {
  530. /* Check the parameters */
  531. assert_param(IS_FUNCTIONAL_STATE(NewState));
  532. *(__IO uint32_t *) DCTRL_RWSTOP_BB = (uint32_t) NewState;
  533. }
  534. /**
  535. * @brief Sets one of the two options of inserting read wait interval.
  536. * @param SDIO_ReadWaitMode: SD I/O Read Wait operation mode.
  537. * This parameter can be:
  538. * @arg SDIO_ReadWaitMode_CLK: Read Wait control by stopping SDIOCLK
  539. * @arg SDIO_ReadWaitMode_DATA2: Read Wait control using SDIO_DATA2
  540. * @retval None
  541. */
  542. void SDIO_SetSDIOReadWaitMode(uint32_t SDIO_ReadWaitMode)
  543. {
  544. /* Check the parameters */
  545. assert_param(IS_SDIO_READWAIT_MODE(SDIO_ReadWaitMode));
  546. *(__IO uint32_t *) DCTRL_RWMOD_BB = SDIO_ReadWaitMode;
  547. }
  548. /**
  549. * @brief Enables or disables the SD I/O Mode Operation.
  550. * @param NewState: new state of SDIO specific operation.
  551. * This parameter can be: ENABLE or DISABLE.
  552. * @retval None
  553. */
  554. void SDIO_SetSDIOOperation(FunctionalState NewState)
  555. {
  556. /* Check the parameters */
  557. assert_param(IS_FUNCTIONAL_STATE(NewState));
  558. *(__IO uint32_t *) DCTRL_SDIOEN_BB = (uint32_t)NewState;
  559. }
  560. /**
  561. * @brief Enables or disables the SD I/O Mode suspend command sending.
  562. * @param NewState: new state of the SD I/O Mode suspend command.
  563. * This parameter can be: ENABLE or DISABLE.
  564. * @retval None
  565. */
  566. void SDIO_SendSDIOSuspendCmd(FunctionalState NewState)
  567. {
  568. /* Check the parameters */
  569. assert_param(IS_FUNCTIONAL_STATE(NewState));
  570. *(__IO uint32_t *) CMD_SDIOSUSPEND_BB = (uint32_t)NewState;
  571. }
  572. /**
  573. * @}
  574. */
  575. /** @defgroup SDIO_Group5 CE-ATA mode management functions
  576. * @brief CE-ATA mode management functions
  577. *
  578. @verbatim
  579. ===============================================================================
  580. ##### CE-ATA mode management functions #####
  581. ===============================================================================
  582. This section provide functions allowing to program and read the CE-ATA card.
  583. @endverbatim
  584. * @{
  585. */
  586. /**
  587. * @brief Enables or disables the command completion signal.
  588. * @param NewState: new state of command completion signal.
  589. * This parameter can be: ENABLE or DISABLE.
  590. * @retval None
  591. */
  592. void SDIO_CommandCompletionCmd(FunctionalState NewState)
  593. {
  594. /* Check the parameters */
  595. assert_param(IS_FUNCTIONAL_STATE(NewState));
  596. *(__IO uint32_t *) CMD_ENCMDCOMPL_BB = (uint32_t)NewState;
  597. }
  598. /**
  599. * @brief Enables or disables the CE-ATA interrupt.
  600. * @param NewState: new state of CE-ATA interrupt.
  601. * This parameter can be: ENABLE or DISABLE.
  602. * @retval None
  603. */
  604. void SDIO_CEATAITCmd(FunctionalState NewState)
  605. {
  606. /* Check the parameters */
  607. assert_param(IS_FUNCTIONAL_STATE(NewState));
  608. *(__IO uint32_t *) CMD_NIEN_BB = (uint32_t)((~((uint32_t)NewState)) & ((uint32_t)0x1));
  609. }
  610. /**
  611. * @brief Sends CE-ATA command (CMD61).
  612. * @param NewState: new state of CE-ATA command.
  613. * This parameter can be: ENABLE or DISABLE.
  614. * @retval None
  615. */
  616. void SDIO_SendCEATACmd(FunctionalState NewState)
  617. {
  618. /* Check the parameters */
  619. assert_param(IS_FUNCTIONAL_STATE(NewState));
  620. *(__IO uint32_t *) CMD_ATACMD_BB = (uint32_t)NewState;
  621. }
  622. /**
  623. * @}
  624. */
  625. /** @defgroup SDIO_Group6 DMA transfers management functions
  626. * @brief DMA transfers management functions
  627. *
  628. @verbatim
  629. ===============================================================================
  630. ##### DMA transfers management functions #####
  631. ===============================================================================
  632. This section provide functions allowing to program SDIO DMA transfer.
  633. @endverbatim
  634. * @{
  635. */
  636. /**
  637. * @brief Enables or disables the SDIO DMA request.
  638. * @param NewState: new state of the selected SDIO DMA request.
  639. * This parameter can be: ENABLE or DISABLE.
  640. * @retval None
  641. */
  642. void SDIO_DMACmd(FunctionalState NewState)
  643. {
  644. /* Check the parameters */
  645. assert_param(IS_FUNCTIONAL_STATE(NewState));
  646. *(__IO uint32_t *) DCTRL_DMAEN_BB = (uint32_t)NewState;
  647. }
  648. /**
  649. * @}
  650. */
  651. /** @defgroup SDIO_Group7 Interrupts and flags management functions
  652. * @brief Interrupts and flags management functions
  653. *
  654. @verbatim
  655. ===============================================================================
  656. ##### Interrupts and flags management functions #####
  657. ===============================================================================
  658. @endverbatim
  659. * @{
  660. */
  661. /**
  662. * @brief Enables or disables the SDIO interrupts.
  663. * @param SDIO_IT: specifies the SDIO interrupt sources to be enabled or disabled.
  664. * This parameter can be one or a combination of the following values:
  665. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  666. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  667. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  668. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  669. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  670. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  671. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  672. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  673. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIDCOUNT, is zero) interrupt
  674. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  675. * bus mode interrupt
  676. * @arg SDIO_IT_DBCKEND: Data block sent/received (CRC check passed) interrupt
  677. * @arg SDIO_IT_CMDACT: Command transfer in progress interrupt
  678. * @arg SDIO_IT_TXACT: Data transmit in progress interrupt
  679. * @arg SDIO_IT_RXACT: Data receive in progress interrupt
  680. * @arg SDIO_IT_TXFIFOHE: Transmit FIFO Half Empty interrupt
  681. * @arg SDIO_IT_RXFIFOHF: Receive FIFO Half Full interrupt
  682. * @arg SDIO_IT_TXFIFOF: Transmit FIFO full interrupt
  683. * @arg SDIO_IT_RXFIFOF: Receive FIFO full interrupt
  684. * @arg SDIO_IT_TXFIFOE: Transmit FIFO empty interrupt
  685. * @arg SDIO_IT_RXFIFOE: Receive FIFO empty interrupt
  686. * @arg SDIO_IT_TXDAVL: Data available in transmit FIFO interrupt
  687. * @arg SDIO_IT_RXDAVL: Data available in receive FIFO interrupt
  688. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  689. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61 interrupt
  690. * @param NewState: new state of the specified SDIO interrupts.
  691. * This parameter can be: ENABLE or DISABLE.
  692. * @retval None
  693. */
  694. void SDIO_ITConfig(uint32_t SDIO_IT, FunctionalState NewState)
  695. {
  696. /* Check the parameters */
  697. assert_param(IS_SDIO_IT(SDIO_IT));
  698. assert_param(IS_FUNCTIONAL_STATE(NewState));
  699. if (NewState != DISABLE)
  700. {
  701. /* Enable the SDIO interrupts */
  702. SDIO->MASK |= SDIO_IT;
  703. }
  704. else
  705. {
  706. /* Disable the SDIO interrupts */
  707. SDIO->MASK &= ~SDIO_IT;
  708. }
  709. }
  710. /**
  711. * @brief Checks whether the specified SDIO flag is set or not.
  712. * @param SDIO_FLAG: specifies the flag to check.
  713. * This parameter can be one of the following values:
  714. * @arg SDIO_FLAG_CCRCFAIL: Command response received (CRC check failed)
  715. * @arg SDIO_FLAG_DCRCFAIL: Data block sent/received (CRC check failed)
  716. * @arg SDIO_FLAG_CTIMEOUT: Command response timeout
  717. * @arg SDIO_FLAG_DTIMEOUT: Data timeout
  718. * @arg SDIO_FLAG_TXUNDERR: Transmit FIFO underrun error
  719. * @arg SDIO_FLAG_RXOVERR: Received FIFO overrun error
  720. * @arg SDIO_FLAG_CMDREND: Command response received (CRC check passed)
  721. * @arg SDIO_FLAG_CMDSENT: Command sent (no response required)
  722. * @arg SDIO_FLAG_DATAEND: Data end (data counter, SDIDCOUNT, is zero)
  723. * @arg SDIO_FLAG_STBITERR: Start bit not detected on all data signals in wide bus mode.
  724. * @arg SDIO_FLAG_DBCKEND: Data block sent/received (CRC check passed)
  725. * @arg SDIO_FLAG_CMDACT: Command transfer in progress
  726. * @arg SDIO_FLAG_TXACT: Data transmit in progress
  727. * @arg SDIO_FLAG_RXACT: Data receive in progress
  728. * @arg SDIO_FLAG_TXFIFOHE: Transmit FIFO Half Empty
  729. * @arg SDIO_FLAG_RXFIFOHF: Receive FIFO Half Full
  730. * @arg SDIO_FLAG_TXFIFOF: Transmit FIFO full
  731. * @arg SDIO_FLAG_RXFIFOF: Receive FIFO full
  732. * @arg SDIO_FLAG_TXFIFOE: Transmit FIFO empty
  733. * @arg SDIO_FLAG_RXFIFOE: Receive FIFO empty
  734. * @arg SDIO_FLAG_TXDAVL: Data available in transmit FIFO
  735. * @arg SDIO_FLAG_RXDAVL: Data available in receive FIFO
  736. * @arg SDIO_FLAG_SDIOIT: SD I/O interrupt received
  737. * @arg SDIO_FLAG_CEATAEND: CE-ATA command completion signal received for CMD61
  738. * @retval The new state of SDIO_FLAG (SET or RESET).
  739. */
  740. FlagStatus SDIO_GetFlagStatus(uint32_t SDIO_FLAG)
  741. {
  742. FlagStatus bitstatus = RESET;
  743. /* Check the parameters */
  744. assert_param(IS_SDIO_FLAG(SDIO_FLAG));
  745. if ((SDIO->STA & SDIO_FLAG) != (uint32_t)RESET)
  746. {
  747. bitstatus = SET;
  748. }
  749. else
  750. {
  751. bitstatus = RESET;
  752. }
  753. return bitstatus;
  754. }
  755. /**
  756. * @brief Clears the SDIO's pending flags.
  757. * @param SDIO_FLAG: specifies the flag to clear.
  758. * This parameter can be one or a combination of the following values:
  759. * @arg SDIO_FLAG_CCRCFAIL: Command response received (CRC check failed)
  760. * @arg SDIO_FLAG_DCRCFAIL: Data block sent/received (CRC check failed)
  761. * @arg SDIO_FLAG_CTIMEOUT: Command response timeout
  762. * @arg SDIO_FLAG_DTIMEOUT: Data timeout
  763. * @arg SDIO_FLAG_TXUNDERR: Transmit FIFO underrun error
  764. * @arg SDIO_FLAG_RXOVERR: Received FIFO overrun error
  765. * @arg SDIO_FLAG_CMDREND: Command response received (CRC check passed)
  766. * @arg SDIO_FLAG_CMDSENT: Command sent (no response required)
  767. * @arg SDIO_FLAG_DATAEND: Data end (data counter, SDIDCOUNT, is zero)
  768. * @arg SDIO_FLAG_STBITERR: Start bit not detected on all data signals in wide bus mode
  769. * @arg SDIO_FLAG_DBCKEND: Data block sent/received (CRC check passed)
  770. * @arg SDIO_FLAG_SDIOIT: SD I/O interrupt received
  771. * @arg SDIO_FLAG_CEATAEND: CE-ATA command completion signal received for CMD61
  772. * @retval None
  773. */
  774. void SDIO_ClearFlag(uint32_t SDIO_FLAG)
  775. {
  776. /* Check the parameters */
  777. assert_param(IS_SDIO_CLEAR_FLAG(SDIO_FLAG));
  778. SDIO->ICR = SDIO_FLAG;
  779. }
  780. /**
  781. * @brief Checks whether the specified SDIO interrupt has occurred or not.
  782. * @param SDIO_IT: specifies the SDIO interrupt source to check.
  783. * This parameter can be one of the following values:
  784. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  785. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  786. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  787. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  788. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  789. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  790. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  791. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  792. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIDCOUNT, is zero) interrupt
  793. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  794. * bus mode interrupt
  795. * @arg SDIO_IT_DBCKEND: Data block sent/received (CRC check passed) interrupt
  796. * @arg SDIO_IT_CMDACT: Command transfer in progress interrupt
  797. * @arg SDIO_IT_TXACT: Data transmit in progress interrupt
  798. * @arg SDIO_IT_RXACT: Data receive in progress interrupt
  799. * @arg SDIO_IT_TXFIFOHE: Transmit FIFO Half Empty interrupt
  800. * @arg SDIO_IT_RXFIFOHF: Receive FIFO Half Full interrupt
  801. * @arg SDIO_IT_TXFIFOF: Transmit FIFO full interrupt
  802. * @arg SDIO_IT_RXFIFOF: Receive FIFO full interrupt
  803. * @arg SDIO_IT_TXFIFOE: Transmit FIFO empty interrupt
  804. * @arg SDIO_IT_RXFIFOE: Receive FIFO empty interrupt
  805. * @arg SDIO_IT_TXDAVL: Data available in transmit FIFO interrupt
  806. * @arg SDIO_IT_RXDAVL: Data available in receive FIFO interrupt
  807. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  808. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61 interrupt
  809. * @retval The new state of SDIO_IT (SET or RESET).
  810. */
  811. ITStatus SDIO_GetITStatus(uint32_t SDIO_IT)
  812. {
  813. ITStatus bitstatus = RESET;
  814. /* Check the parameters */
  815. assert_param(IS_SDIO_GET_IT(SDIO_IT));
  816. if ((SDIO->STA & SDIO_IT) != (uint32_t)RESET)
  817. {
  818. bitstatus = SET;
  819. }
  820. else
  821. {
  822. bitstatus = RESET;
  823. }
  824. return bitstatus;
  825. }
  826. /**
  827. * @brief Clears the SDIO's interrupt pending bits.
  828. * @param SDIO_IT: specifies the interrupt pending bit to clear.
  829. * This parameter can be one or a combination of the following values:
  830. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  831. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  832. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  833. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  834. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  835. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  836. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  837. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  838. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIO_DCOUNT, is zero) interrupt
  839. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  840. * bus mode interrupt
  841. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  842. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61
  843. * @retval None
  844. */
  845. void SDIO_ClearITPendingBit(uint32_t SDIO_IT)
  846. {
  847. /* Check the parameters */
  848. assert_param(IS_SDIO_CLEAR_IT(SDIO_IT));
  849. SDIO->ICR = SDIO_IT;
  850. }
  851. /**
  852. * @}
  853. */
  854. /**
  855. * @}
  856. */
  857. /**
  858. * @}
  859. */
  860. /**
  861. * @}
  862. */