From a61af52d96fa0b511c097d1fa62fd60c1b2fd9a8 Mon Sep 17 00:00:00 2001 From: UncleRus Date: Wed, 30 Nov 2016 00:58:24 +0500 Subject: [PATCH] More convenient GPIO interrupt handlers --- core/esp_gpio.c | 31 +++++++++++++++ core/esp_gpio_interrupts.c | 80 -------------------------------------- core/include/esp/gpio.h | 11 +----- examples/button/button.c | 7 ++-- 4 files changed, 37 insertions(+), 92 deletions(-) delete mode 100644 core/esp_gpio_interrupts.c diff --git a/core/esp_gpio.c b/core/esp_gpio.c index f6db1ef..bb8987f 100644 --- a/core/esp_gpio.c +++ b/core/esp_gpio.c @@ -56,3 +56,34 @@ void gpio_set_pullup(uint8_t gpio_num, bool enabled, bool enabled_during_sleep) iomux_set_pullup_flags(gpio_to_iomux(gpio_num), flags); } +static gpio_interrupt_handler_t gpio_interrupt_handlers[16] = { 0 }; + +static void IRAM _gpio_intr_internal_handler(void) +{ + uint32_t status_reg = GPIO.STATUS; + GPIO.STATUS_CLEAR = status_reg; + + uint8_t gpio_idx; + while((gpio_idx = __builtin_ffs(status_reg))) + { + gpio_idx--; + status_reg &= ~BIT(gpio_idx); + if(FIELD2VAL(GPIO_CONF_INTTYPE, GPIO.CONF[gpio_idx])) { + gpio_interrupt_handler_t handler = gpio_interrupt_handlers[gpio_idx]; + if (handler) { + handler(gpio_idx); + } + } + } +} + +void gpio_set_interrupt(const uint8_t gpio_num, const gpio_inttype_t int_type, gpio_interrupt_handler_t handler) +{ + gpio_interrupt_handlers[gpio_num] = handler; + + GPIO.CONF[gpio_num] = SET_FIELD(GPIO.CONF[gpio_num], GPIO_CONF_INTTYPE, int_type); + if(int_type != GPIO_INTTYPE_NONE && handler) { + _xt_isr_attach(INUM_GPIO, _gpio_intr_internal_handler); + _xt_isr_unmask(1<