QuakeGod
2022-10-17 6a62617e99d79314d1e10995fbc29fbb00419a6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
  ******************************************************************************
  * @file           : KWireLess.c
  * @brief          : K-WireLess Protocol program body
  ******************************************************************************
    */
#include "KWireLess.h"
#include "functions.h"
#include "string.h"
#include "PLCFunctions.h"
#include "stm32f0xx_hal.h"
 
 
/**************************************************************************************************************************************
Demo ³ÌÐòÁ÷³Ì  RadioEnableMaster=true  ÎªÖ÷»ú¶Ë£¬Ö÷»ú¶Ë·¢ËÍÒ»¸ö"PING"Êý¾ÝºóÇл»µ½½ÓÊÕ£¬µÈ´ý´Ó»ú·µ»ØµÄÓ¦´ð"PONG"Êý¾ÝLEDÉÁ˸
 
               RadioEnableMaster=false Îª´Ó»ú¶Ë£¬´Ó»ú¶Ë½ÓÊÕµ½Ö÷»ú¶Ë·¢¹ýÀ´µÄ"PING"Êý¾ÝºóLEDÉÁ˸²¢·¢ËÍÒ»¸ö"PONG"Êý¾Ý×÷ΪӦ´ð
***************************************************************************************************************************************/
 
//#define USE_MODEM_LORA
#define USE_MODEM_FSK
 
#define REGION_CN779
 
#if defined( REGION_AS923 )
 
#define RF_FREQUENCY                                923000000 // Hz
 
#elif defined( REGION_AU915 )
 
#define RF_FREQUENCY                                915000000 // Hz
 
#elif defined( REGION_CN779 )
 
#define RF_FREQUENCY                                433100000 // Hz
 
#elif defined( REGION_EU868 )
 
#define RF_FREQUENCY                                868000000 // Hz
 
#elif defined( REGION_KR920 )
 
#define RF_FREQUENCY                                920000000 // Hz
 
#elif defined( REGION_IN865 )
 
#define RF_FREQUENCY                                865000000 // Hz
 
#elif defined( REGION_US915 )
 
#define RF_FREQUENCY                                915000000 // Hz
 
#elif defined( REGION_US915_HYBRID )
 
#define RF_FREQUENCY                                915000000 // Hz
 
#else
 
    #error "Please define a frequency band in the compiler options."
 
#endif
 
#define TX_OUTPUT_POWER                             22        // 22 dBm
 
extern bool IrqFired;
 
bool RadioEnableMaster=true;//Ö÷´ÓÑ¡Ôñ
 
uint16_t  crc_value;
/*!
 * Radio events function pointer
 */
 
static RadioEvents_t RadioEvents;
 
#if defined( USE_MODEM_LORA )
 
#define LORA_BANDWIDTH                              0         // [0: 125 kHz,    
                                                              //  1: 250 kHz,
                                                              //  2: 500 kHz,
                                                              //  3: Reserved]
#define LORA_SPREADING_FACTOR                       9         // [SF7..SF12]    
#define LORA_CODINGRATE                             4         // [1: 4/5,       
                                                              //  2: 4/6,
                                                              //  3: 4/7,
                                                              //  4: 4/8]
#define LORA_PREAMBLE_LENGTH                        8         // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT                         0         // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON                  false
#define LORA_IQ_INVERSION_ON                        false
 
#elif defined( USE_MODEM_FSK )
 
#define FSK_FDEV                                    12e3      // Hz 
#define FSK_DATARATE                                19.2e3      // bps
#define FSK_BANDWIDTH                               50e3     // Hz >> DSB in sx126x
#define FSK_AFC_BANDWIDTH                           200e3     // Hz
#define FSK_PREAMBLE_LENGTH                         5         // Same for Tx and Rx
#define FSK_FIX_LENGTH_PAYLOAD_ON                   false
 
#else
    #error "Please define a modem in the compiler options."
#endif
 
typedef enum
{
    LOWPOWER,
    RX,
    RX_TIMEOUT,
    RX_ERROR,
    TX,
    TX_TIMEOUT,
}States_t;
 
#define RX_TIMEOUT_VALUE                            100
#define BUFFER_SIZE                                 32 // Define the payload size here
 
const uint8_t PingMsg[] = "PING";
const uint8_t PongMsg[] = "PONG";
 
uint16_t BufferSize = BUFFER_SIZE;
uint8_t TX_Buffer[BUFFER_SIZE];
uint8_t RX_Buffer[BUFFER_SIZE];
 
 
States_t State = LOWPOWER;
 
int8_t RssiValue = 0;
int8_t SnrValue = 0;
 
KWStates KW_State=KW_PON;
 
void LedToggle(void)
{
//  GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_RESET);//LEDÉÁ˸
//  HAL_Delay_nMS(10);
//  GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_SET);
    
        LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_13);        
}
 
int KWireLessInit(bool bRadioEnableMaster)
{
    
        RadioEnableMaster = bRadioEnableMaster;
    // Radio initialization
    RadioEvents.TxDone = OnTxDone;
    RadioEvents.RxDone = OnRxDone;
    RadioEvents.TxTimeout = OnTxTimeout;
    RadioEvents.RxTimeout = OnRxTimeout;
    RadioEvents.RxError = OnRxError;
    
    
    Radio.Init( &RadioEvents );
    Radio.SetChannel( RF_FREQUENCY );
    
  //  Radio.WriteBuffer(0x06C0,data,2);
   // Radio.ReadBuffer(0x06C0,test,2);
    
#if defined( USE_MODEM_LORA )
    
    Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
                                   LORA_SPREADING_FACTOR, LORA_CODINGRATE,
                                   LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
                                   true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
    
    Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
                                   LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
                                   LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
                                   0, true, 0, 0, LORA_IQ_INVERSION_ON, false );
    
