1
WXK
2023-11-29 d3d530578cd646c83b6683439d08a80141205e46
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#include <math.h>
#include <string.h>
#include "sx126x.h"
#include "sx126x-board.h"
#include "delay.h"
#include "Lora.h"
#include "user.h"
#include "sx126x-board.h"
#include "string.h"
#include "stdio.h"
#include "delay.h"
#include "main.h"
#include <stdlib.h>
static RadioEvents_t RadioEvents;
#define USE_MODEM_LORA
//#define USE_MODEM_FSK
#define REGION_CN779 
void Lora47xrx_Init(void);
void Lora433rx_Init(void);
uint32_t RxDutyCycle_RX_time   =128;
uint32_t RxDutyCycle_SLEEP_time=256;
#define LORA_FREQUENCY0 470200000
#define LORA_FREQUENCY1 471200000
#define LORA_FREQUENCY2 472200000
#define LORA_FREQUENCY3 473200000
#define LORA_FREQUENCY4 474200000
uint16_t Lora_rece_error;
#define LORA_RX_433 1
#define LORA_TX_47X 2
#define LORA_RX_47X 3
uint8_t Lora_status=0;
#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                              433000000 // Hz
#define RF_FREQUENCY_R                              500000000 // Hz
#define RF_FREQUENCY_T                              470200000 // 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;
extern uint8_t lora_yingda_flag;
uint16_t lora_yingda_num;
 
 
//bool EnableMaster=true;//Ö÷Ñ¡Ôñ
bool EnableMaster=false;//´ÓÑ¡Ôñ
 
uint16_t  crc_value;
/*!
 * Radio events function pointer
 */
 
static RadioEvents_t RadioEvents;
 
#if defined( USE_MODEM_LORA )
 
#define LORA_BANDWIDTH                              2         // [0: 125 kHz,    
                                                              //  1: 250 kHz,
                                                              //  2: 500 kHz,
                                                              //  3: Reserved]
#define LORA_SPREADING_FACTOR                       5         // [SF7..SF12]    
#define LORA_CODINGRATE                             1         // [1: 4/5,       
                                                              //  2: 4/6,
                                                              //  3: 4/7,
                                                              //  4: 4/8]
#define LORA_PREAMBLE_LENGTH_T                      10         // Same for Tx 
#define LORA_PREAMBLE_LENGTH_R                      64        // Same for Rx
//#define LORA_PREAMBLE_LENGTH                      990         // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT                         0         // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON                  false
#define LORA_IQ_INVERSION_ON_T                      false
#define LORA_IQ_INVERSION_ON_R                      false
 
 
#elif defined( USE_MODEM_FSK )
 
#define FSK_FDEV                                    5e3      // Hz 
#define FSK_DATARATE                                2.4e3      // bps
#define FSK_BANDWIDTH                               20e3     // Hz >> DSB in sx126x
#define FSK_AFC_BANDWIDTH                           100e3     // 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                            1000
#define RX_TIMEOUT_VALUE                            6400
#define BUFFER_SIZE                                 64 // 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;
void OnTxDone( void );
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
void OnTxTimeout( void );
void OnRxTimeout( void );
void OnRxError( void );
uint8_t  lora_chongfuyingda_flag;
uint8_t Lora_rx_open_flag;
extern u32 Loratx_frequency;
void LedToggle(void)
 
{
//    HAL_GPIO_TogglePin(GPIOB, LED1_Pin);
}
void Lora_tx_mode( void )
{
Lora_status=LORA_TX_47X;
            Radio.SetChannel( Loratx_frequency );
            
            Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, 2,
            7, LORA_CODINGRATE,LORA_PREAMBLE_LENGTH_T, LORA_FIX_LENGTH_PAYLOAD_ON,
            false, 0, 0, LORA_IQ_INVERSION_ON_T, 3000 );
 
}
void Lora_rx_open( void )
{
//    if(Lora_rx_open_flag==1)
//    {
//    Lora_Init();
//    Lora_rx_open_flag=0;
//    }
//    if(lora_yingda_flag&&lora_chongfuyingda_flag==0)
//    {
//        printf("Lora:%d  %d\r\n",lora_yingda_flag,lora_chongfuyingda_flag);
//    lora_chongfuyingda_flag=1;
//    RadioEvents.TxDone = OnTxDone;
//    RadioEvents.RxDone = OnRxDone;
//    RadioEvents.TxTimeout = OnTxTimeout;
//    RadioEvents.RxTimeout = OnRxTimeout;
//    RadioEvents.RxError = OnRxError;
 
//    Radio.Init( &RadioEvents );
//    Radio.SetChannel( 500000000 );
//    Radio.SetRxConfig( MODEM_LORA, 0, 7,
//    LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH_R,
//    LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
//    0, false, 0, 0, true, 1 );
 
//    SX126xClearIrqStatus( IRQ_RADIO_ALL );
////    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
//        Radio.Rx( RX_TIMEOUT_VALUE ); 
 
//        
//    }
}
void OnTxDone( void )
{
    LED_TB_OFF;
//    if(lora_yingda_num>1000)
//    {
//    lora_yingda_num=0;
//    lora_yingda_flag=1;
//    }
    
//    if(lora_yingda_flag==0)    
//    Lora_rx_open_flag=1;
//    else
//    {}
    Radio.Standby();
    
// Lora433rx_Init();
   Lora47xrx_Init();   
}
 
