Incorrect operation of the external interrupt

Anton_Dru
Posts: 7
Joined: Sun Dec 27, 2020 7:01 pm

Incorrect operation of the external interrupt

Postby Anton_Dru » Sun Dec 27, 2020 8:04 pm

Hi!
Faced a problem while handling a button click with an external interrupt. I initialized the pin on which the button is located as a falling edge interrupt. However, when the button is pressed, the interrupt is triggered both on the falling edge and on the rising edge. The button is pulled up to power supply through an external 5.1 kΩ resistor and a capacitor is connected in parallel with the button to suppress bounce.
Here is my code:

Code: Select all

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

xTaskHandle Test_Task_Handle;

// interrupt handler function
static void IRAM_ATTR isr_button_pin_heandler(void* arg)
{
  BaseType_t pxHigherPriorityTaskWoken = pdFALSE;

  xTaskNotifyFromISR(Test_Task_Handle, 0, eNoAction, &pxHigherPriorityTaskWoken);
  
  if (pxHigherPriorityTaskWoken == pdTRUE)
    portYIELD_FROM_ISR();
}

void Test_Task(void*);

void app_main(void)
{
  // config interrupt pin
  gpio_config_t button_conf = {
    .intr_type = GPIO_INTR_NEGEDGE,
    .mode = GPIO_MODE_INPUT,
    .pin_bit_mask = (1ULL << GPIO_NUM_14),
    .pull_down_en = 0,
    .pull_up_en = 0,
  };
  gpio_config(&button_conf);
  gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1);
  gpio_isr_handler_add(GPIO_NUM_14, isr_button_pin_heandler, (void*)GPIO_NUM_14);
  
  // config LED pin
  gpio_config_t led_pin_conf = {
    .intr_type = GPIO_INTR_DISABLE,
    .mode = GPIO_MODE_OUTPUT,
    .pin_bit_mask = (1ULL << GPIO_NUM_23),
    .pull_down_en = 0,
    .pull_up_en = 0,
  };
  gpio_config(&led_pin_conf);
  
  xTaskCreate(Test_Task, "Test_Task", 5000, NULL, 1, &Test_Task_Handle);

}

void Test_Task(void* pvParameters)
{
  bool flag = false;
  for (;;)
  {
    xTaskNotifyWait(0x00, LONG_MAX, NULL, portMAX_DELAY);
    flag = !flag;
    gpio_set_level(GPIO_NUM_23, flag);
  }
}

In this case, if I remove the button and connect the square-wave generator, the interrupt is triggered as expected. Help me figure out what the problem is.

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: Incorrect operation of the external interrupt

Postby ESP_Sprite » Mon Dec 28, 2020 1:02 am

Can you put an oscilloscope on the signal, just to be sure you're debouncing it correctly?

Anton_Dru
Posts: 7
Joined: Sun Dec 27, 2020 7:01 pm

Re: Incorrect operation of the external interrupt

Postby Anton_Dru » Mon Dec 28, 2020 6:58 pm

ESP_Sprite wrote:
Mon Dec 28, 2020 1:02 am
Can you put an oscilloscope on the signal, just to be sure you're debouncing it correctly?
I watched the signal on the oscilloscope, no bounce is observed.
In general, the problem is not like bouncing, since if bouncing, there would be repeated operation either when pressed or released. And in this situation, triggering occurs when you press and release, that is, as if the interrupts were configured as GPIO_INTR_ANYEDGE. Although in fact the interrupt is configured as GPIO_INTR_NEGEDGE

liebman
Posts: 18
Joined: Wed Dec 09, 2020 7:03 pm

Re: Incorrect operation of the external interrupt

Postby liebman » Mon Dec 28, 2020 7:39 pm

IIRC edge interrupts have some issues if the rise time is not fast enough. This github issue has a description: https://github.com/espressif/arduino-esp32/issues/4172

Anton_Dru
Posts: 7
Joined: Sun Dec 27, 2020 7:01 pm

Re: Incorrect operation of the external interrupt

Postby Anton_Dru » Wed Dec 30, 2020 9:17 pm

liebman wrote:
Mon Dec 28, 2020 7:39 pm
IIRC edge interrupts have some issues if the rise time is not fast enough. This github issue has a description: https://github.com/espressif/arduino-esp32/issues/4172
Thanks for the answer! This article fully confirms my observations made with the oscilloscope when I checked if there is any bounce from the button.

Who is online

Users browsing this forum: dzungpv and 64 guests