#include<cdefBF548.h>
#include<ccblkfn.h>
#include<sys/exception.h>

#define SystemCLK	 100000		//Value in KHz
#define VCO_CoreCLK  1			//Value of VCO/CCLK - value can be 1, 2, 4, 8
#define VCO			 500000		//Value in KHz
#define CLKin		 25000

void ConfigurePLL(void);
void SetUpPortMuxing(void);
void SetUpInterrupts(void);
void SetupandEnableHostPort(void);


//write data in location 0xFF900060 for the Host to read data.
//For Host write operations, look for data in location 0xFF900060
EX_INTERRUPT_HANDLER (HostDMA);

void main()
{
	ConfigurePLL();
	SetUpPortMuxing();
	SetUpInterrupts();
	SetupandEnableHostPort();
	
	while(1);
}

void ConfigurePLL()
{
	*pSIC_IWR0 = 0x1;
	*pPLL_CTL = SET_MSEL(VCO / CLKin);
	ssync();
	idle();
	*pPLL_DIV = SET_SSEL(VCO / SystemCLK) | ((VCO_CoreCLK)&0x30);
	asm("ssync;");
}

void SetUpPortMuxing()
{
	*pPORTD_FER = 0xFFFF;
	*pPORTD_MUX = 0x55555555;
	*pPORTG_FER = 0xE0;
	*pPORTG_MUX = 0x5400;
	*pPORTH_FER = 0x18;
	*pPORTH_MUX = 0;
	
}

void SetUpInterrupts()
{
	register_handler(ik_ivg9,HostDMA);
	*pSIC_IMASK1 |= 0x40;
}

void SetupandEnableHostPort()
{
	*pHOST_CONTROL = EHR | EHW | 0x4 | 0x1;	//Enable Host Reads, Enable Host Writes, Data Size = 16, Host Port Enable
}

EX_INTERRUPT_HANDLER (HostDMA)
{
	*pDMA14_IRQ_STATUS |= 1;
	ssync();
	*pHOST_STATUS = 0x8;
}
