⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phyp.nc

📁 IEEE802.15.4标准下的基于ZIGBEE协议栈的物理及链路层的代码
💻 NC
📖 第 1 页 / 共 2 页
字号:
/** * */module PhyP {  provides interface Init;  provides interface SplitControl as RadioControl;  provides interface PdSap;  provides interface PlmeSap;      uses interface Alarm<T32khz,uint32_t> as PhyTimer;//  uses interface Alarm<T32khz,uint32_t> as testTimer;  uses interface GeneralIO as CSN;  uses interface GeneralIO as RSTN;  uses interface GeneralIO as VREN;  uses interface GpioInterrupt as InterruptCCA;      uses interface GeneralIO as FIFO;  uses interface GeneralIO as FIFOP;  uses interface GpioInterrupt as InterruptFIFOP;      uses interface GpioCapture as CaptureSFD;  uses interface GeneralIO as CCA;  uses interface GeneralIO as SFD;        /**   * below are spi operation   */    uses interface Resource as ReceiveResource;  uses interface Resource as TransmitResource;  uses interface Resource as SyncResource;  uses interface Resource as ControlResource;//  uses interface Resource as OutsideControlResource;    // commands  uses interface CC2420Strobe as SFLUSHRX;  uses interface CC2420Strobe as SFLUSHTX;  uses interface CC2420Strobe as SNOP;  uses interface CC2420Strobe as SRXON;  uses interface CC2420Strobe as SRFOFF;//  uses interface CC2420Strobe as STXON;  uses interface CC2420Strobe as STXONCCA;  uses interface CC2420Strobe as STXON;    uses interface CC2420Strobe as SXOSCON;  uses interface CC2420Strobe as SXOSCOFF;  // registers  uses interface CC2420Register as FSCTRL;  uses interface CC2420Register as IOCFG0;  uses interface CC2420Register as IOCFG1;  uses interface CC2420Register as MDMCTRL0;  uses interface CC2420Register as MDMCTRL1;  uses interface CC2420Register as TXCTRL;  uses interface CC2420Register as RXCTRL1;      uses interface CC2420Register as FMSTATE;    // ram  uses interface CC2420Ram as IEEEADR;  uses interface CC2420Ram as PANID;  uses interface CC2420Ram as SHORTADR;  uses interface CC2420Ram as TXFIFO_RAM;  // fifos  uses interface CC2420Fifo as RXFIFO;  uses interface CC2420Fifo as TXFIFO;      uses interface Mem;    uses interface Random;    uses interface Leds;}implementation {//	uint8_t rxfifo[25];//	uint16_t fsctrlRead;//	uint16_t test_panId;	//	nxle_uint16_t panIdShortAddr[2];;	enum {    SEND_WAIT_SFD_PERIOD = 320};typedef enum {	S_VREG_STOPPED,	S_VREG_STARTING,	S_VREG_STARTED,	S_XOSC_STARTING,	S_XOSC_STARTED,} cc2420_control_state_t;typedef enum {	SYNC_PANID,	SYNC_SHORT_ADDRESS,	SYNC_TX_POWER,	SYNC_CURRENT_CHANNEL,	SYNC_CCA_MODE,	SYNC_ALL,	SYNC_IDLE,	SYNC_RXON,} sync_state_t;typedef enum {    TX_STOPPED,	    TX_STARTED,    TX_LOAD,    TX_CSMA_CA,    TX_SFD,    TX_EFD,    TX_ACK_WAIT,    TX_SEND_DONE,} transmit_state_t;typedef enum {    RX_STOPPED = 0,    RX_STARTED = 1,    RX_HEADER = 2,    RX_PAYLOAD = 3,    RX_HANDLING = 4,} receive_state_t;/**** * the variable in phyPib must sync with the value in cc2420  */norace phy_pib_t phyPib;/**** * control_state is for cc2420 control function use  */norace cc2420_control_state_t control_state = S_VREG_STOPPED;/**** * sync_state is for phyPib get and set  */norace sync_state_t sync_state = SYNC_IDLE;norace error_t syncStatus = SUCCESS;/**** * below variable is for sending packet   */norace transmit_state_t transmit_state = TX_STOPPED;norace error_t sendStatus = SUCCESS;norace bool isAckSending = TRUE; /**** * below variable is for receiving packet   */norace receive_state_t receive_state = RX_STOPPED;norace uint8_t overflow_buffer_left = 128;norace uint8_t missed_packets = 0;uint8_t receivePacketLength = 0;/**** * macDsn used in ackSending, it save the DSN of the packet and waiting for the right ack packet.   */norace uint8_t macDsn = 0;/**** * turn on the VReg of cc2420, waiting a period of time for VReg started.   */error_t startVReg();/**** * turn on the OSCILLATOR of cc2420, waiting CCA interrupt for OSCILLATOR started.   */error_t startOscillator();/**** * below function is for sending packet.   */void attempSend();void performCSMA();/**** * below function is for receiving packet.   */void receive(); void beginReceive();void afterHandleOnePacket(); void flushRxBuf();/**** * syncDone_task() is for phyPib set function.   */task void syncDone_task();/**** * sendDone_task() is for sending packet.   */task void sendDone_task();/**** * sendDone_task() is for receiving packet.   */task void receiveDone_task();/**** * below task is for cc2420 control use  */task void radioStopDone_task();task void radioStartDone_task();command error_t Init.init(){	phyPib.currentChannel = CC2420_DEF_CHANNEL;	phyPib.transmitPower = CC2420_DEF_POWER;		/* 2.4G support channel is 11-26 */	phyPib.channelsSupported = 0x7FFF800;			/* indicate energy above threshold AND have detected IEEE802.15.4 characters */	phyPib.CCAMode = 3; 		phyPib.csmaBE = 0;	phyPib.csmaNB = macMinBE;	phyPib.panId = DEFAULT_PANID;	phyPib.shortAddress = DEFAULT_SHORT_ADDRESS;	phyPib.psdu = 0;	phyPib.txLength = 0;	phyPib.pRxBuf = 0;		call CSN.makeOutput();	call RSTN.makeOutput();	call VREN.makeOutput();	call CCA.makeInput();	call SFD.makeInput();		transmit_state = TX_STOPPED;	receive_state = RX_STOPPED;	missed_packets = 0;	overflow_buffer_left = 128;		//	call testTimer.start(5000);	return SUCCESS;}command error_t RadioControl.start(){	return startVReg();}error_t startVReg(){	atomic 	{	if(control_state != S_VREG_STOPPED){		return FAIL;      }      control_state = S_VREG_STARTING;	}	call VREN.set();	call PhyTimer.start(CC2420_TIME_VREN);	return SUCCESS;}error_t startOscillator(){	atomic	{      if(control_state != S_VREG_STARTED)		return FAIL;      }      control_state = S_XOSC_STARTING;      call ControlResource.request();	return SUCCESS;}event void ControlResource.granted() {	switch(control_state){	case S_XOSC_STARTING:		call CSN.clr();		atomic		{    		      //enable XOSC CCA interupt		call IOCFG1.write( CC2420_SFDMUX_XOSC16M_STABLE << 	                         CC2420_IOCFG1_CCAMUX );		call InterruptCCA.enableRisingEdge();		call SXOSCON.strobe();                           		call IOCFG0.write( ( 1 << CC2420_IOCFG0_FIFOP_POLARITY ) |	                         ( 127 << CC2420_IOCFG0_FIFOP_THR ) );	                         		call FSCTRL.write( ( 1 << CC2420_FSCTRL_LOCK_THR ) |	                        ( ( (phyPib.currentChannel - 11)*5+357 ) << CC2420_FSCTRL_FREQ ) );	                           		call MDMCTRL0.write( ( 1 << CC2420_MDMCTRL0_RESERVED_FRAME_MODE ) |						 #ifdef IS_COORD						 ( 1 << CC2420_MDMCTRL0_PAN_COORDINATOR ) |						 #endif		                          ( 1 << CC2420_MDMCTRL0_ADR_DECODE ) |	                          ( 2 << CC2420_MDMCTRL0_CCA_HYST ) |	                          ( 3 << CC2420_MDMCTRL0_CCA_MOD ) |	                          ( 1 << CC2420_MDMCTRL0_AUTOCRC ) |	                          ( 1 << CC2420_MDMCTRL0_AUTOACK ) |	                          ( 2 << CC2420_MDMCTRL0_PREAMBLE_LENGTH ) );	//      call FMSTATE.read(&fsctrlRead);                     		call MDMCTRL1.write( ( 20 << CC2420_MDMCTRL1_CORR_THR ) );			//		   ( 2 << CC2420_MDMCTRL1_TX_MODE ) );	                           										                    	//      call MDMCTRL1.write(2 << CC2420_MDMCTRL1_TX_MODE);                     	                           //datasheet p67		call RXCTRL1.write( ( 1 << CC2420_RXCTRL1_RXBPF_LOCUR ) |  	                         ( 1 << CC2420_RXCTRL1_LOW_LOWGAIN ) |	                         ( 1 << CC2420_RXCTRL1_HIGH_HGM ) |	                         ( 1 << CC2420_RXCTRL1_LNA_CAP_ARRAY ) |	                         ( 1 << CC2420_RXCTRL1_RXMIX_TAIL ) |	                         ( 1 << CC2420_RXCTRL1_RXMIX_VCM ) |	                         ( 2 << CC2420_RXCTRL1_RXMIX_CURRENT ) );	      call TXCTRL.write( ( 2 << CC2420_TXCTRL_TXMIXBUF_CUR ) |	                         ( 3 << CC2420_TXCTRL_PA_CURRENT ) |	                         ( 1 << CC2420_TXCTRL_RESERVED ) |	                         ( (phyPib.transmitPower & 0x1F) << CC2420_TXCTRL_PA_LEVEL ) ); 			}		break;    	default: break;	}                     }	async event void InterruptCCA.fired() {	//change to little endian	nxle_uint16_t id[2];	id[0] = phyPib.panId;	id[1] = phyPib.shortAddress;	control_state = S_XOSC_STARTED;	call InterruptCCA.disable();	call IOCFG1.write(0);	//beacause PANID and SHORTADR in the continuous memory	call PANID.write(0, (uint8_t*)id, 4 );	call CSN.set();	call CSN.clr();	call IEEEADR.read(0, (phyPib.longAddress.bytes), 8);	call CSN.set();//	call SFLUSHTX.strobe();	call CSN.clr();	call SRXON.strobe();	call CSN.set();	transmit_state = TX_STARTED;	call CaptureSFD.captureRisingEdge();	call InterruptFIFOP.enableFallingEdge();	receive_state = RX_STARTED;	missed_packets = 0;	overflow_buffer_left = 128;		phyPib.rxOn = TRUE;		post radioStartDone_task();			call ControlResource.release();	}command error_t RadioControl.stop(){	if(control_state != S_XOSC_STARTED)		return FAIL;	control_state = S_VREG_STOPPED;	call RSTN.clr();	call VREN.clr();	call RSTN.set();	call InterruptFIFOP.disable();	transmit_state = TX_STOPPED;	receive_state = RX_STOPPED;	missed_packets = 0;	overflow_buffer_left = 128;		phyPib.rxOn = FALSE;	post radioStopDone_task();	return SUCCESS;		}task void radioStopDone_task(){	control_state = S_VREG_STOPPED;	signal RadioControl.stopDone(SUCCESS);}task void radioStartDone_task(){	signal RadioControl.startDone(SUCCESS);}/***************************** next are phyPib set and get function *****************************************/command uint8_t PlmeSap.getCurrentChannel(){	return phyPib.currentChannel;}command void PlmeSap.setCurrentChannel(uint8_t currentChannel){	atomic	{	if(currentChannel >= 11 && currentChannel <=26 )		phyPib.currentChannel = currentChannel;	}}command error_t PlmeSap.directSetCurrentChannel(uint8_t currentChannel){	if(currentChannel < 11 || currentChannel > 26)		return FAIL;	atomic	{	if(sync_state != SYNC_IDLE || control_state != S_XOSC_STARTED)		return FAIL;	sync_state = SYNC_CURRENT_CHANNEL;	}	phyPib.currentChannel = currentChannel;	return call SyncResource.request();		}command uint32_t* PlmeSap.getSupportChannels(){	return &(phyPib.channelsSupported);	}command uint8_t PlmeSap.getTxPower(){	return phyPib.transmitPower&(0x1f);}command void PlmeSap.setTxPower(uint8_t txPower){	phyPib.transmitPower = txPower&0x1f; }command error_t PlmeSap.directSetTxPower(uint8_t txPower){	atomic	{	if(sync_state != SYNC_IDLE || control_state != S_XOSC_STARTED)		return FAIL;	sync_state = SYNC_TX_POWER;	}	phyPib.transmitPower = txPower & 0x1f;	return call SyncResource.request();	}command uint8_t PlmeSap.getCCAMode(){	return phyPib.CCAMode;}command void PlmeSap.setCCAMode(uint8_t ccaMode){	if(ccaMode > 4 || ccaMode == 0)		phyPib.CCAMode = ccaMode;}command error_t PlmeSap.directSetCCAMode(uint8_t ccaMode){	if(ccaMode > 4 || ccaMode == 0)		return FAIL;	atomic	{	if(sync_state != SYNC_IDLE || control_state != S_XOSC_STARTED)		return FAIL;	sync_state = SYNC_CCA_MODE;	}	phyPib.CCAMode = ccaMode;	return call SyncResource.request();		}command saddr_t* PlmeSap.getShortAddr(){	return &(phyPib.shortAddress);}command void PlmeSap.setShortAddr(saddr_t address){	atomic phyPib.shortAddress = address;}command error_t PlmeSap.directSetShortAddr(saddr_t address){	atomic	{	if(sync_state != SYNC_IDLE || control_state != S_XOSC_STARTED)		return FAIL;	sync_state = SYNC_SHORT_ADDRESS;	}	phyPib.shortAddress = address;	return call SyncResource.request();			}command panid_t* PlmeSap.getPanId(){	return &(phyPib.panId);}command void PlmeSap.setPanId(panid_t panId){	phyPib.panId = panId;}command error_t PlmeSap.directSetPanId(panid_t panId){	atomic	{	if(sync_state != SYNC_IDLE || control_state != S_XOSC_STARTED)		return FAIL;	sync_state = SYNC_PANID;	}	phyPib.panId = panId;	return call SyncResource.request();			}command error_t PlmeSap.sync(){	atomic	{	if(sync_state != SYNC_IDLE || control_state != S_XOSC_STARTED)		return FAIL;	sync_state = SYNC_ALL;	}	return call SyncResource.request();			}command laddr_t* PlmeSap.getLongAddr(){	return &phyPib.longAddress;}	command error_t PlmeSap.rxControl(bool isRxOn){	atomic	{	if(sync_state != SYNC_IDLE || control_state != S_XOSC_STARTED)		return FAIL;	sync_state = SYNC_RXON;	}	phyPib.rxOn = isRxOn;	return call SyncResource.request();		}event void SyncResource.granted() {	//change to little endia	nxle_uint16_t id[2];		if( sync_state == SYNC_RXON){		if(phyPib.rxOn){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -