/************************************************************************/
/* 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()	\
	{	\
		MODULE->MSTCR2 = 0xC0C0;/*0xC0C0（AD0/AD1/CMT/MTU) 0 All activate	*/	\
	}

/*	On Board LED 	1 bit	*/
#define	SETBOARDLED()	\
	{	\
		PORT->PECRL1 &= 0x0FFF;		/*P14-15 for IO*/	\
		PORT->PEIORL  |= (1<<15);	/*P15 Output*/	\
		/*PORT->PEDRL = (1<<15);*io_port_E = (1<<15);*/	\
	}

#define	REVERSELED()	\
	{	\
		PORT->PEDRL ^= 0x8000;/*1<<15*/;	\
	}

/*	8bit Dip Switch 8bit	J2:1-8 (A0-A7)	*/
#define	SETDIPSWITCH()	\
	{	\
	PORT->PCCR = 0x0000;	/* Port C IO port*/	\
	PORT->PCIOR = 0x0000;	/* All Input*/	\
	}
	
#define	GETDIPSWITCH()		((~PORT->PCDR) & 0xFF)

/*	7seg LED 15bit Hight/Low	J2:23-38 (D0-D15)	*/
#define	SET7SEGLED()	\
	{	\
	PORT->PDCRL1 = 0x0000;	/*PD0-15 Default*/	\
	PORT->PDCRL2 = 0x0000;	/*All In Out */	\
	PORT->PDIORL = 0xFFFF;	/*All Output*/	\
	}

#define	PUT7SEGHIGH(val)	\
	{	\
		PORT->PDDRL = (PORT->PDDRL&0x00FF)| ((val<<8)&0xFF00);	\
	}
//	PORT->PDDRL =((digit[out/16]<<8) &0xFF00)| digit[out%16];

#define	PUT7SEGLOW(x)		\
	{	\
		PORT->PDDRL = (PORT->PDDRL&0xFF00) | (x &0x00FF);	\
	}

#define	GET7SEGHIGH()		((PORT->PDDRL/16)&0xFF)

#define	GET7SEGLOW()		((PORT->PDDRL)&0xFF)

/*	Tact Switch :4 Interrupt Request Switch	J1:29-32() 01Fall 23Rise	*/
#define	SETIRQ()	\
	{	\
		PORT->PBIOR=0x0000;				\
		PORT->PBCR1=0x0000;				\
		PORT->PBCR2=0x0550;	/*PB2-5 IRQ0-3*/	\
		IREQ->ICR1 = 0x00F0;	/*IRQ0-3 Edge	*/	\
		IREQ->ICR2 = 0x0500;	/*01- Fall 0000 23 Rise 0101*/	\
		IREQ->IPRA =0xFFFF;	/*High Priority*/	\
		MODULE->MSTCR2 = 0xC0C0;/*0xC0C0（AD0/AD1/CMT/MTU) 0 All activate	*/	\
		/* F0E0 1111 0000 1111 0000  MTU 0x2000 CMT 0x1000   14-15=1 11-8=0 7-6=1 AD1=0x0020 AD0=0x0010 0-3=0*/	\
	};

/*	Timer 1 msec interrupt	*/
/* 24Mhz 24000=1msec Mode3 CPU 48MHz Peri 24MHz M*********/
#define	SETTIMER()	\
	{	\
		CMT->CMSTR = 0x0000;	/*0x01 CMT0 0x02 CMT1 start*/	\
		CMT->CMCNT_0 = 0;	/*counter value default 0*/	\
		CMT->CMCOR_0 =24000/512;	/*750/4 47 1m sec 60 23437	0.5sec		(24000000/8 / 1000)=3000   1/512 = 46 ) */	\
		CMT->CMCSR_0= 0x0043;	/*0x43 1m 0x00 01000011   Interrupt  3000 = 1/8 効果がないことが判明　常に1/512のようだ  1/512*/	\
		IREQ->IPRG =0x00FF;		/*CMT0-1 Priority*/	\
		CMT->CMSTR = 0x0001;	/*Start CMT0*/	\
	}
	
#define	CLEARTIMERINTERRUPT()	\
	{	\
		int data = CMT->CMCSR_0;	\
		CMT->CMCSR_0= 0x0043;	\
	}
#define	CLEARIRQ0()	(0)
#define	CLEARIRQ1()	(0)
#define	CLEARIRQ2()	(0)
#define	CLEARIRQ3()	(0)

