?? mftphy2500.c.bak
字號:
/*********************************************************************
*
* MFT MAC layer
*
*********************************************************************
* FileName: mftPHY2500.c
* Dependencies:
* Processor: c51
* Company: chengdu MFT, Inc.
*
* Software License Agreement
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* 10/14/2005 Rel 0.1
********************************************************************/
//#include "mftPHY2500.h"
//#include "mft.h"
//----------------------------------------------------------------------------------------
extern void MACPhyDataReady(void);
extern BYTE MACMsIsGameMode(void);
extern BYTE MACWaitGdo(void);
#pragma DATA_SEG DEFAULT
extern BYTE macRandom;
#define ECHNUMBER 30 //exclude channel number
BYTE PHYOccupyChl[ECHNUMBER]; //record exclude channel
BYTE phychCount=0;
#pragma DATA_SEG MY_ZEROPAGE
BYTE PHYChannel=0;
BYTE PHYPower=0;
BYTE PHYRvLength=0;
BYTE PHYSendFlag=0;
//#pragma CONST_SEG ConstData
//BYTE const paTable[1] ={0xFB};
/*RF_SETTINGS const rfSettings = {
0x07, //FSCTRL1 Frequency synthesizer control.
0x00, //FSCTRL0 Frequency synthesizer control.
0x5D, //FREQ2 Frequency control word, high byte.
0x93, //FREQ1 Frequency control word, middle byte.
0xB1, //FREQ0 Frequency control word, low byte.
0x2D, //MDMCFG4 Modem configuration.
0x3B, //MDMCFG3 Modem configuration.
0x73, //MDMCFG2 Modem configuration.
0xA2, //MDMCFG1 Modem configuration.
0xF8, //MDMCFG0 Modem configuration.
0x00, //CHANNR Channel number.
0x01, //DEVIATN Modem deviation setting (when FSK modulation is enabled).
0xB6, //FREND1 Front end RX configuration.
0x10, //FREND0 Front end RX configuration. //0x17->0x10 no power table
0x08, //MCSM0 Main Radio Control State Machine configuration.//0x18 to 0x08 jc
0x1D, //FOCCFG Frequency Offset Compensation Configuration.
0x1C, //BSCFG Bit synchronization Configuration.
0xC7, //AGCCTRL2 AGC control.
0xB2, //AGCCTRL0 AGC control.
0xEA, //FSCAL3 Frequency synthesizer calibration.
0x0A, //FSCAL2 Frequency synthesizer calibration.
0x11, //FSCAL0 Frequency synthesizer calibration.
0x59, //FSTEST Frequency synthesizer calibration.
0x88, //TEST2 Various test settings.
0x31, //TEST1 Various test settings.
0x0B, //TEST0 Various test settings.
0x46, //IOCFG2 GDO2 output pin configuration.0x47->0x06->46
0x2E, //IOCFG0D GDO0 output pin configuration. Refer to SmartRF?Studio User Manual for detailed pseudo register explanation.//jc 0x06->0x46->0x2e
0x06, //PKTCTRL1 Packet automation control.
0x05, //PKTCTRL0 Packet automation control.
0x00, //ADDR Device address.
0x40, //PKTLEN Packet length.
};
*/
#pragma CONST_SEG DEFAULT
//---------------------------------------------------------------------------------------
void PHYCaliberateOsc()
{
// while(GDO2_PIN)
// _nop_();
// delayms(8);
// halSpiStrobe(CCxxx0_SIDLE);
halSpiStrobe(CCxxx0_SCAL);
delayms(1);
// halSpiStrobe(CCxxx0_SRX);
}
//check phy whether is in rx state.1 ok.0 error
/*BYTE PHYSelfCheck()
{
// BYTE bstatus=0;
if(GDO2_PIN)
{
return FALSE;
}
PHYRvLength = halSpiReadStatus(CCxxx0_MARCSTATE);
if(PHYRvLength<0x0d||PHYRvLength>0x0f) //rx state
{
return FALSE;
}
return TRUE;
}*/
//jump occupied channel
void PHYJumpExChannel()
{
BYTE i;
for(i=0;i<phychCount;i++)
{
if(PHYOccupyChl[i]==PHYChannel)
{
PHYChannel++;
i=0;
if(PHYChannel>=MAXCHAN)
{
PHYChannel=FIRSTCHAN;
}
}
}
}
//record occupy channel
void PhyRecordExChannel()
{
PHYOccupyChl[phychCount]=PHYChannel; //record exclude channel
phychCount++;
if(phychCount>ECHNUMBER)
{
phychCount=0;
}
}
void PHYClearExChannel()
{
BYTE i;
phychCount = 0;
for(i=0;i<ECHNUMBER;i++)
PHYOccupyChl[i]=0xff;
}
/**********************************************************************
* 函數(shù): void PHYSetChannel(BYTE chanNel)
* 功能:設(shè)置無線收發(fā)器的頻道
* 輸入:channel 邏輯頻道,由RF 無線收發(fā)器支持的頻道決定.
* 描述:此函數(shù)用于在頻道掃描或頻道沖突時,上層可以改變當前的物理頻道
**********************************************************************/
void PHYSetChannel(BYTE channel)
{
PHYChannel=channel;
halSpiStrobe(CCxxx0_SIDLE);
halSpiWriteReg(CCxxx0_CHANNR,PHYChannel);
PHYCaliberateOsc();
}
void PHY_IntInit()
{
//DisableInterrupts;
/// GDO2_PIN = 0x01;
KBSCR = 0x02; //imaskk=1.disable keyboard int
KBIER = 0x10 ;//allow kba4 interrupt
//KBIER = 0x08 ;//allow kba3 interrupt
KBSCR = 0x00 ;//mak 0,allow keyboard interrupt
//KBSCR |= 0x10 ;//mak 0,clear keyfKBSCR |= 0x10 ;//mak 0,clear keyf
// KBSCR &=0xfb;
//EnableInterrupts;
//GDO2_PIN = 0x00;
}
/**********************************************************************
函數(shù): Bool PHYSetNextChannel(void)
功能:設(shè)置無線收發(fā)器的頻道為下一個頻道
輸出:如果設(shè)置成功,則返回TRUE,否則返回FALSE.
描述:在當前頻道的基礎(chǔ)上加1,切換到下一個頻道,如果下一個頻道超出了RF支
持的物理頻道的數(shù)量,則返回失敗,否則設(shè)置下一個頻道為當前頻道,返回TRUE.
**********************************************************************/
BYTE PHYSetNextChannel(byte nextStep)
{
// BYTE i;
// if(PHYChannel>=max_channr)
// return FALSE;
BYTE tempchan=0;
if(nextStep)
{
tempchan=(macRandom&0x03)+1;
PHYChannel+=tempchan;
if(PHYChannel>=MAXCHAN)
PHYChannel= tempchan+FIRSTCHAN;
PHYJumpExChannel();
}
else
{
PHYChannel++;
}
halSpiStrobe(CCxxx0_SIDLE);
halSpiWriteReg(CCxxx0_CHANNR,PHYChannel);
PHYCaliberateOsc();
return TRUE;
}
void PHYSetPHYAddress(BYTE PhyAddress)
{
// BYTE macAddress;
PHYSetTRXState(PHY_TRX_TX_ON) ;
halSpiWriteReg(CCxxx0_ADDR, PhyAddress);
/*macAddress=halSpiReadReg(CCxxx0_ADDR);
while(macAddress!=PhyAddress)
{
halSpiWriteReg(CCxxx0_ADDR, PhyAddress);
__RESET_WATCHDOG();
} */
}
/**********************************************************************
函數(shù): void PHYSetFirstChannel(void)
功能:設(shè)置無線收發(fā)器的第一個頻道
描述:
**********************************************************************/
void PHYSetFirstChannel(byte tryflag)
{
if(tryflag==0)
PHYChannel=FIRSTCHAN;
else {
PHYChannel=(macRandom&0x1F)+FIRSTCHAN;
// PHYChannel=0x01;
}
PHYJumpExChannel();
halSpiStrobe(CCxxx0_SIDLE);
halSpiWriteReg(CCxxx0_CHANNR,PHYChannel);
PHYCaliberateOsc();
}
/**********************************************************************
函數(shù): void PHYClearRxBuffer()
功能:clear rx fifo.in case of 2500 buffer error
描述:
**********************************************************************/
void PHYClearRxBuffer() {
// if(halSpiReadReg(CCxxx0_RXFIFO)) ;
halSpiStrobe(CCxxx0_SFRX);//clear rx fifo
}
//-------------------------------------------------------------------------------------------------------
//void PHYClearRXFifo()
//
// DESCRIPTION: clear rx fifo
/*void PHYClearRXFifo()
{
BYTE static gdoLowTimes =0;
if(!GDO2_PIN)
{
if(gdoLowTimes>2)
{
gdoLowTimes =0;
// if ((halSpiReadStatus(CCxxx0_RXBYTES) & BYTES_IN_RXFIFO))
PHYClearRxBuffer();
return;
}
gdoLowTimes++;
}
else
{
gdoLowTimes =0;
}
}*/
/**********************************************************************
函數(shù): void PHYSetTRXState(PHY_TRX_STATE state)
功能: 設(shè)置RF 的狀態(tài).
輸入: state 表示RF 新的狀態(tài).接收狀態(tài)PHY_TRX_RX_ON,發(fā)送狀態(tài)
PHY_TRX_TX_ON,或者關(guān)閉狀態(tài)PHY_TRX_OFF,PHY_TRX_FORCE_OFF.