QuakeGod
2024-07-27 842bb64195f958b050867c50db66fc0aa413dafb
提交 | 用户 | age
483170 1 /**
Q 2   ******************************************************************************
3   * @file           : KWireLess.c
4   * @brief          : K-WireLess Protocol program body
5   ******************************************************************************
6     */
7 #include "KWireLess.h"
8 #include "functions.h"
9 #include "string.h"
10 #include "PLCFunctions.h"
11 #include "stm32f0xx_hal.h"
12
13
14 /**************************************************************************************************************************************
15 Demo 程序流程  RadioEnableMaster=true  为主机端,主机端发送一个"PING"数据后切换到接收,等待从机返回的应答"PONG"数据LED闪烁
16
17                RadioEnableMaster=false 为从机端,从机端接收到主机端发过来的"PING"数据后LED闪烁并发送一个"PONG"数据作为应答
18 ***************************************************************************************************************************************/
19
20 #define USE_MODEM_LORA
21 //#define USE_MODEM_FSK
22
842bb6 23 /*
483170 24 #define REGION_CN779
Q 25
26 #if defined( REGION_AS923 )
27
28 #define RF_FREQUENCY                                923000000 // Hz
29
30 #elif defined( REGION_AU915 )
31
32 #define RF_FREQUENCY                                915000000 // Hz
33
34 #elif defined( REGION_CN779 )
35
8b51c7 36 #define RF_FREQUENCY                                430620000 // Hz
483170 37
Q 38 #elif defined( REGION_EU868 )
39
40 #define RF_FREQUENCY                                868000000 // Hz
41
42 #elif defined( REGION_KR920 )
43
44 #define RF_FREQUENCY                                920000000 // Hz
45
46 #elif defined( REGION_IN865 )
47
48 #define RF_FREQUENCY                                865000000 // Hz
49
50 #elif defined( REGION_US915 )
51
52 #define RF_FREQUENCY                                915000000 // Hz
53
54 #elif defined( REGION_US915_HYBRID )
55
56 #define RF_FREQUENCY                                915000000 // Hz
57
58 #else
59
60     #error "Please define a frequency band in the compiler options."
61
62 #endif
842bb6 63 */
Q 64 /*
65 #if defined( USE_MODEM_LORA )
483170 66
842bb6 67 #define LORA_BANDWIDTH                              1         // [0: 125 kHz,    
Q 68                                                               //  1: 250 kHz,
69                                                               //  2: 500 kHz,
70                                                               //  3: Reserved]
71 #define LORA_SPREADING_FACTOR                       8         // [SF5..SF12]    
72 #define LORA_CODINGRATE                             4         // [1: 4/5,       
73                                                               //  2: 4/6,
74                                                               //  3: 4/7,
75                                                               //  4: 4/8]
76 #define LORA_PREAMBLE_LENGTH                        4         // Same for Tx and Rx
77
78 */
79 #define LORA_SYMBOL_TIMEOUT                         0         // Symbols
80 #define LORA_FIX_LENGTH_PAYLOAD_ON                  false
81 #define LORA_IQ_INVERSION_ON                        false
82
83 /*
84 #elif defined( USE_MODEM_FSK )
85
86 #define FSK_FDEV                                    20e3      // Hz 
87 #define FSK_DATARATE                                19.2e3      // bps
88 #define FSK_BANDWIDTH                               60e3     // Hz >> DSB in sx126x
89 #define FSK_AFC_BANDWIDTH                           200e3     // Hz
90 #define FSK_PREAMBLE_LENGTH                         5         // Same for Tx and Rx
91 #define FSK_FIX_LENGTH_PAYLOAD_ON                   false
92
93 #else
94     #error "Please define a modem in the compiler options."
95 #endif
96 */
97 /*
8b51c7 98 #define nChannelSpacing                                                         530000 // Hz
Q 99 #define TX_OUTPUT_POWER                             16        // 22 dBm
842bb6 100 */
Q 101
483170 102
Q 103 extern bool IrqFired;
104
105 bool RadioEnableMaster=true;//主从选择
8b51c7 106 uchar nRadioChannel = 0;
Q 107 uchar nRadioAddr = 1;
483170 108 uint16_t  crc_value;
8b51c7 109
Q 110 #define MixAddr(x,y) ((x<<4)|(y))
483170 111 /*!
Q 112  * Radio events function pointer
113  */
114
842bb6 115 const stWLConfig defaultWLConfig =
Q 116 {
117     .RF_T_Freq = 430620000,            // uint32_t     Hz
118     .RF_R_Freq = 430620000,            //uint32_t         //Hz
119     .nChnSpacing = 530,            //          uint16_t ChannelSpacing;        //kHz    
120     .workMode = 1,                //            uchar workMode;            //0: FSK,    1: LoRa    
121     .Tx_Power = 22,                //      uchar Tx_Power;
122     .LoraBandWidth = 0,                //             uchar LoraBandWidth;        //        [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ] 
123     .LoRaFactor = 5,                //             uchar LoRaFactor;                //        [SF5 .. SF 12]
124     .LoRaCodingRate = 1,                //            uchar LoRaCodingRate;        //        [1 : 4/5,  2: 4/6,  3:  4/7,    4:  4/8
125     .NetWorkAddr = 0x00,            //            uint8_t NetWorkAddr;
126     .DeviceAddr = 0x0102,        //            uint16_t DeviceAddr;
127     .bEnableAddr = 0,                //            uchar bEnableAddr;
128     .bEnableEncrypt = 0,                //            uchar bEnableEncrypt;
129     .bEnableRelay = 0,                //            uchar bEnableRelay;
130     .LoRaPreambleLen = 4,                //            uchar LoRaPreamble_Len;            // 2 - 12
131     .bAutoReSend = 1,                        // 自动重发
132 };
133
134 //stWLConfig * pWLCfg = (stWLConfig *)&defaultWLConfig;
483170 135 static RadioEvents_t RadioEvents;
Q 136
842bb6 137 stWLConfig WLCfg ;
483170 138 stWLRunStat KwRunStat;
Q 139
140 typedef enum
141 {
142     LOWPOWER,
143     RX,
144     RX_TIMEOUT,
145     RX_ERROR,
146     TX,
147     TX_TIMEOUT,
148 }States_t;
149
eaf5d5 150 #define MASTER_RX_TIMEOUT_VALUE                 80            //mS
842bb6 151 #define SLAVE_RX_TIMEOUT_VALUE                  400            //mS
Q 152 #define CYCLE_TIME                                                            160            //mS
483170 153
842bb6 154 #define WL_RX_BUFFER_SIZE                                                 256                         // Define the payload size here
Q 155 #define WL_TX_BUFFER_SIZE                                                 128                         // Define the payload size here
483170 156
842bb6 157 uint16_t nTimeOnAir;
Q 158 /*
483170 159 const uint8_t PingMsg[] = "PING";
Q 160 const uint8_t PongMsg[] = "PONG";
842bb6 161 */
483170 162
eaf5d5 163 //uint16_t BufferSize = BUFFER_SIZE;
842bb6 164 uint8_t RX_Buffer[WL_RX_BUFFER_SIZE];
Q 165 uint8_t TX_Buffer[WL_TX_BUFFER_SIZE];
166
483170 167
Q 168
169 States_t State = LOWPOWER;
170
171 int8_t RssiValue = 0;
172 int8_t SnrValue = 0;
173
174 KWStates KW_State=KW_PON;
175
176 void LedToggle(void)
177 {
178 //  GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_RESET);//LED闪烁
179 //  HAL_Delay_nMS(10);
180 //  GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_SET);
181     
182         LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_13);        
183 }
184
842bb6 185 int LoadKwConfig(void)
Q 186 {
187     stStoredWLConfig * pstStoredWLCFG = (stStoredWLConfig *)(STORE_KWCONFIG_BASE);
188
189     if (pstStoredWLCFG->BlockSign == 0x55AA && pstStoredWLCFG->BlockType == 0x05) {
190         WLCfg = pstStoredWLCFG->WLConfig;
191     } else     {
192         WLCfg = defaultWLConfig;
193     }
194     return 0;
195 }
196
197 int SaveKwConfig(void)
198 {
199 //    stStoredWLConfig * pstStoredWLCFG = (stStoredWLConfig *)(STORE_KWCONFIG_BASE);
200
201     stStoredWLConfig theStoredWLCFG;
202     theStoredWLCFG.BlockSign = 0x55AA;
203     theStoredWLCFG.BlockType = 0x05;
204     theStoredWLCFG.nSeq = 1;
205     theStoredWLCFG.nSize = sizeof(stWLConfig);
206     theStoredWLCFG.WLConfig = WLCfg;
207     theStoredWLCFG.nCRC16 = 0x0000;
208     EraseAndWriteToFlashMem(&theStoredWLCFG, (void *)STORE_KWCONFIG_BASE, sizeof(stStoredWLConfig));
209     
210     return 0;
211 }
212
483170 213 int KWireLessInit(bool bRadioEnableMaster, uint32_t nChn)
Q 214 {
842bb6 215         stWLConfig * pWLCfg = & WLCfg;
483170 216         RadioEnableMaster = bRadioEnableMaster;
Q 217     // Radio initialization
218     RadioEvents.TxDone = OnTxDone;
219     RadioEvents.RxDone = OnRxDone;
220     RadioEvents.TxTimeout = OnTxTimeout;
221     RadioEvents.RxTimeout = OnRxTimeout;
222     RadioEvents.RxError = OnRxError;
223     RadioEvents.CadDone = OnCadDone;
224     
225     Radio.Init( &RadioEvents );
226         nRadioChannel = nChn;
842bb6 227         KwRunStat.RF_Freq = pWLCfg->RF_T_Freq + nRadioChannel * pWLCfg->nChnSpacing*1000;
Q 228     
229     Radio.SetChannel( pWLCfg->RF_T_Freq + nRadioChannel * pWLCfg->nChnSpacing*1000 );
483170 230     
Q 231   //  Radio.WriteBuffer(0x06C0,data,2);
232    // Radio.ReadBuffer(0x06C0,test,2);
233     
234 #if defined( USE_MODEM_LORA )
8b51c7 235 /*    
483170 236     Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
Q 237                                    LORA_SPREADING_FACTOR, LORA_CODINGRATE,
238                                    LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
239                                    true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
240     
241     Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
242                                    LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
243                                    LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
244                                    0, true, 0, 0, LORA_IQ_INVERSION_ON, false );
842bb6 245 // */                                                                    
8b51c7 246
842bb6 247     Radio.SetTxConfig( MODEM_LORA, pWLCfg->Tx_Power, 0, pWLCfg->LoraBandWidth,
Q 248                                    pWLCfg->LoRaFactor, pWLCfg->LoRaCodingRate,
249                                    pWLCfg->LoRaPreambleLen, LORA_FIX_LENGTH_PAYLOAD_ON,
8b51c7 250                                    true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
Q 251                                                                      
842bb6 252     Radio.SetRxConfig( MODEM_LORA, pWLCfg->LoraBandWidth, pWLCfg->LoRaFactor,
Q 253                                    pWLCfg->LoRaCodingRate, 0, pWLCfg->LoRaPreambleLen,
8b51c7 254                                    LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
Q 255                                    0, true, 0, 0, LORA_IQ_INVERSION_ON, false );                                                                     
256                                                                      
257         Radio.SetMaxPayloadLength(MODEM_LORA,32);                                                                     
483170 258     
Q 259 #elif defined( USE_MODEM_FSK )
260     
261     Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
262                                   FSK_DATARATE, 0,
263                                   FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
264                                   true, 0, 0, 0, 3000 );
265     
266     Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
267                                   0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
268                                   0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, true,
269                                   0, 0,false, false );
270 #else
842bb6 271     #error "Please define a modem in the compiler options.."
483170 272 #endif
Q 273         SX126xSetRxTxFallbackMode(0x40); // 0x40-> FS    0x30 -> SD_XOSC  0x20 -> SD_RC
eaf5d5 274         
842bb6 275         SX126xSetCadParams(LORA_CAD_04_SYMBOL,0,1,LORA_CAD_ONLY,1);
8b51c7 276
842bb6 277     nTimeOnAir = Radio.TimeOnAir(MODEM_LORA,14);
Q 278     KwRunStat.nTimeOnAir =     nTimeOnAir;                                
279     KMem.WDT[38]=nTimeOnAir;                                                                
483170 280     KwRunStat.runStep=RS_IDEL;
Q 281     return 0;
282 }
eaf5d5 283
483170 284 int KWireLessStart(void)
Q 285 {
286     if(RadioEnableMaster)
287     {
288             KWLMasterSendReqPkt(1);
289     }
290     else
291     {
292        Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); 
293             KwRunStat.runStep=RS_RECVING;
294             
eaf5d5 295             KwRunStat.lastActTime = GetTick();
Q 296             KwRunStat.lastRecvtime = GetTick();            
483170 297     }
Q 298     
299 //    while( 1 )
300  //   {
301  //       Radio.IrqProcess( ); // Process Radio IRQ
302 //    }
303     return 0;
304 }
305
306 void OnTxDone( void )
307 {   
842bb6 308         KwRunStat.bMasterSent = 1;
Q 309         KwRunStat.bMasterRecved = 0;
483170 310         KwRunStat.runStep=RS_IDEL;
Q 311         KMem.WDT[42]++;
eaf5d5 312         KwRunStat.lastSenttime = GetTick();
Q 313         KwRunStat.lastAckTime = GetTick();
483170 314     Radio.Standby();
Q 315         if (RadioEnableMaster) {
316             Radio.Rx( MASTER_RX_TIMEOUT_VALUE ); //进入接收
317         }    else     {
318             Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); //进入接收
319         }
320         KwRunStat.runStep=RS_RECVING;
321         
eaf5d5 322         KwRunStat.lastActTime = GetTick();    
Q 323         KwRunStat.lastRecvtime = GetTick();
483170 324         LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);
eaf5d5 325             int us2=GetTick();
483170 326             KMem.ScanTimeuS=us2-KMem.LastScanTime;
Q 327         KMem.LastScanTime = us2;
328         if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
329         if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
330         
331 }
332
333 void OnTxTimeout( void )
334 {
335         KwRunStat.runStep=RS_IDEL;
eaf5d5 336         KwRunStat.lastAckTime = GetTick();
483170 337         KwRunStat.TXErr++;
Q 338     Radio.Standby();    
8b51c7 339 //    KMem.WDT[44]++;
483170 340 }
Q 341 bool bThisRxError=0;
eaf5d5 342
483170 343 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
Q 344 {
8b51c7 345         KMem.WDT[44] = size;
eaf5d5 346     memcpy( RX_Buffer, payload, size );    
8b51c7 347         memcpy(&KMem.WDT[64],payload,size);
483170 348     Radio.Standby();
eaf5d5 349     
8b51c7 350         if (bThisRxError) {
Q 351             bThisRxError=0;
842bb6 352             return;
8b51c7 353         }
483170 354         
Q 355         KwRunStat.curStat = 0;
eaf5d5 356         KwRunStat.latancy = GetTick() - KwRunStat.lastSendtime;
Q 357
483170 358     RssiValue = rssi;
Q 359     SnrValue = snr;
360     
361         KwRunStat.RSSI = RssiValue;
362         KwRunStat.SNR = SnrValue;
363     
364         KwRunStat.recvCount++;
8b51c7 365 //    KwRunStat.CtnLstPkts=0;
eaf5d5 366
483170 367     if(RadioEnableMaster)
Q 368     {
8b51c7 369                 KWLMasterParsePkt(nRadioChannel,size);
eaf5d5 370             //KWLMasterSendReqPkt(1);
Q 371     }
372     else            //slave
373     {
8b51c7 374                 KWLSlaveParsePkt(nRadioChannel,size);
eaf5d5 375     }
Q 376 }
377
378 void OnRxTimeout( void )
379 {
380         KwRunStat.runStep=RS_IDEL;
381     
382     Radio.Standby();
383         KwRunStat.lastErrTime = GetTick() - KwRunStat.lastAckTime;
384         KwRunStat.lastAckTime = GetTick();
385     
386         KMem.WDT[45]++;
387         KwRunStat.LostPackets++;
388         KwRunStat.CtnLstPkts++;
389         if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
842bb6 390 //        KwRunStat.ErrStat=500;
Q 391         if (KwRunStat.CtnLstPkts > 2) {        KwRunStat.ErrStat=500;}
392             if (KwRunStat.CtnLstPkts ==3 ) {KwRunStat.Err1Count++;}
393         if (KwRunStat.CtnLstPkts > 5) {        KwRunStat.ErrStat=5000;}
394             if (KwRunStat.CtnLstPkts == 6) {KwRunStat.Err2Count++;}
395         if (KwRunStat.CtnLstPkts > 9) {    KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
396             if (KwRunStat.CtnLstPkts == 10) {KwRunStat.Err3Count++;}        
397         if ((KwRunStat.CtnLstPkts &0x0f) == 0x0f) {
398                 KMem.WDT[51]++;
399                 LoadKwConfig();
400                 KWireLessInit(RadioEnableMaster,nRadioChannel);
401                 Radio.Standby();
402             //    KWireLessStart();
403         }
eaf5d5 404     if(RadioEnableMaster)
Q 405     {
406             //KWLMasterSendReqPkt(1);
407     }
408     else
409     {
842bb6 410             Radio.Standby();
eaf5d5 411       Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); 
Q 412             KwRunStat.runStep=RS_RECVING;
413             KwRunStat.lastActTime = GetTick();            
414             KwRunStat.lastRecvtime = GetTick();
415     }
416 }
417
418
419 void OnRxError( void )
420 {
842bb6 421             Radio.Standby();
Q 422       Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); 
423             KwRunStat.runStep=RS_RECVING;
424             KwRunStat.lastActTime = GetTick();            
425             KwRunStat.lastRecvtime = GetTick();            
426     
eaf5d5 427             KwRunStat.RXErr++;
Q 428             KMem.WDT[46]++;
429             bThisRxError=1;
430         KwRunStat.lastErrTime = GetTick() - KwRunStat.lastAckTime;
431     
432     if(RadioEnableMaster)
433     {
434 //            SendPingMsg();
435     }
436     else
437     {
438 //      Radio.Rx( RX_TIMEOUT_VALUE ); 
439                 //KwRunStat.lastActTime = GetTick();            
440             KwRunStat.LostPackets++;
441             KwRunStat.CtnLstPkts++;
442             if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
842bb6 443 //            KwRunStat.ErrStat=500;
Q 444             if (KwRunStat.CtnLstPkts > 1) {        KwRunStat.ErrStat=500; }            
eaf5d5 445             if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;}
842bb6 446             if (KwRunStat.CtnLstPkts > 3) {        KwRunStat.ErrStat=5000;}
eaf5d5 447             if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
842bb6 448             if (KwRunStat.CtnLstPkts > 6) {    KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
eaf5d5 449             if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}
842bb6 450             
eaf5d5 451     }
842bb6 452 //        Radio.Standby();
eaf5d5 453 }
Q 454
455 void OnCadDone( bool channelActivityDetected)
456 {
457     KwRunStat.CADDoneCount++;
458     return;
459 }
460 int KWLMasterParsePkt(int nChn,int size)
461 {
462     bool CRC_OK =1;
463 ///*        
464     crc_value=RadioComputeCRC(RX_Buffer,size-2,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
465     if (RX_Buffer[size-2] != (crc_value&0xff) && RX_Buffer[size-1] != (crc_value >> 8))
466     {
467         KwRunStat.CRCErr++;
842bb6 468         KwRunStat.ErrStat=500;
eaf5d5 469         CRC_OK = 0;
Q 470     }else {
471         CRC_OK = 1;
472     }    
473 //*/    
474     pstKLPacket p1 = (pstKLPacket) RX_Buffer;
8b51c7 475       if(CRC_OK && p1->STSign == enRplySign ) //memcmp(RX_Buffer,PongMsg,4)==0 
483170 476       {
8b51c7 477                 if (p1->DstAddr == MixAddr(nRadioChannel,nRadioAddr))
Q 478                     {
842bb6 479                         KwRunStat.bMasterRecved = 1;                        
8b51c7 480                         LedToggle();//LED闪烁
Q 481                         KwRunStat.lastRecvdtime=GetTick();
482                         KwRunStat.lastAckTime = GetTick();                        
483                         KwRunStat.CtnLstPkts=0;                        
484                         
485                         if (p1->Stat==0) {
486                             KwRunStat.tRSSI = RX_Buffer[10];
487                             KwRunStat.tSNR = RX_Buffer[11];
488                         }else if (p1->Stat==1){
489                             KwRunStat.targetSentCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24);
490                         }else if (p1->Stat==2) {
491                             KwRunStat.targetRecvdCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24);
492                         }else if (p1->Stat==3) {
493                         }
eaf5d5 494
8b51c7 495                             KMem.WDT[41]=(-KwRunStat.tRSSI) + ((-KwRunStat.tSNR)<<8);
eaf5d5 496
8b51c7 497                             KMem.WFX[1]=(RX_Buffer[4]) + (RX_Buffer[5]<<8);
Q 498                     //        KMem.WFX[1]=(RX_Buffer[4]) + (indicator3<<8);
499                         
500                             KMem.WFX[2]=(RX_Buffer[6]) + (RX_Buffer[7]<<8);
501                     }
483170 502       }
eaf5d5 503     
Q 504     return 0;
505 }
506 int KWLSlaveParsePkt(int nChn,int size)
507 {
508         bool CRC_OK =1;
509     pstKLPacket p1 = (pstKLPacket) RX_Buffer;    
510 ///*        
511     crc_value=RadioComputeCRC(RX_Buffer,size-2,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
512     if (RX_Buffer[size-2] != (crc_value&0xff) && RX_Buffer[size-1] != (crc_value >> 8))
513     {
514         KwRunStat.CRCErr++;
842bb6 515         KwRunStat.ErrStat=500;
eaf5d5 516         CRC_OK = 0;
Q 517     }else {
518         CRC_OK = 1;
842bb6 519             if (p1->STSign != enReqSign) {
Q 520                 KwRunStat.PktErr++;
521             }else {
522             if (p1->DstAddr != MixAddr(nRadioChannel,nRadioAddr)) {
523                 KwRunStat.ChnErr++;
524             }
525             }                
eaf5d5 526     }
Q 527 //*/    
842bb6 528
8b51c7 529       if(CRC_OK && p1->STSign == enReqSign && p1->DstAddr == MixAddr(nRadioChannel,nRadioAddr))// memcmp(RX_Buffer,PingMsg,4)==0 && )
483170 530       {
8b51c7 531
483170 532         LedToggle();//LED闪烁
8b51c7 533                 KwRunStat.lastRecvdtime=GetTick();
Q 534                 KwRunStat.lastAckTime = GetTick();                
535                 KwRunStat.CtnLstPkts=0;
536                 
eaf5d5 537                 if (p1->Stat==0) {
Q 538                     KwRunStat.tRSSI = RX_Buffer[10];
539                     KwRunStat.tSNR = RX_Buffer[11];
540                 }else if (p1->Stat==1){
541                     KwRunStat.targetSentCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24);
542                 }else if (p1->Stat==2) {
543                     KwRunStat.targetRecvdCount = RX_Buffer[8] + (RX_Buffer[9]<<8) + (RX_Buffer[10]<<16) + (RX_Buffer[11]<<24);
544                 }else if (p1->Stat==3) {
545                 }
483170 546                 KMem.WFX[1]=(RX_Buffer[4]) + (RX_Buffer[5]<<8);
Q 547             //    KMem.WFX[1]=(RX_Buffer[4]) + (indicator3<<8);
548                 
549                 KMem.WFX[2]=(RX_Buffer[6]) + (RX_Buffer[7]<<8);                
550
551         KWLSlaveSendRplyPkt(1);
552 //                KMem.WFY[0]=(RX_Buffer[4]<<8) + RX_Buffer[5];
553 //                KMem.WFY[1]=(RX_Buffer[6]<<8) + RX_Buffer[7];                
554       }
555       else
556       {
557         Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); 
558                 KwRunStat.runStep=RS_RECVING;
eaf5d5 559                 KwRunStat.lastActTime = GetTick();                
Q 560                 KwRunStat.lastRecvtime = GetTick();
483170 561                 
eaf5d5 562       }       
Q 563     return 0;
483170 564 }
Q 565
566 int KWLMasterSendReqPkt(int nChn)
567 {
568     int len1=12;
eaf5d5 569     pstKLPacket p1 = (pstKLPacket) TX_Buffer;
Q 570     p1->STSign = enReqSign;
8b51c7 571     p1->DstAddr = MixAddr(nRadioChannel,nRadioAddr);
eaf5d5 572     p1->Func = 0x3;
Q 573     p1->Stat = (p1->Stat + 1) &0x03;   //0x00;
574     p1->Data[0]=KMem.WFY[1]; 
575     p1->Data[1]=KMem.WFY[1]>>8; 
576     p1->Data[2]=KMem.WFY[2]; 
577     p1->Data[3]=KMem.WFY[2]>>8; 
578     
579 /*    
483170 580             TX_Buffer[0] = 'P';
Q 581             TX_Buffer[1] = 'I';
582             TX_Buffer[2] = 'N';
583             TX_Buffer[3] = 'G'; 
584             TX_Buffer[4] = KMem.WFY[1]; 
585             TX_Buffer[5] = KMem.WFY[1]>>8; 
586             TX_Buffer[6] = KMem.WFY[2]; 
587             TX_Buffer[7] = KMem.WFY[2]>>8; 
eaf5d5 588 */
483170 589
eaf5d5 590     if (p1->Stat == 0)
Q 591     {
592             TX_Buffer[10] = RssiValue;
593             TX_Buffer[11] = SnrValue;
594     }else if (p1->Stat == 1){
595         memcpy(TX_Buffer+8,&KwRunStat.sentCount,4);
596     }else if (p1->Stat == 2) {
597         memcpy(TX_Buffer+8,&KwRunStat.recvCount,4);        
598     }else if (p1->Stat == 3) {
599         
600     }
483170 601             crc_value=RadioComputeCRC(TX_Buffer,len1,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
Q 602             TX_Buffer[len1]=crc_value;
603             TX_Buffer[len1+1]=crc_value>>8;
604     
605             KMem.WDT[56]=crc_value;
606     
607             KwRunStat.sentCount++;
eaf5d5 608             KwRunStat.cycleTime = GetTick()- KwRunStat.lastSendtime ;    
Q 609             KwRunStat.lastSendtime = GetTick();    
483170 610             Radio.Send( TX_Buffer, len1+2);
Q 611             KwRunStat.runStep=RS_SENDING;
eaf5d5 612             KwRunStat.lastActTime = GetTick();
483170 613     
Q 614     return 0;
615 }
616
617 int KWLSlaveSendRplyPkt(int nChn)
618 {
619     int len1=12;
eaf5d5 620     pstKLPacket p1 = (pstKLPacket) TX_Buffer;
Q 621     p1->STSign = enRplySign;
8b51c7 622     p1->DstAddr = MixAddr(nRadioChannel,nRadioAddr);
eaf5d5 623     p1->Func = 0x3;
Q 624     p1->Stat = (p1->Stat + 1) &0x03;   //0x00;
625     p1->Data[0]=KMem.WFY[1]; 
626     p1->Data[1]=KMem.WFY[1]>>8; 
627     p1->Data[2]=KMem.WFY[2]; 
628     p1->Data[3]=KMem.WFY[2]>>8; 
629 /*    
483170 630             TX_Buffer[0] = 'P';
Q 631             TX_Buffer[1] = 'O';
632             TX_Buffer[2] = 'N';
633             TX_Buffer[3] = 'G'; 
634             TX_Buffer[4] = KMem.WFY[1]; 
635             TX_Buffer[5] = KMem.WFY[1]>>8; 
636             TX_Buffer[6] = KMem.WFY[2]; 
637             TX_Buffer[7] = KMem.WFY[2]>>8; 
eaf5d5 638 */
Q 639     if (p1->Stat == 0)
640     {
483170 641             TX_Buffer[10] = RssiValue;
Q 642             TX_Buffer[11] = SnrValue;
eaf5d5 643     }else if (p1->Stat == 1){
Q 644         memcpy(TX_Buffer+8,&KwRunStat.sentCount,4);
645     }else if (p1->Stat == 2) {
646         memcpy(TX_Buffer+8,&KwRunStat.recvCount,4);        
647     }else if (p1->Stat == 3) {
648         
649     }    
650
483170 651             crc_value=RadioComputeCRC(TX_Buffer,len1,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
Q 652             TX_Buffer[len1]=crc_value;
653             TX_Buffer[len1+1]=crc_value>>8;
654             Radio.Send( TX_Buffer, len1+2);    
655             KwRunStat.runStep=RS_SENDING;
656     
eaf5d5 657             KwRunStat.lastActTime = GetTick();
483170 658             KwRunStat.sentCount++;
eaf5d5 659             KwRunStat.lastSendtime = GetTick();        
483170 660     return 0;
Q 661 }
662
663
664 int KWLMasterProc(int nChn)
665 {
666     return 0;
667 }
668
669 int KWLSlaveProc(int nChn)
670 {
842bb6 671             RadioState_t stat = Radio.GetStatus();
483170 672             if (stat == RF_IDLE){
Q 673                     KMem.WDT[48]++;
674                     Radio.Standby();
675                     KWireLessStart();
676             }
677                 if (stat == RF_RX_RUNNING ){
8b51c7 678                     if (GetTick() - KwRunStat.lastRecvtime > ((SLAVE_RX_TIMEOUT_VALUE + 10 )*10)){
eaf5d5 679                         KwRunStat.lastRecvtime = GetTick();
Q 680                         KwRunStat.StepErr1++;
483170 681                         KMem.WDT[49]++;
Q 682                         Radio.Standby();
683                         KWireLessStart();
684                     }
685                 }
eaf5d5 686                 if (stat == RF_CAD && GetTick() - KwRunStat.lastRecvtime > ((1 )*10)){
Q 687                         KwRunStat.StepErr2++;
483170 688                         KMem.WDT[50]++;
Q 689                         Radio.Standby();
690                         KWireLessStart();
691                 }
8b51c7 692                 if (GetTick() - KwRunStat.lastRecvdtime > 4000){    // 200mS
842bb6 693             //            KwRunStat.ErrStat=500;
Q 694                         KwRunStat.ErrStat=500; 
695 //                        Radio.Standby();
696 //                        KWireLessStart();
8b51c7 697                 }
Q 698                 if (GetTick() - KwRunStat.lastRecvdtime > 12000){    // 1200mS
842bb6 699                         KwRunStat.ErrStat=5000; KMem.WFX[1]=0; 
Q 700                 }
701                 if (GetTick() - KwRunStat.lastRecvtime > 16000){
702                         KMem.WDT[52]++;
703                         LoadKwConfig();
704                         KWireLessInit(RadioEnableMaster,nRadioChannel);
705                         Radio.Standby();
706                         KWireLessStart();
707                     KwRunStat.lastRecvtime = GetTick();
708                 }                        
709     return 0;
710 }
711
712 int KWL_Process(int nChn)
713 {
714         RadioState_t stat = Radio.GetStatus();
715         KMem.WDT[32]=stat;
716 //        KMem.WDT[38]=Radio.Rssi(MODEM_FSK);
717         if (RadioEnableMaster){
718             if (GetTick() - KwRunStat.lastSendtime > CYCLE_TIME *10 && Radio.IsChannelFree(MODEM_LORA,KwRunStat.RF_Freq,-60,1)){
719                 if (!KwRunStat.bMasterRecved) {
720                         KwRunStat.ErrStat=5000; 
721                         KwRunStat.LostPackets++;
722                         KwRunStat.CtnLstPkts++;
723                         if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
724                 //        KwRunStat.ErrStat=500;
725                         if (KwRunStat.CtnLstPkts > 1) {        KwRunStat.ErrStat=2000;}
726                             if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;}
727                         if (KwRunStat.CtnLstPkts > 3) {        KwRunStat.ErrStat=5000;}
728                             if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
729                         if (KwRunStat.CtnLstPkts > 6) {    KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
730                             if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}                            
731                     if ((KwRunStat.CtnLstPkts &0x0f) == 0x0f) {
732                             KMem.WDT[51]++;
733                             LoadKwConfig();
734                             KWireLessInit(RadioEnableMaster,nRadioChannel);
735                             Radio.Standby();
736                         //    KWireLessStart();
737                     }                            
738                 }
739                 if (KwRunStat.curStat == 0 ){ //&& stat == RF_IDLE )    //200mS
740 //                    Radio.Standby();
741                     KWireLessStart();
742                 }else {
743                     Radio.Standby();
744                     KWireLessStart();
745                 }
746             }
747         }else            //slave
748         {
749                 KWLSlaveProc(nChn);
483170 750         }
842bb6 751     if (KwRunStat.RunStat) KwRunStat.RunStat--;
Q 752     if (KwRunStat.ErrStat) KwRunStat.ErrStat--;
753         
754         KwRunStat.Tx_Power = WLCfg.Tx_Power;
755         KwRunStat.LoraBandWidth = WLCfg.LoraBandWidth;
756         KwRunStat.LoRaFactor = WLCfg.LoRaFactor;
757         KwRunStat.LoRaCodingRate = WLCfg.LoRaCodingRate;
758         KwRunStat.LoRaPreambleLen = WLCfg.LoRaPreambleLen;        
759         
760         
483170 761     return 0;
Q 762 }
763
764
765
766