QuakeGod
2022-10-17 6a62617e99d79314d1e10995fbc29fbb00419a6e
提交 | 用户 | age
6a6261 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
35 #define RF_FREQUENCY                                433100000 // Hz
36
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
63 #define TX_OUTPUT_POWER                             22        // 22 dBm
64
65 extern bool IrqFired;
66
67 bool RadioEnableMaster=true;//主从选择
68
69 uint16_t  crc_value;
70 /*!
71  * Radio events function pointer
72  */
73
74 static RadioEvents_t RadioEvents;
75
76 #if defined( USE_MODEM_LORA )
77
78 #define LORA_BANDWIDTH                              0         // [0: 125 kHz,    
79                                                               //  1: 250 kHz,
80                                                               //  2: 500 kHz,
81                                                               //  3: Reserved]
82 #define LORA_SPREADING_FACTOR                       9         // [SF7..SF12]    
83 #define LORA_CODINGRATE                             4         // [1: 4/5,       
84                                                               //  2: 4/6,
85                                                               //  3: 4/7,
86                                                               //  4: 4/8]
87 #define LORA_PREAMBLE_LENGTH                        8         // Same for Tx and Rx
88 #define LORA_SYMBOL_TIMEOUT                         0         // Symbols
89 #define LORA_FIX_LENGTH_PAYLOAD_ON                  false
90 #define LORA_IQ_INVERSION_ON                        false
91
92 #elif defined( USE_MODEM_FSK )
93
94 #define FSK_FDEV                                    12e3      // Hz 
95 #define FSK_DATARATE                                19.2e3      // bps
96 #define FSK_BANDWIDTH                               50e3     // Hz >> DSB in sx126x
97 #define FSK_AFC_BANDWIDTH                           200e3     // Hz
98 #define FSK_PREAMBLE_LENGTH                         5         // Same for Tx and Rx
99 #define FSK_FIX_LENGTH_PAYLOAD_ON                   false
100
101 #else
102     #error "Please define a modem in the compiler options."
103 #endif
104
105 typedef enum
106 {
107     LOWPOWER,
108     RX,
109     RX_TIMEOUT,
110     RX_ERROR,
111     TX,
112     TX_TIMEOUT,
113 }States_t;
114
115 #define RX_TIMEOUT_VALUE                            100
116 #define BUFFER_SIZE                                 32 // Define the payload size here
117
118 const uint8_t PingMsg[] = "PING";
119 const uint8_t PongMsg[] = "PONG";
120
121 uint16_t BufferSize = BUFFER_SIZE;
122 uint8_t TX_Buffer[BUFFER_SIZE];
123 uint8_t RX_Buffer[BUFFER_SIZE];
124
125
126 States_t State = LOWPOWER;
127
128 int8_t RssiValue = 0;
129 int8_t SnrValue = 0;
130
131 KWStates KW_State=KW_PON;
132
133 void LedToggle(void)
134 {
135 //  GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_RESET);//LED闪烁
136 //  HAL_Delay_nMS(10);
137 //  GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_SET);
138     
139         LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_13);        
140 }
141
142 int KWireLessInit(bool bRadioEnableMaster)
143 {
144     
145         RadioEnableMaster = bRadioEnableMaster;
146     // Radio initialization
147     RadioEvents.TxDone = OnTxDone;
148     RadioEvents.RxDone = OnRxDone;
149     RadioEvents.TxTimeout = OnTxTimeout;
150     RadioEvents.RxTimeout = OnRxTimeout;
151     RadioEvents.RxError = OnRxError;
152     
153     
154     Radio.Init( &RadioEvents );
155     Radio.SetChannel( RF_FREQUENCY );
156     
157   //  Radio.WriteBuffer(0x06C0,data,2);
158    // Radio.ReadBuffer(0x06C0,test,2);
159     
160 #if defined( USE_MODEM_LORA )
161     
162     Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
163                                    LORA_SPREADING_FACTOR, LORA_CODINGRATE,
164                                    LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
165                                    true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
166     
167     Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
168                                    LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
169                                    LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
170                                    0, true, 0, 0, LORA_IQ_INVERSION_ON, false );
171     
172 #elif defined( USE_MODEM_FSK )
173     
174     Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
175                                   FSK_DATARATE, 0,
176                                   FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
177                                   true, 0, 0, 0, 3000 );
178     
179     Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
180                                   0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
181                                   0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, true,
182                                   0, 0,false, false );
183 #else
184     #error "Please define a frequency band in the compiler options."
185 #endif
186         SX126xSetRxTxFallbackMode(0x40); // 0x40-> FS    0x30 -> SD_XOSC  0x20 -> SD_RC
187     return 0;
188 }
189 int KWireLessStart(void)
190 {
191     if(RadioEnableMaster)
192     {
193             
194             SendPingMsg();
195         /*    
196           TX_Buffer[0] = 'P';
197           TX_Buffer[1] = 'I';
198           TX_Buffer[2] = 'N';
199           TX_Buffer[3] = 'G'; 
200           TX_Buffer[4] = 'G'; 
201           TX_Buffer[5] = 'G'; 
202           TX_Buffer[6] = 'G'; 
203           TX_Buffer[7] = 'G'; 
204           
205             
206             
207           crc_value=RadioComputeCRC(TX_Buffer,8,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
208           TX_Buffer[8]=crc_value>>8;
209           TX_Buffer[9]=crc_value;
210           Radio.Send( TX_Buffer, 10);
211             */
212     }
213     else
214     {
215        Radio.Rx( RX_TIMEOUT_VALUE ); 
216     }
217     
218 //    while( 1 )
219  //   {
220  //       Radio.IrqProcess( ); // Process Radio IRQ
221 //    }
222     return 0;
223 }
224
225 void SendPingMsg()
226 {
227     int len1=12;
228             TX_Buffer[0] = 'P';
229             TX_Buffer[1] = 'I';
230             TX_Buffer[2] = 'N';
231             TX_Buffer[3] = 'G'; 
232             TX_Buffer[4] = KMem.WY[1]>>8; 
233             TX_Buffer[5] = KMem.WY[1]; 
234             TX_Buffer[6] = KMem.WY[2]>>8; 
235             TX_Buffer[7] = KMem.WY[2]; 
236             
237             crc_value=RadioComputeCRC(TX_Buffer,len1,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
238             TX_Buffer[len1]=crc_value>>8;
239             TX_Buffer[len1+1]=crc_value;
240             Radio.Send( TX_Buffer, len1+2);
241 }
242 void SendPongMsg(int len1)
243 {
244             TX_Buffer[0] = 'P';
245             TX_Buffer[1] = 'O';
246             TX_Buffer[2] = 'N';
247             TX_Buffer[3] = 'G'; 
248             TX_Buffer[4] = KMem.WX[0]>>8; 
249             TX_Buffer[5] = KMem.WX[0]; 
250             TX_Buffer[6] = KMem.WX[1]>>8; 
251             TX_Buffer[7] = KMem.WX[1]; 
252             
253             crc_value=RadioComputeCRC(TX_Buffer,len1,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
254             TX_Buffer[len1]=crc_value>>8;
255             TX_Buffer[len1+1]=crc_value;
256             Radio.Send( TX_Buffer, len1+2);    
257     
258 }
259 void OnTxDone( void )
260 {   
261     Radio.Standby();
262     Radio.Rx( RX_TIMEOUT_VALUE ); //进入接收
263         LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);
264             int us2=GetuS();
265             KMem.ScanTimeuS=us2-KMem.LastScanTime;
266         KMem.LastScanTime = us2;
267         if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
268         if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
269         
270 }
271
272 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
273 {
274     BufferSize = size;
275     memcpy( RX_Buffer, payload, BufferSize );
276     RssiValue = rssi;
277     SnrValue = snr;
278     KMem.DT[40]=rssi; //Radio.Rssi(MODEM_FSK);;
279         KMem.DT[41]=snr;
280         KMem.DT[42]++;
281         
282     Radio.Standby();
283     
284     if(RadioEnableMaster)
285     {
286       if(memcmp(RX_Buffer,PongMsg,4)==0)
287       {
288         LedToggle();//LED闪烁
289                     KMem.WX[1]=(RX_Buffer[4]<<8) + RX_Buffer[5];
290                     KMem.WX[2]=(RX_Buffer[6]<<8) + RX_Buffer[7];                
291       }
292             SendPingMsg();
293     }
294     else
295     {
296       if(memcmp(RX_Buffer,PingMsg,4)==0)
297       {
298         LedToggle();//LED闪烁
299         SendPongMsg(size-2);
300                 KMem.WY[0]=(RX_Buffer[4]<<8) + RX_Buffer[5];
301                 KMem.WY[1]=(RX_Buffer[6]<<8) + RX_Buffer[7];                
302       }
303       else
304       {
305         Radio.Rx( RX_TIMEOUT_VALUE ); 
306       }   
307     }
308 }
309
310 void OnTxTimeout( void )
311 {
312    
313 }
314
315 void OnRxTimeout( void )
316 {
317     Radio.Standby();
318             KMem.DT[43]++;
319         KMem.ErrStat=200;
320     if(RadioEnableMaster)
321     {
322             SendPingMsg();
323     }
324     else
325     {
326       Radio.Rx( RX_TIMEOUT_VALUE ); 
327     }
328 }
329
330 void OnRxError( void )
331 {
332
333     Radio.Standby();
334             KMem.DT[44]++;
335     if(RadioEnableMaster)
336     {
337             SendPingMsg();
338     }
339     else
340     {
341       Radio.Rx( RX_TIMEOUT_VALUE ); 
342     }
343   
344 }