GPIO & RTC GPIO¶
概述¶
The ESP32 chip features 40 physical GPIO pads. Some GPIO pads cannot be used or do not have the corresponding pin on the chip package(refer to technical reference manual). Each pad can be used as a general purpose I/O or can be connected to an internal peripheral signal.
- Note that GPIO6-11 are usually used for SPI flash.
- GPIO34-39 can only be set as input mode and do not have software pullup or pulldown functions.
There is also separate “RTC GPIO” support, which functions when GPIOs are routed to the “RTC” low-power and analog subsystem. These pin functions can be used when in deep sleep, when the Ultra Low Power co-processor is running, or when analog functions such as ADC/DAC/etc are in use.
应用程序示例¶
GPIO output and input interrupt example: peripherals/gpio.
API 参考手册¶
宏¶
Normal GPIO¶
-
GPIO_SEL_0
¶ Pin 0 selected
-
GPIO_SEL_1
¶ Pin 1 selected
-
GPIO_SEL_2
¶ Pin 2 selected
-
GPIO_SEL_3
¶ Pin 3 selected
-
GPIO_SEL_4
¶ Pin 4 selected
-
GPIO_SEL_5
¶ Pin 5 selected
-
GPIO_SEL_6
¶ Pin 6 selected
-
GPIO_SEL_7
¶ Pin 7 selected
-
GPIO_SEL_8
¶ Pin 8 selected
-
GPIO_SEL_9
¶ Pin 9 selected
-
GPIO_SEL_10
¶ Pin 10 selected
-
GPIO_SEL_11
¶ Pin 11 selected
-
GPIO_SEL_12
¶ Pin 12 selected
-
GPIO_SEL_13
¶ Pin 13 selected
-
GPIO_SEL_14
¶ Pin 14 selected
-
GPIO_SEL_15
¶ Pin 15 selected
-
GPIO_SEL_16
¶ Pin 16 selected
-
GPIO_SEL_17
¶ Pin 17 selected
-
GPIO_SEL_18
¶ Pin 18 selected
-
GPIO_SEL_19
¶ Pin 19 selected
-
GPIO_SEL_21
¶ Pin 21 selected
-
GPIO_SEL_22
¶ Pin 22 selected
-
GPIO_SEL_23
¶ Pin 23 selected
-
GPIO_SEL_25
¶ Pin 25 selected
-
GPIO_SEL_26
¶ Pin 26 selected
-
GPIO_SEL_27
¶ Pin 27 selected
-
GPIO_SEL_32
¶ Pin 32 selected
-
GPIO_SEL_33
¶ Pin 33 selected
-
GPIO_SEL_34
¶ Pin 34 selected
-
GPIO_SEL_35
¶ Pin 35 selected
-
GPIO_SEL_36
¶ Pin 36 selected
-
GPIO_SEL_37
¶ Pin 37 selected
-
GPIO_SEL_38
¶ Pin 38 selected
-
GPIO_SEL_39
¶ Pin 39 selected
-
GPIO_PIN_REG_0
¶
-
GPIO_PIN_REG_1
¶
-
GPIO_PIN_REG_2
¶
-
GPIO_PIN_REG_3
¶
-
GPIO_PIN_REG_4
¶
-
GPIO_PIN_REG_5
¶
-
GPIO_PIN_REG_6
¶
-
GPIO_PIN_REG_7
¶
-
GPIO_PIN_REG_8
¶
-
GPIO_PIN_REG_9
¶
-
GPIO_PIN_REG_10
¶
-
GPIO_PIN_REG_11
¶
-
GPIO_PIN_REG_12
¶
-
GPIO_PIN_REG_13
¶
-
GPIO_PIN_REG_14
¶
-
GPIO_PIN_REG_15
¶
-
GPIO_PIN_REG_16
¶
-
GPIO_PIN_REG_17
¶
-
GPIO_PIN_REG_18
¶
-
GPIO_PIN_REG_19
¶
-
GPIO_PIN_REG_20
¶
-
GPIO_PIN_REG_21
¶
-
GPIO_PIN_REG_22
¶
-
GPIO_PIN_REG_23
¶
-
GPIO_PIN_REG_25
¶
-
GPIO_PIN_REG_26
¶
-
GPIO_PIN_REG_27
¶
-
GPIO_PIN_REG_32
¶
-
GPIO_PIN_REG_33
¶
-
GPIO_PIN_REG_34
¶
-
GPIO_PIN_REG_35
¶
-
GPIO_PIN_REG_36
¶
-
GPIO_PIN_REG_37
¶
-
GPIO_PIN_REG_38
¶
-
GPIO_PIN_REG_39
¶
-
GPIO_APP_CPU_INTR_ENA
¶
-
GPIO_APP_CPU_NMI_INTR_ENA
¶
-
GPIO_PRO_CPU_INTR_ENA
¶
-
GPIO_PRO_CPU_NMI_INTR_ENA
¶
-
GPIO_SDIO_EXT_INTR_ENA
¶
-
GPIO_MODE_DEF_INPUT
¶
-
GPIO_MODE_DEF_OUTPUT
¶
-
GPIO_MODE_DEF_OD
¶
-
GPIO_PIN_COUNT
¶
-
GPIO_IS_VALID_GPIO
(gpio_num)¶
-
GPIO_IS_VALID_OUTPUT_GPIO
(gpio_num)¶
枚举¶
Normal GPIO¶
-
enum
gpio_num_t
¶ Values:
-
GPIO_NUM_0
= 0¶ GPIO0, input and output
-
GPIO_NUM_1
= 1¶ GPIO1, input and output
-
GPIO_NUM_2
= 2¶ GPIO2, input and output
-
GPIO_NUM_3
= 3¶ GPIO3, input and output
-
GPIO_NUM_4
= 4¶ GPIO4, input and output
-
GPIO_NUM_5
= 5¶ GPIO5, input and output
-
GPIO_NUM_6
= 6¶ GPIO6, input and output
-
GPIO_NUM_7
= 7¶ GPIO7, input and output
-
GPIO_NUM_8
= 8¶ GPIO8, input and output
-
GPIO_NUM_9
= 9¶ GPIO9, input and output
-
GPIO_NUM_10
= 10¶ GPIO10, input and output
-
GPIO_NUM_11
= 11¶ GPIO11, input and output
-
GPIO_NUM_12
= 12¶ GPIO12, input and output
-
GPIO_NUM_13
= 13¶ GPIO13, input and output
-
GPIO_NUM_14
= 14¶ GPIO14, input and output
-
GPIO_NUM_15
= 15¶ GPIO15, input and output
-
GPIO_NUM_16
= 16¶ GPIO16, input and output
-
GPIO_NUM_17
= 17¶ GPIO17, input and output
-
GPIO_NUM_18
= 18¶ GPIO18, input and output
-
GPIO_NUM_19
= 19¶ GPIO19, input and output
-
GPIO_NUM_21
= 21¶ GPIO21, input and output
-
GPIO_NUM_22
= 22¶ GPIO22, input and output
-
GPIO_NUM_23
= 23¶ GPIO23, input and output
-
GPIO_NUM_25
= 25¶ GPIO25, input and output
-
GPIO_NUM_26
= 26¶ GPIO26, input and output
-
GPIO_NUM_27
= 27¶ GPIO27, input and output
-
GPIO_NUM_32
= 32¶ GPIO32, input and output
-
GPIO_NUM_33
= 33¶ GPIO32, input and output
-
GPIO_NUM_34
= 34¶ GPIO34, input mode only
-
GPIO_NUM_35
= 35¶ GPIO35, input mode only
-
GPIO_NUM_36
= 36¶ GPIO36, input mode only
-
GPIO_NUM_37
= 37¶ GPIO37, input mode only
-
GPIO_NUM_38
= 38¶ GPIO38, input mode only
-
GPIO_NUM_39
= 39¶ GPIO39, input mode only
-
GPIO_NUM_MAX
= 40¶
-
-
enum
gpio_int_type_t
¶ Values:
-
GPIO_INTR_DISABLE
= 0¶ Disable GPIO interrupt
-
GPIO_INTR_POSEDGE
= 1¶ GPIO interrupt type : rising edge
-
GPIO_INTR_NEGEDGE
= 2¶ GPIO interrupt type : falling edge
-
GPIO_INTR_ANYEDGE
= 3¶ GPIO interrupt type : both rising and falling edge
-
GPIO_INTR_LOW_LEVEL
= 4¶ GPIO interrupt type : input low level trigger
-
GPIO_INTR_HIGH_LEVEL
= 5¶ GPIO interrupt type : input high level trigger
-
GPIO_INTR_MAX
¶
-
-
enum
gpio_mode_t
¶ Values:
-
GPIO_MODE_INPUT
= GPIO_MODE_DEF_INPUT¶ GPIO mode : input only
-
GPIO_MODE_OUTPUT
= GPIO_MODE_DEF_OUTPUT¶ GPIO mode : output only mode
-
GPIO_MODE_OUTPUT_OD
= ((GPIO_MODE_DEF_OUTPUT)|(GPIO_MODE_DEF_OD))¶ GPIO mode : output only with open-drain mode
-
GPIO_MODE_INPUT_OUTPUT_OD
= ((GPIO_MODE_DEF_INPUT)|(GPIO_MODE_DEF_OUTPUT)|(GPIO_MODE_DEF_OD))¶ GPIO mode : output and input with open-drain mode
-
GPIO_MODE_INPUT_OUTPUT
= ((GPIO_MODE_DEF_INPUT)|(GPIO_MODE_DEF_OUTPUT))¶ GPIO mode : output and input mode
-
-
enum
gpio_pullup_t
¶ Values:
-
GPIO_PULLUP_DISABLE
= 0x0¶ Disable GPIO pull-up resistor
-
GPIO_PULLUP_ENABLE
= 0x1¶ Enable GPIO pull-up resistor
-
结构体¶
Normal GPIO¶
-
struct
gpio_config_t
¶ Configuration parameters of GPIO pad for gpio_config function.
Public Members
-
uint64_t
pin_bit_mask
¶ GPIO pin: set with bit mask, each bit maps to a GPIO
-
gpio_mode_t
mode
¶ GPIO mode: set input/output mode
-
gpio_pullup_t
pull_up_en
¶ GPIO pull-up
-
gpio_pulldown_t
pull_down_en
¶ GPIO pull-down
-
gpio_int_type_t
intr_type
¶ GPIO interrupt type
-
uint64_t
函数¶
Normal GPIO¶
-
esp_err_t
gpio_config
(const gpio_config_t *pGPIOConfig)¶ GPIO common configuration.
Configure GPIO’s Mode,pull-up,PullDown,IntrType
- Return
- ESP_OK success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
pGPIOConfig
: Pointer to GPIO configure struct
-
esp_err_t
gpio_set_intr_type
(gpio_num_t gpio_num, gpio_int_type_t intr_type)¶ GPIO set interrupt trigger type.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
gpio_num
: GPIO number. If you want to set the trigger type of e.g. of GPIO16, gpio_num should be GPIO_NUM_16 (16);intr_type
: Interrupt type, select from gpio_int_type_t
-
esp_err_t
gpio_intr_enable
(gpio_num_t gpio_num)¶ Enable GPIO module interrupt signal.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
gpio_num
: GPIO number. If you want to enable an interrupt on e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16);
-
esp_err_t
gpio_intr_disable
(gpio_num_t gpio_num)¶ Disable GPIO module interrupt signal.
- Return
- ESP_OK success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
gpio_num
: GPIO number. If you want to disable the interrupt of e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16);
-
esp_err_t
gpio_set_level
(gpio_num_t gpio_num, uint32_t level)¶ GPIO set output level.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG GPIO number error
- Parameters
gpio_num
: GPIO number. If you want to set the output level of e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16);level
: Output level. 0: low ; 1: high
-
int
gpio_get_level
(gpio_num_t gpio_num)¶ GPIO get input level.
- Return
- 0 the GPIO input level is 0
- 1 the GPIO input level is 1
- Parameters
gpio_num
: GPIO number. If you want to get the logic level of e.g. pin GPIO16, gpio_num should be GPIO_NUM_16 (16);
-
esp_err_t
gpio_set_direction
(gpio_num_t gpio_num, gpio_mode_t mode)¶ GPIO set direction.
Configure GPIO direction,such as output_only,input_only,output_and_input
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG GPIO error
- Parameters
gpio_num
: Configure GPIO pins number, it should be GPIO number. If you want to set direction of e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16);mode
: GPIO direction
-
esp_err_t
gpio_set_pull_mode
(gpio_num_t gpio_num, gpio_pull_mode_t pull)¶ Configure GPIO pull-up/pull-down resistors.
Only pins that support both input & output have integrated pull-up and pull-down resistors. Input-only GPIOs 34-39 do not.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG : Parameter error
- Parameters
gpio_num
: GPIO number. If you want to set pull up or down mode for e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16);pull
: GPIO pull up/down mode.
-
esp_err_t
gpio_wakeup_enable
(gpio_num_t gpio_num, gpio_int_type_t intr_type)¶ Enable GPIO wake-up function.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
gpio_num
: GPIO number.intr_type
: GPIO wake-up type. Only GPIO_INTR_LOW_LEVEL or GPIO_INTR_HIGH_LEVEL can be used.
-
esp_err_t
gpio_wakeup_disable
(gpio_num_t gpio_num)¶ Disable GPIO wake-up function.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
gpio_num
: GPIO number
-
esp_err_t
gpio_isr_register
(void (*fn)(void *), void *arg, int intr_alloc_flags, gpio_isr_handle_t *handle, )¶ Register GPIO interrupt handler, the handler is an ISR. The handler will be attached to the same CPU core that this function is running on.
This ISR function is called whenever any GPIO interrupt occurs. See the alternative gpio_install_isr_service() and gpio_isr_handler_add() API in order to have the driver support per-GPIO ISRs.
To disable or remove the ISR, pass the returned handle to the interrupt allocation functions.
- Parameters
fn
: Interrupt handler function.intr_alloc_flags
: Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.arg
: Parameter for handler functionhandle
: Pointer to return handle. If non-NULL, a handle for the interrupt will be returned here.
- Return
- ESP_OK Success ;
- ESP_ERR_INVALID_ARG GPIO error
-
esp_err_t
gpio_pullup_en
(gpio_num_t gpio_num)¶ Enable pull-up on GPIO.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
gpio_num
: GPIO number
-
esp_err_t
gpio_pullup_dis
(gpio_num_t gpio_num)¶ Disable pull-up on GPIO.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
gpio_num
: GPIO number
-
esp_err_t
gpio_pulldown_en
(gpio_num_t gpio_num)¶ Enable pull-down on GPIO.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
gpio_num
: GPIO number
-
esp_err_t
gpio_pulldown_dis
(gpio_num_t gpio_num)¶ Disable pull-down on GPIO.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
gpio_num
: GPIO number
-
esp_err_t
gpio_install_isr_service
(int intr_alloc_flags)¶ Install the driver’s GPIO ISR handler service, which allows per-pin GPIO interrupt handlers.
This function is incompatible with gpio_isr_register() - if that function is used, a single global ISR is registered for all GPIO interrupts. If this function is used, the ISR service provides a global GPIO ISR and individual pin handlers are registered via the gpio_isr_register() function.
- Return
- ESP_OK Success
- ESP_FAIL Operation fail
- ESP_ERR_NO_MEM No memory to install this service
- Parameters
intr_alloc_flags
: Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
-
void
gpio_uninstall_isr_service
()¶ Uninstall the driver’s GPIO ISR service, freeing related resources.
-
esp_err_t
gpio_isr_handler_add
(gpio_num_t gpio_num, gpio_isr_t isr_handler, void *args)¶ Add ISR handler for the corresponding GPIO pin.
Call this function after using gpio_install_isr_service() to install the driver’s GPIO ISR handler service.
The pin ISR handlers no longer need to be declared with IRAM_ATTR, unless you pass the ESP_INTR_FLAG_IRAM flag when allocating the ISR in gpio_install_isr_service().
This ISR handler will be called from an ISR. So there is a stack size limit (configurable as “ISR stack size” in menuconfig). This limit is smaller compared to a global GPIO interrupt handler due to the additional level of indirection.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_STATE Wrong state, the ISR service has not been initialized.
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
gpio_num
: GPIO numberisr_handler
: ISR handler function for the corresponding GPIO number.args
: parameter for ISR handler.
-
esp_err_t
gpio_isr_handler_remove
(gpio_num_t gpio_num)¶ Remove ISR handler for the corresponding GPIO pin.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_STATE Wrong state, the ISR service has not been initialized.
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
gpio_num
: GPIO number
RTC GPIO¶
-
static bool
rtc_gpio_is_valid_gpio
(gpio_num_t gpio_num)¶ Determine if the specified GPIO is a valid RTC GPIO.
- Return
- true if GPIO is valid for RTC GPIO use. talse otherwise.
- Parameters
gpio_num
: GPIO number
-
esp_err_t
rtc_gpio_init
(gpio_num_t gpio_num)¶ Init a GPIO as RTC GPIO.
This function must be called when initializing a pad for an analog function.
- Return
- ESP_OK success
- ESP_ERR_INVALID_ARG GPIO is not an RTC IO
- Parameters
gpio_num
: GPIO number (e.g. GPIO_NUM_12)
-
esp_err_t
rtc_gpio_deinit
(gpio_num_t gpio_num)¶ Init a GPIO as digital GPIO.
- Return
- ESP_OK success
- ESP_ERR_INVALID_ARG GPIO is not an RTC IO
- Parameters
gpio_num
: GPIO number (e.g. GPIO_NUM_12)
-
uint32_t
rtc_gpio_get_level
(gpio_num_t gpio_num)¶ Get the RTC IO input level.
- Return
- 1 High level
- 0 Low level
- ESP_ERR_INVALID_ARG GPIO is not an RTC IO
- Parameters
gpio_num
: GPIO number (e.g. GPIO_NUM_12)
-
esp_err_t
rtc_gpio_set_level
(gpio_num_t gpio_num, uint32_t level)¶ Set the RTC IO output level.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG GPIO is not an RTC IO
- Parameters
gpio_num
: GPIO number (e.g. GPIO_NUM_12)level
: output level
-
esp_err_t
rtc_gpio_set_direction
(gpio_num_t gpio_num, rtc_gpio_mode_t mode)¶ RTC GPIO set direction.
Configure RTC GPIO direction, such as output only, input only, output and input.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG GPIO is not an RTC IO
- Parameters
gpio_num
: GPIO number (e.g. GPIO_NUM_12)mode
: GPIO direction
-
esp_err_t
rtc_gpio_pullup_en
(gpio_num_t gpio_num)¶ RTC GPIO pullup enable.
This function only works for RTC IOs. In general, call gpio_pullup_en, which will work both for normal GPIOs and RTC IOs.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG GPIO is not an RTC IO
- Parameters
gpio_num
: GPIO number (e.g. GPIO_NUM_12)
-
esp_err_t
rtc_gpio_pulldown_en
(gpio_num_t gpio_num)¶ RTC GPIO pulldown enable.
This function only works for RTC IOs. In general, call gpio_pulldown_en, which will work both for normal GPIOs and RTC IOs.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG GPIO is not an RTC IO
- Parameters
gpio_num
: GPIO number (e.g. GPIO_NUM_12)
-
esp_err_t
rtc_gpio_pullup_dis
(gpio_num_t gpio_num)¶ RTC GPIO pullup disable.
This function only works for RTC IOs. In general, call gpio_pullup_dis, which will work both for normal GPIOs and RTC IOs.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG GPIO is not an RTC IO
- Parameters
gpio_num
: GPIO number (e.g. GPIO_NUM_12)
-
esp_err_t
rtc_gpio_pulldown_dis
(gpio_num_t gpio_num)¶ RTC GPIO pulldown disable.
This function only works for RTC IOs. In general, call gpio_pulldown_dis, which will work both for normal GPIOs and RTC IOs.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG GPIO is not an RTC IO
- Parameters
gpio_num
: GPIO number (e.g. GPIO_NUM_12)
警告
doxygenfunction: Cannot find function “rtc_gpio_unhold_all” in doxygen xml output for project “esp32-idf” from directory: xml/