#include <startup.h>
#include <LPC214X.h>
#include <armVIC.h>
#include <type.h>
#include <irq.h>
#include <TFT018.h>
#include <rprintf.h>
#include "extint.h"

// LED P0.31 
#define LED 0x80000000

#define	INT_VECT_NUM	0

static void IRQ_Eint1(void) __attribute__ ((interrupt("IRQ")));

int times = 0;

void IRQ_Eint1 (void)
{
	if ((IOSET0 & LED) == 0)
		IOSET0 = LED;
	else
		IOCLR0 = LED;
	
	times++;
	EXTINT = EINT1;
	VICVectAddr = 0x00;
}

char s[20], *sp;
int input_str(int ch){
	return *sp ++ = (char)ch;
}

int main (void) {
	Initialize();

	BacklightOn();
	LCD_Init();
	LCD_Clear_Screen(Blue);

	PINSEL0 |= 0x20000000;			/* P0.14 is EINT1			*/
    EXTMODE = EINT1_EDGE;		/* INT1 edge trigger */
    EXTPOLAR = 0;			/* INT1 is falling edge by default */

	IODIR0  |= LED;

	// set up EXTINT interrupt
	VICIntSelect &= ~(1<<EINT1_INT);
	VICIntEnable |= (1<<EINT1_INT);

	(*(&VICVectCntl0+INT_VECT_NUM)) = 0x20 | EINT1_INT; // choose highest priority ISR slot 	
	(*(&VICVectAddr0+INT_VECT_NUM)) = (int)IRQ_Eint1;
	
	enableIRQ();

	for(;;) {
		char s[20] = "1234";
		
		sp = s;
		rprintf_devopen( input_str );
		rprintf("EINT1 INT:%d",times);
		LCD_PutString(1,30,s,0xF800,0xFFE0);
		}
	}
