Tip: as a partner of Espressif in Greater China and a partner of sigmastar VAD, we not only carefully sorted out the problems you may encounter in the development process, but also a concise tutorial to get started quickly for the reference of development partners. At the same time, it also carefully sorted out the main characteristics and applications of new products and schemes of Lexin and Xingchen technology! I hope you can understand and quickly use good solutions and products at the first time!
ESP32_GPIO
1, General GPIO
ESP32 chip has 34 physical GPIO pads. Each pad can be used as a general IO or connected to an internal peripheral signal. IO_MUX,RTC IO_MUX and GPIO switching matrix are used to transmit signals from peripherals to GPIO pad. These modules together constitute the IO control of the chip.
GPIO output and input interrupt example: Home / ymy / ESP IDF / examples / peripherals / GPIO / generic_ gpio/
- GPIO1 and gpio3 should be reserved separately for serial communication
- GPIO6, 7, 8, 9, 10 and 11 should be reserved separately for Flash control
- GPIO34, 35, 36 and 39 can only be input without up and down (in order to ensure that the pins are in the default state, these four pins can achieve a stable level by connecting external up and down resistors.)
- Pull up
- PullDown
- IntrType (interrupt)
- GPIO_PIN_INTR_POSEDGE (rising edge)
- GPIO_INTR_POSEDGE (falling edge)
- GPIO_INTR_ANYEDGE (whether it is rising edge or falling edge, the system will automatically select one as the breaking edge)
Example code:
//Function declaration #include <stdio.h> #include <string.h> #include <stdlib.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. //How to configure gpio and how to use gpio interrupt. * * GPIO status: * GPIO18: output //GP1018 Push pull output * GPIO19: output //GPI019 Push pull output * GPIO4: input, pulled up, interrupt from rising edge and falling edge //GPI04 Input, pull-up and interrupt from rising and falling edges * GPIO5: input, pulled up, interrupt from rising edge. //GPIO5 Input, pull up, interrupt from rising edge * * Test: * Connect GPIO18 with GPIO4 //Connect GP1018 and GPIO4 * Connect GPIO19 with GPIO5 //Connect GP1019 and GPIO5 * Generate pulses on GPIO18/19, that triggers interrupt on GPIO4/5 //Connect GP1019 and GPIO5 and generate pulses on GPI018/19 to trigger interrupts on GPIO4/5 * */ #define GPIO_OUTPUT_IO_0 18 / / define GPIO18 as output pin 0 #define GPIO_OUTPUT_IO_1 19 / / define GPIO19 as output pin 1 #define GPIO_ OUTPUT_ PIN_ Sel ((1ull < < gpio_output_io_0) | (1ull < < gpio_output_io_1)) / / mixed #define GPIO_INPUT_IO_0 4 / / define GPIO4 as input pin 0 #define GPIO_INPUT_IO_1 5 / / define GPIO5 as input pin 1 #define GPIO_ INPUT_ PIN_ Sel ((1ull < < gpio_input_io_0) | (1ull < < gpio_input_io_1)) / / mixing #define ESP_INTR_FLAG_DEFAULT 0 / / the interrupt flag is cleared // Set the message queue, which is used to deliver the information of interruption and return variables static xQueueHandle gpio_evt_queue = NULL; //Empty variable gpio_evt_queue is defined as a queue (xQueueHandle) //Interrupt handling function static void IRAM_ATTR gpio_isr_handler(void *arg) { uint32_t gpio_num = (uint32_t)arg; //Get the pin number of the GPIO passed xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL); //Deliver data to the message queue, and the message content is the pin number of GPIO } // GPIO interrupt message processing task static void gpio_task_example(void *arg) { uint32_t io_num; for (;;) { // API function: receive queue message function xQueueReceive(). In the task of FreeRTOS, you can receive the interrupt message of obtaining the chip through function xQueueReceive, because this function can set the timeout wait until there is a message stored in the message queue or the set timeout overflows. //Accept the gpio queue and delete the queue after reading if (xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) //Obtain the interrupt message of the chip, the value of the enable pin and the delay time through the xQueueReceive() function. { printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num)); //Print GPIO pin and pin output values } } } //Main function void app_main(void) { //Initialize GPIO18/19 gpio_config_t io_conf1 = { .intr_type = GPIO_INTR_DISABLE, // GPIO interrupt disable .mode = GPIO_MODE_OUTPUT, // GPIO set to output mode .pin_bit_mask = GPIO_OUTPUT_PIN_SEL, //Set GPIO18 as output pin 0 and GPIO19 as output pin 1 as mixed mode .pull_down_en = 0, //Do not set GPIO drop-down .pull_up_en = 0, //Do not set GPIO pull-up }; gpio_config(&io_conf1); // gpio_ The function of config is to configure GPIO pin parameters //Initialize GPIO4/5 gpio_config_t io_conf2; io_conf2.intr_type = GPIO_INTR_POSEDGE; //Rising edge, lower rising edge: GPIO_INTR_NEGEDGE io_conf2.pin_bit_mask = GPIO_INPUT_PIN_SEL; //Set GPIO4 as output pin 0 and GPIO5 as output pin 1 as mixed mode io_conf2.mode = GPIO_MODE_INPUT; // GPIO set to input mode io_conf2.pull_up_en = 1; //Enable pull-up gpio_config(&io_conf2); // gpio_ The function of config is to configure GPIO pin parameters //Interrupt on rising and falling edge of gpio pin (any one) gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE); gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t)); //Create a variable to store queue data xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL); //Create tasks that enable GPIO interrupts //Install gpio interrupt driver gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); //Bind the interrupt service function to the specified gpio gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void *)GPIO_INPUT_IO_0); gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void *)GPIO_INPUT_IO_1); //Delete the interrupt service function of the corresponding GPIO pin gpio_isr_handler_remove(GPIO_INPUT_IO_0); //Bind the interrupt service function to the specified gpio again gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void *)GPIO_INPUT_IO_0); //Print memory usage printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size()); int cnt = 0; while (1) { printf("cnt: %d\n", cnt++); vTaskDelay(1000 / portTICK_RATE_MS); //Delay 1s gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2); //Set output low level gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2); //Set output low level } }
2, RTC_GPIO
- In deep sleep mode, only RTC_GPIO can keep working. And only RTC GPIO can be used as the wake-up source instead of digital GPIO.
- 18 RTC GPIO pins, controlled by the RTC subsystem of ESP32. As an output pin, the chip can still maintain the output level value when the chip is in deep sleep sleep mode or wake up the chip from deep sleep when it is used as an input pin. See "RTC_MUX pin list" in ESP32 technical reference manual.
- RTC in ULP mode_ Application routine of GPIO: esp-iot-solution/examples/ulp_examples
- Original link: https://blog.csdn.net/Marchtwentytwo/article/details/122196199
summary
To set GPIO interrupt, you need to perform the following three steps:
-
Set GPIO to input mode: GPIO_INPUT_IO_x
-
Write interrupt handling functions and interrupt handling tasks:
//Interrupt handling function static void IRAM_ATTR gpio_isr_handler(void *arg) { ... } // GPIO interrupt message processing task static void gpio_task_example(void *arg) { ... }
-
Set interrupt trigger condition and callback function:
- GPIO_PIN_INTR_POSEDGE (rising edge)
- GPIO_INTR_POSEDGE (falling edge)
Difficult problems
1, Run github to download 6 ESP32_ Multithread file cannot be compiled because: cmakelists txt not found in project directory /home/ymy/ESP32_ Code/2. Code/6.ESP32_MultiThread/6. ESP32_ MultiThread?
A: since UP is mainly developed by arduino, it cannot be compiled in IDF environment. How can I transfer this to IDF environment?
arduino is an IDE. The compilation method is different from IDF. Projects on both sides cannot compile each other. arduino can be used as a library
2, What is CMack? What is CMackFiles?
Answer:
- CMake is a cross platform installation (compilation) tool.
- CMackFiles are cmack files, which are generated after compilation.
3, What function is xTaskCreate?
A: if you don't know the function of this function, please learn it first FreeRTOS
- If the task is created using xTaskCreate(), RAM needs to be automatically allocated from FreeRTOS heap.
- If you create a task using xtask createstatic(), RAM is provided by the application writer, which results in two additional function parameters, but allows RAM to be allocated statically at compile time.
4, After modifying the GPIO instance code, there is an error in the compilation. After modifying it, it can be done, but I don't know why the program I modified will bug?
A: the reason is that I will gpio_config_t io_conf; Rewritten as gpio_config_t io_conf () {} or gpio_config_t io_conf = {}, so compilation will fail
When initializing the declaration, the end of the structure assignment is a comma, and a semicolon should be added after the square brackets of the declared variable.
**Note: * * remember to save the code and compile it after modification
Method 1:
//Initialize GPIO18/19
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_DISABLE; //GPIO interrupt disable
io_conf.mode = GPIO_MODE_OUTPUT; //GPIO set to output mode
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL; // Set GPIO18 as output pin 0 and GPIO19 as output pin 1 as mixed mode
io_conf.pull_down_en = 0; // Do not set GPIO drop-down
io_conf.pull_up_en = 0; // Do not set GPIO pull-up
gpio_ config(&io_conf); // gpio_ The function of config is to configure GPIO pin parameters
//Initialize GPIO4/5 io_conf.intr_type = GPIO_INTR_POSEDGE;
//Falling edge, rising edge: GPIO_PIN_INTR_POSEDGE io_conf.pin_bit_mask =
GPIO_INPUT_PIN_SEL; // Set GPIO4 as output pin 0 and GPIO5 as output pin 1 as mixed mode io_conf.mode =
GPIO_MODE_INPUT; //GPIO set to input mode_ conf.pull_ up_ en = 1;
//Enable pull-up GPIO_ config(&io_conf); // gpio_ config
The function of is to configure GPIO pin parameters
Method 2:
//Initialize GPIO18/19
gpio_config_t io_conf1 = {
.intr_type = GPIO_INTR_DISABLE, // GPIO interrupt disabled
.mode = GPIO_ MODE_ Output, / / set GPIO to output mode
.pin_bit_mask = GPIO_OUTPUT_PIN_SEL, / / set GPIO18 as output pin 0 and GPIO19 as output pin 1 as mixed mode
.pull_down_en = 0, / / GPIO drop-down is not set
.pull_up_en = 0, / / GPIO pull-up is not set
};
gpio_ config(&io_conf1); // gpio_ The function of config is to configure GPIO pin parameters
//Gpio5 / GPIO initialization_ config_ t io_ conf2 = { io_conf2.intr_type =
GPIO_INTR_POSEDGE, / / falling edge, rising edge: GPIO_PIN_INTR_POSEDGE
io_conf2.pin_bit_mask = GPIO_INPUT_PIN_SEL, / / set GPIO4 as output pin 0 and GPIO5 as output pin 1 as mixed mode
io_ conf2. mode = GPIO_ MODE_ Input, / / set GPIO to input mode
io_conf2.pull_up_en = 1, / / enable pull-up}; gpio_ config(&io_conf2); // gpio_ The function of config is to configure GPIO pin parameters