Skip to content

Commit 6538f89

Browse files
committed
NMI interrupts are now correctly ignored by attach/detachInterrupt
Fixes #30
1 parent cc5948b commit 6538f89

File tree

1 file changed

+35
-86
lines changed

1 file changed

+35
-86
lines changed

Diff for: cores/arduino/WInterrupts.c

+35-86
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
7676

7777
if (digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT)
7878
return;
79+
if (digitalPinToInterrupt(pin) == EXTERNAL_INT_NMI)
80+
return;
7981

8082
if (!enabled) {
8183
__initialize();
@@ -89,91 +91,53 @@ void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
8991
callbacksInt[digitalPinToInterrupt(pin)]._ulPin = pin;
9092
callbacksInt[digitalPinToInterrupt(pin)]._callback = callback;
9193

92-
// Check if normal interrupt or NMI
93-
if (pin != 2)
94-
{
95-
// Look for right CONFIG register to be addressed
96-
if (digitalPinToInterrupt(pin) > EXTERNAL_INT_7) {
97-
config = 1;
98-
} else {
99-
config = 0;
100-
}
101-
102-
// Configure the interrupt mode
103-
pos = ((digitalPinToInterrupt(pin) - (8 * config)) << 2);
104-
switch (mode)
105-
{
106-
case LOW:
107-
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_LOW_Val << pos;
108-
break;
109-
110-
case HIGH:
111-
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_HIGH_Val << pos;
112-
break;
113-
114-
case CHANGE:
115-
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_BOTH_Val << pos;
116-
break;
117-
118-
case FALLING:
119-
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_FALL_Val << pos;
120-
break;
121-
122-
case RISING:
123-
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_RISE_Val << pos;
124-
break;
125-
}
126-
127-
// Enable the interrupt
128-
EIC->INTENSET.reg = EIC_INTENSET_EXTINT(1 << digitalPinToInterrupt(pin));
94+
// Look for right CONFIG register to be addressed
95+
if (digitalPinToInterrupt(pin) > EXTERNAL_INT_7) {
96+
config = 1;
97+
} else {
98+
config = 0;
12999
}
130-
else // Handles NMI on pin 2
100+
101+
// Configure the interrupt mode
102+
pos = ((digitalPinToInterrupt(pin) - (8 * config)) << 2);
103+
switch (mode)
131104
{
132-
// Configure the interrupt mode
133-
switch (mode)
134-
{
135-
case LOW:
136-
EIC->NMICTRL.reg = EIC_NMICTRL_NMISENSE_LOW;
137-
break;
105+
case LOW:
106+
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_LOW_Val << pos;
107+
break;
138108

139-
case HIGH:
140-
EIC->NMICTRL.reg = EIC_NMICTRL_NMISENSE_HIGH;
141-
break;
109+
case HIGH:
110+
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_HIGH_Val << pos;
111+
break;
142112

143-
case CHANGE:
144-
EIC->NMICTRL.reg = EIC_NMICTRL_NMISENSE_BOTH;
145-
break;
113+
case CHANGE:
114+
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_BOTH_Val << pos;
115+
break;
146116

147-
case FALLING:
148-
EIC->NMICTRL.reg = EIC_NMICTRL_NMISENSE_FALL;
149-
break;
117+
case FALLING:
118+
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_FALL_Val << pos;
119+
break;
150120

151-
case RISING:
152-
EIC->NMICTRL.reg= EIC_NMICTRL_NMISENSE_RISE;
153-
break;
154-
}
121+
case RISING:
122+
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_RISE_Val << pos;
123+
break;
155124
}
125+
126+
// Enable the interrupt
127+
EIC->INTENSET.reg = EIC_INTENSET_EXTINT(1 << digitalPinToInterrupt(pin));
156128
}
157129

158130
/*
159131
* \brief Turns off the given interrupt.
160132
*/
161-
void detachInterrupt( uint32_t ulPin )
133+
void detachInterrupt(uint32_t pin)
162134
{
163-
/*
164-
// Retrieve pin information
165-
Pio *pio = g_APinDescription[pin].pPort;
166-
uint32_t mask = g_APinDescription[pin].ulPin;
167-
168-
// Disable interrupt
169-
pio->PIO_IDR = mask;
170-
*/
171-
if ( digitalPinToInterrupt( ulPin ) == NOT_AN_INTERRUPT )
172-
{
173-
return ;
174-
}
135+
if (digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT)
136+
return;
137+
if (digitalPinToInterrupt(pin) == EXTERNAL_INT_NMI)
138+
return;
175139

176-
EIC->INTENCLR.reg = EIC_INTENCLR_EXTINT( 1 << digitalPinToInterrupt( ulPin ) ) ;
140+
EIC->INTENCLR.reg = EIC_INTENCLR_EXTINT(1 << digitalPinToInterrupt(pin));
177141
}
178142

179143
/*
@@ -200,21 +164,6 @@ void EIC_Handler( void )
200164
}
201165
}
202166

203-
/*
204-
* External Non-Maskable Interrupt Controller NVIC Interrupt Handler
205-
*/
206-
void NMI_Handler( void )
207-
{
208-
// Call the callback function if assigned
209-
if ( callbacksInt[EXTERNAL_INT_NMI]._callback != NULL )
210-
{
211-
callbacksInt[EXTERNAL_INT_NMI]._callback() ;
212-
}
213-
214-
// Clear the interrupt
215-
EIC->NMIFLAG.reg = EIC_NMIFLAG_NMI ;
216-
}
217-
218167
#ifdef __cplusplus
219168
}
220169
#endif

0 commit comments

Comments
 (0)