From 8b51c78f1b88d94a89bb8c37ae38a54f523cb597 Mon Sep 17 00:00:00 2001
From: QuakeGod <quakegod@sina.com>
Date: 星期六, 24 二月 2024 10:16:54 +0800
Subject: [PATCH] CCTE bootloader and KMini

---
 Radio_LLCC68/Radio/KWireLess.c |  134 +++++++++++++++++++++++++-------------------
 1 files changed, 77 insertions(+), 57 deletions(-)

diff --git a/Radio_LLCC68/Radio/KWireLess.c b/Radio_LLCC68/Radio/KWireLess.c
index 771a2ff..cb2c987 100644
--- a/Radio_LLCC68/Radio/KWireLess.c
+++ b/Radio_LLCC68/Radio/KWireLess.c
@@ -32,7 +32,7 @@
 
 #elif defined( REGION_CN779 )
 
-#define RF_FREQUENCY                                432000000 // Hz
+#define RF_FREQUENCY                                430620000 // Hz
 
 #elif defined( REGION_EU868 )
 
@@ -60,14 +60,17 @@
 
 #endif
 
-#define nChannelBandwidth 													250000 // Hz
-#define TX_OUTPUT_POWER                             22        // 22 dBm
+#define nChannelSpacing 														530000 // Hz
+#define TX_OUTPUT_POWER                             16        // 22 dBm
 
 extern bool IrqFired;
 
 bool RadioEnableMaster=true;//涓讳粠閫夋嫨
-uint32_t nRadioChannel=0;
+uchar nRadioChannel = 0;
+uchar nRadioAddr = 1;
 uint16_t  crc_value;
+
+#define MixAddr(x,y) ((x<<4)|(y))
 /*!
  * Radio events function pointer
  */
@@ -78,11 +81,11 @@
 
 #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         // [SF7..SF12]    
 #define LORA_CODINGRATE                             4         // [1: 4/5,       
                                                               //  2: 4/6,
                                                               //  3: 4/7,
@@ -159,13 +162,13 @@
     
     Radio.Init( &RadioEvents );
 		nRadioChannel = nChn;
-    Radio.SetChannel( RF_FREQUENCY + nRadioChannel * nChannelBandwidth );
+    Radio.SetChannel( RF_FREQUENCY + nRadioChannel * nChannelSpacing );
     
   //  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 +178,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, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
+                                   LORA_SPREADING_FACTOR, LORA_CODINGRATE,
+                                   LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
+                                   true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
+																	 
+    Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
+                                   LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
+                                   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 )
     
@@ -192,8 +208,8 @@
 #endif
 		SX126xSetRxTxFallbackMode(0x40); // 0x40-> FS    0x30 -> SD_XOSC  0x20 -> SD_RC
 		
-//		SX126xSetCadParams(4,0,1,0,1);
-		
+		SX126xSetCadParams(2,0,1,0,1);
+
 																	
 	KwRunStat.runStep=RS_IDEL;
 	return 0;
@@ -252,19 +268,22 @@
 		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,32 +294,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);
 
     }
 }
@@ -389,26 +393,32 @@
 	}	
 //*/	
 	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))
+					{
+						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;
@@ -428,9 +438,14 @@
 		CRC_OK = 1;
 	}
 //*/	
-      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 +461,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 +480,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 +531,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]; 
@@ -575,7 +589,7 @@
 		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
+			if (GetTick() - KwRunStat.lastSendtime > CYCLE_TIME *10 && KwRunStat.curStat == 0 ) //&& stat == RF_IDLE )	//200mS
 			{
 				KWireLessStart();
 			}
@@ -587,7 +601,7 @@
 					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,7 +615,13 @@
 						Radio.Standby();
 						KWireLessStart();
 				}
-				
+				if (GetTick() - KwRunStat.lastRecvdtime > 4000){	// 200mS
+			//			KMem.ErrStat=500;
+						KMem.ErrStat=500; 
+				}
+				if (GetTick() - KwRunStat.lastRecvdtime > 12000){	// 1200mS
+						KMem.ErrStat=5000; KMem.WFX[1]=0; 
+				}				
 		}
 	return 0;
 }

--
Gitblit v1.9.1