n32g45x_sdio.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_sdio.c
  29. * @author Nations
  30. * @version v1.0.1
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_sdio.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup SDIO
  40. * @brief SDIO driver modules
  41. * @{
  42. */
  43. /** @addtogroup SDIO_Private_TypesDefinitions
  44. * @{
  45. */
  46. /* ------------ SDIO registers bit address in the alias region ----------- */
  47. #define SDIO_OFFSET (SDIO_BASE - PERIPH_BASE)
  48. /* --- CLKCTRL Register ---*/
  49. /* Alias word address of CLKEN bit */
  50. #define CLKCTRL_OFFSET (SDIO_OFFSET + 0x04)
  51. #define CLKEN_BIT_NUMBER 0x08
  52. #define CLKCTRL_CLKEN_BB (PERIPH_BB_BASE + (CLKCTRL_OFFSET * 32) + (CLKEN_BIT_NUMBER * 4))
  53. /* --- CMDCTRL Register ---*/
  54. /* Alias word address of SDIOSUSPEND bit */
  55. #define CMD_OFFSET (SDIO_OFFSET + 0x0C)
  56. #define SDIO_SUSPEND_BIT_NUMBER 0x0B
  57. #define CMD_SDIO_SUSPEND_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (SDIO_SUSPEND_BIT_NUMBER * 4))
  58. /* Alias word address of ENCMDCOMPL bit */
  59. #define EN_CMD_COMPL_BIT_NUMBER 0x0C
  60. #define EN_CMD_COMPL_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (EN_CMD_COMPL_BIT_NUMBER * 4))
  61. /* Alias word address of NIEN bit */
  62. #define NIEN_BIT_NUMBER 0x0D
  63. #define CMD_NIEN_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (NIEN_BIT_NUMBER * 4))
  64. /* Alias word address of ATACMD bit */
  65. #define ATACMD_BIT_NUMBER 0x0E
  66. #define CMD_ATACMD_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (ATACMD_BIT_NUMBER * 4))
  67. /* --- DATCTRL Register ---*/
  68. /* Alias word address of DMAEN bit */
  69. #define DCTRL_OFFSET (SDIO_OFFSET + 0x2C)
  70. #define DMAEN_BIT_NUMBER 0x03
  71. #define DCTRL_DMAEN_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (DMAEN_BIT_NUMBER * 4))
  72. /* Alias word address of RWSTART bit */
  73. #define RWSTART_BIT_NUMBER 0x08
  74. #define DCTRL_RWSTART_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTART_BIT_NUMBER * 4))
  75. /* Alias word address of RWSTOP bit */
  76. #define RWSTOP_BIT_NUMBER 0x09
  77. #define DCTRL_RWSTOP_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTOP_BIT_NUMBER * 4))
  78. /* Alias word address of RWMOD bit */
  79. #define RWMOD_BIT_NUMBER 0x0A
  80. #define DCTRL_RWMOD_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWMOD_BIT_NUMBER * 4))
  81. /* Alias word address of SDIOEN bit */
  82. #define SDIOEN_BIT_NUMBER 0x0B
  83. #define DCTRL_SDIOEN_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (SDIOEN_BIT_NUMBER * 4))
  84. /* ---------------------- SDIO registers bit mask ------------------------ */
  85. /* --- CLKCTRL Register ---*/
  86. /* CLKCTRL register clear mask */
  87. #define CLKCTRL_CLR_MASK ((uint32_t)0xFFFF8100)
  88. /* --- PWRCTRL Register ---*/
  89. /* SDIO PWRCTRL Mask */
  90. #define POWER_PWRCTRL_MASK ((uint32_t)0xFFFFFFFC)
  91. /* --- DATCTRL Register ---*/
  92. /* SDIO DATCTRL Clear Mask */
  93. #define DATCTRL_CLR_MASK ((uint32_t)0xFFFFFF08)
  94. /* --- CMDCTRL Register ---*/
  95. /* CMDCTRL Register clear mask */
  96. #define CMD_CLR_MASK ((uint32_t)0xFFFFF800)
  97. /* SDIO RESP Registers Address */
  98. #define SDID_RESPONSE_ADDR ((uint32_t)(SDIO_BASE + 0x14))
  99. /**
  100. * @}
  101. */
  102. /** @addtogroup SDIO_Private_Defines
  103. * @{
  104. */
  105. /**
  106. * @}
  107. */
  108. /** @addtogroup SDIO_Private_Macros
  109. * @{
  110. */
  111. /**
  112. * @}
  113. */
  114. /** @addtogroup SDIO_Private_Variables
  115. * @{
  116. */
  117. /**
  118. * @}
  119. */
  120. /** @addtogroup SDIO_Private_FunctionPrototypes
  121. * @{
  122. */
  123. /**
  124. * @}
  125. */
  126. /** @addtogroup SDIO_Private_Functions
  127. * @{
  128. */
  129. /**
  130. * @brief Deinitializes the SDIO peripheral registers to their default reset values.
  131. */
  132. void SDIO_DeInit(void)
  133. {
  134. SDIO->PWRCTRL = 0x00000000;
  135. SDIO->CLKCTRL = 0x00000000;
  136. SDIO->CMDARG = 0x00000000;
  137. SDIO->CMDCTRL = 0x00000000;
  138. SDIO->DTIMER = 0x00000000;
  139. SDIO->DATLEN = 0x00000000;
  140. SDIO->DATCTRL = 0x00000000;
  141. SDIO->INTCLR = 0x00C007FF;
  142. SDIO->INTEN = 0x00000000;
  143. }
  144. /**
  145. * @brief Initializes the SDIO peripheral according to the specified
  146. * parameters in the SDIO_InitStruct.
  147. * @param SDIO_InitStruct pointer to a SDIO_InitType structure
  148. * that contains the configuration information for the SDIO peripheral.
  149. */
  150. void SDIO_Init(SDIO_InitType* SDIO_InitStruct)
  151. {
  152. uint32_t tmpregister = 0;
  153. /* Check the parameters */
  154. assert_param(IS_SDIO_CLK_EDGE(SDIO_InitStruct->ClkEdge));
  155. assert_param(IS_SDIO_CLK_BYPASS(SDIO_InitStruct->ClkBypass));
  156. assert_param(IS_SDIO_CLK_POWER_SAVE(SDIO_InitStruct->ClkPwrSave));
  157. assert_param(IS_SDIO_BUS_WIDTH(SDIO_InitStruct->BusWidth));
  158. assert_param(IS_SDIO_HARDWARE_CLKCTRL(SDIO_InitStruct->HardwareClkCtrl));
  159. /*---------------------------- SDIO CLKCTRL Configuration ------------------------*/
  160. /* Get the SDIO CLKCTRL value */
  161. tmpregister = SDIO->CLKCTRL;
  162. /* Clear CLKDIV, PWRSAV, BYPASS, WIDBUS, NEGEDGE, HWFC_EN bits */
  163. tmpregister &= CLKCTRL_CLR_MASK;
  164. /* Set CLKDIV bits according to ClkDiv value */
  165. /* Set PWRSAV bit according to ClkPwrSave value */
  166. /* Set BYPASS bit according to ClkBypass value */
  167. /* Set WIDBUS bits according to BusWidth value */
  168. /* Set NEGEDGE bits according to ClkEdge value */
  169. /* Set HWFC_EN bits according to HardwareClkCtrl value */
  170. tmpregister |= (SDIO_InitStruct->ClkDiv | SDIO_InitStruct->ClkPwrSave | SDIO_InitStruct->ClkBypass
  171. | SDIO_InitStruct->BusWidth | SDIO_InitStruct->ClkEdge | SDIO_InitStruct->HardwareClkCtrl);
  172. /* Write to SDIO CLKCTRL */
  173. SDIO->CLKCTRL = tmpregister;
  174. }
  175. /**
  176. * @brief Fills each SDIO_InitStruct member with its default value.
  177. * @param SDIO_InitStruct pointer to an SDIO_InitType structure which
  178. * will be initialized.
  179. */
  180. void SDIO_InitStruct(SDIO_InitType* SDIO_InitStruct)
  181. {
  182. /* SDIO_InitStruct members default value */
  183. SDIO_InitStruct->ClkDiv = 0x00;
  184. SDIO_InitStruct->ClkEdge = SDIO_CLKEDGE_RISING;
  185. SDIO_InitStruct->ClkBypass = SDIO_ClkBYPASS_DISABLE;
  186. SDIO_InitStruct->ClkPwrSave = SDIO_CLKPOWERSAVE_DISABLE;
  187. SDIO_InitStruct->BusWidth = SDIO_BUSWIDTH_1B;
  188. SDIO_InitStruct->HardwareClkCtrl = SDIO_HARDWARE_CLKCTRL_DISABLE;
  189. }
  190. /**
  191. * @brief Enables or disables the SDIO Clock.
  192. * @param Cmd new state of the SDIO Clock. This parameter can be: ENABLE or DISABLE.
  193. */
  194. void SDIO_EnableClock(FunctionalState Cmd)
  195. {
  196. /* Check the parameters */
  197. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  198. *(__IO uint32_t*)CLKCTRL_CLKEN_BB = (uint32_t)Cmd;
  199. }
  200. /**
  201. * @brief Sets the power status of the controller.
  202. * @param SDIO_PowerState new state of the Power state.
  203. * This parameter can be one of the following values:
  204. * @arg SDIO_POWER_CTRL_OFF
  205. * @arg SDIO_POWER_CTRL_ON
  206. */
  207. void SDIO_SetPower(uint32_t SDIO_PowerState)
  208. {
  209. /* Check the parameters */
  210. assert_param(IS_SDIO_POWER_CTRL(SDIO_PowerState));
  211. SDIO->PWRCTRL &= POWER_PWRCTRL_MASK;
  212. SDIO->PWRCTRL |= SDIO_PowerState;
  213. }
  214. /**
  215. * @brief Gets the power status of the controller.
  216. * @return Power status of the controller. The returned value can
  217. * be one of the following:
  218. * - 0x00: Power OFF
  219. * - 0x02: Power UP
  220. * - 0x03: Power ON
  221. */
  222. uint32_t SDIO_GetPower(void)
  223. {
  224. return (SDIO->PWRCTRL & (~POWER_PWRCTRL_MASK));
  225. }
  226. /**
  227. * @brief Enables or disables the SDIO interrupts.
  228. * @param SDIO_IT specifies the SDIO interrupt sources to be enabled or disabled.
  229. * This parameter can be one or a combination of the following values:
  230. * @arg SDIO_INT_CCRCERR Command response received (CRC check failed) interrupt
  231. * @arg SDIO_INT_DCRCERR Data block sent/received (CRC check failed) interrupt
  232. * @arg SDIO_INT_CMDTIMEOUT Command response timeout interrupt
  233. * @arg SDIO_INT_DATTIMEOUT Data timeout interrupt
  234. * @arg SDIO_INT_TXURERR Transmit DATFIFO underrun error interrupt
  235. * @arg SDIO_INT_RXORERR Received DATFIFO overrun error interrupt
  236. * @arg SDIO_INT_CMDRESPRECV Command response received (CRC check passed) interrupt
  237. * @arg SDIO_INT_CMDSEND Command sent (no response required) interrupt
  238. * @arg SDIO_INT_DATEND Data end (data counter, SDIDCOUNT, is zero) interrupt
  239. * @arg SDIO_INT_SBERR Start bit not detected on all data signals in wide
  240. * bus mode interrupt
  241. * @arg SDIO_INT_DATBLKEND Data block sent/received (CRC check passed) interrupt
  242. * @arg SDIO_INT_CMDRUN Command transfer in progress interrupt
  243. * @arg SDIO_INT_TXRUN Data transmit in progress interrupt
  244. * @arg SDIO_INT_RXRUN Data receive in progress interrupt
  245. * @arg SDIO_INT_TFIFOHE Transmit DATFIFO Half Empty interrupt
  246. * @arg SDIO_INT_RFIFOHF Receive DATFIFO Half Full interrupt
  247. * @arg SDIO_INT_TFIFOF Transmit DATFIFO full interrupt
  248. * @arg SDIO_INT_RFIFOF Receive DATFIFO full interrupt
  249. * @arg SDIO_INT_TFIFOE Transmit DATFIFO empty interrupt
  250. * @arg SDIO_INT_RFIFOE Receive DATFIFO empty interrupt
  251. * @arg SDIO_INT_TDATVALID Data available in transmit DATFIFO interrupt
  252. * @arg SDIO_INT_RDATVALID Data available in receive DATFIFO interrupt
  253. * @arg SDIO_INT_SDIOINT SD I/O interrupt received interrupt
  254. * @arg SDIO_INT_CEATAF CE-ATA command completion signal received for CMD61 interrupt
  255. * @param Cmd new state of the specified SDIO interrupts.
  256. * This parameter can be: ENABLE or DISABLE.
  257. */
  258. void SDIO_ConfigInt(uint32_t SDIO_IT, FunctionalState Cmd)
  259. {
  260. /* Check the parameters */
  261. assert_param(IS_SDIO_INT(SDIO_IT));
  262. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  263. if (Cmd != DISABLE)
  264. {
  265. /* Enable the SDIO interrupts */
  266. SDIO->INTEN |= SDIO_IT;
  267. }
  268. else
  269. {
  270. /* Disable the SDIO interrupts */
  271. SDIO->INTEN &= ~SDIO_IT;
  272. }
  273. }
  274. /**
  275. * @brief Enables or disables the SDIO DMA request.
  276. * @param Cmd new state of the selected SDIO DMA request.
  277. * This parameter can be: ENABLE or DISABLE.
  278. */
  279. void SDIO_DMACmd(FunctionalState Cmd)
  280. {
  281. /* Check the parameters */
  282. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  283. *(__IO uint32_t*)DCTRL_DMAEN_BB = (uint32_t)Cmd;
  284. }
  285. /**
  286. * @brief Initializes the SDIO Command according to the specified
  287. * parameters in the SDIO_CmdInitStruct and send the command.
  288. * @param SDIO_CmdInitStruct pointer to a SDIO_CmdInitType
  289. * structure that contains the configuration information for the SDIO command.
  290. */
  291. void SDIO_SendCmd(SDIO_CmdInitType* SDIO_CmdInitStruct)
  292. {
  293. uint32_t tmpregister = 0;
  294. /* Check the parameters */
  295. assert_param(IS_SDIO_CMD_INDEX(SDIO_CmdInitStruct->CmdIndex));
  296. assert_param(IS_SDIO_RESP(SDIO_CmdInitStruct->ResponseType));
  297. assert_param(IS_SDIO_WAIT(SDIO_CmdInitStruct->WaitType));
  298. assert_param(IS_SDIO_CPSM(SDIO_CmdInitStruct->CPSMConfig));
  299. /*---------------------------- SDIO CMDARG Configuration ------------------------*/
  300. /* Set the SDIO Argument value */
  301. SDIO->CMDARG = SDIO_CmdInitStruct->CmdArgument;
  302. /*---------------------------- SDIO CMDCTRL Configuration ------------------------*/
  303. /* Get the SDIO CMDCTRL value */
  304. tmpregister = SDIO->CMDCTRL;
  305. /* Clear CMDINDEX, WAITRESP, WAITINT, WAITPEND, CPSMEN bits */
  306. tmpregister &= CMD_CLR_MASK;
  307. /* Set CMDINDEX bits according to CmdIndex value */
  308. /* Set WAITRESP bits according to ResponseType value */
  309. /* Set WAITINT and WAITPEND bits according to WaitType value */
  310. /* Set CPSMEN bits according to CPSMConfig value */
  311. tmpregister |= (uint32_t)SDIO_CmdInitStruct->CmdIndex | SDIO_CmdInitStruct->ResponseType
  312. | SDIO_CmdInitStruct->WaitType | SDIO_CmdInitStruct->CPSMConfig;
  313. /* Write to SDIO CMDCTRL */
  314. SDIO->CMDCTRL = tmpregister;
  315. }
  316. /**
  317. * @brief Fills each SDIO_CmdInitStruct member with its default value.
  318. * @param SDIO_CmdInitStruct pointer to an SDIO_CmdInitType
  319. * structure which will be initialized.
  320. */
  321. void SDIO_InitCmdStruct(SDIO_CmdInitType* SDIO_CmdInitStruct)
  322. {
  323. /* SDIO_CmdInitStruct members default value */
  324. SDIO_CmdInitStruct->CmdArgument = 0x00;
  325. SDIO_CmdInitStruct->CmdIndex = 0x00;
  326. SDIO_CmdInitStruct->ResponseType = SDIO_RESP_NO;
  327. SDIO_CmdInitStruct->WaitType = SDIO_WAIT_NO;
  328. SDIO_CmdInitStruct->CPSMConfig = SDIO_CPSM_DISABLE;
  329. }
  330. /**
  331. * @brief Returns command index of last command for which response received.
  332. * @return Returns the command index of the last command response received.
  333. */
  334. uint8_t SDIO_GetCmdResp(void)
  335. {
  336. return (uint8_t)(SDIO->CMDRESP);
  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_RESPONSE_1 Response Register 1
  343. * @arg SDIO_RESPONSE_2 Response Register 2
  344. * @arg SDIO_RESPONSE_3 Response Register 3
  345. * @arg SDIO_RESPONSE_4 Response Register 4
  346. * @return The Corresponding response register value.
  347. */
  348. uint32_t SDIO_GetResp(uint32_t SDIO_RESP)
  349. {
  350. __IO uint32_t tmp = 0;
  351. /* Check the parameters */
  352. assert_param(IS_SDIO_RESPONSE(SDIO_RESP));
  353. tmp = SDID_RESPONSE_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_DataInitType structure that
  360. * contains the configuration information for the SDIO command.
  361. */
  362. void SDIO_ConfigData(SDIO_DataInitType* SDIO_DataInitStruct)
  363. {
  364. uint32_t tmpregister = 0;
  365. /* Check the parameters */
  366. assert_param(IS_SDIO_DAT_LEN(SDIO_DataInitStruct->DatLen));
  367. assert_param(IS_SDIO_BLK_SIZE(SDIO_DataInitStruct->DatBlkSize));
  368. assert_param(IS_SDIO_TRANSFER_DIRECTION(SDIO_DataInitStruct->TransferDirection));
  369. assert_param(IS_SDIO_TRANS_MODE(SDIO_DataInitStruct->TransferMode));
  370. assert_param(IS_SDIO_DPSM(SDIO_DataInitStruct->DPSMConfig));
  371. /*---------------------------- SDIO DATTIMEOUT Configuration ---------------------*/
  372. /* Set the SDIO Data TimeOut value */
  373. SDIO->DTIMER = SDIO_DataInitStruct->DatTimeout;
  374. /*---------------------------- SDIO DATLEN Configuration -----------------------*/
  375. /* Set the SDIO DataLength value */
  376. SDIO->DATLEN = SDIO_DataInitStruct->DatLen;
  377. /*---------------------------- SDIO DATCTRL Configuration ----------------------*/
  378. /* Get the SDIO DATCTRL value */
  379. tmpregister = SDIO->DATCTRL;
  380. /* Clear DEN, DTMODE, DTDIR and DBCKSIZE bits */
  381. tmpregister &= DATCTRL_CLR_MASK;
  382. /* Set DEN bit according to DPSMConfig value */
  383. /* Set DTMODE bit according to TransferMode value */
  384. /* Set DTDIR bit according to TransferDirection value */
  385. /* Set DBCKSIZE bits according to DatBlkSize value */
  386. tmpregister |= (uint32_t)SDIO_DataInitStruct->DatBlkSize | SDIO_DataInitStruct->TransferDirection
  387. | SDIO_DataInitStruct->TransferMode | SDIO_DataInitStruct->DPSMConfig;
  388. if(SDIO_DataInitStruct->TransferDirection)
  389. {
  390. tmpregister &= ~(1<<12);
  391. }
  392. else
  393. {
  394. tmpregister |= 1<<12;
  395. }
  396. /* Write to SDIO DATCTRL */
  397. SDIO->DATCTRL = tmpregister;
  398. }
  399. /**
  400. * @brief Fills each SDIO_DataInitStruct member with its default value.
  401. * @param SDIO_DataInitStruct pointer to an SDIO_DataInitType structure which
  402. * will be initialized.
  403. */
  404. void SDIO_InitDataStruct(SDIO_DataInitType* SDIO_DataInitStruct)
  405. {
  406. /* SDIO_DataInitStruct members default value */
  407. SDIO_DataInitStruct->DatTimeout = 0xFFFFFFFF;
  408. SDIO_DataInitStruct->DatLen = 0x00;
  409. SDIO_DataInitStruct->DatBlkSize = SDIO_DATBLK_SIZE_1B;
  410. SDIO_DataInitStruct->TransferDirection = SDIO_TRANSDIR_TOCARD;
  411. SDIO_DataInitStruct->TransferMode = SDIO_TRANSMODE_BLOCK;
  412. SDIO_DataInitStruct->DPSMConfig = SDIO_DPSM_DISABLE;
  413. }
  414. /**
  415. * @brief Returns number of remaining data bytes to be transferred.
  416. * @return Number of remaining data bytes to be transferred
  417. */
  418. uint32_t SDIO_GetDataCountValue(void)
  419. {
  420. return SDIO->DATCOUNT;
  421. }
  422. /**
  423. * @brief Read one data word from Rx DATFIFO.
  424. * @return Data received
  425. */
  426. uint32_t SDIO_ReadData(void)
  427. {
  428. return SDIO->DATFIFO;
  429. }
  430. /**
  431. * @brief Write one data word to Tx DATFIFO.
  432. * @param Data 32-bit data word to write.
  433. */
  434. void SDIO_WriteData(uint32_t Data)
  435. {
  436. SDIO->DATFIFO = Data;
  437. }
  438. /**
  439. * @brief Returns the number of words left to be written to or read from DATFIFO.
  440. * @return Remaining number of words.
  441. */
  442. uint32_t SDIO_GetFifoCounter(void)
  443. {
  444. return SDIO->FIFOCOUNT;
  445. }
  446. /**
  447. * @brief Starts the SD I/O Read Wait operation.
  448. * @param Cmd new state of the Start SDIO Read Wait operation.
  449. * This parameter can be: ENABLE or DISABLE.
  450. */
  451. void SDIO_EnableReadWait(FunctionalState Cmd)
  452. {
  453. /* Check the parameters */
  454. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  455. *(__IO uint32_t*)DCTRL_RWSTART_BB = (uint32_t)Cmd;
  456. }
  457. /**
  458. * @brief Stops the SD I/O Read Wait operation.
  459. * @param Cmd new state of the Stop SDIO Read Wait operation.
  460. * This parameter can be: ENABLE or DISABLE.
  461. */
  462. void SDIO_DisableReadWait(FunctionalState Cmd)
  463. {
  464. /* Check the parameters */
  465. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  466. *(__IO uint32_t*)DCTRL_RWSTOP_BB = (uint32_t)Cmd;
  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 parameter can be:
  472. * @arg SDIO_RDWAIT_MODE_CLK Read Wait control by stopping SDIOCLK
  473. * @arg SDIO_RDWAIT_MODE_DAT2 Read Wait control using SDIO_DATA2
  474. */
  475. void SDIO_EnableSdioReadWaitMode(uint32_t SDIO_ReadWaitMode)
  476. {
  477. /* Check the parameters */
  478. assert_param(IS_SDIO_RDWAIT_MODE(SDIO_ReadWaitMode));
  479. *(__IO uint32_t*)DCTRL_RWMOD_BB = SDIO_ReadWaitMode;
  480. }
  481. /**
  482. * @brief Enables or disables the SD I/O Mode Operation.
  483. * @param Cmd new state of SDIO specific operation.
  484. * This parameter can be: ENABLE or DISABLE.
  485. */
  486. void SDIO_EnableSdioOperation(FunctionalState Cmd)
  487. {
  488. /* Check the parameters */
  489. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  490. *(__IO uint32_t*)DCTRL_SDIOEN_BB = (uint32_t)Cmd;
  491. }
  492. /**
  493. * @brief Enables or disables the SD I/O Mode suspend command sending.
  494. * @param Cmd new state of the SD I/O Mode suspend command.
  495. * This parameter can be: ENABLE or DISABLE.
  496. */
  497. void SDIO_EnableSendSdioSuspend(FunctionalState Cmd)
  498. {
  499. /* Check the parameters */
  500. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  501. *(__IO uint32_t*)CMD_SDIO_SUSPEND_BB = (uint32_t)Cmd;
  502. }
  503. /**
  504. * @brief Enables or disables the command completion signal.
  505. * @param Cmd new state of command completion signal.
  506. * This parameter can be: ENABLE or DISABLE.
  507. */
  508. void SDIO_EnableCommandCompletion(FunctionalState Cmd)
  509. {
  510. /* Check the parameters */
  511. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  512. *(__IO uint32_t*)EN_CMD_COMPL_BB = (uint32_t)Cmd;
  513. }
  514. /**
  515. * @brief Enables or disables the CE-ATA interrupt.
  516. * @param Cmd new state of CE-ATA interrupt. This parameter can be: ENABLE or DISABLE.
  517. */
  518. void SDIO_EnableCEATAInt(FunctionalState Cmd)
  519. {
  520. /* Check the parameters */
  521. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  522. *(__IO uint32_t*)CMD_NIEN_BB = (uint32_t)((~((uint32_t)Cmd)) & ((uint32_t)0x1));
  523. }
  524. /**
  525. * @brief Sends CE-ATA command (CMD61).
  526. * @param Cmd new state of CE-ATA command. This parameter can be: ENABLE or DISABLE.
  527. */
  528. void SDIO_EnableSendCEATA(FunctionalState Cmd)
  529. {
  530. /* Check the parameters */
  531. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  532. *(__IO uint32_t*)CMD_ATACMD_BB = (uint32_t)Cmd;
  533. }
  534. /**
  535. * @brief Checks whether the specified SDIO flag is set or not.
  536. * @param SDIO_FLAG specifies the flag to check.
  537. * This parameter can be one of the following values:
  538. * @arg SDIO_FLAG_CCRCERR Command response received (CRC check failed)
  539. * @arg SDIO_FLAG_DCRCERR Data block sent/received (CRC check failed)
  540. * @arg SDIO_FLAG_CMDTIMEOUT Command response timeout
  541. * @arg SDIO_FLAG_DATTIMEOUT Data timeout
  542. * @arg SDIO_FLAG_TXURERR Transmit DATFIFO underrun error
  543. * @arg SDIO_FLAG_RXORERR Received DATFIFO overrun error
  544. * @arg SDIO_FLAG_CMDRESPRECV Command response received (CRC check passed)
  545. * @arg SDIO_FLAG_CMDSEND Command sent (no response required)
  546. * @arg SDIO_FLAG_DATEND Data end (data counter, SDIDCOUNT, is zero)
  547. * @arg SDIO_FLAG_SBERR Start bit not detected on all data signals in wide
  548. * bus mode.
  549. * @arg SDIO_FLAG_DATBLKEND Data block sent/received (CRC check passed)
  550. * @arg SDIO_FLAG_CMDRUN Command transfer in progress
  551. * @arg SDIO_FLAG_TXRUN Data transmit in progress
  552. * @arg SDIO_FLAG_RXRUN Data receive in progress
  553. * @arg SDIO_FLAG_TFIFOHE Transmit DATFIFO Half Empty
  554. * @arg SDIO_FLAG_RFIFOHF Receive DATFIFO Half Full
  555. * @arg SDIO_FLAG_TFIFOF Transmit DATFIFO full
  556. * @arg SDIO_FLAG_RFIFOF Receive DATFIFO full
  557. * @arg SDIO_FLAG_TFIFOE Transmit DATFIFO empty
  558. * @arg SDIO_FLAG_RFIFOE Receive DATFIFO empty
  559. * @arg SDIO_FLAG_TDATVALID Data available in transmit DATFIFO
  560. * @arg SDIO_FLAG_RDATVALID Data available in receive DATFIFO
  561. * @arg SDIO_FLAG_SDIOINT SD I/O interrupt received
  562. * @arg SDIO_FLAG_CEATAF CE-ATA command completion signal received for CMD61
  563. * @return The new state of SDIO_FLAG (SET or RESET).
  564. */
  565. FlagStatus SDIO_GetFlag(uint32_t SDIO_FLAG)
  566. {
  567. FlagStatus bitstatus = RESET;
  568. /* Check the parameters */
  569. assert_param(IS_SDIO_FLAG(SDIO_FLAG));
  570. if ((SDIO->STS & SDIO_FLAG) != (uint32_t)RESET)
  571. {
  572. bitstatus = SET;
  573. }
  574. else
  575. {
  576. bitstatus = RESET;
  577. }
  578. return bitstatus;
  579. }
  580. /**
  581. * @brief Clears the SDIO's pending flags.
  582. * @param SDIO_FLAG specifies the flag to clear.
  583. * This parameter can be one or a combination of the following values:
  584. * @arg SDIO_FLAG_CCRCERR Command response received (CRC check failed)
  585. * @arg SDIO_FLAG_DCRCERR Data block sent/received (CRC check failed)
  586. * @arg SDIO_FLAG_CMDTIMEOUT Command response timeout
  587. * @arg SDIO_FLAG_DATTIMEOUT Data timeout
  588. * @arg SDIO_FLAG_TXURERR Transmit DATFIFO underrun error
  589. * @arg SDIO_FLAG_RXORERR Received DATFIFO overrun error
  590. * @arg SDIO_FLAG_CMDRESPRECV Command response received (CRC check passed)
  591. * @arg SDIO_FLAG_CMDSEND Command sent (no response required)
  592. * @arg SDIO_FLAG_DATEND Data end (data counter, SDIDCOUNT, is zero)
  593. * @arg SDIO_FLAG_SBERR Start bit not detected on all data signals in wide
  594. * bus mode
  595. * @arg SDIO_FLAG_DATBLKEND Data block sent/received (CRC check passed)
  596. * @arg SDIO_FLAG_SDIOINT SD I/O interrupt received
  597. * @arg SDIO_FLAG_CEATAF CE-ATA command completion signal received for CMD61
  598. */
  599. void SDIO_ClrFlag(uint32_t SDIO_FLAG)
  600. {
  601. /* Check the parameters */
  602. assert_param(IS_SDIO_CLR_FLAG(SDIO_FLAG));
  603. SDIO->INTCLR = SDIO_FLAG;
  604. }
  605. /**
  606. * @brief Checks whether the specified SDIO interrupt has occurred or not.
  607. * @param SDIO_IT specifies the SDIO interrupt source to check.
  608. * This parameter can be one of the following values:
  609. * @arg SDIO_INT_CCRCERR Command response received (CRC check failed) interrupt
  610. * @arg SDIO_INT_DCRCERR Data block sent/received (CRC check failed) interrupt
  611. * @arg SDIO_INT_CMDTIMEOUT Command response timeout interrupt
  612. * @arg SDIO_INT_DATTIMEOUT Data timeout interrupt
  613. * @arg SDIO_INT_TXURERR Transmit DATFIFO underrun error interrupt
  614. * @arg SDIO_INT_RXORERR Received DATFIFO overrun error interrupt
  615. * @arg SDIO_INT_CMDRESPRECV Command response received (CRC check passed) interrupt
  616. * @arg SDIO_INT_CMDSEND Command sent (no response required) interrupt
  617. * @arg SDIO_INT_DATEND Data end (data counter, SDIDCOUNT, is zero) interrupt
  618. * @arg SDIO_INT_SBERR Start bit not detected on all data signals in wide
  619. * bus mode interrupt
  620. * @arg SDIO_INT_DATBLKEND Data block sent/received (CRC check passed) interrupt
  621. * @arg SDIO_INT_CMDRUN Command transfer in progress interrupt
  622. * @arg SDIO_INT_TXRUN Data transmit in progress interrupt
  623. * @arg SDIO_INT_RXRUN Data receive in progress interrupt
  624. * @arg SDIO_INT_TFIFOHE Transmit DATFIFO Half Empty interrupt
  625. * @arg SDIO_INT_RFIFOHF Receive DATFIFO Half Full interrupt
  626. * @arg SDIO_INT_TFIFOF Transmit DATFIFO full interrupt
  627. * @arg SDIO_INT_RFIFOF Receive DATFIFO full interrupt
  628. * @arg SDIO_INT_TFIFOE Transmit DATFIFO empty interrupt
  629. * @arg SDIO_INT_RFIFOE Receive DATFIFO empty interrupt
  630. * @arg SDIO_INT_TDATVALID Data available in transmit DATFIFO interrupt
  631. * @arg SDIO_INT_RDATVALID Data available in receive DATFIFO interrupt
  632. * @arg SDIO_INT_SDIOINT SD I/O interrupt received interrupt
  633. * @arg SDIO_INT_CEATAF CE-ATA command completion signal received for CMD61 interrupt
  634. * @return The new state of SDIO_IT (SET or RESET).
  635. */
  636. INTStatus SDIO_GetIntStatus(uint32_t SDIO_IT)
  637. {
  638. INTStatus bitstatus = RESET;
  639. /* Check the parameters */
  640. assert_param(IS_SDIO_GET_INT(SDIO_IT));
  641. if ((SDIO->STS & SDIO_IT) != (uint32_t)RESET)
  642. {
  643. bitstatus = SET;
  644. }
  645. else
  646. {
  647. bitstatus = RESET;
  648. }
  649. return bitstatus;
  650. }
  651. /**
  652. * @brief Clears the SDIO's interrupt pending bits.
  653. * @param SDIO_IT specifies the interrupt pending bit to clear.
  654. * This parameter can be one or a combination of the following values:
  655. * @arg SDIO_INT_CCRCERR Command response received (CRC check failed) interrupt
  656. * @arg SDIO_INT_DCRCERR Data block sent/received (CRC check failed) interrupt
  657. * @arg SDIO_INT_CMDTIMEOUT Command response timeout interrupt
  658. * @arg SDIO_INT_DATTIMEOUT Data timeout interrupt
  659. * @arg SDIO_INT_TXURERR Transmit DATFIFO underrun error interrupt
  660. * @arg SDIO_INT_RXORERR Received DATFIFO overrun error interrupt
  661. * @arg SDIO_INT_CMDRESPRECV Command response received (CRC check passed) interrupt
  662. * @arg SDIO_INT_CMDSEND Command sent (no response required) interrupt
  663. * @arg SDIO_INT_DATEND Data end (data counter, SDIDCOUNT, is zero) interrupt
  664. * @arg SDIO_INT_SBERR Start bit not detected on all data signals in wide
  665. * bus mode interrupt
  666. * @arg SDIO_INT_SDIOINT SD I/O interrupt received interrupt
  667. * @arg SDIO_INT_CEATAF CE-ATA command completion signal received for CMD61
  668. */
  669. void SDIO_ClrIntPendingBit(uint32_t SDIO_IT)
  670. {
  671. /* Check the parameters */
  672. assert_param(IS_SDIO_CLR_INT(SDIO_IT));
  673. SDIO->INTCLR = SDIO_IT;
  674. }
  675. /**
  676. * @}
  677. */
  678. /**
  679. * @}
  680. */
  681. /**
  682. * @}
  683. */