gpio.h 8.5 KB

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