// 
// rw1600 driver
//
// author: Radioway
//=========================================================================    
//              
//    SCK--PA4 |1    16| PA5--MISO
//         PA0 |2    15| PA1
//    CSN--PB7 |3    14| PA2
//         PB6 |4    13| PA3
//         VDD |5    12| GND
//         PB5 |6    11| PB0--MOSI
//         PB4 |7    10| PB1                                
//         PB3 |8     9| PB2
//
//==========================================================================
#include    <8PE531A.H>
#include    "rw1600drv.h"
#include    "typesdefine.h"

byte SPIDDR;
byte RegAddr;
byte RegData;
byte rf_rwBuf[16];
byte pl_long;

byte DelayCnt;

byte rf_Mode;
byte spiwrbytei;

//
//=========================== Sub Function =================================
//
void Delay100US(void)
{
    unsigned char cntt0;
    
	while(--DelayCnt)
	{
	    cntt0=50;
		while(--cntt0)
		{
		    NOP();
		    NOP();
		}
	};
}

void Delay5US(unsigned int dlyus)
{
	while(--dlyus)
	{NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
	}
}


void RW1600_SPI_init(void)
{
    PORTA |= 0x20;    // PA5--MISO out 0
    IOSTA |= 0x20;    // PA5--MISO as input pin

    PORTA &= ~0x10;    // PA4--SCK out 0
    IOSTA &= ~0x10;   // PA4--SCK as output pin

    PORTB |= 0x81;    // PB0--MOSI,PB7--CSN out 1
    IOSTB &= ~0x81;   // PB0--MOSI,PB7--CSN as output pin
   
}


void  RW1600_SPI_WR_Byte(void)
{
	spiwrbytei=8;

	while(spiwrbytei--)//do
	{
		if(SPIDDR&0x80)SPI_MOSI = 1;//MSB sent first
		else	   SPI_MOSI = 0;
		SPIDDR<<=1;	
		if(SPI_MISO)SPIDDR |= 0x01;
		SPI_CLK = 1;
		SPI_CLK = 0;
	};
	
}

void RW1600_SPI_WriteCmd(void)
{
	SPI_CSN = 0;
	SPIDDR = RegAddr;
	RW1600_SPI_WR_Byte();	   //cmd类型
	
	SPIDDR = RegData;
	RW1600_SPI_WR_Byte();	   //cmd参数
	SPI_CSN = 1;
}

void RW1600_SPI_wrOperation(void)
{
	SPI_CSN = 0;
	SPIDDR = RegAddr;
	RW1600_SPI_WR_Byte();
	RegData = SPIDDR;
	SPI_CSN = 1;
}

void RW1600_spicmd_WriteReg(void)
{
	SPI_CSN = 0;

	SPIDDR = RW1600_WR_REG|RegAddr;
	RW1600_SPI_WR_Byte();//write register addr
	
	SPIDDR = RegData;
	RW1600_SPI_WR_Byte();

	SPI_CSN = 1;
}

void RW1600_spicmd_ReadReg(void)
{
	SPI_CSN = 0;

	SPIDDR = RegAddr;//RW1600_RD_REG = 0
	RW1600_SPI_WR_Byte();

	SPIDDR = 0x00;
	RW1600_SPI_WR_Byte();

	RegData = SPIDDR;
	SPI_CSN = 1;
}

//
// Input: U8 pl_long, payload length
// Output: rf_rwBuf
//
void RW1600_spicmd_Read_Rx_Payload(void)
{
	U8 i;
	
	SPI_CSN = 0;   //拉低 
	SPIDDR = RW1600_R_RX_PAYLOAD;
	RW1600_SPI_WR_Byte();
	for(i=0x00;i<pl_long;i++)
	{
	    SPIDDR = 0x00;
	    RW1600_SPI_WR_Byte();
	    rf_rwBuf[i] = SPIDDR;
	}    
	SPI_CSN = 1;
}

//
// Input: U8 *rf_rwBuf, U8 pl_long
//
/*
void  RW1600_spicmd_Write_Tx_Payload(void)
{
	U8 i;
	SPI_CSN = 0;   //拉低
	SPIDDR = RW1600_W_TX_PAYLOAD;
	RW1600_SPI_WR_Byte();
	for(i=0x00;i<pl_long;i++)
    {
        SPIDDR = rf_rwBuf[i];
        RW1600_SPI_WR_Byte();
    }
	SPI_CSN = 1;	
}
*/

