diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c index 6b2f9cc..7907758 100644 --- a/cores/esp32/esp32-hal-gpio.c +++ b/cores/esp32/esp32-hal-gpio.c @@ -72,7 +72,17 @@ const DRAM_ATTR esp32_gpioMux_t esp32_gpioMux[GPIO_PIN_COUNT]={ }; typedef void (*voidFuncPtr)(void); +typedef void (*intFuncPtr)(int pin, int value, void *data); + +struct PinInterruptInfo { + intFuncPtr handler; + void *data; +}; + +static bool interrupt_initialized = false; + static voidFuncPtr __pinInterruptHandlers[GPIO_PIN_COUNT] = {0,}; +static struct PinInterruptInfo __pinInterruptHandlers2[GPIO_PIN_COUNT]; #include "driver/rtc_io.h" @@ -208,7 +218,10 @@ 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(__pinInterruptHandlers2[pin].handler) { + __pinInterruptHandlers2[pin].handler(pin, (GPIO.in >> pin) & 1, + __pinInterruptHandlers2[pin].data); + } else if(__pinInterruptHandlers[pin]) { __pinInterruptHandlers[pin](); } } @@ -218,7 +231,10 @@ static void IRAM_ATTR __onPinInterrupt(void *arg) pin=32; do { if(gpio_intr_status_h & ((uint32_t)1 << (pin - 32))) { - if(__pinInterruptHandlers[pin]) { + if(__pinInterruptHandlers2[pin].handler) { + __pinInterruptHandlers2[pin].handler(pin, (GPIO.in1.val >> pin) & 1, + __pinInterruptHandlers2[pin].data); + } else if(__pinInterruptHandlers[pin]) { __pinInterruptHandlers[pin](); } } @@ -228,7 +244,6 @@ static void IRAM_ATTR __onPinInterrupt(void *arg) extern void __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int intr_type) { - static bool interrupt_initialized = false; static int core_id = 0; if(!interrupt_initialized) { @@ -252,11 +267,39 @@ extern void __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int intr_type) //unlock gpio } +extern void __attachInterrupt2(uint8_t pin, intFuncPtr userFunc, int intr_type, const void *data) +{ + static int core_id = 0; + + if(!interrupt_initialized) { + interrupt_initialized = true; + core_id = xPortGetCoreID(); + ESP_INTR_DISABLE(ETS_GPIO_INUM); + intr_matrix_set(core_id, ETS_GPIO_INTR_SOURCE, ETS_GPIO_INUM); + xt_set_interrupt_handler(ETS_GPIO_INUM, &__onPinInterrupt, NULL); + ESP_INTR_ENABLE(ETS_GPIO_INUM); + } + __pinInterruptHandlers2[pin].handler = userFunc; + __pinInterruptHandlers2[pin].data = data; + //lock gpio + ESP_INTR_DISABLE(ETS_GPIO_INUM); + if(core_id) { //APP_CPU + GPIO.pin[pin].int_ena = 1; + } else { //PRO_CPU + GPIO.pin[pin].int_ena = 4; + } + GPIO.pin[pin].int_type = intr_type; + ESP_INTR_ENABLE(ETS_GPIO_INUM); + //unlock gpio +} + extern void __detachInterrupt(uint8_t pin) { //lock gpio ESP_INTR_DISABLE(ETS_GPIO_INUM); __pinInterruptHandlers[pin] = NULL; + __pinInterruptHandlers2[pin].handler = NULL; + __pinInterruptHandlers2[pin].data = NULL; GPIO.pin[pin].int_ena = 0; GPIO.pin[pin].int_type = 0; ESP_INTR_ENABLE(ETS_GPIO_INUM); @@ -268,5 +311,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 attachInterrupt2(uint8_t pin, intFuncPtr handler, int mode, const void *data) __attribute__ ((weak, alias("__attachInterrupt2"))); 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 b3ccc25..980e921 100644 --- a/cores/esp32/esp32-hal-gpio.h +++ b/cores/esp32/esp32-hal-gpio.h @@ -79,6 +79,8 @@ void digitalWrite(uint8_t pin, uint8_t val); int digitalRead(uint8_t pin); void attachInterrupt(uint8_t pin, void (*)(void), int mode); +void attachInterrupt2(uint8_t pin, void (*)(int pin, int value, void *data), + int mode, const void *data); void detachInterrupt(uint8_t pin); #ifdef __cplusplus diff --git a/variants/feather_esp32/pins_arduino.h b/variants/feather_esp32/pins_arduino.h index f952a83..e0cbcd8 100644 --- a/variants/feather_esp32/pins_arduino.h +++ b/variants/feather_esp32/pins_arduino.h @@ -60,4 +60,7 @@ static const uint8_t T9 = 32; static const uint8_t DAC1 = 25; static const uint8_t DAC2 = 26; +static const uint8_t SD_CHIP_SELECT_PIN = 14; +static const uint8_t SDCARD_SS_PIN = 14; + #endif /* Pins_Arduino_h */