Skip to content

Commit 4dedfab

Browse files
committed
Modified example to showcase how general FunctionalInterrupt uses violate the ISR in
ICACHE_RAM rule (ESP8266 core catches this).
1 parent af1a7dd commit 4dedfab

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

libraries/ESP32/examples/GPIO/FunctionalInterrupt/FunctionalInterrupt.ino

+30-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <Arduino.h>
2+
#include <Schedule.h>
23
#include "FunctionalInterrupts.h"
34

45
#if defined(ESP32)
@@ -16,10 +17,15 @@ class Button {
1617
public:
1718
Button(uint8_t reqPin) : PIN(reqPin) {
1819
pinMode(PIN, INPUT_PULLUP);
19-
attachInterrupt(PIN, std::bind(&Button::isr, this), FALLING);
20+
// Arduino C API:
21+
attachInterruptArg(PIN, [](void* self) { static_cast<Button*>(self)->isr(); }, this, FALLING); // works on ESP32; fails on ESP8266: "ISR not in IRAM"
22+
//attachInterruptArg(PIN, reinterpret_cast<void(*)(void*)>(&isr_static), this, FALLING); // works on ESP32; works on ESP8266
23+
// FunctionalInterrupts API:
24+
//attachInterrupt(PIN, [this]() { isr(); }, FALLING); // works on ESP32; works on ESP8266
25+
//attachScheduledInterrupt(PIN, [this](InterruptInfo ii) { Serial.print("Pin "); Serial.println(ii.pin); isr(); }, FALLING); // works on ESP32; works on ESP8266
2026
};
2127
~Button() {
22-
detachFunctionalInterrupt(PIN);
28+
detachInterrupt(PIN);
2329
}
2430

2531
#if defined(ESP8266)
@@ -32,6 +38,15 @@ class Button {
3238
pressed = true;
3339
}
3440

41+
#if defined(ESP8266)
42+
static void ICACHE_RAM_ATTR isr_static(Button* const self)
43+
#elif defined(ESP32)
44+
static void IRAM_ATTR isr_static(Button* const self)
45+
#endif
46+
{
47+
self->isr();
48+
}
49+
3550
void checkPressed() {
3651
if (pressed) {
3752
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
@@ -41,19 +56,26 @@ class Button {
4156

4257
private:
4358
const uint8_t PIN;
44-
volatile uint32_t numberKeyPresses;
45-
volatile bool pressed;
59+
volatile uint32_t numberKeyPresses = 0;
60+
volatile bool pressed = false;
4661
};
4762

48-
Button button1(BUTTON1);
49-
Button button2(BUTTON2);
63+
Button* button1;
64+
Button* button2;
5065

5166

5267
void setup() {
5368
Serial.begin(115200);
69+
schedule_function([]() { Serial.println("Scheduled function"); });
70+
Serial.println("FunctionalInterrupt test/example");
71+
72+
button1 = new Button(BUTTON1);
73+
button2 = new Button(BUTTON2);
74+
75+
Serial.println("setup() complete");
5476
}
5577

5678
void loop() {
57-
button1.checkPressed();
58-
button2.checkPressed();
79+
button1->checkPressed();
80+
button2->checkPressed();
5981
}

libraries/ESP32/examples/GPIO/FunctionalInterrupt/FunctionalInterrupts.cpp

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "FunctionalInterrupts.h"
2-
#include "Schedule.h"
2+
#include <Schedule.h>
33
#include "Arduino.h"
44

55
void ICACHE_RAM_ATTR interruptFunctional(void* arg)
@@ -33,10 +33,7 @@ void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode
3333
// use the local interrupt routine which takes the ArgStructure as argument
3434

3535
void* localArg = detachInterruptArg(pin);
36-
if (localArg)
37-
{
38-
cleanupFunctional(localArg);
39-
}
36+
if (localArg) cleanupFunctional(localArg);
4037

4138
FunctionInfo* fi = new FunctionInfo;
4239
fi->reqFunction = intRoutine;
@@ -50,10 +47,7 @@ void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode
5047
void attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> scheduledIntRoutine, int mode)
5148
{
5249
void* localArg = detachInterruptArg(pin);
53-
if (localArg)
54-
{
55-
cleanupFunctional(localArg);
56-
}
50+
if (localArg) cleanupFunctional(localArg);
5751

5852
InterruptInfo* ii = new InterruptInfo(pin);
5953

@@ -70,8 +64,5 @@ void attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> sc
7064
void detachFunctionalInterrupt(uint8_t pin)
7165
{
7266
void* localArg = detachInterruptArg(pin);
73-
if (localArg)
74-
{
75-
cleanupFunctional(localArg);
76-
}
67+
if (localArg) cleanupFunctional(localArg);
7768
}

0 commit comments

Comments
 (0)