Skip to content

Commit 18fee01

Browse files
tekka007mfalkvidd
authored andcommitted
ESP32/ESP8266: place ISR in IRAM (#1348)
1 parent 40d8a18 commit 18fee01

File tree

8 files changed

+70
-48
lines changed

8 files changed

+70
-48
lines changed

core/Version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
#define MYSENSORS_LIBRARY_VERSION_MINOR 3 //!< Minor release version
5151
#define MYSENSORS_LIBRARY_VERSION_PATCH 2 //!< Patch version
5252
#define MYSENSORS_LIBRARY_VERSION_PRERELEASE "beta" //!< Pre-release suffix, i.e. alpha, beta, rc.1, etc
53-
#define MYSENSORS_LIBRARY_VERSION_PRERELEASE_NUMBER 0x07 //!< incremental counter, starting at 0x00. 0xFF for final release
53+
#define MYSENSORS_LIBRARY_VERSION_PRERELEASE_NUMBER 0x08 //!< incremental counter, starting at 0x00. 0xFF for final release
5454

5555

5656
#if (MYSENSORS_LIBRARY_VERSION_PRERELEASE_NUMBER != 0xFF)

examples/EnergyMeterPulseSensor/EnergyMeterPulseSensor.ino

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,33 @@ MyMessage wattMsg(CHILD_ID, V_WATT);
6868
MyMessage kWhMsg(CHILD_ID, V_KWH);
6969
MyMessage pcMsg(CHILD_ID, V_VAR1);
7070

71+
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
72+
#define IRQ_HANDLER_ATTR ICACHE_RAM_ATTR
73+
#else
74+
#define IRQ_HANDLER_ATTR
75+
#endif
76+
77+
void IRQ_HANDLER_ATTR onPulse()
78+
{
79+
if (!SLEEP_MODE) {
80+
uint32_t newBlinkmicros = micros();
81+
uint32_t newBlinkmillis = millis();
82+
uint32_t intervalmicros = newBlinkmicros - lastBlinkmicros;
83+
uint32_t intervalmillis = newBlinkmillis - lastBlinkmillis;
84+
if (intervalmicros < 10000L && intervalmillis < 10L) { // Sometimes we get interrupt on RISING
85+
return;
86+
}
87+
if (intervalmillis < 360000) { // Less than an hour since last pulse, use microseconds
88+
watt = (3600000000.0 / intervalmicros) / ppwh;
89+
} else {
90+
watt = (3600000.0 / intervalmillis) /
91+
ppwh; // more thAn an hour since last pulse, use milliseconds as micros will overflow after 70min
92+
}
93+
lastBlinkmicros = newBlinkmicros;
94+
lastBlinkmillis = newBlinkmillis;
95+
}
96+
pulseCount++;
97+
}
7198

7299
void setup()
73100
{
@@ -127,7 +154,7 @@ void loop()
127154
}
128155

129156
if (SLEEP_MODE) {
130-
sleep(SEND_FREQUENCY);
157+
sleep(SEND_FREQUENCY, false);
131158
}
132159
}
133160

@@ -140,25 +167,3 @@ void receive(const MyMessage &message)
140167
pcReceived = true;
141168
}
142169
}
143-
144-
void onPulse()
145-
{
146-
if (!SLEEP_MODE) {
147-
uint32_t newBlinkmicros = micros();
148-
uint32_t newBlinkmillis = millis();
149-
uint32_t intervalmicros = newBlinkmicros - lastBlinkmicros;
150-
uint32_t intervalmillis = newBlinkmillis - lastBlinkmillis;
151-
if (intervalmicros < 10000L && intervalmillis < 10L) { // Sometimes we get interrupt on RISING
152-
return;
153-
}
154-
if (intervalmillis < 360000) { // Less than an hour since last pulse, use microseconds
155-
watt = (3600000000.0 / intervalmicros) / ppwh;
156-
} else {
157-
watt = (3600000.0 / intervalmillis) /
158-
ppwh; // more thAn an hour since last pulse, use milliseconds as micros will overflow after 70min
159-
}
160-
lastBlinkmicros = newBlinkmicros;
161-
lastBlinkmillis = newBlinkmillis;
162-
}
163-
pulseCount++;
164-
}

examples/WaterMeterPulseSensor/WaterMeterPulseSensor.ino

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,31 @@ double oldvolume =0;
7474
uint32_t lastSend =0;
7575
uint32_t lastPulse =0;
7676