//
// Input: U8 *rf_rwBuf, U8 pl_long
//
void  RW1600_spicmd_Write_Tx_Payload_No_Ack(void)
{
	U8 byte_ctr;
	
	SPI_CSN = 0;   //拉低
    SPIDDR = RW1600_W_TX_PAYLOAD_NOACK;
	RW1600_SPI_WR_Byte(); 	 
	for(byte_ctr=0; byte_ctr<pl_long; byte_ctr++)	   // then write all byte in buffer(*pBuf)
	{                          
        SPIDDR = rf_rwBuf[byte_ctr];
    	RW1600_SPI_WR_Byte();
	}
    SPI_CSN = 1;			
}

//
// Input: U8 PipeNum, U8 *rf_rwBuf, U8 pl_long
// 
/*
void RW1600_spicmd_Write_Ack_Payload()
{
	U8 byte_ctr;
	
	SPI_CSN = 0;   //拉低
	SPIDDR = RW1600_W_ACK_PAYLOAD | 0x01;  //PipeNum = 1
	RW1600_SPI_WR_Byte(); 
	for(byte_ctr=0; byte_ctr<pl_long; byte_ctr++)	   // then write all byte in buffer(*pBuf)
	{     
	    SPIDDR = rf_rwBuf[byte_ctr];
    	RW1600_SPI_WR_Byte();
	}
    SPI_CSN = 1;			
}
*/

void RW1600_spicmd_FlushTx(void)
{
	RegAddr = RW1600_FLUSH_TX;
	RW1600_SPI_wrOperation();
}

void RW1600_spicmd_FlushRx(void)
{
	RegAddr = RW1600_FLUSH_RX;
	RW1600_SPI_wrOperation();
}

void RW1600_bank_Switch(void)
{
	byte bank = RegData;//保存选择blank

	RegAddr = RW1600_BANK0_STATUS; // bank0 和 bank1 里都有这个同样的寄存器
	RW1600_spicmd_ReadReg();
	
	if(bank == RW1600_Bank1)//选择bank1
	{
		if(!(RegData&0x80)){
			RegAddr = RW1600_ACTIVATE;
			RegData = RW1600_ACTIVATE_DATA;
			RW1600_SPI_WriteCmd();
		}
	}
	else
	{
		if(RegData&0x80){
			RegAddr = RW1600_ACTIVATE;
			RegData = RW1600_ACTIVATE_DATA;
			RW1600_SPI_WriteCmd();
		}
	}
/*
	if(bank != (RegData&0x80))
	{
		//if(!(RegData&0x80)){
			RegAddr = RW1600_ACTIVATE;
			RegData = RW1600_ACTIVATE_DATA;
			RW1600_SPI_WriteCmd();
		//}
	}
*/
}
/*
void RW1600_PowerChange(void)
{
	switch(RegData)
	{
	case 0:  // Power 0dbm
		//RW1600_write_byte(RW1600_BANK0_RF_SETUP,0x03);
		RegAddr = RW1600_BANK0_RF_SETUP;  
		RegData = 0x03|RW1600_TR_MKPS;
		RW1600_spicmd_WriteReg();
	break;
	case 1://5dbm
		//RW1600_write_byte(RW1600_BANK0_RF_SETUP,0x40);
		RegAddr = RW1600_BANK0_RF_SETUP;  
		RegData = 0x40|RW1600_TR_MKPS;
		RW1600_spicmd_WriteReg();
		break;
	case 2://-6dbm
		//RW1600_write_byte(RW1600_BANK0_RF_SETUP,0x47);
		RegAddr = RW1600_BANK0_RF_SETUP;  
		RegData = 0x01|RW1600_TR_MKPS;
		RW1600_spicmd_WriteReg();
		break;
	case 3://8dbm
		//RW1600_write_byte(RW1600_BANK0_RF_SETUP,0x47);
		RegAddr = RW1600_BANK0_RF_SETUP;  
		RegData = 0x47|RW1600_TR_MKPS;
		RW1600_spicmd_WriteReg();
		break;
	default:
		break;
	}
}
*/
void RW1600_Clear_All_Irq(void)
{
	RegAddr = RW1600_BANK0_STATUS;
	RegData = 0x70;
	RW1600_spicmd_WriteReg();
}

