From 3e4e6f9bac08a414a4b589fe0238f43ed9d80b4d Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 6 Nov 2018 15:11:23 +0700 Subject: [PATCH] Added ISR_DEFERRED option for attachInterrupt() to defer callback from ISR context --- changelog.md | 4 +- cores/nRF5/WInterrupts.c | 14 +++++- cores/nRF5/WInterrupts.h | 2 + keywords.txt | 1 + .../digital_interrupt_deferred.ino | 46 +++++++++++++++++++ 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 libraries/Bluefruit52Lib/examples/Hardware/digital_interrupt_deferred/digital_interrupt_deferred.ino diff --git a/changelog.md b/changelog.md index 86622fe3..82700ff1 100644 --- a/changelog.md +++ b/changelog.md @@ -13,7 +13,9 @@ - InternalFS use LittleFS to manage internal flash for bonding and other user data - ExternalFS use fatfs to manage external spi flash (nrf52840 only) for usb msc. - Replace cpritnf by std printf -- Add HwPWM3 for nRF52840 +- Added HwPWM3 for nRF52840 +- Added ISR_DEFERRED for attachInterrupt() for deferring callback from ISR context + - Added digital_interrupt_deferred sketch for demo ## 0.8.6 diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index 657630e4..6982c049 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -31,6 +31,7 @@ #endif static voidFuncPtr callbacksInt[NUMBER_OF_GPIO_TE]; +static bool callbackDeferred[NUMBER_OF_GPIO_TE]; static int8_t channelMap[NUMBER_OF_GPIO_TE]; static int enabled = 0; @@ -39,6 +40,7 @@ static void __initialize() { memset(callbacksInt, 0, sizeof(callbacksInt)); memset(channelMap, -1, sizeof(channelMap)); + memset(callbackDeferred, 0, sizeof(callbackDeferred)); NVIC_DisableIRQ(GPIOTE_IRQn); NVIC_ClearPendingIRQ(GPIOTE_IRQn); @@ -65,6 +67,9 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) pin = g_ADigitalPinMap[pin]; + bool deferred = (mode & ISR_DEFERRED) ? true : false; + mode &= ~ISR_DEFERRED; + uint32_t polarity; switch (mode) { @@ -88,6 +93,7 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) if (channelMap[ch] == -1 || (uint32_t)channelMap[ch] == pin) { channelMap[ch] = pin; callbacksInt[ch] = callback; + callbackDeferred[ch] = deferred; NRF_GPIOTE->CONFIG[ch] &= ~(GPIOTE_CONFIG_PSEL_Msk | GPIOTE_CONFIG_POLARITY_Msk); NRF_GPIOTE->CONFIG[ch] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk) | @@ -119,6 +125,7 @@ void detachInterrupt(uint32_t pin) if ((uint32_t)channelMap[ch] == pin) { channelMap[ch] = -1; callbacksInt[ch] = NULL; + callbackDeferred[ch] = false; NRF_GPIOTE->CONFIG[ch] &= ~GPIOTE_CONFIG_MODE_Event; @@ -136,7 +143,12 @@ void GPIOTE_IRQHandler() for (int ch = 0; ch < NUMBER_OF_GPIO_TE; ch++) { if ((*(uint32_t *)((uint32_t)NRF_GPIOTE + event) == 0x1UL) && (NRF_GPIOTE->INTENSET & (1 << ch))) { if (channelMap[ch] != -1 && callbacksInt[ch]) { - callbacksInt[ch](); + if ( callbackDeferred[ch] ) { + // Adafruit defer callback to non-isr if configured so + ada_callback_fromISR(NULL, callbacksInt[ch]); + }else{ + callbacksInt[ch](); + } } *(uint32_t *)((uint32_t)NRF_GPIOTE + event) = 0; diff --git a/cores/nRF5/WInterrupts.h b/cores/nRF5/WInterrupts.h index a8bdd9e0..c35a8570 100644 --- a/cores/nRF5/WInterrupts.h +++ b/cores/nRF5/WInterrupts.h @@ -31,6 +31,8 @@ extern "C" { #define FALLING 3 #define RISING 4 +#define ISR_DEFERRED 0x0100 + #define DEFAULT 1 #define EXTERNAL 0 diff --git a/keywords.txt b/keywords.txt index 520f08a1..4b99ad22 100644 --- a/keywords.txt +++ b/keywords.txt @@ -124,3 +124,4 @@ PIN_SPI_SCK LITERAL1 PIN_WIRE_SDA LITERAL1 PIN_WIRE_SCL LITERAL1 +ISR_DEFERRED LITERAL1 diff --git a/libraries/Bluefruit52Lib/examples/Hardware/digital_interrupt_deferred/digital_interrupt_deferred.ino b/libraries/Bluefruit52Lib/examples/Hardware/digital_interrupt_deferred/digital_interrupt_deferred.ino new file mode 100644 index 00000000..a68b6048 --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Hardware/digital_interrupt_deferred/digital_interrupt_deferred.ino @@ -0,0 +1,46 @@ +/********************************************************************* + This is an example for our Feather Bluefruit modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* + * This sketch demotrate how to pass ISR_DEFFERED as additional parameter + * to defer callback from ISR context with attachInterrupt + */ +#include + +int interruptPin = A0; + +void setup() +{ + Serial.begin(115200); + + pinMode(interruptPin, INPUT_PULLUP); + + // ISR_DEFERRED flag cause the callback to be deferred from ISR context + // and invoked within a callback thread. + // It is required to use ISR_DEFERRED if callback function take long time + // to run e.g Serial.print() or using any of Bluefruit API() which will + // potentially call rtos API + attachInterrupt(interruptPin, digital_callback, ISR_DEFERRED | CHANGE); +} + +void loop() +{ + // nothing to do +} + +void digital_callback(void) +{ + Serial.print("Pin value: "); + Serial.println(digitalRead(interruptPin)); +}