-->
Page 1 of 1

Reading GPIO from rising edge ISR

PostPosted: Thu Mar 14, 2019 4:15 pm
by hsd
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?

Re: Reading GPIO from rising edge ISR

PostPosted: Tue Mar 19, 2019 8:21 am
by eriksl
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.

Re: Reading GPIO from rising edge ISR

PostPosted: Tue Mar 19, 2019 10:21 am
by btidey
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.

Re: Reading GPIO from rising edge ISR

PostPosted: Thu Mar 21, 2019 10:48 am
by eriksl
My own experience is that explicitly requesting rising or falling edge for GPIO's only works on GPIO 4 and GPIO 5. The others always trigger, regardless.