From beb115a4083fe0b022884ecb6633849708f15c79 Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Tue, 26 Jun 2018 20:50:58 +0200 Subject: [PATCH 1/8] Allow using argument with attachInterrupt --- cores/esp32/esp32-hal-gpio.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c index 7c5e02bd47f..bb86b91c3a3 100644 --- a/cores/esp32/esp32-hal-gpio.c +++ b/cores/esp32/esp32-hal-gpio.c @@ -70,6 +70,7 @@ const DRAM_ATTR esp32_gpioMux_t esp32_gpioMux[GPIO_PIN_COUNT]={ }; typedef void (*voidFuncPtr)(void); +typedef void (*voidFuncPtrArg)(void*); static voidFuncPtr __pinInterruptHandlers[GPIO_PIN_COUNT] = {0,}; #include "driver/rtc_io.h" @@ -208,7 +209,11 @@ static void IRAM_ATTR __onPinInterrupt(void *arg) do { if(gpio_intr_status_l & ((uint32_t)1 << pin)) { if(__pinInterruptHandlers[pin]) { - __pinInterruptHandlers[pin](); + if(arg){ + ((voidFuncPtrArg)__pinInterruptHandlers[pin])(arg); + } else { + __pinInterruptHandlers[pin](); + } } } } while(++pin<32); @@ -218,22 +223,26 @@ static void IRAM_ATTR __onPinInterrupt(void *arg) do { if(gpio_intr_status_h & ((uint32_t)1 << (pin - 32))) { if(__pinInterruptHandlers[pin]) { - __pinInterruptHandlers[pin](); + if(arg){ + ((voidFuncPtrArg)__pinInterruptHandlers[pin])(arg); + } else { + __pinInterruptHandlers[pin](); + } } } } while(++pin Date: Wed, 27 Jun 2018 08:55:22 +0200 Subject: [PATCH 2/8] formatting replace tabs with spaces --- cores/esp32/esp32-hal-gpio.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c index bb86b91c3a3..194e29753b4 100644 --- a/cores/esp32/esp32-hal-gpio.c +++ b/cores/esp32/esp32-hal-gpio.c @@ -209,11 +209,11 @@ static void IRAM_ATTR __onPinInterrupt(void *arg) do { if(gpio_intr_status_l & ((uint32_t)1 << pin)) { if(__pinInterruptHandlers[pin]) { - if(arg){ - ((voidFuncPtrArg)__pinInterruptHandlers[pin])(arg); - } else { - __pinInterruptHandlers[pin](); - } + if(arg){ + ((voidFuncPtrArg)__pinInterruptHandlers[pin])(arg); + } else { + __pinInterruptHandlers[pin](); + } } } } while(++pin<32); @@ -223,11 +223,11 @@ static void IRAM_ATTR __onPinInterrupt(void *arg) do { if(gpio_intr_status_h & ((uint32_t)1 << (pin - 32))) { if(__pinInterruptHandlers[pin]) { - if(arg){ - ((voidFuncPtrArg)__pinInterruptHandlers[pin])(arg); - } else { - __pinInterruptHandlers[pin](); - } + if(arg){ + ((voidFuncPtrArg)__pinInterruptHandlers[pin])(arg); + } else { + __pinInterruptHandlers[pin](); + } } } } while(++pin Date: Fri, 29 Jun 2018 20:42:59 +0200 Subject: [PATCH 3/8] fix bug more then 1 interrupt --- cores/esp32/esp32-hal-gpio.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c index 194e29753b4..e179be25e87 100644 --- a/cores/esp32/esp32-hal-gpio.c +++ b/cores/esp32/esp32-hal-gpio.c @@ -71,7 +71,11 @@ const DRAM_ATTR esp32_gpioMux_t esp32_gpioMux[GPIO_PIN_COUNT]={ typedef void (*voidFuncPtr)(void); typedef void (*voidFuncPtrArg)(void*); -static voidFuncPtr __pinInterruptHandlers[GPIO_PIN_COUNT] = {0,}; +typedef struct { + voidFuncPtr fn; + void* arg; +} InterruptHandle_t; +static InterruptHandle_t __pinInterruptHandlers[GPIO_PIN_COUNT] = {0,}; #include "driver/rtc_io.h" @@ -194,7 +198,7 @@ extern int IRAM_ATTR __digitalRead(uint8_t pin) static intr_handle_t gpio_intr_handle = NULL; -static void IRAM_ATTR __onPinInterrupt(void *arg) +static void IRAM_ATTR __onPinInterrupt() { uint32_t gpio_intr_status_l=0; uint32_t gpio_intr_status_h=0; @@ -208,11 +212,11 @@ static void IRAM_ATTR __onPinInterrupt(void *arg) if(gpio_intr_status_l) { do { if(gpio_intr_status_l & ((uint32_t)1 << pin)) { - if(__pinInterruptHandlers[pin]) { - if(arg){ - ((voidFuncPtrArg)__pinInterruptHandlers[pin])(arg); + if(__pinInterruptHandlers[pin].fn) { + if(__pinInterruptHandlers[pin].arg){ + ((voidFuncPtrArg)__pinInterruptHandlers[pin].fn)(__pinInterruptHandlers[pin].arg); } else { - __pinInterruptHandlers[pin](); + __pinInterruptHandlers[pin].fn(); } } } @@ -222,11 +226,11 @@ static void IRAM_ATTR __onPinInterrupt(void *arg) pin=32; do { if(gpio_intr_status_h & ((uint32_t)1 << (pin - 32))) { - if(__pinInterruptHandlers[pin]) { + if(__pinInterruptHandlers[pin].fn) { if(arg){ - ((voidFuncPtrArg)__pinInterruptHandlers[pin])(arg); + ((voidFuncPtrArg)__pinInterruptHandlers[pin].fn)(__pinInterruptHandlers[pin].arg); } else { - __pinInterruptHandlers[pin](); + __pinInterruptHandlers[pin].fn(); } } } @@ -240,9 +244,10 @@ extern void __attachInterruptArg(uint8_t pin, voidFuncPtrArg userFunc, void * ar if(!interrupt_initialized) { interrupt_initialized = true; - esp_intr_alloc(ETS_GPIO_INTR_SOURCE, (int)ESP_INTR_FLAG_IRAM, __onPinInterrupt, arg, &gpio_intr_handle); + esp_intr_alloc(ETS_GPIO_INTR_SOURCE, (int)ESP_INTR_FLAG_IRAM, __onPinInterrupt, NULL, &gpio_intr_handle); } - __pinInterruptHandlers[pin] = (voidFuncPtr)userFunc; + __pinInterruptHandlers[pin].fn = (voidFuncPtr)userFunc; + __pinInterruptHandlers[pin].arg = arg; esp_intr_disable(gpio_intr_handle); if(esp_intr_get_cpu(gpio_intr_handle)) { //APP_CPU GPIO.pin[pin].int_ena = 1; @@ -260,7 +265,8 @@ extern void __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int intr_type) extern void __detachInterrupt(uint8_t pin) { esp_intr_disable(gpio_intr_handle); - __pinInterruptHandlers[pin] = NULL; + __pinInterruptHandlers[pin].fn = NULL; + __pinInterruptHandlers[pin].arg = NULL; GPIO.pin[pin].int_ena = 0; GPIO.pin[pin].int_type = 0; esp_intr_enable(gpio_intr_handle); From 1e79a6fe7a507f50f81849203b19bea255bc0cea Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Fri, 29 Jun 2018 20:54:16 +0200 Subject: [PATCH 4/8] leftover --- cores/esp32/esp32-hal-gpio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c index e179be25e87..04a095615dc 100644 --- a/cores/esp32/esp32-hal-gpio.c +++ b/cores/esp32/esp32-hal-gpio.c @@ -227,7 +227,7 @@ static void IRAM_ATTR __onPinInterrupt() do { if(gpio_intr_status_h & ((uint32_t)1 << (pin - 32))) { if(__pinInterruptHandlers[pin].fn) { - if(arg){ + if(__pinInterruptHandlers[pin].arg){ ((voidFuncPtrArg)__pinInterruptHandlers[pin].fn)(__pinInterruptHandlers[pin].arg); } else { __pinInterruptHandlers[pin].fn(); From ac45eb977fd066c23c8a3ea9d3856f9f63616e2f Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Tue, 3 Jul 2018 23:01:03 +0200 Subject: [PATCH 5/8] add example --- .../examples/GPIOInterrupt/GPIOInterrupt.ino | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino diff --git a/libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino b/libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino new file mode 100644 index 00000000000..88e422ece28 --- /dev/null +++ b/libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino @@ -0,0 +1,57 @@ +#include + +/* + +The next three lines are only needed when using an argument on attachInterrupt. +This makes the core's internal function available for use in the sketch. + +*/ +typedef void (*voidFuncPtrArg)(void*); +extern "C" { + void __attachInterruptArg(uint8_t pin, voidFuncPtrArg handler, void* arg, int mode); +} + + +struct Button { + const uint8_t PIN; + uint32_t numberKeyPresses; + bool pressed; +}; + +Button button1 = {23, 0, false}; +Button button2 = {18, 0, false}; + +void IRAM_ATTR isr(void* arg) { + Button* s = static_cast(arg); + s->numberKeyPresses += 1; + s->pressed = true; +} + +void IRAM_ATTR isr() { + button2.numberKeyPresses += 1; + button2.pressed = true; +} + +void setup() { + Serial.begin(115200); + pinMode(button1.PIN, INPUT_PULLUP); + __attachInterruptArg(button1.PIN, isr, &button1, FALLING); + pinMode(button2.PIN, INPUT_PULLUP); + attachInterrupt(button2.PIN, isr, FALLING); +} + +void loop() { + if (button1.pressed) { + Serial.printf("Button 1 has been pressed %u times\n", button1.numberKeyPresses); + button1.pressed = false; + } + if (button2.pressed) { + Serial.printf("Button 2 has been pressed %u times\n", button2.numberKeyPresses); + button2.pressed = false; + } + static uint32_t lastMillis = 0; + if (millis() - lastMillis > 10000) { + lastMillis = millis(); + detachInterrupt(button1.PIN); + } +} From bcda7376ab1ec843ba9499bd71747ab982e28bcf Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Wed, 4 Jul 2018 20:49:41 +0200 Subject: [PATCH 6/8] make attachInterruptArg public --- cores/esp32/esp32-hal-gpio.c | 1 + cores/esp32/esp32-hal-gpio.h | 1 + 2 files changed, 2 insertions(+) diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c index 04a095615dc..795dba2b7ab 100644 --- a/cores/esp32/esp32-hal-gpio.c +++ b/cores/esp32/esp32-hal-gpio.c @@ -277,5 +277,6 @@ extern void pinMode(uint8_t pin, uint8_t mode) __attribute__ ((weak, alias("__pi extern void digitalWrite(uint8_t pin, uint8_t val) __attribute__ ((weak, alias("__digitalWrite"))); extern int digitalRead(uint8_t pin) __attribute__ ((weak, alias("__digitalRead"))); extern void attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode) __attribute__ ((weak, alias("__attachInterrupt"))); +extern void attachInterruptArg(uint8_t pin, voidFuncPtr handler, void * arg, int mode) __attribute__ ((weak, alias("__attachInterruptArg"))); extern void detachInterrupt(uint8_t pin) __attribute__ ((weak, alias("__detachInterrupt"))); diff --git a/cores/esp32/esp32-hal-gpio.h b/cores/esp32/esp32-hal-gpio.h index b3ccc25239d..977bfe8f5e3 100644 --- a/cores/esp32/esp32-hal-gpio.h +++ b/cores/esp32/esp32-hal-gpio.h @@ -79,6 +79,7 @@ void digitalWrite(uint8_t pin, uint8_t val); int digitalRead(uint8_t pin); void attachInterrupt(uint8_t pin, void (*)(void), int mode); +void attachInterruptArg(uint8_t pin, void (*)(void), void * arg, int mode); void detachInterrupt(uint8_t pin); #ifdef __cplusplus From f806244b83fd2182b7414268617526dfdd8e82de Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Wed, 4 Jul 2018 20:51:50 +0200 Subject: [PATCH 7/8] update example --- .../ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino b/libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino index 88e422ece28..c866b22e608 100644 --- a/libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino +++ b/libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino @@ -1,17 +1,5 @@ #include -/* - -The next three lines are only needed when using an argument on attachInterrupt. -This makes the core's internal function available for use in the sketch. - -*/ -typedef void (*voidFuncPtrArg)(void*); -extern "C" { - void __attachInterruptArg(uint8_t pin, voidFuncPtrArg handler, void* arg, int mode); -} - - struct Button { const uint8_t PIN; uint32_t numberKeyPresses; From d56548a4af9f69b2896a72549b4a7c1f0cb48c1a Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Wed, 4 Jul 2018 21:16:11 +0200 Subject: [PATCH 8/8] leftover --- libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino b/libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino index c866b22e608..c3d1245fa0c 100644 --- a/libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino +++ b/libraries/ESP32/examples/GPIOInterrupt/GPIOInterrupt.ino @@ -23,7 +23,7 @@ void IRAM_ATTR isr() { void setup() { Serial.begin(115200); pinMode(button1.PIN, INPUT_PULLUP); - __attachInterruptArg(button1.PIN, isr, &button1, FALLING); + attachInterruptArg(button1.PIN, isr, &button1, FALLING); pinMode(button2.PIN, INPUT_PULLUP); attachInterrupt(button2.PIN, isr, FALLING); }