apm32f10x_sdio.h 11 KB

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