1 /*
2  * SPDX-FileCopyrightText: 2020-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   * @brief Sample the value of GPIO input pins(0-31) and returns a bitmask.
68   *
69   * @param None
70   *
71   * @return uint32_t : bitmask for GPIO input pins, BIT(0) for GPIO0.
72   */
73 uint32_t gpio_input_get(void);
74 
75 /**
76   * @brief Set GPIO to wakeup the ESP32.
77   *        Please do not call this function in SDK.
78   *
79   * @param uint32_t i: gpio number.
80   *
81   * @param GPIO_INT_TYPE intr_state : only GPIO_PIN_INTR_LOLEVEL\GPIO_PIN_INTR_HILEVEL can be used
82   *
83   * @return None
84   */
85 void gpio_pin_wakeup_enable(uint32_t i, GPIO_INT_TYPE intr_state);
86 
87 /**
88   * @brief disable GPIOs to wakeup the ESP32.
89   *        Please do not call this function in SDK.
90   *
91   * @param None
92   *
93   * @return None
94   */
95 void gpio_pin_wakeup_disable(void);
96 
97 /**
98   * @brief set gpio input to a signal, one gpio can input to several signals.
99   *
100   * @param uint32_t gpio : gpio number, 0~0x2f
101   *                        gpio == 0x3C, input 0 to signal
102   *                        gpio == 0x3A, input nothing to signal
103   *                        gpio == 0x38, input 1 to signal
104   *
105   * @param uint32_t signal_idx : signal index.
106   *
107   * @param bool inv : the signal is inv or not
108   *
109   * @return None
110   */
111 void gpio_matrix_in(uint32_t gpio, uint32_t signal_idx, bool inv);
112 
113 /**
114   * @brief set signal output to gpio, one signal can output to several gpios.
115   *
116   * @param uint32_t gpio : gpio number, 0~0x2f
117   *
118   * @param uint32_t signal_idx : signal index.
119   *                        signal_idx == 0x100, cancel output put to the gpio
120   *
121   * @param bool out_inv : the signal output is invert or not
122   *
123   * @param bool oen_inv : the signal output enable is invert or not
124   *
125   * @return None
126   */
127 void gpio_matrix_out(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv);
128 
129 /**
130   * @brief Select pad as a gpio function from IOMUX.
131   *
132   * @param uint32_t gpio_num : gpio number, 0~0x2f
133   *
134   * @return None
135   */
136 void gpio_pad_select_gpio(uint32_t gpio_num);
137 
138 /**
139   * @brief Set pad driver capability.
140   *
141   * @param uint32_t gpio_num : gpio number, 0~0x2f
142   *
143   * @param uint32_t drv : 0-3
144   *
145   * @return None
146   */
147 void gpio_pad_set_drv(uint32_t gpio_num, uint32_t drv);
148 
149 /**
150   * @brief Pull up the pad from gpio number.
151   *
152   * @param uint32_t gpio_num : gpio number, 0~0x2f
153   *
154   * @return None
155   */
156 void gpio_pad_pullup(uint32_t gpio_num);
157 
158 /**
159   * @brief Pull down the pad from gpio number.
160   *
161   * @param uint32_t gpio_num : gpio number, 0~0x2f
162   *
163   * @return None
164   */
165 void gpio_pad_pulldown(uint32_t gpio_num);
166 
167 /**
168   * @brief Unhold the pad from gpio number.
169   *
170   * @param uint32_t gpio_num : gpio number, 0~0x2f
171   *
172   * @return None
173   */
174 void gpio_pad_unhold(uint32_t gpio_num);
175 
176 /**
177   * @brief Hold the pad from gpio number.
178   *
179   * @param uint32_t gpio_num : gpio number, 0~0x2f
180   *
181   * @return None
182   */
183 void gpio_pad_hold(uint32_t gpio_num);
184 
185 /**
186   * @brief enable gpio pad input.
187   *
188   * @param uint32_t gpio_num : gpio number, 0~0x2f
189   *
190   * @return None
191   */
192 void gpio_pad_input_enable(uint32_t gpio_num);
193 
194 /**
195   * @brief disable gpio pad input.
196   *
197   * @param uint32_t gpio_num : gpio number, 0~0x2f
198   *
199   * @return None
200   */
201 void gpio_pad_input_disable(uint32_t gpio_num);
202 
203 /**
204   * @}
205   */
206 
207 #ifdef __cplusplus
208 }
209 #endif
210