Added ISR_DEFERRED option for attachInterrupt() to defer callback from ISR context

This commit is contained in:
hathach committed 2018-11-06 15:12:20 +07:00
1 parent 01a927340b
commit 3e4e6f9bac
5 files changed
+65 -2

No files matched your search

+3 -1
View File
@@ -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
+13 -1
View File
@@ -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;
+2
View File
@@ -31,6 +31,8 @@ extern "C" {
#define FALLING 3
#define RISING 4
#define ISR_DEFERRED 0x0100
#define DEFAULT 1
#define EXTERNAL 0
+1
View File
@@ -124,3 +124,4 @@ PIN_SPI_SCK LITERAL1
PIN_WIRE_SDA LITERAL1
PIN_WIRE_SCL LITERAL1
ISR_DEFERRED LITERAL1
@@ -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 <Arduino.h>
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));
}