Appearance
GPIO
API
GPIO & RTC GPIO - ESP32 - — ESP-IDF 编程指南 v5.2.1 文档 (espressif.com)
ESP32S2开发学习之路--GPIO与中断(三)_gpio isr-CSDN博客
gpio_config_t 结构体
- pin_bit_mask端口号:GPIO_SEL_X。
- intr_type中断触发类型:GPIO_INTR_DISABLE关闭中断触发;GPIO_INTR_POSEDGE上升沿;GPIO_INTR_NEGEDGE下降沿;GPIO_INTR_ANYEDGE双边沿;GPIO_INTR_LOW_LEVEL低电平;GPIO_INTR_HIGH_LEVEL高电平。
- mode模式:GPIO_MODE_DISABLE关闭;GPIO_MODE_INPUT输入;GPIO_MODE_OUTPUT输出;GPIO_MODE_OUTPUT_OD开漏输出;PIO_MODE_INPUT_OUTPUT_OD开漏输入输出;GPIO_MODE_INPUT_OUTPUT输入输出。
- pull_up_en上拉电阻:GPIO_PULLUP_DISABLE断开;GPIO_PULLUP_ENABLE使能。
- pull_down_en下拉电阻:GPIO_PULLDOWN_DISABLE断开;GPIO_PULLDOWN_ENABLE使能。
gpio_config配置函数
c
esp_err_t gpio_config(const gpio_config_t *pGPIOConfig)
用于配置GPIO
gpio_install_isr_service单个gpio中断
c
esp_err_t gpio_install_isr_service(int 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.
开启一个处理单个引脚中断的服务
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.
gpio_isr_handler_add添加处理函数
c
esp_err_t gpio_isr_handler_add(gpio_num_t gpio_num, gpio_isr_t isr_handler, void *args)
给某一个引脚添加一个中断处理函数
- gpio_num -- GPIO number
- isr_handler -- ISR handler function for the corresponding GPIO number.
- args -- parameter for ISR handler.
gpio_isr_register所有GPIO中断
c
esp_err_t gpio_isr_register(void (*fn)(void*), void *arg, int intr_alloc_flags, gpio_isr_handle_t *handle)
- gpio_num -- GPIO number
- isr_handler -- ISR handler function for the corresponding GPIO number.
- args -- parameter for ISR handler.
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.
gpio_set_level设置电平
c
esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level)
gpio_get_level获取电平状态
python
int gpio_get_level(gpio_num_t gpio_num)
If the pad is not configured for input (or input and output) the returned value is always 0.
配置的 时候需要是输入输出模式
示例
c
#include <stdio.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#define GPIO_OUTPUT_PIN_SEL (1ULL<<GPIO_NUM_0)
static QueueHandle_t gpio_evt_queue = NULL;
//任务函数
static void gpio_task_example(void* arg)
{
uint32_t io_num;
for (;;) {
if (xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
printf("GPIO[%"PRIu32"] intr, val: %d\n", io_num, gpio_get_level(io_num));
}
}
}
// 中断处理函数
static void IRAM_ATTR gpio_isr_handler(void* arg)
{
uint32_t gpio_num = (uint32_t) arg;
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
}
void app_main(void)
{
//zero-initialize the config structure.
gpio_config_t io_conf = {
//disable interrupt
.intr_type = GPIO_INTR_NEGEDGE, // 设置中断,下降沿
//set as input mode
.mode = GPIO_MODE_INPUT,
//bit mask of the pins that you want to set
.pin_bit_mask = GPIO_OUTPUT_PIN_SEL,
//disable pull-down mode
.pull_down_en = 0,
//enable pull-up mode 打开上拉电阻
.pull_up_en = 1,
};
//configure GPIO with the given settings
gpio_config(&io_conf);
//create a queue to handle gpio event from isr 消息队列
gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
//start gpio task
xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
//install gpio isr service
/*
安装GPIO ISR服务的驱动,开启每个引脚GPIO中断处理程序 。
这个函数与gpio_isr_register()不兼容。
如果使用这个函数,ISR服务将提供一个全局GPIO ISR,并且通过gpio_isr_handler_add()函数注册单个的pin处理程序。
*/
gpio_install_isr_service(0);
//hook isr handler for specific gpio pin
gpio_isr_handler_add(GPIO_NUM_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
}