gpio.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * SPDX-FileCopyrightText: 2020-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 "soc/gpio_reg.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /** \defgroup gpio_apis, uart configuration and communication related apis
  14. * @brief gpio apis
  15. */
  16. /** @addtogroup gpio_apis
  17. * @{
  18. */
  19. #define GPIO_REG_READ(reg) READ_PERI_REG(reg)
  20. #define GPIO_REG_WRITE(reg, val) WRITE_PERI_REG(reg, val)
  21. #define GPIO_ID_PIN0 0
  22. #define GPIO_ID_PIN(n) (GPIO_ID_PIN0+(n))
  23. #define GPIO_PIN_ADDR(i) (GPIO_PIN0_REG + i*4)
  24. #define GPIO_FUNC_IN_HIGH 0x1E
  25. #define GPIO_FUNC_IN_LOW 0x1F
  26. #define GPIO_ID_IS_PIN_REGISTER(reg_id) \
  27. ((reg_id >= GPIO_ID_PIN0) && (reg_id <= GPIO_ID_PIN(GPIO_PIN_COUNT-1)))
  28. #define GPIO_REGID_TO_PINIDX(reg_id) ((reg_id) - GPIO_ID_PIN0)
  29. typedef enum {
  30. GPIO_PIN_INTR_DISABLE = 0,
  31. GPIO_PIN_INTR_POSEDGE = 1,
  32. GPIO_PIN_INTR_NEGEDGE = 2,
  33. GPIO_PIN_INTR_ANYEDGE = 3,
  34. GPIO_PIN_INTR_LOLEVEL = 4,
  35. GPIO_PIN_INTR_HILEVEL = 5
  36. } GPIO_INT_TYPE;
  37. /**
  38. * @brief Change GPIO(0-20) pin output by setting, clearing, or disabling pins, GPIO0<->BIT(0).
  39. * There is no particular ordering guaranteed; so if the order of writes is significant,
  40. * calling code should divide a single call into multiple calls.
  41. *
  42. * @param uint32_t set_mask : the gpios that need high level.
  43. *
  44. * @param uint32_t clear_mask : the gpios that need low level.
  45. *
  46. * @param uint32_t enable_mask : the gpios that need be changed.
  47. *
  48. * @param uint32_t disable_mask : the gpios that need diable output.
  49. *
  50. * @return None
  51. */
  52. void gpio_output_set(uint32_t set_mask, uint32_t clear_mask, uint32_t enable_mask, uint32_t disable_mask);
  53. /**
  54. * @brief Sample the value of GPIO input pins(0-20) and returns a bitmask.
  55. *
  56. * @param None
  57. *
  58. * @return uint32_t : bitmask for GPIO input pins, BIT(0) for GPIO0.
  59. */
  60. uint32_t gpio_input_get(void);
  61. /**
  62. * @brief Set GPIO to wakeup the ESP32.
  63. * Please do not call this function in SDK.
  64. *
  65. * @param uint32_t i: gpio number.
  66. *
  67. * @param GPIO_INT_TYPE intr_state : only GPIO_PIN_INTR_LOLEVEL\GPIO_PIN_INTR_HILEVEL can be used
  68. *
  69. * @return None
  70. */
  71. void gpio_pin_wakeup_enable(uint32_t i, GPIO_INT_TYPE intr_state);
  72. /**
  73. * @brief disable GPIOs to wakeup the ESP32.
  74. * Please do not call this function in SDK.
  75. *
  76. * @param None
  77. *
  78. * @return None
  79. */
  80. void gpio_pin_wakeup_disable(void);
  81. /**
  82. * @brief set gpio input to a signal, one gpio can input to several signals.
  83. *
  84. * @param uint32_t gpio : gpio number, 0~20
  85. * gpio == 0x1F, input 0 to signal
  86. * gpio == 0x1E, input 1 to signal
  87. *
  88. * @param uint32_t signal_idx : signal index.
  89. *
  90. * @param bool inv : the signal is inv or not
  91. *
  92. * @return None
  93. */
  94. void gpio_matrix_in(uint32_t gpio, uint32_t signal_idx, bool inv);
  95. /**
  96. * @brief set signal output to gpio, one signal can output to several gpios.
  97. *
  98. * @param uint32_t gpio : gpio number, 0~20
  99. *
  100. * @param uint32_t signal_idx : signal index.
  101. * signal_idx == 0x80, cancel output put to the gpio
  102. *
  103. * @param bool out_inv : the signal output is invert or not
  104. *
  105. * @param bool oen_inv : the signal output enable is invert or not
  106. *
  107. * @return None
  108. */
  109. void gpio_matrix_out(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv);
  110. /**
  111. * @brief Select pad as a gpio function from IOMUX.
  112. *
  113. * @param uint32_t gpio_num : gpio number, 0~20
  114. *
  115. * @return None
  116. */
  117. void gpio_pad_select_gpio(uint32_t gpio_num);
  118. /**
  119. * @brief Set pad driver capability.
  120. *
  121. * @param uint32_t gpio_num : gpio number, 0~20
  122. *
  123. * @param uint32_t drv : 0-3
  124. *
  125. * @return None
  126. */
  127. void gpio_pad_set_drv(uint32_t gpio_num, uint32_t drv);
  128. /**
  129. * @brief Pull up the pad from gpio number.
  130. *
  131. * @param uint32_t gpio_num : gpio number, 0~20
  132. *
  133. * @return None
  134. */
  135. void gpio_pad_pullup(uint32_t gpio_num);
  136. /**
  137. * @brief Pull down the pad from gpio number.
  138. *
  139. * @param uint32_t gpio_num : gpio number, 0~20
  140. *
  141. * @return None
  142. */
  143. void gpio_pad_pulldown(uint32_t gpio_num);
  144. /**
  145. * @brief Unhold the pad from gpio number.
  146. *
  147. * @param uint32_t gpio_num : gpio number, 0~20
  148. *
  149. * @return None
  150. */
  151. void gpio_pad_unhold(uint32_t gpio_num);
  152. /**
  153. * @brief Hold the pad from gpio number.
  154. *
  155. * @param uint32_t gpio_num : gpio number, 0~20
  156. *
  157. * @return None
  158. */
  159. void gpio_pad_hold(uint32_t gpio_num);
  160. /**
  161. * @brief enable gpio pad input.
  162. *
  163. * @param uint32_t gpio_num : gpio number, 0~20
  164. *
  165. * @return None
  166. */
  167. void gpio_pad_input_enable(uint32_t gpio_num);
  168. /**
  169. * @brief disable gpio pad input.
  170. *
  171. * @param uint32_t gpio_num : gpio number, 0~20
  172. *
  173. * @return None
  174. */
  175. void gpio_pad_input_disable(uint32_t gpio_num);
  176. /**
  177. * @}
  178. */
  179. #ifdef __cplusplus
  180. }
  181. #endif