Skip to content

External Interrupt handling fix #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 40 additions & 97 deletions cores/arduino/WInterrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@
extern "C" {
#endif

static struct
{
uint32_t _ulPin ;
voidFuncPtr _callback ;
} callbacksInt[EXTERNAL_NUM_INTERRUPTS] ;
static voidFuncPtr callbacksInt[EXTERNAL_NUM_INTERRUPTS];

/* Configure I/O interrupt sources */
static void __initialize()
{
memset( callbacksInt, 0, sizeof( callbacksInt ) ) ;
memset(callbacksInt, 0, sizeof(callbacksInt));

NVIC_DisableIRQ( EIC_IRQn ) ;
NVIC_ClearPendingIRQ( EIC_IRQn ) ;
Expand Down Expand Up @@ -76,6 +72,8 @@ void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)

if (digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT)
return;
if (digitalPinToInterrupt(pin) == EXTERNAL_INT_NMI)
return;

if (!enabled) {
__initialize();
Expand All @@ -86,94 +84,55 @@ void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
pinPeripheral(pin, PIO_EXTINT);

// Assign callback to interrupt
callbacksInt[digitalPinToInterrupt(pin)]._ulPin = pin;
callbacksInt[digitalPinToInterrupt(pin)]._callback = callback;

// Check if normal interrupt or NMI
if (pin != 2)
{
// Look for right CONFIG register to be addressed
if (digitalPinToInterrupt(pin) > EXTERNAL_INT_7) {
config = 1;
} else {
config = 0;
}

// Configure the interrupt mode
pos = ((digitalPinToInterrupt(pin) - (8 * config)) << 2);
switch (mode)
{
case LOW:
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_LOW_Val << pos;
break;

case HIGH:
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_HIGH_Val << pos;
break;
callbacksInt[digitalPinToInterrupt(pin)] = callback;

case CHANGE:
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_BOTH_Val << pos;
break;

case FALLING:
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_FALL_Val << pos;
break;

case RISING:
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_RISE_Val << pos;
break;
}

// Enable the interrupt
EIC->INTENSET.reg = EIC_INTENSET_EXTINT(1 << digitalPinToInterrupt(pin));
// Look for right CONFIG register to be addressed
if (digitalPinToInterrupt(pin) > EXTERNAL_INT_7) {
config = 1;
} else {
config = 0;
}
else // Handles NMI on pin 2

// Configure the interrupt mode
pos = ((digitalPinToInterrupt(pin) - (8 * config)) << 2);
switch (mode)
{
// Configure the interrupt mode
switch (mode)
{
case LOW:
EIC->NMICTRL.reg = EIC_NMICTRL_NMISENSE_LOW;
break;
case LOW:
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_LOW_Val << pos;
break;

case HIGH:
EIC->NMICTRL.reg = EIC_NMICTRL_NMISENSE_HIGH;
break;
case HIGH:
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_HIGH_Val << pos;
break;

case CHANGE:
EIC->NMICTRL.reg = EIC_NMICTRL_NMISENSE_BOTH;
break;
case CHANGE:
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_BOTH_Val << pos;
break;

case FALLING:
EIC->NMICTRL.reg = EIC_NMICTRL_NMISENSE_FALL;
break;
case FALLING:
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_FALL_Val << pos;
break;

case RISING:
EIC->NMICTRL.reg= EIC_NMICTRL_NMISENSE_RISE;
break;
}
case RISING:
EIC->CONFIG[config].reg |= EIC_CONFIG_SENSE0_RISE_Val << pos;
break;
}

// Enable the interrupt
EIC->INTENSET.reg = EIC_INTENSET_EXTINT(1 << digitalPinToInterrupt(pin));
}

/*
* \brief Turns off the given interrupt.
*/
void detachInterrupt( uint32_t ulPin )
void detachInterrupt(uint32_t pin)
{
/*
// Retrieve pin information
Pio *pio = g_APinDescription[pin].pPort;
uint32_t mask = g_APinDescription[pin].ulPin;

// Disable interrupt
pio->PIO_IDR = mask;
*/
if ( digitalPinToInterrupt( ulPin ) == NOT_AN_INTERRUPT )
{
return ;
}
if (digitalPinToInterrupt(pin) == NOT_AN_INTERRUPT)
return;
if (digitalPinToInterrupt(pin) == EXTERNAL_INT_NMI)
return;

EIC->INTENCLR.reg = EIC_INTENCLR_EXTINT( 1 << digitalPinToInterrupt( ulPin ) ) ;
EIC->INTENCLR.reg = EIC_INTENCLR_EXTINT(1 << digitalPinToInterrupt(pin));
}

/*
Expand All @@ -189,9 +148,8 @@ void EIC_Handler( void )
if ( (EIC->INTFLAG.reg & ( 1 << ul ) ) != 0 )
{
// Call the callback function if assigned
if ( callbacksInt[ul]._callback != NULL )
{
callbacksInt[ul]._callback() ;
if (callbacksInt[ul]) {
callbacksInt[ul]();
}

// Clear the interrupt
Expand All @@ -200,21 +158,6 @@ void EIC_Handler( void )
}
}

/*
* External Non-Maskable Interrupt Controller NVIC Interrupt Handler
*/
void NMI_Handler( void )
{
// Call the callback function if assigned
if ( callbacksInt[EXTERNAL_INT_NMI]._callback != NULL )
{
callbacksInt[EXTERNAL_INT_NMI]._callback() ;
}

// Clear the interrupt
EIC->NMIFLAG.reg = EIC_NMIFLAG_NMI ;
}

#ifdef __cplusplus
}
#endif