Page 1 of 1

GPIO interrupt not working

Posted: Thu Jan 25, 2024 11:45 am
by HEckardt
Dear ESP-Forum Members,

for my project I need a GPIO interrupt (pressed botton). I selected the GPIO25 pin connect it though the switch to the ground. The negative edge should start the isr. Message from the GPIO configurator:
I (2283) gpio: GPIO[25]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:2

I adapted the ESP-example-code for my needs:

Code: Select all

#define GPIO_OK_Button 25  
#define ESP_INTR_FLAG_DEFAULT 0

static const char *TAG = "HE_Ctrl";

static bool ok_pressed;

static QueueHandle_t gpio_evt_queue = NULL;

static void IRAM_ATTR gpio_isr_handler(void* arg)
{
	ESP_LOGI(TAG, "gpio_isr_before queuesend");
	ESP_LOGI(TAG, "isr_arg: %lX", (uint32_t) arg);
    uint32_t gpio_num = (uint32_t) arg;
    xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
	ESP_LOGI(TAG, "gpio_isr_after queuesend");

}

static void OK_task(void* arg)
{
    uint32_t io_num;
    for(;;) {
        if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
			vTaskDelay(100 / portTICK_PERIOD_MS);
			printf("GPIO[%"PRIu32"] intr, val: %d\n", io_num, gpio_get_level(io_num));
			ok_pressed = true;
        }
    }
}

static void init_ok_button(void)
{
	ok_pressed = false;
    //zero-initialize the config structure.
    gpio_config_t io_conf = {};
    io_conf.intr_type = GPIO_INTR_NEGEDGE;
    io_conf.pin_bit_mask = 1ULL<<GPIO_OK_Button;
    io_conf.mode = GPIO_MODE_INPUT;
    io_conf.pull_up_en = 1;
    gpio_config(&io_conf);


	ESP_LOGI(TAG, "BitMask: %LX", io_conf.pin_bit_mask);

	
    // create a queue to handle gpio event from isr
    gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
    // start gpio task
    xTaskCreate(OK_task, "gpio_task_example", 2048, NULL, 10, NULL);

    //install gpio isr service
    gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
    //hook isr handler for specific gpio pin
    gpio_isr_handler_add(GPIO_OK_Button, gpio_isr_handler, (void*) GPIO_OK_Button);


    printf("Minimum free heap size: %"PRIu32" bytes\n", esp_get_minimum_free_heap_size());
	

	ESP_LOGI(TAG, "gpio_initialized");

}

But when I press the switch to trigger the isr, the ESP32 is restarting and the monitor get this printout:

abort() was called at PC 0x40083043 on core 0


Backtrace: 0x40081ac6:0x3ffb1770 0x40089d6d:0x3ffb1790 0x40091686:0x3ffb17b0 0x40083043:0x3ffb1820 0x40083181:0x3ffb1850 0x400831fa:0x3ffb1870 0x4014a23a:0x3ffb18a0 0x4014d3a9:0x3ffb1bc0 0x401597d1:0x3ffb1bf0 0x4009004d:0x3ffb1c20 0x4008379d:0x3ffb1c70 0x400838b2:0x3ffb1ca0 0x40083942:0x3ffb1cd0 0x40082e35:0x3ffb1d00 0x40088b3f:0x3ffbcd00 0x400d370b:0x3ffbcd20 0x4008af41:0x3ffbcd40 0x4008c74d:0x3ffbcd60




ELF file SHA256: 43476a0f78228a12

Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)

I added some ESP_LOGs in the isr. These outputs are not visible, so the problem accurs befor the isr is starting.

I have no idea what could be wrong. I assume it could be a storage problem.

I attached the complete monitor printout. Please help me to interprete the error message to find the root cause for the problem.

Thank you for your support.

Greetings
Henry

Re: GPIO interrupt not working

Posted: Thu Jan 25, 2024 12:45 pm
by mikemoy
remove the ESP_LOGI's from the ISR handler and see what happens.

