gpio.h 9.0 KB

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