#include "common.h"

void irq_demo(int a,char** b)
{
    printf("**** PIN IRQ Demo ****\r\n");
    printf("**** Press any key to exit ****\r\n");
    // enable PTB11, PTC9 as GPIO output pin
    PORTC_PCR1 &= ~0x700;
    PORTC_PCR1 |= PORT_PCR_MUX(1);    
    PORTC_PCR2 &= ~0x700;
    PORTC_PCR2 |= PORT_PCR_MUX(1);    

    PORTC_PCR9 &= ~0x700;
    PORTC_PCR9 |= PORT_PCR_MUX(1);    
    PORTC_PCR10 &= ~0x700;
    PORTC_PCR10 |= PORT_PCR_MUX(1);     
    GPIOC_PDDR |= 0x600;
    
    // init sw6 & sw7
    PORTC_PCR1 = PORT_PCR_IRQC(0x0A)|PORT_PCR_MUX(1)
                |PORT_PCR_PFE_MASK|PORT_PCR_PE_MASK
                |PORT_PCR_PS_MASK; 
    PORTC_PCR2 = PORT_PCR_IRQC(0x0A)|PORT_PCR_MUX(1)
                |PORT_PCR_PFE_MASK|PORT_PCR_PE_MASK
                |PORT_PCR_PS_MASK; 
    
    // enable interrupt
    enable_irq(INT_PORTC-16);
    
    in_char();  // wait for key;
    disable_irq(INT_PORTC - 16);

    GPIOC_PDOR &= ~0x780; 
    GPIOC_PDDR &= ~0x600;  // disable PTC9,10    
}

void irq_isr(void)
{
    if(PORTC_ISFR&(0x1<<1))
    {
        PORTC_ISFR |= (0x1<<1); //clear flag
        GPIOC_PTOR |= 0x400; //toggle led
    }
    if(PORTC_ISFR&(0x1<<2))
    {
        PORTC_ISFR |= (0x1<<2); // clear flag
        GPIOC_PTOR |= 0x200; //toggle led
    }
}