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

📄 srf.c

📁 制作Zigbee模块的详细电路原理图和C代码
💻 C
📖 第 1 页 / 共 2 页
字号:

void UzForceTxMode(){

	spi_sw(RFCTL, spi_sr(RFCTL) | 0x02);
}


/**************************************************************
	Function Name: UzTx()
	Description: Send Raw Data
	Parameters: DataPtr -> Out Data Pointer
	                   Length -> Out data Length
	                   
	Return Value: SUCCESS -> Send Data Successful
	                     FAILURE -> Send Data Failure 
**************************************************************/

UINT8 UzTx(UINT8 *DataPtr, UINT8 Length){
	UINT8 Value;
	UINT32 Timer;
	
	spi_lw(TX_N_LEN, Length); // Fill Data Length Into TXFIFO

	spi_fill_fifo(TX_N_BASE, DataPtr, Length); //Fill Data Into TXFIFO

	Value = spi_sr(TXNMTRIG);

	if(*(DataPtr) & 0x20){
		Value |= 0x05; //Set Ackreq(SREG0x1B[2]) if re-transmission is required

		//Set Wait Time
		Timer = ((640 + 32*Length + 912)*3 + 2816)*2; //(CCA + Tx*Length + AckWaitDuration)*MaxRetry + Backoff (us) , System Clock: 20MHz
	}else{
		Value &= ~0x07; //Clear
		Value |= 0x01;

		//Set Wait Time
		Timer = ((640 + 32*Length + 912)*3)*2; //(CCA + Tx*Length)*MaxRetry + Backoff (us) , System Clock: 20MHz
	}
      
       spi_sw(TXNMTRIG, Value);  //Set trigger bit(SREG0x1B[0]) to send packet This bit will be automatically cleared.

	//Wait Interrupt
	while(Timer > 0){
		if(UnetSys.IntFlag.TxN){
			UnetSys.IntFlag.TxN = 0; //Reset Status
			if(!(spi_sr(TXSR) & 0x01)) {
				return SUCCESS; //Check TXFIFO release state
			}else{ 
				break;
			}			
		}

              Timer--;
	}

       return FAILURE;

}

/**************************************************************
	Function Name:  UzRx()
	Description: Receive Data fron RXFIFO
	Parameters: RecvBuff -> Data buff that put received data
	Return Value: reveived data length
**************************************************************/

UINT8 UzRx(UINT8 *RecvBuff){ 
	UINT8 Length;

	if(UnetSys.IntFlag.Rx ==1){ //Check Interrupt Status
		spi_rd_rx_fifo(RecvBuff, &Length); //Receive Data from RxFIFO
		UnetSys.IntFlag.Rx =0; //Reset Status
		return Length;
	}

	return 0;
}


/**************************************************************
	Function Name: UzRxOnlyBeacon()
	Description: Let Rx Receive Only IEEE 802.15.4 Beacon Frame
	Parameters: None
	Return Value: None
**************************************************************/

void UzRxOnlyBeacon(){

	spi_sw(RXFLUSH, (spi_sr(RXFLUSH) & ~0x0e) | 0x02);
}

/**************************************************************
	Function Name: UzRxOnlyCommand()
	Description: Let Rx Receive Only IEEE 802.15.4 Command Frame
	Parameters: None
	Return Value: None
**************************************************************/

void UzRxOnlyCommand(){

	spi_sw(RXFLUSH, (spi_sr(RXFLUSH) & ~0x0e) | 0x08);
}


/**************************************************************
	Function Name: UzRxOnlyData()
	Description: Let Rx Receive Only IEEE 802.15.4 Data Frame
	Parameters: None
	Return Value: None
**************************************************************/

void UzRxOnlyData(){

	spi_sw(RXFLUSH, (spi_sr(RXFLUSH) & ~0x0e) | 0x04);
}

/**************************************************************
	Function Name: UzRxAllFrame()
	Description: Let Rx Receive all IEEE 802.15.4 Frame
	Parameters: None
	Return Value: None
**************************************************************/

void UzRxAllFrame(){

	spi_sw(RXFLUSH, spi_sr(RXFLUSH) & ~0x0e);
}

/**************************************************************
	Function Name: UzRxFlush()
	Description: Drop data in RXFIFO 
	Parameters: None
	Return Value: None
**************************************************************/

