QuakeGod
2024-11-25 9aed5d7e7b3c7bf09da712e9c272ece401a7acc9
提交 | 用户 | age
9aed5d 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
23 /*
24 #define REGION_CN779
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
36 #define RF_FREQUENCY                                430620000 // Hz
37
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
63 */
64 /*
65 #if defined( USE_MODEM_LORA )
66
67 #define LORA_BANDWIDTH                              1         // [0: 125 kHz,    
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 /*
98 #define nChannelSpacing                                                         530000 // Hz
99 #define TX_OUTPUT_POWER                             16        // 22 dBm
100 */
101
102
103 extern bool IrqFired;
104
105 //bool RadioEnableMaster=true;//主从选择
106 //uchar nRadioChannel = 0;
107 //uchar nRadioAddr = 1;
108 uint16_t  crc_value;
109
110 #define MixAddr(x,y) ((x<<4)|(y))
111 /*!
112  * Radio events function pointer
113  */
114 #define MASTER_RX_TIMEOUT_VALUE                 80            //mS
115 #define SLAVE_RX_TIMEOUT_VALUE                  400            //mS
116 #define CYCLE_TIME                                                            80            //mS
117
118 const stWLConfig defaultWLConfig =
119 {
120     .RF_T_Freq = 430620000,            // uint32_t     Hz
121     .RF_R_Freq = 430620000,            // uint32_t         //Hz
122     .nChnSpacing = 530,            //          uint16_t ChannelSpacing;        //kHz    
123     .nCycleTime = CYCLE_TIME,        // CyCleTime
124     .workMode = 1,                //            uchar workMode;            //0: FSK,    1: LoRa    
125     .nChannel = 0,
126     .bMaster = 0,
127     .nRadioAddr =1 ,
128     .bEnableMulti = 0,
129     
130     .Tx_Power = 20,                //      uchar Tx_Power;
131     .LoraBandWidth = 1,                //             uchar LoraBandWidth;        //        [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ] 
132     .LoRaFactor = 5,                //             uchar LoRaFactor;                //        [SF5 .. SF 12]
133     .LoRaCodingRate = 1,                //            uchar LoRaCodingRate;        //        [1 : 4/5,  2: 4/6,  3:  4/7,    4:  4/8
134     .NetWorkAddr = 0x00,            //            uint8_t NetWorkAddr;
135     .DeviceAddr = 0x0102,        //            uint16_t DeviceAddr;
136     .bEnableAddr = 0,                //            uchar bEnableAddr;
137     .bEnableEncrypt = 0,                //            uchar bEnableEncrypt;
138     .bEnableRelay = 0,                //            uchar bEnableRelay;
139     .LoRaPreambleLen = 4,                //            uchar LoRaPreamble_Len;            // 2 - 12
140     .bAutoPower = 1,
141     .bAutoReSend = 1,                        // 自动重发
142 };
143
144 //stWLConfig * pWLCfg = (stWLConfig *)&defaultWLConfig;
145 static RadioEvents_t RadioEvents;
146
147 stWLConfig WLCfg ;
148 stWLRunStat KwRunStat;
149
150 typedef enum
151 {
152     LOWPOWER,
153     RX,
154     RX_TIMEOUT,
155     RX_ERROR,
156     TX,
157     TX_TIMEOUT,
158 }States_t;
159
160
161
162 #define WL_RX_BUFFER_SIZE                                                 160                         // Define the payload size here
163 #define WL_TX_BUFFER_SIZE                                                 160                         // Define the payload size here
164
165 uint16_t nTimeOnAir;
166 /*
167 const uint8_t PingMsg[] = "PING";
168 const uint8_t PongMsg[] = "PONG";
169 */
170
171 //uint16_t BufferSize = BUFFER_SIZE;
172 uint8_t KwRx_Buffer[WL_RX_BUFFER_SIZE];
173 uint8_t KwTx_Buffer[WL_TX_BUFFER_SIZE];
174
175 #define WL_TT_EACH_SIZE                                                                64                        // transparent transmission byte size for each send
176
177
178 States_t State = LOWPOWER;
179
180 int8_t RssiValue = 0;
181 int8_t SnrValue = 0;
182
183 KWStates KW_State=KW_PON;
184
185 void LedToggle(void)
186 {
187 //  GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_RESET);//LED闪烁
188 //  HAL_Delay_nMS(10);
189 //  GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_SET);
190     
191         LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_13);        
192 }
193
194 int LoadKwConfig(void)
195 {
196     stStoredWLConfig * pstStoredWLCFG = (stStoredWLConfig *)(STORE_KWCONFIG_BASE);
197
198     if (pstStoredWLCFG->BlockSign == 0x55AA && pstStoredWLCFG->BlockType == 0x05) {
199         WLCfg = pstStoredWLCFG->WLConfig;
200     } else     {
201         WLCfg = defaultWLConfig;
202     }
203     return 0;
204 }
205
206 int SaveKwConfig(void)
207 {
208 //    stStoredWLConfig * pstStoredWLCFG = (stStoredWLConfig *)(STORE_KWCONFIG_BASE);
209
210     stStoredWLConfig theStoredWLCFG;
211     theStoredWLCFG.BlockSign = 0x55AA;
212     theStoredWLCFG.BlockType = 0x05;
213     theStoredWLCFG.nSeq = 1;
214     theStoredWLCFG.nSize = sizeof(stWLConfig);
215     theStoredWLCFG.WLConfig = WLCfg;
216     theStoredWLCFG.nCRC16 = 0x0000;
217     EraseAndWriteToFlashMem(&theStoredWLCFG, (void *)STORE_KWCONFIG_BASE, sizeof(stStoredWLConfig));
218     
219     return 0;
220 }
221
222 int KWireLessInit(bool bRadioEnableMaster, uint32_t nChn)
223 {
224         stWLConfig * pWLCfg = & WLCfg;
225         pWLCfg->bMaster = bRadioEnableMaster;
226     // Radio initialization
227     RadioEvents.TxDone = OnTxDone;
228     RadioEvents.RxDone = OnRxDone;
229     RadioEvents.TxTimeout = OnTxTimeout;
230     RadioEvents.RxTimeout = OnRxTimeout;
231     RadioEvents.RxError = OnRxError;
232     RadioEvents.CadDone = OnCadDone;
233     
234     Radio.Init( &RadioEvents );
235         pWLCfg->nChannel = nChn;
236         KwRunStat.RF_Freq = pWLCfg->RF_T_Freq + pWLCfg->nChannel * pWLCfg->nChnSpacing*1000;
237     
238     Radio.SetChannel( KwRunStat.RF_Freq );
239     
240   //  Radio.WriteBuffer(0x06C0,data,2);
241    // Radio.ReadBuffer(0x06C0,test,2);
242     
243 #if defined( USE_MODEM_LORA )
244 /*    
245     Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
246                                    LORA_SPREADING_FACTOR, LORA_CODINGRATE,
247                                    LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
248                                    true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
249     
250     Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
251                                    LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
252                                    LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
253                                    0, true, 0, 0, LORA_IQ_INVERSION_ON, false );
254 // */                                                                    
255
256     Radio.SetTxConfig( MODEM_LORA, pWLCfg->Tx_Power, 0, pWLCfg->LoraBandWidth,
257                                    pWLCfg->LoRaFactor, pWLCfg->LoRaCodingRate,
258                                    pWLCfg->LoRaPreambleLen, LORA_FIX_LENGTH_PAYLOAD_ON,
259                                    true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
260                                                                      
261     Radio.SetRxConfig( MODEM_LORA, pWLCfg->LoraBandWidth, pWLCfg->LoRaFactor,
262                                    pWLCfg->LoRaCodingRate, 0, pWLCfg->LoRaPreambleLen,
263                                    LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
264                                    0, true, 0, 0, LORA_IQ_INVERSION_ON, false );                                                                     
265                                                                      
266         Radio.SetMaxPayloadLength(MODEM_LORA,32);                                                                     
267     
268 #elif defined( USE_MODEM_FSK )
269     
270     Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
271                                   FSK_DATARATE, 0,
272                                   FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
273                                   true, 0, 0, 0, 3000 );
274     
275     Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
276                                   0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
277                                   0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, true,
278                                   0, 0,false, false );
279 #else
280     #error "Please define a modem in the compiler options.."
281 #endif
282         SX126xSetRxTxFallbackMode(0x40); // 0x40-> FS    0x30 -> SD_XOSC  0x20 -> SD_RC
283         
284         SX126xSetCadParams(LORA_CAD_02_SYMBOL,22,10,LORA_CAD_ONLY,1);
285
286     nTimeOnAir = Radio.TimeOnAir(MODEM_LORA,14);
287     KwRunStat.nTimeOnAir =     nTimeOnAir;                                
288 //    KMem.WDT[38]=nTimeOnAir;                                                                
289     KwRunStat.runStep=RS_IDLE;
290     KMem.WDT[0]++;
291         KwRunStat.Tx_Power = WLCfg.Tx_Power;
292         KwRunStat.LoraBandWidth = WLCfg.LoraBandWidth;
293         KwRunStat.LoRaFactor = WLCfg.LoRaFactor;
294         KwRunStat.LoRaCodingRate = WLCfg.LoRaCodingRate;
295         KwRunStat.LoRaPreambleLen = WLCfg.LoRaPreambleLen;        
296                                                                     
297     return 0;
298 }
299
300 int KwResetRf()
301 {
302     //    LoadKwConfig();
303     //    KWireLessInit(RadioEnableMaster,nRadioChannel);    
304     Radio.Standby();
305     KwRunStat.runStep=RS_IDLE;    
306     return 0;
307 }
308
309 int KwStartRecv(int nChnd)
310 {
311         Radio.Standby();
312              Radio.SetChannel( KwRunStat.RF_Freq );
313             Radio.SetRxConfig( MODEM_LORA, WLCfg.LoraBandWidth, WLCfg.LoRaFactor,
314                                    WLCfg.LoRaCodingRate, 0, WLCfg.LoRaPreambleLen,
315                                    LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
316                                    0, true, 0, 0, LORA_IQ_INVERSION_ON, false );                                                                     
317                                                                      
318             Radio.SetMaxPayloadLength(MODEM_LORA,32);                                                                     
319             
320        Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); 
321             KwRunStat.runStep=RS_RECVING;
322             
323             KwRunStat.lastActTime = GetTick();
324             KwRunStat.lastRecvtime = GetTick();            
325         return 0;
326 }
327
328 int KWireLessStart(void)
329 {
330     if(WLCfg.bMaster)
331     {
332             KWLMasterSendReqPkt(1);
333     }
334     else
335     {
336             KwStartRecv(1);
337     }
338     
339 //    while( 1 )
340  //   {
341  //       Radio.IrqProcess( ); // Process Radio IRQ
342 //    }
343     return 0;
344 }
345
346
347 void OnTxDone( void )
348 {   
349         KwRunStat.bMasterSent = 1;
350         KwRunStat.bMasterRecved = 0;
351         KwRunStat.runStep=RS_IDLE;
352         KMem.WDT[42]++;
353         KwRunStat.lastSenttime = GetTick();
354         KwRunStat.lastAckTime = GetTick();
355     Radio.Standby();
356         if (WLCfg.bMaster) {
357             Radio.Rx( MASTER_RX_TIMEOUT_VALUE ); //进入接收
358         }    else     {
359             KwStartRecv(1);
360             //Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); //进入接收
361         }
362         KwRunStat.runStep=RS_RECVING;
363         
364         KwRunStat.lastActTime = GetTick();    
365         KwRunStat.lastRecvtime = GetTick();
366         LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);
367 //            int us2=GetTick();
368 //            KMem.ScanTimeuS=us2-KMem.LastScanTime;
369 //            KMem.LastScanTime = us2;
370 //        if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
371 //        if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
372         
373 }
374
375 void OnTxTimeout( void )
376 {
377         KwRunStat.runStep=RS_IDLE;
378         KwRunStat.lastAckTime = GetTick();
379         KwRunStat.TXErr++;
380     Radio.Standby();    
381 //    KMem.WDT[44]++;
382 }
383 bool bThisRxError=0;
384
385 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
386 {
387         KMem.WDT[44] = size;
388         if (size <= 120) {
389             memcpy( KwRx_Buffer, payload, size );    
390             memcpy(&KMem.WDT[64],payload,size);
391         }else
392         {
393             
394         }
395     Radio.Standby();
396         KwRunStat.runStep = RS_IDLE;
397
398         
399         KwRunStat.curStat = 0;
400         KwRunStat.latancy = GetTick() - KwRunStat.lastSendtime;
401
402     RssiValue = rssi;
403     SnrValue = snr;
404     
405         KwRunStat.RSSI = RssiValue;
406         KwRunStat.SNR = SnrValue;
407     
408         KwRunStat.recvCount++;
409 //    KwRunStat.CtnLstPkts=0;
410
411     if(WLCfg.bMaster)
412     {
413                 KWLMasterParsePkt(WLCfg.nChannel,size);
414             //KWLMasterSendReqPkt(1);
415     }
416     else            //slave
417     {
418                 KWLSlaveParsePkt(WLCfg.nChannel,size);
419     }
420         if (bThisRxError) {
421             bThisRxError=0;
422     //        return;
423         }
424 }
425
426 void OnRxTimeout( void )
427 {
428         KwRunStat.runStep=RS_IDLE;
429     
430     Radio.Standby();
431         KwRunStat.lastErrTime = GetTick() - KwRunStat.lastAckTime;
432         KwRunStat.lastAckTime = GetTick();
433     
434         KwRunStat.Tx_Power+=6;                //丢包, 增加发射功率
435         if (KwRunStat.Tx_Power > WLCfg.Tx_Power) {KwRunStat.Tx_Power= WLCfg.Tx_Power; }
436         SX126xSetRfTxPower(KwRunStat.Tx_Power);
437                         
438     
439         KMem.WDT[45]++;
440         KwRunStat.LostPackets++;
441         KwRunStat.CtnLstPkts++;
442         if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
443 //        KwRunStat.ErrStat=500;
444         if (KwRunStat.CtnLstPkts > 2) {        KwRunStat.ErrStat=500;}
445             if (KwRunStat.CtnLstPkts ==3 ) {KwRunStat.Err1Count++;}
446         if (KwRunStat.CtnLstPkts > 5) {        KwRunStat.ErrStat=5000;}
447             if (KwRunStat.CtnLstPkts == 6) {KwRunStat.Err2Count++;}
448         if (KwRunStat.CtnLstPkts > 9) {    KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
449             if (KwRunStat.CtnLstPkts == 10) {KwRunStat.Err3Count++;}        
450         if ((KwRunStat.CtnLstPkts &0x0f) == 0x0f) {
451                 KMem.WDT[51]++;
452                 KwResetRf();
453                 Radio.Standby();
454             //    KWireLessStart();
455         }
456     if(WLCfg.bMaster)
457     {
458             //KWLMasterSendReqPkt(1);
459     }
460     else
461     {
462             KwStartRecv(1);
463 //            Radio.Standby();
464 //     Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); 
465 //            KwRunStat.runStep=RS_RECVING;
466 //            KwRunStat.lastActTime = GetTick();            
467 //            KwRunStat.lastRecvtime = GetTick();
468     }
469 }
470
471
472 void OnRxError( void )
473 {
474             KwStartRecv(1);
475 //            Radio.Standby();
476 //     Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); 
477 //            KwRunStat.runStep=RS_RECVING;
478 //            KwRunStat.lastActTime = GetTick();            
479 //            KwRunStat.lastRecvtime = GetTick();            
480     
481             KwRunStat.RXErr++;
482             KMem.WDT[46]++;
483             bThisRxError=1;
484         KwRunStat.lastErrTime = GetTick() - KwRunStat.lastAckTime;
485     
486     if(WLCfg.bMaster)
487     {
488 //            SendPingMsg();
489     }
490     else
491     {
492 /*            
493 //      Radio.Rx( RX_TIMEOUT_VALUE ); 
494                 //KwRunStat.lastActTime = GetTick();            
495             KwRunStat.LostPackets++;
496             KwRunStat.CtnLstPkts++;
497             if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
498 //            KwRunStat.ErrStat=500;
499             if (KwRunStat.CtnLstPkts > 1) {        KwRunStat.ErrStat=500; }            
500             if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;}
501             if (KwRunStat.CtnLstPkts > 3) {        KwRunStat.ErrStat=5000;}
502             if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
503             if (KwRunStat.CtnLstPkts > 6) {    KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
504             if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}
505 */            
506     }
507 //        Radio.Standby();
508 }
509
510 void OnCadDone( bool channelActivityDetected)
511 {
512     KMem.WDT[60]++;
513     KwRunStat.CADDoneCount++;
514     KwRunStat.lastAckTime = GetTick();
515     KMem.WDT[16] = KwRunStat.lastAckTime - KwRunStat.lastActTime;
516     if (channelActivityDetected) {
517         KwRunStat.CADNgCount++;
518         KMem.WDT[61]++;
519         Radio.StartCad();
520         KwRunStat.lastActTime = GetTick();
521     }
522     else {
523         KwRunStat.CADOkCount++;
524         KMem.WDT[62]++;
525         if (WLCfg.bMaster) {
526             if (KwRunStat.curStat == 0 ){ //&& stat == RF_IDLE )    //200mS
527     //                    Radio.Standby();
528                 KWireLessStart();
529             }else {
530                 Radio.Standby();
531                 KWireLessStart();
532             }
533         }else {
534             KWLSlaveSendRplyPkt(1);
535         }
536     }
537     return;
538 }
539
540 int KWLMasterParsePkt(int nChn,int size)
541 {
542     bool CRC_OK =1;
543 ///*        
544     crc_value=RadioComputeCRC(KwRx_Buffer,size-2,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
545     if (KwRx_Buffer[size-2] != (crc_value&0xff) && KwRx_Buffer[size-1] != (crc_value >> 8))
546     {
547         KwRunStat.CRCErr++;
548         KwRunStat.ErrStat=500;
549         CRC_OK = 0;
550     }else {
551         CRC_OK = 1;
552     }    
553 //*/    
554     pstKwPacket p1 = (pstKwPacket) KwRx_Buffer;
555       if(CRC_OK && p1->STSign == enRplySign ) //memcmp(RX_Buffer,PongMsg,4)==0 
556       {
557                 if (p1->DstAddr == MixAddr(WLCfg.nChannel,WLCfg.nRadioAddr))
558                     {
559                         KwRunStat.bMasterRecved = 1;                        
560                         LedToggle();//LED闪烁
561                         KwRunStat.lastRecvdtime=GetTick();
562                         KwRunStat.lastAckTime = GetTick();                        
563                         KwRunStat.CtnLstPkts=0;                        
564                         
565                         if (p1->Stat==0) {
566                             KwRunStat.tRSSI = KwRx_Buffer[10];
567                             KwRunStat.tSNR = KwRx_Buffer[11];
568                             if (KwRunStat.tRSSI > -50) {
569                                 if (KwRunStat.Tx_Power > 0) {KwRunStat.Tx_Power--; SX126xSetRfTxPower(KwRunStat.Tx_Power);}
570                             }else if (KwRunStat.tRSSI < -60) {
571                                 KwRunStat.Tx_Power+=6;
572                                 if (KwRunStat.Tx_Power > WLCfg.Tx_Power) {KwRunStat.Tx_Power= WLCfg.Tx_Power; }
573                                 SX126xSetRfTxPower(KwRunStat.Tx_Power);
574                             }
575                             
576                         }else if (p1->Stat==1){
577                             KwRunStat.targetSentCount = KwRx_Buffer[8] + (KwRx_Buffer[9]<<8) + (KwRx_Buffer[10]<<16) + (KwRx_Buffer[11]<<24);
578                         }else if (p1->Stat==2) {
579                             KwRunStat.targetRecvdCount = KwRx_Buffer[8] + (KwRx_Buffer[9]<<8) + (KwRx_Buffer[10]<<16) + (KwRx_Buffer[11]<<24);
580                         }else if (p1->Stat==3) {
581                         }
582
583                             KMem.WDT[41]=(-KwRunStat.tRSSI) + ((-KwRunStat.tSNR)<<8);
584
585                             KMem.WFX[1]=(KwRx_Buffer[4]) + (KwRx_Buffer[5]<<8);
586                     //        KMem.WFX[1]=(RX_Buffer[4]) + (indicator3<<8);
587                         
588                             KMem.WFX[2]=(KwRx_Buffer[6]) + (KwRx_Buffer[7]<<8);
589                         
590 // 数据透传  数据的发送成功与否,需要在对方的应答包中确认
591                         if (KwRunStat.sizesending > 0) {            
592                             //  确认正在发送的,发送成功
593                             KwRunStat.sentsize += KwRunStat.sizesending;
594                             KwRunStat.sizesending = 0;
595                             
596                             if (KwRunStat.sentsize >= KwRunStat.sizetosend) {
597                                 //整体 发送完成
598                                 KwRunStat.sizetosend =0;
599                                 KwRunStat.sentsize = 0;
600                                 KwRunStat.ttAirSize = 0;
601                                 // 发送完成
602                             }
603                         }    
604 // 接受到对方发来的数据
605
606                         if (p1->bHasData && size >= 14+3 ) {
607 //                            int nFulllen1 = KwRx_Buffer[12];
608                             int nthislen2 = KwRx_Buffer[13];
609                             if (size >= 14 + nthislen2) {
610                                 if (p1->bHead) {
611                                     // 数据包的起始, 一个新的数据包的开始
612                                     KwRunStat.ttRxSize = 0;
613 //                                    SendPacket(1, "H", 1);
614                                     
615                                 }
616                                 memcpy (KwRunStat.ttRxBuf1 + KwRunStat.ttRxSize , KwRx_Buffer + 14 , nthislen2);
617                                 KwRunStat.ttRxSize += nthislen2;
618                                 if (p1->bTail) 
619                                     {
620                                     // 已经收到完整的一个数据包
621                                     KwRunStat.ttRxRecved =1;
622                                     KwRunStat.ErrStat=500;                        
623 //                                    SendPacket(1, "T", 1);
624                                     
625                                     // 后续数据包处理流程 ;
626                                  if (Uart1Mode == 1) {        // 处于透传模式, 数据发回去。
627                                         SendPacket(1, KwRunStat.ttRxBuf1, KwRunStat.ttRxSize);
628                                  }
629                                     KwRunStat.ttRxSize = 0;
630                                     KwRunStat.ttRxRecved = 0;                                     
631                                  
632                                     // 暂时流程,全部发回去。
633 //                                    memcpy (KwRunStat.ttTxBuf1, KwRunStat.ttRxBuf1, KwRunStat.ttRxSize);
634 //                                    KwRunStat.sizetosend = KwRunStat.ttRxSize;
635 //                                    KwRunStat.sentsize = 0;
636 //                                    KwRunStat.sizesending = 0;
637                                 }
638
639                             }
640                         }
641
642 /*
643
644
645                         
646                         if (KwRunStat.ttAirSize > 0)
647                         {
648                             if (KwRunStat.bttAirHeader) {    KwRunStat.ttRxRecved=0;    }
649                             memcpy(KwRunStat.ttRxBuf1 + KwRunStat.ttRxRecved,KwRunStat.ttAirBuf, KwRunStat.ttAirSize);
650                             KwRunStat.ttRxRecved += KwRunStat.ttAirSize;
651                             
652                             if (KwRunStat.bttAirTail) {
653                                 // 收到    一个数据包的最后部分    
654                                     KwRunStat.ttRxSize = KwRunStat.ttRxRecved;
655                                  if (Uart1Mode == 1) {        // 处于透传模式, 数据发回去。
656                                         SendPacket(1, KwRunStat.ttRxBuf1, KwRunStat.ttRxSize);
657                                  }
658                                 KwRunStat.ttRxSize = 0;
659                                 KwRunStat.ttRxRecved = 0;
660                             }
661                             KwRunStat.ttAirSize = 0;
662                         }
663 */                        
664                         
665                     }
666       }
667     
668     return 0;
669 }
670 int KWLSlaveParsePkt(int nChn,int size)
671 {
672         bool CRC_OK =1;
673     pstKwPacket p1 = (pstKwPacket) KwRx_Buffer;    
674 ///*        
675     crc_value=RadioComputeCRC(KwRx_Buffer,size-2,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
676     if (KwRx_Buffer[size-2] != (crc_value&0xff) && KwRx_Buffer[size-1] != (crc_value >> 8))
677     {
678         KwRunStat.CRCErr++;
679 //        KwRunStat.ErrStat=500;
680         CRC_OK = 0;
681     }else {
682         CRC_OK = 1;
683             if (p1->STSign != enReqSign) {
684                 KwRunStat.PktErr++;
685             }else {
686                 if (p1->DstAddr != MixAddr(WLCfg.nChannel,WLCfg.nRadioAddr)) {
687                     KwRunStat.ChnErr++;
688                     KwRunStat.nErrChn = p1->DstAddr;
689                 }
690             }                
691     }
692 //*/    
693
694       if(CRC_OK && p1->STSign == enReqSign && p1->DstAddr == MixAddr(WLCfg.nChannel,WLCfg.nRadioAddr))// memcmp(RX_Buffer,PingMsg,4)==0 && )
695       {
696
697         LedToggle();//LED闪烁
698                 KwRunStat.lastRecvdtime=GetTick();
699                 KwRunStat.lastAckTime = GetTick();                
700                 KwRunStat.CtnLstPkts=0;
701                 int nSeq = p1->nSeq;
702                 if (nSeq==0) {
703                     KwRunStat.tRSSI = KwRx_Buffer[10];
704                     KwRunStat.tSNR = KwRx_Buffer[11];
705                             if (KwRunStat.tRSSI > -50) {
706                                 if (KwRunStat.Tx_Power > 0) {KwRunStat.Tx_Power--; SX126xSetRfTxPower(KwRunStat.Tx_Power);}
707                             }else if (KwRunStat.tRSSI < -60) {
708                                 KwRunStat.Tx_Power+=6;
709                                 if (KwRunStat.Tx_Power > WLCfg.Tx_Power) {KwRunStat.Tx_Power= WLCfg.Tx_Power; }
710                                 SX126xSetRfTxPower(KwRunStat.Tx_Power);
711                             }
712                 }else if (nSeq==1){
713                     KwRunStat.targetSentCount = KwRx_Buffer[8] + (KwRx_Buffer[9]<<8) + (KwRx_Buffer[10]<<16) + (KwRx_Buffer[11]<<24);
714                 }else if (nSeq==2) {
715                     KwRunStat.targetRecvdCount = KwRx_Buffer[8] + (KwRx_Buffer[9]<<8) + (KwRx_Buffer[10]<<16) + (KwRx_Buffer[11]<<24);
716                 }else if (nSeq==3) {
717                 }
718                 KMem.WFX[1]=(KwRx_Buffer[4]) + (KwRx_Buffer[5]<<8);
719             //    KMem.WFX[1]=(RX_Buffer[4]) + (indicator3<<8);
720                 
721                 KMem.WFX[2]=(KwRx_Buffer[6]) + (KwRx_Buffer[7]<<8);                
722
723 // 数据透传  数据的发送成功与否,需要在对方的应答包中确认
724                         if (KwRunStat.sizesending > 0) {            
725                             //  确认正在发送的,发送成功
726                             KwRunStat.sentsize += KwRunStat.sizesending;
727                             KwRunStat.sizesending = 0;
728                             
729                             if (KwRunStat.sentsize >= KwRunStat.sizetosend) {
730                                 //整体 发送完成
731                                 KwRunStat.sizetosend =0;
732                                 KwRunStat.sentsize = 0;
733                                 KwRunStat.ttAirSize = 0;
734                                 // 发送完成
735                             }
736                         }    
737                 
738                 if (p1->bHasData && size >= 14+3 ) {
739 //                    int nFulllen1 = KwRx_Buffer[12];
740                     int nthislen2 = KwRx_Buffer[13];
741                     if (size >= 14 + nthislen2) {
742                         // 收到一个合格的携带传统数据的 包。
743                         if (p1->bHead) {
744                             // 数据段的起始, 一个新的数据段的开始
745                             KwRunStat.ttRxSize = 0;
746                         }
747                         memcpy (KwRunStat.ttRxBuf1 + KwRunStat.ttRxSize, KwRx_Buffer + 14 , nthislen2);
748                         KwRunStat.ttRxSize += nthislen2;
749                         if (p1->bTail) {
750                             // 已经收到完整的一个数据段
751                             KwRunStat.ttRxRecved =1;
752                             KwRunStat.ErrStat=500;                        
753                             
754                             // 后续数据段处理流程 ;
755                                  if (Uart1Mode == 1) {        // 处于透传模式, 数据发回去。
756                                         SendPacket(1, KwRunStat.ttRxBuf1, KwRunStat.ttRxSize);
757                                  }
758                                     KwRunStat.ttRxSize = 0;
759                                     KwRunStat.ttRxRecved = 0;                                                                 
760                             
761                             // 暂时流程,全部发回去。
762 //                            memcpy (KwRunStat.ttTxBuf1, KwRunStat.ttRxBuf1, KwRunStat.ttRxSize);
763 //                            KwRunStat.sizetosend = KwRunStat.ttRxSize;
764 //                            KwRunStat.sentsize = 0;
765 //                            KwRunStat.sizesending = 0;
766 //                            KwRunStat.ttRxRecved = 0;
767 //                            KwRunStat.ttRxSize = 0; 
768                             
769                         }
770
771                     }
772                     
773                 
774                 }
775         KWLSlaveSendRplyPkt(1);
776             //    Radio.StartCad();
777 //                KMem.WFY[0]=(RX_Buffer[4]<<8) + RX_Buffer[5];
778 //                KMem.WFY[1]=(RX_Buffer[6]<<8) + RX_Buffer[7];                
779       }
780       else
781       {
782                 KwStartRecv(1);
783 //        Radio.Rx( SLAVE_RX_TIMEOUT_VALUE ); 
784 //                KwRunStat.runStep=RS_RECVING;
785 //                KwRunStat.lastActTime = GetTick();                
786 //                KwRunStat.lastRecvtime = GetTick();
787                 
788       }       
789     return 0;
790 }
791
792 int KWLMasterSendReqPkt(int nChn)
793 {
794     int len1=12;
795     pstKwPacket p1 = (pstKwPacket) KwTx_Buffer;
796     p1->STSign = enReqSign;
797     p1->DstAddr = MixAddr(WLCfg.nChannel,WLCfg.nRadioAddr);
798     p1->Func = 0x3;
799     KwRunStat.nSeq = (KwRunStat.nSeq + 1)&0x03;
800     p1->Stat = KwRunStat.nSeq;   //0x00;
801     p1->Data[0]=KMem.WFY[1]; 
802     p1->Data[1]=KMem.WFY[1]>>8; 
803     p1->Data[2]=KMem.WFY[2]; 
804     p1->Data[3]=KMem.WFY[2]>>8; 
805     
806
807     if (p1->Stat == 0)
808     {
809             KwTx_Buffer[10] = RssiValue;
810             KwTx_Buffer[11] = SnrValue;
811     }else if (p1->Stat == 1){
812         memcpy(KwTx_Buffer+8,&KwRunStat.sentCount,4);
813     }else if (p1->Stat == 2) {
814         memcpy(KwTx_Buffer+8,&KwRunStat.recvCount,4);        
815     }else if (p1->Stat == 3) {
816         
817     }
818 // 受限于空中包大小的限制, 透传数据可能需要分几次发送。
819 // 发送方负责拆包, 接收方负责 合包,  发送方发送第一个包,有起始标志, 发送方发送最后一个包,有结束标志。
820 // 如果只有一个包的数据,则同时具有起始和结束标志。
821 // 中间的数据包,不具有起始标志,也不具有结束标志。
822 // 利用包序列号来保证 中间 的数据包的 完整性 和顺序正确性。
823 // 先实现1个包的转发。    
824             if (KwRunStat.sizetosend > 0) {
825                 uchar bStart=0,bEnd=1;
826                 int16_t thisToSend = KwRunStat.sizetosend - KwRunStat.sentsize;
827                 if (thisToSend > WL_TT_EACH_SIZE) { thisToSend = WL_TT_EACH_SIZE; bEnd=0;}
828                 
829                 if (KwRunStat.sentsize == 0) {        // 第一次发送
830                     bStart = 1;
831                 }
832                 
833                 memcpy(KwRunStat.ttAirBuf, KwRunStat.ttTxBuf1 + KwRunStat.sentsize , thisToSend);            // 发送数据, 如果上次有,继续上次的发。
834                 
835                 KwRunStat.sizesending = thisToSend;
836                 KwRunStat.ttAirSize = thisToSend;
837
838 //                KwRunStat.sentsize = 0;
839                 KwRunStat.bttAirHeader = bStart;
840                 KwRunStat.bttAirTail = bEnd;
841                 
842                 p1->bHasData=1;
843                 p1->bHead = bStart;
844                 p1->bTail = bEnd;
845                 
846                 KwTx_Buffer[len1] = KwRunStat.sizetosend;
847                 KwTx_Buffer[len1+1] = thisToSend;
848                 
849                 memcpy( KwTx_Buffer + len1 + 2 , KwRunStat.ttTxBuf1 + KwRunStat.sentsize, thisToSend);
850                 len1 += 2 + thisToSend;
851                 
852             }
853             
854
855             crc_value=RadioComputeCRC(KwTx_Buffer,len1,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
856             KwTx_Buffer[len1]=crc_value;
857             KwTx_Buffer[len1+1]=crc_value>>8;
858             
859             
860             KMem.WDT[56]=crc_value;
861
862             KwRunStat.sentCount++;
863             KwRunStat.cycleTime = GetTick()- KwRunStat.lastSendtime ;    
864             KwRunStat.lastSendtime = GetTick();    
865             Radio.Send( KwTx_Buffer, len1+2);
866             KwRunStat.runStep=RS_SENDING;
867             KwRunStat.lastActTime = GetTick();
868     
869     return 0;
870 }
871
872 int KWLSlaveSendRplyPkt(int nChn)
873 {
874     int len1=12;
875     pstKwPacket p1 = (pstKwPacket) KwTx_Buffer;
876     p1->STSign = enRplySign;
877     p1->DstAddr = MixAddr(WLCfg.nChannel,WLCfg.nRadioAddr);
878     p1->Func = 0x3;
879     p1->Stat = (p1->Stat + 1) &0x03;   //0x00;
880     p1->Data[0]=KMem.WFY[1]; 
881     p1->Data[1]=KMem.WFY[1]>>8; 
882     p1->Data[2]=KMem.WFY[2]; 
883     p1->Data[3]=KMem.WFY[2]>>8; 
884
885     if (p1->Stat == 0)
886     {
887             KwTx_Buffer[10] = RssiValue;
888             KwTx_Buffer[11] = SnrValue;
889     }else if (p1->Stat == 1){
890         memcpy(KwTx_Buffer+8,&KwRunStat.sentCount,4);
891     }else if (p1->Stat == 2) {
892         memcpy(KwTx_Buffer+8,&KwRunStat.recvCount,4);        
893     }else if (p1->Stat == 3) {
894         
895     }    
896
897 // 受限于空中包大小的限制, 透传数据可能需要分几次发送。
898 // 发送方负责拆包, 接收方负责 合包,  发送方发送第一个包,有起始标志, 发送方发送最后一个包,有结束标志。
899 // 如果只有一个包的数据,则同时具有起始和结束标志。
900 // 中间的数据包,不具有起始标志,也不具有结束标志。
901 // 利用包序列号来保证 中间 的数据包的 完整性 和顺序正确性。
902 // 先实现1个包的转发。    
903     
904             if (KwRunStat.sizetosend > 0) {
905                 uchar bStart=0,bEnd=1;
906                 int16_t thisToSend = KwRunStat.sizetosend - KwRunStat.sentsize;
907                 if (thisToSend > WL_TT_EACH_SIZE) { thisToSend = WL_TT_EACH_SIZE; bEnd=0;}
908                 
909                 if (KwRunStat.sentsize == 0) {        // 第一次发送
910                     bStart = 1;
911                 }
912                 
913                 memcpy(KwRunStat.ttAirBuf, KwRunStat.ttTxBuf1 + KwRunStat.sentsize , thisToSend);            // 发送数据, 如果上次有,继续上次的发。
914                 
915                 KwRunStat.sizesending = thisToSend;
916                 KwRunStat.ttAirSize = thisToSend;
917
918 //                KwRunStat.sentsize = 0;
919                 KwRunStat.bttAirHeader = bStart;
920                 KwRunStat.bttAirTail = bEnd;
921                 
922                 p1->bHasData=1;
923                 p1->bHead = bStart;
924                 p1->bTail = bEnd;
925                 
926                 KwTx_Buffer[len1] = KwRunStat.sizetosend;
927                 KwTx_Buffer[len1+1] = thisToSend;
928                 
929                 memcpy( KwTx_Buffer + len1 + 2 , KwRunStat.ttTxBuf1 + KwRunStat.sentsize, thisToSend);
930                 len1 += 2 + thisToSend;
931                 
932             }
933     
934             crc_value=RadioComputeCRC(KwTx_Buffer,len1,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
935             KwTx_Buffer[len1]=crc_value;
936             KwTx_Buffer[len1+1]=crc_value>>8;
937             Radio.Send( KwTx_Buffer, len1+2);    
938             KwRunStat.runStep=RS_SENDING;
939     
940             KwRunStat.lastActTime = GetTick();
941             KwRunStat.sentCount++;
942             KwRunStat.lastSendtime = GetTick();        
943     return 0;
944 }
945
946
947 int KWL_Process(int nChn)
948 {
949     RadioStatus_t rs = SX126xGetStatus();
950     KMem.WDT[1] = rs.Value;
951
952     RadioError_t  er = SX126xGetDeviceErrors();
953     KMem.WDT[2] = er.Value;
954
955     KMem.WDT[3] = SX126xGetIrqStatus();
956
957     KMem.WDT[4] = SX126xGetRssiInst();
958     
959     KMem.WDT[5] = GetRadioBusyPin();
960     
961         RadioState_t stat = Radio.GetStatus();
962     
963         KMem.WDT[32]=stat;
964 //        KMem.WDT[38]=Radio.Rssi(MODEM_FSK);
965         if (WLCfg.bMaster){
966
967             KWLMasterProc(nChn);
968         }else            //slave
969         {
970                 KWLSlaveProc(nChn);
971         }
972     if (KwRunStat.RunStat) KwRunStat.RunStat--;
973     if (KwRunStat.ErrStat) KwRunStat.ErrStat--;
974                 
975         
976     return 0;
977 }
978
979 int KWLMasterProc(int nChn)
980 {
981             RadioState_t stat = Radio.GetStatus();
982     
983             switch (KwRunStat.runStep){
984                 case RS_IDLE:
985                     if (GetTick() - KwRunStat.lastSendtime > (WLCfg.nCycleTime) *10 ||KwRunStat.sizetosend > 0 ){
986                         Radio.StartCad();
987                         KwRunStat.runStep = RS_MASTER_CAD;
988                         KwRunStat.lastActTime = GetTick();
989                     }                
990                     break;
991                 case RS_MASTER_CAD:
992                     if (GetTick() - KwRunStat.lastActTime > 15) {
993                         KwRunStat.CADTimeOut++;                        
994                         KwRunStat.runStep = RS_IDLE;            
995                         Radio.Standby();
996                         KMem.WDT[17]++;
997                         KwResetRf();
998                     }
999                     break;
1000                 case RS_SENDING:
1001                     break;
1002                 case RS_SENT:
1003                     break;
1004                 case RS_RECVING:
1005                     if (GetTick() - KwRunStat.lastSendtime > (WLCfg.nCycleTime) *10 || KwRunStat.bMasterRecved){
1006                         KwRunStat.ErrStat=5000; 
1007                         KwRunStat.LostPackets++;
1008                         KwRunStat.CtnLstPkts++;
1009                         
1010                         KwRunStat.Tx_Power+=6;                //丢包, 增加发射功率
1011                         if (KwRunStat.Tx_Power > WLCfg.Tx_Power) {KwRunStat.Tx_Power= WLCfg.Tx_Power; }
1012                         SX126xSetRfTxPower(KwRunStat.Tx_Power);
1013                                 
1014                         if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
1015                 //        KwRunStat.ErrStat=500;
1016                         if (KwRunStat.CtnLstPkts > 1) {        KwRunStat.ErrStat=2000;}
1017                             if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;}
1018                         if (KwRunStat.CtnLstPkts > 3) {        KwRunStat.ErrStat=5000;}
1019                             if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
1020                         if (KwRunStat.CtnLstPkts > 6) {    KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
1021                             if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}                            
1022                         if ((KwRunStat.CtnLstPkts &0x0f) == 0x0f) {
1023                                 KMem.WDT[51]++;
1024                             
1025                                 KwResetRf();
1026                             //    LoadKwConfig();
1027                             //    KWireLessInit(RadioEnableMaster,nRadioChannel);
1028                             //    Radio.Standby();
1029                             //    KWireLessStart();
1030                         }                                                    
1031                         
1032                         
1033                         KwRunStat.runStep = RS_IDLE;            
1034                     }
1035                     
1036                     break;
1037                 case RS_RECVED:
1038                         KwRunStat.runStep = RS_IDLE;                    
1039                     break;
1040                 
1041                 default:
1042                     break;
1043             
1044             }
1045 /*            
1046             if (GetTick() - KwRunStat.lastSendtime > (WLCfg.nCycleTime) *10){
1047                 if (!KwRunStat.bMasterRecved) {
1048                         KwRunStat.ErrStat=5000; 
1049                         KwRunStat.LostPackets++;
1050                         KwRunStat.CtnLstPkts++;
1051                         if (KwRunStat.CtnLstPkts > KwRunStat.MaxCtnLstPkts) {KwRunStat.MaxCtnLstPkts = KwRunStat.CtnLstPkts;}
1052                 //        KwRunStat.ErrStat=500;
1053                         if (KwRunStat.CtnLstPkts > 1) {        KwRunStat.ErrStat=2000;}
1054                             if (KwRunStat.CtnLstPkts == 2) {KwRunStat.Err1Count++;}
1055                         if (KwRunStat.CtnLstPkts > 3) {        KwRunStat.ErrStat=5000;}
1056                             if (KwRunStat.CtnLstPkts == 4) {KwRunStat.Err2Count++;}
1057                         if (KwRunStat.CtnLstPkts > 6) {    KwRunStat.ErrStat=5000; KMem.WFX[1]=0; }
1058                             if (KwRunStat.CtnLstPkts == 7) {KwRunStat.Err3Count++;}                            
1059                     if ((KwRunStat.CtnLstPkts &0x0f) == 0x0f) {
1060                             KMem.WDT[51]++;
1061                         //    LoadKwConfig();
1062                         //    KWireLessInit(RadioEnableMaster,nRadioChannel);
1063                         //    Radio.Standby();
1064                         //    KWireLessStart();
1065                     }                            
1066                 }
1067             }
1068 */    
1069 /*                
1070                 if (KwRunStat.curStat == 0 ){ //&& stat == RF_IDLE )    //200mS
1071 //                    Radio.Standby();
1072                     KWireLessStart();
1073                 }else {
1074                     Radio.Standby();
1075                     KWireLessStart();
1076                 }
1077 */                    
1078     return 0;
1079 }
1080
1081 int KWLSlaveProc(int nChn)
1082 {
1083             RadioState_t stat = Radio.GetStatus();
1084             if (stat == RF_IDLE){
1085                     KMem.WDT[48]++;
1086                     Radio.Standby();
1087                     KWireLessStart();
1088             }
1089                 if (stat == RF_RX_RUNNING ){
1090                     if (GetTick() - KwRunStat.lastRecvtime > ((SLAVE_RX_TIMEOUT_VALUE + 10 )*10)){
1091                         KwRunStat.lastRecvtime = GetTick();
1092                         KwRunStat.StepErr1++;
1093                         KMem.WDT[49]++;
1094                         Radio.Standby();
1095                         KWireLessStart();
1096                     }
1097                 }
1098                 if (stat == RF_CAD && GetTick() - KwRunStat.lastRecvtime > ((2 )*10)){
1099                         KwRunStat.StepErr2++;
1100                         KwRunStat.CADTimeOut++;
1101                         KMem.WDT[50]++;
1102                         Radio.Standby();
1103                         KWireLessStart();
1104                 }
1105                 if (GetTick() - KwRunStat.lastRecvdtime > 4000){    // 200mS
1106             //            KwRunStat.ErrStat=500;
1107                         KwRunStat.ErrStat=500; 
1108 //                        Radio.Standby();
1109 //                        KWireLessStart();
1110                 }
1111                 if (GetTick() - KwRunStat.lastRecvdtime > 12000){    // 1200mS
1112                         KwRunStat.ErrStat=5000; KMem.WFX[1]=0; 
1113                 }
1114                 if (GetTick() - KwRunStat.lastRecvtime > 16000){
1115                         KMem.WDT[52]++;
1116                         KwResetRf();
1117                         Radio.Standby();
1118                         KWireLessStart();
1119                     KwRunStat.lastRecvtime = GetTick();
1120                 }                        
1121     return 0;
1122 }
1123
1124
1125
1126
1127