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