/*****************************************************************/
/*	FILE NAME : pll_4113.c											 */
/*	Description : pll setting routine									 */
/*	Author : H.S.MOON												 */
/*	Date : 01-09-2009												 */
/*	Revision : 0.0.1												 */
/*****************************************************************/


#include "includes.h"

INT8U PLL_OUT = 0x00;
//INT8U index=0;

void SetPLL(INT32U freq) 
{
	PLL_DATA_PORT  &= ~(1<<PLL_DATA_BIT);
	PLL_CLK_PORT &= ~(1<<PLL_CLK_BIT );
	PLL_EN_PORT &= ~(1<<PLL_EN_BIT );
	INT32U R0,R1,R2,R3;
	if(freq == 1740000) {
		printk("1freq = %d\r\n",freq/1000);
	//	R0 = 0x570000;
	//	R1 = 0x148051;
	//	R2 = 0x3C2;
	//	R3 = 0x3C7;
		R0 = 0x2B8000;
		R1 = 0x1440A1;
		R2 = 0x3C2;
		R3 = 0x3C7;
		pdata_out(R3);
		pdata_out(R2);
		pdata_out(R1);
		pdata_out(R0);
//PLL_DATA_PORT  |= (1<<PLL_DATA_BIT);
//PLL_CLK_PORT |= (1<<PLL_CLK_BIT );
//PLL_EN_PORT |= (1<<PLL_EN_BIT );
	}
	else if(freq == 1742500) {
		printk("2freq = %d\r\n",freq/1000);
	//	R0 = 0x570004;
	//	R1 = 0x148009;
	//	R2 = 0x3C2;
	//	R3 = 0x3C7;
		
		R0 = 0x2B8004;
		R1 = 0x144011;
		R2 = 0x3C2;
		R3 = 0x3C7;
		pdata_out(R3);
		pdata_out(R2);
		pdata_out(R1);
		pdata_out(R0);
	//PLL_DATA_PORT  &= ~(1<<PLL_DATA_BIT);
	//PLL_CLK_PORT &= ~(1<<PLL_CLK_BIT );
	//PLL_EN_PORT &= ~(1<<PLL_EN_BIT );
	}
	
//	Delay_ns(5);
}

void SetPLL4153(INT32U freq, INT16U ref, INT8U pre, INT8U step)
{
	INT32U	bcnt,rcnt;
	INT32U	acnt;

	//INT32U 	R_set_data=0;
	//INT32U 	N_set_data=0;
	//INT32U 	F_set_data=0x27e024L;
	INT8U	R_LAT=0x00;
	INT8U	N_LAT=0x01;
	INT8U	F_LAT=0x03;

	INT32U R_data_n=0;
	INT32U N_data_n=0;
	INT32U F_data_n=0x27e024L;
//	INT32U I_data_n=0;

	unsigned char i;
	
	rcnt = (unsigned int)(ref / step);
	bcnt = (unsigned int)(freq/step/pre);
	acnt = (unsigned char)(freq/step)%pre;
//	printk("pll out\n");
	printk("\r\nfreq = %ld, ref = %d, pre = %d, step = %d\r\n", (unsigned long)freq,(unsigned int)ref,(unsigned char)pre,(unsigned char)step);

	R_data_n = ( R_data_n<<16) |((rcnt<<2)|R_LAT);
	N_data_n = (N_data_n <<21) |(bcnt<<8)|((acnt<<2)|N_LAT);
	F_data_n = (F_data_n<<2) |F_LAT;

	pdata_out(F_data_n);
	pdata_out(N_data_n);
	pdata_out(R_data_n);

	Delay_ns(5);
	
}

void pclk_out()
{
	PLL_CLK_PORT |= (1<<PLL_CLK_BIT );
//	PLL_OUT |= FWD_PLL_CLK;
//	PLL_ADDR = PLL_OUT;

	Delay_ns(25);
	PLL_CLK_PORT &= ~(1<<PLL_CLK_BIT );
//	PLL_OUT  &= ~(FWD_PLL_CLK);
//	PLL_ADDR = PLL_OUT;

}

void platch_out()
{

	PLL_EN_PORT |= (1<<PLL_EN_BIT );
	Delay_ns(10);
	PLL_EN_PORT &= ~(1<<PLL_EN_BIT );

}

void pdata_out(INT32U dat)
{
	INT8U i;
	
	for(i=0; i<ADF4153; i++) { 
		if((dat & 0x800000)) {
			PLL_DATA_PORT  |= (1<<PLL_DATA_BIT);
			printk(" 1 ");
		}
		else {
			PLL_DATA_PORT  &= ~(1<<PLL_DATA_BIT);
			printk(" 0 ");
		}
		
		
		Delay_ns(3);

		pclk_out();

		PLL_DATA_PORT  &= ~(1<<PLL_DATA_BIT);
	//	printf(" 0 ");
		dat <<= 1;	
	}
	platch_out();

	printk("\r\n");
}

