stm32f4xx_sdio.c 39 KB

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