ald_gpio.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file ald_gpio.h
  5. * @brief Header file of GPIO module driver
  6. *
  7. * @version V1.0
  8. * @date 07 Nov 2017
  9. * @author AE Team
  10. * @note
  11. *
  12. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  13. *
  14. *********************************************************************************
  15. */
  16. #ifndef __ALD_GPIO_H__
  17. #define __ALD_GPIO_H__
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "utils.h"
  22. /** @addtogroup ES32FXXX_ALD
  23. * @{
  24. */
  25. /** @addtogroup GPIO
  26. * @{
  27. */
  28. /**
  29. * @defgroup GPIO_Public_Macros GPIO Public Macros
  30. * @{
  31. */
  32. #define GPIO_PIN_0 (1U << 0)
  33. #define GPIO_PIN_1 (1U << 1)
  34. #define GPIO_PIN_2 (1U << 2)
  35. #define GPIO_PIN_3 (1U << 3)
  36. #define GPIO_PIN_4 (1U << 4)
  37. #define GPIO_PIN_5 (1U << 5)
  38. #define GPIO_PIN_6 (1U << 6)
  39. #define GPIO_PIN_7 (1U << 7)
  40. #define GPIO_PIN_8 (1U << 8)
  41. #define GPIO_PIN_9 (1U << 9)
  42. #define GPIO_PIN_10 (1U << 10)
  43. #define GPIO_PIN_11 (1U << 11)
  44. #define GPIO_PIN_12 (1U << 12)
  45. #define GPIO_PIN_13 (1U << 13)
  46. #define GPIO_PIN_14 (1U << 14)
  47. #define GPIO_PIN_15 (1U << 15)
  48. #define GPIO_PIN_ALL (0xFFFF)
  49. /**
  50. * @}
  51. */
  52. /**
  53. * @defgroup GPIO_Public_Types GPIO Public Types
  54. * @{
  55. */
  56. /**
  57. * @brief GPIO mode
  58. */
  59. typedef enum {
  60. GPIO_MODE_CLOSE = 0x0, /**< Digital close Analog open */
  61. GPIO_MODE_INPUT = 0x1, /**< Input */
  62. GPIO_MODE_OUTPUT = 0x2, /**< Output */
  63. } gpio_mode_t;
  64. /**
  65. * @brief GPIO open-drain or push-pull
  66. */
  67. typedef enum {
  68. GPIO_PUSH_PULL = 0x0, /**< Push-Pull */
  69. GPIO_OPEN_DRAIN = 0x2, /**< Open-Drain */
  70. GPIO_OPEN_SOURCE = 0x3, /**< Open-Source */
  71. } gpio_odos_t;
  72. /**
  73. * @brief GPIO push-up or push-down
  74. */
  75. typedef enum {
  76. GPIO_FLOATING = 0x0,/**< Floating */
  77. GPIO_PUSH_UP = 0x1,/**< Push-Up */
  78. GPIO_PUSH_DOWN = 0x2,/**< Push-Down */
  79. GPIO_PUSH_UP_DOWN = 0x3,/**< Push-Up and Push-Down */
  80. } gpio_push_t;
  81. /**
  82. * @brief GPIO output drive
  83. */
  84. typedef enum {
  85. GPIO_OUT_DRIVE_NORMAL = 0x0, /**< Normal current flow */
  86. GPIO_OUT_DRIVE_STRONG = 0x1, /**< Strong current flow */
  87. } gpio_out_drive_t;
  88. /**
  89. * @brief GPIO filter
  90. */
  91. typedef enum {
  92. GPIO_FILTER_DISABLE = 0x0, /**< Disable filter */
  93. GPIO_FILTER_ENABLE = 0x1, /**< Enable filter */
  94. } gpio_filter_t;
  95. /**
  96. * @brief GPIO type
  97. */
  98. typedef enum {
  99. GPIO_TYPE_CMOS = 0x0, /**< CMOS Type */
  100. GPIO_TYPE_TTL = 0x1, /**< TTL Type */
  101. } gpio_type_t;
  102. /**
  103. * @brief GPIO functions
  104. */
  105. typedef enum {
  106. GPIO_FUNC_0 = 0, /**< function #0 */
  107. GPIO_FUNC_1 = 1, /**< function #1 */
  108. GPIO_FUNC_2 = 2, /**< function #2 */
  109. GPIO_FUNC_3 = 3, /**< function #3 */
  110. GPIO_FUNC_4 = 4, /**< function #4 */
  111. GPIO_FUNC_5 = 5, /**< function #5 */
  112. GPIO_FUNC_6 = 6, /**< function #6 */
  113. GPIO_FUNC_7 = 7, /**< function #7 */
  114. } gpio_func_t;
  115. /**
  116. * @brief GPIO Init Structure definition
  117. */
  118. typedef struct {
  119. gpio_mode_t mode; /**< Specifies the operating mode for the selected pins.
  120. This parameter can be any value of @ref gpio_mode_t */
  121. gpio_odos_t odos; /**< Specifies the Open-Drain or Push-Pull for the selected pins.
  122. This parameter can be a value of @ref gpio_odos_t */
  123. gpio_push_t pupd; /**< Specifies the Pull-up or Pull-Down for the selected pins.
  124. This parameter can be a value of @ref gpio_push_t */
  125. gpio_out_drive_t odrv; /**< Specifies the output driver for the selected pins.
  126. This parameter can be a value of @ref gpio_out_drive_t */
  127. gpio_filter_t flt; /**< Specifies the input filter for the selected pins.
  128. This parameter can be a value of @ref gpio_filter_t */
  129. gpio_type_t type; /**< Specifies the type for the selected pins.
  130. This parameter can be a value of @ref gpio_type_t */
  131. gpio_func_t func; /**< Specifies the function for the selected pins.
  132. This parameter can be a value of @ref gpio_func_t */
  133. } gpio_init_t;
  134. /**
  135. * @brief EXTI trigger style
  136. */
  137. typedef enum {
  138. EXTI_TRIGGER_RISING_EDGE = 0, /**< Rising edge trigger */
  139. EXTI_TRIGGER_TRAILING_EDGE = 1, /**< Trailing edge trigger */
  140. EXTI_TRIGGER_BOTH_EDGE = 2, /**< Rising and trailing edge trigger */
  141. } exti_trigger_style_t;
  142. /**
  143. * @brief EXTI filter clock select
  144. */
  145. typedef enum {
  146. EXTI_FILTER_CLOCK_10K = 0, /**< cks = 10KHz */
  147. EXTI_FILTER_CLOCK_32K = 1, /**< cks = 32KHz */
  148. } exti_filter_clock_t;
  149. /**
  150. * @brief EXTI Init Structure definition
  151. */
  152. typedef struct {
  153. type_func_t filter; /**< Enable filter. */
  154. exti_filter_clock_t cks; /**< Filter clock select. */
  155. uint8_t filter_time; /**< Filter duration */
  156. } exti_init_t;
  157. /**
  158. * @}
  159. */
  160. /**
  161. * @defgroup GPIO_Private_Macros GPIO Private Macros
  162. * @{
  163. */
  164. #define PIN_MASK 0xFFFF
  165. #define UNLOCK_KEY 0x55AA
  166. #define IS_GPIO_PIN(x) ((((x) & (uint16_t)0x00) == 0) && ((x) != (uint16_t)0x0))
  167. #define IS_GPIO_PORT(GPIOx) ((GPIOx == GPIOA) || \
  168. (GPIOx == GPIOB) || \
  169. (GPIOx == GPIOC) || \
  170. (GPIOx == GPIOD) || \
  171. (GPIOx == GPIOE) || \
  172. (GPIOx == GPIOF) || \
  173. (GPIOx == GPIOG) || \
  174. (GPIOx == GPIOH))
  175. #define IS_GPIO_MODE(x) (((x) == GPIO_MODE_CLOSE) || \
  176. ((x) == GPIO_MODE_INPUT) || \
  177. ((x) == GPIO_MODE_OUTPUT))
  178. #define IS_GPIO_ODOS(x) (((x) == GPIO_PUSH_PULL) || \
  179. ((x) == GPIO_OPEN_DRAIN) || \
  180. ((x) == GPIO_OPEN_SOURCE))
  181. #define IS_GPIO_PUPD(x) (((x) == GPIO_FLOATING) || \
  182. ((x) == GPIO_PUSH_UP) || \
  183. ((x) == GPIO_PUSH_DOWN) || \
  184. ((x) == GPIO_PUSH_UP_DOWN))
  185. #define IS_GPIO_ODRV(x) (((x) == GPIO_OUT_DRIVE_NORMAL) || \
  186. ((x) == GPIO_OUT_DRIVE_STRONG))
  187. #define IS_GPIO_FLT(x) (((x) == GPIO_FILTER_DISABLE) || \
  188. ((x) == GPIO_FILTER_ENABLE))
  189. #define IS_GPIO_TYPE(x) (((x) == GPIO_TYPE_TTL) || \
  190. ((x) == GPIO_TYPE_CMOS))
  191. #define IS_TRIGGER_STYLE(x) (((x) == EXTI_TRIGGER_RISING_EDGE) || \
  192. ((x) == EXTI_TRIGGER_TRAILING_EDGE) || \
  193. ((x) == EXTI_TRIGGER_BOTH_EDGE))
  194. #define IS_EXTI_FLTCKS_TYPE(x) (((x) == EXTI_FILTER_CLOCK_10K) || \
  195. ((x) == EXTI_FILTER_CLOCK_32K))
  196. #define IS_GPIO_FUNC(x) ((x) <= 7)
  197. /**
  198. * @}
  199. */
  200. /** @addtogroup GPIO_Public_Functions
  201. * @{
  202. */
  203. /** @addtogroup GPIO_Public_Functions_Group1
  204. * @{
  205. */
  206. void gpio_init(GPIO_TypeDef *GPIOx, uint16_t pin, gpio_init_t *init);
  207. void gpio_init_default(GPIO_TypeDef *GPIOx, uint16_t pin);
  208. void gpio_func_default(GPIO_TypeDef *GPIOx);
  209. void gpio_exti_init(GPIO_TypeDef *GPIOx, uint16_t pin, exti_init_t *init);
  210. /**
  211. * @}
  212. */
  213. /** @addtogroup GPIO_Public_Functions_Group2
  214. * @{
  215. */
  216. uint8_t gpio_read_pin(GPIO_TypeDef *GPIOx, uint16_t pin);
  217. void gpio_write_pin(GPIO_TypeDef *GPIOx, uint16_t pin, uint8_t val);
  218. void gpio_toggle_pin(GPIO_TypeDef *GPIOx, uint16_t pin);
  219. void gpio_toggle_dir(GPIO_TypeDef *GPIOx, uint16_t pin);
  220. void gpio_lock_pin(GPIO_TypeDef *GPIOx, uint16_t pin);
  221. uint16_t gpio_read_port(GPIO_TypeDef *GPIOx);
  222. void gpio_write_port(GPIO_TypeDef *GPIOx, uint16_t val);
  223. /**
  224. * @}
  225. */
  226. /** @addtogroup GPIO_Public_Functions_Group3
  227. * @{
  228. */
  229. void gpio_exti_interrupt_config(uint16_t pin, exti_trigger_style_t style, type_func_t status);
  230. flag_status_t gpio_exti_get_flag_status(uint16_t pin);
  231. void gpio_exti_clear_flag_status(uint16_t pin);
  232. /**
  233. * @}
  234. */
  235. /**
  236. * @}
  237. */
  238. /**
  239. * @}
  240. */
  241. /**
  242. * @}
  243. */
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. #endif /* __ALD_GPIO_H__ */