void UzRxFlush(){

	spi_sw(RXFLUSH, spi_sr(RXFLUSH) | 0x01);// Trigger drop data
}

/**************************************************************
	Function Name: UzSleep()
	Description: Force UZ2400 into Sleep Mode
	Parameters: None
	Return Value: None
**************************************************************/

void UzSleep(){

	spi_sw(SLPACK ,spi_sr(SLPACK) | 0x80);
}

/**************************************************************
	Function Name: UzTimedSleep()
	Description: Periodic Sleep Mode of UZ2400
	Parameters: MS -> Sleep Time
	Return Value: None
**************************************************************/

void UzTimedSleep(UINT8 MS){  // Range 0~2000 MS, use in un-sloted mode
	INT16 Main, Remain;
	div_t Temp;

	Temp = div(1000000*MS, 30517);  //If sleep clock = 32.768KHz
	Main = Temp.quot;

	Temp = div(1000*Temp.rem, 50); // System clock = 20MHz
	Remain = Temp.quot;

	//Set Main Counter
	spi_lw(TXMAINCNTH0, 0);
	
	spi_lw(TXMAINCNTM, (Main & 0xff00) >> 8);

	spi_lw(TXMAINCNTL, (Main & 0x00ff));

	//Set Re-main Counter
	spi_lw(TXREMCNTH, (Remain & 0xff00) >> 8);

	spi_lw(TXREMCNTL, (Remain & 0x00ff));

	//WakeCnt
	spi_sw(SLPACK ,spi_sr(SLPACK) |0x41);

	spi_sw(RFCTL, spi_sr(RFCTL) & ~0x18);

	//Trigger sleep
	spi_lw(TXMAINCNTH1, spi_lr(TXMAINCNTH1) | 0x80);

}

/**************************************************************
	Function Name: UzRegWakeUp()
	Description: Register Wake Up Mode, wake Up UZ2400
	Parameters: None
	Return Value: None
**************************************************************/

void UzRegWakeUp(){
	UINT8 Value;

	//Disable ext. wake up (im. wakeup mode), Register wake up
	Value = spi_sr(TXBCNINTL);
	Value &= ~0x80;

	Value |= 0x40;
	spi_sw(TXBCNINTL, Value); 

	//Flag be released by software
	Value &= ~0x40;
	spi_sw(TXBCNINTL, Value);
}

/**************************************************************
	Function Name: UzPowWakeUp()
	Description: Power Management Wake Up Mode, wake Up UZ2400
	Parameters: None
	Return Value: None
**************************************************************/

void UzPowWakeUp(){

	//Disable ext. wake up
	spi_sw(TXBCNINTL, spi_sr(TXBCNINTL) & ~0x80);

	//Reset power management
	spi_sw(SOFTRST, spi_sr(SOFTRST) | 0x04);
}

/**************************************************************
	Function Name: UzEnabExtWakeUp()
	Description: Enable External Wake Up
	Parameters: None
	Return Value: None
**************************************************************/

void UzEnabExtWakeUp(){

	spi_sw(TXBCNINTL, spi_sr(TXBCNINTL) | 0x80);

	spi_sw(RXFLUSH, spi_sr(RXFLUSH) | 0x60);
}

/**************************************************************
	Function Name: UzEnabBatteryLifeEx()
	Description: Set IEEE 802.15.4 Battey Life Extension
	Parameters: None
	Return Value: None
**************************************************************/

void UzEnabBatteryLifeEx(){
	
	spi_sw(TXMCR, spi_sr(TXMCR) | 0x40);
}


/**************************************************************
	Function Name: UzDisBatteryLifeEx()
	Description: Set IEEE 802.15.4 Battey Life Extension
	Parameters: None
	Return Value: None
**************************************************************/

void UzDisBatteryLifeEx(){

	spi_sw(TXMCR, spi_sr(TXMCR) & ~0x40);
}

/**************************************************************
	Function Name: UzSetBatteryMonitor()
	Description: Enable and Set threshold for Battery Monitor
	Parameters: Threshold -> Monitor Threshold
	Return Value: None
**************************************************************/

