From 842bb64195f958b050867c50db66fc0aa413dafb Mon Sep 17 00:00:00 2001
From: QuakeGod <quakegod@sina.com>
Date: 星期六, 27 七月 2024 10:42:56 +0800
Subject: [PATCH] KBus upgrade

---
 Radio_LLCC68/Radio/KWireLess.c |  375 +++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 265 insertions(+), 110 deletions(-)

diff --git a/Radio_LLCC68/Radio/KWireLess.c b/Radio_LLCC68/Radio/KWireLess.c
index 771a2ff..07f25d8 100644
--- a/Radio_LLCC68/Radio/KWireLess.c
+++ b/Radio_LLCC68/Radio/KWireLess.c
@@ -20,6 +20,7 @@
 #define USE_MODEM_LORA
 //#define USE_MODEM_FSK
 
+/*
 #define REGION_CN779
 
 #if defined( REGION_AS923 )
@@ -32,7 +33,7 @@
 
 #elif defined( REGION_CN779 )
 
-#define RF_FREQUENCY                                432000000 // Hz
+#define RF_FREQUENCY                                430620000 // Hz
 
 #elif defined( REGION_EU868 )
 
@@ -59,39 +60,27 @@
     #error "Please define a frequency band in the compiler options."
 
 #endif
-
-#define nChannelBandwidth 													250000 // Hz
-#define TX_OUTPUT_POWER                             22        // 22 dBm
-
-extern bool IrqFired;
-
-bool RadioEnableMaster=true;//涓讳粠閫夋嫨
-uint32_t nRadioChannel=0;
-uint16_t  crc_value;
-/*!
- * Radio events function pointer
- */
-
-static RadioEvents_t RadioEvents;
-
-stWLRunStat KwRunStat;
-
+*/
+/*
 #if defined( USE_MODEM_LORA )
 
-#define LORA_BANDWIDTH                              0         // [0: 125 kHz,    
+#define LORA_BANDWIDTH                              1         // [0: 125 kHz,    
                                                               //  1: 250 kHz,
                                                               //  2: 500 kHz,
                                                               //  3: Reserved]
-#define LORA_SPREADING_FACTOR                       7         // [SF7..SF12]    
+#define LORA_SPREADING_FACTOR                       8         // [SF5..SF12]    
 #define LORA_CODINGRATE                             4         // [1: 4/5,       
                                                               //  2: 4/6,
                                                               //  3: 4/7,
                                                               //  4: 4/8]
 #define LORA_PREAMBLE_LENGTH                        4         // Same for Tx and Rx
+
+*/
 #define LORA_SYMBOL_TIMEOUT                         0         // Symbols
 #define LORA_FIX_LENGTH_PAYLOAD_ON                  false
 #define LORA_IQ_INVERSION_ON                        false
 