#elif defined( USE_MODEM_FSK )
    
    Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
                                  FSK_DATARATE, 0,
                                  FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
                                  true, 0, 0, 0, 3000 );
    
    Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
                                  0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
                                  0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, true,
                                  0, 0,false, false );
#else
    #error "Please define a frequency band in the compiler options."
#endif
        SX126xSetRxTxFallbackMode(0x40); // 0x40-> FS    0x30 -> SD_XOSC  0x20 -> SD_RC
    return 0;
}
int KWireLessStart(void)
{
    if(RadioEnableMaster)
    {
            
            SendPingMsg();
        /*    
          TX_Buffer[0] = 'P';
          TX_Buffer[1] = 'I';
          TX_Buffer[2] = 'N';
          TX_Buffer[3] = 'G'; 
          TX_Buffer[4] = 'G'; 
          TX_Buffer[5] = 'G'; 
          TX_Buffer[6] = 'G'; 
          TX_Buffer[7] = 'G'; 
          
            
            
          crc_value=RadioComputeCRC(TX_Buffer,8,CRC_TYPE_IBM);//¼ÆËãµÃ³öÒª·¢ËÍÊý¾Ý°üCRCÖµ
          TX_Buffer[8]=crc_value>>8;
          TX_Buffer[9]=crc_value;
          Radio.Send( TX_Buffer, 10);
            */
    }
    else
    {
       Radio.Rx( RX_TIMEOUT_VALUE ); 
    }
    
//    while( 1 )
 //   {
 //       Radio.IrqProcess( ); // Process Radio IRQ
//    }
    return 0;
}
 
void SendPingMsg()
{
    int len1=12;
            TX_Buffer[0] = 'P';
            TX_Buffer[1] = 'I';
            TX_Buffer[2] = 'N';
            TX_Buffer[3] = 'G'; 
            TX_Buffer[4] = KMem.WY[1]>>8; 
            TX_Buffer[5] = KMem.WY[1]; 
            TX_Buffer[6] = KMem.WY[2]>>8; 
            TX_Buffer[7] = KMem.WY[2]; 
            
            crc_value=RadioComputeCRC(TX_Buffer,len1,CRC_TYPE_IBM);//¼ÆËãµÃ³öÒª·¢ËÍÊý¾Ý°üCRCÖµ
            TX_Buffer[len1]=crc_value>>8;
            TX_Buffer[len1+1]=crc_value;
            Radio.Send( TX_Buffer, len1+2);
}
void SendPongMsg(int len1)
{
            TX_Buffer[0] = 'P';
            TX_Buffer[1] = 'O';
            TX_Buffer[2] = 'N';
            TX_Buffer[3] = 'G'; 
            TX_Buffer[4] = KMem.WX[0]>>8; 
            TX_Buffer[5] = KMem.WX[0]; 
            TX_Buffer[6] = KMem.WX[1]>>8; 
            TX_Buffer[7] = KMem.WX[1]; 
            
            crc_value=RadioComputeCRC(TX_Buffer,len1,CRC_TYPE_IBM);//¼ÆËãµÃ³öÒª·¢ËÍÊý¾Ý°üCRCÖµ
            TX_Buffer[len1]=crc_value>>8;
            TX_Buffer[len1+1]=crc_value;
            Radio.Send( TX_Buffer, len1+2);    
    
}
void OnTxDone( void )
{   
    Radio.Standby();
    Radio.Rx( RX_TIMEOUT_VALUE ); //½øÈë½ÓÊÕ
        LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);
            int us2=GetuS();
            KMem.ScanTimeuS=us2-KMem.LastScanTime;
        KMem.LastScanTime = us2;
        if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
        if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
        
}
 
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
    BufferSize = size;
    memcpy( RX_Buffer, payload, BufferSize );
    RssiValue = rssi;
    SnrValue = snr;
    KMem.DT[40]=rssi; //Radio.Rssi(MODEM_FSK);;
        KMem.DT[41]=snr;
        KMem.DT[42]++;
        
    Radio.Standby();
    
    if(RadioEnableMaster)
    {
      if(memcmp(RX_Buffer,PongMsg,4)==0)
      {
        LedToggle();//LEDÉÁ˸
                    KMem.WX[1]=(RX_Buffer[4]<<8) + RX_Buffer[5];
                    KMem.WX[2]=(RX_Buffer[6]<<8) + RX_Buffer[7];                
      }
            SendPingMsg();
    }
    else
    {
      if(memcmp(RX_Buffer,PingMsg,4)==0)
      {
        LedToggle();//LEDÉÁ˸
        SendPongMsg(size-2);
                KMem.WY[0]=(RX_Buffer[4]<<8) + RX_Buffer[5];
                KMem.WY[1]=(RX_Buffer[6]<<8) + RX_Buffer[7];                
      }
      else
      {
        Radio.Rx( RX_TIMEOUT_VALUE ); 
      }   
    }
}
 
void OnTxTimeout( void )
{
   
}
 
void OnRxTimeout( void )
{
    Radio.Standby();
            KMem.DT[43]++;
        KMem.ErrStat=200;
    if(RadioEnableMaster)
    {
            SendPingMsg();
    }
    else
    {
      Radio.Rx( RX_TIMEOUT_VALUE ); 
    }
}
 
void OnRxError( void )
{
 
    Radio.Standby();
            KMem.DT[44]++;
    if(RadioEnableMaster)
    {
            SendPingMsg();
    }
    else
    {
      Radio.Rx( RX_TIMEOUT_VALUE ); 
    }
  
}