diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c index 93d888200..2c6ed74e1 100644 --- a/cores/arduino/WInterrupts.c +++ b/cores/arduino/WInterrupts.c @@ -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 ) ; @@ -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(); @@ -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)); } /* @@ -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 @@ -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