GPIO interrupt handler not firing on ESP32-C6

thed1m0
Posts: 2
Joined: Wed Feb 26, 2025 7:41 pm

GPIO interrupt handler not firing on ESP32-C6

Postby thed1m0 » Wed Feb 26, 2025 7:55 pm

I wired up a simple pull-up circuit with a button and a resistor to manually toggle the value of a pin. I wanted to see an edge interrupt fire. However the interrupt is not firing, even though I *see* the value of the pin changing. Here is my code:

Code: Select all


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "driver/gpio.h"
#include "sys/time.h"

#define BAUD_RATE 9600
#define INTERVAL_MICROSECOND 1000000 / 9600
#define MASTER_TX_PIN 4

volatile bool called = false;

static void IRAM_ATTR gpio_isr_handler(void* arg)
{
    called=true;
}

void setup_gpio_intr() {
    esp_err_t err;

    err = gpio_install_isr_service(0);

    if (err != ESP_OK) {
        printf("Failed gpio_install_isr_service: %s\n", esp_err_to_name(err));
    }

    gpio_config_t io_conf = {};
    io_conf.intr_type = GPIO_INTR_ANYEDGE;
    io_conf.mode = GPIO_MODE_INPUT;
    io_conf.pin_bit_mask = MASTER_TX_PIN;
    io_conf.pull_down_en = 0;
    io_conf.pull_up_en = 0;
    err = gpio_config(&io_conf);

    if (err != ESP_OK) {
        printf("Failed gpio_config: %s\n", esp_err_to_name(err));
    }


    err = gpio_isr_handler_add(MASTER_TX_PIN, gpio_isr_handler, NULL);

    if (err != ESP_OK) {
        printf("Failed gpio_isr_handler_add: %s\n", esp_err_to_name(err));
    }

    printf("GPIO interrupt config done\n");
}

void watch_interrupts(void *pvParameters) {
    while(1) {
        if (called) {
            printf("Interrupt handler called!");
        }

        printf("cur lvl %d\n", gpio_get_level(MASTER_TX_PIN));

        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

void app_main(void)
{
    setup_gpio_intr();                                                    
    watch_interrupts(NULL);
}
When I run the code I see the following output (with me pressing and releasing the button):

Code: Select all

I (250) main_task: Started on CPU0
I (250) main_task: Calling app_main()
I (250) gpio: GPIO[2]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:3 
GPIO interrupt config done
cur lvl 1
cur lvl 1
cur lvl 0
cur lvl 0
cur lvl 1
cur lvl 1
cur lvl 0
cur lvl 0
cur lvl 1
cur lvl 1
Notice that we:

1. See the value of the pin change
2. But never print "Interrupt handler called", which means that the "called" volatile bool was never set. It is set in the interrupt handler.
3. All the gpio setup functions returned success

The board in question in ESP32-Dev-C6-1 (chip is ESP32-C6-WROOM-1). Using esp-idf v5.4.0 inside VS-Code.

Note: I've also tried several different combinations of ESP_INTR_FLAG_EDGE, ESP_INTR_FLAG_LOWMED and ESP_INTR_FLAG_IRAM as flags to pass to gpio_install_isr_service and none worked...

Thanks!

thed1m0
Posts: 2
Joined: Wed Feb 26, 2025 7:41 pm

Re: GPIO interrupt handler not firing on ESP32-C6

Postby thed1m0 » Thu Feb 27, 2025 2:20 am

Nevermind... I found my silly mistake. I passed a number instead of bitmask to `io_conf.pin_bit_mask`. *Facepalm*

boarchuz
Posts: 656
Joined: Tue Aug 21, 2018 5:28 am

Re: GPIO interrupt handler not firing on ESP32-C6

Postby boarchuz » Thu Feb 27, 2025 2:23 am

gpio_config_t::pin_bit_mask is a bit mask. For GPIO4, you need to set bit 4, not a value of 4.

Who is online

Users browsing this forum: No registered users and 5 guests