#include "inc\44b0x.h"
#include "DataType.h"

#define	EXT_OSC_CLK	10000000

unsigned int MCLK = 20000000;

void ChangePllValue(int mdiv, int pdiv, int sdiv)
{
	int i = 1;		
	
	rPLLCON = (mdiv<<12)|(pdiv<<4)|sdiv;
	
	while(sdiv--)
		i *= 2;	
	
	MCLK = (EXT_OSC_CLK*(mdiv+8))/((pdiv+2)*i);		
}


/****************************************************************************
【功能说明】IO端口功能、方向设定
****************************************************************************/
void PortInit(void)
{
	// PORT A GROUP
	/*  BIT 9	8	7	6	5	4	3	2	1	0	*/
	/*  A24	  A23	A22	A21	A20	A19	A18	A17	A16	A0	*/	      
	/*  0		1	1	1	1	1	1	1	1	1	*/
	rPCONA = 0x1ff;	

	// PORT B GROUP
	/*  BIT 10	9		8		7		6		5		4		3		2		1		0		*/
	/*	/CS5	/CS4	/CS3	/CS2	/CS1	nWBE3	nWBE2	/SRAS	/SCAS	SCLK	SCKE	*/
	/*	EXT		NIC		8019	IDE		D12		NC		NC		Sdram	Sdram	Sdram	Sdram	*/
	/*	1,		1,   	1,   	1,    	1,    	0,      0,		1,    	1,    	1,   	1       */
	rPDATB = 0x04f;
	rPCONB = 0x7cf;    
    
	//PORT C GROUP
	//BUSWIDTH=16													*/
	/*  PC15	14		13		12		11		10		9		8	*/
	/*	o		o		RXD1	TXD1	o		o		o		o	*/
	/*	NC		NC		Uart1	Uart1	NC		NC		NC		NC	*/
	/*  01		01		11		11		01		01		01		01	*/

	/*  PC7		6		5		4		3		2		1		0	*/
	/*  o		o		o		o		o		o		o		o	*/
	/*  NC		NC		NC		NC		LED		LED		LED		LED	*/
	/*  01		01		01		01		01		01		01		01	*/
	rPDATC = 0x0000;	//All IO is low
	rPCONC = 0x5f555555;	
	rPUPC  = 0x3000;	//PULL UP RESISTOR should be enabled to I/O

	//PORT D GROUP
	/*  PORT D GROUP(I/O OR LCD)										*/
	/*  BIT7		6		5		4		3		2		1		0	*/
	/*  VF			VM		VLINE	VCLK	VD3		VD2		VD1		VD0	*/
	/*	01			01		01		01		01		01		01		01	*/
	rPDATD= 0x55;
	rPCOND= 0xaaaa;	
	rPUPD = 0x00;
	//These pins must be set only after CPU's internal LCD controller is enable
/////////////////////////////////////////////////////	
	//PORT E GROUP 
	/*  Bit 8		7		6		5		4		3		2		1		0		*/
	/*	ENDLAN		o		o		TOU2	o		o		RXD0	TXD0	FOUT	*/ 
	/*  00			01		01		10		01		01		10		10		11		*/	
	rPDATE	= 0x157;				//Beep = 10
	rPCONE	= 0x556b;				
	rPUPE	= 0xff;
	
	//PORT F GROUP
	/*  Bit8		7		6		5		 4		3		2		1		0		*/   
	/*  IISCLK		IISDI	IISDO	IISLRCK	Input	Input	Input	IICSDA	IICSCL	*/
	/*	100			100		100		100		00		00		00		10		10		*/	
	rPDATF = 0x0;
	rPCONF = 0x24900a;	 
	rPUPF  = 0x1d3;

	//PORT G GROUP
	/*  BIT7		6		5		4		3		2		1		0		*/
	/*	EINT7		EINT6	EINT5	EINT4	EINT3	EINT2	EINT1	EINT0	*/
	/*  KEY3		KEY2	KEY1	KEY0	NIC		EXT		IDE		USB		*/
	/*	11 			11      11      11      11      11      11      11		*/
	rPDATG = 0xff;
	rPCONG = 0x000f;	//enable extint0 extint1
	rPUPG  = 0x00;		//enabled pull up register

	rSPUCR= 0x7;  //D15-D0 pull-up disable
	//rSPUCR= 0x6;  //D15-D0 pull-up enable
	
	rSYSCFG = 0x0e;				//使用8K字节的指令缓存,write buffer disable		 
	rNCACHBE0 = 0xc0000000;		//定义非Cache区,从0x0000000到0xbffffff	烧写FLASH时不能对FLASH cache
	rEXTINT = 0x12;				//所有的外部硬件中断为低电平触发, extint0 为下降沿，extint1为上升沿	
	rINTCON = 5;				// non vector mode, irq enable
	rINTMOD = 0;				//all are irq mode
//		rINTMSK = BIT_EINT1|BIT_GLOBAL;
		
}

