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