1 /*
2  * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include "soc/gpio_reg.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /** \defgroup gpio_apis, uart configuration and communication related apis
18   * @brief gpio apis
19   */
20 
21 /** @addtogroup gpio_apis
22   * @{
23   */
24 
25 #define GPIO_REG_READ(reg)              READ_PERI_REG(reg)
26 #define GPIO_REG_WRITE(reg, val)        WRITE_PERI_REG(reg, val)
27 #define GPIO_ID_PIN0                    0
28 #define GPIO_ID_PIN(n)                  (GPIO_ID_PIN0+(n))
29 #define GPIO_PIN_ADDR(i)                (GPIO_PIN0_REG + i*4)
30 
31 #define GPIO_FUNC_IN_HIGH               0x38
32 #define GPIO_FUNC_IN_LOW                0x3C
33 
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 
37 #define GPIO_REGID_TO_PINIDX(reg_id) ((reg_id) - GPIO_ID_PIN0)
38 
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 
48 
49 /**
50   * @brief Change GPIO(0-31) pin output by setting, clearing, or disabling pins, GPIO0<->BIT(0).
51   *         There is no particular ordering guaranteed; so if the order of writes is significant,
52   *         calling code should divide a single call into multiple calls.
53   *
54   * @param  uint32_t set_mask : the gpios that need high level.
55   *
56   * @param  uint32_t clear_mask : the gpios that need low level.
57   *
58   * @param  uint32_t enable_mask : the gpios that need be changed.
59   *
60   * @param  uint32_t disable_mask : the gpios that need diable output.
61   *
62   * @return None
63   */
64 void gpio_output_set(uint32_t set_mask, uint32_t clear_mask, uint32_t enable_mask, uint32_t disable_mask);
65 
66 
67 /**
68   * @brief Sample the value of GPIO input pins(0-31) and returns a bitmask.
69   *
70   * @param None
71   *
72   * @return uint32_t : bitmask for GPIO input pins, BIT(0) for GPIO0.
73   */
74 uint32_t gpio_input_get(void);
75 
76 /**
77   * @brief Set GPIO to wakeup the ESP32.
78   *        Please do not call this function in SDK.
79   *
80   * @param uint32_t i: gpio number.
81   *
82   * @param GPIO_INT_TYPE intr_state : only GPIO_PIN_INTR_LOLEVEL\GPIO_PIN_INTR_HILEVEL can be used
83   *
84   * @return None
85   */
86 void gpio_pin_wakeup_enable(uint32_t i, GPIO_INT_TYPE intr_state);
87 
88 /**
89   * @brief disable GPIOs to wakeup the ESP32.
90   *        Please do not call this function in SDK.
91   *
92   * @param None
93   *
94   * @return None
95   */
96 void gpio_pin_wakeup_disable(void);
97 
98 /**
99   * @brief set gpio input to a signal, one gpio can input to several signals.
100   *
101   * @param uint32_t gpio : gpio number, 0~0x2f
102   *                        gpio == 0x3C, input 0 to signal
103   *                        gpio == 0x3A, input nothing to signal
104   *                        gpio == 0x38, input 1 to signal
105   *
106   * @param uint32_t signal_idx : signal index.
107   *
108   * @param bool inv : the signal is inv or not
109   *
110   * @return None
111   */
112 void gpio_matrix_in(uint32_t gpio, uint32_t signal_idx, bool inv);
113 
114 /**
115   * @brief set signal output to gpio, one signal can output to several gpios.
116   *
117   * @param uint32_t gpio : gpio number, 0~0x2f
118   *
119   * @param uint32_t signal_idx : signal index.
120   *                        signal_idx == 0x100, cancel output put to the gpio
121   *
122   * @param bool out_inv : the signal output is invert or not
123   *
124   * @param bool oen_inv : the signal output enable is invert or not
125   *
126   * @return None
127   */
128 void gpio_matrix_out(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv);
129 
130 /**
131   * @brief Select pad as a gpio function from IOMUX.
132   *
133   * @param uint32_t gpio_num : gpio number, 0~0x2f
134   *
135   * @return None
136   */
137 void gpio_pad_select_gpio(uint32_t gpio_num);
138 
139 /**
140   * @brief Set pad driver capability.
141   *
142   * @param uint32_t gpio_num : gpio number, 0~0x2f
143   *
144   * @param uint32_t drv : 0-3
145   *
146   * @return None
147   */
148 void gpio_pad_set_drv(uint32_t gpio_num, uint32_t drv);
149 
150 /**
151   * @brief Pull up the pad from gpio number.
152   *
153   * @param uint32_t gpio_num : gpio number, 0~0x2f
154   *
155   * @return None
156   */
157 void gpio_pad_pullup(uint32_t gpio_num);
158 
159 /**
160   * @brief Pull down the pad from gpio number.
161   *
162   * @param uint32_t gpio_num : gpio number, 0~0x2f
163   *
164   * @return None
165   */
166 void gpio_pad_pulldown(uint32_t gpio_num);
167 
168 /**
169   * @brief Unhold the pad from gpio number.
170   *
171   * @param uint32_t gpio_num : gpio number, 0~0x2f
172   *
173   * @return None
174   */
175 void gpio_pad_unhold(uint32_t gpio_num);
176 
177 /**
178   * @brief Hold the pad from gpio number.
179   *
180   * @param uint32_t gpio_num : gpio number, 0~0x2f
181   *
182   * @return None
183   */
184 void gpio_pad_hold(uint32_t gpio_num);
185 
186 /**
187   * @brief enable gpio pad input.
188   *
189   * @param uint32_t gpio_num : gpio number, 0~0x2f
190   *
191   * @return None
192   */
193 void gpio_pad_input_enable(uint32_t gpio_num);
194 
195 /**
196   * @brief disable gpio pad input.
197   *
198   * @param uint32_t gpio_num : gpio number, 0~0x2f
199   *
200   * @return None
201   */
202 void gpio_pad_input_disable(uint32_t gpio_num);
203 
204 /**
205   * @}
206   */
207 
208 #ifdef __cplusplus
209 }
210 #endif
211