gpio.h 8.8 KB

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