/***
 *** 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 avaiblable 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.
 *** The SAM L11 example uses the Mixed Secure Peripheral usage
 *** EIC_SEC.
 ***/
void EIC_2_Handler(void)
{
    /*** Clear EIC Flag ***/
    EIC_SEC->INTFLAG.reg |= EIC_INTFLAG_EXTINT(1<<2);

}

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

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

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

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

    /*** Set EIC EXTINT[1] as non secure interrupt (Mix-Secure Use ***/
    EIC_SEC->NONSEC.reg = (EIC_NONSEC_EXTINT(1<<1));
    EIC_SEC->NSCHK.reg = (EIC_NSCHK_EXTINT(1<<1));

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

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