77+
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
78+
#define IRQ_HANDLER_ATTR ICACHE_RAM_ATTR
79+
#else
80+
#define IRQ_HANDLER_ATTR
81+
#endif
82+
83+
void IRQ_HANDLER_ATTR onPulse()
84+
{
85+
if (!SLEEP_MODE) {
86+
uint32_t newBlink = micros();
87+
uint32_t interval = newBlink-lastBlink;
88+
89+
if (interval!=0) {
90+
lastPulse = millis();
91+
if (interval<500000L) {
92+
// Sometimes we get interrupt on RISING, 500000 = 0.5 second debounce ( max 120 l/min)
93+
return;
94+
}
95+
flow = (60000000.0 /interval) / ppl;
96+
}
97+
lastBlink = newBlink;
98+
}
99+
pulseCount++;
100+
}
101+
77102
void setup()
78103
{
79104
// initialize our digital pins internal pullup resistor so one pulse switches from high to low (less distortion)
@@ -151,7 +176,7 @@ void loop()
151176
}
152177
}
153178
if (SLEEP_MODE) {
154-
sleep(SEND_FREQUENCY);
179+
sleep(SEND_FREQUENCY, false);
155180
}
156181
}
157182

@@ -167,21 +192,3 @@ void receive(const MyMessage &message)
167192
}
168193
}
169194

170-
void onPulse()
171-
{
172-
if (!SLEEP_MODE) {
173-
uint32_t newBlink = micros();
174-
uint32_t interval = newBlink-lastBlink;
175-
176-
if (interval!=0) {
177-
lastPulse = millis();
178-
if (interval<500000L) {
179-
// Sometimes we get interrupt on RISING, 500000 = 0.5 second debounce ( max 120 l/min)
180-
return;
181-
}
182-
flow = (60000000.0 /interval) / ppl;
183-
}
184-
lastBlink = newBlink;
185-
}
186-
pulseCount++;
187-
}

hal/architecture/MyHwHAL.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@
3838
*/
3939
#define MY_HWID_PADDING_BYTE (0xAAu)
4040

41+
/**
42+
* @def IRQ_HANDLER_ATTR
43+
* @brief ESP8266/ESP32 IRQ handlers need to be stored in IRAM
44+
*/
45+
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
46+
#define IRQ_HANDLER_ATTR ICACHE_RAM_ATTR
47+
#else
48+
#define IRQ_HANDLER_ATTR
49+
#endif
50+
4151
// Implement these as functions or macros
4252
/*
4353
#define hwInit() MY_SERIALDEVICE.begin(BAUD_RATE)

hal/transport/RF24/driver/RF24.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ LOCAL bool RF24_getReceivedPowerDetector(void)
455455
}
456456

457457
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
458-
LOCAL void RF24_irqHandler(void)
458+
LOCAL void IRQ_HANDLER_ATTR RF24_irqHandler(void)
459459
{
460460
if (RF24_receiveCallback) {
461461
#if defined(MY_GATEWAY_SERIAL) && !defined(__linux__)

hal/transport/RFM69/driver/new/RFM69_new.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ LOCAL void RFM69_clearFIFO(void)
242242
(void)RFM69_writeReg(RFM69_REG_IRQFLAGS2, RFM69_IRQFLAGS2_FIFOOVERRUN);
243243
}
244244
// IRQ handler: PayloadReady (RX) & PacketSent (TX) mapped to DI0
245-
LOCAL void RFM69_interruptHandler(void)
245+
LOCAL void IRQ_HANDLER_ATTR RFM69_interruptHandler(void)
246246
{
247247
// set flag
248248
RFM69_irq = true;

hal/transport/RFM69/driver/old/RFM69_old.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ void RFM69::sendFrame(uint8_t toAddress, const void* buffer, uint8_t bufferSize,
414414
}
415415

416416
// internal function - interrupt gets called when a packet is received
417-
void RFM69::interruptHandler()
417+
void IRQ_HANDLER_ATTR RFM69::interruptHandler()
418418
{
419419
//hwPinMode(4, OUTPUT);
420420
//hwDigitalWrite(4, 1);
@@ -461,7 +461,7 @@ void RFM69::interruptHandler()
461461
}
462462

463463
// internal function
464-
void RFM69::isr0()
464+
void IRQ_HANDLER_ATTR RFM69::isr0()
465465
{
466466
selfPointer->interruptHandler();
467467
}

hal/transport/RFM95/driver/RFM95.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ LOCAL bool RFM95_initialise(const uint32_t frequencyHz)
233233
return true;
234234
}
235235

236-
LOCAL void RFM95_interruptHandler(void)
236+
LOCAL void IRQ_HANDLER_ATTR RFM95_interruptHandler(void)
237237
{
238238
// set flag
239239
RFM95_irq = true;

0 commit comments

Comments
 (0)