/*	AD Converter	J1:1(AN0)*/
#define	SETAD()	\
	{	\
		/*PORTF Not Setting*/	\
		ADC->ADCR_0= 0x07;	/*NoTrigger A/D Stop*/	\
		ADS->ADCSR_0=0x08;	/*SingleMode AN0*/	\
	}

#define	STARTAD0()	\
	{	\
		ADC->ADCR_0 =0x17;	/*|= 0x10; 	ADST=1;*/	\
		while((ADS->ADCSR_0 & 0x80)==0);/*W*ait ADF 0x80 Single AN0*/\
		ADS->ADCSR_0 =0x08;	/*Clear*/	\
	}

#define	GETAD0()			((ADD->ADDR0>>8)&0x00FF)

/*	RS232C/UART	*/
#define	SETUART()	\
	{	int i;\
		MODULE->MSTCR1 &= ~0x2;  /*SCI1 Activate*/	\
		PORT->PACRL2 = ((1<<8) | (1<<6));	/* Enable TXD1,RXD1*/	\
		/* 9600bps (CKS = 00, BRR=77) Async 8bit  non Parity 1stop	*/	\
		SCI1->SCR = 0x0;	\
		SCI1->SMR = 0x0;	\
		SCI1->BRR = 77;	\
		for(i=0;i< (SCI1->BRR *1000);i++){}	\
		SCI1->SCR = ((1<<6)|(1<<5)|(1<<4));  /* RecInerrupt Rec Send Act*/\
		IREQ->IPRF |= 0xF;	\
							\
		MODULE->MSTCR1 &= ~0x1;/*SCI0 Aactivate*/	\
		PORT->PACRL2 |= ((1<<2) | (1<<0));/*Enable TXD0,RXD0*/	\
		/* 9600bps (CKS = 00, BRR=77 24MHz?) Async 8bit  non Parity 1stop	*/	\
		SCI0->SCR = 0x0;	\
		SCI0->SMR = 0x0;	\
		SCI0->BRR = 77;		\
		for(i=0;i<(SCI0->BRR*1000);i++){}	\
		SCI1->SCR = ((1<<6)|(1<<5)|(1<<4));  /* RecInerrupt Rec Send Act*/	\
		IREQ->IPRF |= (0xF<<4);	\
	}

#define	PUTCHAR(data)	\
	{	\
		SCI0->TDR = data;	\
		SCI0->SSR = SCI0->SSR & (~0x80);	/*Clear Empty?*/	\
	}

#define	WAITUART()	\
	{	\
		while((SCI0->SSR & (1<<7)) == 0){/*Wait TDRE*/	\
			if((SCI0->SSR & (1<<5)) != 0)	\
				SCI0->SSR &= ~(1<<5);	/*Reset Overrun?*/	\
		}	\
	}

#define GETCHAR()	(SCI0->RDR)	/*Need?RDRF*/

#define	CLEARSENDFLAG()	{}

#define	CLEARRECFLAG()	{}

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

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

#define	DUMMYINTERRUPT()	\
	{	\
	}

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



//void __attribute__ ((interrupt_handler)) SCI0_RXI_func(void)	// GNU Interrupt Function
////void SCI0_RXI_func()
//{
//  int recv_char;  
//  recv_char = SCI0->RDR;
//  SCI0->SSR &= ~(1<<6);
//  // 受信したデータをループバックする
//  while((SCI0->SSR & (1<<7)) == 0){
//    // オーバランエラーが発生したらリセット
//    if((SCI0->SSR & (1<<5)) != 0)
//      SCI0->SSR &= ~(1<<5);
//  }
//  SCI0->TDR = recv_char;
//  SCI0->SSR = SCI0->SSR & ~(1<<7);?????????
//}
//void __attribute__ ((interrupt_handler)) SCI1_RXI_func(void)	// GNU Interrupt Function
////void SCI1_RXI_func()
//{
//  int recv_char;
//  recv_char = SCI1->RDR;
//  SCI1->SSR &= ~(1<<6);
//  
//  // 受信したデータをループバックする
//  while((SCI1->SSR & (1<<7)) == 0){
//    // オーバランエラーが発生したらリセット
//    if((SCI1->SSR & (1<<5)) != 0)
//      SCI1->SSR &= ~(1<<5);
//  }
//  SCI1->TDR = recv_char;
//  SCI1->SSR = SCI1->SSR & ~(1<<7);
//}