//
// Set CE to low by software
//
void  RW1600_SPI_CE_LOW(void)
{
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK0_PRE_GURD | RW1600_WR_REG;
	RW1600_SPI_WR_Byte();
	
    SPIDDR = 0x77;
    RW1600_SPI_WR_Byte();
	
    SPIDDR = 0x80;
    RW1600_SPI_WR_Byte();
    SPI_CSN = 1;
}

//
// Set CE to high by software
//
void  RW1600_SPI_CE_HIGH(void)
{
    SPI_CSN = 0;
	SPIDDR = RW1600_BANK0_PRE_GURD | RW1600_WR_REG;
	RW1600_SPI_WR_Byte();
	
    SPIDDR = 0x77;
    RW1600_SPI_WR_Byte();
	
    SPIDDR = 0x00;
    RW1600_SPI_WR_Byte();
    SPI_CSN = 1;
}

void  RW1600_Sleep(void)
{
	//RW1600_spicmd_FlushTx();
	//RW1600_spicmd_FlushRx();
	RegAddr = RW1600_BANK0_CONFIG;
	RW1600_spicmd_ReadReg();
	//while(1){
		RegData &= 0xfC;	//PowerDowm
		RW1600_spicmd_WriteReg();
	//	RW1600_spicmd_ReadReg();
	//	if(!(RegData&0x01))
	//	    break;
	//}
}



void  RW1600_WeakUp(void)
{
	RegAddr = RW1600_BANK0_CONFIG;
	RW1600_spicmd_ReadReg();
	RegData |= 0x02;	//Power Up
	RW1600_spicmd_WriteReg();
	DelayCnt = 50;
	Delay100US();//延迟>=4.5MS
	//Wirless_Keyset();
}