+/*
 #elif defined( USE_MODEM_FSK )
 
 #define FSK_FDEV                                    20e3      // Hz 
@@ -104,6 +93,49 @@
 #else
     #error "Please define a modem in the compiler options."
 #endif
+*/
+/*
+#define nChannelSpacing 														530000 // Hz
+#define TX_OUTPUT_POWER                             16        // 22 dBm
+*/
+
+
+extern bool IrqFired;
+
+bool RadioEnableMaster=true;//涓讳粠閫夋嫨
+uchar nRadioChannel = 0;
+uchar nRadioAddr = 1;
+uint16_t  crc_value;
+
+#define MixAddr(x,y) ((x<<4)|(y))
+/*!
+ * Radio events function pointer
+ */
+
+const stWLConfig defaultWLConfig =
+{
+	.RF_T_Freq = 430620000,			// uint32_t 	Hz
+	.RF_R_Freq = 430620000,			//uint32_t 		//Hz
+	.nChnSpacing = 530,			//		  uint16_t ChannelSpacing;		//kHz	
+	.workMode = 1,				//			uchar workMode;			//0: FSK,    1: LoRa	
+	.Tx_Power = 22,				//      uchar Tx_Power;
+	.LoraBandWidth = 0,				// 			uchar LoraBandWidth;		//		[0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ] 
+	.LoRaFactor = 5,				// 			uchar LoRaFactor;				//		[SF5 .. SF 12]
+	.LoRaCodingRate = 1,				//			uchar LoRaCodingRate;		//		[1 : 4/5,  2: 4/6,  3:  4/7,	4:  4/8
+	.NetWorkAddr = 0x00,			//			uint8_t NetWorkAddr;
+	.DeviceAddr = 0x0102,		//			uint16_t DeviceAddr;
+	.bEnableAddr = 0,				//			uchar bEnableAddr;
+	.bEnableEncrypt = 0,				//			uchar bEnableEncrypt;
+	.bEnableRelay = 0,				//			uchar bEnableRelay;
+	.LoRaPreambleLen = 4,				//			uchar LoRaPreamble_Len;			// 2 - 12
+	.bAutoReSend = 1,						// 鑷姩閲嶅彂
+};
+
+//stWLConfig * pWLCfg = (stWLConfig *)&defaultWLConfig;
+static RadioEvents_t RadioEvents;
+
+stWLConfig WLCfg ;
+stWLRunStat KwRunStat;
 
 typedef enum
 {
@@ -116,17 +148,22 @@
 }States_t;
 
 #define MASTER_RX_TIMEOUT_VALUE                 80			//mS
-#define SLAVE_RX_TIMEOUT_VALUE                  250			//mS
-#define CYCLE_TIME															200			//mS
+#define SLAVE_RX_TIMEOUT_VALUE                  400			//mS
+#define CYCLE_TIME															160			//mS
 
-#define BUFFER_SIZE         										32 						// Define the payload size here
+#define WL_RX_BUFFER_SIZE         										256 						// Define the payload size here
+#define WL_TX_BUFFER_SIZE         										128 						// Define the payload size here
 
+uint16_t nTimeOnAir;
+/*
 const uint8_t PingMsg[] = "PING";
 const uint8_t PongMsg[] = "PONG";
+*/
 
 //uint16_t BufferSize = BUFFER_SIZE;
-uint8_t TX_Buffer[BUFFER_SIZE];
-uint8_t RX_Buffer[BUFFER_SIZE];
+uint8_t RX_Buffer[WL_RX_BUFFER_SIZE];
+uint8_t TX_Buffer[WL_TX_BUFFER_SIZE];
+
 
 
 States_t State = LOWPOWER;
@@ -145,9 +182,37 @@
 		LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_13);		
 }
 
