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