gpio.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2022 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 0x38
  25. #define GPIO_FUNC_IN_LOW 0x30
  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. #define GPIO_OUTPUT_SET(gpio_no, bit_value) \
  38. ((gpio_no < 32) ? gpio_output_set(bit_value<<gpio_no, (bit_value ? 0 : 1)<<gpio_no, 1<<gpio_no,0) : \
  39. gpio_output_set_high(bit_value<<(gpio_no - 32), (bit_value ? 0 : 1)<<(gpio_no - 32), 1<<(gpio_no -32),0))
  40. #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)))
  41. #define GPIO_INPUT_GET(gpio_no) ((gpio_no < 32) ? ((gpio_input_get()>>gpio_no)&BIT0) : ((gpio_input_get_high()>>(gpio_no - 32))&BIT0))
  42. /**
  43. * @brief Change GPIO(0-31) pin output by setting, clearing, or disabling pins, GPIO0<->BIT(0).
  44. * There is no particular ordering guaranteed; so if the order of writes is significant,
  45. * calling code should divide a single call into multiple calls.
  46. *
  47. * @param uint32_t set_mask : the gpios that need high level.
  48. *
  49. * @param uint32_t clear_mask : the gpios that need low level.
  50. *
  51. * @param uint32_t enable_mask : the gpios that need be changed.
  52. *
  53. * @param uint32_t disable_mask : the gpios that need diable output.
  54. *
  55. * @return None
  56. */
  57. void gpio_output_set(uint32_t set_mask, uint32_t clear_mask, uint32_t enable_mask, uint32_t disable_mask);
  58. /**
  59. * @brief Change GPIO(32-39) pin output by setting, clearing, or disabling pins, GPIO32<->BIT(0).
  60. * There is no particular ordering guaranteed; so if the order of writes is significant,
  61. * calling code should divide a single call into multiple calls.
  62. *
  63. * @param uint32_t set_mask : the gpios that need high level.
  64. *
  65. * @param uint32_t clear_mask : the gpios that need low level.
  66. *
  67. * @param uint32_t enable_mask : the gpios that need be changed.
  68. *
  69. * @param uint32_t disable_mask : the gpios that need diable output.
  70. *
  71. * @return None
  72. */
  73. void gpio_output_set_high(uint32_t set_mask, uint32_t clear_mask, uint32_t enable_mask, uint32_t disable_mask);
  74. /**
  75. * @brief Sample the value of GPIO input pins(0-31) and returns a bitmask.
  76. *
  77. * @param None
  78. *
  79. * @return uint32_t : bitmask for GPIO input pins, BIT(0) for GPIO0.
  80. */
  81. uint32_t gpio_input_get(void);
  82. /**
  83. * @brief Sample the value of GPIO input pins(32-39) and returns a bitmask.
  84. *
  85. * @param None
  86. *
  87. * @return uint32_t : bitmask for GPIO input pins, BIT(0) for GPIO32.
  88. */
  89. uint32_t gpio_input_get_high(void);
  90. /**
  91. * @brief Set GPIO to wakeup the ESP32.
  92. * Please do not call this function in SDK.
  93. *
  94. * @param uint32_t i: gpio number.
  95. *
  96. * @param GPIO_INT_TYPE intr_state : only GPIO_PIN_INTR_LOLEVEL\GPIO_PIN_INTR_HILEVEL can be used
  97. *
  98. * @return None
  99. */
  100. void gpio_pin_wakeup_enable(uint32_t i, GPIO_INT_TYPE intr_state);
  101. /**
  102. * @brief disable GPIOs to wakeup the ESP32.
  103. * Please do not call this function in SDK.
  104. *
  105. * @param None
  106. *
  107. * @return None
  108. */
  109. void gpio_pin_wakeup_disable(void);
  110. /**
  111. * @brief set gpio input to a signal, one gpio can input to several signals.
  112. *
  113. * @param uint32_t gpio : gpio number, 0~0x27
  114. * gpio == 0x30, input 0 to signal
  115. * gpio == 0x34, ???
  116. * gpio == 0x38, input 1 to signal
  117. *
  118. * @param uint32_t signal_idx : signal index.
  119. *
  120. * @param bool inv : the signal is inv or not
  121. *
  122. * @return None
  123. */
  124. void gpio_matrix_in(uint32_t gpio, uint32_t signal_idx, bool inv);
  125. /**
  126. * @brief set signal output to gpio, one signal can output to several gpios.
  127. *
  128. * @param uint32_t gpio : gpio number, 0~0x27
  129. *
  130. * @param uint32_t signal_idx : signal index.
  131. * signal_idx == 0x100, cancel output put to the gpio
  132. *
  133. * @param bool out_inv : the signal output is inv or not
  134. *
  135. * @param bool oen_inv : the signal output enable is inv or not
  136. *
  137. * @return None
  138. */
  139. void gpio_matrix_out(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv);
  140. /**
  141. * @brief Select pad as a gpio function from IOMUX.
  142. *
  143. * @param uint32_t gpio_num : gpio number, 0~0x27
  144. *
  145. * @return None
  146. */
  147. void gpio_pad_select_gpio(uint8_t gpio_num);
  148. /**
  149. * @brief Set pad driver capability.
  150. *
  151. * @param uint32_t gpio_num : gpio number, 0~0x27
  152. *
  153. * @param uint8_t drv : 0-3
  154. *
  155. * @return None
  156. */
  157. void gpio_pad_set_drv(uint8_t gpio_num, uint8_t drv);
  158. /**
  159. * @brief Pull up the pad from gpio number.
  160. *
  161. * @param uint32_t gpio_num : gpio number, 0~0x27
  162. *
  163. * @return None
  164. */
  165. void gpio_pad_pullup(uint8_t gpio_num);
  166. /**
  167. * @brief Pull down the pad from gpio number.
  168. *
  169. * @param uint32_t gpio_num : gpio number, 0~0x27
  170. *
  171. * @return None
  172. */
  173. void gpio_pad_pulldown(uint8_t gpio_num);
  174. /**
  175. * @brief Unhold the pad from gpio number.
  176. *
  177. * @param uint32_t gpio_num : gpio number, 0~0x27
  178. *
  179. * @return None
  180. */
  181. void gpio_pad_unhold(uint8_t gpio_num);
  182. /**
  183. * @brief Hold the pad from gpio number.
  184. *
  185. * @param uint32_t gpio_num : gpio number, 0~0x27
  186. *
  187. * @return None
  188. */
  189. void gpio_pad_hold(uint8_t gpio_num);
  190. /**
  191. * @}
  192. */
  193. #ifdef __cplusplus
  194. }
  195. #endif