@@ -105,12 +105,15 @@ extern int ICACHE_RAM_ATTR __digitalRead(uint8_t pin) {
105105*/
106106
107107typedef void (* voidFuncPtr )(void );
108+ typedef void (* voidFuncPtrArg )(void * );
108109
109110typedef struct {
110111 uint8_t mode ;
111112 void (* fn )(void );
113+ void * arg ;
112114} interrupt_handler_t ;
113115
116+
114117static interrupt_handler_t interrupt_handlers [16 ];
115118static uint32_t interrupt_reg = 0 ;
116119
@@ -127,31 +130,44 @@ void ICACHE_RAM_ATTR interrupt_handler(void *arg) {
127130 while (!(changedbits & (1 << i ))) i ++ ;
128131 changedbits &= ~(1 << i );
129132 interrupt_handler_t * handler = & interrupt_handlers [i ];
130- if (handler -> fn &&
131- (handler -> mode == CHANGE ||
133+ if (handler -> fn &&
134+ (handler -> mode == CHANGE ||
132135 (handler -> mode & 1 ) == !!(levels & (1 << i )))) {
133136 // to make ISR compatible to Arduino AVR model where interrupts are disabled
134137 // we disable them before we call the client ISR
135- uint32_t savedPS = xt_rsil (15 ); // stop other interrupts
136- handler -> fn ();
137- xt_wsr_ps (savedPS );
138+ uint32_t savedPS = xt_rsil (15 ); // stop other interrupts
139+ if (handler -> arg )
140+ {
141+ ((voidFuncPtrArg )handler -> fn )(handler -> arg );
142+ }
143+ else
144+ {
145+ handler -> fn ();
146+ }
147+ xt_wsr_ps (savedPS );
138148 }
139149 }
140150 ETS_GPIO_INTR_ENABLE ();
141151}
142152
143- extern void ICACHE_RAM_ATTR __attachInterrupt (uint8_t pin , voidFuncPtr userFunc , int mode ) {
153+ extern void ICACHE_RAM_ATTR __attachInterruptArg (uint8_t pin , voidFuncPtr userFunc , void * arg , int mode ) {
144154 if (pin < 16 ) {
145155 interrupt_handler_t * handler = & interrupt_handlers [pin ];
146156 handler -> mode = mode ;
147157 handler -> fn = userFunc ;
158+ handler -> arg = arg ;
148159 interrupt_reg |= (1 << pin );
149160 GPC (pin ) &= ~(0xF << GPCI );//INT mode disabled
150161 GPIEC = (1 << pin ); //Clear Interrupt for this pin
151162 GPC (pin ) |= ((mode & 0xF ) << GPCI );//INT mode "mode"
152163 }
153164}
154165
166+ extern void ICACHE_RAM_ATTR __attachInterrupt (uint8_t pin , voidFuncPtr userFunc , int mode )
167+ {
168+ __attachInterruptArg (pin , userFunc , 0 , mode );
169+ }
170+
155171extern void ICACHE_RAM_ATTR __detachInterrupt (uint8_t pin ) {
156172 if (pin < 16 ) {
157173 GPC (pin ) &= ~(0xF << GPCI );//INT mode disabled
@@ -160,6 +176,7 @@ extern void ICACHE_RAM_ATTR __detachInterrupt(uint8_t pin) {
160176 interrupt_handler_t * handler = & interrupt_handlers [pin ];
161177 handler -> mode = 0 ;
162178 handler -> fn = 0 ;
179+ handler -> arg = 0 ;
163180 }
164181}
165182
@@ -176,7 +193,7 @@ void initPins() {
176193 for (int i = 12 ; i <= 16 ; ++ i ) {
177194 pinMode (i , INPUT );
178195 }
179-
196+
180197 ETS_GPIO_INTR_ATTACH (interrupt_handler , & interrupt_reg );
181198 ETS_GPIO_INTR_ENABLE ();
182199}
@@ -186,3 +203,4 @@ extern void digitalWrite(uint8_t pin, uint8_t val) __attribute__ ((weak, alias("
186203extern int digitalRead (uint8_t pin ) __attribute__ ((weak , alias ("__digitalRead" )));
187204extern void attachInterrupt (uint8_t pin , voidFuncPtr handler , int mode ) __attribute__ ((weak , alias ("__attachInterrupt" )));
188205extern void detachInterrupt (uint8_t pin ) __attribute__ ((weak , alias ("__detachInterrupt" )));
206+
0 commit comments