Skip to content

Commit 0c8556b

Browse files
committed
Fast-track for void(*)() ISR
1 parent db86a5a commit 0c8556b

File tree

1 file changed

+55
-21
lines changed

1 file changed

+55
-21
lines changed

cores/esp8266/core_esp8266_wiring_digital.cpp

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,16 @@ extern int ICACHE_RAM_ATTR __digitalRead(uint8_t pin) {
108108
typedef void (*voidFuncPtr)(void);
109109
typedef void (*voidFuncPtrArg)(void*);
110110

111-
typedef struct {
112-
uint8_t mode;
113-
std::function<void()> fn;
114-
} interrupt_handler_t;
111+
namespace
112+
{
113+
struct interrupt_handler_t {
114+
uint8_t mode;
115+
voidFuncPtr fn;
116+
std::function<void()> functional;
117+
};
118+
}
115119

116-
static interrupt_handler_t interrupt_handlers[16] = { {0, nullptr}, };
120+
static interrupt_handler_t interrupt_handlers[16] = { {0, nullptr, nullptr}, };
117121
static uint32_t interrupt_reg = 0;
118122

119123
void ICACHE_RAM_ATTR interrupt_handler(void *arg, void *frame)
@@ -136,7 +140,10 @@ void ICACHE_RAM_ATTR interrupt_handler(void *arg, void *frame)
136140
// to make ISR compatible to Arduino AVR model where interrupts are disabled
137141
// we disable them before we call the client ISR
138142
esp8266::InterruptLock irqLock; // stop other interrupts
139-
handler->fn();
143+
if (handler->fn)
144+
handler->fn();
145+
else
146+
handler->functional();
140147
}
141148
}
142149
ETS_GPIO_INTR_ENABLE();
@@ -156,6 +163,27 @@ extern void __attachInterruptArg(uint8_t pin, voidFuncPtrArg userFunc, void* arg
156163
attachInterrupt(pin, std::bind(userFunc, arg), mode);
157164
}
158165

166+
namespace
167+
{
168+
void set_interrupt_reg(uint8_t pin, int mode)
169+
{
170+
interrupt_reg |= (1 << pin);
171+
GPC(pin) &= ~(0xF << GPCI);//INT mode disabled
172+
GPIEC = (1 << pin); //Clear Interrupt for this pin
173+
GPC(pin) |= ((mode & 0xF) << GPCI);//INT mode "mode"
174+
ETS_GPIO_INTR_ATTACH(interrupt_handler, &interrupt_reg);
175+
}
176+
177+
void set_interrupt_handlers(uint8_t pin, voidFuncPtr userFunc, uint8_t mode)
178+
{
179+
interrupt_handler_t* handler = &interrupt_handlers[pin];
180+
handler->fn = userFunc;
181+
handler->functional = nullptr;
182+
if (userFunc)
183+
handler->mode = mode;
184+
}
185+
}
186+
159187
extern void __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int mode)
160188
{
161189
// #5780
@@ -167,15 +195,13 @@ extern void __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int mode)
167195
abort();
168196
}
169197

170-
attachInterrupt(pin, std::function<void()>(userFunc), mode);
171-
}
172-
173-
void set_interrupt_handlers(uint8_t pin, std::function<void()>&& userFunc, uint8_t mode)
174-
{
175-
if (!userFunc) return;
176-
interrupt_handler_t* handler = &interrupt_handlers[pin];
177-
handler->fn = std::move(userFunc);
178-
handler->mode = mode;
198+
if (pin < 16)
199+
{
200+
ETS_GPIO_INTR_DISABLE();
201+
set_interrupt_handlers(pin, userFunc, mode);
202+
set_interrupt_reg(pin, mode);
203+
ETS_GPIO_INTR_ENABLE();
204+
}
179205
}
180206

181207
extern void ICACHE_RAM_ATTR __detachInterrupt(uint8_t pin)
@@ -186,7 +212,7 @@ extern void ICACHE_RAM_ATTR __detachInterrupt(uint8_t pin)
186212
GPC(pin) &= ~(0xF << GPCI);//INT mode disabled
187213
GPIEC = (1 << pin); //Clear Interrupt for this pin
188214
interrupt_reg &= ~(1 << pin);
189-
set_interrupt_handlers(pin, nullptr, 0);
215+
set_interrupt_handlers(pin, nullptr, 0);
190216
if (interrupt_reg)
191217
{
192218
ETS_GPIO_INTR_ENABLE();
@@ -221,17 +247,25 @@ extern void detachInterrupt(uint8_t pin) __attribute__ ((weak, alias("__detachIn
221247

222248
};
223249

250+
namespace
251+
{
252+
void set_interrupt_handlers(uint8_t pin, std::function<void()>&& userFunc, uint8_t mode)
253+
{
254+
interrupt_handler_t* handler = &interrupt_handlers[pin];
255+
handler->fn = nullptr;
256+
handler->functional = std::move(userFunc);
257+
if (userFunc)
258+
handler->mode = mode;
259+
}
260+
}
261+
224262
extern void attachInterrupt(uint8_t pin, std::function<void()> userFunc, int mode)
225263
{
226264
if (pin < 16)
227265
{
228266
ETS_GPIO_INTR_DISABLE();
229267
set_interrupt_handlers(pin, std::move(userFunc), mode);
230-
interrupt_reg |= (1 << pin);
231-
GPC(pin) &= ~(0xF << GPCI);//INT mode disabled
232-
GPIEC = (1 << pin); //Clear Interrupt for this pin
233-
GPC(pin) |= ((mode & 0xF) << GPCI);//INT mode "mode"
234-
ETS_GPIO_INTR_ATTACH(interrupt_handler, &interrupt_reg);
268+
set_interrupt_reg(pin, mode);
235269
ETS_GPIO_INTR_ENABLE();
236270
}
237271
}

0 commit comments

Comments
 (0)