/*********************************************************/
#define RTC_YEAR0		(0x03)		//年
#define RTC_MONTH0		(0x02)		//月
#define RTC_DAY0		(0x0e)		//日
#define RTC_DATE0		(0x06)		//星期SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
#define RTC_HOUR0		(0x08)		//小时
#define RTC_MIN0		(0x08)		//分
#define RTC_SEC0		(0x08)		//秒

#define RTC_YEAR1		(0x03)		//年
#define RTC_MONTH1		(0x03)		//月
#define RTC_DAY1		(0x0e)		//日
#define RTC_DATE1		(0x06)		//星期－SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
#define RTC_HOUR1		(0x08)		//小时
#define RTC_MIN1		(0x08)		//分
#define RTC_SEC1		(0x08)		//秒

#define	RTC_RW_EN()	rRTCCON = 1		//|= 1
#define	RTC_RW_DS()	rRTCCON &= 0xfe

void RtcSetDay(TIME_STRUC *time)
{
	RTC_RW_EN();				//RTC读写使能，选择BCD时钟、计数器，无复位，1/32768		
	rBCDYEAR = time->year;;		//年
    rBCDMON  = time->month;		//月
    rBCDDAY  = time->day;		//日	
	RTC_RW_DS();
}

void RtcSetWeek(TIME_STRUC *time)
{
	RTC_RW_EN();
	rBCDDATE = time->weekday;
	RTC_RW_DS();
}

void RtcSetTime(TIME_STRUC *time)
{
	RTC_RW_EN();				//RTC读写使能，选择BCD时钟、计数器，无复位，1/32768		
	rBCDHOUR = time->hour;		//小时
    rBCDMIN  = time->min;		//分
    rBCDSEC  = time->sec;		//秒
	RTC_RW_DS();
}


void RtcInit(TIME_STRUC *time)
{
    RTC_RW_EN();				//RTC读写使能，选择BCD时钟、计数器，无复位，1/32768
    rBCDYEAR = time->year;;		//年
    rBCDMON  = time->month;		//月
    rBCDDAY  = time->day;		//日
    rBCDDATE = time->weekday;	//星期
    rBCDHOUR = time->hour;		//小时
    rBCDMIN  = time->min;		//分
    rBCDSEC  = time->sec;		//秒
    
    rTICNT = 0x80+127;		//enable tick timer interrupt, set tick timer interrupt time = (127+1)/128 second	
    RTC_RW_DS();	
}

void RtcAlarm(TIME_STRUC *time)  
{
	rALMYEAR = time->year;		//年
	rALMMON  = time->month;		//月
	rALMDAY  = time->day;		//日
	rALMHOUR = time->hour;		//小时
	rALMMIN  = time->min;		//分
	rALMSEC  = time->sec; 		//秒

	rRTCALM = 0x7f;		//全局警报使能控制，年、月、日、星期、时分秒警报使能控制
}

void RtcGetTime(TIME_STRUC *time)
{
	U8 m;

	RTC_RW_EN();		//RTC读写使能
	time->year = 2000 + rBCDYEAR;
	time->weekday = rBCDDATE;

	m = rBCDMON;	
	time->month = m;//(m>>4)*10 + (m&0xf);	//BCD码转十进制

	m = rBCDDAY;	
	time->day = m;//(m>>4)*10 + (m&0xf);	//BCD码转十进制	

	m = rBCDHOUR;	
	time->hour = m;//(m>>4)*10 + (m&0xf);	//BCD码转十进制
	
	m = rBCDMIN;	
	time->min = m;//(m>>4)*10 + (m&0xf);	//BCD码转十进制

	m = rBCDSEC;			
	time->sec = m;//(m>>4)*10 + (m&0xf);	//BCD码转十进制
	
    RTC_RW_DS();		//RTC读写禁止(降低功率消耗)，选择BCD时钟、计数器，无复位，1/32768    

}

void InitSysTimer(U8 Freq)
{		
	rTCON   &= 0xf0ffffff;			//clear manual update bit, stop Timer 5 	
	rTCFG0  &= 0xff00ffff;			//定时器4/5的预分频为16
	rTCFG0  |= (16-1)<<16;
	rTCFG1  &= 0xff0fffff;			//定时器5的MUX为1/8
	rTCFG1  |= 2<<20;	
	rTCNTB5  = MCLK/(Freq*16*8);	//定时器5的重装值,在ENABLE之前设定	
	rTCON   |= 0x02000000;			//定时器5的MANUAL UPDATE BIT设为1
	rTCON   &= 0xf0ffffff;			//MANUAL UPDATE BIT清零
	rTCON   |= 0x05000000;			//定时器5 start, 设为INTERVAL模式
}
	