+int LoadKwConfig(void)
+{
+	stStoredWLConfig * pstStoredWLCFG = (stStoredWLConfig *)(STORE_KWCONFIG_BASE);
+
+	if (pstStoredWLCFG->BlockSign == 0x55AA && pstStoredWLCFG->BlockType == 0x05) {
+		WLCfg = pstStoredWLCFG->WLConfig;
+	} else 	{
+		WLCfg = defaultWLConfig;
+	}
+	return 0;
+}
+
+int SaveKwConfig(void)
+{
+//	stStoredWLConfig * pstStoredWLCFG = (stStoredWLConfig *)(STORE_KWCONFIG_BASE);
+
+	stStoredWLConfig theStoredWLCFG;
+	theStoredWLCFG.BlockSign = 0x55AA;
+	theStoredWLCFG.BlockType = 0x05;
+	theStoredWLCFG.nSeq = 1;
+	theStoredWLCFG.nSize = sizeof(stWLConfig);
+	theStoredWLCFG.WLConfig = WLCfg;
+	theStoredWLCFG.nCRC16 = 0x0000;
+	EraseAndWriteToFlashMem(&theStoredWLCFG, (void *)STORE_KWCONFIG_BASE, sizeof(stStoredWLConfig));
+	
+	return 0;
+}
+
 int KWireLessInit(bool bRadioEnableMaster, uint32_t nChn)
 {
-	
+		stWLConfig * pWLCfg = & WLCfg;
 		RadioEnableMaster = bRadioEnableMaster;
     // Radio initialization
     RadioEvents.TxDone = OnTxDone;
@@ -159,13 +224,15 @@
     
     Radio.Init( &RadioEvents );
 		nRadioChannel = nChn;
-    Radio.SetChannel( RF_FREQUENCY + nRadioChannel * nChannelBandwidth );
+		KwRunStat.RF_Freq = pWLCfg->RF_T_Freq + nRadioChannel * pWLCfg->nChnSpacing*1000;
+	
+    Radio.SetChannel( pWLCfg->RF_T_Freq + nRadioChannel * pWLCfg->nChnSpacing*1000 );
     
   //  Radio.WriteBuffer(0x06C0,data,2);
    // Radio.ReadBuffer(0x06C0,test,2);
     
 #if defined( USE_MODEM_LORA )
-    
+/*    
     Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
                                    LORA_SPREADING_FACTOR, LORA_CODINGRATE,
                                    LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
@@ -175,6 +242,19 @@
                                    LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
                                    LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
                                    0, true, 0, 0, LORA_IQ_INVERSION_ON, false );
+// */																	
+
+    Radio.SetTxConfig( MODEM_LORA, pWLCfg->Tx_Power, 0, pWLCfg->LoraBandWidth,
+                                   pWLCfg->LoRaFactor, pWLCfg->LoRaCodingRate,
+                                   pWLCfg->LoRaPreambleLen, LORA_FIX_LENGTH_PAYLOAD_ON,
+                                   true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
+																	 
+    Radio.SetRxConfig( MODEM_LORA, pWLCfg->LoraBandWidth, pWLCfg->LoRaFactor,
+                                   pWLCfg->LoRaCodingRate, 0, pWLCfg->LoRaPreambleLen,
+                                   LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
+                                   0, true, 0, 0, LORA_IQ_INVERSION_ON, false );																	 
+																	 
+		Radio.SetMaxPayloadLength(MODEM_LORA,32);																	 
     
 #elif defined( USE_MODEM_FSK )
     
@@ -188,13 +268,15 @@
                                   0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, true,
                                   0, 0,false, false );
 #else
-    #error "Please define a frequency band in the compiler options."
+    #error "Please define a modem in the compiler options.."
 #endif
 		SX126xSetRxTxFallbackMode(0x40); // 0x40-> FS    0x30 -> SD_XOSC  0x20 -> SD_RC
 		
-//		SX126xSetCadParams(4,0,1,0,1);
-		
-																	
+		SX126xSetCadParams(LORA_CAD_04_SYMBOL,0,1,LORA_CAD_ONLY,1);
+
+	nTimeOnAir = Radio.TimeOnAir(MODEM_LORA,14);
+	KwRunStat.nTimeOnAir = 	nTimeOnAir;								
+	KMem.WDT[38]=nTimeOnAir;																
 	KwRunStat.runStep=RS_IDEL;
 	return 0;
 }
