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 #define GPIO_OUTPUT_SET(gpio_no, bit_value) \
49         ((gpio_no < 32) ? gpio_output_set(bit_value<<gpio_no, (bit_value ? 0 : 1)<<gpio_no, 1<<gpio_no,0) : \
50                          gpio_output_set_high(bit_value<<(gpio_no - 32), (bit_value ? 0 : 1)<<(gpio_no - 32), 1<<(gpio_no -32),0))
51 #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)))
52 #define GPIO_INPUT_GET(gpio_no)     ((gpio_no < 32) ? ((gpio_input_get()>>gpio_no)&BIT0) : ((gpio_input_get_high()>>(gpio_no - 32))&BIT0))
53 
54 /**
55   * @brief Change GPIO(0-31) pin output by setting, clearing, or disabling pins, GPIO0<->BIT(0).
56   *         There is no particular ordering guaranteed; so if the order of writes is significant,
57   *         calling code should divide a single call into multiple calls.
58   *
59   * @param  uint32_t set_mask : the gpios that need high level.
60   *
61   * @param  uint32_t clear_mask : the gpios that need low level.
62   *
63   * @param  uint32_t enable_mask : the gpios that need be changed.
64   *
65   * @param  uint32_t disable_mask : the gpios that need diable output.
66   *
67   * @return None
68   */
69 void gpio_output_set(uint32_t set_mask, uint32_t clear_mask, uint32_t enable_mask, uint32_t disable_mask);
70 
71 /**
72   * @brief Change GPIO(32-39) pin output by setting, clearing, or disabling pins, GPIO32<->BIT(0).
73   *         There is no particular ordering guaranteed; so if the order of writes is significant,
74   *         calling code should divide a single call into multiple calls.
75   *
76   * @param  uint32_t set_mask : the gpios that need high level.
77   *
78   * @param  uint32_t clear_mask : the gpios that need low level.
79   *
80   * @param  uint32_t enable_mask : the gpios that need be changed.
81   *
82   * @param  uint32_t disable_mask : the gpios that need diable output.
83   *
84   * @return None
85   */
86 void gpio_output_set_high(uint32_t set_mask, uint32_t clear_mask, uint32_t enable_mask, uint32_t disable_mask);
87 
88 /**
89   * @brief Sample the value of GPIO input pins(0-31) and returns a bitmask.
90   *
91   * @param None
92   *
93   * @return uint32_t : bitmask for GPIO input pins, BIT(0) for GPIO0.
94   */
95 uint32_t gpio_input_get(void);
96 
97 /**
98   * @brief Sample the value of GPIO input pins(32-39) and returns a bitmask.
99   *
100   * @param None
101   *
102   * @return uint32_t : bitmask for GPIO input pins, BIT(0) for GPIO32.
103   */
104 uint32_t gpio_input_get_high(void);
105 
106 /**
107   * @brief Set GPIO to wakeup the ESP32.
108   *        Please do not call this function in SDK.
109   *
110   * @param uint32_t i: gpio number.
111   *
112   * @param GPIO_INT_TYPE intr_state : only GPIO_PIN_INTR_LOLEVEL\GPIO_PIN_INTR_HILEVEL can be used
113   *
114   * @return None
115   */
116 void gpio_pin_wakeup_enable(uint32_t i, GPIO_INT_TYPE intr_state);
117 
118 /**
119   * @brief disable GPIOs to wakeup the ESP32.
120   *        Please do not call this function in SDK.
121   *
122   * @param None
123   *
124   * @return None
125   */
126 void gpio_pin_wakeup_disable(void);
127 
128 /**
129   * @brief set gpio input to a signal, one gpio can input to several signals.
130   *
131   * @param uint32_t gpio : gpio number, 0~0x2f
132   *                        gpio == 0x3C, input 0 to signal
133   *                        gpio == 0x3A, input nothing to signal
134   *                        gpio == 0x38, input 1 to signal
135   *
136   * @param uint32_t signal_idx : signal index.
137   *
138   * @param bool inv : the signal is inv or not
139   *
140   * @return None
141   */
142 void gpio_matrix_in(uint32_t gpio, uint32_t signal_idx, bool inv);
143 
144 /**
145   * @brief set signal output to gpio, one signal can output to several gpios.
146   *
147   * @param uint32_t gpio : gpio number, 0~0x2f
148   *
149   * @param uint32_t signal_idx : signal index.
150   *                        signal_idx == 0x100, cancel output put to the gpio
151   *
152   * @param bool out_inv : the signal output is invert or not
153   *
154   * @param bool oen_inv : the signal output enable is invert or not
155   *
156   * @return None
157   */
158 void gpio_matrix_out(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv);
159 
160 /**
161   * @brief Select pad as a gpio function from IOMUX.
162   *
163   * @param uint32_t gpio_num : gpio number, 0~0x2f
164   *
165   * @return None
166   */
167 void gpio_pad_select_gpio(uint32_t gpio_num);
168 
169 /**
170   * @brief Set pad driver capability.
171   *
172   * @param uint32_t gpio_num : gpio number, 0~0x2f
173   *
174   * @param uint32_t drv : 0-3
175   *
176   * @return None
177   */
178 void gpio_pad_set_drv(uint32_t gpio_num, uint32_t drv);
179 
180 /**
181   * @brief Pull up the pad from gpio number.
182   *
183   * @param uint32_t gpio_num : gpio number, 0~0x2f
184   *
185   * @return None
186   */
187 void gpio_pad_pullup(uint32_t gpio_num);
188 
189 /**
190   * @brief Pull down the pad from gpio number.
191   *
192   * @param uint32_t gpio_num : gpio number, 0~0x2f
193   *
194   * @return None
195   */
196 void gpio_pad_pulldown(uint32_t gpio_num);
197 
198 /**
199   * @brief Unhold the pad from gpio number.
200   *
201   * @param uint32_t gpio_num : gpio number, 0~0x2f
202   *
203   * @return None
204   */
205 void gpio_pad_unhold(uint32_t gpio_num);
206 
207 /**
208   * @brief Hold the pad from gpio number.
209   *
210   * @param uint32_t gpio_num : gpio number, 0~0x2f
211   *
212   * @return None
213   */
214 void gpio_pad_hold(uint32_t gpio_num);
215 
216 /**
217   * @brief enable gpio pad input.
218   *
219   * @param uint32_t gpio_num : gpio number, 0~0x2f
220   *
221   * @return None
222   */
223 void gpio_pad_input_enable(uint32_t gpio_num);
224 
225 /**
226   * @brief disable gpio pad input.
227   *
228   * @param uint32_t gpio_num : gpio number, 0~0x2f
229   *
230   * @return None
231   */
232 void gpio_pad_input_disable(uint32_t gpio_num);
233 
234 /**
235   * @}
236   */
237 
238 #ifdef __cplusplus
239 }
240 #endif
241