hpm_gpio_drv.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_GPIO_DRV_H
  8. #define HPM_GPIO_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_gpio_regs.h"
  11. #ifndef PORT_PIN_COUNT
  12. #define PORT_PIN_COUNT (32U)
  13. #endif
  14. #define GPIO_GET_PORT_INDEX(x) (x / PORT_PIN_COUNT)
  15. #define GPIO_GET_PIN_INDEX(x) (x % PORT_PIN_COUNT)
  16. /**
  17. *
  18. * @brief GPIO driver APIs
  19. * @defgroup gpio_interface GPIO driver APIs
  20. * @ingroup io_interfaces
  21. * @{
  22. */
  23. /**
  24. * @brief Interrupt trigger type
  25. */
  26. typedef enum gpio_interrupt_trigger {
  27. gpio_interrupt_trigger_level_high = 0,
  28. gpio_interrupt_trigger_level_low,
  29. gpio_interrupt_trigger_edge_rising,
  30. gpio_interrupt_trigger_edge_falling,
  31. } gpio_interrupt_trigger_t;
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /**
  36. * @brief Read target pin level
  37. *
  38. * @param ptr GPIO base address
  39. * @param port Port index
  40. * @param pin Pin index
  41. *
  42. * @return Pin status mask
  43. */
  44. static inline uint8_t gpio_read_pin(GPIO_Type *ptr, uint32_t port, uint8_t pin)
  45. {
  46. return (ptr->DI[port].VALUE & (1 << pin)) >> pin;
  47. }
  48. /**
  49. * @brief Toggle pin level
  50. *
  51. * @param ptr GPIO base address
  52. * @param port Port index
  53. * @param pin Pin index
  54. */
  55. static inline void gpio_toggle_pin(GPIO_Type *ptr, uint32_t port, uint8_t pin)
  56. {
  57. ptr->DO[port].TOGGLE = 1 << pin;
  58. }
  59. /**
  60. * @brief Write pin level
  61. *
  62. * @param ptr GPIO base address
  63. * @param port Port index
  64. * @param pin Pin index
  65. * @param high Pin level set to high when it is set to true
  66. */
  67. static inline void gpio_write_pin(GPIO_Type *ptr, uint32_t port, uint8_t pin, uint8_t high)
  68. {
  69. if (high) {
  70. ptr->DO[port].SET = 1 << pin;
  71. } else {
  72. ptr->DO[port].CLEAR = 1 << pin;
  73. }
  74. }
  75. /**
  76. * @brief Set pin to input mode
  77. *
  78. * @param ptr GPIO base address
  79. * @param port Port index
  80. * @param pin Pin index
  81. */
  82. static inline void gpio_set_pin_input(GPIO_Type *ptr, uint32_t port, uint8_t pin)
  83. {
  84. ptr->OE[port].CLEAR = 1 << pin;
  85. }
  86. /**
  87. * @brief Set pin to output mode
  88. *
  89. * @param ptr GPIO base address
  90. * @param port Port index
  91. * @param pin Pin index
  92. */
  93. static inline void gpio_set_pin_output(GPIO_Type *ptr, uint32_t port, uint8_t pin)
  94. {
  95. ptr->OE[port].SET = 1 << pin;
  96. }
  97. /**
  98. * @brief Set pin to output mode with initial value
  99. *
  100. * @param ptr GPIO base address
  101. * @param port Port index
  102. * @param pin Pin index
  103. * @param initial Initial value
  104. */
  105. void gpio_set_pin_output_with_initial(GPIO_Type *ptr, uint32_t port, uint8_t pin, uint8_t initial);
  106. /**
  107. * @brief Check specific pin interrupt status
  108. *
  109. * @param ptr GPIO base address
  110. * @param port Port index
  111. * @param pin Pin index
  112. *
  113. * @return true if interrupt flag is set
  114. */
  115. static inline bool gpio_check_pin_interrupt_flag(GPIO_Type *ptr, uint32_t port, uint8_t pin)
  116. {
  117. return ptr->IF[port].VALUE & (1 << pin);
  118. }
  119. /**
  120. * @brief Clear specific pin interrupt flag
  121. *
  122. * @param ptr GPIO base address
  123. * @param port Port index
  124. * @param pin Pin index
  125. */
  126. static inline void gpio_clear_pin_interrupt_flag(GPIO_Type *ptr, uint32_t port, uint8_t pin)
  127. {
  128. ptr->IF[port].VALUE = 1 << pin;
  129. }
  130. /**
  131. * @brief Check if specific pin interrupt is enabled or not
  132. *
  133. * @param ptr GPIO base address
  134. * @param port Port index
  135. * @param pin Pin index
  136. *
  137. * @return true if interrupt is enabled
  138. */
  139. static inline bool gpio_check_pin_interrupt_enabled(GPIO_Type *ptr, uint32_t port, uint8_t pin)
  140. {
  141. return (ptr->IE[port].VALUE & (1 << pin)) == (1 << pin);
  142. }
  143. /**
  144. * @brief Enable interrupt for specific pin
  145. *
  146. * @param ptr GPIO base address
  147. * @param port Port index
  148. * @param pin Pin index
  149. */
  150. static inline void gpio_enable_pin_interrupt(GPIO_Type *ptr, uint32_t port, uint8_t pin)
  151. {
  152. ptr->IE[port].SET = 1 << pin;
  153. }
  154. /**
  155. * @brief Disable interrupt for specific pin
  156. *
  157. * @param ptr GPIO base address
  158. * @param port Port index
  159. * @param pin Pin index
  160. */
  161. static inline void gpio_disable_pin_interrupt(GPIO_Type *ptr, uint32_t port, uint8_t pin)
  162. {
  163. ptr->IE[port].CLEAR = 1 << pin;
  164. }
  165. /**
  166. * @brief Check interrupt flag of specific pin and clear if it is set
  167. *
  168. * @param ptr GPIO base address
  169. * @param port Port index
  170. * @param pin Pin index
  171. *
  172. * @return true if the interrupt flag is set and cleared
  173. * @return false if the interrupt flag is not set
  174. */
  175. static inline bool gpio_check_clear_interrupt_flag(GPIO_Type *ptr, uint32_t port, uint8_t pin)
  176. {
  177. if (!gpio_check_pin_interrupt_flag(ptr, port, pin)) {
  178. return false;
  179. }
  180. gpio_clear_pin_interrupt_flag(ptr, port, pin);
  181. return true;
  182. }
  183. /**
  184. * @brief Read all pins level on specific port
  185. *
  186. * @param ptr GPIO base address
  187. * @param port Port index
  188. *
  189. * @return Port pin level status
  190. */
  191. static inline uint32_t gpio_read_port(GPIO_Type *ptr, uint32_t port)
  192. {
  193. return ptr->DI[port].VALUE;
  194. }
  195. /**
  196. * @brief Toggle port with specific pin mask
  197. *
  198. * @param ptr GPIO base address
  199. * @param port Port index
  200. * @param mask Mask pins to be toggled
  201. */
  202. static inline void gpio_toggle_port_with_mask(GPIO_Type *ptr, uint32_t port, uint32_t mask)
  203. {
  204. ptr->DO[port].TOGGLE = mask;
  205. }
  206. /**
  207. * @brief Write specific port with value
  208. *
  209. * @param ptr GPIO base address
  210. * @param port Port index
  211. * @param value Value to be written
  212. */
  213. static inline void gpio_write_port(GPIO_Type *ptr, uint32_t port, uint32_t value)
  214. {
  215. ptr->DO[port].VALUE = value;
  216. }
  217. /**
  218. * @brief Set spcific port pin high according to the given mask
  219. *
  220. * @param ptr GPIO base address
  221. * @param port Port index
  222. * @param mask Mask of pins to be set to low
  223. */
  224. static inline void gpio_set_port_low_with_mask(GPIO_Type *ptr, uint32_t port, uint32_t mask)
  225. {
  226. ptr->DO[port].CLEAR = mask;
  227. }
  228. /**
  229. * @brief Set spcific port pin high according to the given mask
  230. *
  231. * @param ptr GPIO base address
  232. * @param port Port index
  233. * @param mask Mask of pins to be set to high
  234. */
  235. static inline void gpio_set_port_high_with_mask(GPIO_Type *ptr, uint32_t port, uint32_t mask)
  236. {
  237. ptr->DO[port].SET = mask;
  238. }
  239. /**
  240. * @brief Enable pins output of specific port according to the given mask
  241. *
  242. * @param ptr GPIO base address
  243. * @param port Port index
  244. * @param mask Mask of pins to be enabled
  245. */
  246. static inline void gpio_enable_port_output_with_mask(GPIO_Type *ptr, uint32_t port, uint32_t mask)
  247. {
  248. ptr->OE[port].SET = mask;
  249. }
  250. /**
  251. * @brief Disable pins output of specific port according to the given mask
  252. *
  253. * @param ptr GPIO base address
  254. * @param port Port index
  255. * @param mask Mask of pins to be disabled
  256. */
  257. static inline void gpio_disable_port_output_with_mask(GPIO_Type *ptr, uint32_t port, uint32_t mask)
  258. {
  259. ptr->OE[port].CLEAR = mask;
  260. }
  261. /**
  262. * @brief Get current interrupt flags on specific port
  263. *
  264. * @param ptr GPIO base address
  265. * @param port Port index
  266. *
  267. * @return Current interrupt flags on specific port
  268. */
  269. static inline uint32_t gpio_get_port_interrupt_flags(GPIO_Type *ptr, uint32_t port)
  270. {
  271. return ptr->IF[port].VALUE;
  272. }
  273. /**
  274. * @brief Clear interrupt flags with given mask on specific port
  275. *
  276. * @param ptr GPIO base address
  277. * @param port Port index
  278. * @param mask Mask of interrupts to be cleared
  279. */
  280. static inline void gpio_clear_port_interrupt_flags_with_mask(GPIO_Type *ptr, uint32_t port, uint32_t mask)
  281. {
  282. ptr->IF[port].VALUE |= mask;
  283. }
  284. /**
  285. * @brief Enable interrupts with given mask on specific port
  286. *
  287. * @param ptr GPIO base address
  288. * @param port Port index
  289. * @param mask Mask of interrupts to be enabled
  290. */
  291. static inline void gpio_enable_port_interrupt_with_mask(GPIO_Type *ptr, uint32_t port, uint8_t mask)
  292. {
  293. ptr->IE[port].SET = mask;
  294. }
  295. /**
  296. * @brief Disable interrupts with given mask on specific port
  297. *
  298. * @param ptr GPIO base address
  299. * @param port Port index
  300. * @param mask Mask of interrupts to be disabled
  301. */
  302. static inline void gpio_disable_port_interrupt_with_mask(GPIO_Type *ptr, uint32_t port, uint8_t mask)
  303. {
  304. ptr->IE[port].CLEAR = mask;
  305. }
  306. /**
  307. * @brief Config pin interrupt
  308. *
  309. * @param ptr GPIO base address
  310. * @param port Port index
  311. * @param pin Pin index
  312. * @param trigger Trigger type
  313. */
  314. void gpio_config_pin_interrupt(GPIO_Type *ptr, uint32_t port, uint8_t pin, gpio_interrupt_trigger_t trigger);
  315. /**
  316. * @brief Toggle pin interrupt trigger polarity
  317. *
  318. * @param ptr GPIO base address
  319. * @param port Port index
  320. * @param pin Pin index
  321. */
  322. void gpio_toggle_pin_interrupt_trigger_polarity(GPIO_Type *ptr, uint32_t port, uint8_t pin);
  323. /**
  324. * @brief Toggle pin interrupt trigger type
  325. *
  326. * @param ptr GPIO base address
  327. * @param port Port index
  328. * @param pin Pin index
  329. */
  330. void gpio_toggle_pin_interrupt_trigger_type(GPIO_Type *ptr, uint32_t port, uint8_t pin);
  331. #ifdef __cplusplus
  332. }
  333. #endif
  334. /**
  335. * @}
  336. */
  337. #endif /* HPM_GPIO_DRV_H */