GPIO & RTC GPIO

[English]

概述

ESP32-C3 芯片具有 22 个物理 GPIO 管脚(GPIO0 ~ GPIO21)。每个管脚都可用作一个通用 IO,或连接一个内部的外设信号。通过 GPIO 交换矩阵和 IO MUX,可配置外设模块的输入信号来源于任何的 IO 管脚,并且外设模块的输出信号也可连接到任意 IO 管脚。这些模块共同组成了芯片的 IO 控制。更多详细信息,请参阅 ESP32-C3 技术参考手册 > IO MUX 和 GPIO 矩阵(GPIO、IO_MUX) [PDF]。

下表提供了各管脚的详细信息,部分 GPIO 具有特殊的使用限制,具体可参考表中的注释列。

GPIO

模拟功能

注释

GPIO0

ADC1_CH0

RTC

GPIO1

ADC1_CH1

RTC

GPIO2

ADC1_CH2

Strapping 管脚;RTC

GPIO3

ADC1_CH3

RTC

GPIO4

ADC1_CH4

RTC

GPIO5

ADC2_CH0

RTC

GPIO6

GPIO7

GPIO8

Strapping 管脚

GPIO9

Strapping 管脚

GPIO10

GPIO11

GPIO12

SPI0/1

GPIO13

SPI0/1

GPIO14

SPI0/1

GPIO15

SPI0/1

GPIO16

SPI0/1

GPIO17

SPI0/1

GPIO18

USB-JTAG

GPIO19

USB-JTAG

GPIO20

GPIO21

备注

  • Strapping 管脚:GPIO2、GPIO8、和 GPIO9 是 Strapping 管脚。更多信息请参考 ESP32-C3 技术规格书

  • SPI0/1:GPIO12-17 通常用于 SPI flash 和 PSRAM,不推荐用于其他用途。

  • USB-JTAG:GPIO18 和 GPIO19 默认用于 USB-JTAG。用做 GPIO 时驱动程序将禁用 USB-JTAG。

  • RTC:GPIO0-5 可以在 Deep-sleep 模式时使用。

应用示例

GPIO 输出和输入中断示例:peripherals/gpio/generic_gpio

API 参考 - 普通 GPIO

Header File

Functions

esp_err_t gpio_config(const gpio_config_t *pGPIOConfig)

GPIO common configuration.

   Configure GPIO's Mode,pull-up,PullDown,IntrType

参数

pGPIOConfig – Pointer to GPIO configure struct

返回

  • ESP_OK success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_reset_pin(gpio_num_t gpio_num)

Reset an gpio to default state (select gpio function, enable pullup and disable input and output).

备注

This function also configures the IOMUX for this pin to the GPIO function, and disconnects any other peripheral output configured via GPIO Matrix.

参数

gpio_num – GPIO number.

返回

Always return ESP_OK.

esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, gpio_int_type_t intr_type)

GPIO set interrupt trigger type.

参数
  • 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_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_intr_enable(gpio_num_t gpio_num)

Enable GPIO module interrupt signal.

备注

ESP32: Please do not use the interrupt of GPIO36 and GPIO39 when using ADC or Wi-Fi and Bluetooth with sleep mode enabled. Please refer to the comments of adc1_get_raw. Please refer to Section 3.11 of ESP32 ECO and Workarounds for Bugs for the description of this issue. As a workaround, call adc_power_acquire() in the app. This will result in higher power consumption (by ~1mA), but will remove the glitches on GPIO36 and GPIO39.

参数

gpio_num – GPIO number. If you want to enable an interrupt on e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16);

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_intr_disable(gpio_num_t gpio_num)

Disable GPIO module interrupt signal.

备注

This function is allowed to be executed when Cache is disabled within ISR context, by enabling CONFIG_GPIO_CTRL_FUNC_IN_IRAM

参数

gpio_num – GPIO number. If you want to disable the interrupt of e.g. GPIO16, gpio_num should be GPIO_NUM_16 (16);

返回

  • ESP_OK success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level)

GPIO set output level.

备注

This function is allowed to be executed when Cache is disabled within ISR context, by enabling CONFIG_GPIO_CTRL_FUNC_IN_IRAM

参数
  • 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

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG GPIO number error

int gpio_get_level(gpio_num_t gpio_num)

GPIO get input level.

警告

If the pad is not configured for input (or input and output) the returned value is always 0.

参数

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);

返回

  • 0 the GPIO input level is 0

  • 1 the GPIO input level is 1

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

