硬件:ESP32-Devkit-V4 MODEL:ESP32-32U
库:ESP-IDF v5.4.1
系统:windows中的虚拟机 ubuntu 22.04
引脚数量:多数ESP32型号有34个GPIO(部分型号有更多)。注意:部分GPIO内部已用于特殊功能(如外部Flash/PSRAM),需避免使用。
电平:支持3.3V TTL电平,不兼容5V输入,需注意电压匹配。
内部上/下拉电阻:每个GPIO可通过软件启用内部上拉或下拉电阻,简化硬件设计。
中断功能:所有GPIO支持中断,可配置为上升沿、下降沿、高/低电平触发等。
功能复用:多数引脚功能可配置,如GPIO、ADC、DAC、PWM、SPI、I2C、UART等
注意!!!:第一次使用ESP32开发板,对API理解不够,现总结几点,供以后使用
1.GPIO编号 ≠ 物理管脚编号
比如UART2 在手册中的管脚标号为27,28,GPIO编号为 16 17,配置时使用的是GPIO编号
2.开发板总共有3个UART,UART0一般供给烧录口使用,UART1供给SPI flash,UART2 供给客户使用
3.模组的 GPIO6 至 GPIO11 已连接内部 SPI Flash,不建议用于其他功能
头文件
#ifndefBSP_GPIO_H_#defineBSP_GPIO_H_voidgpio_recv_task(void*arg);voidgpio_set_task(void*arg);#endif源文件
#include"bsp_gpio.h"#include<stdio.h>#include<string.h>#include<stdlib.h>#include<inttypes.h>#include"freertos/FreeRTOS.h"#include"freertos/task.h"#include"freertos/queue.h"#include"driver/gpio.h"/** * Brief: * This test code shows how to configure gpio and how to use gpio interrupt. * * GPIO status: * GPIO18: output (ESP32C2/ESP32H2 uses GPIO8 as the second output pin) * GPIO19: output (ESP32C2/ESP32H2 uses GPIO9 as the second output pin) * GPIO4: input, pulled up, interrupt from rising edge and falling edge * GPIO5: input, pulled up, interrupt from rising edge. * * Note. These are the default GPIO pins to be used in the example. You can * change IO pins in menuconfig. * * Test: * Connect GPIO18(8) with GPIO4 * Connect GPIO19(9) with GPIO5 * Generate pulses on GPIO18(8)/19(9), that triggers interrupt on GPIO4/5 * */#defineGPIO_OUTPUT_IO_018#defineGPIO_OUTPUT_IO_119#defineGPIO_OUTPUT_PIN_SEL((1ULL<<GPIO_OUTPUT_IO_0)|(1ULL<<GPIO_OUTPUT_IO_1))/* * Let's say, GPIO_OUTPUT_IO_0=18, GPIO_OUTPUT_IO_1=19 * In binary representation, * 1ULL<<GPIO_OUTPUT_IO_0 is equal to 0000000000000000000001000000000000000000 and * 1ULL<<GPIO_OUTPUT_IO_1 is equal to 0000000000000000000010000000000000000000 * GPIO_OUTPUT_PIN_SEL 0000000000000000000011000000000000000000 * */#defineGPIO_INPUT_IO_04#defineGPIO_INPUT_IO_15#defineGPIO_INPUT_PIN_SEL((1ULL<<GPIO_INPUT_IO_0)|(1ULL<<GPIO_INPUT_IO_1))/* * Let's say, GPIO_INPUT_IO_0=4, GPIO_INPUT_IO_1=5 * In binary representation, * 1ULL<<GPIO_INPUT_IO_0 is equal to 0000000000000000000000000000000000010000 and * 1ULL<<GPIO_INPUT_IO_1 is equal to 0000000000000000000000000000000000100000 * GPIO_INPUT_PIN_SEL 0000000000000000000000000000000000110000 * */#defineESP_INTR_FLAG_DEFAULT0staticQueueHandle_t gpio_evt_queue=NULL;staticvoidIRAM_ATTRgpio_isr_handler(void*arg){uint32_tgpio_num=(uint32_t)arg;xQueueSendFromISR(gpio_evt_queue,&gpio_num,NULL);}voidgpio_recv_task(void*arg){uint32_tio_num;for(;;){if(gpio_evt_queue==NULL){vTaskDelay(1000/portTICK_PERIOD_MS);}else{if(xQueueReceive(gpio_evt_queue,&io_num,portMAX_DELAY)){printf("GPIO[%"PRIu32"] intr, val: %d\n",io_num,gpio_get_level(io_num));}}}}voidgpio_set_task(void*arg){//zero-initialize the config structure.gpio_config_tio_conf={};//disable interruptio_conf.intr_type=GPIO_INTR_DISABLE;//set as output modeio_conf.mode=GPIO_MODE_OUTPUT;//bit mask of the pins that you want to set,e.g.GPIO18/19io_conf.pin_bit_mask=GPIO_OUTPUT_PIN_SEL;//disable pull-down modeio_conf.pull_down_en=0;//disable pull-up modeio_conf.pull_up_en=0;//configure GPIO with the given settingsgpio_config(&io_conf);//interrupt of rising edgeio_conf.intr_type=GPIO_INTR_POSEDGE;//bit mask of the pins, use GPIO4/5 hereio_conf.pin_bit_mask=GPIO_INPUT_PIN_SEL;//set as input modeio_conf.mode=GPIO_MODE_INPUT;//enable pull-up modeio_conf.pull_up_en=1;gpio_config(&io_conf);//change gpio interrupt type for one pingpio_set_intr_type(GPIO_INPUT_IO_0,GPIO_INTR_ANYEDGE);//create a queue to handle gpio event from isrgpio_evt_queue=xQueueCreate(10,sizeof(uint32_t));//start gpio task//xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);//install gpio isr servicegpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);//hook isr handler for specific gpio pingpio_isr_handler_add(GPIO_INPUT_IO_0,gpio_isr_handler,(void*)GPIO_INPUT_IO_0);//hook isr handler for specific gpio pingpio_isr_handler_add(GPIO_INPUT_IO_1,gpio_isr_handler,(void*)GPIO_INPUT_IO_1);//remove isr handler for gpio number.gpio_isr_handler_remove(GPIO_INPUT_IO_0);//hook isr handler for specific gpio pin againgpio_isr_handler_add(GPIO_INPUT_IO_0,gpio_isr_handler,(void*)GPIO_INPUT_IO_0);printf("Minimum free heap size: %"PRIu32" bytes\n",esp_get_minimum_free_heap_size());intcnt=0;while(1){printf("cnt: %d\n",cnt++);vTaskDelay(1000/portTICK_PERIOD_MS);gpio_set_level(GPIO_OUTPUT_IO_0,cnt%2);gpio_set_level(GPIO_OUTPUT_IO_1,cnt%2);}}