hc32_ll_gpio.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**
  2. *******************************************************************************
  3. * @file hc32_ll_gpio.h
  4. * @brief This file contains all the functions prototypes of the GPIO driver
  5. * library.
  6. @verbatim
  7. Change Logs:
  8. Date Author Notes
  9. 2022-12-31 CDT First version
  10. @endverbatim
  11. *******************************************************************************
  12. * Copyright (C) 2022, Xiaohua Semiconductor Co., Ltd. All rights reserved.
  13. *
  14. * This software component is licensed by XHSC under BSD 3-Clause license
  15. * (the "License"); You may not use this file except in compliance with the
  16. * License. You may obtain a copy of the License at:
  17. * opensource.org/licenses/BSD-3-Clause
  18. *
  19. *******************************************************************************
  20. */
  21. #ifndef __HC32_LL_GPIO_H__
  22. #define __HC32_LL_GPIO_H__
  23. /* C binding of definitions if building with C++ compiler */
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. /*******************************************************************************
  29. * Include files
  30. ******************************************************************************/
  31. #include "hc32_ll_def.h"
  32. #include "hc32f4xx.h"
  33. #include "hc32f4xx_conf.h"
  34. /**
  35. * @addtogroup LL_Driver
  36. * @{
  37. */
  38. /**
  39. * @addtogroup LL_GPIO
  40. * @{
  41. */
  42. #if (LL_GPIO_ENABLE == DDL_ON)
  43. /*******************************************************************************
  44. * Global type definitions ('typedef')
  45. ******************************************************************************/
  46. /**
  47. * @defgroup GPIO_Global_Types GPIO Global Types
  48. * @{
  49. */
  50. /**
  51. * @brief GPIO Pin Set and Reset enumeration
  52. */
  53. typedef enum {
  54. PIN_RESET = 0U, /*!< Pin reset */
  55. PIN_SET = 1U /*!< Pin set */
  56. } en_pin_state_t;
  57. /**
  58. * @brief GPIO Init structure definition
  59. */
  60. typedef struct {
  61. uint16_t u16PinState; /*!< Set pin state to High or Low, @ref GPIO_PinState_Sel for details */
  62. uint16_t u16PinDir; /*!< Pin mode setting, @ref GPIO_PinDirection_Sel for details */
  63. uint16_t u16PinOutputType; /*!< Output type setting, @ref GPIO_PinOutType_Sel for details */
  64. uint16_t u16PinDrv; /*!< Pin drive capacity setting, @ref GPIO_PinDrv_Sel for details */
  65. uint16_t u16Latch; /*!< Pin latch setting, @ref GPIO_PinLatch_Sel for details */
  66. uint16_t u16PullUp; /*!< Internal pull-up resistor setting, @ref GPIO_PinPU_Sel for details */
  67. uint16_t u16PullDown; /*!< Internal pull-down registor setting, @ref GPIO_PinPD_Sel for detail */
  68. uint16_t u16Invert; /*!< Pin input/output invert setting, @ref GPIO_PinInvert_Sel */
  69. uint16_t u16ExtInt; /*!< External interrupt pin setting, @ref GPIO_PinExtInt_Sel for details */
  70. uint16_t u16PinInputType; /*!< Input type setting, @ref GPIO_PinInType_Sel */
  71. uint16_t u16PinAttr; /*!< Digital or analog attribute setting, @ref GPIO_PinMode_Sel for details */
  72. } stc_gpio_init_t;
  73. /**
  74. * @}
  75. */
  76. /*******************************************************************************
  77. * Global pre-processor symbols/macros ('#define')
  78. ******************************************************************************/
  79. /**
  80. * @defgroup GPIO_Global_Macros GPIO Global Macros
  81. * @{
  82. */
  83. /**
  84. * @defgroup GPIO_Pins_Define GPIO Pin Source
  85. * @{
  86. */
  87. #define GPIO_PIN_00 (0x0001U) /*!< Pin 00 selected */
  88. #define GPIO_PIN_01 (0x0002U) /*!< Pin 01 selected */
  89. #define GPIO_PIN_02 (0x0004U) /*!< Pin 02 selected */
  90. #define GPIO_PIN_03 (0x0008U) /*!< Pin 03 selected */
  91. #define GPIO_PIN_04 (0x0010U) /*!< Pin 04 selected */
  92. #define GPIO_PIN_05 (0x0020U) /*!< Pin 05 selected */
  93. #define GPIO_PIN_06 (0x0040U) /*!< Pin 06 selected */
  94. #define GPIO_PIN_07 (0x0080U) /*!< Pin 07 selected */
  95. #define GPIO_PIN_08 (0x0100U) /*!< Pin 08 selected */
  96. #define GPIO_PIN_09 (0x0200U) /*!< Pin 09 selected */
  97. #define GPIO_PIN_10 (0x0400U) /*!< Pin 10 selected */
  98. #define GPIO_PIN_11 (0x0800U) /*!< Pin 11 selected */
  99. #define GPIO_PIN_12 (0x1000U) /*!< Pin 12 selected */
  100. #define GPIO_PIN_13 (0x2000U) /*!< Pin 13 selected */
  101. #define GPIO_PIN_14 (0x4000U) /*!< Pin 14 selected */
  102. #define GPIO_PIN_15 (0x8000U) /*!< Pin 15 selected */
  103. #define GPIO_PIN_ALL (0xFFFFU) /*!< All pins selected */
  104. /**
  105. * @}
  106. */
  107. /**
  108. * @defgroup GPIO_All_Pins_Define GPIO All Pin Definition for Each Product
  109. * @{
  110. */
  111. #define GPIO_PIN_A_ALL (0xFFFFU) /*!< Pin A all*/
  112. #define GPIO_PIN_B_ALL (0xFFFFU) /*!< Pin B all*/
  113. #define GPIO_PIN_C_ALL (0xFFFFU) /*!< Pin C all*/
  114. #define GPIO_PIN_D_ALL (0x0F07U) /*!< Pin D all*/
  115. #define GPIO_PIN_E_ALL (0xF01FU) /*!< Pin E all*/
  116. #define GPIO_PIN_H_ALL (0x0007U) /*!< Pin H all*/
  117. /**
  118. * @}
  119. */
  120. /**
  121. * @defgroup GPIO_Port_Source GPIO Port Source
  122. * @{
  123. */
  124. #define GPIO_PORT_A (0x00U) /*!< Port A selected */
  125. #define GPIO_PORT_B (0x01U) /*!< Port B selected */
  126. #define GPIO_PORT_C (0x02U) /*!< Port C selected */
  127. #define GPIO_PORT_D (0x03U) /*!< Port D selected */
  128. #define GPIO_PORT_E (0x04U) /*!< Port E selected */
  129. #define GPIO_PORT_H (0x05U) /*!< Port H selected */
  130. /**
  131. * @}
  132. */
  133. /**
  134. * @defgroup GPIO_Function_Sel GPIO Function Selection
  135. * @{
  136. */
  137. #define GPIO_FUNC_0 (0U)
  138. #define GPIO_FUNC_1 (1U)
  139. #define GPIO_FUNC_2 (2U)
  140. #define GPIO_FUNC_3 (3U)
  141. #define GPIO_FUNC_4 (4U)
  142. #define GPIO_FUNC_5 (5U)
  143. #define GPIO_FUNC_6 (6U)
  144. #define GPIO_FUNC_7 (7U)
  145. #define GPIO_FUNC_8 (8U)
  146. #define GPIO_FUNC_11 (11U)
  147. #define GPIO_FUNC_12 (12U)
  148. #define GPIO_FUNC_13 (13U)
  149. #define GPIO_FUNC_14 (14U)
  150. #define GPIO_FUNC_15 (15U)
  151. #define GPIO_FUNC_21 (21U)
  152. #define GPIO_FUNC_22 (22U)
  153. #define GPIO_FUNC_32 (32U)
  154. #define GPIO_FUNC_33 (33U)
  155. #define GPIO_FUNC_34 (34U)
  156. #define GPIO_FUNC_35 (35U)
  157. #define GPIO_FUNC_36 (36U)
  158. #define GPIO_FUNC_37 (37U)
  159. #define GPIO_FUNC_38 (38U)
  160. #define GPIO_FUNC_39 (39U)
  161. #define GPIO_FUNC_40 (40U)
  162. #define GPIO_FUNC_41 (41U)
  163. #define GPIO_FUNC_42 (42U)
  164. #define GPIO_FUNC_43 (43U)
  165. #define GPIO_FUNC_44 (44U)
  166. #define GPIO_FUNC_45 (45U)
  167. #define GPIO_FUNC_46 (46U)
  168. #define GPIO_FUNC_47 (47U)
  169. #define GPIO_FUNC_48 (48U)
  170. #define GPIO_FUNC_49 (49U)
  171. #define GPIO_FUNC_50 (50U)
  172. #define GPIO_FUNC_51 (51U)
  173. #define GPIO_FUNC_52 (52U)
  174. #define GPIO_FUNC_53 (53U)
  175. #define GPIO_FUNC_54 (54U)
  176. #define GPIO_FUNC_55 (55U)
  177. #define GPIO_FUNC_56 (56U)
  178. #define GPIO_FUNC_57 (57U)
  179. /**
  180. * @}
  181. */
  182. /**
  183. * @defgroup GPIO_DebugPin_Sel GPIO Debug Pin Selection
  184. * @{
  185. */
  186. #define GPIO_PIN_TCK (0x01U)
  187. #define GPIO_PIN_TMS (0x02U)
  188. #define GPIO_PIN_TDO (0x04U)
  189. #define GPIO_PIN_TDI (0x08U)
  190. #define GPIO_PIN_TRST (0x10U)
  191. #define GPIO_PIN_DEBUG_JTAG (0x1FU)
  192. #define GPIO_PIN_SWCLK (0x01U)
  193. #define GPIO_PIN_SWDIO (0x02U)
  194. #define GPIO_PIN_SWO (0x04U)
  195. #define GPIO_PIN_DEBUG_SWD (0x07U)
  196. #define GPIO_PIN_DEBUG (0x1FU)
  197. /**
  198. * @}
  199. */
  200. /**
  201. * @defgroup GPIO_ReadCycle_Sel GPIO Pin Read Wait Cycle Selection
  202. * @{
  203. */
  204. #define GPIO_RD_WAIT0 (0x00U << GPIO_PCCR_RDWT_POS)
  205. #define GPIO_RD_WAIT1 (0x01U << GPIO_PCCR_RDWT_POS)
  206. #define GPIO_RD_WAIT2 (0x02U << GPIO_PCCR_RDWT_POS)
  207. #define GPIO_RD_WAIT3 (0x03U << GPIO_PCCR_RDWT_POS)
  208. #define GPIO_RD_WAIT4 (0x04U << GPIO_PCCR_RDWT_POS)
  209. #define GPIO_RD_WAIT5 (0x05U << GPIO_PCCR_RDWT_POS)
  210. #define GPIO_RD_WAIT6 (0x06U << GPIO_PCCR_RDWT_POS)
  211. #define GPIO_RD_WAIT7 (0x07U << GPIO_PCCR_RDWT_POS)
  212. /**
  213. * @}
  214. */
  215. /**
  216. * @defgroup GPIO_PinState_Sel GPIO Pin Output State Selection
  217. * @{
  218. */
  219. #define PIN_STAT_RST (0U)
  220. #define PIN_STAT_SET (GPIO_PCR_POUT)
  221. /**
  222. * @}
  223. */
  224. /**
  225. * @defgroup GPIO_PinDirection_Sel GPIO Pin Input/Output Direction Selection
  226. * @{
  227. */
  228. #define PIN_DIR_IN (0U)
  229. #define PIN_DIR_OUT (GPIO_PCR_POUTE)
  230. /**
  231. * @}
  232. */
  233. /**
  234. * @defgroup GPIO_PinOutType_Sel GPIO Pin Output Type Selection
  235. * @{
  236. */
  237. #define PIN_OUT_TYPE_CMOS (0U)
  238. #define PIN_OUT_TYPE_NMOS (GPIO_PCR_NOD)
  239. /**
  240. * @}
  241. */
  242. /**
  243. * @defgroup GPIO_PinDrv_Sel GPIO Pin Drive Capacity Selection
  244. * @{
  245. */
  246. #define PIN_LOW_DRV (0U)
  247. #define PIN_MID_DRV (GPIO_PCR_DRV_0)
  248. #define PIN_HIGH_DRV (GPIO_PCR_DRV_1)
  249. /**
  250. * @}
  251. */
  252. /**
  253. * @defgroup GPIO_PinLatch_Sel GPIO Pin Output Latch Selection
  254. * @{
  255. */
  256. #define PIN_LATCH_OFF (0U)
  257. #define PIN_LATCH_ON (GPIO_PCR_LTE)
  258. /**
  259. * @}
  260. */
  261. /**
  262. * @defgroup GPIO_PinPU_Sel GPIO Pin Internal Pull-Up Resistor Selection
  263. * @{
  264. */
  265. #define PIN_PU_OFF (0U)
  266. #define PIN_PU_ON (GPIO_PCR_PUU)
  267. /**
  268. * @}
  269. */
  270. /**
  271. * @defgroup GPIO_PinPD_Sel GPIO Pin Internal Pull-Down Resistor Selection
  272. * @{
  273. */
  274. #define PIN_PD_OFF (0U)
  275. #define PIN_PD_ON (GPIO_PCR_PUD)
  276. /**
  277. * @}
  278. */
  279. /**
  280. * @defgroup GPIO_PinInputSw_Sel GPIO Pin Input Switch Resistor Selection
  281. * @{
  282. */
  283. #define PIN_IN_SW_OFF (0U)
  284. #define PIN_IN_SW_ON (GPIO_PCR_PINAE)
  285. /**
  286. * @}
  287. */
  288. /**
  289. * @defgroup GPIO_PinInvert_Sel GPIO Pin I/O Invert Selection
  290. * @{
  291. */
  292. #define PIN_INVT_OFF (0U)
  293. #define PIN_INVT_ON (GPIO_PCR_INVE)
  294. /**
  295. * @}
  296. */
  297. /**
  298. * @defgroup GPIO_PinInType_Sel GPIO Pin Input Type Selection
  299. * @{
  300. */
  301. #define PIN_IN_TYPE_SMT (0U)
  302. #define PIN_IN_TYPE_CMOS (GPIO_PCR_CINSEL)
  303. /**
  304. * @}
  305. */
  306. /**
  307. * @defgroup GPIO_PinExtInt_Sel GPIO Pin External Interrupt Selection
  308. * @{
  309. */
  310. #define PIN_EXTINT_OFF (0U)
  311. #define PIN_EXTINT_ON (GPIO_PCR_INTE)
  312. /**
  313. * @}
  314. */
  315. /**
  316. * @defgroup GPIO_PinMode_Sel GPIO Pin Mode Selection
  317. * @{
  318. */
  319. #define PIN_ATTR_DIGITAL (0U)
  320. #define PIN_ATTR_ANALOG (GPIO_PCR_DDIS)
  321. /**
  322. * @}
  323. */
  324. /**
  325. * @defgroup GPIO_PinSubFuncSet_Sel GPIO Pin Sub-function Enable or Disable
  326. * @{
  327. */
  328. #define PIN_SUBFUNC_DISABLE (0U)
  329. #define PIN_SUBFUNC_ENABLE (GPIO_PFSR_BFE)
  330. /**
  331. * @}
  332. */
  333. /**
  334. * @defgroup GPIO_Register_Protect_Key GPIO Registers Protect Key
  335. * @{
  336. */
  337. #define GPIO_REG_LOCK_KEY (0xA500U)
  338. #define GPIO_REG_UNLOCK_KEY (0xA501U)
  339. /**
  340. * @}
  341. */
  342. /**
  343. * @}
  344. */
  345. /*******************************************************************************
  346. * Global variable definitions ('extern')
  347. ******************************************************************************/
  348. /*******************************************************************************
  349. Global function prototypes (definition in C source)
  350. ******************************************************************************/
  351. /**
  352. * @addtogroup GPIO_Global_Functions
  353. * @{
  354. */
  355. /**
  356. * @brief GPIO lock. PSPCR, PCCR, PINAER, PCRxy, PFSRxy write disable
  357. * @param None
  358. * @retval None
  359. */
  360. __STATIC_INLINE void GPIO_REG_Lock(void)
  361. {
  362. WRITE_REG16(CM_GPIO->PWPR, GPIO_REG_LOCK_KEY);
  363. }
  364. /**
  365. * @brief GPIO unlock. PSPCR, PCCR, PINAER, PCRxy, PFSRxy write enable
  366. * @param None
  367. * @retval None
  368. */
  369. __STATIC_INLINE void GPIO_REG_Unlock(void)
  370. {
  371. WRITE_REG16(CM_GPIO->PWPR, GPIO_REG_UNLOCK_KEY);
  372. }
  373. int32_t GPIO_Init(uint8_t u8Port, uint16_t u16Pin, const stc_gpio_init_t *pstcGpioInit);
  374. void GPIO_DeInit(void);
  375. int32_t GPIO_StructInit(stc_gpio_init_t *pstcGpioInit);
  376. void GPIO_SetDebugPort(uint8_t u8DebugPort, en_functional_state_t enNewState);
  377. void GPIO_SetFunc(uint8_t u8Port, uint16_t u16Pin, uint16_t u16Func);
  378. void GPIO_SubFuncCmd(uint8_t u8Port, uint16_t u16Pin, en_functional_state_t enNewState);
  379. void GPIO_SetSubFunc(uint8_t u8Func);
  380. void GPIO_SetReadWaitCycle(uint16_t u16ReadWait);
  381. void GPIO_OutputCmd(uint8_t u8Port, uint16_t u16Pin, en_functional_state_t enNewState);
  382. en_pin_state_t GPIO_ReadInputPins(uint8_t u8Port, uint16_t u16Pin);
  383. uint16_t GPIO_ReadInputPort(uint8_t u8Port);
  384. en_pin_state_t GPIO_ReadOutputPins(uint8_t u8Port, uint16_t u16Pin);
  385. uint16_t GPIO_ReadOutputPort(uint8_t u8Port);
  386. void GPIO_SetPins(uint8_t u8Port, uint16_t u16Pin);
  387. void GPIO_ResetPins(uint8_t u8Port, uint16_t u16Pin);
  388. void GPIO_WritePort(uint8_t u8Port, uint16_t u16PortVal);
  389. void GPIO_TogglePins(uint8_t u8Port, uint16_t u16Pin);
  390. void GPIO_ExIntCmd(uint8_t u8Port, uint16_t u16Pin, en_functional_state_t enNewState);
  391. void GPIO_AnalogCmd(uint8_t u8Port, uint16_t u16Pin, en_functional_state_t enNewState);
  392. /**
  393. * @}
  394. */
  395. #endif /* LL_GPIO_ENABLE */
  396. /**
  397. * @}
  398. */
  399. /**
  400. * @}
  401. */
  402. #ifdef __cplusplus
  403. }
  404. #endif
  405. #endif /* __HC32_LL_GPIO_H__ */
  406. /*******************************************************************************
  407. * EOF (not truncated)
  408. ******************************************************************************/