参数
  • 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_OK Success

  • ESP_ERR_INVALID_ARG GPIO error

esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)

Configure GPIO pull-up/pull-down resistors.

备注

ESP32: Only pins that support both input & output have integrated pull-up and pull-down resistors. Input-only GPIOs 34-39 do not.

参数
  • 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_OK Success

  • ESP_ERR_INVALID_ARG : Parameter error

esp_err_t gpio_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)

Enable GPIO wake-up function.

参数
  • gpio_num – GPIO number.

  • intr_type – GPIO wake-up type. Only GPIO_INTR_LOW_LEVEL or GPIO_INTR_HIGH_LEVEL can be used.

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_wakeup_disable(gpio_num_t gpio_num)

Disable GPIO wake-up function.

参数

gpio_num – GPIO number

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

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.

参数
  • fn – Interrupt handler function.

  • arg – Parameter for 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.

  • handle – Pointer to return handle. If non-NULL, a handle for the interrupt will be returned here.

返回

  • ESP_OK Success ;

  • ESP_ERR_INVALID_ARG GPIO error

  • ESP_ERR_NOT_FOUND No free interrupt found with the specified flags

esp_err_t gpio_pullup_en(gpio_num_t gpio_num)

Enable pull-up on GPIO.

参数

gpio_num – GPIO number

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_pullup_dis(gpio_num_t gpio_num)

Disable pull-up on GPIO.

参数

gpio_num – GPIO number

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_pulldown_en(gpio_num_t gpio_num)

Enable pull-down on GPIO.

参数

gpio_num – GPIO number

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_pulldown_dis(gpio_num_t gpio_num)

Disable pull-down on GPIO.

参数

gpio_num – GPIO number

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_install_isr_service(int intr_alloc_flags)

Install the GPIO driver’s ETS_GPIO_INTR_SOURCE 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_handler_add() 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.

返回

  • ESP_OK Success

  • ESP_ERR_NO_MEM No memory to install this service

  • ESP_ERR_INVALID_STATE ISR service already installed.

  • ESP_ERR_NOT_FOUND No free interrupt found with the specified flags

  • ESP_ERR_INVALID_ARG GPIO error

void gpio_uninstall_isr_service(void)

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.

参数
  • gpio_num – GPIO number

  • isr_handler – ISR handler function for the corresponding GPIO number.

  • args – parameter for ISR handler.

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_STATE Wrong state, the ISR service has not been initialized.

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_isr_handler_remove(gpio_num_t gpio_num)

Remove ISR handler for the corresponding GPIO pin.

参数

gpio_num – GPIO number

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_STATE Wrong state, the ISR service has not been initialized.

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_set_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t strength)

Set GPIO pad drive capability.

参数
  • gpio_num – GPIO number, only support output GPIOs

  • strength – Drive capability of the pad

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_get_drive_capability(gpio_num_t gpio_num, gpio_drive_cap_t *strength)

Get GPIO pad drive capability.

参数
  • gpio_num – GPIO number, only support output GPIOs

  • strength – Pointer to accept drive capability of the pad

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_hold_en(gpio_num_t gpio_num)

Enable gpio pad hold function.

The gpio pad hold function works in both input and output modes, but must be output-capable gpios. If pad hold enabled: in output mode: the output level of the pad will be force locked and can not be changed. in input mode: the input value read will not change, regardless the changes of input signal.

The state of digital gpio cannot be held during Deep-sleep, and it will resume the hold function when the chip wakes up from Deep-sleep. If the digital gpio also needs to be held during Deep-sleep, gpio_deep_sleep_hold_en should also be called.

Power down or call gpio_hold_dis will disable this function.

参数

gpio_num – GPIO number, only support output-capable GPIOs

返回

  • ESP_OK Success

  • ESP_ERR_NOT_SUPPORTED Not support pad hold function

esp_err_t gpio_hold_dis(gpio_num_t gpio_num)

Disable gpio pad hold function.

When the chip is woken up from Deep-sleep, the gpio will be set to the default mode, so, the gpio will output the default level if this function is called. If you don’t want the level changes, the gpio should be configured to a known state before this function is called. e.g. If you hold gpio18 high during Deep-sleep, after the chip is woken up and gpio_hold_dis is called, gpio18 will output low level(because gpio18 is input mode by default). If you don’t want this behavior, you should configure gpio18 as output mode and set it to hight level before calling gpio_hold_dis.

参数

