/************************************************************************/
/* 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()	\
	{	\
		BUS->VSWC=0x1;		/* No External Bus Wait	*/\
		SYSTEM->PRCMD = 0;	\
		DEBUG->OCDM=0x00;	/* Inhibit onchip debug module	*/\
		WATCHDOC->WDTM2=0x00;	/* Inhibit Watch Dog timer */	\
		/*PLL 4x clock 20MHz */	\
		SYSTEM->PRCMD = 0;	\
		CLOCK->PCC = 0;	\
		while(CLOCK->LOCKR);	\
		CLOCK->PLLCTL |= (1<<1);	\
	}

/*	On Board LED 	1 bit	*/
#define	SETBOARDLED()	\
	{	\
		PORTDLM->PCT &= ~(1<<6);/*PMCT*/	\
		PORTDLMC->PCT = 0x00;/*PMCCT*/	\
	}

#define	REVERSELED()	\
	{	\
		PORTDL->PCT ^= 0x40;/*PCT*/	\
	}

/*	8bit Dip Switch 8bit	J2:1-8 (A0-A7)	*/
#define	SETDIPSWITCH()	\
	{	\
		PORTMC->P9L = 0;		/* *PMC9LIn Out*/	\
		PORTM->P9L = 0xFF;	/* *PM9LInput*/	\
	}

#define	GETDIPSWITCH()		((~PORT->P9L) & 0xFF)

/*	7seg LED 15bit Hight/Low	J2:23-38 (D0-D15)	*/
#define	SET7SEGLED()	\
	{	\
		PORTDLMC->PDLH=0;	/* *PMCDLH Inout Output*/	\
		PORTDLMC->PDLL=0;	/* *PMCDLL */\
		PORTDLM->PDLH=0;	/* *PMDLH Output*/	\
		PORTDLM->PDLL=0;	/* *PMDLL */\
	}

#define	PUT7SEGHIGH(x)	\
	{	\
		PORTDL->PDLH=x;	\
	}

#define	PUT7SEGLOW(x)		\
	{	\
		PORTDL->PDLL=x;	\
	}


#define	GET7SEGHIGH()		((PORTDL->PDLH)&0xFF)

#define	GET7SEGLOW()		((PORTDL->PDLL)&0xFF)

/*	Tact Switch :4 Interrupt Request Switch	J1:29-32() 01Fall 23Rise	*/
#define	SETIRQ()	\
	{	\
		PORTM->P0 = 0xFF;	/*InputMod *PM0e*/\
		PORTMC->P0 = 0x78;		/*P96 INTP0-3 INPUT *PMC0*/	\
		PORTFC->P0 = 0x00; 		/*PFC03 for INPUT *PFC00*/	\
		PORTINTF->P0 = 0x18;		/* *INTF0Fall/Fall/Rise/Rise bit3-6*/	\
		PORTINTR->P0 = 0x60;		/* *INTR0Fall/Fall/Rise/Rise*/	\
		PORTFC->P0 = 0;		/*INTP0 input*PFC0*/	\
		IREQ->PIC0	= 0;/*0x07;*/	\
		IREQ->PIC1	= 0;/*0x07;*/	\
		IREQ->PIC2	= 0;/*0x07;*/	\
		IREQ->PIC3	= 0;/*0x07;*/	\
	};

/*	Timer 1 msec interrupt	*/
			/* 1sec = 20,000,000 1msec = 20,000 20MHz */
			/* 1sec = 5,000,000 1msec = 5,000 5MHz =64*78 */
#define	SETTIMER()	\
	{	\
		TMP1->CTL0=0;	/*TP1CTL0.7 =0 TMP1 Stop*/	\
		IREQ->TP1CCIC0=0x40;	/*TP1CCIC0 = 0x40 INTTP1CC0 interrupt priority 0*/	\
		/* TP1CTL0=0x07 internal counter clock fxx/512*/	\
		TMP1->CTL0 = 0x00;/*0 div 0x06;	 256分周  0x06 fxx/256(n=1,3,5)*/	\
		TMP1->CTL1 = 0x00;	/* Interval timer  20MHz */	\
		TMP1->CCR0 = 78;/* 78 1msec O?K (1000000*20/512)-1;	500msec (1/256)1000ms (fxx/512)*/	\
		TMP1->CCR1 = 0xFFFF;	/*not used*/	\
		IREQ->TP1CCIC0=0x00; /*Level0TP1CCMK0 = 0;	INTTP1CC0 */	\
		TMP1->CTL0=0x86;	/*TP1CE=1;TMP1 start Timer  Interrupt*/	\
	}
	
#define	CLEARTIMERINTERRUPT()	{}
#define	CLEARIRQ0()	{}	
#define	CLEARIRQ1()	{}
#define	CLEARIRQ2()	{}
#define	CLEARIRQ3()	{}
/*	AD Converter	J1:1(AN0)*/
#define	SETAD()	\
	{	\
		PORTM->P7L |= 0x01;	/*P70 ANI0 input*/	\
		ADC->ADA0M0 = 0x00;	/*0x80 Stop/Permit  0x01 Stop/Act 0x20 One Shot select mode*/	\
		ADC->ADA0M1 = 0x01;	/*20MHz settting shortest*/	\
		ADC->ADA0S = 0x00;	/* Select/Scan SNI0*/	\
	}

#define	STARTAD0()	\
	{	\
		ADC->ADA0M0 = 0xA0;while(ADC->ADA0M0 &0x01) ;	\
	}

#define	GETAD0()			((ADD->ADA0CR0 >> 8) & 0xFF)

/*	RS232C/UART	*/
#define	SETUART()	\
	{	\
		/*UA0CTL1 = 0x01;UA0CTL2 = 0x82;	/* 38400 bps*/	\
		/*UA0CTL0 = 0x92;  /* パリティなし、データ8ビット、ストップビット1ビット*/	\
		/*PMC3 |= 0x0003; PFC3 &= ~(0x0003);  /* ポートをUARTに設定*/	\
		/*UA0CTL0 |= (1<<6) | (1<<5);  /*送受信許可*/	\
		/*UA0RIC = 0x00;  /* UART0受信割り込みイネーブル*/	\
	}

#define	PUTCHAR(data)	\
	{	\
		/*UA0TX = data;	*/	\
		/*while((UA0TIC & (1<<7)) == 0);*/	\
		/*UA0TIC &= ~(1<<7);*/	\
	}

#define	WAITUART()	\
	{	\
	}

#define GETCHAR()	(0)	

#define	CLEARSENDFLAG()	{}

#define	CLEARRECFLAG()	{}

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

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

#define	DUMMYINTERRUPT()	\
	{	\
	}

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


