stm32f10x_sdio.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_sdio.c
  4. * @author MCD Application Team
  5. * @version V3.3.0
  6. * @date 04/16/2010
  7. * @brief This file provides all the SDIO firmware functions.
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f10x_sdio.h"
  22. #include "stm32f10x_rcc.h"
  23. /** @addtogroup STM32F10x_StdPeriph_Driver
  24. * @{
  25. */
  26. /** @defgroup SDIO
  27. * @brief SDIO driver modules
  28. * @{
  29. */
  30. /** @defgroup SDIO_Private_TypesDefinitions
  31. * @{
  32. */
  33. /* ------------ SDIO registers bit address in the alias region ----------- */
  34. #define SDIO_OFFSET (SDIO_BASE - PERIPH_BASE)
  35. /* --- CLKCR Register ---*/
  36. /* Alias word address of CLKEN bit */
  37. #define CLKCR_OFFSET (SDIO_OFFSET + 0x04)
  38. #define CLKEN_BitNumber 0x08
  39. #define CLKCR_CLKEN_BB (PERIPH_BB_BASE + (CLKCR_OFFSET * 32) + (CLKEN_BitNumber * 4))
  40. /* --- CMD Register ---*/
  41. /* Alias word address of SDIOSUSPEND bit */
  42. #define CMD_OFFSET (SDIO_OFFSET + 0x0C)
  43. #define SDIOSUSPEND_BitNumber 0x0B
  44. #define CMD_SDIOSUSPEND_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (SDIOSUSPEND_BitNumber * 4))
  45. /* Alias word address of ENCMDCOMPL bit */
  46. #define ENCMDCOMPL_BitNumber 0x0C
  47. #define CMD_ENCMDCOMPL_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (ENCMDCOMPL_BitNumber * 4))
  48. /* Alias word address of NIEN bit */
  49. #define NIEN_BitNumber 0x0D
  50. #define CMD_NIEN_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (NIEN_BitNumber * 4))
  51. /* Alias word address of ATACMD bit */
  52. #define ATACMD_BitNumber 0x0E
  53. #define CMD_ATACMD_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (ATACMD_BitNumber * 4))
  54. /* --- DCTRL Register ---*/
  55. /* Alias word address of DMAEN bit */
  56. #define DCTRL_OFFSET (SDIO_OFFSET + 0x2C)
  57. #define DMAEN_BitNumber 0x03
  58. #define DCTRL_DMAEN_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (DMAEN_BitNumber * 4))
  59. /* Alias word address of RWSTART bit */
  60. #define RWSTART_BitNumber 0x08
  61. #define DCTRL_RWSTART_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTART_BitNumber * 4))
  62. /* Alias word address of RWSTOP bit */
  63. #define RWSTOP_BitNumber 0x09
  64. #define DCTRL_RWSTOP_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTOP_BitNumber * 4))
  65. /* Alias word address of RWMOD bit */
  66. #define RWMOD_BitNumber 0x0A
  67. #define DCTRL_RWMOD_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWMOD_BitNumber * 4))
  68. /* Alias word address of SDIOEN bit */
  69. #define SDIOEN_BitNumber 0x0B
  70. #define DCTRL_SDIOEN_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (SDIOEN_BitNumber * 4))
  71. /* ---------------------- SDIO registers bit mask ------------------------ */
  72. /* --- CLKCR Register ---*/
  73. /* CLKCR register clear mask */
  74. #define CLKCR_CLEAR_MASK ((uint32_t)0xFFFF8100)
  75. /* --- PWRCTRL Register ---*/
  76. /* SDIO PWRCTRL Mask */
  77. #define PWR_PWRCTRL_MASK ((uint32_t)0xFFFFFFFC)
  78. /* --- DCTRL Register ---*/
  79. /* SDIO DCTRL Clear Mask */
  80. #define DCTRL_CLEAR_MASK ((uint32_t)0xFFFFFF08)
  81. /* --- CMD Register ---*/
  82. /* CMD Register clear mask */
  83. #define CMD_CLEAR_MASK ((uint32_t)0xFFFFF800)
  84. /* SDIO RESP Registers Address */
  85. #define SDIO_RESP_ADDR ((uint32_t)(SDIO_BASE + 0x14))
  86. /**
  87. * @}
  88. */
  89. /** @defgroup SDIO_Private_Defines
  90. * @{
  91. */
  92. /**
  93. * @}
  94. */
  95. /** @defgroup SDIO_Private_Macros
  96. * @{
  97. */
  98. /**
  99. * @}
  100. */
  101. /** @defgroup SDIO_Private_Variables
  102. * @{
  103. */
  104. /**
  105. * @}
  106. */
  107. /** @defgroup SDIO_Private_FunctionPrototypes
  108. * @{
  109. */
  110. /**
  111. * @}
  112. */
  113. /** @defgroup SDIO_Private_Functions
  114. * @{
  115. */
  116. /**
  117. * @brief Deinitializes the SDIO peripheral registers to their default reset values.
  118. * @param None
  119. * @retval None
  120. */
  121. void SDIO_DeInit(void)
  122. {
  123. SDIO->POWER = 0x00000000;
  124. SDIO->CLKCR = 0x00000000;
  125. SDIO->ARG = 0x00000000;
  126. SDIO->CMD = 0x00000000;
  127. SDIO->DTIMER = 0x00000000;
  128. SDIO->DLEN = 0x00000000;
  129. SDIO->DCTRL = 0x00000000;
  130. SDIO->ICR = 0x00C007FF;
  131. SDIO->MASK = 0x00000000;
  132. }
  133. /**
  134. * @brief Initializes the SDIO peripheral according to the specified
  135. * parameters in the SDIO_InitStruct.
  136. * @param SDIO_InitStruct : pointer to a SDIO_InitTypeDef structure
  137. * that contains the configuration information for the SDIO peripheral.
  138. * @retval None
  139. */
  140. void SDIO_Init(SDIO_InitTypeDef* SDIO_InitStruct)
  141. {
  142. uint32_t tmpreg = 0;
  143. /* Check the parameters */
  144. assert_param(IS_SDIO_CLOCK_EDGE(SDIO_InitStruct->SDIO_ClockEdge));
  145. assert_param(IS_SDIO_CLOCK_BYPASS(SDIO_InitStruct->SDIO_ClockBypass));
  146. assert_param(IS_SDIO_CLOCK_POWER_SAVE(SDIO_InitStruct->SDIO_ClockPowerSave));
  147. assert_param(IS_SDIO_BUS_WIDE(SDIO_InitStruct->SDIO_BusWide));
  148. assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(SDIO_InitStruct->SDIO_HardwareFlowControl));
  149. /*---------------------------- SDIO CLKCR Configuration ------------------------*/
  150. /* Get the SDIO CLKCR value */
  151. tmpreg = SDIO->CLKCR;
  152. /* Clear CLKDIV, PWRSAV, BYPASS, WIDBUS, NEGEDGE, HWFC_EN bits */
  153. tmpreg &= CLKCR_CLEAR_MASK;
  154. /* Set CLKDIV bits according to SDIO_ClockDiv value */
  155. /* Set PWRSAV bit according to SDIO_ClockPowerSave value */
  156. /* Set BYPASS bit according to SDIO_ClockBypass value */
  157. /* Set WIDBUS bits according to SDIO_BusWide value */
  158. /* Set NEGEDGE bits according to SDIO_ClockEdge value */
  159. /* Set HWFC_EN bits according to SDIO_HardwareFlowControl value */
  160. tmpreg |= (SDIO_InitStruct->SDIO_ClockDiv | SDIO_InitStruct->SDIO_ClockPowerSave |
  161. SDIO_InitStruct->SDIO_ClockBypass | SDIO_InitStruct->SDIO_BusWide |
  162. SDIO_InitStruct->SDIO_ClockEdge | SDIO_InitStruct->SDIO_HardwareFlowControl);
  163. /* Write to SDIO CLKCR */
  164. SDIO->CLKCR = tmpreg;
  165. }
  166. /**
  167. * @brief Fills each SDIO_InitStruct member with its default value.
  168. * @param SDIO_InitStruct: pointer to an SDIO_InitTypeDef structure which
  169. * will be initialized.
  170. * @retval None
  171. */
  172. void SDIO_StructInit(SDIO_InitTypeDef* SDIO_InitStruct)
  173. {
  174. /* SDIO_InitStruct members default value */
  175. SDIO_InitStruct->SDIO_ClockDiv = 0x00;
  176. SDIO_InitStruct->SDIO_ClockEdge = SDIO_ClockEdge_Rising;
  177. SDIO_InitStruct->SDIO_ClockBypass = SDIO_ClockBypass_Disable;
  178. SDIO_InitStruct->SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
  179. SDIO_InitStruct->SDIO_BusWide = SDIO_BusWide_1b;
  180. SDIO_InitStruct->SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
  181. }
  182. /**
  183. * @brief Enables or disables the SDIO Clock.
  184. * @param NewState: new state of the SDIO Clock. This parameter can be: ENABLE or DISABLE.
  185. * @retval None
  186. */
  187. void SDIO_ClockCmd(FunctionalState NewState)
  188. {
  189. /* Check the parameters */
  190. assert_param(IS_FUNCTIONAL_STATE(NewState));
  191. *(__IO uint32_t *) CLKCR_CLKEN_BB = (uint32_t)NewState;
  192. }
  193. /**
  194. * @brief Sets the power status of the controller.
  195. * @param SDIO_PowerState: new state of the Power state.
  196. * This parameter can be one of the following values:
  197. * @arg SDIO_PowerState_OFF
  198. * @arg SDIO_PowerState_ON
  199. * @retval None
  200. */
  201. void SDIO_SetPowerState(uint32_t SDIO_PowerState)
  202. {
  203. /* Check the parameters */
  204. assert_param(IS_SDIO_POWER_STATE(SDIO_PowerState));
  205. SDIO->POWER &= PWR_PWRCTRL_MASK;
  206. SDIO->POWER |= SDIO_PowerState;
  207. }
  208. /**
  209. * @brief Gets the power status of the controller.
  210. * @param None
  211. * @retval Power status of the controller. The returned value can
  212. * be one of the following:
  213. * - 0x00: Power OFF
  214. * - 0x02: Power UP
  215. * - 0x03: Power ON
  216. */
  217. uint32_t SDIO_GetPowerState(void)
  218. {
  219. return (SDIO->POWER & (~PWR_PWRCTRL_MASK));
  220. }
  221. /**
  222. * @brief Enables or disables the SDIO interrupts.
  223. * @param SDIO_IT: specifies the SDIO interrupt sources to be enabled or disabled.
  224. * This parameter can be one or a combination of the following values:
  225. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  226. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  227. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  228. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  229. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  230. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  231. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  232. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  233. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIDCOUNT, is zero) interrupt
  234. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  235. * bus mode interrupt
  236. * @arg SDIO_IT_DBCKEND: Data block sent/received (CRC check passed) interrupt
  237. * @arg SDIO_IT_CMDACT: Command transfer in progress interrupt
  238. * @arg SDIO_IT_TXACT: Data transmit in progress interrupt
  239. * @arg SDIO_IT_RXACT: Data receive in progress interrupt
  240. * @arg SDIO_IT_TXFIFOHE: Transmit FIFO Half Empty interrupt
  241. * @arg SDIO_IT_RXFIFOHF: Receive FIFO Half Full interrupt
  242. * @arg SDIO_IT_TXFIFOF: Transmit FIFO full interrupt
  243. * @arg SDIO_IT_RXFIFOF: Receive FIFO full interrupt
  244. * @arg SDIO_IT_TXFIFOE: Transmit FIFO empty interrupt
  245. * @arg SDIO_IT_RXFIFOE: Receive FIFO empty interrupt
  246. * @arg SDIO_IT_TXDAVL: Data available in transmit FIFO interrupt
  247. * @arg SDIO_IT_RXDAVL: Data available in receive FIFO interrupt
  248. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  249. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61 interrupt
  250. * @param NewState: new state of the specified SDIO interrupts.
  251. * This parameter can be: ENABLE or DISABLE.
  252. * @retval None
  253. */
  254. void SDIO_ITConfig(uint32_t SDIO_IT, FunctionalState NewState)
  255. {
  256. /* Check the parameters */
  257. assert_param(IS_SDIO_IT(SDIO_IT));
  258. assert_param(IS_FUNCTIONAL_STATE(NewState));
  259. if (NewState != DISABLE)
  260. {
  261. /* Enable the SDIO interrupts */
  262. SDIO->MASK |= SDIO_IT;
  263. }
  264. else
  265. {
  266. /* Disable the SDIO interrupts */
  267. SDIO->MASK &= ~SDIO_IT;
  268. }
  269. }
  270. /**
  271. * @brief Enables or disables the SDIO DMA request.
  272. * @param NewState: new state of the selected SDIO DMA request.
  273. * This parameter can be: ENABLE or DISABLE.
  274. * @retval None
  275. */
  276. void SDIO_DMACmd(FunctionalState NewState)
  277. {
  278. /* Check the parameters */
  279. assert_param(IS_FUNCTIONAL_STATE(NewState));
  280. *(__IO uint32_t *) DCTRL_DMAEN_BB = (uint32_t)NewState;
  281. }
  282. /**
  283. * @brief Initializes the SDIO Command according to the specified
  284. * parameters in the SDIO_CmdInitStruct and send the command.
  285. * @param SDIO_CmdInitStruct : pointer to a SDIO_CmdInitTypeDef
  286. * structure that contains the configuration information for the SDIO command.
  287. * @retval None
  288. */
  289. void SDIO_SendCommand(SDIO_CmdInitTypeDef *SDIO_CmdInitStruct)
  290. {
  291. uint32_t tmpreg = 0;
  292. /* Check the parameters */
  293. assert_param(IS_SDIO_CMD_INDEX(SDIO_CmdInitStruct->SDIO_CmdIndex));
  294. assert_param(IS_SDIO_RESPONSE(SDIO_CmdInitStruct->SDIO_Response));
  295. assert_param(IS_SDIO_WAIT(SDIO_CmdInitStruct->SDIO_Wait));
  296. assert_param(IS_SDIO_CPSM(SDIO_CmdInitStruct->SDIO_CPSM));
  297. /*---------------------------- SDIO ARG Configuration ------------------------*/
  298. /* Set the SDIO Argument value */
  299. SDIO->ARG = SDIO_CmdInitStruct->SDIO_Argument;
  300. /*---------------------------- SDIO CMD Configuration ------------------------*/
  301. /* Get the SDIO CMD value */
  302. tmpreg = SDIO->CMD;
  303. /* Clear CMDINDEX, WAITRESP, WAITINT, WAITPEND, CPSMEN bits */
  304. tmpreg &= CMD_CLEAR_MASK;
  305. /* Set CMDINDEX bits according to SDIO_CmdIndex value */
  306. /* Set WAITRESP bits according to SDIO_Response value */
  307. /* Set WAITINT and WAITPEND bits according to SDIO_Wait value */
  308. /* Set CPSMEN bits according to SDIO_CPSM value */
  309. tmpreg |= (uint32_t)SDIO_CmdInitStruct->SDIO_CmdIndex | SDIO_CmdInitStruct->SDIO_Response
  310. | SDIO_CmdInitStruct->SDIO_Wait | SDIO_CmdInitStruct->SDIO_CPSM;
  311. /* Write to SDIO CMD */
  312. SDIO->CMD = tmpreg;
  313. }
  314. /**
  315. * @brief Fills each SDIO_CmdInitStruct member with its default value.
  316. * @param SDIO_CmdInitStruct: pointer to an SDIO_CmdInitTypeDef
  317. * structure which will be initialized.
  318. * @retval None
  319. */
  320. void SDIO_CmdStructInit(SDIO_CmdInitTypeDef* SDIO_CmdInitStruct)
  321. {
  322. /* SDIO_CmdInitStruct members default value */
  323. SDIO_CmdInitStruct->SDIO_Argument = 0x00;
  324. SDIO_CmdInitStruct->SDIO_CmdIndex = 0x00;
  325. SDIO_CmdInitStruct->SDIO_Response = SDIO_Response_No;
  326. SDIO_CmdInitStruct->SDIO_Wait = SDIO_Wait_No;
  327. SDIO_CmdInitStruct->SDIO_CPSM = SDIO_CPSM_Disable;
  328. }
  329. /**
  330. * @brief Returns command index of last command for which response received.
  331. * @param None
  332. * @retval Returns the command index of the last command response received.
  333. */
  334. uint8_t SDIO_GetCommandResponse(void)
  335. {
  336. return (uint8_t)(SDIO->RESPCMD);
  337. }
  338. /**
  339. * @brief Returns response received from the card for the last command.
  340. * @param SDIO_RESP: Specifies the SDIO response register.
  341. * This parameter can be one of the following values:
  342. * @arg SDIO_RESP1: Response Register 1
  343. * @arg SDIO_RESP2: Response Register 2
  344. * @arg SDIO_RESP3: Response Register 3
  345. * @arg SDIO_RESP4: Response Register 4
  346. * @retval The Corresponding response register value.
  347. */
  348. uint32_t SDIO_GetResponse(uint32_t SDIO_RESP)
  349. {
  350. __IO uint32_t tmp = 0;
  351. /* Check the parameters */
  352. assert_param(IS_SDIO_RESP(SDIO_RESP));
  353. tmp = SDIO_RESP_ADDR + SDIO_RESP;
  354. return (*(__IO uint32_t *) tmp);
  355. }
  356. /**
  357. * @brief Initializes the SDIO data path according to the specified
  358. * parameters in the SDIO_DataInitStruct.
  359. * @param SDIO_DataInitStruct : pointer to a SDIO_DataInitTypeDef structure that
  360. * contains the configuration information for the SDIO command.
  361. * @retval None
  362. */
  363. void SDIO_DataConfig(SDIO_DataInitTypeDef* SDIO_DataInitStruct)
  364. {
  365. uint32_t tmpreg = 0;
  366. /* Check the parameters */
  367. assert_param(IS_SDIO_DATA_LENGTH(SDIO_DataInitStruct->SDIO_DataLength));
  368. assert_param(IS_SDIO_BLOCK_SIZE(SDIO_DataInitStruct->SDIO_DataBlockSize));
  369. assert_param(IS_SDIO_TRANSFER_DIR(SDIO_DataInitStruct->SDIO_TransferDir));
  370. assert_param(IS_SDIO_TRANSFER_MODE(SDIO_DataInitStruct->SDIO_TransferMode));
  371. assert_param(IS_SDIO_DPSM(SDIO_DataInitStruct->SDIO_DPSM));
  372. /*---------------------------- SDIO DTIMER Configuration ---------------------*/
  373. /* Set the SDIO Data TimeOut value */
  374. SDIO->DTIMER = SDIO_DataInitStruct->SDIO_DataTimeOut;
  375. /*---------------------------- SDIO DLEN Configuration -----------------------*/
  376. /* Set the SDIO DataLength value */
  377. SDIO->DLEN = SDIO_DataInitStruct->SDIO_DataLength;
  378. /*---------------------------- SDIO DCTRL Configuration ----------------------*/
  379. /* Get the SDIO DCTRL value */
  380. tmpreg = SDIO->DCTRL;
  381. /* Clear DEN, DTMODE, DTDIR and DBCKSIZE bits */
  382. tmpreg &= DCTRL_CLEAR_MASK;
  383. /* Set DEN bit according to SDIO_DPSM value */
  384. /* Set DTMODE bit according to SDIO_TransferMode value */
  385. /* Set DTDIR bit according to SDIO_TransferDir value */
  386. /* Set DBCKSIZE bits according to SDIO_DataBlockSize value */
  387. tmpreg |= (uint32_t)SDIO_DataInitStruct->SDIO_DataBlockSize | SDIO_DataInitStruct->SDIO_TransferDir
  388. | SDIO_DataInitStruct->SDIO_TransferMode | SDIO_DataInitStruct->SDIO_DPSM;
  389. /* Write to SDIO DCTRL */
  390. SDIO->DCTRL = tmpreg;
  391. }
  392. /**
  393. * @brief Fills each SDIO_DataInitStruct member with its default value.
  394. * @param SDIO_DataInitStruct: pointer to an SDIO_DataInitTypeDef structure which
  395. * will be initialized.
  396. * @retval None
  397. */
  398. void SDIO_DataStructInit(SDIO_DataInitTypeDef* SDIO_DataInitStruct)
  399. {
  400. /* SDIO_DataInitStruct members default value */
  401. SDIO_DataInitStruct->SDIO_DataTimeOut = 0xFFFFFFFF;
  402. SDIO_DataInitStruct->SDIO_DataLength = 0x00;
  403. SDIO_DataInitStruct->SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
  404. SDIO_DataInitStruct->SDIO_TransferDir = SDIO_TransferDir_ToCard;
  405. SDIO_DataInitStruct->SDIO_TransferMode = SDIO_TransferMode_Block;
  406. SDIO_DataInitStruct->SDIO_DPSM = SDIO_DPSM_Disable;
  407. }
  408. /**
  409. * @brief Returns number of remaining data bytes to be transferred.
  410. * @param None
  411. * @retval Number of remaining data bytes to be transferred
  412. */
  413. uint32_t SDIO_GetDataCounter(void)
  414. {
  415. return SDIO->DCOUNT;
  416. }
  417. /**
  418. * @brief Read one data word from Rx FIFO.
  419. * @param None
  420. * @retval Data received
  421. */
  422. uint32_t SDIO_ReadData(void)
  423. {
  424. return SDIO->FIFO;
  425. }
  426. /**
  427. * @brief Write one data word to Tx FIFO.
  428. * @param Data: 32-bit data word to write.
  429. * @retval None
  430. */
  431. void SDIO_WriteData(uint32_t Data)
  432. {
  433. SDIO->FIFO = Data;
  434. }
  435. /**
  436. * @brief Returns the number of words left to be written to or read from FIFO.
  437. * @param None
  438. * @retval Remaining number of words.
  439. */
  440. uint32_t SDIO_GetFIFOCount(void)
  441. {
  442. return SDIO->FIFOCNT;
  443. }
  444. /**
  445. * @brief Starts the SD I/O Read Wait operation.
  446. * @param NewState: new state of the Start SDIO Read Wait operation.
  447. * This parameter can be: ENABLE or DISABLE.
  448. * @retval None
  449. */
  450. void SDIO_StartSDIOReadWait(FunctionalState NewState)
  451. {
  452. /* Check the parameters */
  453. assert_param(IS_FUNCTIONAL_STATE(NewState));
  454. *(__IO uint32_t *) DCTRL_RWSTART_BB = (uint32_t) NewState;
  455. }
  456. /**
  457. * @brief Stops the SD I/O Read Wait operation.
  458. * @param NewState: new state of the Stop SDIO Read Wait operation.
  459. * This parameter can be: ENABLE or DISABLE.
  460. * @retval None
  461. */
  462. void SDIO_StopSDIOReadWait(FunctionalState NewState)
  463. {
  464. /* Check the parameters */
  465. assert_param(IS_FUNCTIONAL_STATE(NewState));
  466. *(__IO uint32_t *) DCTRL_RWSTOP_BB = (uint32_t) NewState;
  467. }
  468. /**
  469. * @brief Sets one of the two options of inserting read wait interval.
  470. * @param SDIO_ReadWaitMode: SD I/O Read Wait operation mode.
  471. * This parametre can be:
  472. * @arg SDIO_ReadWaitMode_CLK: Read Wait control by stopping SDIOCLK
  473. * @arg SDIO_ReadWaitMode_DATA2: Read Wait control using SDIO_DATA2
  474. * @retval None
  475. */
  476. void SDIO_SetSDIOReadWaitMode(uint32_t SDIO_ReadWaitMode)
  477. {
  478. /* Check the parameters */
  479. assert_param(IS_SDIO_READWAIT_MODE(SDIO_ReadWaitMode));
  480. *(__IO uint32_t *) DCTRL_RWMOD_BB = SDIO_ReadWaitMode;
  481. }
  482. /**
  483. * @brief Enables or disables the SD I/O Mode Operation.
  484. * @param NewState: new state of SDIO specific operation.
  485. * This parameter can be: ENABLE or DISABLE.
  486. * @retval None
  487. */
  488. void SDIO_SetSDIOOperation(FunctionalState NewState)
  489. {
  490. /* Check the parameters */
  491. assert_param(IS_FUNCTIONAL_STATE(NewState));
  492. *(__IO uint32_t *) DCTRL_SDIOEN_BB = (uint32_t)NewState;
  493. }
  494. /**
  495. * @brief Enables or disables the SD I/O Mode suspend command sending.
  496. * @param NewState: new state of the SD I/O Mode suspend command.
  497. * This parameter can be: ENABLE or DISABLE.
  498. * @retval None
  499. */
  500. void SDIO_SendSDIOSuspendCmd(FunctionalState NewState)
  501. {
  502. /* Check the parameters */
  503. assert_param(IS_FUNCTIONAL_STATE(NewState));
  504. *(__IO uint32_t *) CMD_SDIOSUSPEND_BB = (uint32_t)NewState;
  505. }
  506. /**
  507. * @brief Enables or disables the command completion signal.
  508. * @param NewState: new state of command completion signal.
  509. * This parameter can be: ENABLE or DISABLE.
  510. * @retval None
  511. */
  512. void SDIO_CommandCompletionCmd(FunctionalState NewState)
  513. {
  514. /* Check the parameters */
  515. assert_param(IS_FUNCTIONAL_STATE(NewState));
  516. *(__IO uint32_t *) CMD_ENCMDCOMPL_BB = (uint32_t)NewState;
  517. }
  518. /**
  519. * @brief Enables or disables the CE-ATA interrupt.
  520. * @param NewState: new state of CE-ATA interrupt. This parameter can be: ENABLE or DISABLE.
  521. * @retval None
  522. */
  523. void SDIO_CEATAITCmd(FunctionalState NewState)
  524. {
  525. /* Check the parameters */
  526. assert_param(IS_FUNCTIONAL_STATE(NewState));
  527. *(__IO uint32_t *) CMD_NIEN_BB = (uint32_t)((~((uint32_t)NewState)) & ((uint32_t)0x1));
  528. }
  529. /**
  530. * @brief Sends CE-ATA command (CMD61).
  531. * @param NewState: new state of CE-ATA command. This parameter can be: ENABLE or DISABLE.
  532. * @retval None
  533. */
  534. void SDIO_SendCEATACmd(FunctionalState NewState)
  535. {
  536. /* Check the parameters */
  537. assert_param(IS_FUNCTIONAL_STATE(NewState));
  538. *(__IO uint32_t *) CMD_ATACMD_BB = (uint32_t)NewState;
  539. }
  540. /**
  541. * @brief Checks whether the specified SDIO flag is set or not.
  542. * @param SDIO_FLAG: specifies the flag to check.
  543. * This parameter can be one of the following values:
  544. * @arg SDIO_FLAG_CCRCFAIL: Command response received (CRC check failed)
  545. * @arg SDIO_FLAG_DCRCFAIL: Data block sent/received (CRC check failed)
  546. * @arg SDIO_FLAG_CTIMEOUT: Command response timeout
  547. * @arg SDIO_FLAG_DTIMEOUT: Data timeout
  548. * @arg SDIO_FLAG_TXUNDERR: Transmit FIFO underrun error
  549. * @arg SDIO_FLAG_RXOVERR: Received FIFO overrun error
  550. * @arg SDIO_FLAG_CMDREND: Command response received (CRC check passed)
  551. * @arg SDIO_FLAG_CMDSENT: Command sent (no response required)
  552. * @arg SDIO_FLAG_DATAEND: Data end (data counter, SDIDCOUNT, is zero)
  553. * @arg SDIO_FLAG_STBITERR: Start bit not detected on all data signals in wide
  554. * bus mode.
  555. * @arg SDIO_FLAG_DBCKEND: Data block sent/received (CRC check passed)
  556. * @arg SDIO_FLAG_CMDACT: Command transfer in progress
  557. * @arg SDIO_FLAG_TXACT: Data transmit in progress
  558. * @arg SDIO_FLAG_RXACT: Data receive in progress
  559. * @arg SDIO_FLAG_TXFIFOHE: Transmit FIFO Half Empty
  560. * @arg SDIO_FLAG_RXFIFOHF: Receive FIFO Half Full
  561. * @arg SDIO_FLAG_TXFIFOF: Transmit FIFO full
  562. * @arg SDIO_FLAG_RXFIFOF: Receive FIFO full
  563. * @arg SDIO_FLAG_TXFIFOE: Transmit FIFO empty
  564. * @arg SDIO_FLAG_RXFIFOE: Receive FIFO empty
  565. * @arg SDIO_FLAG_TXDAVL: Data available in transmit FIFO
  566. * @arg SDIO_FLAG_RXDAVL: Data available in receive FIFO
  567. * @arg SDIO_FLAG_SDIOIT: SD I/O interrupt received
  568. * @arg SDIO_FLAG_CEATAEND: CE-ATA command completion signal received for CMD61
  569. * @retval The new state of SDIO_FLAG (SET or RESET).
  570. */
  571. FlagStatus SDIO_GetFlagStatus(uint32_t SDIO_FLAG)
  572. {
  573. FlagStatus bitstatus = RESET;
  574. /* Check the parameters */
  575. assert_param(IS_SDIO_FLAG(SDIO_FLAG));
  576. if ((SDIO->STA & SDIO_FLAG) != (uint32_t)RESET)
  577. {
  578. bitstatus = SET;
  579. }
  580. else
  581. {
  582. bitstatus = RESET;
  583. }
  584. return bitstatus;
  585. }
  586. /**
  587. * @brief Clears the SDIO's pending flags.
  588. * @param SDIO_FLAG: specifies the flag to clear.
  589. * This parameter can be one or a combination of the following values:
  590. * @arg SDIO_FLAG_CCRCFAIL: Command response received (CRC check failed)
  591. * @arg SDIO_FLAG_DCRCFAIL: Data block sent/received (CRC check failed)
  592. * @arg SDIO_FLAG_CTIMEOUT: Command response timeout
  593. * @arg SDIO_FLAG_DTIMEOUT: Data timeout
  594. * @arg SDIO_FLAG_TXUNDERR: Transmit FIFO underrun error
  595. * @arg SDIO_FLAG_RXOVERR: Received FIFO overrun error
  596. * @arg SDIO_FLAG_CMDREND: Command response received (CRC check passed)
  597. * @arg SDIO_FLAG_CMDSENT: Command sent (no response required)
  598. * @arg SDIO_FLAG_DATAEND: Data end (data counter, SDIDCOUNT, is zero)
  599. * @arg SDIO_FLAG_STBITERR: Start bit not detected on all data signals in wide
  600. * bus mode
  601. * @arg SDIO_FLAG_DBCKEND: Data block sent/received (CRC check passed)
  602. * @arg SDIO_FLAG_SDIOIT: SD I/O interrupt received
  603. * @arg SDIO_FLAG_CEATAEND: CE-ATA command completion signal received for CMD61
  604. * @retval None
  605. */
  606. void SDIO_ClearFlag(uint32_t SDIO_FLAG)
  607. {
  608. /* Check the parameters */
  609. assert_param(IS_SDIO_CLEAR_FLAG(SDIO_FLAG));
  610. SDIO->ICR = SDIO_FLAG;
  611. }
  612. /**
  613. * @brief Checks whether the specified SDIO interrupt has occurred or not.
  614. * @param SDIO_IT: specifies the SDIO interrupt source to check.
  615. * This parameter can be one of the following values:
  616. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  617. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  618. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  619. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  620. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  621. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  622. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  623. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  624. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIDCOUNT, is zero) interrupt
  625. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  626. * bus mode interrupt
  627. * @arg SDIO_IT_DBCKEND: Data block sent/received (CRC check passed) interrupt
  628. * @arg SDIO_IT_CMDACT: Command transfer in progress interrupt
  629. * @arg SDIO_IT_TXACT: Data transmit in progress interrupt
  630. * @arg SDIO_IT_RXACT: Data receive in progress interrupt
  631. * @arg SDIO_IT_TXFIFOHE: Transmit FIFO Half Empty interrupt
  632. * @arg SDIO_IT_RXFIFOHF: Receive FIFO Half Full interrupt
  633. * @arg SDIO_IT_TXFIFOF: Transmit FIFO full interrupt
  634. * @arg SDIO_IT_RXFIFOF: Receive FIFO full interrupt
  635. * @arg SDIO_IT_TXFIFOE: Transmit FIFO empty interrupt
  636. * @arg SDIO_IT_RXFIFOE: Receive FIFO empty interrupt
  637. * @arg SDIO_IT_TXDAVL: Data available in transmit FIFO interrupt
  638. * @arg SDIO_IT_RXDAVL: Data available in receive FIFO interrupt
  639. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  640. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61 interrupt
  641. * @retval The new state of SDIO_IT (SET or RESET).
  642. */
  643. ITStatus SDIO_GetITStatus(uint32_t SDIO_IT)
  644. {
  645. ITStatus bitstatus = RESET;
  646. /* Check the parameters */
  647. assert_param(IS_SDIO_GET_IT(SDIO_IT));
  648. if ((SDIO->STA & SDIO_IT) != (uint32_t)RESET)
  649. {
  650. bitstatus = SET;
  651. }
  652. else
  653. {
  654. bitstatus = RESET;
  655. }
  656. return bitstatus;
  657. }
  658. /**
  659. * @brief Clears the SDIO’s interrupt pending bits.
  660. * @param SDIO_IT: specifies the interrupt pending bit to clear.
  661. * This parameter can be one or a combination of the following values:
  662. * @arg SDIO_IT_CCRCFAIL: Command response received (CRC check failed) interrupt
  663. * @arg SDIO_IT_DCRCFAIL: Data block sent/received (CRC check failed) interrupt
  664. * @arg SDIO_IT_CTIMEOUT: Command response timeout interrupt
  665. * @arg SDIO_IT_DTIMEOUT: Data timeout interrupt
  666. * @arg SDIO_IT_TXUNDERR: Transmit FIFO underrun error interrupt
  667. * @arg SDIO_IT_RXOVERR: Received FIFO overrun error interrupt
  668. * @arg SDIO_IT_CMDREND: Command response received (CRC check passed) interrupt
  669. * @arg SDIO_IT_CMDSENT: Command sent (no response required) interrupt
  670. * @arg SDIO_IT_DATAEND: Data end (data counter, SDIDCOUNT, is zero) interrupt
  671. * @arg SDIO_IT_STBITERR: Start bit not detected on all data signals in wide
  672. * bus mode interrupt
  673. * @arg SDIO_IT_SDIOIT: SD I/O interrupt received interrupt
  674. * @arg SDIO_IT_CEATAEND: CE-ATA command completion signal received for CMD61
  675. * @retval None
  676. */
  677. void SDIO_ClearITPendingBit(uint32_t SDIO_IT)
  678. {
  679. /* Check the parameters */
  680. assert_param(IS_SDIO_CLEAR_IT(SDIO_IT));
  681. SDIO->ICR = SDIO_IT;
  682. }
  683. /**
  684. * @}
  685. */
  686. /**
  687. * @}
  688. */
  689. /**
  690. * @}
  691. */
  692. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/