Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By hsd
#81126 I have a GPIO ISR configured on GPIO_INTR_ANYEDGE.
I want to know in the ISR if it's rising or falling edge.
How to do that?

If I just read the GPIO status in the ISR, it's almost always 0, even on a rising edge.

Example:

Code: Select all#define PIN GPIO_NUM_5

   gpio_set_intr_type(PIN, GPIO_INTR_POSEDGE);   // <<<< Rising edge only!
   gpio_set_direction(PIN, GPIO_MODE_INPUT);
   gpio_pullup_en(PIN);
   gpio_isr_handler_add(PIN, gpio_sda_handler, (void *) PIN);

static void gpio_handler(void *arg)
{
   uint32_t x = gpio_get_level(PIN);
   xQueueSendFromISR(gpio_evt_queue, &x, NULL);
}

static void gpio_task(void *arg)
{
   uint32_t x;

   for (;;) {
      if (xQueueReceive(gpio_evt_queue, &x, portMAX_DELAY)) {
         ESP_LOGI(TAG, "GPIO[%d] intr, val: %d\n", PIN, x & 1);
      }
   }
}


Output:

Code: Select allI (32183) i2c-tools: GPIO[5] intr, val: 0
I (32183) i2c-tools: GPIO[5] intr, val: 0
I (32193) i2c-tools: GPIO[5] intr, val: 1
I (32193) i2c-tools: GPIO[5] intr, val: 0
I (32203) i2c-tools: GPIO[5] intr, val: 0


Do I need some kind of delay? If so, how much? Is it OK to delay in the ISR?
User avatar
By eriksl
#81205 You can use the relevant other "EDGE" values when configuring the PIN INT. But mostly it doesn't work I found, only on a few pins.

Does the interrupt signal only go high for a very short time? That's always tricky, the period can be too short for even the PIN INT mechanism to notice.
User avatar
By btidey
#81209 If the pulse is long enough then reading the GPIO during the service routine will tell you the current state and therefore which was the last edge.

If the pulse is so short that it has reverted before the service routine can read it, then knowing whether it was the rising or falling edge is a bit moot anyway.

I haven't had any trouble with using FALLING or RISING conditions on a variety of GPIO pins but haven't comprehensively tested all of them.