apm32f10x_sdio.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*!
  2. * @file apm32f10x_sdio.h
  3. *
  4. * @brief This file contains all the functions prototypes for the SDIO firmware library
  5. *
  6. * @version V1.0.4
  7. *
  8. * @date 2022-12-01
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2020-2022 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be useful and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. /* Define to prevent recursive inclusion */
  26. #ifndef __APM32F10X_SDIO_H
  27. #define __APM32F10X_SDIO_H
  28. /* Includes */
  29. #include "apm32f10x.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /** @addtogroup APM32F10x_StdPeriphDriver
  34. @{
  35. */
  36. /** @addtogroup SDIO_Driver SDIO Driver
  37. @{
  38. */
  39. /** @defgroup SDIO_Enumerations Enumerations
  40. @{
  41. */
  42. /**
  43. * @brief SDIO clock edge
  44. */
  45. typedef enum
  46. {
  47. SDIO_CLOCK_EDGE_RISING = 0x00000000,
  48. SDIO_CLOCK_EDGE_FALLING = 0x00002000
  49. } SDIO_CLOCK_EDGE_T;
  50. /**
  51. * @brief SDIO clock bypass
  52. */
  53. typedef enum
  54. {
  55. SDIO_CLOCK_BYPASS_DISABLE = 0x00000000,
  56. SDIO_CLOCK_BYPASS_ENABLE = 0x00000400
  57. } SDIO_CLOCK_BYPASS_T;
  58. /**
  59. * @brief SDIO clock power save
  60. */
  61. typedef enum
  62. {
  63. SDIO_CLOCK_POWER_SAVE_DISABLE = 0x00000000,
  64. SDIO_CLOCK_POWER_SAVE_ENABLE = 0x00000200
  65. } SDIO_CLOCK_POWER_SAVE_T;
  66. /**
  67. * @brief SDIO bus wide
  68. */
  69. typedef enum
  70. {
  71. SDIO_BUS_WIDE_1B = 0x00000000,
  72. SDIO_BUS_WIDE_4B = 0x00000800,
  73. SDIO_BUS_WIDE_8B = 0x00001000
  74. } SDIO_BUS_WIDE_T;
  75. /**
  76. * @brief SDIO hardware flow control
  77. */
  78. typedef enum
  79. {
  80. SDIO_HARDWARE_FLOW_CONTROL_DISABLE = 0x00000000,
  81. SDIO_HARDWARE_FLOW_CONTROL_ENABLE = 0x00004000
  82. } SDIO_HARDWARE_FLOW_CONTROL_T;
  83. /**
  84. * @brief SDIO power state
  85. */
  86. typedef enum
  87. {
  88. SDIO_POWER_STATE_OFF = 0x00000000,
  89. SDIO_POWER_STATE_ON = 0x00000003
  90. } SDIO_POWER_STATE_T;
  91. /**
  92. * @brief SDIO interrupt sources
  93. */
  94. typedef enum
  95. {
  96. SDIO_INT_COMRESP = 0x00000001,
  97. SDIO_INT_DBDR = 0x00000002,
  98. SDIO_INT_CMDRESTO = 0x00000004,
  99. SDIO_INT_DATATO = 0x00000008,
  100. SDIO_INT_TXUDRER = 0x00000010,
  101. SDIO_INT_RXOVRER = 0x00000020,
  102. SDIO_INT_CMDRES = 0x00000040,
  103. SDIO_INT_CMDSENT = 0x00000080,
  104. SDIO_INT_DATAEND = 0x00000100,
  105. SDIO_INT_SBE = 0x00000200,
  106. SDIO_INT_DBCP = 0x00000400,
  107. SDIO_INT_CMDACT = 0x00000800,
  108. SDIO_INT_TXACT = 0x00001000,
  109. SDIO_INT_RXACT = 0x00002000,
  110. SDIO_INT_TXFHF = 0x00004000,
  111. SDIO_INT_RXFHF = 0x00008000,
  112. SDIO_INT_TXFF = 0x00010000,
  113. SDIO_INT_RXFF = 0x00020000,
  114. SDIO_INT_TXFE = 0x00040000,
  115. SDIO_INT_RXFE = 0x00080000,
  116. SDIO_INT_TXDA = 0x00100000,
  117. SDIO_INT_RXDA = 0x00200000,
  118. SDIO_INT_SDIOINT = 0x00400000,
  119. SDIO_INT_ATAEND = 0x00800000
  120. } SDIO_INT_T;
  121. /**
  122. * @brief SDIO response
  123. */
  124. typedef enum
  125. {
  126. SDIO_RESPONSE_NO = 0x00000000,
  127. SDIO_RESPONSE_SHORT = 0x00000040,
  128. SDIO_RESPONSE_LONG = 0x000000C0
  129. } SDIO_RESPONSE_T;
  130. /**
  131. * @brief SDIO wait interrupt state
  132. */
  133. typedef enum
  134. {
  135. SDIO_WAIT_NO = 0x00000000,
  136. SDIO_WAIT_INT = 0x00000100,
  137. SDIO_WAIT_PEND = 0x00000200
  138. } SDIO_WAIT_T;
  139. /**
  140. * @brief SDIO CPSM state
  141. */
  142. typedef enum
  143. {
  144. SDIO_CPSM_DISABLE = 0x00000000,
  145. SDIO_CPSM_ENABLE = 0x00000400
  146. } SDIO_CPSM_T;
  147. /**
  148. * @brief SDIO response registers
  149. */
  150. typedef enum
  151. {
  152. SDIO_RES1 = 0x00000000,
  153. SDIO_RES2 = 0x00000004,
  154. SDIO_RES3 = 0x00000008,
  155. SDIO_RES4 = 0x0000000C
  156. } SDIO_RES_T;
  157. /**
  158. * @brief SDIO data block size
  159. */
  160. typedef enum
  161. {
  162. SDIO_DATA_BLOCKSIZE_1B = 0x00000000,
  163. SDIO_DATA_BLOCKSIZE_2B = 0x00000010,
  164. SDIO_DATA_BLOCKSIZE_4B = 0x00000020,
  165. SDIO_DATA_BLOCKSIZE_8B = 0x00000030,
  166. SDIO_DATA_BLOCKSIZE_16B = 0x00000040,
  167. SDIO_DATA_BLOCKSIZE_32B = 0x00000050,
  168. SDIO_DATA_BLOCKSIZE_64B = 0x00000060,
  169. SDIO_DATA_BLOCKSIZE_128B = 0x00000070,
  170. SDIO_DATA_BLOCKSIZE_256B = 0x00000080,
  171. SDIO_DATA_BLOCKSIZE_512B = 0x00000090,
  172. SDIO_DATA_BLOCKSIZE_1024B = 0x000000A0,
  173. SDIO_DATA_BLOCKSIZE_2048B = 0x000000B0,
  174. SDIO_DATA_BLOCKSIZE_496B = 0x000000C0,
  175. SDIO_DATA_BLOCKSIZE_8192B = 0x000000D0,
  176. SDIO_DATA_BLOCKSIZE_16384B = 0x000000E0
  177. } SDIO_DATA_BLOCKSIZE_T;
  178. /**
  179. * @brief SDIO transfer direction
  180. */
  181. typedef enum
  182. {
  183. SDIO_TRANSFER_DIR_TO_CARD = 0x00000000,
  184. SDIO_TRANSFER_DIR_TO_SDIO = 0x00000002
  185. } SDIO_TRANSFER_DIR_T;
  186. /**
  187. * @brief SDIO transfer type
  188. */
  189. typedef enum
  190. {
  191. SDIO_TRANSFER_MODE_BLOCK = 0x00000000,
  192. SDIO_TRANSFER_MODE_STREAM = 0x00000004
  193. } SDIO_TRANSFER_MODE_T;
  194. /**
  195. * @brief SDIO DPSM state
  196. */
  197. typedef enum
  198. {
  199. SDIO_DPSM_DISABLE = 0x00000000,
  200. SDIO_DPSM_ENABLE = 0x00000001
  201. } SDIO_DPSM_T;
  202. /**
  203. * @brief SDIO flag
  204. */
  205. typedef enum
  206. {
  207. SDIO_FLAG_COMRESP = 0x00000001,
  208. SDIO_FLAG_DBDR = 0x00000002,
  209. SDIO_FLAG_CMDRESTO = 0x00000004,
  210. SDIO_FLAG_DATATO = 0x00000008,
  211. SDIO_FLAG_TXUDRER = 0x00000010,
  212. SDIO_FLAG_RXOVRER = 0x00000020,
  213. SDIO_FLAG_CMDRES = 0x00000040,
  214. SDIO_FLAG_CMDSENT = 0x00000080,
  215. SDIO_FLAG_DATAEND = 0x00000100,
  216. SDIO_FLAG_SBE = 0x00000200,
  217. SDIO_FLAG_DBCP = 0x00000400,
  218. SDIO_FLAG_CMDACT = 0x00000800,
  219. SDIO_FLAG_TXACT = 0x00001000,
  220. SDIO_FLAG_RXACT = 0x00002000,
  221. SDIO_FLAG_TXFHF = 0x00004000,
  222. SDIO_FLAG_RXFHF = 0x00008000,
  223. SDIO_FLAG_TXFF = 0x00010000,
  224. SDIO_FLAG_RXFF = 0x00020000,
  225. SDIO_FLAG_TXFE = 0x00040000,
  226. SDIO_FLAG_RXFE = 0x00080000,
  227. SDIO_FLAG_TXDA = 0x00100000,
  228. SDIO_FLAG_RXDA = 0x00200000,
  229. SDIO_FLAG_SDIOINT = 0x00400000,
  230. SDIO_FLAG_ATAEND = 0x00800000
  231. } SDIO_FLAG_T;
  232. /**
  233. * @brief SDIO read wait mode
  234. */
  235. typedef enum
  236. {
  237. SDIO_READ_WAIT_MODE_CLK = 0x00000001,
  238. SDIO_READ_WAIT_MODE_DATA2 = 0x00000000
  239. } SDIO_READ_WAIT_MODE_T;
  240. /**@} end of group SDIO_Enumerations */
  241. /** @defgroup SDIO_Macros Macros
  242. @{
  243. */
  244. /* ------------ SDIO registers bit address in the alias region ----------- */
  245. #define SDIO_OFFSET (SDIO_BASE - PERIPH_BASE)
  246. /* --- CLKCTRL Register --- */
  247. /* Alias word address of CLKEN bit */
  248. #define CLKCTRL_OFFSET (SDIO_OFFSET + 0x04)
  249. #define CLKEN_BitNumber 0x08
  250. #define CLKCTRL_CLKEN_BB (PERIPH_BB_BASE + (CLKCTRL_OFFSET * 32) + (CLKEN_BitNumber * 4))
  251. /* --- CMD Register --- */
  252. /* Alias word address of SDIOSC bit */
  253. #define CMD_OFFSET (SDIO_OFFSET + 0x0C)
  254. #define SDIOSC_BitNumber 0x0B
  255. #define CMD_SDIOSC_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (SDIOSC_BitNumber * 4))
  256. /* Alias word address of CMDCPEN bit */
  257. #define CMDCPEN_BitNumber 0x0C
  258. #define CMD_CMDCPEN_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (CMDCPEN_BitNumber * 4))
  259. /* Alias word address of INTEN bit */
  260. #define INTEN_BitNumber 0x0D
  261. #define CMD_INTEN_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (INTEN_BitNumber * 4))
  262. /* Alias word address of ATACMD bit */
  263. #define ATACMD_BitNumber 0x0E
  264. #define CMD_ATACMD_BB (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (ATACMD_BitNumber * 4))
  265. /* --- DCTRL Register --- */
  266. /* Alias word address of DMAEN bit */
  267. #define DCTRL_OFFSET (SDIO_OFFSET + 0x2C)
  268. #define DMAEN_BitNumber 0x03
  269. #define DCTRL_DMAEN_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (DMAEN_BitNumber * 4))
  270. /* Alias word address of RWSTR bit */
  271. #define RWSTR_BitNumber 0x08
  272. #define DCTRL_RWSTR_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTR_BitNumber * 4))
  273. /* Alias word address of RWSTOP bit */
  274. #define RWSTOP_BitNumber 0x09
  275. #define DCTRL_RWSTOP_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTOP_BitNumber * 4))
  276. /* Alias word address of RDWAIT bit */
  277. #define RDWAIT_BitNumber 0x0A
  278. #define DCTRL_RDWAIT_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RDWAIT_BitNumber * 4))
  279. /* Alias word address of SDIOF bit */
  280. #define SDIOF_BitNumber 0x0B
  281. #define DCTRL_SDIOF_BB (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (SDIOF_BitNumber * 4))
  282. /**@} end of group SDIO_Macros */
  283. /** @defgroup SDIO_Structures Structures
  284. @{
  285. */
  286. /**
  287. * @brief SDIO Configure structure definition
  288. */
  289. typedef struct
  290. {
  291. SDIO_CLOCK_EDGE_T clockEdge;
  292. SDIO_CLOCK_BYPASS_T clockBypass;
  293. SDIO_CLOCK_POWER_SAVE_T clockPowerSave;
  294. SDIO_BUS_WIDE_T busWide;
  295. SDIO_HARDWARE_FLOW_CONTROL_T hardwareFlowControl;
  296. uint8_t clockDiv;
  297. } SDIO_Config_T;
  298. /**
  299. * @brief SDIO CMD Configure structure definition
  300. */
  301. typedef struct
  302. {
  303. uint32_t argument;
  304. uint32_t cmdIndex;
  305. SDIO_RESPONSE_T response;
  306. SDIO_WAIT_T wait;
  307. SDIO_CPSM_T CPSM;
  308. } SDIO_CmdConfig_T;
  309. /**
  310. * @brief SDIO Data Configure structure definition
  311. */
  312. typedef struct
  313. {
  314. uint32_t dataTimeOut;
  315. uint32_t dataLength;
  316. SDIO_DATA_BLOCKSIZE_T dataBlockSize;
  317. SDIO_TRANSFER_DIR_T transferDir;
  318. SDIO_TRANSFER_MODE_T transferMode;
  319. SDIO_DPSM_T DPSM;
  320. } SDIO_DataConfig_T;
  321. /**@} end of group SDIO_Structures */
  322. /** @defgroup SDIO_Functions Functions
  323. @{
  324. */
  325. /* SDIO reset and configuration */
  326. void SDIO_Reset(void);
  327. void SDIO_Config(SDIO_Config_T* sdioConfig);
  328. void SDIO_ConfigStructInit(SDIO_Config_T* sdioConfig);
  329. void SDIO_EnableClock(void);
  330. void SDIO_DisableClock(void);
  331. void SDIO_ConfigPowerState(SDIO_POWER_STATE_T powerState);
  332. uint32_t SDIO_ReadPowerState(void);
  333. /* DMA */
  334. void SDIO_EnableDMA(void);
  335. void SDIO_DisableDMA(void);
  336. /* Command */
  337. void SDIO_TxCommand(SDIO_CmdConfig_T* cmdConfig);
  338. void SDIO_TxCommandStructInit(SDIO_CmdConfig_T* cmdconfig);
  339. uint8_t SDIO_ReadCommandResponse(void);
  340. uint32_t SDIO_ReadResponse(SDIO_RES_T res);
  341. /* SDIO data configuration */
  342. void SDIO_ConfigData(SDIO_DataConfig_T* dataConfig);
  343. void SDIO_ConfigDataStructInit(SDIO_DataConfig_T* dataConfig);
  344. uint32_t SDIO_ReadDataCounter(void);
  345. void SDIO_WriteData(uint32_t data);
  346. uint32_t SDIO_ReadData(void);
  347. uint32_t SDIO_ReadFIFOCount(void);
  348. /* SDIO mode */
  349. void SDIO_EnableStartReadWait(void);
  350. void SDIO_DisableStartReadWait(void);
  351. void SDIO_EnableStopReadWait(void);
  352. void SDIO_DisableStopReadWait(void);
  353. void SDIO_ConfigSDIOReadWaitMode(SDIO_READ_WAIT_MODE_T readWaitMode);
  354. void SDIO_EnableSDIO(void);
  355. void SDIO_DisableSDIO(void);
  356. void SDIO_EnableTxSDIOSuspend(void);
  357. void SDIO_DisableTxSDIOSuspend(void);
  358. void SDIO_EnableCommandCompletion(void);
  359. void SDIO_DisableCommandCompletion(void);
  360. void SDIO_EnableCEATAInterrupt(void);
  361. void SDIO_DisableCEATAInterrupt(void);
  362. void SDIO_EnableTxCEATA(void);
  363. void SDIO_DisableTxCEATA(void);
  364. /* Interrupt and flags */
  365. void SDIO_EnableInterrupt(uint32_t interrupt);
  366. void SDIO_DisableInterrupt(uint32_t interrupt);
  367. uint8_t SDIO_ReadStatusFlag(SDIO_FLAG_T flag);
  368. void SDIO_ClearStatusFlag(uint32_t flag);
  369. uint8_t SDIO_ReadIntFlag(SDIO_INT_T flag);
  370. void SDIO_ClearIntFlag(uint32_t flag);
  371. /**@} end of group SDIO_Functions */
  372. /**@} end of group SDIO_Driver */
  373. /**@} end of group APM32F10x_StdPeriphDriver */
  374. #ifdef __cplusplus
  375. }
  376. #endif
  377. #endif /*__APM32F10X_SDIO_H */