gpio_num – GPIO number, only support output-capable GPIOs

返回

  • ESP_OK Success

  • ESP_ERR_NOT_SUPPORTED Not support pad hold function

void gpio_deep_sleep_hold_en(void)

Enable all digital gpio pad hold function during Deep-sleep.

When the chip is in Deep-sleep mode, all digital gpio will hold the state before sleep, and when the chip is woken up, the status of digital gpio will not be held. Note that the pad hold feature only works when the chip is in Deep-sleep mode, when not in sleep mode, the digital gpio state can be changed even you have called this function.

Power down or call gpio_hold_dis will disable this function, otherwise, the digital gpio hold feature works as long as the chip enter Deep-sleep.

void gpio_deep_sleep_hold_dis(void)

Disable all digital gpio pad hold function during Deep-sleep.

void gpio_iomux_in(uint32_t gpio_num, uint32_t signal_idx)

Set pad input to a peripheral signal through the IOMUX.

参数
  • gpio_num – GPIO number of the pad.

  • signal_idx – Peripheral signal id to input. One of the *_IN_IDX signals in soc/gpio_sig_map.h.

void gpio_iomux_out(uint8_t gpio_num, int func, bool oen_inv)

Set peripheral output to an GPIO pad through the IOMUX.

参数
  • gpio_num – gpio_num GPIO number of the pad.

  • func – The function number of the peripheral pin to output pin. One of the FUNC_X_* of specified pin (X) in soc/io_mux_reg.h.

  • oen_inv – True if the output enable needs to be inverted, otherwise False.

esp_err_t gpio_force_hold_all(void)

Force hold digital and rtc gpio pad.

备注

GPIO force hold, whether the chip in sleep mode or wakeup mode.

esp_err_t gpio_force_unhold_all(void)

Force unhold digital and rtc gpio pad.

备注

GPIO force unhold, whether the chip in sleep mode or wakeup mode.

esp_err_t gpio_sleep_sel_en(gpio_num_t gpio_num)

Enable SLP_SEL to change GPIO status automantically in lightsleep.

参数

gpio_num – GPIO number of the pad.

返回

  • ESP_OK Success

esp_err_t gpio_sleep_sel_dis(gpio_num_t gpio_num)

Disable SLP_SEL to change GPIO status automantically in lightsleep.

参数

gpio_num – GPIO number of the pad.

返回

  • ESP_OK Success

esp_err_t gpio_sleep_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)

GPIO set direction at sleep.

Configure GPIO direction,such as output_only,input_only,output_and_input

参数
  • 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_OK Success

  • ESP_ERR_INVALID_ARG GPIO error

esp_err_t gpio_sleep_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)

Configure GPIO pull-up/pull-down resistors at sleep.

备注

ESP32: Only pins that support both input & output have integrated pull-up and pull-down resistors. Input-only GPIOs 34-39 do not.

参数
  • 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_OK Success

  • ESP_ERR_INVALID_ARG : Parameter error

esp_err_t gpio_deep_sleep_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)

Enable GPIO deep-sleep wake-up function.

备注

Called by the SDK. User shouldn’t call this directly in the APP.

参数
  • gpio_num – GPIO number.

  • intr_type – GPIO wake-up type. Only GPIO_INTR_LOW_LEVEL or GPIO_INTR_HIGH_LEVEL can be used.

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

esp_err_t gpio_deep_sleep_wakeup_disable(gpio_num_t gpio_num)

Disable GPIO deep-sleep wake-up function.

参数

gpio_num – GPIO number

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

Structures

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

Macros

GPIO_PIN_COUNT
GPIO_IS_VALID_GPIO(gpio_num)

Check whether it is a valid GPIO number.

GPIO_IS_VALID_OUTPUT_GPIO(gpio_num)

Check whether it can be a valid GPIO number of output mode.

GPIO_IS_DEEP_SLEEP_WAKEUP_VALID_GPIO(gpio_num)

Type Definitions

typedef intr_handle_t gpio_isr_handle_t
typedef void (*gpio_isr_t)(void *arg)

GPIO interrupt handler.

Param arg

User registered data

Header File

Macros

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_24
GPIO_PIN_REG_25
GPIO_PIN_REG_26
GPIO_PIN_REG_27
GPIO_PIN_REG_28
GPIO_PIN_REG_29
GPIO_PIN_REG_30
GPIO_PIN_REG_31
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_PIN_REG_40
GPIO_PIN_REG_41
GPIO_PIN_REG_42
GPIO_PIN_REG_43
GPIO_PIN_REG_44
GPIO_PIN_REG_45
GPIO_PIN_REG_46
GPIO_PIN_REG_47
GPIO_PIN_REG_48

