stm32f4xx_sdio.c 38 KB

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