gpio.h 8.1 KB

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