gpio.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "esp_attr.h"
  10. #include "soc/gpio_reg.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /** \defgroup gpio_apis, uart configuration and communication related apis
  15. * @brief gpio apis
  16. */
  17. /** @addtogroup gpio_apis
  18. * @{
  19. */
  20. #define GPIO_REG_READ(reg) READ_PERI_REG(reg)
  21. #define GPIO_REG_WRITE(reg, val) WRITE_PERI_REG(reg, val)
  22. #define GPIO_ID_PIN0 0
  23. #define GPIO_ID_PIN(n) (GPIO_ID_PIN0+(n))
  24. #define GPIO_PIN_ADDR(i) (GPIO_PIN0_REG + i*4)
  25. #define GPIO_FUNC_IN_HIGH 0x3F
  26. #define GPIO_FUNC_IN_LOW 0x3E
  27. #define GPIO_ID_IS_PIN_REGISTER(reg_id) \
  28. ((reg_id >= GPIO_ID_PIN0) && (reg_id <= GPIO_ID_PIN(GPIO_PIN_COUNT-1)))
  29. #define GPIO_REGID_TO_PINIDX(reg_id) ((reg_id) - GPIO_ID_PIN0)
  30. typedef enum {
  31. GPIO_PIN_INTR_DISABLE = 0,
  32. GPIO_PIN_INTR_POSEDGE = 1,
  33. GPIO_PIN_INTR_NEGEDGE = 2,
  34. GPIO_PIN_INTR_ANYEDGE = 3,
  35. GPIO_PIN_INTR_LOLEVEL = 4,
  36. GPIO_PIN_INTR_HILEVEL = 5
  37. } GPIO_INT_TYPE;
  38. #define GPIO_OUTPUT_SET(gpio_no, bit_value) \
  39. ((gpio_no < 32) ? gpio_output_set(bit_value<<gpio_no, (bit_value ? 0 : 1)<<gpio_no, 1<<gpio_no,0) : \
  40. gpio_output_set_high(bit_value<<(gpio_no - 32), (bit_value ? 0 : 1)<<(gpio_no - 32), 1<<(gpio_no -32),0))
  41. #define GPIO_DIS_OUTPUT(gpio_no) ((gpio_no < 32) ? gpio_output_set(0,0,0, 1<<gpio_no) : gpio_output_set_high(0,0,0, 1<<(gpio_no - 32)))
  42. #define GPIO_INPUT_GET(gpio_no) ((gpio_no < 32) ? ((gpio_input_get()>>gpio_no)&BIT0) : ((gpio_input_get_high()>>(gpio_no - 32))&BIT0))
  43. /* GPIO interrupt handler, registered through gpio_intr_handler_register */
  44. typedef void (* gpio_intr_handler_fn_t)(uint32_t intr_mask, bool high, void *arg);
  45. /**
  46. * @brief Initialize GPIO. This includes reading the GPIO Configuration DataSet
  47. * to initialize "output enables" and pin configurations for each gpio pin.
  48. * Please do not call this function in SDK.
  49. *
  50. * @param None
  51. *
  52. * @return None
  53. */
  54. void gpio_init(void);
  55. /**
  56. * @brief Change GPIO(0-31) pin output by setting, clearing, or disabling pins, GPIO0<->BIT(0).
  57. * There is no particular ordering guaranteed; so if the order of writes is significant,
  58. * calling code should divide a single call into multiple calls.
  59. *
  60. * @param uint32_t set_mask : the gpios that need high level.
  61. *
  62. * @param uint32_t clear_mask : the gpios that need low level.
  63. *
  64. * @param uint32_t enable_mask : the gpios that need be changed.
  65. *
  66. * @param uint32_t disable_mask : the gpios that need diable output.
  67. *
  68. * @return None
  69. */
  70. void gpio_output_set(uint32_t set_mask, uint32_t clear_mask, uint32_t enable_mask, uint32_t disable_mask);
  71. /**
  72. * @brief Change GPIO(32-56) pin output by setting, clearing, or disabling pins, GPIO32<->BIT(0).
  73. * There is no particular ordering guaranteed; so if the order of writes is significant,
  74. * calling code should divide a single call into multiple calls.
  75. *
  76. * @param uint32_t set_mask : the gpios that need high level.
  77. *
  78. * @param uint32_t clear_mask : the gpios that need low level.
  79. *
  80. * @param uint32_t enable_mask : the gpios that need be changed.
  81. *
  82. * @param uint32_t disable_mask : the gpios that need diable output.
  83. *
  84. * @return None
  85. */
  86. void gpio_output_set_high(uint32_t set_mask, uint32_t clear_mask, uint32_t enable_mask, uint32_t disable_mask);
  87. /**
  88. * @brief Sample the value of GPIO input pins(0-31) and returns a bitmask.
  89. *
  90. * @param None
  91. *
  92. * @return uint32_t : bitmask for GPIO input pins, BIT(0) for GPIO0.
  93. */
  94. uint32_t gpio_input_get(void);
  95. /**
  96. * @brief Sample the value of GPIO input pins(32-56) and returns a bitmask.
  97. *
  98. * @param None
  99. *
  100. * @return uint32_t : bitmask for GPIO input pins, BIT(0) for GPIO32.
  101. */
  102. uint32_t gpio_input_get_high(void);
  103. /**
  104. * @brief Register an application-specific interrupt handler for GPIO pin interrupts.
  105. * Once the interrupt handler is called, it will not be called again until after a call to gpio_intr_ack.
  106. * Please do not call this function in SDK.
  107. *
  108. * @param gpio_intr_handler_fn_t fn : gpio application-specific interrupt handler
  109. *
  110. * @param void *arg : gpio application-specific interrupt handler argument.
  111. *
  112. * @return None
  113. */
  114. void gpio_intr_handler_register(gpio_intr_handler_fn_t fn, void *arg);
  115. /**
  116. * @brief Get gpio interrupts which happens but not processed.
  117. * Please do not call this function in SDK.
  118. *
  119. * @param None
  120. *
  121. * @return uint32_t : bitmask for GPIO pending interrupts, BIT(0) for GPIO0.
  122. */
  123. uint32_t gpio_intr_pending(void);
  124. /**
  125. * @brief Get gpio interrupts which happens but not processed.
  126. * Please do not call this function in SDK.
  127. *
  128. * @param None
  129. *
  130. * @return uint32_t : bitmask for GPIO pending interrupts, BIT(0) for GPIO32.
  131. */
  132. uint32_t gpio_intr_pending_high(void);
  133. /**
  134. * @brief Ack gpio interrupts to process pending interrupts.
  135. * Please do not call this function in SDK.
  136. *
  137. * @param uint32_t ack_mask: bitmask for GPIO ack interrupts, BIT(0) for GPIO0.
  138. *
  139. * @return None
  140. */
  141. void gpio_intr_ack(uint32_t ack_mask);
  142. /**
  143. * @brief Ack gpio interrupts to process pending interrupts.
  144. * Please do not call this function in SDK.
  145. *
  146. * @param uint32_t ack_mask: bitmask for GPIO ack interrupts, BIT(0) for GPIO32.
  147. *
  148. * @return None
  149. */
  150. void gpio_intr_ack_high(uint32_t ack_mask);
  151. /**
  152. * @brief Set GPIO to wakeup the ESP32P4.
  153. * Please do not call this function in SDK.
  154. *
  155. * @param uint32_t i: gpio number.
  156. *
  157. * @param GPIO_INT_TYPE intr_state : only GPIO_PIN_INTR_LOLEVEL\GPIO_PIN_INTR_HILEVEL can be used
  158. *
  159. * @return None
  160. */
  161. void gpio_pin_wakeup_enable(uint32_t i, GPIO_INT_TYPE intr_state);
  162. /**
  163. * @brief disable GPIOs to wakeup the ESP32P4.
  164. * Please do not call this function in SDK.
  165. *
  166. * @param None
  167. *
  168. * @return None
  169. */
  170. void gpio_pin_wakeup_disable(void);
  171. /**
  172. * @brief set gpio input to a signal, one gpio can input to several signals.
  173. *
  174. * @param uint32_t gpio : gpio number, 0~56
  175. * gpio == 0x3E, input 0 to signal
  176. * gpio == 0x3F, input 1 to signal
  177. *
  178. * @param uint32_t signal_idx : signal index.
  179. *
  180. * @param bool inv : the signal is inv or not
  181. *
  182. * @return None
  183. */
  184. void gpio_matrix_in(uint32_t gpio, uint32_t signal_idx, bool inv);
  185. /**
  186. * @brief set signal output to gpio, one signal can output to several gpios.
  187. *
  188. * @param uint32_t gpio : gpio number, 0~56
  189. *
  190. * @param uint32_t signal_idx : signal index.
  191. * signal_idx == 0x100, cancel output put to the gpio
  192. *
  193. * @param bool out_inv : the signal output is invert or not
  194. *
  195. * @param bool oen_inv : the signal output enable is invert or not
  196. *
  197. * @return None
  198. */
  199. void gpio_matrix_out(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv);
  200. /**
  201. * @brief Select pad as a gpio function from IOMUX.
  202. *
  203. * @param uint32_t gpio_num : gpio number, 0~56
  204. *
  205. * @return None
  206. */
  207. void gpio_pad_select_gpio(uint32_t gpio_num);
  208. /**
  209. * @brief Set pad driver capability.
  210. *
  211. * @param uint32_t gpio_num : gpio number, 0~56
  212. *
  213. * @param uint32_t drv : 0-3
  214. *
  215. * @return None
  216. */
  217. void gpio_pad_set_drv(uint32_t gpio_num, uint32_t drv);
  218. /**
  219. * @brief Pull up the pad from gpio number.
  220. *
  221. * @param uint32_t gpio_num : gpio number, 0~56
  222. *
  223. * @return None
  224. */
  225. void gpio_pad_pullup(uint32_t gpio_num);
  226. /**
  227. * @brief Pull down the pad from gpio number.
  228. *
  229. * @param uint32_t gpio_num : gpio number, 0~56
  230. *
  231. * @return None
  232. */
  233. void gpio_pad_pulldown(uint32_t gpio_num);
  234. /**
  235. * @brief Unhold the pad from gpio number.
  236. *
  237. * @param uint32_t gpio_num : gpio number, 0~56
  238. *
  239. * @return None
  240. */
  241. void gpio_pad_unhold(uint32_t gpio_num);
  242. /**
  243. * @brief Hold the pad from gpio number.
  244. *
  245. * @param uint32_t gpio_num : gpio number, 0~56
  246. *
  247. * @return None
  248. */
  249. void gpio_pad_hold(uint32_t gpio_num);
  250. /**
  251. * @brief enable gpio pad input.
  252. *
  253. * @param uint32_t gpio_num : gpio number, 0~56
  254. *
  255. * @return None
  256. */
  257. void gpio_pad_input_enable(uint32_t gpio_num);
  258. /**
  259. * @brief disable gpio pad input.
  260. *
  261. * @param uint32_t gpio_num : gpio number, 0~56
  262. *
  263. * @return None
  264. */
  265. void gpio_pad_input_disable(uint32_t gpio_num);
  266. /**
  267. * @}
  268. */
  269. #ifdef __cplusplus
  270. }
  271. #endif