stm32f4xx_sdio.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_sdio.c
  4. * @author MCD Application Team
  5. * @version V1.0.2
  6. * @date 05-March-2012
  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. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  138. *
  139. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  140. * You may not use this file except in compliance with the License.
  141. * You may obtain a copy of the License at:
  142. *
  143. * http://www.st.com/software_license_agreement_liberty_v2
  144. *
  145. * Unless required by applicable law or agreed to in writing, software
  146. * distributed under the License is distributed on an "AS IS" BASIS,
  147. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  148. * See the License for the specific language governing permissions and
  149. * limitations under the License.
  150. *
  151. ******************************************************************************
  152. */
  153. /* Includes ------------------------------------------------------------------*/
  154. #include "stm32f4xx_sdio.h"
  155. #include "stm32f4xx_rcc.h"
  156. /** @addtogroup STM32F4xx_StdPeriph_Driver
  157. * @{
  158. */
  159. /** @defgroup SDIO
  160. * @brief SDIO driver modules
  161. * @{
  162. */
  163. /* Private typedef -----------------------------------------------------------*/
  164. /* Private define ------------------------------------------------------------*/
  165. /* ------------ SDIO registers bit address in the alias region ----------- */
  166. #define SDIO_OFFSET (SDIO_BASE - PERIPH_BASE)
  167. /* --- CLKCR Register ---*/
  168. /* Alias word address of CLKEN bit */
  169. #define CLKCR_OFFSET (SDIO_OFFSET + 0x04)
  170. #define CLKEN_BitNumber 0x08
  171. #define CLKCR_CLKEN_BB (PERIPH_BB_BASE + (CLKCR_OFFSET * 32) + (CLKEN_BitNumber * 4))
  172. /* --- CMD Register ---*/
  173. /* Alias word address of SDIOSUSPEND bit */
  174. #define CMD_OFFSET (SDIO_OFFSET + 0x0C)
  175. #define SDIOSUSPEND_BitNumber 0x0B
  176. #define CMD_SDIOSUSPEND_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (SDIOSUSPEND_BitNumber * 4))
  177. /* Alias word address of ENCMDCOMPL bit */
  178. #define ENCMDCOMPL_BitNumber 0x0C
  179. #define CMD_ENCMDCOMPL_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (ENCMDCOMPL_BitNumber * 4))
  180. /* Alias word address of NIEN bit */
  181. #define NIEN_BitNumber 0x0D
  182. #define CMD_NIEN_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (NIEN_BitNumber * 4))
  183. /* Alias word address of ATACMD bit */
  184. #define ATACMD_BitNumber 0x0E
  185. #define CMD_ATACMD_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (ATACMD_BitNumber * 4))
  186. /* --- DCTRL Register ---*/
  187. /* Alias word address of DMAEN bit */
  188. #define DCTRL_OFFSET (SDIO_OFFSET + 0x2C)
  189. #define DMAEN_BitNumber 0x03
  190. #define DCTRL_DMAEN_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (DMAEN_BitNumber * 4))
  191. /* Alias word address of RWSTART bit */
  192. #define RWSTART_BitNumber 0x08
  193. #define DCTRL_RWSTART_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTART_BitNumber * 4))
  194. /* Alias word address of RWSTOP bit */
  195. #define RWSTOP_BitNumber 0x09
  196. #define DCTRL_RWSTOP_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTOP_BitNumber * 4))
  197. /* Alias word address of RWMOD bit */
  198. #define RWMOD_BitNumber 0x0A
  199. #define DCTRL_RWMOD_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWMOD_BitNumber * 4))
  200. /* Alias word address of SDIOEN bit */
  201. #define SDIOEN_BitNumber 0x0B
  202. #define DCTRL_SDIOEN_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (SDIOEN_BitNumber * 4))
  203. /* ---------------------- SDIO registers bit mask ------------------------ */
  204. /* --- CLKCR Register ---*/
  205. /* CLKCR register clear mask */
  206. #define CLKCR_CLEAR_MASK ((uint32_t)0xFFFF8100)
  207. /* --- PWRCTRL Register ---*/
  208. /* SDIO PWRCTRL Mask */
  209. #define PWR_PWRCTRL_MASK ((uint32_t)0xFFFFFFFC)
  210. /* --- DCTRL Register ---*/
  211. /* SDIO DCTRL Clear Mask */
  212. #define DCTRL_CLEAR_MASK ((uint32_t)0xFFFFFF08)
  213. /* --- CMD Register ---*/
  214. /* CMD Register clear mask */
  215. #define CMD_CLEAR_MASK ((uint32_t)0xFFFFF800)
  216. /* SDIO RESP Registers Address */
  217. #define SDIO_RESP_ADDR ((uint32_t)(SDIO_BASE + 0x14))
  218. /* Private macro -------------------------------------------------------------*/
  219. /* Private variables ---------------------------------------------------------*/
  220. /* Private function prototypes -----------------------------------------------*/
  221. /* Private functions ---------------------------------------------------------*/
  222. /** @defgroup SDIO_Private_Functions
  223. * @{
  224. */
  225. /** @defgroup SDIO_Group1 Initialization and Configuration functions
  226. * @brief Initialization and Configuration functions
  227. *
  228. @verbatim
  229. ===============================================================================
  230. Initialization and Configuration functions
  231. ===============================================================================
  232. @endverbatim
  233. * @{
  234. */
  235. /**
  236. * @brief Deinitializes the SDIO peripheral registers to their default reset values.
  237. * @param None
  238. * @retval None
  239. */
  240. void SDIO_DeInit(void)
  241. {
  242. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SDIO, ENABLE);
  243. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SDIO, DISABLE);
  244. }
  245. /**
  246. * @brief Initializes the SDIO peripheral according to the specified
  247. * parameters in the SDIO_InitStruct.
  248. * @param SDIO_InitStruct : pointer to a SDIO_InitTypeDef structure
  249. * that contains the configuration information for the SDIO peripheral.
  250. * @retval None
  251. */
  252. void SDIO_Init(SDIO_InitTypeDef* SDIO_InitStruct)
  253. {
  254. uint32_t tmpreg = 0;
  255. /* Check the parameters */
  256. assert_param(IS_SDIO_CLOCK_EDGE(SDIO_InitStruct->SDIO_ClockEdge));
  257. assert_param(IS_SDIO_CLOCK_BYPASS(SDIO_InitStruct->SDIO_ClockBypass));
  258. assert_param(IS_SDIO_CLOCK_POWER_SAVE(SDIO_InitStruct->SDIO_ClockPowerSave));
  259. assert_param(IS_SDIO_BUS_WIDE(SDIO_InitStruct->SDIO_BusWide));
  260. assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(SDIO_InitStruct->SDIO_HardwareFlowControl));
  261. /*---------------------------- SDIO CLKCR Configuration ------------------------*/
  262. /* Get the SDIO CLKCR value */
  263. tmpreg = SDIO->CLKCR;
  264. /* Clear CLKDIV, PWRSAV, BYPASS, WIDBUS, NEGEDGE, HWFC_EN bits */
  265. tmpreg &= CLKCR_CLEAR_MASK;
  266. /* Set CLKDIV bits according to SDIO_ClockDiv value */
  267. /* Set PWRSAV bit according to SDIO_ClockPowerSave value */
  268. /* Set BYPASS bit according to SDIO_ClockBypass value */
  269. /* Set WIDBUS bits according to SDIO_BusWide value */
  270. /* Set NEGEDGE bits according to SDIO_ClockEdge value */
  271. /* Set HWFC_EN bits according to SDIO_HardwareFlowControl value */
  272. tmpreg |= (SDIO_InitStruct->SDIO_ClockDiv | SDIO_InitStruct->SDIO_ClockPowerSave |
  273. SDIO_InitStruct->SDIO_ClockBypass | SDIO_InitStruct->SDIO_BusWide |
  274. SDIO_InitStruct->SDIO_ClockEdge | SDIO_InitStruct->SDIO_HardwareFlowControl);
  275. /* Write to SDIO CLKCR */
  276. SDIO->CLKCR = tmpreg;
  277. }
  278. /**
  279. * @brief Fills each SDIO_InitStruct member with its default value.
  280. * @param SDIO_InitStruct: pointer to an SDIO_InitTypeDef structure which
  281. * will be initialized.
  282. * @retval None
  283. */
  284. void SDIO_StructInit(SDIO_InitTypeDef* SDIO_InitStruct)
  285. {
  286. /* SDIO_InitStruct members default value */
  287. SDIO_InitStruct->SDIO_ClockDiv = 0x00;
  288. SDIO_InitStruct->SDIO_ClockEdge = SDIO_ClockEdge_Rising;
  289. SDIO_InitStruct->SDIO_ClockBypass = SDIO_ClockBypass_Disable;
  290. SDIO_InitStruct->SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
  291. SDIO_InitStruct->SDIO_BusWide = SDIO_BusWide_1b;
  292. SDIO_InitStruct->SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
  293. }
  294. /**
  295. * @brief Enables or disables the SDIO Clock.
  296. * @param NewState: new state of the SDIO Clock.
  297. * This parameter can be: ENABLE or DISABLE.
  298. * @retval None
  299. */
  300. void SDIO_ClockCmd(FunctionalState NewState)
  301. {
  302. /* Check the parameters */
  303. assert_param(IS_FUNCTIONAL_STATE(NewState));
  304. *(__IO uint32_t *) CLKCR_CLKEN_BB = (uint32_t)NewState;
  305. }
  306. /**
  307. * @brief Sets the power status of the controller.
  308. * @param SDIO_PowerState: new state of the Power state.
  309. * This parameter can be one of the following values:
  310. * @arg SDIO_PowerState_OFF: SDIO Power OFF
  311. * @arg SDIO_PowerState_ON: SDIO Power ON
  312. * @retval None
  313. */
  314. void SDIO_SetPowerState(uint32_t SDIO_PowerState)
  315. {
  316. /* Check the parameters */
  317. assert_param(IS_SDIO_POWER_STATE(SDIO_PowerState));
  318. SDIO->POWER = SDIO_PowerState;
  319. }
  320. /**
  321. * @brief Gets the power status of the controller.
  322. * @param None
  323. * @retval Power status of the controller. The returned value can be one of the
  324. * following values:
  325. * - 0x00: Power OFF
  326. * - 0x02: Power UP
  327. * - 0x03: Power ON
  328. */
  329. uint32_t SDIO_GetPowerState(void)
  330. {
  331. return (SDIO->POWER & (~PWR_PWRCTRL_MASK));
  332. }
  333. /**
  334. * @}
  335. */
  336. /** @defgroup SDIO_Group2 Command path state machine (CPSM) management functions
  337. * @brief Command path state machine (CPSM) management functions
  338. *
  339. @verbatim
  340. ===============================================================================
  341. Command path state machine (CPSM) management functions
  342. ===============================================================================
  343. This section provide functions allowing to program and read the Command path
  344. state machine (CPSM).
  345. @endverbatim
  346. * @{
  347. */
  348. /**
  349. * @brief Initializes the SDIO Command according to the specified
  350. * parameters in the SDIO_CmdInitStruct and send the command.
  351. * @param SDIO_CmdInitStruct : pointer to a SDIO_CmdInitTypeDef
  352. * structure that contains the configuration information for the SDIO
  353. * command.
  354. * @retval None
  355. */
  356. void SDIO_SendCommand(SDIO_CmdInitTypeDef *SDIO_CmdInitStruct)
  357. {
  358. uint32_t tmpreg = 0;
  359. /* Check the parameters */
  360. assert_param(IS_SDIO_CMD_INDEX(SDIO_CmdInitStruct->SDIO_CmdIndex));
  361. assert_param(IS_SDIO_RESPONSE(SDIO_CmdInitStruct->SDIO_Response));
  362. assert_param(IS_SDIO_WAIT(SDIO_CmdInitStruct->SDIO_Wait));
  363. assert_param(IS_SDIO_CPSM(SDIO_CmdInitStruct->SDIO_CPSM));
  364. /*---------------------------- SDIO ARG Configuration ------------------------*/
  365. /* Set the SDIO Argument value */
  366. SDIO->ARG = SDIO_CmdInitStruct->SDIO_Argument;
  367. /*---------------------------- SDIO CMD Configuration ------------------------*/
  368. /* Get the SDIO CMD value */
  369. tmpreg = SDIO->CMD;
  370. /* Clear CMDINDEX, WAITRESP, WAITINT, WAITPEND, CPSMEN bits */
  371. tmpreg &= CMD_CLEAR_MASK;
  372. /* Set CMDINDEX bits according to SDIO_CmdIndex value */
  373. /* Set WAITRESP bits according to SDIO_Response value */
  374. /* Set WAITINT and WAITPEND bits according to SDIO_Wait value */
  375. /* Set CPSMEN bits according to SDIO_CPSM value */
  376. tmpreg |= (uint32_t)SDIO_CmdInitStruct->SDIO_CmdIndex | SDIO_CmdInitStruct->SDIO_Response
  377. | SDIO_CmdInitStruct->SDIO_Wait | SDIO_CmdInitStruct->SDIO_CPSM;
  378. /* Write to SDIO CMD */
  379. SDIO->CMD = tmpreg;
  380. }
  381. /**
  382. * @brief Fills each SDIO_CmdInitStruct member with its default value.
  383. * @param SDIO_CmdInitStruct: pointer to an SDIO_CmdInitTypeDef
  384. * structure which will be initialized.
  385. * @retval None
  386. */
  387. void SDIO_CmdStructInit(SDIO_CmdInitTypeDef* SDIO_CmdInitStruct)
  388. {
  389. /* SDIO_CmdInitStruct members default value */
  390. SDIO_CmdInitStruct->SDIO_Argument = 0x00;
  391. SDIO_CmdInitStruct->SDIO_CmdIndex = 0x00;
  392. SDIO_CmdInitStruct->SDIO_Response = SDIO_Response_No;
  393. SDIO_CmdInitStruct->SDIO_Wait = SDIO_Wait_No;
  394. SDIO_CmdInitStruct->SDIO_CPSM = SDIO_CPSM_Disable;
  395. }
  396. /**
  397. * @brief Returns command index of last command for which response received.
  398. * @param None
  399. * @retval Returns the command index of the last command response received.
  400. */
  401. uint8_t SDIO_GetCommandResponse(void)
  402. {
  403. return (uint8_t)(SDIO->RESPCMD);
  404. }
  405. /**
  406. * @brief Returns response received from the card for the last command.
  407. * @param SDIO_RESP: Specifies the SDIO response register.
  408. * This parameter can be one of the following values:
  409. * @arg SDIO_RESP1: Response Register 1
  410. * @arg SDIO_RESP2: Response Register 2
  411. * @arg SDIO_RESP3: Response Register 3
  412. * @arg SDIO_RESP4: Response Register 4
  413. * @retval The Corresponding response register value.
  414. */
  415. uint32_t SDIO_GetResponse(uint32_t SDIO_RESP)
  416. {
  417. __IO uint32_t tmp = 0;
  418. /* Check the parameters */
  419. assert_param(IS_SDIO_RESP(SDIO_RESP));
  420. tmp = SDIO_RESP_ADDR + SDIO_RESP;
  421. return (*(__IO uint32_t *) tmp);
  422. }
  423. /**
  424. * @}
  425. */
  426. /** @defgroup SDIO_Group3 Data path state machine (DPSM) management functions
  427. * @brief Data path state machine (DPSM) management functions
  428. *
  429. @verbatim
  430. ===============================================================================
  431. Data path state machine (DPSM) management functions
  432. ===============================================================================
  433. This section provide functions allowing to program and read the Data path
  434. state machine (DPSM).
  435. @endverbatim
  436. * @{
  437. */
  438. /**
  439. * @brief Initializes the SDIO data path according to the specified
  440. * parameters in the SDIO_DataInitStruct.
  441. * @param SDIO_DataInitStruct : pointer to a SDIO_DataInitTypeDef structure
  442. * that contains the configuration information for the SDIO command.
  443. * @retval None
  444. */
  445. void SDIO_DataConfig(SDIO_DataInitTypeDef* SDIO_DataInitStruct)
  446. {
  447. uint32_t tmpreg = 0;
  448. /* Check the parameters */
  449. assert_param(IS_SDIO_DATA_LENGTH(SDIO_DataInitStruct->SDIO_DataLength));
  450. assert_param(IS_SDIO_BLOCK_SIZE(SDIO_DataInitStruct->SDIO_DataBlockSize));
  451. assert_param(IS_SDIO_TRANSFER_DIR(SDIO_DataInitStruct->SDIO_TransferDir));
  452. assert_param(IS_SDIO_TRANSFER_MODE(SDIO_DataInitStruct->SDIO_TransferMode));
  453. assert_param(IS_SDIO_DPSM(SDIO_DataInitStruct->SDIO_DPSM));
  454. /*---------------------------- SDIO DTIMER Configuration ---------------------*/
  455. /* Set the SDIO Data TimeOut value */
  456. SDIO->DTIMER = SDIO_DataInitStruct->SDIO_DataTimeOut;
  457. /*---------------------------- SDIO DLEN Configuration -----------------------*/
  458. /* Set the SDIO DataLength value */
  459. SDIO->DLEN = SDIO_DataInitStruct->SDIO_DataLength;
  460. /*---------------------------- SDIO DCTRL Configuration ----------------------*/
  461. /* Get the SDIO DCTRL value */
  462. tmpreg = SDIO->DCTRL;
  463. /* Clear DEN, DTMODE, DTDIR and DBCKSIZE bits */
  464. tmpreg &= DCTRL_CLEAR_MASK;
  465. /* Set DEN bit according to SDIO_DPSM value */
  466. /* Set DTMODE bit according to SDIO_TransferMode value */
  467. /* Set DTDIR bit according to SDIO_TransferDir value */
  468. /* Set DBCKSIZE bits according to SDIO_DataBlockSize value */
  469. tmpreg |= (uint32_t)SDIO_DataInitStruct->SDIO_DataBlockSize | SDIO_DataInitStruct->SDIO_TransferDir
  470. | SDIO_DataInitStruct->SDIO_TransferMode | SDIO_DataInitStruct->SDIO_DPSM;
  471. /* Write to SDIO DCTRL */
  472. SDIO->DCTRL = tmpreg;
  473. }
  474. /**
  475. * @brief Fills each SDIO_DataInitStruct member with its default value.
  476. * @param SDIO_DataInitStruct: pointer to an SDIO_DataInitTypeDef structure
  477. * which will be initialized.
  478. * @retval None
  479. */
  480. void SDIO_DataStructInit(SDIO_DataInitTypeDef* SDIO_DataInitStruct)
  481. {
  482. /* SDIO_DataInitStruct members default value */
  483. SDIO_DataInitStruct->SDIO_DataTimeOut = 0xFFFFFFFF;
  484. SDIO_DataInitStruct->SDIO_DataLength = 0x00;
  485. SDIO_DataInitStruct->SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
  486. SDIO_DataInitStruct->SDIO_TransferDir = SDIO_TransferDir_ToCard;
  487. SDIO_DataInitStruct->SDIO_TransferMode = SDIO_TransferMode_Block;
  488. SDIO_DataInitStruct->SDIO_DPSM = SDIO_DPSM_Disable;
  489. }
  490. /**
  491. * @brief Returns number of remaining data bytes to be transferred.
  492. * @param None
  493. * @retval Number of remaining data bytes to be transferred
  494. */
  495. uint32_t SDIO_GetDataCounter(void)
  496. {
  497. return SDIO->DCOUNT;
  498. }
  499. /**
  500. * @brief Read one data word from Rx FIFO.
  501. * @param None
  502. * @retval Data received
  503. */
  504. uint32_t SDIO_ReadData(void)
  505. {
  506. return SDIO->FIFO;
  507. }
  508. /**
  509. * @brief Write one data word to Tx FIFO.
  510. * @param Data: 32-bit data word to write.
  511. * @retval None
  512. */
  513. void SDIO_WriteData(uint32_t Data)
  514. {
  515. SDIO->FIFO = Data;
  516. }
  517. /**
  518. * @brief Returns the number of words left to be written to or read from FIFO.
  519. * @param None
  520. * @retval Remaining number of words.
  521. */
  522. uint32_t SDIO_GetFIFOCount(void)
  523. {
  524. return SDIO->FIFOCNT;
  525. }
  526. /**
  527. * @}
  528. */
  529. /** @defgroup SDIO_Group4 SDIO IO Cards mode management functions
  530. * @brief SDIO IO Cards mode management functions
  531. *
  532. @verbatim
  533. ===============================================================================
  534. SDIO IO Cards mode management functions
  535. ===============================================================================
  536. This section provide functions allowing to program and read the SDIO IO Cards.
  537. @endverbatim
  538. * @{
  539. */
  540. /**
  541. * @brief Starts the SD I/O Read Wait operation.
  542. * @param NewState: new state of the Start SDIO Read Wait operation.
  543. * This parameter can be: ENABLE or DISABLE.
  544. * @retval None
  545. */
  546. void SDIO_StartSDIOReadWait(FunctionalState NewState)
  547. {
  548. /* Check the parameters */
  549. assert_param(IS_FUNCTIONAL_STATE(NewState));
  550. *(__IO uint32_t *) DCTRL_RWSTART_BB = (uint32_t) NewState;
  551. }
  552. /**
  553. * @brief Stops the SD I/O Read Wait operation.
  554. * @param NewState: new state of the Stop SDIO Read Wait operation.
  555. * This parameter can be: ENABLE or DISABLE.
  556. * @retval None
  557. */
  558. void SDIO_StopSDIOReadWait(FunctionalState NewState)
  559. {
  560. /* Check the parameters */
  561. assert_param(IS_FUNCTIONAL_STATE(NewState));
  562. *(__IO uint32_t *) DCTRL_RWSTOP_BB = (uint32_t) NewState;
  563. }
  564. /**
  565. * @brief Sets one of the two options of inserting read wait interval.
  566. * @param SDIO_ReadWaitMode: SD I/O Read Wait operation mode.
  567. * This parameter can be:
  568. * @arg SDIO_ReadWaitMode_CLK: Read Wait control by stopping SDIOCLK
  569. * @arg SDIO_ReadWaitMode_DATA2: Read Wait control using SDIO_DATA2
  570. * @retval None
  571. */
  572. void SDIO_SetSDIOReadWaitMode(uint32_t SDIO_ReadWaitMode)
  573. {
  574. /* Check the parameters */
  575. assert_param(IS_SDIO_READWAIT_MODE(SDIO_ReadWaitMode));
  576. *(__IO uint32_t *) DCTRL_RWMOD_BB = SDIO_ReadWaitMode;
  577. }
  578. /**
  579. * @brief Enables or disables the SD I/O Mode Operation.
  580. * @param NewState: new state of SDIO specific operation.
  581. * This parameter can be: ENABLE or DISABLE.
  582. * @retval None
  583. */
  584. void SDIO_SetSDIOOperation(FunctionalState NewState)
  585. {
  586. /* Check the parameters */
  587. assert_param(IS_FUNCTIONAL_STATE(NewState));
  588. *(__IO uint32_t *) DCTRL_SDIOEN_BB = (uint32_t)NewState;
  589. }
  590. /**
  591. * @brief Enables or disables the SD I/O Mode suspend command sending.
  592. * @param NewState: new state of the SD I/O Mode suspend command.
  593. * This parameter can be: ENABLE or DISABLE.
  594. * @retval None
  595. */
  596. void SDIO_SendSDIOSuspendCmd(FunctionalState NewState)
  597. {
  598. /* Check the parameters */
  599. assert_param(IS_FUNCTIONAL_STATE(NewState));
  600. *(__IO uint32_t *) CMD_SDIOSUSPEND_BB = (uint32_t)NewState;
  601. }
  602. /**
  603. * @}
  604. */
  605. /** @defgroup SDIO_Group5 CE-ATA mode management functions
  606. * @brief CE-ATA mode management functions
  607. *
  608. @verbatim
  609. ===============================================================================
  610. CE-ATA mode management functions
  611. ===============================================================================
  612. This section provide functions allowing to program and read the CE-ATA card.
  613. @endverbatim
  614. * @{
  615. */
  616. /**
  617. * @brief Enables or disables the command completion signal.
  618. * @param NewState: new state of command completion signal.
  619. * This parameter can be: ENABLE or DISABLE.
  620. * @retval None
  621. */
  622. void SDIO_CommandCompletionCmd(FunctionalState NewState)
  623. {
  624. /* Check the parameters */
  625. assert_param(IS_FUNCTIONAL_STATE(NewState));
  626. *(__IO uint32_t *) CMD_ENCMDCOMPL_BB = (uint32_t)NewState;
  627. }
  628. /**
  629. * @brief Enables or disables the CE-ATA interrupt.
  630. * @param NewState: new state of CE-ATA interrupt.
  631. * This parameter can be: ENABLE or DISABLE.
  632. * @retval None
  633. */
  634. void SDIO_CEATAITCmd(FunctionalState NewState)
  635. {
  636. /* Check the parameters */
  637. assert_param(IS_FUNCTIONAL_STATE(NewState));
  638. *(__IO uint32_t *) CMD_NIEN_BB = (uint32_t)((~((uint32_t)NewState)) & ((uint32_t)0x1));
  639. }
  640. /**
  641. * @brief Sends CE-ATA command (CMD61).
  642. * @param NewState: new state of CE-ATA command.
  643. * This parameter can be: ENABLE or DISABLE.
  644. * @retval None
  645. */
  646. void SDIO_SendCEATACmd(FunctionalState NewState)
  647. {
  648. /* Check the parameters */
  649. assert_param(IS_FUNCTIONAL_STATE(NewState));
  650. *(__IO uint32_t *) CMD_ATACMD_BB = (uint32_t)NewState;
  651. }
  652. /**
  653. * @}
  654. */
  655. /** @defgroup SDIO_Group6 DMA transfers management functions
  656. * @brief DMA transfers management functions
  657. *
  658. @verbatim
  659. ===============================================================================
  660. DMA transfers management functions
  661. ===============================================================================
  662. This section provide functions allowing to program SDIO DMA transfer.
  663. @endverbatim
  664. * @{
  665. */
  666. /**
  667. * @brief Enables or disables the SDIO DMA request.
  668. * @param NewState: new state of the selected SDIO DMA request.
  669. * This parameter can be: ENABLE or DISABLE.
  670. * @retval None
  671. */
  672. void SDIO_DMACmd(FunctionalState NewState)
  673. {
  674. /* Check the parameters */
  675. assert_param(IS_FUNCTIONAL_STATE(NewState));
  676. *(__IO uint32_t *) DCTRL_DMAEN_BB = (uint32_t)NewState;
  677. }
  678. /**
  679. * @}
  680. */
  681. /** @defgroup SDIO_Group7 Interrupts and flags management functions
  682. * @brief Interrupts and flags management functions
  683. *
  684. @verbatim
  685. ===============================================================================
  686. Interrupts and flags management functions
  687. ===============================================================================
  688. @endverbatim
  689. * @{
  690. */
  691. /**
  692. * @brief Enables or disables the SDIO interrupts.
  693. * @param SDIO_IT: specifies the SDIO interrupt sources to be enabled or disabled.
  694. * This parameter can be one or a combination of the following values:
  695. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  696. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  697. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  698. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  699. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  700. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  701. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  702. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  703. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIDCOUNT, is zero) interrupt
  704. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  705. * bus mode interrupt
  706. * @arg SDIO_IT_DBCKEND: Data block sent/received (CRC check passed) interrupt
  707. * @arg SDIO_IT_CMDACT: Command transfer in progress interrupt
  708. * @arg SDIO_IT_TXACT: Data transmit in progress interrupt
  709. * @arg SDIO_IT_RXACT: Data receive in progress interrupt
  710. * @arg SDIO_IT_TXFIFOHE: Transmit FIFO Half Empty interrupt
  711. * @arg SDIO_IT_RXFIFOHF: Receive FIFO Half Full interrupt
  712. * @arg SDIO_IT_TXFIFOF: Transmit FIFO full interrupt
  713. * @arg SDIO_IT_RXFIFOF: Receive FIFO full interrupt
  714. * @arg SDIO_IT_TXFIFOE: Transmit FIFO empty interrupt
  715. * @arg SDIO_IT_RXFIFOE: Receive FIFO empty interrupt
  716. * @arg SDIO_IT_TXDAVL: Data available in transmit FIFO interrupt
  717. * @arg SDIO_IT_RXDAVL: Data available in receive FIFO interrupt
  718. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  719. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61 interrupt
  720. * @param NewState: new state of the specified SDIO interrupts.
  721. * This parameter can be: ENABLE or DISABLE.
  722. * @retval None
  723. */
  724. void SDIO_ITConfig(uint32_t SDIO_IT, FunctionalState NewState)
  725. {
  726. /* Check the parameters */
  727. assert_param(IS_SDIO_IT(SDIO_IT));
  728. assert_param(IS_FUNCTIONAL_STATE(NewState));
  729. if (NewState != DISABLE)
  730. {
  731. /* Enable the SDIO interrupts */
  732. SDIO->MASK |= SDIO_IT;
  733. }
  734. else
  735. {
  736. /* Disable the SDIO interrupts */
  737. SDIO->MASK &= ~SDIO_IT;
  738. }
  739. }
  740. /**
  741. * @brief Checks whether the specified SDIO flag is set or not.
  742. * @param SDIO_FLAG: specifies the flag to check.
  743. * This parameter can be one of the following values:
  744. * @arg SDIO_FLAG_CCRCFAIL: Command response received (CRC check failed)
  745. * @arg SDIO_FLAG_DCRCFAIL: Data block sent/received (CRC check failed)
  746. * @arg SDIO_FLAG_CTIMEOUT: Command response timeout
  747. * @arg SDIO_FLAG_DTIMEOUT: Data timeout
  748. * @arg SDIO_FLAG_TXUNDERR: Transmit FIFO underrun error
  749. * @arg SDIO_FLAG_RXOVERR: Received FIFO overrun error
  750. * @arg SDIO_FLAG_CMDREND: Command response received (CRC check passed)
  751. * @arg SDIO_FLAG_CMDSENT: Command sent (no response required)
  752. * @arg SDIO_FLAG_DATAEND: Data end (data counter, SDIDCOUNT, is zero)
  753. * @arg SDIO_FLAG_STBITERR: Start bit not detected on all data signals in wide bus mode.
  754. * @arg SDIO_FLAG_DBCKEND: Data block sent/received (CRC check passed)
  755. * @arg SDIO_FLAG_CMDACT: Command transfer in progress
  756. * @arg SDIO_FLAG_TXACT: Data transmit in progress
  757. * @arg SDIO_FLAG_RXACT: Data receive in progress
  758. * @arg SDIO_FLAG_TXFIFOHE: Transmit FIFO Half Empty
  759. * @arg SDIO_FLAG_RXFIFOHF: Receive FIFO Half Full
  760. * @arg SDIO_FLAG_TXFIFOF: Transmit FIFO full
  761. * @arg SDIO_FLAG_RXFIFOF: Receive FIFO full
  762. * @arg SDIO_FLAG_TXFIFOE: Transmit FIFO empty
  763. * @arg SDIO_FLAG_RXFIFOE: Receive FIFO empty
  764. * @arg SDIO_FLAG_TXDAVL: Data available in transmit FIFO
  765. * @arg SDIO_FLAG_RXDAVL: Data available in receive FIFO
  766. * @arg SDIO_FLAG_SDIOIT: SD I/O interrupt received
  767. * @arg SDIO_FLAG_CEATAEND: CE-ATA command completion signal received for CMD61
  768. * @retval The new state of SDIO_FLAG (SET or RESET).
  769. */
  770. FlagStatus SDIO_GetFlagStatus(uint32_t SDIO_FLAG)
  771. {
  772. FlagStatus bitstatus = RESET;
  773. /* Check the parameters */
  774. assert_param(IS_SDIO_FLAG(SDIO_FLAG));
  775. if ((SDIO->STA & SDIO_FLAG) != (uint32_t)RESET)
  776. {
  777. bitstatus = SET;
  778. }
  779. else
  780. {
  781. bitstatus = RESET;
  782. }
  783. return bitstatus;
  784. }
  785. /**
  786. * @brief Clears the SDIO's pending flags.
  787. * @param SDIO_FLAG: specifies the flag to clear.
  788. * This parameter can be one or a combination of the following values:
  789. * @arg SDIO_FLAG_CCRCFAIL: Command response received (CRC check failed)
  790. * @arg SDIO_FLAG_DCRCFAIL: Data block sent/received (CRC check failed)
  791. * @arg SDIO_FLAG_CTIMEOUT: Command response timeout
  792. * @arg SDIO_FLAG_DTIMEOUT: Data timeout
  793. * @arg SDIO_FLAG_TXUNDERR: Transmit FIFO underrun error
  794. * @arg SDIO_FLAG_RXOVERR: Received FIFO overrun error
  795. * @arg SDIO_FLAG_CMDREND: Command response received (CRC check passed)
  796. * @arg SDIO_FLAG_CMDSENT: Command sent (no response required)
  797. * @arg SDIO_FLAG_DATAEND: Data end (data counter, SDIDCOUNT, is zero)
  798. * @arg SDIO_FLAG_STBITERR: Start bit not detected on all data signals in wide bus mode
  799. * @arg SDIO_FLAG_DBCKEND: Data block sent/received (CRC check passed)
  800. * @arg SDIO_FLAG_SDIOIT: SD I/O interrupt received
  801. * @arg SDIO_FLAG_CEATAEND: CE-ATA command completion signal received for CMD61
  802. * @retval None
  803. */
  804. void SDIO_ClearFlag(uint32_t SDIO_FLAG)
  805. {
  806. /* Check the parameters */
  807. assert_param(IS_SDIO_CLEAR_FLAG(SDIO_FLAG));
  808. SDIO->ICR = SDIO_FLAG;
  809. }
  810. /**
  811. * @brief Checks whether the specified SDIO interrupt has occurred or not.
  812. * @param SDIO_IT: specifies the SDIO interrupt source to check.
  813. * This parameter can be one of the following values:
  814. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  815. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  816. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  817. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  818. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  819. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  820. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  821. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  822. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIDCOUNT, is zero) interrupt
  823. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  824. * bus mode interrupt
  825. * @arg SDIO_IT_DBCKEND: Data block sent/received (CRC check passed) interrupt
  826. * @arg SDIO_IT_CMDACT: Command transfer in progress interrupt
  827. * @arg SDIO_IT_TXACT: Data transmit in progress interrupt
  828. * @arg SDIO_IT_RXACT: Data receive in progress interrupt
  829. * @arg SDIO_IT_TXFIFOHE: Transmit FIFO Half Empty interrupt
  830. * @arg SDIO_IT_RXFIFOHF: Receive FIFO Half Full interrupt
  831. * @arg SDIO_IT_TXFIFOF: Transmit FIFO full interrupt
  832. * @arg SDIO_IT_RXFIFOF: Receive FIFO full interrupt
  833. * @arg SDIO_IT_TXFIFOE: Transmit FIFO empty interrupt
  834. * @arg SDIO_IT_RXFIFOE: Receive FIFO empty interrupt
  835. * @arg SDIO_IT_TXDAVL: Data available in transmit FIFO interrupt
  836. * @arg SDIO_IT_RXDAVL: Data available in receive FIFO interrupt
  837. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  838. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61 interrupt
  839. * @retval The new state of SDIO_IT (SET or RESET).
  840. */
  841. ITStatus SDIO_GetITStatus(uint32_t SDIO_IT)
  842. {
  843. ITStatus bitstatus = RESET;
  844. /* Check the parameters */
  845. assert_param(IS_SDIO_GET_IT(SDIO_IT));
  846. if ((SDIO->STA & SDIO_IT) != (uint32_t)RESET)
  847. {
  848. bitstatus = SET;
  849. }
  850. else
  851. {
  852. bitstatus = RESET;
  853. }
  854. return bitstatus;
  855. }
  856. /**
  857. * @brief Clears the SDIO's interrupt pending bits.
  858. * @param SDIO_IT: specifies the interrupt pending bit to clear.
  859. * This parameter can be one or a combination of the following values:
  860. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  861. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  862. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  863. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  864. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  865. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  866. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  867. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  868. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIO_DCOUNT, is zero) interrupt
  869. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  870. * bus mode interrupt
  871. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  872. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61
  873. * @retval None
  874. */
  875. void SDIO_ClearITPendingBit(uint32_t SDIO_IT)
  876. {
  877. /* Check the parameters */
  878. assert_param(IS_SDIO_CLEAR_IT(SDIO_IT));
  879. SDIO->ICR = SDIO_IT;
  880. }
  881. /**
  882. * @}
  883. */
  884. /**
  885. * @}
  886. */
  887. /**
  888. * @}
  889. */
  890. /**
  891. * @}
  892. */
  893. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/