Skip to content

Commit efe4632

Browse files
cmagliemattairtech
authored andcommitted
NMI interrupts are now correctly ignored by attach/detachInterrupt
Fixes #30
1 parent 39cb1c1 commit efe4632

File tree

1 file changed

+36
-87
lines changed

1 file changed

+36
-87
lines changed

cores/arduino/WInterrupts.c

Lines changed: 36 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
7575
uint32_t pos;
7676

7777
// Assign pin to EIC
78-
if ( pinPeripheral(pin, PIO_EXTINT) != RET_STATUS_OK )
78+
if (pinPeripheral(pin, PIO_EXTINT) != RET_STATUS_OK)
79+
return;
80+
if (digitalPinToInterrupt(pin) == EXTERNAL_INT_NMI)
7981
return;
8082

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

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

137-
case HIGH:
138-
EIC->NMICTRL.reg = EIC_NMICTRL_NMISENSE_HIGH;
139-
break;
107+
case HIGH:
108+
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_HIGH_Val << pos;
109+
break;
140110

141-
case CHANGE:
142-
EIC->NMICTRL.reg = EIC_NMICTRL_NMISENSE_BOTH;
143-
break;
111+
case CHANGE:
112+
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_BOTH_Val << pos;
113+
break;
144114

145-
case FALLING:
146-
EIC->NMICTRL.reg = EIC_NMICTRL_NMISENSE_FALL;
147-
break;
115+
case FALLING:
116+
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_FALL_Val << pos;
117+
break;
148118

149-
case RISING:
150-
EIC->NMICTRL.reg= EIC_NMICTRL_NMISENSE_RISE;
151-
break;
152-
}
119+
case RISING:
120+
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_RISE_Val << pos;
121+
break;
153122
}
123+
124+
// Enable the interrupt
125+
EIC->INTENSET.reg = EIC_INTENSET_EXTINT(1 << digitalPinToInterrupt(pin));
154126
}
155127

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

174-
EIC->INTENCLR.reg = EIC_INTENCLR_EXTINT( 1 << digitalPinToInterrupt( ulPin ) ) ;
138+
EIC->INTENCLR.reg = EIC_INTENCLR_EXTINT(1 << digitalPinToInterrupt(pin));
175139
}
176140

177141
/*
@@ -198,21 +162,6 @@ void EIC_Handler( void )
198162
}
199163
}
200164

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

0 commit comments

Comments
 (0)