/**********************************************************************************/
/**********************************************************************************/

#include <aduc845.h>
 #include <intrins.h>

/**********************************************************************************/
//#define uchar unsigned char 
//#define uint  unsigned int

#define	Fclk	3686400
#define	BitRate	19200
#define	BRR		(Fclk/(16*Bitrate))-1  //BRR=11

#define	timerdiv	1
#define	timerval	256-(Fclk/timerdiv)/100000			// timeout each 10us TIMERAL=219.

#define nRF2401_ON		PWR_UP = 1
#define nRF2401_OFF		PWR_UP = 0
#define CE_HIGH			CE = 1
#define CE_LOW			CE = 0
//extern UINT8 ChangMode;
/**********************************************************************************/

unsigned char code nRF2401AddrLen = 4;		//
unsigned char code nRF2401Addr1[5] = { 0x33, 0x33, 0x33, 0x33, 0x33  };		//
unsigned char code nRF2401Addr2[5] = { 0x33, 0x33, 0x33, 0x33, 0x33  };		//

unsigned char code CH1DataLen = 25;	//Addr1Len + CH1DataLen + CRCLen <= 32Byte
unsigned char code CH2DataLen = 25;	//every package data must match the Len, shorter or longer can not be sent

bit RX2_EN = 0;

//uchar RXData[26];

//#define RCV_FIFO_LEN 25                    /* size of serial receiving buffer      */
//unsigned char xdata RcvFifo[ RCV_FIFO_LEN ] = {0, 0};
//unsigned char xdata *pRcvFifoWr = RcvFifo;  //最后由中断进入接收缓冲区的字节位置
//unsigned char xdata *pRcvFifoRd = RcvFifo;  //最后取走的字节位置
//bit RcvFifoSign = 0;      //接收缓冲区非空标志   有=1
//bit RcvFifoFul = 0;       //输入缓冲区满标志  满=1

//unsigned char RcvDataLen = 0;	// RXMODE receive avail DataLen

/**********************************************************************************/

// PIN CONFIG for S52
sbit	CLK1 	= P2^3;		// Clock TX/RX for Data Channel1 3-wire interface
sbit	CS		= P2^2;		// Chip Select Activates Comfiguration Mode
sbit	Data	= P2^4;		// RX Data Channel1/TX Data Input/3-wire interface
sbit	CE		= P2^1;		// Chip Enalbe Activates RX or TX mode
//sbit	Clk2	= P2^4;		// Clock for RX Data Channel2
//sbit	Dout2	= P2^3;		// RX Data Channel2

//sbit	DR2		= P3^3;		// RX Data Ready at Data Channel2(ShockBurst only)
//sbit	DR1		= P3^3;		// RX Data Ready at Data Channel1(ShockBurst only)
sbit	PWR_UP	= P2^0;		// nRF24011

/**********************************************************************************/


 void SPI_SendByte( unsigned char ch )
{
	unsigned char i;

	for ( i = 0; i < 8; i++ )
	{
		if ( ch & 0x80 )
			Data = 1;
		else
			Data = 0;
			
		ch <<= 1;	
		_nop_();
		CLK1 = 1;
		_nop_();	
		_nop_();		// wait one clk cycle
		CLK1 = 0;
	}
}

/************************************* InitnRF2401 START ******************************/		
void nRF2401_Init(void)
{
	unsigned char i;

	nRF2401_ON;	

	CE = 0;						//Disable CE
	CS = 1;						//Enable CS--confige mode
	
	SPI_SendByte( CH2DataLen << 3 );	// DATA2_W, 119:112
	
	SPI_SendByte( CH1DataLen << 3 );	// DATA1_W, 111:104
	
	for( i = 5; i > 0; i-- )		// 103:64
	{
		SPI_SendByte( nRF2401Addr2[i-1] );	
	}
	
	for( i = 5; i > 0; i-- )		// 63:24
	{
		SPI_SendByte( nRF2401Addr1[i-1] );	
	}
	
	SPI_SendByte( ( nRF2401AddrLen << 5 ) | 3 );	//  23:18, ADDR_W both RX channels; 17bit, CRC_Len; 16bit, CRC_EN

	// RX2_EN; ShockBrust Mode; Datarate(Bit5) 250K,
	if( RX2_EN == 1 )
		SPI_SendByte( 0x80 | 0x4f );	// Number of receiver(Bit7), shock Mode(Bit6), Datarate(Bit5) 250K, crystal(Bit4-Bit2), RF Power(Bit1-Bit0)
	else
		SPI_SendByte( 0x4f );
	
	SPI_SendByte( 0x05 );	// Bit7-Bit1, Channel1 Frequency; Bit0, RX/TX mode(Bit0)
	
	CS = 0;					// Disable CS--standby mode
	Data = 0;				// Set Data low

	CE_HIGH;
}


/************************************* InitnRF2401 END *******************************/



unsigned char SPI_RcvByte( void )
{
	unsigned char i, ch = 0;
	  CS = 0;
	  CE_HIGH;	
	for ( i = 8; i > 0; i-- )
	{
	   
		ch <<= 1;
		CLK1=1;			// set clock high
		_nop_();	
		_nop_();
		Data=1;			// wait one clk cycle		
		if( Data == 1 )
			ch |= 1;
			
		CLK1 = 0;
	}
	
	return ch;	
}


/************************************* SetRXMode START ******************************/
void SetRXMode()
{
	CE = 0;				// Set CE LOW
	CS = 1;				// Set CS HIGH--confige mode
	
	Data = 1;			// Set Data HIGH, Set RX mode		
	CLK1 = 1;			// Make one SPIClock cycle
	_nop_();
	_nop_();
	CLK1 = 0;
	
	CS = 0;				// Set CS LOW		
	CE = 1;				// Set CE HIGH--active mode
}
/************************************* SetRXMode  END *******************************/





