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