]> granicus.if.org Git - esp-idf/blob - components/driver/gpio.c
Merge branch 'bugfix/esp_err_ignore_dirs' into 'master'
[esp-idf] / components / driver / gpio.c
1 // Copyright 2015-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 #include <esp_types.h>
15 #include "esp_err.h"
16 #include "esp_intr.h"
17 #include "esp_intr_alloc.h"
18 #include "freertos/FreeRTOS.h"
19 #include "freertos/xtensa_api.h"
20 #include "driver/gpio.h"
21 #include "driver/rtc_io.h"
22 #include "soc/soc.h"
23 #include "esp_log.h"
24 #include "soc/gpio_periph.h"
25
26 static const char* GPIO_TAG = "gpio";
27 #define GPIO_CHECK(a, str, ret_val) \
28     if (!(a)) { \
29         ESP_LOGE(GPIO_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
30         return (ret_val); \
31     }
32
33 typedef struct {
34     gpio_isr_t fn;   /*!< isr function */
35     void* args;      /*!< isr function args */
36 } gpio_isr_func_t;
37
38 static gpio_isr_func_t* gpio_isr_func = NULL;
39 static gpio_isr_handle_t gpio_isr_handle;
40 static portMUX_TYPE gpio_spinlock = portMUX_INITIALIZER_UNLOCKED;
41
42 esp_err_t gpio_pullup_en(gpio_num_t gpio_num)
43 {
44     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
45     if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
46         rtc_gpio_pullup_en(gpio_num);
47     } else {
48         REG_SET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PU);
49     }
50     return ESP_OK;
51 }
52
53 esp_err_t gpio_pullup_dis(gpio_num_t gpio_num)
54 {
55     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
56     if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
57         rtc_gpio_pullup_dis(gpio_num);
58     } else {
59         REG_CLR_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PU);
60     }
61     return ESP_OK;
62 }
63
64 esp_err_t gpio_pulldown_en(gpio_num_t gpio_num)
65 {
66     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
67     if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
68         rtc_gpio_pulldown_en(gpio_num);
69     } else {
70         REG_SET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PD);
71     }
72     return ESP_OK;
73 }
74
75 esp_err_t gpio_pulldown_dis(gpio_num_t gpio_num)
76 {
77     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
78     if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
79         rtc_gpio_pulldown_dis(gpio_num);
80     } else {
81         REG_CLR_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PD);
82     }
83     return ESP_OK;
84 }
85
86 esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, gpio_int_type_t intr_type)
87 {
88     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
89     GPIO_CHECK(intr_type < GPIO_INTR_MAX, "GPIO interrupt type error", ESP_ERR_INVALID_ARG);
90     GPIO.pin[gpio_num].int_type = intr_type;
91     return ESP_OK;
92 }
93
94 static void gpio_intr_status_clr(gpio_num_t gpio_num)
95 {
96     if (gpio_num < 32) {
97         GPIO.status_w1tc = BIT(gpio_num);
98     } else {
99         GPIO.status1_w1tc.intr_st = BIT(gpio_num - 32);
100     }
101 }
102
103 static esp_err_t gpio_intr_enable_on_core (gpio_num_t gpio_num, uint32_t core_id)
104 {
105     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
106     gpio_intr_status_clr(gpio_num);
107     if (core_id == 0) {
108         GPIO.pin[gpio_num].int_ena = GPIO_PRO_CPU_INTR_ENA;     //enable pro cpu intr
109     } else {
110         GPIO.pin[gpio_num].int_ena = GPIO_APP_CPU_INTR_ENA;     //enable pro cpu intr
111     }
112     return ESP_OK;
113 }
114
115 esp_err_t gpio_intr_enable(gpio_num_t gpio_num)
116 {
117     return gpio_intr_enable_on_core (gpio_num, xPortGetCoreID());
118 }
119
120 esp_err_t gpio_intr_disable(gpio_num_t gpio_num)
121 {
122     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
123     GPIO.pin[gpio_num].int_ena = 0;                             //disable GPIO intr
124     gpio_intr_status_clr(gpio_num);
125     return ESP_OK;
126 }
127
128 static esp_err_t gpio_output_disable(gpio_num_t gpio_num)
129 {
130     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
131     if (gpio_num < 32) {
132         GPIO.enable_w1tc = (0x1 << gpio_num);
133     } else {
134         GPIO.enable1_w1tc.data = (0x1 << (gpio_num - 32));
135     }
136
137     // Ensure no other output signal is routed via GPIO matrix to this pin
138     REG_WRITE(GPIO_FUNC0_OUT_SEL_CFG_REG + (gpio_num * 4),
139               SIG_GPIO_OUT_IDX);
140
141     return ESP_OK;
142 }
143
144 static esp_err_t gpio_output_enable(gpio_num_t gpio_num)
145 {
146     GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
147     if (gpio_num < 32) {
148         GPIO.enable_w1ts = (0x1 << gpio_num);
149     } else {
150         GPIO.enable1_w1ts.data = (0x1 << (gpio_num - 32));
151     }
152     gpio_matrix_out(gpio_num, SIG_GPIO_OUT_IDX, false, false);
153     return ESP_OK;
154 }
155
156 esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level)
157 {
158     GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
159     if (level) {
160         if (gpio_num < 32) {
161             GPIO.out_w1ts = (1 << gpio_num);
162         } else {
163             GPIO.out1_w1ts.data = (1 << (gpio_num - 32));
164         }
165     } else {
166         if (gpio_num < 32) {
167             GPIO.out_w1tc = (1 << gpio_num);
168         } else {
169             GPIO.out1_w1tc.data = (1 << (gpio_num - 32));
170         }
171     }
172     return ESP_OK;
173 }
174
175 int gpio_get_level(gpio_num_t gpio_num)
176 {
177     if (gpio_num < 32) {
178         return (GPIO.in >> gpio_num) & 0x1;
179     } else {
180         return (GPIO.in1.data >> (gpio_num - 32)) & 0x1;
181     }
182 }
183
184 esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
185 {
186     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
187     GPIO_CHECK(pull <= GPIO_FLOATING, "GPIO pull mode error", ESP_ERR_INVALID_ARG);
188     esp_err_t ret = ESP_OK;
189     switch (pull) {
190     case GPIO_PULLUP_ONLY:
191         gpio_pulldown_dis(gpio_num);
192         gpio_pullup_en(gpio_num);
193         break;
194     case GPIO_PULLDOWN_ONLY:
195         gpio_pulldown_en(gpio_num);
196         gpio_pullup_dis(gpio_num);
197         break;
198     case GPIO_PULLUP_PULLDOWN:
199         gpio_pulldown_en(gpio_num);
200         gpio_pullup_en(gpio_num);
201         break;
202     case GPIO_FLOATING:
203         gpio_pulldown_dis(gpio_num);
204         gpio_pullup_dis(gpio_num);
205         break;
206     default:
207         ESP_LOGE(GPIO_TAG, "Unknown pull up/down mode,gpio_num=%u,pull=%u", gpio_num, pull);
208         ret = ESP_ERR_INVALID_ARG;
209         break;
210     }
211     return ret;
212 }
213
214 esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
215 {
216     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
217     if (gpio_num >= 34 && (mode & GPIO_MODE_DEF_OUTPUT)) {
218         ESP_LOGE(GPIO_TAG, "io_num=%d can only be input", gpio_num);
219         return ESP_ERR_INVALID_ARG;
220     }
221     esp_err_t ret = ESP_OK;
222     if (mode & GPIO_MODE_DEF_INPUT) {
223         PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[gpio_num]);
224     } else {
225         PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[gpio_num]);
226     }
227     if (mode & GPIO_MODE_DEF_OUTPUT) {
228         gpio_output_enable(gpio_num);
229     } else {
230         gpio_output_disable(gpio_num);
231     }
232     if (mode & GPIO_MODE_DEF_OD) {
233         GPIO.pin[gpio_num].pad_driver = 1;
234     } else {
235         GPIO.pin[gpio_num].pad_driver = 0;
236     }
237     return ret;
238 }
239
240 esp_err_t gpio_config(const gpio_config_t *pGPIOConfig)
241 {
242     uint64_t gpio_pin_mask = (pGPIOConfig->pin_bit_mask);
243     uint32_t io_reg = 0;
244     uint32_t io_num = 0;
245     uint8_t input_en = 0;
246     uint8_t output_en = 0;
247     uint8_t od_en = 0;
248     uint8_t pu_en = 0;
249     uint8_t pd_en = 0;
250     if (pGPIOConfig->pin_bit_mask == 0 || pGPIOConfig->pin_bit_mask >= (((uint64_t) 1) << GPIO_PIN_COUNT)) {
251         ESP_LOGE(GPIO_TAG, "GPIO_PIN mask error ");
252         return ESP_ERR_INVALID_ARG;
253     }
254     if ((pGPIOConfig->mode) & (GPIO_MODE_DEF_OUTPUT)) {
255         //GPIO 34/35/36/37/38/39 can only be used as input mode;
256         if ((gpio_pin_mask & ( GPIO_SEL_34 | GPIO_SEL_35 | GPIO_SEL_36 | GPIO_SEL_37 | GPIO_SEL_38 | GPIO_SEL_39))) {
257             ESP_LOGE(GPIO_TAG, "GPIO34-39 can only be used as input mode");
258             return ESP_ERR_INVALID_ARG;
259         }
260     }
261     do {
262         io_reg = GPIO_PIN_MUX_REG[io_num];
263         if (((gpio_pin_mask >> io_num) & BIT(0))) {
264             if (!io_reg) {
265                 ESP_LOGE(GPIO_TAG, "IO%d is not a valid GPIO",io_num);
266                 return ESP_ERR_INVALID_ARG;
267             }
268             if(RTC_GPIO_IS_VALID_GPIO(io_num)){
269                 rtc_gpio_deinit(io_num);
270             }
271             if ((pGPIOConfig->mode) & GPIO_MODE_DEF_INPUT) {
272                 input_en = 1;
273                 PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[io_num]);
274             } else {
275                 PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[io_num]);
276             }
277             if ((pGPIOConfig->mode) & GPIO_MODE_DEF_OD) {
278                 od_en = 1;
279                 GPIO.pin[io_num].pad_driver = 1; /*0x01 Open-drain */
280             } else {
281                 GPIO.pin[io_num].pad_driver = 0; /*0x00 Normal gpio output */
282             }
283             if ((pGPIOConfig->mode) & GPIO_MODE_DEF_OUTPUT) {
284                 output_en = 1;
285                 gpio_output_enable(io_num);
286             } else {
287                 gpio_output_disable(io_num);
288             }
289             if (pGPIOConfig->pull_up_en) {
290                 pu_en = 1;
291                 gpio_pullup_en(io_num);
292             } else {
293                 gpio_pullup_dis(io_num);
294             }
295             if (pGPIOConfig->pull_down_en) {
296                 pd_en = 1;
297                 gpio_pulldown_en(io_num);
298             } else {
299                 gpio_pulldown_dis(io_num);
300             }
301             ESP_LOGI(GPIO_TAG, "GPIO[%d]| InputEn: %d| OutputEn: %d| OpenDrain: %d| Pullup: %d| Pulldown: %d| Intr:%d ", io_num, input_en, output_en, od_en, pu_en, pd_en, pGPIOConfig->intr_type);
302             gpio_set_intr_type(io_num, pGPIOConfig->intr_type);
303             if (pGPIOConfig->intr_type) {
304                 gpio_intr_enable(io_num);
305             } else {
306                 gpio_intr_disable(io_num);
307             }
308             PIN_FUNC_SELECT(io_reg, PIN_FUNC_GPIO); /*function number 2 is GPIO_FUNC for each pin */
309         }
310         io_num++;
311     } while (io_num < GPIO_PIN_COUNT);
312     return ESP_OK;
313 }
314
315 esp_err_t gpio_reset_pin(gpio_num_t gpio_num)
316 {
317     assert(gpio_num >= 0 && GPIO_IS_VALID_GPIO(gpio_num));
318     gpio_config_t cfg = {
319         .pin_bit_mask = BIT(gpio_num),
320         .mode = GPIO_MODE_DISABLE,
321         //for powersave reasons, the GPIO should not be floating, select pullup
322         .pull_up_en = true,
323         .pull_down_en = false,
324         .intr_type = GPIO_INTR_DISABLE,
325     };
326     gpio_config(&cfg);
327     return ESP_OK;
328 }
329
330 void IRAM_ATTR gpio_intr_service(void* arg)
331 {
332     //GPIO intr process
333     uint32_t gpio_num = 0;
334     //read status to get interrupt status for GPIO0-31
335     uint32_t gpio_intr_status;
336     gpio_intr_status = GPIO.status;
337     //read status1 to get interrupt status for GPIO32-39
338     uint32_t gpio_intr_status_h;
339     gpio_intr_status_h = GPIO.status1.intr_st;
340
341     if (gpio_isr_func == NULL) {
342         return;
343     }
344     do {
345         if (gpio_num < 32) {
346             if (gpio_intr_status & BIT(gpio_num)) { //gpio0-gpio31
347                 if (gpio_isr_func[gpio_num].fn != NULL) {
348                     gpio_isr_func[gpio_num].fn(gpio_isr_func[gpio_num].args);
349                 }
350                 GPIO.status_w1tc = BIT(gpio_num);
351             }
352         } else {
353             if (gpio_intr_status_h & BIT(gpio_num - 32)) {
354                 if (gpio_isr_func[gpio_num].fn != NULL) {
355                     gpio_isr_func[gpio_num].fn(gpio_isr_func[gpio_num].args);
356                 }
357                 GPIO.status1_w1tc.intr_st = BIT(gpio_num - 32);
358             }
359         }
360     } while (++gpio_num < GPIO_PIN_COUNT);
361 }
362
363 esp_err_t gpio_isr_handler_add(gpio_num_t gpio_num, gpio_isr_t isr_handler, void* args)
364 {
365     GPIO_CHECK(gpio_isr_func != NULL, "GPIO isr service is not installed, call gpio_install_isr_service() first", ESP_ERR_INVALID_STATE);
366     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
367     portENTER_CRITICAL(&gpio_spinlock);
368     gpio_intr_disable(gpio_num);
369     if (gpio_isr_func) {
370         gpio_isr_func[gpio_num].fn = isr_handler;
371         gpio_isr_func[gpio_num].args = args;
372     }
373     gpio_intr_enable_on_core (gpio_num, esp_intr_get_cpu(gpio_isr_handle));
374     portEXIT_CRITICAL(&gpio_spinlock);
375     return ESP_OK;
376 }
377
378 esp_err_t gpio_isr_handler_remove(gpio_num_t gpio_num)
379 {
380     GPIO_CHECK(gpio_isr_func != NULL, "GPIO isr service is not installed, call gpio_install_isr_service() first", ESP_ERR_INVALID_STATE);
381     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
382     portENTER_CRITICAL(&gpio_spinlock);
383     gpio_intr_disable(gpio_num);
384     if (gpio_isr_func) {
385         gpio_isr_func[gpio_num].fn = NULL;
386         gpio_isr_func[gpio_num].args = NULL;
387     }
388     portEXIT_CRITICAL(&gpio_spinlock);
389     return ESP_OK;
390 }
391
392 esp_err_t gpio_install_isr_service(int intr_alloc_flags)
393 {
394     GPIO_CHECK(gpio_isr_func == NULL, "GPIO isr service already installed", ESP_ERR_INVALID_STATE);
395     esp_err_t ret;
396     portENTER_CRITICAL(&gpio_spinlock);
397     gpio_isr_func = (gpio_isr_func_t*) calloc(GPIO_NUM_MAX, sizeof(gpio_isr_func_t));
398     if (gpio_isr_func == NULL) {
399         ret = ESP_ERR_NO_MEM;
400     } else {
401         ret = gpio_isr_register(gpio_intr_service, NULL, intr_alloc_flags, &gpio_isr_handle);
402     }
403     portEXIT_CRITICAL(&gpio_spinlock);
404     return ret;
405 }
406
407 void gpio_uninstall_isr_service()
408 {
409     if (gpio_isr_func == NULL) {
410         return;
411     }
412     portENTER_CRITICAL(&gpio_spinlock);
413     esp_intr_free(gpio_isr_handle);
414     free(gpio_isr_func);
415     gpio_isr_func = NULL;
416     portEXIT_CRITICAL(&gpio_spinlock);
417     return;
418 }
419
420 esp_err_t gpio_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags, gpio_isr_handle_t *handle)
421 {
422     GPIO_CHECK(fn, "GPIO ISR null", ESP_ERR_INVALID_ARG);
423     return esp_intr_alloc(ETS_GPIO_INTR_SOURCE, intr_alloc_flags, fn, arg, handle);
424 }
425
426 /*only level interrupt can be used for wake-up function*/
427 esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
428 {
429     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
430     esp_err_t ret = ESP_OK;
431     if (( intr_type == GPIO_INTR_LOW_LEVEL ) || ( intr_type == GPIO_INTR_HIGH_LEVEL )) {
432         GPIO.pin[gpio_num].int_type = intr_type;
433         GPIO.pin[gpio_num].wakeup_enable = 0x1;
434     } else {
435         ESP_LOGE(GPIO_TAG, "GPIO wakeup only support Level mode,but edge mode set. gpio_num:%u", gpio_num);
436         ret = ESP_ERR_INVALID_ARG;
437     }
438     return ret;
439 }
440
441 esp_err_t gpio_wakeup_disable(gpio_num_t gpio_num)
442 {
443     GPIO_CHECK(GPIO_IS_VALID_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
444     GPIO.pin[gpio_num].wakeup_enable = 0;
445     return ESP_OK;
446 }
447
448 esp_err_t gpio_set_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t strength)
449 {
450     GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
451     GPIO_CHECK(strength < GPIO_DRIVE_CAP_MAX, "GPIO drive capability error", ESP_ERR_INVALID_ARG);
452
453     if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
454         rtc_gpio_set_drive_capability(gpio_num, strength);
455     } else {
456         SET_PERI_REG_BITS(GPIO_PIN_MUX_REG[gpio_num], FUN_DRV_V, strength, FUN_DRV_S);
457     }
458     return ESP_OK;
459 }
460
461 esp_err_t gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t* strength)
462 {
463     GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO number error", ESP_ERR_INVALID_ARG);
464     GPIO_CHECK(strength != NULL, "GPIO drive capability pointer error", ESP_ERR_INVALID_ARG);
465
466     if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
467         return rtc_gpio_get_drive_capability(gpio_num, strength);
468     } else {
469         *strength = GET_PERI_REG_BITS2(GPIO_PIN_MUX_REG[gpio_num], FUN_DRV_V, FUN_DRV_S);
470     }
471     return ESP_OK;
472 }
473
474 static const uint32_t GPIO_HOLD_MASK[34] = {
475     0,
476     GPIO_SEL_1,
477     0,
478     GPIO_SEL_0,
479     0,
480     GPIO_SEL_8,
481     GPIO_SEL_2,
482     GPIO_SEL_3,
483     GPIO_SEL_4,
484     GPIO_SEL_5,
485     GPIO_SEL_6,
486     GPIO_SEL_7,
487     0,
488     0,
489     0,
490     0,
491     GPIO_SEL_9,
492     GPIO_SEL_10,
493     GPIO_SEL_11,
494     GPIO_SEL_12,
495     0,
496     GPIO_SEL_14,
497     GPIO_SEL_15,
498     GPIO_SEL_16,
499     0,
500     0,
501     0,
502     0,
503     0,
504     0,
505     0,
506     0,
507     0,
508     0,
509 };
510
511 esp_err_t gpio_hold_en(gpio_num_t gpio_num)
512 {
513     GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED);
514     esp_err_t r = ESP_OK;
515     if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
516         r = rtc_gpio_hold_en(gpio_num);
517     } else if (GPIO_HOLD_MASK[gpio_num]) {
518         SET_PERI_REG_MASK(RTC_IO_DIG_PAD_HOLD_REG, GPIO_HOLD_MASK[gpio_num]);
519     } else {
520         r = ESP_ERR_NOT_SUPPORTED;
521     }
522     return r == ESP_OK ? ESP_OK : ESP_ERR_NOT_SUPPORTED;
523 }
524
525 esp_err_t gpio_hold_dis(gpio_num_t gpio_num)
526 {
527     GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "Only output-capable GPIO support this function", ESP_ERR_NOT_SUPPORTED);
528     esp_err_t r = ESP_OK;
529     if (RTC_GPIO_IS_VALID_GPIO(gpio_num)) {
530         r = rtc_gpio_hold_dis(gpio_num);
531     } else if (GPIO_HOLD_MASK[gpio_num]) {
532         CLEAR_PERI_REG_MASK(RTC_IO_DIG_PAD_HOLD_REG, GPIO_HOLD_MASK[gpio_num]);
533     } else {
534         r = ESP_ERR_NOT_SUPPORTED;
535     }
536     return r == ESP_OK ? ESP_OK : ESP_ERR_NOT_SUPPORTED;
537 }
538
539 void gpio_iomux_in(uint32_t gpio, uint32_t signal_idx)
540 {
541     GPIO.func_in_sel_cfg[signal_idx].sig_in_sel = 0;
542     PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[gpio]);
543 }
544
545 void gpio_iomux_out(uint8_t gpio_num, int func, bool oen_inv)
546 {
547     GPIO.func_out_sel_cfg[gpio_num].oen_sel = 0;
548     GPIO.func_out_sel_cfg[gpio_num].oen_inv_sel = oen_inv;
549     PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], func);
550 }