/***
 *** The Example has no copyright and can be used by anyone.
 *** The following example is based on Device File Package
 *** required to compile the macro definitions used.
 *** The Device File Package is available by downloading Atmel Studio 7.
 ***/

/***
 *** In this Example the External Interrupt Controller 
 *** is configured to detect Falling edge on EXTINT[2] (PA02),
 *** with a deboucing filter enabled.
 *** 
 *** 
 ***/
void EIC_2_Handler(void)
{
    /*** Clear EIC Flag ***/
    EIC->INTFLAG.reg |= EIC_INTFLAG_EXTINT(1<<2);    
}

void eic_init(void)
{
    /*** Detect Falling edge on EXTINT[2] (PA02) ***/
    EIC->CONFIG[0].reg = (uint32_t)(EIC_CONFIG_SENSE2_FALL | EIC_CONFIG_SENSE3_FALL);
    EIC->ASYNCH.reg = (EIC_ASYNCH_ASYNCH(1<<2));

    /*** Configure Debounce ***/
    EIC->DEBOUNCEN.reg = (EIC_DEBOUNCEN_DEBOUNCEN(1<<2));
    EIC->DPRESCALER.reg = (EIC_DPRESCALER_TICKON|EIC_DPRESCALER_STATES0|EIC_DPRESCALER_PRESCALER0(7));

    /*** Enable interrupt on EXTINT[2] (PA02) ***/
    EIC->INTENSET.reg = (EIC_INTENSET_EXTINT((1<<2)));

    /*** Enable EIC interrupt at core level ***/
    NVIC_EnableIRQ(EIC_2_IRQn);

    /*** Set EXTINT[1] Interrupt at core level ***/
    NVIC_SetTargetState(EIC_1_IRQn);
}

void secure_eic_enable(void)
{
    /*Enable EIC Peripheral */
    while(EIC->SYNCBUSY.bit.ENABLE);
    EIC->CTRLA.reg |= EIC_CTRLA_ENABLE;
}