@@ -111,16 +111,18 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
111
111
// to preferably re-use any channel that was already in use (even if
112
112
// earlier channel is no longer in use).
113
113
for (int ch = 0 ; ch < NUMBER_OF_GPIO_TE ; ch ++ ) {
114
- if ((uint32_t )channelMap [ch ] == pin || channelMap [ ch ] == -1 ) {
114
+ if ((uint32_t )channelMap [ch ] == pin ) {
115
115
// The pin is already allocated in the channelMap
116
116
// update the polarity (when events fire) and callbacks
117
+ // However, do NOT clear any GPIOTE events
117
118
uint32_t tmp = NRF_GPIOTE -> CONFIG [ch ];
119
+ channelMap [ch ] = pin ;
120
+ callbacksInt [ch ] = callback ;
121
+ callbackDeferred [ch ] = deferred ;
118
122
tmp &= oldRegMask ;
119
123
tmp |= newRegBits ;
120
124
NRF_GPIOTE -> CONFIG [ch ] = tmp ;
121
125
NRF_GPIOTE -> INTENSET = (1 << ch ); // old code did this ... no harm in ensuring this is set
122
- callbacksInt [ch ] = callback ;
123
- callbackDeferred [ch ] = deferred ;
124
126
return (1 << ch );
125
127
}
126
128
}
@@ -133,14 +135,17 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
133
135
if (channelMap [ch ] != -1 ) continue ;
134
136
// skip if channel is not disabled (e.g., in use by some other component or library)
135
137
if (nrf_gpiote_te_is_enabled (NRF_GPIOTE , ch )) continue ;
138
+ // clear any old events on this GPIOTE channel
139
+ NRF_GPIOTE -> EVENTS_IN [ch ] = 0 ;
136
140
uint32_t tmp = NRF_GPIOTE -> CONFIG [ch ];
141
+ channelMap [ch ] = pin ;
142
+ callbacksInt [ch ] = callback ;
143
+ callbackDeferred [ch ] = deferred ;
137
144
tmp &= oldRegMask ;
138
145
tmp |= newRegBits ;
139
146
// TODO: make check/set for new channel an atomic operation
140
147
NRF_GPIOTE -> CONFIG [ch ] = tmp ;
141
148
NRF_GPIOTE -> INTENSET = (1 << ch );
142
- callbacksInt [ch ] = callback ;
143
- callbackDeferred [ch ] = deferred ;
144
149
return (1 << ch );
145
150
}
146
151
// Else, pin was neither already setup, nor could a GPIOTE be allocated
0 commit comments