gpio.h 8.7 KB

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