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