extern uint32_t lp_time;
uint32_t lora_txtime_old;
extern uint8_t frame_seq_nb2,rec_nearbase_num,ancidlist_num;
uint16_t RX_TIMEOUTs;
extern LPTIM_HandleTypeDef hlptim1;
uint16_t current_time,start_time,end_time;
    uint16_t lora_recv_devid=0;
u32 id;
extern uint8_t Lora_tx_ancnum;
extern uint16_t Lora_tx_ancid[50];
extern uint16_t Lora_tx_ancdist[50];
uint8_t power_and_key=0;
extern u8 battary,button;
u8 lora_seq_nb2;
extern uint8_t stationary_flag;
extern uint8_t anjian_flag;
void Lora_tx_zubao( void )
{
    uint16_t checksum;
                    usart_send[2] = 0x22;//Õý³£Ä£Ê½ 
                    usart_send[3] = 9+Lora_tx_ancnum*4;//Êý¾Ý¶Î³¤¶È
                    memcpy(&usart_send[4],&dev_id,2);
                    usart_send[6] = lora_seq_nb2;//°üÐò
                    power_and_key=stationary_flag<<1;//°Ñ°´¼ü¸ú¾²Ö¹2ºÏ1£¬·¢³öÈ¥
                    power_and_key=power_and_key+anjian_flag;;
                    memcpy(usart_send+7,&bat_percent,1);
                    memcpy(usart_send+8,&power_and_key,1);
                    usart_send[9] = lora_yingda_flag;//loraµÈ´ýÓ¦´ðλ
                    usart_send[10] = Lora_tx_ancnum;
//                    usart_send[9] = lora_yingda_flag;
                    memcpy(&usart_send[11],&Lora_tx_ancid,2*Lora_tx_ancnum);                
                    memcpy(&usart_send[11+Lora_tx_ancnum*2],&Lora_tx_ancdist,2*Lora_tx_ancnum);
                    checksum = Checksum_u16(&usart_send[2],8+4*Lora_tx_ancnum);
                    memcpy(&usart_send[11+4*Lora_tx_ancnum],&checksum,2);
}
uint16_t current_count;
u16 Lora_recevcount;
extern uint16_t no_data_chongqi_num;
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{   
    if(Lora_status==LORA_RX_433)
    {
    Lora_recevcount=HAL_LPTIM_ReadCounter(&hlptim1);
    Lora_recevcount=Lora_recevcount+197;//197´ú±í6ms
    if(Lora_recevcount>=32768)
    {
    Lora_recevcount -=32768;
    }
//    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
    BufferSize = size;
    memcpy( RX_Buffer, payload, BufferSize );
    RssiValue = rssi;
    SnrValue = snr;
    no_data_chongqi_num=0;
    Radio.Standby();
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
//    if(lora_yingda_flag)
//    {
//    memcpy(&lora_recv_devid,&RX_Buffer[4],2);
//        if(lora_recv_devid==dev_id)
//        {
//        
//        
//        }
//    }
//    if(lora_yingda_flag==0)
    {
//        loraa_flag=0;
//        HAL_GPIO_chongfuyingd_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
//        Delay_Ms(10);
//        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);
//        uint16_t i=0;
//        for(i=0;i<BufferSize;i++)
//        {printf("R %d:\r\n",RX_Buffer[3]);}
//        RX_TIMEOUTs++;
//        printf("RX_num :%d\r\n",RX_TIMEOUTs);
//        start_time=HAL_LPTIM_ReadCounter(&hlptim1);
//        end_time=start_time+((32768/1000)*20);
//        if(end_time>=32768)
//        {end_time-=32768;}
//        current_time=HAL_LPTIM_ReadCounter(&hlptim1);
        HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_SET); //ƬѡloraÎÞЧ
 
            id =  dwt_readdevid() ;
    while (0xDECA0130!=id) 
    {
        u8 iderror_count = 0;
        id =  dwt_readdevid() ;
        if(iderror_count++>100)
        {
////////            printf("UWBоƬID´íÎó");
            break;
        }
    }
////////            printf("Anchor_App\r\n");
        current_count=HAL_LPTIM_ReadCounter(&hlptim1);
        while(current_count<Lora_recevcount||current_count>Lora_recevcount+16384)
        {
            current_count=HAL_LPTIM_ReadCounter(&hlptim1);
            if(current_count<Lora_recevcount-300)
            {
                break;
            }
        }
//    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
            Anchor_App();
//    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);            
            HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_RESET); //ƬѡloraÓÐЧ
            delay_ms(1);
//            if(lp_time-lora_txtime_old>=1)
//            {
//            LED_TB_ON;    
//            lora_txtime_old=lp_time;
//            lora_seq_nb2++;
//            lora_yingda_num++;
//            Lora_tx_zubao();
//            Lora_tx_mode();            
//            Radio.Send( usart_send, 12+Lora_tx_ancnum*4);
//            Lora_tx_ancnum=0;    
//            }
//            else
//            {
            Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
//            }
 
 
    }
    }
    if(Lora_status==LORA_RX_47X)//¡£
    {
        BufferSize = size;
        memcpy( RX_Buffer, payload, BufferSize );
        RssiValue = rssi;
        SnrValue = snr;
        Radio.Standby();
        if(RX_Buffer[0]==0x55&&RX_Buffer[1]==0xAA)
        {
            Lora_rece_error=0;
            Lora433rx_Init();
            Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
        }
    }
}
 
