#include "system.h"
/********************************************************************************/
/***** InitSPI_for_AD9866()													*****/
/********************************************************************************/
void InitSPI_for_AD9866(void)
{ unsigned char temp =0;
	

	*pSPI_CTL 	&=  (~SPE);					// disable SPI
	*pSPI_FLG 	|=  (FLS1|FLG1);			// Enable PF1 as SlaveSelect, active low
	*pSPI_BAUD  =   BAUD_CONF;			
	*pSPI_CTL   =   MSTR | SIZE | GM | (TIMOD & 1);
	asm("ssync;");
	*pSPI_CTL 	|=  SPE;					// enable SPI
	asm("ssync;");

	
	temp = Write_Byte_to_AD9866(0, 0x80);	// set SPI to 4-wire and MSB first
	temp = Read_Byte_from_AD9866(0);	// set SPI to 4-wire and MSB first	
	printf("%d",temp);
}




/********************************************************************************/
/***** Write_Byte_to_AD9866(address, data)									*****/
/***** sends a single configuration byte via SPI to the AD9866 register		*****/
/***** only polling is done, no interrupts. Return value is RX reg			*****/
/********************************************************************************/
unsigned char Write_Byte_to_AD9866(unsigned char AD9866_address, unsigned char AD9866_data)
{	unsigned short Instr_Sequence;
	
	// instruction sequence is 1 instruction byte followed by a configuration byte.
	// both are combined into a single 16-bit transfer over the SPI
	
	Instr_Sequence = 0x0000 | (unsigned short)AD9866_address<<8 | (unsigned short)AD9866_data;	// set I7 to Write, and N1:0 to 1byte transfer
	
	*pSPI_TDBR = Instr_Sequence;
	asm("ssync;");
	
	while (!(*pSPI_STAT & 0x0001));			// wait until single-word transfer bit is set

	while (!(*pSPI_STAT & 0x0020));			// wait until receive buffer full bit is set

	return (unsigned char)(*pSPI_RDBR & 0x00FF);		// return LSByte
}


/********************************************************************************/
/***** data = Read_Byte_from_AD9866(address)								*****/
/***** reads a single configuration byte via SPI from the AD9866 register	*****/
/***** only polling is done, no interrupts. 								*****/
/********************************************************************************/
unsigned char Read_Byte_from_AD9866(unsigned char AD9866_address)
{	unsigned short Instr_Sequence;
	
	// instruction sequence is 1 instruction byte followed by a zero byte.
	// both are combined into a single 16-bit transfer over the SPI
	
	Instr_Sequence = 0x8000 | (unsigned short)AD9866_address<<8;	// set I7 to Read, and N1:0 to 1byte transfer
	
	*pSPI_TDBR = Instr_Sequence;
	asm("ssync;");
	
	while (!(*pSPI_STAT & 0x0001));			// wait until single-word transfer bit is set

	while (!(*pSPI_STAT & 0x0020));			// wait until receive buffer full bit is set

	return (unsigned char)(*pSPI_RDBR & 0x00FF);		// return LSByte
}

