diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c index 6bbdf3624061380d89acdc793f50f93f997decaa..25b45d794bdac0948d171b1527043ee613fc0a3c 100644 --- a/cores/arduino/WInterrupts.c +++ b/cores/arduino/WInterrupts.c @@ -51,6 +51,8 @@ static void __initialize() NVIC_SetPriority( EIC_IRQn, 0 ) ; NVIC_EnableIRQ( EIC_IRQn ) ; + + // Enable GCLK for IEC (External Interrupt Controller) GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID( GCM_EIC )) ; @@ -60,9 +62,18 @@ static void __initialize() while ( (EIC->CTRL.SWRST.bit == 1) && (EIC->STATUS.SYNCBUSY.bit == 1) ) { - // Wait for synchronisation + // Waiting for synchronisation } */ + + // Enable EIC + EIC->CTRL.bit.ENABLE = 1 ; + + while ( EIC->STATUS.bit.SYNCBUSY == 1 ) + { + // Waiting for synchronisation + } + } /* @@ -109,27 +120,31 @@ void attachInterrupt( uint32_t ulPin, voidFuncPtr callback, uint32_t ulMode ) } // Configure the interrupt mode - ulPos = ((digitalPinToInterrupt( ulPin ) >> ulConfig ) << 2) ; + ulPos = ((digitalPinToInterrupt( ulPin ) - (8*ulConfig) ) << 2) ; switch ( ulMode ) { case LOW: - EIC->CONFIG[ulConfig].reg = EIC_CONFIG_SENSE0_LOW_Val << ulPos ; + EIC->CONFIG[ulConfig].reg |= EIC_CONFIG_SENSE0_LOW_Val << ulPos ; break ; case HIGH: - EIC->CONFIG[ulConfig].reg = EIC_CONFIG_SENSE0_HIGH_Val << ((digitalPinToInterrupt( ulPin ) >> ulConfig ) << 2) ; + // EIC->CONFIG[ulConfig].reg = EIC_CONFIG_SENSE0_HIGH_Val << ((digitalPinToInterrupt( ulPin ) >> ulConfig ) << ulPos) ; + EIC->CONFIG[ulConfig].reg |= EIC_CONFIG_SENSE0_HIGH_Val << ulPos ; break ; case CHANGE: - EIC->CONFIG[ulConfig].reg = EIC_CONFIG_SENSE0_BOTH_Val << ((digitalPinToInterrupt( ulPin ) >> ulConfig ) << 2) ; + // EIC->CONFIG[ulConfig].reg = EIC_CONFIG_SENSE0_BOTH_Val << ((digitalPinToInterrupt( ulPin ) >> ulConfig ) << ulPos) ; + EIC->CONFIG[ulConfig].reg |= EIC_CONFIG_SENSE0_BOTH_Val << ulPos ; break ; case FALLING: - EIC->CONFIG[ulConfig].reg = EIC_CONFIG_SENSE0_FALL_Val << ((digitalPinToInterrupt( ulPin ) >> ulConfig ) << 2) ; + // EIC->CONFIG[ulConfig].reg = EIC_CONFIG_SENSE0_FALL_Val << ((digitalPinToInterrupt( ulPin ) >> ulConfig ) << ulPos) ; + EIC->CONFIG[ulConfig].reg |= EIC_CONFIG_SENSE0_FALL_Val << ulPos ; break ; case RISING: - EIC->CONFIG[ulConfig].reg = EIC_CONFIG_SENSE0_RISE_Val << ((digitalPinToInterrupt( ulPin ) >> ulConfig ) << 2) ; + // EIC->CONFIG[ulConfig].reg = EIC_CONFIG_SENSE0_RISE_Val << ((digitalPinToInterrupt( ulPin ) >> ulConfig ) << ulPos) ; + EIC->CONFIG[ulConfig].reg |= EIC_CONFIG_SENSE0_RISE_Val << ulPos ; break ; } @@ -209,9 +224,9 @@ void EIC_Handler( void ) } // Test the 16 normal interrupts - for ( ul = EXTERNAL_INT_0 ; ul < EXTERNAL_INT_15 ; ul++ ) + for ( ul = EXTERNAL_INT_0 ; ul < EXTERNAL_INT_NMI ; ul++ ) { - if ( (EIC->INTFLAG.reg & 1 << ul) != 0 ) + if ( (EIC->INTFLAG.reg & ( 1 << ul ) ) != 0 ) { // Call the callback function if assigned if ( callbacksInt[ul]._callback != NULL ) diff --git a/cores/arduino/delay.c b/cores/arduino/delay.c index 834278413afa6a04696a2196a2992a372d79550a..2548f91edc1b659f61c0764edc9fbf243f263920 100644 --- a/cores/arduino/delay.c +++ b/cores/arduino/delay.c @@ -55,7 +55,7 @@ void delay( uint32_t ms ) do { yield() ; - } while ( _ulTickCount - start < ms ) ; + } while ( _ulTickCount - start <= ms ) ; } void SysTick_Handler( void ) diff --git a/cores/arduino/delay.h b/cores/arduino/delay.h index f9b08efeff3b608ef09265eb2f261cc46ff72919..9809fc3753f56b3f2a17be392119c8a8bd0e2854 100644 --- a/cores/arduino/delay.h +++ b/cores/arduino/delay.h @@ -64,13 +64,18 @@ static inline void delayMicroseconds(uint32_t) __attribute__((always_inline, unu static inline void delayMicroseconds(uint32_t usec){ if (usec == 0) return; uint32_t n = usec * (VARIANT_MCK / 3000000); - +#if 0 __asm__ volatile( "L_%=_delayMicroseconds:" "\n\t" - "subs %0, #1" "\n\t" + "subs %0, %0, #1" "\n\t" "bne L_%=_delayMicroseconds" "\n" : "+r" (n) : ); +#else + for ( ; n != 0 ; n-- ) + { + } +#endif } /* diff --git a/cores/validation/validation_core/test.cpp b/cores/validation/validation_core/test.cpp index 85e5315d712cc6d1db9b1902998178957ba4adf0..e86988ab09506d3b284c4eaf7b349643543a47cd 100644 --- a/cores/validation/validation_core/test.cpp +++ b/cores/validation/validation_core/test.cpp @@ -56,9 +56,9 @@ void setup( void ) //********************************************** // Clock output on pin 4 for measure - pinPeripheral( 4, PIO_AC_CLK ) ; // Clock Gen 0 + /* pinPeripheral( 4, PIO_AC_CLK ) ; // Clock Gen 0 pinPeripheral( 5, PIO_AC_CLK ) ; // Clock Gen 1 - pinPeripheral( 13, PIO_AC_CLK ) ; // Clock Gen 3 + pinPeripheral( 13, PIO_AC_CLK ) ; // Clock Gen 3*/ //********************************************** Serial5.begin( 115200 ) ; // Output to EDBG Virtual COM Port @@ -113,10 +113,6 @@ void loop( void ) analogWrite( 10 ,duty_cycle ) ; analogWrite( 9, duty_cycle ) ; analogWrite( 8, duty_cycle ) ; - analogWrite( 7, duty_cycle ) ; - analogWrite( 6, duty_cycle ) ; - analogWrite( 5, duty_cycle ) ; - analogWrite( 4, duty_cycle ) ; Serial5.print("\r\nAnalog pins: "); @@ -157,7 +153,7 @@ void loop( void ) if ( ul_Interrupt_Pin7 == 1 ) { - Serial5.println( "Pin 3 triggered (CHANGE)" ) ; + Serial5.println( "Pin 7 triggered (CHANGE)" ) ; ul_Interrupt_Pin7 = 0 ; } }