void OnTxTimeout( void )
{
       Radio.Standby();
        Lora433rx_Init();
    Lora_rx_open_flag=0;
    
    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
}
uint16_t Lora_rece_error;
void OnRxTimeout( void )
{
    uint8_t i;
    Radio.Standby();
    if(Lora_status==LORA_RX_47X) //470rx³¬Ê±£¬Ïȵ÷ʱ¼ä·¢ËÍ£¬5´Îºó»¹²»ÐУ¬»»Æµ¶Î
    {
        Lora_rece_error++;
        if(Lora_rece_error>5)
        {
            Loratx_frequency=Loratx_frequency+1000000;
            Lora_rece_error=0;
        }
        else
        {
           i= (rand()%100);
        SetNextPollTime(i);
        }
    }
    Lora433rx_Init();
    Lora_rx_open_flag=0;
    
    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
//    printf("slave OnRxTimeout\r\n");    
    
}
 
void OnRxError( void )
{
 
    Radio.Standby();
    Lora433rx_Init();
    Lora_rx_open_flag=0;
    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
}
 
void Lora433rx_Init(void)
{
    Lora_status=LORA_RX_433;
//    RadioEvents.TxDone = OnTxDone;
//    RadioEvents.RxDone = OnRxDone;
//    RadioEvents.TxTimeout = OnTxTimeout;
//    RadioEvents.RxTimeout = OnRxTimeout;
//    RadioEvents.RxError = OnRxError;
////    printf("Lora_Init3\r\n");
//        Radio.Init( &RadioEvents );
        Radio.SetChannel( RF_FREQUENCY );        
 
//        Radio.SetStandby( STDBY_RC );
    
//    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.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
//                                   LORA_SPREADING_FACTOR, LORA_CODINGRATE,
//                                   LORA_PREAMBLE_LENGTH_T, LORA_FIX_LENGTH_PAYLOAD_ON,
//                                   false, 0, 0, LORA_IQ_INVERSION_ON_T, 1000 );
    
//    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 );
//                    printf("Lora_Init1\r\n");                                                 
        Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
                                   LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH_R,
                                   LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
                                   0, false, 0, 0, LORA_IQ_INVERSION_ON_R, 1 );
                                   
//        Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
//                                   LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
//                                   LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
//                                   0, false, 0, 0, LORA_IQ_INVERSION_ON, true );
    
 
 
    {
       SX126xClearIrqStatus( IRQ_RADIO_ALL );
//        SX126xSetDioIrqParams( IRQ_RX_DONE | IRQ_CRC_ERROR,
// IRQ_RX_DONE | IRQ_CRC_ERROR,
// IRQ_RADIO_NONE, IRQ_RADIO_NONE
// );
//            SX126xSetDioIrqParams( IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
//       IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
//       IRQ_RADIO_NONE,
//       IRQ_RADIO_NONE );
       Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
//        Radio.Rx( RX_TIMEOUT_VALUE ); 
//       printf("I am Slave!\r\n");        
    }
       
}
 
void Lora47xrx_Init(void)
{   Lora_status=LORA_RX_47X;
//    RadioEvents.TxDone = OnTxDone;
//    RadioEvents.RxDone = OnRxDone;
//    RadioEvents.TxTimeout = OnTxTimeout;
//    RadioEvents.RxTimeout = OnRxTimeout;
//    RadioEvents.RxError = OnRxError;
 
//        Radio.Init( &RadioEvents );
        Radio.SetChannel( Loratx_frequency );        
 
                                         
        Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, 7,
                                   LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH_R,
                                   LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
                                   0, false, 0, 0, LORA_IQ_INVERSION_ON_R, 0 );
                                   
    {
       SX126xClearIrqStatus( IRQ_RADIO_ALL );
 
//       Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
        Radio.Rx( RX_TIMEOUT_VALUE ); 
//       printf("I am Slave!\r\n");        
    }
       
}
void Lora_Init(void)
{
    RadioEvents.TxDone = OnTxDone;
    RadioEvents.RxDone = OnRxDone;
    RadioEvents.TxTimeout = OnTxTimeout;
    RadioEvents.RxTimeout = OnRxTimeout;
    RadioEvents.RxError = OnRxError;
 
        Radio.Init( &RadioEvents );
}