//
// 功能: 无线2.4G模块初始化
//
void  RW1600_Init(void)
{
    //
	//-------- RW1600_Calibration -------------------------------------------------
	//
	RegData = RW1600_Bank0; RW1600_bank_Switch();//切换到bank0
	RW1600_SPI_CE_LOW();                     //模式片选信号，使能发射模式或接收模式
	//
	//RW1600_BANK0_CONFIG			0x03
	//RW1600_BANK0_RF_CH			0x32		 //频道
	//RW1600_BANK0_RF_SETUP		    0x40
	//
	RegAddr = RW1600_BANK0_CONFIG;  RegData = 0x03;
	RW1600_spicmd_WriteReg();
	RegAddr = RW1600_BANK0_RF_CH;   RegData = 0x32;
	RW1600_spicmd_WriteReg();
	RegAddr = RW1600_BANK0_RF_SETUP;RegData = 0x40;
	RW1600_spicmd_WriteReg();

	RegData = RW1600_Bank1; RW1600_bank_Switch();//切换到bank1
	//
	//RW1600_BANK1_PLL_CTL0,		0x40,0x01,0x10,0xE5,
	//
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK1_PLL_CTL0 | RW1600_WR_REG; RW1600_SPI_WR_Byte();//选择地址
	SPIDDR = 0x40;RW1600_SPI_WR_Byte();
	SPIDDR = 0x00;RW1600_SPI_WR_Byte();
	SPIDDR = 0x10;RW1600_SPI_WR_Byte();
	SPIDDR = 0xE5;RW1600_SPI_WR_Byte();
	SPI_CSN = 1;

	//
	//RW1600_BANK1_CAL_CTL,		0x20,0x08,0x50,0x40,0x50,
	//
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK1_CAL_CTL | RW1600_WR_REG; RW1600_SPI_WR_Byte();//选择地址
	SPIDDR = 0x20;RW1600_SPI_WR_Byte();
	SPIDDR = 0x08;RW1600_SPI_WR_Byte();
	SPIDDR = 0x50;RW1600_SPI_WR_Byte();
	SPIDDR = 0x40;RW1600_SPI_WR_Byte();
	SPIDDR = 0x50;RW1600_SPI_WR_Byte();
	SPI_CSN = 1;

	//
	//RW1600_BANK1_IF_FREQ,		0x00,0x00,0x1F
	//
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK1_IF_FREQ | RW1600_WR_REG; RW1600_SPI_WR_Byte();//选择地址
	SPIDDR = 0x00;RW1600_SPI_WR_Byte();
	SPIDDR = 0x00;RW1600_SPI_WR_Byte();
	SPIDDR = 0x1F;RW1600_SPI_WR_Byte();
	SPI_CSN = 1;

	//
	//RW1600_BANK1_FDEV,			0x20,
	//RW1600_BANK1_DAC_CAL_LOW,	    0x00,
	//RW1600_BANK1_DAC_CAL_HI,	    0x7F,
	//
	RegAddr = RW1600_BANK1_FDEV;RegData = 0x20;
	RW1600_spicmd_WriteReg();
	//RegAddr = RW1600_BANK1_DAC_CAL_LOW; RegData = 0x00;
	//RW1600_spicmd_WriteReg();
	RegAddr = RW1600_BANK1_DAC_CAL_HI;	RegData = 0x7F;
	RW1600_spicmd_WriteReg();

	//
	//RW1600_BANK1_RF_IVGEN,		0x1F,0x64,0x00,0x81, 
	//
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK1_RF_IVGEN | RW1600_WR_REG; RW1600_SPI_WR_Byte();//选择地址
	SPIDDR = 0x1F;RW1600_SPI_WR_Byte();
	SPIDDR = 0x64;RW1600_SPI_WR_Byte();
	SPIDDR = 0x00;RW1600_SPI_WR_Byte();
	SPIDDR = 0x81;RW1600_SPI_WR_Byte();
	SPI_CSN = 1;

	RegData = RW1600_Bank0; RW1600_bank_Switch(); //切换到bank0
	DelayCnt = 25;Delay100US();					  //延迟>=2MS
	RW1600_SPI_CE_HIGH();
	Delay5US(4);					              //延迟>=20uS
	RW1600_SPI_CE_LOW(); 
	DelayCnt = 250;Delay100US();				  //延迟>= 40mS
	DelayCnt = 200;Delay100US();				  //延迟>= 40mS
	// 
	//---------- RW1600_Calibration End -----------------------------------------------
	//
	
	//
	//--------- Init Start ------------------------------------------------
	//
	RegData = RW1600_Bank1; RW1600_bank_Switch();//切换到bank1
	//
	//RW1600_BANK1_PLL_CTL0,		0x40,0x01,0x30,0xE1, 
	//
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK1_PLL_CTL0 | RW1600_WR_REG; RW1600_SPI_WR_Byte();//选择地址
	SPIDDR = 0x40;RW1600_SPI_WR_Byte();
	SPIDDR = 0x01;RW1600_SPI_WR_Byte();
	SPIDDR = 0x30;RW1600_SPI_WR_Byte();
	SPIDDR = 0xE1;RW1600_SPI_WR_Byte();
	SPI_CSN = 1;

	//
	//RW1600_BANK1_PLL_CTL1,		0x00,0x42,0x10,0x01
	//
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK1_PLL_CTL1 | RW1600_WR_REG; RW1600_SPI_WR_Byte();//选择地址
	SPIDDR = 0x00;RW1600_SPI_WR_Byte();
	SPIDDR = 0x42;RW1600_SPI_WR_Byte();
	SPIDDR = 0x10;RW1600_SPI_WR_Byte();
	SPIDDR = 0x01;RW1600_SPI_WR_Byte();
	SPI_CSN = 1;

	//
	//RW1600_BANK1_CAL_CTL,		0x29,0x89,0x75,0x28,0x50
	//
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK1_CAL_CTL | RW1600_WR_REG; RW1600_SPI_WR_Byte();//选择地址
	SPIDDR = 0x29;RW1600_SPI_WR_Byte();
	SPIDDR = 0x89;RW1600_SPI_WR_Byte();
	SPIDDR = 0x65;RW1600_SPI_WR_Byte();
	SPIDDR = 0x28;RW1600_SPI_WR_Byte();
	SPIDDR = 0x50;RW1600_SPI_WR_Byte();
	SPI_CSN = 1;


	//
	//RW1600_BANK1_RX_CTRL,		0x52,0xC2,0x09,0xAC
	//
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK1_RX_CTRL | RW1600_WR_REG; RW1600_SPI_WR_Byte();//选择地址
	SPIDDR = 0x52;RW1600_SPI_WR_Byte();
	SPIDDR = 0xC2;RW1600_SPI_WR_Byte();
	SPIDDR = 0x09;RW1600_SPI_WR_Byte();
	SPIDDR = 0xAC;RW1600_SPI_WR_Byte();
	SPI_CSN = 1;

	//
	//RW1600_BANK1_FAGC_CTRL_1,		0x80,0x14,0x08,0x29
	//
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK1_FAGC_CTRL_1 | RW1600_WR_REG; RW1600_SPI_WR_Byte();//选择地址
	SPIDDR = 0x80;RW1600_SPI_WR_Byte();
	SPIDDR = 0x14;RW1600_SPI_WR_Byte();
	SPIDDR = 0x08;RW1600_SPI_WR_Byte();
	SPIDDR = 0x29;RW1600_SPI_WR_Byte();
	SPI_CSN = 1;

	//
	//RW1600_BANK1_RF_IVGEN,		0x1F,0x64,0x00,0x11
	//
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK1_RF_IVGEN | RW1600_WR_REG; RW1600_SPI_WR_Byte();//选择地址
	SPIDDR = 0x1F;RW1600_SPI_WR_Byte();
	SPIDDR = 0x64;RW1600_SPI_WR_Byte();
	SPIDDR = 0x00;RW1600_SPI_WR_Byte();
	SPIDDR = 0x01;RW1600_SPI_WR_Byte();
	SPI_CSN = 1;	

	RegData = RW1600_Bank0; RW1600_bank_Switch();//切换到bank0
	//
	//RW1600_BANK0_CONFIG,		0x0e,
	//RW1600_BANK0_RX_PW_P0,		0x20, //根据需要配置
	//RW1600_BANK0_DYNPD,			0x3F,
	//RW1600_BANK0_FEATURE,		0x07,
	//
	RegAddr = RW1600_BANK0_CONFIG;   RegData = 0x0E;
	RW1600_spicmd_WriteReg();
	RegAddr = RW1600_BANK0_RX_PW_P0; RegData = 0x10;
	RW1600_spicmd_WriteReg();
	RegAddr = RW1600_BANK0_DYNPD;	 RegData = 0x3c;
	RW1600_spicmd_WriteReg();
	RegAddr = RW1600_BANK0_FEATURE;	 RegData = 0x00;
	RW1600_spicmd_WriteReg();

	//
	//RW1600_BANK0_SETUP_VALUE,	0x40,0x5A,0x80,0x06,0x00,
	//
	SPI_CSN = 0;
	SPIDDR = RW1600_BANK0_SETUP_VALUE | RW1600_WR_REG; RW1600_SPI_WR_Byte();//选择地址
	SPIDDR = 0x40;RW1600_SPI_WR_Byte();
	SPIDDR = 0x5A;RW1600_SPI_WR_Byte();
	SPIDDR = 0x80;RW1600_SPI_WR_Byte();
	SPIDDR = 0x06;RW1600_SPI_WR_Byte();
	SPIDDR = 0x00;RW1600_SPI_WR_Byte();
	SPI_CSN = 1;	

	//
	//RW1600_BANK0_PRE_GURD,		0x77,
	//RW1600_BANK0_EN_AA,			0x3F,  //0x00
	//RW1600_BANK0_EN_RXADDR,		0x3F,  
	//RW1600_BANK0_SETUP_AW,		0x03,	
	//RW1600_BANK0_SETUP_RETR,	    0x0F,  //根据需要配置
	//RW1600_BANK0_RF_CH,			0x46,  //2.40G-2.525G通道2.4G+0.7 = 2.47
	//RW1600_BANK0_RF_SETUP,		0x47,  //8 dbm 2Mkps
	//
	RegAddr = RW1600_BANK0_PRE_GURD;   RegData = 0x77;
	RW1600_spicmd_WriteReg();
	RegAddr = RW1600_BANK0_EN_AA; 	   RegData = 0x00;
	RW1600_spicmd_WriteReg();   //no auto ack
	RegAddr = RW1600_BANK0_EN_RXADDR;  RegData = 0x01;
	RW1600_spicmd_WriteReg();
	RegAddr = RW1600_BANK0_SETUP_AW;   RegData = 0x03;
	RW1600_spicmd_WriteReg();
	RegAddr = RW1600_BANK0_SETUP_RETR; RegData = 0x0F;
	RW1600_spicmd_WriteReg();
	RegAddr = RW1600_BANK0_RF_CH; 	   RegData = 0x02;
	RW1600_spicmd_WriteReg();   //Chnel: 50
	RegAddr = RW1600_BANK0_RF_SETUP;   RegData = 0x47|RW1600_TR_MKPS;
	RW1600_spicmd_WriteReg();

	RW1600_Clear_All_Irq();
	RW1600_spicmd_FlushTx();
	RW1600_spicmd_FlushRx();

	RW1600_SPI_CE_HIGH();
	//
	//------- Init End --------------------------------------------------
}