void UzSetBatteryMonitor(UINT8 Threshold){

	spi_lw(RFCTRL6, spi_lr(RFCTRL6) | 0x08); //Enable Battery Monitor

	spi_lw(RFCTRL5, (spi_lr(RFCTRL5) & ~0xf0) | (Threshold << 4)); //Set Threshold
}

/**************************************************************
	Function Name: UzCheckBattery()
	Description: Check battery whether the level lower or higher than threshold
	Parameters: None
	Return Value: SUCCESS -> Battery Higher than threshold
	                     FAILURE -> Battery Lower than threshold
**************************************************************/

UINT8 UzCheckBattery(){

	if((spi_sr(RXSR) & 0x20) == 0x20) //Check Battery Level
		return FAILURE; //Battery Lower than threshold

	return SUCCESS; //Battery Higher than threshold
}

/**************************************************************
	Function Name:  UzSecureTx()
	Description: Transmit Secure Data
	Parameters: SecMode -> Encryption Mode
	                   *SecKey -> Security Key, defined by user
	                   *DataPtr -> Data Pointer
	                   Length -> Data Length
	                   HeaderLength -> Length of Data Header	                
	Return Value: SUCCESS -> Send Data Successful
	                     FAILURE -> Send Data Failure 
**************************************************************/

UINT8 UzSecureTx(UINT8 SecMode, UINT8 *SecKey, UINT8 *DataPtr, UINT8 Length, UINT8 HeaderLength){
	UINT8 Value;
	UINT32 Timer; 
	
	//Fill TXFIFO
	spi_lw(TX_N_HDR, HeaderLength); //Header Length
	spi_lw(TX_N_LEN, Length); //Data Length
	spi_fill_fifo(TX_N_BASE, DataPtr, Length); //Data

	//Fill Security key
	spi_fill_fifo(KEY_TX_N, SecKey, 16);

	//Fill in cipher mode
	spi_sw(SECCR0, (spi_sr(SECCR0) & ~0x07) | SecMode);

	Value = spi_sr(TXNMTRIG);

	if(*(DataPtr) & 0x20){
		Value |= 0x05; //Set Ackreq(SREG0x1B[2]) if re-transmission is required

		//Set Wait Time
		Timer = ((640 + 32*Length + 912)*3 + 2816)*2; //(CCA + Tx*Length + AckWaitDuration)*MaxRetry + Backoff (us) , System Clock: 20MHz
	}else{
		Value &= ~0x07; //Clear
		Value |= 0x01;

		//Set Wait Time
		Timer = ((640 + 32*Length + 912)*3)*2; //(CCA + Tx*Length)*MaxRetry + Backoff (us) , System Clock: 20MHz
	}

	spi_sw(TXNMTRIG, Value); //Trigger Tx start with security

	while(Timer > 0){
		if(UnetSys.IntFlag.TxN == 1){
			UnetSys.IntFlag.TxN = 0;
			if(!(spi_sr(TXSR) & 0x01)){ //Check TXFIFO release state
				return SUCCESS;
		 	}else{
				break;
			}			
		}

		Timer -= 1;
	}

	return FAILURE;
}

/**************************************************************
	Function Name: UzSecureRx()
	Description: Receive Secure Data
	Parameters: SecMode -> Decryption Mode
	                   *SecKey -> Security Key, defined by user
	                   *InBuff -> Data Buffer Pointer
	Return Value: Length -> Received Data Length
**************************************************************/

UINT8 UzSecureRx(UINT8 SecMode, UINT8 *SecKey, UINT8 *InBuff){
	UINT8 Length;

	if(UnetSys.IntFlag.Sec == 1){
		UnetSys.IntFlag.Sec = 0;
		
		spi_fill_fifo(KEY_RX_N, SecKey, 16);//Fill Secure key into FIFO

		spi_sw(SECCR0, (spi_sr(SECCR0) & ~0x38) | SecMode); //Fill cipher mode

		//Security start
		spi_sw(SECCR0, spi_sr(SECCR0) | 0x40); // Trigger Security Process 

		//Wait Interrupt
		while(1){
			if(UnetSys.IntFlag.Rx == 1){
				spi_rd_rx_fifo(InBuff, &Length); //Fetch Data from RXFIFO
				UnetSys.IntFlag.Rx =0; //Reset Interrupt Flag
				return Length;
			}
		}		
	}
		
	return 0;
}

⌨️ 快捷键说明

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