Page 1 of 1

Incorrect operation of the external interrupt

Posted: Sun Dec 27, 2020 8:04 pm
by Anton_Dru
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.

Re: Incorrect operation of the external interrupt

Posted: Mon Dec 28, 2020 1:02 am
by ESP_Sprite
Can you put an oscilloscope on the signal, just to be sure you're debouncing it correctly?

Re: Incorrect operation of the external interrupt

Posted: Mon Dec 28, 2020 6:58 pm
by Anton_Dru
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

Re: Incorrect operation of the external interrupt

Posted: Mon Dec 28, 2020 7:39 pm
by liebman
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

Re: Incorrect operation of the external interrupt

Posted: Wed Dec 30, 2020 9:17 pm
by Anton_Dru
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.