/************************************************************************/
/* CQBBEL GNU CC Common Control Software / (C) Toshio MURAI 2011, 2012	*/
/*      This is Free Software;   You can redistribute this software     */
/*      and/or modify it under the terms of the GNU General Public      */
/*      License version 3, or (at your option) any later version.       */
/************************************************************************/

#include	"io.h"

#define	SETCPU()	\
	{	\
		SYSSCS->SCS |=  0x01;/*FGPIO*/	\
	}

/*	On Board LED 	1 bit  P1.18 (L.on)	*/
#define	SETBOARDLED()	\
	{	\
		FGPIO_1->FIO_DIR  = 0x00040000;		\
		FGPIO_1->FIO_MASK = 0x00000000;		\
		/*FGPIO_1->FIO_PIN  = 0x00040000;*/	\
	}

#define	REVERSELED()	\
	{	\
		FGPIO_1->FIO_PIN ^= 0x00040000	;	\
	}

/*	8bit Dip Switch 8bit	J2:1-8 (A0-A7)= P4.0-P4.7	*/
#define	SETDIPSWITCH()	\
	{	\
		FGPIO_4->FIO_DIR  = 0x00000000;	\
	}

#define	GETDIPSWITCH()		((~FGPIO_4->FIO_PIN) & 0xFF)

/*	7seg LED 15bit Hight/Low	J2:23-38 (D0-D15) P3.0-P3.7 P2.0-P2.7	*/
#define	SET7SEGLED()	\
	{	\
		FGPIO_3->FIO_DIR  = 0x0000FFFF;	\
		FGPIO_2->FIO_DIR  = 0x0000FFFF;	\
	}

#define	PUT7SEGHIGH(val)	\
	{	\
		FGPIO_2->FIO_PIN = val & 0xFF; \
	}

#define	PUT7SEGLOW(val)		\
	{	\
		FGPIO_3->FIO_PIN = val & 0xFF;	\
	}

#define	GET7SEGHIGH()		(FGPIO_2->FIO_PIN & 0xFF)

#define	GET7SEGLOW()		(FGPIO_3->FIO_PIN & 0xFF)

/*	Tact Switch :4 Interrupt Request Switch	J1:29-32() 01Fall 23Rise	P0.4-P0.7 */
#define	SETIRQ()	\
	{	\
		FGPIO_0->FIO_DIR = 0x00000000;	/*Input*/\
		IO0IntEnR->IntClr = 0xF0;/*Clear P0.4-P0.7*/	\
		IO0IntEnR->IntEnF = 0x30; /*Fall*/	\
		IO0IntEnR->IntEnR = 0xC0; /*Rise*/	\
		VIC->IntSelect &= (~0x20000);	/*IRQ選択 */	\
		VIC->IntEnable |= 0x20000;		/*  */	\
	};


#define	CLEARIRQ0()	{IO0IntEnR->IntClr=0x10;}
#define	CLEARIRQ1()	{IO0IntEnR->IntClr=0x20;}
#define	CLEARIRQ2()	{IO0IntEnR->IntClr=0x40;}
#define	CLEARIRQ3()	{IO0IntEnR->IntClr=0x80;}

/*	Timer 1 msec interrupt	*/
#define	SETTIMER()	\
	{	\
	/* タイマコントローラへの供給クロック分周設定 */	\
	CLKSEL->PCLKSEL0 &=  0xfffffff3;	/* CPU 4MHz 2:3=00=1/4 (01=1/1 10=1/2) */	\
	TIMER0->PR  = 0x00000000;		/* PreScale NO		*/	\
	TIMER0->MR0 = 1000;			/* 1200? Clock 1MHz -> 1KHz	*/	\
	TIMER0->MCR = 0x00000003;		/* Match Contorl: Interrupt & Restart */	\
	TIMER0->TCR = 1;	/* Start Timer*/	\
	VIC->IntSelect &= (~0x10);		/*IRQ Select */	\
	VIC->IntEnable |= 0x10;			/* Enable Timer0 */	\
	}
	
#define	CLEARTIMERINTERRUPT()	\
	{	\
		TIMER0->IR = 1;	/* Clear Timer Interrupt	*/	\
	}

/*	AD Converter	J1:1(AN0) P0.23 */
#define	PCAD	0x00001000
#define	SETAD()	\
	{	\
		POWERCTL->PCONP |= 0x00001000;	/*PoweON*/\
		CLKSEL->PCLKSEL0 = (CLKSEL->PCLKSEL0 & 0xFCFFFFFF)|  0x01000000 ;/* Clock On 24:25=01=1/1 */	\
		PINSEL->PINSEL1 =(PINSEL->PINSEL1 & 0xFFFF3FFF )|0x00004000 ;/*14:15=A0 16:17=A1 18-19=A2 01 */	\
		AD0->AD0CR = (AD0->AD0CR& 0xFFFFFF00) |0x00201001;	/*21PDN 15:8CLKDIV=16 AD0*/	\
	}

#define	STARTAD0()	\
	{	\
		AD0->AD0CR = (AD0->AD0CR & 0xF8FFFFFF) | 0x01000000;	/*Start 26:24:001*/\
		while((AD0->AD0DR0 & 0x80000000)==0x00);		/*	*/ \
	}

#define	GETAD0()			((AD0->AD0DR0 >>8) &0xFF)

/*	RS232C/UART	*/
#define	SETUART()	\
	{	\
	}

#define	PUTCHAR(data)	\
	{	\
	}

#define	WAITUART()	\
	{	\
	}

#define GETCHAR()	(0)

#define	CLEARSENDFLAG()	{}

#define	CLEARRECFLAG()	{}

/*	Interrupt Handler	*/
void enableInterrupt(void);
void disableInterrupt(void);
#define	ENABLEINTERRUPT()	\
	{	\
		enableInterrupt();\
	}

#define	DISABLEINTERRUPT()	\
	{	\
		/*disableInterrupt();*/\
	}

#define	DUMMYINTERRUPT()	\
	{	\
	}

#define	INTERRUPTATTRIBUTE	
#define	ARMINTERRUPTATTRIBUTE	__attribute__ ((interrupt))

#define	TIMEINTR()	(TIMER0->IR & 0x01)	
#define	IRQ0INTR()	(IO0IntEnR->IntStatF & 0x10)
#define	IRQ1INTR()	(IO0IntEnR->IntStatF & 0x20)
#define	IRQ2INTR()	(IO0IntEnR->IntStatR & 0x40)
#define	IRQ3INTR()	(IO0IntEnR->IntStatR & 0x80)
