/************************************************************************/
/* 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()	\
	{	\
		PFR->PFR_0 = 0x00;	\
		PFR->PFR_1 = 0x00;	\
		PFR->PFR_2 = 0x00;	\
		PFR->PFR_3 = 0x00;	\
	}

/*	On Board LED 	1 bit	*/
#define	SETBOARDLED()	\
	{	\
		DDR->PORT3 |= 0x20; /* Data Direction */	\
		PDR->PORT3 |= 0x20;	/* LED OFF	*/	\
	}

#define	REVERSELED()	\
	{	\
		PDR->PORT3 ^=0x20;	\
	}

/*	8bit Dip Switch 8bit	J2:1-8 (A0-A7)	*/
#define	SETDIPSWITCH()	\
	{	\
		DDR->PORT0 = 0x00;/* DipSwitch Input 0*/\
	}

#define	GETDIPSWITCH()		((~PDR->PORT0)&0xFF)

/*	7seg LED 15bit Hight/Low	J2:23-38 (D0-D15)	*/
#define	SET7SEGLED()	\
	{	\
		DDR->PORT4 = 0xFF;	/*Output*/	\
		DDR->PORT5 = 0xFF;	/*Output*/	\
	}

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

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

#define	GET7SEGHIGH()		(PDR->PORT5 &0xFF)

#define	GET7SEGLOW()		(PDR->PORT4 &0xFF)

/*	Tact Switch :4 Interrupt Request Switch	J1:29-32() 01Fall 23Rise	*/
#define	SETIRQ()	\
	{	\
		DDR->PORT6 = 0x00;/*Input*/	\
		INTER->ENIR = 0x0F;/*IRQ0-3 Enable*/	\
		INTER->ELVR = 0x00AF;	\
		ICR[0] = 0x10;	\
		ICR[1] = 0x10;	\
		ICR[2] = 0x10;	\
		ICR[3] = 0x10;	\
	};

/*	Timer 1 msec interrupt	*/
#define	SETTIMER()	\
	{	\
		ICR[23] = 0x10;\
		PPG0->PCSR = 0xC00;\
		PPG0->PDUT = 0x600;\
		PPG0->PCN=0xC02C;\
	}
#define	CNTE	0x8000			//1	15	Enalbe
#define	STGR	0x4000			//1	14	Software Triger
#define	MDSE	0x2000			//0	13	0:PWM/1:OneShot
#define	RTRG	0x1000			//	12	Retrigger?
#define	CKS		0x0C00			//2	10-11	0 1(1/4) 2(1/16) 3(1/64)
#define	PGMS	0x0200			//	 9	Mask PGM Output
#define	EGS		0x00C0			//	6-7	Triger Edge 
#define	IREN	0x0020			//1	 5	Interrupt Enable
#define	IRQF	0x0010			//0	 4	Interrupt Flag
#define	IRS		0x000C			//3	2-3	Interrupt Reason
#define	OSEL	0x0001			//	 0	Outp put 


#define	CLEARTIMERINTERRUPT()	(PPG0->PCN &=~IRQF)
#define	CLEARIRQ0()	(INTER->EIRR &= 0xFE)
#define	CLEARIRQ1()	(INTER->EIRR &= 0xFD)
#define	CLEARIRQ2()	(INTER->EIRR &= 0xFB)
#define	CLEARIRQ3()	(INTER->EIRR &= 0xF7)

/*	AD Converter	J1:1(AN0)*/
#define	SETAD()	\
	{	\
		ADC->ADCH = 0x01;		/*AIN0*/	\
		ADC->ADCT = 0x00;		/*AIN0*/	\
	}

#define	STARTAD0()	\
	{	\
		ADC->ADCT |= 0x0100;	/* Start AD*/\
		while(ADC->ADCT & 0x08);	\
	}

#define	GETAD0()			((ADC->ADAT0>>2)&0x0FF)

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

#define	PUTCHAR(data)	\
	{	\
	}

#define	WAITUART()	\
	{	\
	}

#define GETCHAR()	(0)

#define	CLEARSENDFLAG()	{}

#define	CLEARRECFLAG()	{}

/*	Interrupt Handler	*/
#define	ENABLEINTERRUPT()	\
	{	\
		_enableInterrupt();\
	}

#define	DISABLEINTERRUPT()	\
	{	\
	}

#define	DUMMYINTERRUPT()	\
	{	\
	}

//#define	INTERRUPTATTRIBUTE	__attribute__ ((interrupt))
//#define	INTERRUPTATTRIBUTE	__attribute__ ((interrupt_handler))
#define	INTERRUPTATTRIBUTE