/*
void RW1600_Read_Rx_Payload_Width(void)
{
	RegAddr = RW1600_R_RX_PL_WID;
	RW1600_spicmd_ReadReg();
}
*/

//
// read status
// return: RegData
//
void  RW1600_Read_Status(void)
{	
	//unsigned char status;
	RegAddr = RW1600_BANK0_STATUS;
	RW1600_spicmd_ReadReg();
	//return RegData;
}

//
// buf: data read from rx buffer
//
unsigned char  RW1600_RecivePack(void)
{
	unsigned char sta,fifo_sta,len;

    rf_rwBuf[0] = 0;
	RW1600_Read_Status();
	sta = RegData;
	
	if(RW1600_STATUS_RX_DR & sta)
	{
		do
		{
			//RW1600_Read_Rx_Payload_Width();
			RegAddr = RW1600_R_RX_PL_WID;
	        RW1600_spicmd_ReadReg();

			len = RegData;
			//printf("l:%d",(int)len);
			
			if(len <= 0x20)
			{
			    pl_long = len;
			    RW1600_spicmd_Read_Rx_Payload();
			}
			else
				RW1600_spicmd_FlushRx();

            RegAddr = RW1600_BANK0_FIFO_STATUS;
			RW1600_spicmd_ReadReg();
			fifo_sta = RegData;
			
		}
		while(!(fifo_sta & RW1600_FIFO_STA_RX_EMPTY));//not empty continue read out
		RegAddr = RW1600_BANK0_STATUS; RegData = sta;
		RW1600_spicmd_WriteReg();//clear irq
		return len;
	}

	if(sta & (RW1600_STATUS_RX_DR | RW1600_STATUS_TX_DS | RW1600_STATUS_MAX_RT))
	{
	    RegAddr = RW1600_BANK0_STATUS; RegData = sta;
		RW1600_spicmd_WriteReg();//clear irq
	}
	
	return 0;		
}


