Chat freely about anything...

User avatar
By JannikJung0
#8536 Hello everybody,
I'm trying to code my own little program for the ESP. I try to use gpio interrupts instead of polling.
Now, when I press the button and pull the gpio low, the interrupt function gets called... but the problem is, it gets called twice. Therefore I tried to disable interrupts in the beginning of the function and reenable it when it's done, but that doesn't fix it.

Here is my code:
Code: Select all#include <ets_sys.h>
#include <osapi.h>
#include <gpio.h>

void interupt_test() {
   ETS_GPIO_INTR_DISABLE(); // Disable gpio interrupts
   //wdt_feed();

   uint32 gpio_status;
   gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
   //clear interrupt status
   GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status);

   ets_uart_printf("GPIO Interrupt!\r\n");
   GPIO_OUTPUT_SET(2, 0);

   ETS_GPIO_INTR_ENABLE(); // Enable gpio interrupts
}

void ICACHE_FLASH_ATTR gpio_init() {
   ETS_GPIO_INTR_DISABLE(); // Disable gpio interrupts
   ETS_GPIO_INTR_ATTACH(interupt_test, 12); // GPIO12 interrupt handler
   PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); // Set GPIO12 function
   gpio_output_set(0, 0, 0, GPIO_ID_PIN(12)); // Set GPIO12 as input
   GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(12)); // Clear GPIO12 status
   gpio_pin_intr_state_set(GPIO_ID_PIN(12), 1); // Interrupt on any GPIO12 edge
   ETS_GPIO_INTR_ENABLE(); // Enable gpio interrupts
   //wdt_feed();
}


Looking at the serial console, I can see the message "GPIO Interrupt!" twice... any ideas how to fix that or what causes this problem ?

Kind regards,
Jannik Jung
User avatar
By krich
#8557 Could be switch bounce. There are a number of hardware and software methods to mitigate it. Lots out there on the internet about it, so pick your poison.

An easy test is to turn off interrupts and insert a significant delay (~100ms+) in the interrupt routine to give the switch time to settle while interrupts are off. Turn interrupts back on before you leave the handler. I don't know much about your application, but I wouldn't recommend this as a permanent solution in an interrupt driven project unless you're exclusively dealing with interrupts on a human to machine interface (us humans are quite slow by comparison). It works well to isolate the issue though. Other better solutions involve a little bit of extra hardware (cap with a bleed resistor) or a more involved software algorithm than described above.

Ken.
User avatar
By JannikJung0
#8586 You were right about the switch debounce. Once I added a small delay, I only got one interrupt called!
The next problem is, that the interrupt gets called once the button is pressed and once again when it is released.
I basically tried all the interupt types available, but I couldn't get it to work, so that i only get one interrupt when the button is pressed down. (I thought that would be the negative edge option).

Basically, I'm trying to setup a tcp server, to control the (GPIOs of the) ESP8266. In addition, I want to be able to control the GPIOs by button presses as well. Would I be better of simply polling the gpios or are interrupts okay for that ?

Kind regards,
Jannik Jung
User avatar
By Fr4gg0r
#8600 Probably button jitter.
When the button is released, it oscillates between closed and open state for a few µs before it stays opened. Easiest solution is adding enough capacitance.