@@ -223,6 +305,8 @@
 
 void OnTxDone( void )
 {   
+		KwRunStat.bMasterSent = 1;
+		KwRunStat.bMasterRecved = 0;
 		KwRunStat.runStep=RS_IDEL;
 		KMem.WDT[42]++;
 		KwRunStat.lastSenttime = GetTick();
@@ -252,19 +336,21 @@
 		KwRunStat.lastAckTime = GetTick();
 		KwRunStat.TXErr++;
     Radio.Standby();	
-    KMem.WDT[44]++;
+//    KMem.WDT[44]++;
 }
 bool bThisRxError=0;
 
 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
 {
+		KMem.WDT[44] = size;
     memcpy( RX_Buffer, payload, size );	
+		memcpy(&KMem.WDT[64],payload,size);
     Radio.Standby();
 	
-		if (bThisRxError) {bThisRxError=0;return;}
-		
-		KwRunStat.lastRecvdtime=GetTick();
-		KwRunStat.lastAckTime = GetTick();
+		if (bThisRxError) {
+			bThisRxError=0;
+			return;
+		}
 		
 		KwRunStat.curStat = 0;
 		KwRunStat.latancy = GetTick() - KwRunStat.lastSendtime;
@@ -275,33 +361,17 @@
 		KwRunStat.RSSI = RssiValue;
 		KwRunStat.SNR = SnrValue;
 	
-    KMem.WDT[40]=-RssiValue; //Radio.Rssi(MODEM_FSK);;
-		KMem.WDT[40]=-RssiValue + ((-SnrValue) <<8);
-		
-		KMem.WDT[43]++;
 		KwRunStat.recvCount++;
-		KwRunStat.CtnLstPkts=0;
+//	KwRunStat.CtnLstPkts=0;
 
-//*/	
-	unsigned char indicator1 = 128+RssiValue;
-	unsigned char indicator2;
-	const unsigned char maxrssi = 100;
-	const unsigned char minrssi = 30;
-	if (indicator1 > maxrssi) indicator2=8; 
-	else if (indicator1 < minrssi) indicator2 = 0;
-	
-	else indicator2 = (indicator1 - minrssi)*7/(maxrssi-minrssi) + 1;
-	
-	unsigned char indicator3 = (1 << indicator2) -1;
     if(RadioEnableMaster)
     {
-					KWLMasterParsePkt(1,size);
+				KWLMasterParsePkt(nRadioChannel,size);
 			//KWLMasterSendReqPkt(1);
     }
     else			//slave
     {
-			KWLSlaveParsePkt(1,size);
-
+				KWLSlaveParsePkt(nRadioChannel,size);
     }
 }
 
@@ -317,20 +387,27 @@
 		KwRunStat.LostPackets++;
 		KwRunStat.CtnLstPkts++;
 		if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
-//		KMem.ErrStat=500;
-		if (KwRunStat.CtnLstPkts > 1) {		KMem.ErrStat=500;}
-			if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;}
-		if (KwRunStat.CtnLstPkts > 3) {		KMem.ErrStat=5000;}
-			if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
-		if (KwRunStat.CtnLstPkts > 6) {	KMem.ErrStat=5000; KMem.WFX[1]=0; }
-			if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}		
-		
+//		KwRunStat.ErrStat=500;
+		if (KwRunStat.CtnLstPkts > 2) {		KwRunStat.ErrStat=500;}
+			if (KwRunStat.CtnLstPkts ==3 ) {KwRunStat.Err1Count++;}
+		if (KwRunStat.CtnLstPkts > 5) {		KwRunStat.ErrStat=5000;}
+			if (KwRunStat.CtnLstPkts == 6) {KwRunStat.Err2Count++;}
+		if (KwRunStat.CtnLstPkts > 9) {	KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
+			if (KwRunStat.CtnLstPkts == 10) {KwRunStat.Err3Count++;}		
+		if ((KwRunStat.CtnLstPkts &0x0f) == 0x0f) {
+				KMem.WDT[51]++;
+				LoadKwConfig();
+				KWireLessInit(RadioEnableMaster,nRadioChannel);
+				Radio.Standby();
+			//	KWireLessStart();
+		}
     if(RadioEnableMaster)
     {
 			//KWLMasterSendReqPkt(1);
     }
     else
     {
+			Radio.Standby();
       Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); 
 			KwRunStat.runStep=RS_RECVING;
 			KwRunStat.lastActTime = GetTick();			
@@ -341,8 +418,12 @@
 
 void OnRxError( void )
 {
-
-//    Radio.Standby();
+			Radio.Standby();
+      Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); 
+			KwRunStat.runStep=RS_RECVING;
+			KwRunStat.lastActTime = GetTick();			
+			KwRunStat.lastRecvtime = GetTick();			
+	
 			KwRunStat.RXErr++;
 			KMem.WDT[46]++;
 			bThisRxError=1;
@@ -359,14 +440,16 @@
 			KwRunStat.LostPackets++;
 			KwRunStat.CtnLstPkts++;
 			if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
-//			KMem.ErrStat=500;
-			if (KwRunStat.CtnLstPkts > 1) {		KMem.ErrStat=500; }			
+//			KwRunStat.ErrStat=500;
+			if (KwRunStat.CtnLstPkts > 1) {		KwRunStat.ErrStat=500; }			
 			if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;}