//
// input: 
//
void RW1600_Write_Pipe_Addr(U8 Px_Addr_Reg)
{
    //U8 byte_ctr; 
    
    SPI_CSN = 0;                                     // CSN low, init SPI transaction
    SPIDDR = RW1600_WR_REG + Px_Addr_Reg;
    RW1600_SPI_WR_Byte();                            // Select register to write to and read status byte
    //for(byte_ctr=0; byte_ctr<5; byte_ctr++)          // then write all byte in buffer(*pBuf)
    {
        SPIDDR = rf_rwBuf[0];
        RW1600_SPI_WR_Byte();
        SPIDDR = rf_rwBuf[1];
        RW1600_SPI_WR_Byte();
        SPIDDR = rf_rwBuf[2];
        RW1600_SPI_WR_Byte();
        SPIDDR = rf_rwBuf[3];
        RW1600_SPI_WR_Byte();
        SPIDDR = rf_rwBuf[4];
        RW1600_SPI_WR_Byte();
        
    }
    SPI_CSN = 1;                                     // CSN high again                                                            // return HS6206 status byte
}

//Mode_Switch
void RW1600_ModeSwitch(void)
{
	unsigned char sta;
	
	RW1600_SPI_CE_LOW();

	RegAddr = RW1600_BANK0_CONFIG;
	RW1600_spicmd_ReadReg();
	sta = RegData;
	if(rf_Mode!= 0)   //Rf_PRX_Mode
		sta &= 0xFE;
	else	
		sta |= 0x01;
    RegAddr = RW1600_BANK0_CONFIG;
    RegData = sta;
	RW1600_spicmd_WriteReg();

    RegAddr = RW1600_BANK0_RF_SETUP;
	RW1600_spicmd_ReadReg();
	if(rf_Mode == 2)    //Rf_Carrier_Mode
	{
		sta = 0x80 | RegData;
	}
	else
	{
		sta = 0x7f & RegData;
	}
	RegAddr = RW1600_BANK0_RF_SETUP;
	RegData = sta;
	RW1600_spicmd_WriteReg(); 
		
	RW1600_SPI_CE_HIGH();
}