Enumerations

enum gpio_port_t

Values:

enumerator GPIO_PORT_0
enumerator GPIO_PORT_MAX
enum gpio_num_t

Values:

enumerator GPIO_NUM_NC

Use to signal not connected to S/W

enumerator GPIO_NUM_0

GPIO0, input and output

enumerator GPIO_NUM_1

GPIO1, input and output

enumerator GPIO_NUM_2

GPIO2, input and output

enumerator GPIO_NUM_3

GPIO3, input and output

enumerator GPIO_NUM_4

GPIO4, input and output

enumerator GPIO_NUM_5

GPIO5, input and output

enumerator GPIO_NUM_6

GPIO6, input and output

enumerator GPIO_NUM_7

GPIO7, input and output

enumerator GPIO_NUM_8

GPIO8, input and output

enumerator GPIO_NUM_9

GPIO9, input and output

enumerator GPIO_NUM_10

GPIO10, input and output

enumerator GPIO_NUM_11

GPIO11, input and output

enumerator GPIO_NUM_12

GPIO12, input and output

enumerator GPIO_NUM_13

GPIO13, input and output

enumerator GPIO_NUM_14

GPIO14, input and output

enumerator GPIO_NUM_15

GPIO15, input and output

enumerator GPIO_NUM_16

GPIO16, input and output

enumerator GPIO_NUM_17

GPIO17, input and output

enumerator GPIO_NUM_18

GPIO18, input and output

enumerator GPIO_NUM_19

GPIO19, input and output

enumerator GPIO_NUM_20

GPIO20, input and output

enumerator GPIO_NUM_21

GPIO21, input and output

enumerator GPIO_NUM_MAX
enum gpio_int_type_t

Values:

enumerator GPIO_INTR_DISABLE

Disable GPIO interrupt

enumerator GPIO_INTR_POSEDGE

GPIO interrupt type : rising edge

enumerator GPIO_INTR_NEGEDGE

GPIO interrupt type : falling edge

enumerator GPIO_INTR_ANYEDGE

GPIO interrupt type : both rising and falling edge

enumerator GPIO_INTR_LOW_LEVEL

GPIO interrupt type : input low level trigger

enumerator GPIO_INTR_HIGH_LEVEL

GPIO interrupt type : input high level trigger

enumerator GPIO_INTR_MAX
enum gpio_mode_t

Values:

enumerator GPIO_MODE_DISABLE

GPIO mode : disable input and output

enumerator GPIO_MODE_INPUT

GPIO mode : input only

enumerator GPIO_MODE_OUTPUT

GPIO mode : output only mode

enumerator GPIO_MODE_OUTPUT_OD

GPIO mode : output only with open-drain mode

enumerator GPIO_MODE_INPUT_OUTPUT_OD

GPIO mode : output and input with open-drain mode

enumerator GPIO_MODE_INPUT_OUTPUT

GPIO mode : output and input mode

enum gpio_pullup_t

Values:

enumerator GPIO_PULLUP_DISABLE

Disable GPIO pull-up resistor

enumerator GPIO_PULLUP_ENABLE

Enable GPIO pull-up resistor

enum gpio_pulldown_t

Values:

enumerator GPIO_PULLDOWN_DISABLE

Disable GPIO pull-down resistor

enumerator GPIO_PULLDOWN_ENABLE

Enable GPIO pull-down resistor

enum gpio_pull_mode_t

Values:

enumerator GPIO_PULLUP_ONLY

Pad pull up

enumerator GPIO_PULLDOWN_ONLY

Pad pull down

enumerator GPIO_PULLUP_PULLDOWN

Pad pull up + pull down

enumerator GPIO_FLOATING

Pad floating

enum gpio_drive_cap_t

Values:

enumerator GPIO_DRIVE_CAP_0

Pad drive capability: weak

enumerator GPIO_DRIVE_CAP_1

Pad drive capability: stronger

enumerator GPIO_DRIVE_CAP_2

Pad drive capability: medium

enumerator GPIO_DRIVE_CAP_DEFAULT

Pad drive capability: medium

enumerator GPIO_DRIVE_CAP_3

Pad drive capability: strongest

enumerator GPIO_DRIVE_CAP_MAX