-			if (KwRunStat.CtnLstPkts > 3) {		KMem.ErrStat=5000;}
+			if (KwRunStat.CtnLstPkts > 3) {		KwRunStat.ErrStat=5000;}
 			if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
-			if (KwRunStat.CtnLstPkts > 6) {	KMem.ErrStat=5000; KMem.WFX[1]=0; }
+			if (KwRunStat.CtnLstPkts > 6) {	KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
 			if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}
+			
     }
+//		Radio.Standby();
 }
 
 void OnCadDone( bool channelActivityDetected)
@@ -382,33 +465,40 @@
 	if (RX_Buffer[size-2] != (crc_value&0xff) && RX_Buffer[size-1] != (crc_value >> 8))
 	{
 		KwRunStat.CRCErr++;
-		KMem.ErrStat=500;
+		KwRunStat.ErrStat=500;
 		CRC_OK = 0;
 	}else {
 		CRC_OK = 1;
 	}	
 //*/	
 	pstKLPacket p1 = (pstKLPacket) RX_Buffer;
-      if(CRC_OK && p1->STSign == enRplySign) //memcmp(RX_Buffer,PongMsg,4)==0 
+      if(CRC_OK && p1->STSign == enRplySign ) //memcmp(RX_Buffer,PongMsg,4)==0 
       {
-					LedToggle();//LED闂儊
-				if (p1->Stat==0) {
-					KwRunStat.tRSSI = RX_Buffer[10];
-					KwRunStat.tSNR = RX_Buffer[11];
-				}else if (p1->Stat==1){
-					KwRunStat.targetSentCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24);
-				}else if (p1->Stat==2) {
-					KwRunStat.targetRecvdCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24);
-				}else if (p1->Stat==3) {
-				}
+				if (p1->DstAddr == MixAddr(nRadioChannel,nRadioAddr))
+					{
+						KwRunStat.bMasterRecved = 1;						
+						LedToggle();//LED闂儊
+						KwRunStat.lastRecvdtime=GetTick();
+						KwRunStat.lastAckTime = GetTick();						
+						KwRunStat.CtnLstPkts=0;						
+						
+						if (p1->Stat==0) {
+							KwRunStat.tRSSI = RX_Buffer[10];
+							KwRunStat.tSNR = RX_Buffer[11];
+						}else if (p1->Stat==1){
+							KwRunStat.targetSentCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24);
+						}else if (p1->Stat==2) {
+							KwRunStat.targetRecvdCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24);
+						}else if (p1->Stat==3) {
+						}
 
-					KMem.WDT[41]=(-KwRunStat.tRSSI) + ((-KwRunStat.tSNR)<<8);
-				
+							KMem.WDT[41]=(-KwRunStat.tRSSI) + ((-KwRunStat.tSNR)<<8);
 
-					KMem.WFX[1]=(RX_Buffer[4]) + (RX_Buffer[5]<<8);
-			//		KMem.WFX[1]=(RX_Buffer[4]) + (indicator3<<8);
-				
-					KMem.WFX[2]=(RX_Buffer[6]) + (RX_Buffer[7]<<8);
+							KMem.WFX[1]=(RX_Buffer[4]) + (RX_Buffer[5]<<8);
+					//		KMem.WFX[1]=(RX_Buffer[4]) + (indicator3<<8);
+						
+							KMem.WFX[2]=(RX_Buffer[6]) + (RX_Buffer[7]<<8);
+					}
       }
 	
 	return 0;
