//-----------------------------------------------------------------------------
// TITLE	:Car Application code
// REVISION	:00.01
// AUTHOR	:Chengfj
// COMPANY	:Radioway Co.,Ltd..
// DATE		:2016/05/23
// CHIP		:RW1600

//--------------Include files Segment-----------------------------------------
#include	<8PE531A.H>
//#include    <8PE531A.ASH>
#include    "timer.h"
#include    "carapp.h"
#include    "rw1620drv.h"



//--------------extern function claim Segment-----------------------------------------
extern void WDT_Feed(void);
//extern void	SetTimer(U8,U16);
extern U8   CheckTimer0(void);
extern U8   CheckTimer1(void);

//extern void StopTimer(U8);
extern void RW1600_Write_Pipe_Addr(U8);
void RW1600_Change_Channel(void);
//--------------Global or Other define segment--------------------------------- 
/*
enum {
	Rf_PRX_Mode = 0,
	Rf_PTX_Mode,
	Rf_Carrier_Mode,
}RF_ModeTypeDef;
 */

//--------------extern parameter claim Segment-----------------------------------------
extern byte rf_data_buf[20];
extern byte DelayCnt;

//-------------- globle parameter segment -------------------------------------------
u8 Std_Private_Addr[5]={0xa6,0x61,0xa2,0x93,0xc9};  //通信地址，发射端对码后会被改写
U8 Freq_Tbl[4]={05,28,45,71};



//-------------- const parameter segment -------------------------------------------
//
#pragma tableromaddr (0x07c0) // ?? table ROM  start address at 0x7C0
//
//选频原则: 随机选频. 随机因子为
//地址第4位+第5位,除10取余
//余=0 则选择  0,  10+0, 20+0,30+0
//余=5 则选择  0+5 10+5, 20+5,30+5
//余=9 则选择  0+9 10+9, 20+9,30+9
//             
//频率表 最少10台*4个=40个[10]
const u8 Freq_Tbl0[40]= {05,28,45,71,
                         05,25,45,77,
                         22,46,49,72,
                         06,27,47,49,
                         25,49,51,73,
                         26,49,76,79,
                         24,56,44,67,
                         27,57,46,68,
                         36,58,47,69,
                         37,59,49,70};

const u8 Std_Pairing_Addr[5]={0xAE,0x5B,0x93,0xA5,0x0D};  //对码地址    
                             //{0x55,0x42,0x93,0x8f,0xc9};  
const u8 rollingcode_data[3]={0x01,0x00,0x52}; //R: 0x52, Y: 0x59

#pragma tableromaddr (off) // end of table ROM allocation

#define Std_Pairing_Freq  76//50

//-------------- subroutine segment -------------------------------------------
void rcCtrlinit(void)
{
    PORTA &= ~0x0e;    // PA1,PA2,PA3 out 0
    IOSTA |=  0x0e;    // PA1,PA2,PA3 as in pin
    APHCON &= ~0x0e;   // pull up
    AWUCON |= 0x0e;    // Enable PA1,2,3 week up
    
    PORTB &= ~0x02;    // PB1,out 0
    IOSTB |=  0x02;    // PB1,as in pin, PB1--turn right
    BPHCON &= ~0x02;   // pull up
    BWUCON |= 0x02;    // Enable PB1 week up
    
    PORTB &= ~0x04;    // PB2,out 0
    IOSTB &= ~0x04;    // PB2,as out pin, PB2--Tx_LED
}   

//
//@brief:  get the address from the serial number 
//TEST:    Serial Number: 0x00, 0x00,0x01
//corresponding  Address: 0x01, 0xc0, 0xb0,0x50,0x80.
//
//Rollcode 4字节滚动码，本函数提供一个简易方法，
//实现把滚动码转换为5个字节的通讯地址
//此方法产生的地址通讯效果不保证是最佳，
//但经过几KK的测试，距离都满足要求
//
void Device_Address_Init(void)
{
	u8 i;
	
	for(i=0;i<7;i++)
	{
		rf_data_buf[i] = 0;
	} 
	
	rf_data_buf[0]= rollingcode_data[0]; 
	rf_data_buf[1]= rollingcode_data[1];
	rf_data_buf[2]= rollingcode_data[2]; 
	rf_data_buf[5]= 16; 
	while(rf_data_buf[5]!=0)
	{
		rf_data_buf[6]=rf_data_buf[1];
		rf_data_buf[1]>>=1;
		if(rf_data_buf[2]&0x01)
		{
			rf_data_buf[1]|=0x80;
		}
		rf_data_buf[2]>>=1;
		if(rf_data_buf[6]&0x01)
		{
			rf_data_buf[1] ^=0x05;
			rf_data_buf[2] ^=0x88;
		}
		rf_data_buf[5]--;
	}
	rf_data_buf[4] = rf_data_buf[2] >> 4; 
	rf_data_buf[4] += 0x80; 
	rf_data_buf[3] = rf_data_buf[2] & 0x0f; 
	rf_data_buf[3] += 0x50; 
	rf_data_buf[2] = rf_data_buf[1] >> 4; 
	rf_data_buf[2] += 0xb0; 
	rf_data_buf[1] &= 0x0f; 
	rf_data_buf[1] += 0xc0;
	for(i=0;i<5;i++)
	{
		Std_Private_Addr[i] = rf_data_buf[i];
	} 
}

void rf_send_data_test(void)    
{        
 //     hs6220_carrier_mode();       //载波模式
      	
      while(1)
      {
	rf_data_buf[0]=8;
	rf_data_buf[1]=1;
	rf_data_buf[2]=1;
	rf_data_buf[3]=0xaa;
	rf_data_buf[4]=0xaa;
	rf_data_buf[5]=0xa5;
	rf_data_buf[6]=0xa8;
	rf_tx_data(); 
	DelayCnt = 20;Delay100US();      
            
      }


}
void rf_recive_data_test(void)    
{        
	hs6220_carrier_mode();       //载波模式
	rf_ce_low();
	rf_rx_set();
	rf_ce_high(); 
      while(1)
      {
	rf_RecivePack();
	DelayCnt = 20;Delay100US();    

	          
      }

}