apm32f4xx_sdio.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*!
  2. * @file apm32f4xx_sdio.h
  3. *
  4. * @brief This file contains all the functions prototypes for the SDIO firmware
  5. * library.
  6. *
  7. * @version V1.0.2
  8. *
  9. * @date 2022-06-23
  10. *
  11. * @attention
  12. *
  13. * Copyright (C) 2021-2022 Geehy Semiconductor
  14. *
  15. * You may not use this file except in compliance with the
  16. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  17. *
  18. * The program is only for reference, which is distributed in the hope
  19. * that it will be usefull and instructional for customers to develop
  20. * their software. Unless required by applicable law or agreed to in
  21. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  22. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  24. * and limitations under the License.
  25. */
  26. /* Define to prevent recursive inclusion */
  27. #ifndef __APM32F4XX_SDIO_H
  28. #define __APM32F4XX_SDIO_H
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* Includes */
  33. #include "apm32f4xx.h"
  34. /** @addtogroup APM32F4xx_StdPeriphDriver
  35. @{
  36. */
  37. /** @addtogroup SDIO_Driver
  38. @{
  39. */
  40. /** @defgroup SDIO_Enumerations
  41. @{
  42. */
  43. /**
  44. * @brief SDIO clock edge
  45. */
  46. typedef enum
  47. {
  48. SDIO_CLOCK_EDGE_RISING, /*!< Select rising edge of SDIOCLK to generate SDIO_CLK */
  49. SDIO_CLOCK_EDGE_FALLING /*!< Select falling edge of SDIOCLK to generate SDIO_CLK */
  50. } SDIO_CLOCK_EDGE_T;
  51. /**
  52. * @brief SDIO clock bypass
  53. */
  54. typedef enum
  55. {
  56. SDIO_CLOCK_BYPASS_DISABLE, /*!< Disable divider bypass */
  57. SDIO_CLOCK_BYPASS_ENABLE /*!< Enable divider bypass */
  58. } SDIO_CLOCK_BYPASS_T;
  59. /**
  60. * @brief SDIO clock power save
  61. */
  62. typedef enum
  63. {
  64. SDIO_CLOCK_POWER_SAVE_DISABLE, /*!< Disable power saving mode */
  65. SDIO_CLOCK_POWER_SAVE_ENABLE /*!< Enable power saving mode */
  66. } SDIO_CLOCK_POWER_SAVE_T;
  67. /**
  68. * @brief SDIO bus wide
  69. */
  70. typedef enum
  71. {
  72. SDIO_BUS_WIDE_1B, /*!< 1-bit wide bus mode */
  73. SDIO_BUS_WIDE_4B, /*!< 4-bit wide bus mode */
  74. SDIO_BUS_WIDE_8B /*!< 8-bit wide bus mode */
  75. } SDIO_BUS_WIDE_T;
  76. /**
  77. * @brief SDIO hardware flow control
  78. */
  79. typedef enum
  80. {
  81. SDIO_HARDWARE_FLOW_CONTROL_DISABLE, /*!< Disable hardware flow control */
  82. SDIO_HARDWARE_FLOW_CONTROL_ENABLE /*!< Enable hardware flow control */
  83. } SDIO_HARDWARE_FLOW_CONTROL_T;
  84. /**
  85. * @brief SDIO power state
  86. */
  87. typedef enum
  88. {
  89. SDIO_POWER_STATE_OFF = 0x00, /*!< Power off */
  90. SDIO_POWER_STATE_ON = 0x03 /*!< Power on */
  91. } SDIO_POWER_STATE_T;
  92. /**
  93. * @brief SDIO response
  94. */
  95. typedef enum
  96. {
  97. SDIO_RESPONSE_NO = (uint8_t)0x00, /*!< No response */
  98. SDIO_RESPONSE_SHORT = (uint8_t)0x01, /*!< Short response */
  99. SDIO_RESPONSE_LONG = (uint8_t)0x03 /*!< Long response */
  100. } SDIO_RESPONSE_T;
  101. /**
  102. * @brief SDIO wait interrupt state
  103. */
  104. typedef enum
  105. {
  106. SDIO_WAIT_NO = (uint8_t)0x00, /*!< No wait */
  107. SDIO_WAIT_INT = (uint8_t)0x01, /*!< Wait interrupt request */
  108. SDIO_WAIT_PEND = (uint8_t)0x02 /*!< Wait CmdPend */
  109. } SDIO_WAIT_T;
  110. /**
  111. * @brief SDIO CPSM state
  112. */
  113. typedef enum
  114. {
  115. SDIO_CPSM_DISABLE, /*!< Disable CPSM */
  116. SDIO_CPSM_ENABLE /*!< Enable CPSM */
  117. } SDIO_CPSM_T;
  118. /**
  119. * @brief SDIO response registers
  120. */
  121. typedef enum
  122. {
  123. SDIO_RES1 = ((uint8_t)0x00), /*!< Response Register 1 */
  124. SDIO_RES2 = ((uint8_t)0x04), /*!< Response Register 2 */
  125. SDIO_RES3 = ((uint8_t)0x08), /*!< Response Register 3 */
  126. SDIO_RES4 = ((uint8_t)0x0C) /*!< Response Register 4 */
  127. } SDIO_RES_T;
  128. /**
  129. * @brief SDIO data block size
  130. */
  131. typedef enum
  132. {
  133. SDIO_DATA_BLOCKSIZE_1B, /*!< block size = 1 byte */
  134. SDIO_DATA_BLOCKSIZE_2B, /*!< block size = 2 bytes */
  135. SDIO_DATA_BLOCKSIZE_4B, /*!< block size = 4 bytes */
  136. SDIO_DATA_BLOCKSIZE_8B, /*!< block size = 8 bytes */
  137. SDIO_DATA_BLOCKSIZE_16B, /*!< block size = 16 bytes */
  138. SDIO_DATA_BLOCKSIZE_32B, /*!< block size = 32 bytes */
  139. SDIO_DATA_BLOCKSIZE_64B, /*!< block size = 64 bytes */
  140. SDIO_DATA_BLOCKSIZE_128B, /*!< block size = 128 bytes */
  141. SDIO_DATA_BLOCKSIZE_256B, /*!< block size = 256 bytes */
  142. SDIO_DATA_BLOCKSIZE_512B, /*!< block size = 512 bytes */
  143. SDIO_DATA_BLOCKSIZE_1024B, /*!< block size = 1024 bytes */
  144. SDIO_DATA_BLOCKSIZE_2048B, /*!< block size = 2048 bytes */
  145. SDIO_DATA_BLOCKSIZE_4096B, /*!< block size = 4096 bytes */
  146. SDIO_DATA_BLOCKSIZE_8192B, /*!< block size = 8192 bytes */
  147. SDIO_DATA_BLOCKSIZE_16384B /*!< block size = 16384 bytes */
  148. } SDIO_DATA_BLOCKSIZE_T;
  149. /**
  150. * @brief SDIO transfer direction
  151. */
  152. typedef enum
  153. {
  154. SDIO_TRANSFER_DIR_TO_CARD, /*!< Controller to SD card */
  155. SDIO_TRANSFER_DIR_TO_SDIO /*!< SD card to controller */
  156. } SDIO_TRANSFER_DIR_T;
  157. /**
  158. * @brief SDIO transfer type
  159. */
  160. typedef enum
  161. {
  162. SDIO_TRANSFER_MODE_BLOCK, /*!< Block data mode */
  163. SDIO_TRANSFER_MODE_STREAM /*!< Stream data mode */
  164. } SDIO_TRANSFER_MODE_T;
  165. /**
  166. * @brief SDIO DPSM state
  167. */
  168. typedef enum
  169. {
  170. SDIO_DPSM_DISABLE, /*!< Disable DPSM */
  171. SDIO_DPSM_ENABLE /*!< Enable DPSM */
  172. } SDIO_DPSM_T;
  173. /**
  174. * @brief SDIO flag
  175. */
  176. typedef enum
  177. {
  178. SDIO_FLAG_COMRESP = ((uint32_t)0x00000001), /*!< Command response received (CRC check failed) flag */
  179. SDIO_FLAG_DBDR = ((uint32_t)0x00000002), /*!< Data block sent/received (CRC check failed) flag */
  180. SDIO_FLAG_CMDRESTO = ((uint32_t)0x00000004), /*!< Command response timeout flag */
  181. SDIO_FLAG_DATATO = ((uint32_t)0x00000008), /*!< Data timeout flag */
  182. SDIO_FLAG_TXUDRER = ((uint32_t)0x00000010), /*!< Transmit FIFO underrun error flag */
  183. SDIO_FLAG_RXOVRER = ((uint32_t)0x00000020), /*!< Received FIFO overrun error flag */
  184. SDIO_FLAG_CMDRES = ((uint32_t)0x00000040), /*!< Command response received (CRC check passed) flag */
  185. SDIO_FLAG_CMDSENT = ((uint32_t)0x00000080), /*!< Command sent (no response required) flag */
  186. SDIO_FLAG_DATAEND = ((uint32_t)0x00000100), /*!< Data end (data counter is zero) flag */
  187. SDIO_FLAG_SBE = ((uint32_t)0x00000200), /*!< Start bit not detected on all data signals in wide bus mode flag */
  188. SDIO_FLAG_DBCP = ((uint32_t)0x00000400), /*!< Data block sent/received (CRC check passed) flag */
  189. SDIO_FLAG_CMDACT = ((uint32_t)0x00000800), /*!< Command transfer in progress flag */
  190. SDIO_FLAG_TXACT = ((uint32_t)0x00001000), /*!< Data transmit in progress flag */
  191. SDIO_FLAG_RXACT = ((uint32_t)0x00002000), /*!< Data receive in progress flag */
  192. SDIO_FLAG_TXFHF = ((uint32_t)0x00004000), /*!< Transmit FIFO Half Empty flag */
  193. SDIO_FLAG_RXFHF = ((uint32_t)0x00008000), /*!< Receive FIFO Half Full flag */
  194. SDIO_FLAG_TXFF = ((uint32_t)0x00010000), /*!< Transmit FIFO full flag */
  195. SDIO_FLAG_RXFF = ((uint32_t)0x00020000), /*!< Receive FIFO full flag */
  196. SDIO_FLAG_TXFE = ((uint32_t)0x00040000), /*!< Transmit FIFO empty flag */
  197. SDIO_FLAG_RXFE = ((uint32_t)0x00080000), /*!< Receive FIFO empty flag */
  198. SDIO_FLAG_TXDA = ((uint32_t)0x00100000), /*!< Data available in transmit FIFO flag */
  199. SDIO_FLAG_RXDA = ((uint32_t)0x00200000), /*!< Data available in receive FIFO flag */
  200. SDIO_FLAG_SDIOINT = ((uint32_t)0x00400000), /*!< SD I/O interrupt received flag */
  201. SDIO_FLAG_ATAEND = ((uint32_t)0x00800000) /*!< CE-ATA command completion signal received for CMD61 flag */
  202. } SDIO_FLAG_T;
  203. /**
  204. * @brief SDIO interrupt sources
  205. */
  206. typedef enum
  207. {
  208. SDIO_INT_COMRESP = ((uint32_t)0x00000001), /*!< Command response received (CRC check failed) interrupt */
  209. SDIO_INT_DBDR = ((uint32_t)0x00000002), /*!< Data block sent/received (CRC check failed) interrupt */
  210. SDIO_INT_CMDRESTO = ((uint32_t)0x00000004), /*!< Command response timeout interrupt */
  211. SDIO_INT_DATATO = ((uint32_t)0x00000008), /*!< Data timeout interrupt */
  212. SDIO_INT_TXUDRER = ((uint32_t)0x00000010), /*!< Transmit FIFO underrun error interrupt */
  213. SDIO_INT_RXOVRER = ((uint32_t)0x00000020), /*!< Received FIFO overrun error interrupt */
  214. SDIO_INT_CMDRES = ((uint32_t)0x00000040), /*!< Command response received (CRC check passed) interrupt */
  215. SDIO_INT_CMDSENT = ((uint32_t)0x00000080), /*!< Command sent (no response required) interrupt */
  216. SDIO_INT_DATAEND = ((uint32_t)0x00000100), /*!< Data end (data counter, SDIDCOUNT, is zero) interrupt */
  217. SDIO_INT_SBE = ((uint32_t)0x00000200), /*!< Start bit not detected on all data signals in wide bus mode interrupt */
  218. SDIO_INT_DBCP = ((uint32_t)0x00000400), /*!< Data block sent/received (CRC check passed) interrupt */
  219. SDIO_INT_CMDACT = ((uint32_t)0x00000800), /*!< Command transfer in progress interrupt */
  220. SDIO_INT_TXACT = ((uint32_t)0x00001000), /*!< Data transmit in progress interrupt */
  221. SDIO_INT_RXACT = ((uint32_t)0x00002000), /*!< Data receive in progress interrupt */
  222. SDIO_INT_TXFHF = ((uint32_t)0x00004000), /*!< Transmit FIFO Half Empty interrupt */
  223. SDIO_INT_RXFHF = ((uint32_t)0x00008000), /*!< Receive FIFO Half Full interrupt */
  224. SDIO_INT_TXFF = ((uint32_t)0x00010000), /*!< Transmit FIFO full interrupt */
  225. SDIO_INT_RXFF = ((uint32_t)0x00020000), /*!< Receive FIFO full interrupt */
  226. SDIO_INT_TXFE = ((uint32_t)0x00040000), /*!< Transmit FIFO empty interrupt */
  227. SDIO_INT_RXFE = ((uint32_t)0x00080000), /*!< Receive FIFO empty interrupt */
  228. SDIO_INT_TXDA = ((uint32_t)0x00100000), /*!< Data available in transmit FIFO interrupt */
  229. SDIO_INT_RXDA = ((uint32_t)0x00200000), /*!< Data available in receive FIFO interrupt */
  230. SDIO_INT_SDIOINT = ((uint32_t)0x00400000), /*!< SD I/O interrupt received interrupt */
  231. SDIO_INT_ATAEND = ((uint32_t)0x00800000) /*!< CE-ATA command completion signal received for CMD61 interrupt */
  232. } SDIO_INT_T;
  233. /**
  234. * @brief SDIO read wait mode
  235. */
  236. typedef enum
  237. {
  238. SDIO_READ_WAIT_MODE_DATA2, /*!< Read Wait control using SDIO_DATA2 */
  239. SDIO_READ_WAIT_MODE_CLK /*!< Read Wait control by stopping SDIOCLK */
  240. } SDIO_READ_WAIT_MODE_T;
  241. /**@} end of group SDIO_Enumerations*/
  242. /** @addtogroup SDIO_Structure Data Structure
  243. @{
  244. */
  245. /**
  246. * @brief SDIO Config structure definition
  247. */
  248. typedef struct
  249. {
  250. SDIO_CLOCK_EDGE_T clockEdge; /*!< Specifies the clock transition on which the bit capture is made */
  251. SDIO_CLOCK_BYPASS_T clockBypass; /*!< Specifies whether the SDIO Clock divider bypass is enabled or disabled */
  252. SDIO_CLOCK_POWER_SAVE_T clockPowerSave; /*!< Specifies whether SDIO Clock output is enabled or disabled when the bus is idle */
  253. SDIO_BUS_WIDE_T busWide; /*!< Specifies the SDIO bus width */
  254. SDIO_HARDWARE_FLOW_CONTROL_T hardwareFlowControl; /*!< Specifies whether the SDIO hardware flow control is enabled or disabled */
  255. uint8_t clockDiv; /*!< This parameter can be a value between 0x00 and 0xFF */
  256. } SDIO_Config_T;
  257. /**
  258. * @brief SDIO Command Config structure definition
  259. */
  260. typedef struct
  261. {
  262. uint32_t argument; /*!< Specifies the SDIO command argument which is sentto a card as part of a command message.
  263. If a command contains an argument, it must be loaded into this register before writing
  264. the command to the command register */
  265. uint32_t cmdIndex; /*!< It must be lower than 0x40 */
  266. SDIO_RESPONSE_T response; /*!< SDIO response */
  267. SDIO_WAIT_T wait; /*!< Specifies whether SDIO wait for interrupt request */
  268. SDIO_CPSM_T CPSM; /*!< Specifies whether SDIO Command path state machine (CPSM) */
  269. } SDIO_CmdConfig_T;
  270. /**
  271. * @brief SDIO Data Config
  272. */
  273. typedef struct
  274. {
  275. uint32_t dataTimeOut; /*!< SDIO data timeout period */
  276. uint32_t dataLength; /*!< SDIO data length */
  277. SDIO_DATA_BLOCKSIZE_T dataBlockSize; /*!< SDIO data block size */
  278. SDIO_TRANSFER_DIR_T transferDir; /*!< SDIO transfer direction */
  279. SDIO_TRANSFER_MODE_T transferMode; /*!< SDIO transfer type */
  280. SDIO_DPSM_T DPSM; /*!< Specifies whether SDIO Data path state machine(DPSM) */
  281. } SDIO_DataConfig_T;
  282. /**@} end of group SDIO_Structure*/
  283. /** @defgroup SDIO_Functions
  284. @{
  285. */
  286. /* SDIO reset and configuration */
  287. void SDIO_Reset(void);
  288. void SDIO_Config(SDIO_Config_T* sdioConfig);
  289. void SDIO_ConfigStructInit(SDIO_Config_T* sdioConfig);
  290. void SDIO_EnableClock(void);
  291. void SDIO_DisableClock(void);
  292. void SDIO_ConfigPowerState(SDIO_POWER_STATE_T powerState);
  293. uint32_t SDIO_ReadPowerState(void);
  294. /* DMA */
  295. void SDIO_EnableDMA(void);
  296. void SDIO_DisableDMA(void);
  297. /* Command */
  298. void SDIO_TxCommand(SDIO_CmdConfig_T *cmdConfig);
  299. void SDIO_TxCommandStructInit(SDIO_CmdConfig_T* cmdconfig);
  300. uint8_t SDIO_ReadCommandResponse(void);
  301. uint32_t SDIO_ReadResponse(SDIO_RES_T res);
  302. /* SDIO data configuration */
  303. void SDIO_ConfigData(SDIO_DataConfig_T* dataConfig);
  304. void SDIO_ConfigDataStructInit(SDIO_DataConfig_T* dataConfig);
  305. uint32_t SDIO_ReadDataCounter(void);
  306. void SDIO_WriteData(uint32_t data);
  307. uint32_t SDIO_ReadData(void);
  308. uint32_t SDIO_ReadFIFOCount(void);
  309. /* SDIO mode */
  310. void SDIO_EnableStartReadWait(void);
  311. void SDIO_DisableStartReadWait(void);
  312. void SDIO_EnableStopReadWait(void);
  313. void SDIO_DisableStopReadWait(void);
  314. void SDIO_ConfigSDIOReadWaitMode(SDIO_READ_WAIT_MODE_T readWaitMode);
  315. void SDIO_EnableSDIO(void);
  316. void SDIO_DisableSDIO(void);
  317. void SDIO_EnableTxSDIOSuspend(void);
  318. void SDIO_DisableTxSDIOSuspend(void);
  319. void SDIO_EnableCommandCompletion(void);
  320. void SDIO_DisableCommandCompletion(void);
  321. void SDIO_EnableCEATAInterrupt(void);
  322. void SDIO_DisableCEATAInterrupt(void);
  323. void SDIO_EnableTxCEATA(void);
  324. void SDIO_DisableTxCEATA(void);
  325. /* Interrupt and flags */
  326. void SDIO_EnableInterrupt(uint32_t interrupt);
  327. void SDIO_DisableInterrupt(uint32_t interrupt);
  328. uint8_t SDIO_ReadStatusFlag(SDIO_FLAG_T flag);
  329. void SDIO_ClearStatusFlag(uint32_t flag);
  330. uint8_t SDIO_ReadIntFlag(SDIO_INT_T flag);
  331. void SDIO_ClearIntFlag(uint32_t flag);
  332. #ifdef __cplusplus
  333. }
  334. #endif
  335. #endif /* __APM32F4XX_SDIO_H */
  336. /**@} end of group SDIO_Enumerations */
  337. /**@} end of group SDIO_Driver */
  338. /**@} end of group APM32F4xx_StdPeriphDriver */