@@ -422,15 +512,28 @@
 	if (RX_Buffer[size-2] != (crc_value&0xff) && RX_Buffer[size-1] != (crc_value >> 8))
 	{
 		KwRunStat.CRCErr++;
-		KMem.ErrStat=500;
+		KwRunStat.ErrStat=500;
 		CRC_OK = 0;
 	}else {
 		CRC_OK = 1;
+			if (p1->STSign != enReqSign) {
+				KwRunStat.PktErr++;
+			}else {
+			if (p1->DstAddr != MixAddr(nRadioChannel,nRadioAddr)) {
+				KwRunStat.ChnErr++;
+			}
+			}				
 	}
 //*/	
-      if(CRC_OK && p1->STSign == enReqSign)// memcmp(RX_Buffer,PingMsg,4)==0 && )
+
+      if(CRC_OK && p1->STSign == enReqSign && p1->DstAddr == MixAddr(nRadioChannel,nRadioAddr))// memcmp(RX_Buffer,PingMsg,4)==0 && )
       {
+
         LedToggle();//LED闂儊
+				KwRunStat.lastRecvdtime=GetTick();
+				KwRunStat.lastAckTime = GetTick();				
+				KwRunStat.CtnLstPkts=0;
+				
 				if (p1->Stat==0) {
 					KwRunStat.tRSSI = RX_Buffer[10];
 					KwRunStat.tSNR = RX_Buffer[11];
@@ -446,7 +549,6 @@
 				KMem.WFX[2]=(RX_Buffer[6]) + (RX_Buffer[7]<<8);				
 
         KWLSlaveSendRplyPkt(1);
-				
 //				KMem.WFY[0]=(RX_Buffer[4]<<8) + RX_Buffer[5];
 //				KMem.WFY[1]=(RX_Buffer[6]<<8) + RX_Buffer[7];				
       }
@@ -466,7 +568,7 @@
 	int len1=12;
 	pstKLPacket p1 = (pstKLPacket) TX_Buffer;
 	p1->STSign = enReqSign;
-	p1->DstAddr = (nRadioChannel<<4) || 0x1;
+	p1->DstAddr = MixAddr(nRadioChannel,nRadioAddr);
 	p1->Func = 0x3;
 	p1->Stat = (p1->Stat + 1) &0x03;   //0x00;
 	p1->Data[0]=KMem.WFY[1]; 
@@ -517,7 +619,7 @@
 	int len1=12;
 	pstKLPacket p1 = (pstKLPacket) TX_Buffer;
 	p1->STSign = enRplySign;
-	p1->DstAddr = (nRadioChannel<<4) || 0x1;
+	p1->DstAddr = MixAddr(nRadioChannel,nRadioAddr);
 	p1->Func = 0x3;
 	p1->Stat = (p1->Stat + 1) &0x03;   //0x00;
 	p1->Data[0]=KMem.WFY[1]; 
@@ -566,28 +668,14 @@
 
 int KWLSlaveProc(int nChn)
 {
-	return 0;
-}
-
-int KWL_Process(int nChn)
-{
-		RadioState_t stat = Radio.GetStatus();
-		KMem.WDT[32]=stat;
-//		KMem.WDT[38]=Radio.Rssi(MODEM_FSK);
-		if (RadioEnableMaster){
-			if (GetTick() - KwRunStat.lastSendtime >2000 && KwRunStat.curStat == 0 ) //&& stat == RF_IDLE )	//200mS
-			{
-				KWireLessStart();
-			}
-		}else			//slave
-		{
+			RadioState_t stat = Radio.GetStatus();
 			if (stat == RF_IDLE){
 					KMem.WDT[48]++;
 					Radio.Standby();
 					KWireLessStart();
 			}
 				if (stat == RF_RX_RUNNING ){
-					if (GetTick() - KwRunStat.lastRecvtime > ((CYCLE_TIME + 10 )*10)){
+					if (GetTick() - KwRunStat.lastRecvtime > ((SLAVE_RX_TIMEOUT_VALUE + 10 )*10)){
 						KwRunStat.lastRecvtime = GetTick();
 						KwRunStat.StepErr1++;
 						KMem.WDT[49]++;
@@ -601,8 +689,75 @@
 						Radio.Standby();
 						KWireLessStart();
 				}
-				
+				if (GetTick() - KwRunStat.lastRecvdtime > 4000){	// 200mS
+			//			KwRunStat.ErrStat=500;
+						KwRunStat.ErrStat=500; 
+//						Radio.Standby();
+//						KWireLessStart();
+				}
+				if (GetTick() - KwRunStat.lastRecvdtime > 12000){	// 1200mS
+						KwRunStat.ErrStat=5000; KMem.WFX[1]=0; 
+				}
+				if (GetTick() - KwRunStat.lastRecvtime > 16000){
+						KMem.WDT[52]++;
+						LoadKwConfig();
+						KWireLessInit(RadioEnableMaster,nRadioChannel);
+						Radio.Standby();
+						KWireLessStart();
+					KwRunStat.lastRecvtime = GetTick();
+				}						
+	return 0;
+}
+
+int KWL_Process(int nChn)
+{
+		RadioState_t stat = Radio.GetStatus();
+		KMem.WDT[32]=stat;
+//		KMem.WDT[38]=Radio.Rssi(MODEM_FSK);
+		if (RadioEnableMaster){
+			if (GetTick() - KwRunStat.lastSendtime > CYCLE_TIME *10 && Radio.IsChannelFree(MODEM_LORA,KwRunStat.RF_Freq,-60,1)){
+				if (!KwRunStat.bMasterRecved) {
+						KwRunStat.ErrStat=5000; 
+						KwRunStat.LostPackets++;
+						KwRunStat.CtnLstPkts++;
+						if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
+				//		KwRunStat.ErrStat=500;
+						if (KwRunStat.CtnLstPkts > 1) {		KwRunStat.ErrStat=2000;}
+							if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;}
+						if (KwRunStat.CtnLstPkts > 3) {		KwRunStat.ErrStat=5000;}
+							if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
+						if (KwRunStat.CtnLstPkts > 6) {	KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
+							if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}							
+					if ((KwRunStat.CtnLstPkts &0x0f) == 0x0f) {
+							KMem.WDT[51]++;
+							LoadKwConfig();
+							KWireLessInit(RadioEnableMaster,nRadioChannel);
+							Radio.Standby();
+						//	KWireLessStart();
+					}							
+				}
+				if (KwRunStat.curStat == 0 ){ //&& stat == RF_IDLE )	//200mS
+//					Radio.Standby();
+					KWireLessStart();
+				}else {
+					Radio.Standby();
+					KWireLessStart();
+				}
+			}
+		}else			//slave
+		{
+				KWLSlaveProc(nChn);
 		}
+	if (KwRunStat.RunStat) KwRunStat.RunStat--;
+	if (KwRunStat.ErrStat) KwRunStat.ErrStat--;
+		
+		KwRunStat.Tx_Power = WLCfg.Tx_Power;
+		KwRunStat.LoraBandWidth = WLCfg.LoraBandWidth;
+		KwRunStat.LoRaFactor = WLCfg.LoRaFactor;
+		KwRunStat.LoRaCodingRate = WLCfg.LoRaCodingRate;
+		KwRunStat.LoRaPreambleLen = WLCfg.LoRaPreambleLen;		
+		
+		
 	return 0;
 }
 

--
Gitblit v1.9.1