@@ -108,12 +108,16 @@ extern int ICACHE_RAM_ATTR __digitalRead(uint8_t pin) {
108
108
typedef void (*voidFuncPtr)(void );
109
109
typedef void (*voidFuncPtrArg)(void *);
110
110
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
+ }
115
119
116
- static interrupt_handler_t interrupt_handlers[16 ] = { {0 , nullptr }, };
120
+ static interrupt_handler_t interrupt_handlers[16 ] = { {0 , nullptr , nullptr }, };
117
121
static uint32_t interrupt_reg = 0 ;
118
122
119
123
void ICACHE_RAM_ATTR interrupt_handler (void *arg, void *frame)
@@ -136,7 +140,10 @@ void ICACHE_RAM_ATTR interrupt_handler(void *arg, void *frame)
136
140
// to make ISR compatible to Arduino AVR model where interrupts are disabled
137
141
// we disable them before we call the client ISR
138
142
esp8266::InterruptLock irqLock; // stop other interrupts
139
- handler->fn ();
143
+ if (handler->fn )
144
+ handler->fn ();
145
+ else
146
+ handler->functional ();
140
147
}
141
148
}
142
149
ETS_GPIO_INTR_ENABLE ();
@@ -156,6 +163,27 @@ extern void __attachInterruptArg(uint8_t pin, voidFuncPtrArg userFunc, void* arg
156
163
attachInterrupt (pin, std::bind (userFunc, arg), mode);
157
164
}
158
165
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
+
159
187
extern void __attachInterrupt (uint8_t pin, voidFuncPtr userFunc, int mode)
160
188
{
161
189
// #5780
@@ -167,15 +195,13 @@ extern void __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int mode)
167
195
abort ();
168
196
}
169
197
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
+ }
179
205
}
180
206
181
207
extern void ICACHE_RAM_ATTR __detachInterrupt (uint8_t pin)
@@ -186,7 +212,7 @@ extern void ICACHE_RAM_ATTR __detachInterrupt(uint8_t pin)
186
212
GPC (pin) &= ~(0xF << GPCI);// INT mode disabled
187
213
GPIEC = (1 << pin); // Clear Interrupt for this pin
188
214
interrupt_reg &= ~(1 << pin);
189
- set_interrupt_handlers (pin, nullptr , 0 );
215
+ set_interrupt_handlers (pin, nullptr , 0 );
190
216
if (interrupt_reg)
191
217
{
192
218
ETS_GPIO_INTR_ENABLE ();
@@ -221,17 +247,25 @@ extern void detachInterrupt(uint8_t pin) __attribute__ ((weak, alias("__detachIn
221
247
222
248
};
223
249
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
+
224
262
extern void attachInterrupt (uint8_t pin, std::function<void ()> userFunc, int mode)
225
263
{
226
264
if (pin < 16 )
227
265
{
228
266
ETS_GPIO_INTR_DISABLE ();
229
267
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);
235
269
ETS_GPIO_INTR_ENABLE ();
236
270
}
237
271
}
0 commit comments