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