stm32f10x_sdio.c 28 KB

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