Re: GPIO interrupt not working

Posted: Thu Jan 25, 2024 5:21 pm
by HEckardt
Hi mikemoy,

it is the same. I inserted the LOG output, when I recognized, that the isr failed.

greetings
Henry

Re: GPIO interrupt not working WTD Reset

Posted: Sun Jul 28, 2024 9:53 pm
by h2ggabrielvicente
I have a problem when trying to configure an interrupt on GPIO 0, and I always get a wtd reset, as follows: Minimum free heap size: 298252 bytes
cnt: 0
cnt: 1
Guru Meditation Error: Core 0 panic'ed (Interrupt wdt timeout on CPU0).

Core 0 register dump:
PC : 0x40082943 PS : 0x00050034 A0 : 0x40084666 A1 : 0x3ffb0d60, I figured I was doing something wrong in my application and I tested the example already included in the IDF and the problem persists, is anyone else experiencing this problem? I've already wasted a few hours trying to solve it but without success.
ESP-IDF: v5.1.2-dirty
  1. /* GPIO Example
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <inttypes.h>
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/task.h"
  15. #include "freertos/queue.h"
  16. #include "driver/gpio.h"
  17.  
  18. /**
  19.  * Brief:
  20.  * This test code shows how to configure gpio and how to use gpio interrupt.
  21.  *
  22.  * GPIO status:
  23.  * GPIO18: output (ESP32C2/ESP32H2 uses GPIO8 as the second output pin)
  24.  * GPIO19: output (ESP32C2/ESP32H2 uses GPIO9 as the second output pin)
  25.  * GPIO4:  input, pulled up, interrupt from rising edge and falling edge
  26.  * GPIO5:  input, pulled up, interrupt from rising edge.
  27.  *
  28.  * Note. These are the default GPIO pins to be used in the example. You can
  29.  * change IO pins in menuconfig.
  30.  *
  31.  * Test:
  32.  * Connect GPIO18(8) with GPIO4
  33.  * Connect GPIO19(9) with GPIO5
  34.  * Generate pulses on GPIO18(8)/19(9), that triggers interrupt on GPIO4/5
  35.  *
  36.  */
  37.  
  38. #define GPIO_OUTPUT_IO_0    CONFIG_GPIO_OUTPUT_0
  39. #define GPIO_OUTPUT_IO_1    CONFIG_GPIO_OUTPUT_1
  40. #define GPIO_OUTPUT_PIN_SEL  ((1ULL<<GPIO_OUTPUT_IO_0) | (1ULL<<GPIO_OUTPUT_IO_1))
  41. /*
  42.  * Let's say, GPIO_OUTPUT_IO_0=18, GPIO_OUTPUT_IO_1=19
  43.  * In binary representation,
  44.  * 1ULL<<GPIO_OUTPUT_IO_0 is equal to 0000000000000000000001000000000000000000 and
  45.  * 1ULL<<GPIO_OUTPUT_IO_1 is equal to 0000000000000000000010000000000000000000
  46.  * GPIO_OUTPUT_PIN_SEL                0000000000000000000011000000000000000000
  47.  * */
  48. #define GPIO_INPUT_IO_0     CONFIG_GPIO_INPUT_0
  49. #define GPIO_INPUT_IO_1     CONFIG_GPIO_INPUT_1
  50. #define GPIO_INPUT_PIN_SEL  ((1ULL<<GPIO_INPUT_IO_0) | (1ULL<<GPIO_INPUT_IO_1))
  51. /*
  52.  * Let's say, GPIO_INPUT_IO_0=4, GPIO_INPUT_IO_1=5
  53.  * In binary representation,
  54.  * 1ULL<<GPIO_INPUT_IO_0 is equal to 0000000000000000000000000000000000010000 and
  55.  * 1ULL<<GPIO_INPUT_IO_1 is equal to 0000000000000000000000000000000000100000
  56.  * GPIO_INPUT_PIN_SEL                0000000000000000000000000000000000110000
  57.  * */
  58. #define ESP_INTR_FLAG_DEFAULT 0
  59.  
  60. static QueueHandle_t gpio_evt_queue = NULL;
  61.  
  62. static void IRAM_ATTR gpio_isr_handler(void* arg)
  63. {
  64.     uint32_t gpio_num = (uint32_t) arg;
  65.     xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
  66. }
  67.  
  68. static void gpio_task_example(void* arg)
  69. {
  70.     uint32_t io_num;
  71.     for(;;) {
  72.         if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
  73.             printf("GPIO[%"PRIu32"] intr, val: %d\n", io_num, gpio_get_level(io_num));
  74.         }
  75.     }
  76. }
  77.  
  78. void app_main(void)
  79. {
  80.     //zero-initialize the config structure.
  81.     gpio_config_t io_conf = {};
  82.     //disable interrupt
  83.     io_conf.intr_type = GPIO_INTR_DISABLE;
  84.     //set as output mode
  85.     io_conf.mode = GPIO_MODE_OUTPUT;
  86.     //bit mask of the pins that you want to set,e.g.GPIO18/19
  87.     io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
  88.     //disable pull-down mode
  89.     io_conf.pull_down_en = 0;
  90.     //disable pull-up mode
  91.     io_conf.pull_up_en = 0;
  92.     //configure GPIO with the given settings
  93.     gpio_config(&io_conf);
  94.  
  95.     //interrupt of rising edge
  96.     io_conf.intr_type = GPIO_INTR_POSEDGE;
  97.     //bit mask of the pins, use GPIO4/5 here
  98.     io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
  99.     //set as input mode
  100.     io_conf.mode = GPIO_MODE_INPUT;
  101.     //enable pull-up mode
  102.     io_conf.pull_up_en = 1;
  103.     gpio_config(&io_conf);
  104.  
  105.     //change gpio interrupt type for one pin
  106.     gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);
  107.  
  108.     //create a queue to handle gpio event from isr
  109.     gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
  110.     //start gpio task
  111.     xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
  112.  
  113.     //install gpio isr service
  114.     gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
  115.     //hook isr handler for specific gpio pin
  116.     gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  117.     //hook isr handler for specific gpio pin
  118.     gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void*) GPIO_INPUT_IO_1);
  119.  
  120.     //remove isr handler for gpio number.
  121.     gpio_isr_handler_remove(GPIO_INPUT_IO_0);
  122.     //hook isr handler for specific gpio pin again
  123.     gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
  124.  
  125.     printf("Minimum free heap size: %"PRIu32" bytes\n", esp_get_minimum_free_heap_size());
  126.  
  127.     int cnt = 0;
  128.     while(1) {
  129.         printf("cnt: %d\n", cnt++);
  130.         vTaskDelay(1000 / portTICK_PERIOD_MS);
  131.         gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2);
  132.         gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2);
  133.     }
  134. }

Re: GPIO interrupt not working

Posted: Mon Jul 29, 2024 2:13 am
by ESP_Sprite
Install the ISR service *before* you configure the GPIOs.

Re: GPIO interrupt not working

Posted: Tue Aug 06, 2024 10:25 am
by h2ggabrielvicente
ESP_Sprite wrote:
Mon Jul 29, 2024 2:13 am
Install the ISR service *before* you configure the GPIOs.
It still doesn't work, I will return to the previous version of esp-idf, at first even the example code doesn't work causing wtd reset.

Re: GPIO interrupt not working

Posted: Thu Aug 08, 2024 11:18 pm
by h2ggabrielvicente
h2ggabrielvicente wrote:
Tue Aug 06, 2024 10:25 am
ESP_Sprite wrote:
Mon Jul 29, 2024 2:13 am
Install the ISR service *before* you configure the GPIOs.
It still doesn't work, I will return to the previous version of esp-idf, at first even the example code doesn't work causing wtd reset.
Update: I installed a new version of esp-idf and the problem was solved, I believe my installation was faulty.