提交 | 用户 | age
|
9aed5d
|
1 |
/** |
Q |
2 |
****************************************************************************** |
|
3 |
* @file : KWireLess.h |
|
4 |
* @brief : Header for KWireLess.c file. |
|
5 |
* This file contains the common defines of the application. |
|
6 |
****************************************************************************** |
|
7 |
*/ |
|
8 |
#include "KMachine.h" |
|
9 |
#include <stdint.h> |
|
10 |
#include "user.h" |
|
11 |
|
|
12 |
#include "radio/inc/sx126x-board.h" |
|
13 |
|
|
14 |
|
|
15 |
#ifndef __KWIRELESS_H__ |
|
16 |
#define __KWIRELESS_H__ |
|
17 |
typedef unsigned char uchar; |
|
18 |
|
|
19 |
|
|
20 |
/************************************************************************************************************************************** |
|
21 |
开机后,进入Pon状态。 |
|
22 |
主机自动依次进入 Ready状态。 |
|
23 |
监听指定频道上的通讯,如果有占用/干扰,则进入错误1(频道被占用)状态。 频道有干扰 |
|
24 |
|
|
25 |
如果持续1秒没有收到任何通讯。则占用频道。 |
|
26 |
占用频道后,周期性广播宣布频道的所有权,同时等待是否有其他主机要进入频道。 |
|
27 |
|
|
28 |
错误1状态,如果频道上的主机支持多主模式,则在适当时刻,作为次要主机进入频道,与主机轮流使用令牌。 |
|
29 |
|
|
30 |
|
|
31 |
************************************************************************************ |
|
32 |
子机自动进入UnConfiged状态。 |
|
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 |
IOCTRL, 无线信号强度。 |
|
58 |
|
|
59 |
|
|
60 |
协议层与底层无线通讯的接口 |
|
61 |
设置频率,功率,编码模式等。 |
|
62 |
查询工作和状态。 |
|
63 |
设置接收模式。接收到状态。 |
|
64 |
设置发送模式,发送完状态。 |
|
65 |
数据包收,数据包发。 |
|
66 |
|
|
67 |
|
|
68 |
IOCTRL, 无线信道信号强度 |
|
69 |
|
|
70 |
|
|
71 |
***************************************************************************************************************************************/ |
|
72 |
|
|
73 |
typedef enum tag_KWStates // 主状态机 状态 |
|
74 |
{ |
|
75 |
// KW_PON, //开机 |
|
76 |
KW_UNINIT, //未初始化 |
|
77 |
KW_INITED, //已初始化 |
|
78 |
KW_UNCONFIGED, //未配置 |
|
79 |
KW_CONFIGING, //配置中 |
|
80 |
KW_CONFIGED, //已配置 |
|
81 |
KW_READY, //就绪 |
|
82 |
KW_PREOP, //预启动 |
|
83 |
// KW_STARTING, //启动中 |
|
84 |
KW_OPERATIONAL, //操作 |
|
85 |
KW_STOPPED, //停止 |
|
86 |
KW_ERROR1, //错误1 |
|
87 |
// KW_ERROR2, //错误2 |
|
88 |
// KW_ERROR3, //错误3 |
|
89 |
}emKWStates; |
|
90 |
|
|
91 |
#define STORE_KWCONFIG_BASE (FLASH_BASE + 0x0000F000) //50k K //and |
|
92 |
//#define STORE_RUNSTAT_PAGESIZE (0x00000400) //Page Size = 1K |
|
93 |
#define STORE_KWCONFIG_PAGES 1 //use 1 pages |
|
94 |
|
|
95 |
|
|
96 |
enum enWLWorkMode |
|
97 |
{ |
|
98 |
WLWorkModeNone, // |
|
99 |
WLWorkModeUni, // 一对一 |
|
100 |
WLWorkModeThr, // 透传 |
|
101 |
WLWorkModeMulti1, // 一对多,轮询 |
|
102 |
WLWorkModeMulti2, // 一对多,令牌 |
|
103 |
WLWorkModeMulMa, // 多主 |
|
104 |
}; |
|
105 |
|
|
106 |
enum { |
|
107 |
enReqSign = 0x55, |
|
108 |
enRplySign = 0xAA, |
|
109 |
}; |
|
110 |
|
|
111 |
enum enKwCMDs |
|
112 |
{ |
|
113 |
KwcmdNone = 0x00, //Nothing |
|
114 |
|
|
115 |
KwcmdQuery = 0x01, //Query basic Info |
|
116 |
KwcmdQueryRply = 0x81, //Query Info Reply |
|
117 |
|
|
118 |
KwcmdSetCfg = 0x02, // Set Configuration |
|
119 |
KwcmdSetCfgRply = 0x82, // Set Configuration Response |
|
120 |
|
|
121 |
KwcmdToRunMode = 0x03, |
|
122 |
KwcmdToRunModeRply = 0x83, |
|
123 |
|
|
124 |
KwcmdBroadCastCfg = 0x04, // |
|
125 |
|
|
126 |
KwcmdMuExchgData = 0x05, // |
|
127 |
KwcmdMuExchgDataRply = 0x85, // |
|
128 |
|
|
129 |
KwcmdToSafeMode = 0x06, |
|
130 |
|
|
131 |
KwcmdHeartBeat = 0x07, // |
|
132 |
KwcmdReHeartBeat = 0x87, // |
|
133 |
}; |
|
134 |
|
|
135 |
typedef struct tagWLConfig |
|
136 |
{ |
|
137 |
uint32_t RF_T_Freq; //Hz |
|
138 |
uint32_t RF_R_Freq; //Hz |
|
139 |
uint16_t nChnSpacing; //kHz |
|
140 |
uint16_t nCycleTime; // |
|
141 |
// uchar ModemType; // 0: FSK, 1: LoRa |
|
142 |
uchar workMode; // 0, None 1, Uni, 2 Thr, 3 Multi1, 4 Multi2, 5 MulMa |
|
143 |
uchar nChannel; |
|
144 |
uchar bMaster; |
|
145 |
uchar nRadioAddr; |
|
146 |
uchar bEnableMulti; |
|
147 |
|
|
148 |
uchar Tx_Power; // dBm 5 - 22 dBm |
|
149 |
uchar LoraBandWidth; // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ] |
|
150 |
uchar LoRaFactor; // [SF5 .. SF 12] |
|
151 |
uchar LoRaCodingRate; // [1 : 4/5, 2: 4/6, 3: 4/7, 4: 4/8 ] |
|
152 |
uint8_t NetWorkAddr; |
|
153 |
uint16_t DeviceAddr; |
|
154 |
uchar bEnableAddr; |
|
155 |
uchar bEnableEncrypt; |
|
156 |
uchar bEnableRelay; |
|
157 |
uchar LoRaPreambleLen; // 2 - 12 |
|
158 |
uchar bAutoPower; //自动功率 |
|
159 |
uchar bAutoReSend; //自动重发 |
|
160 |
|
|
161 |
// uchar ; // |
|
162 |
// uchar |
|
163 |
|
|
164 |
}stWLConfig, *pstWLConfig; |
|
165 |
|
|
166 |
typedef struct tagKwInitStruct |
|
167 |
{ |
|
168 |
uchar nWorkMode; |
|
169 |
uchar bMaster; |
|
170 |
uchar nChannel; |
|
171 |
uchar nRadioAddr; |
|
172 |
uchar bEnableMultiClient; |
|
173 |
uchar bEnableMultiMaster; |
|
174 |
|
|
175 |
|
|
176 |
}stKwInitStruct,*pstKwInitStruct; |
|
177 |
|
|
178 |
typedef struct tagStoredWLConfig |
|
179 |
{ |
|
180 |
unsigned short BlockSign; |
|
181 |
unsigned char BlockType; |
|
182 |
unsigned char nSeq; |
|
183 |
unsigned short nSize; |
|
184 |
unsigned short nCRC16; |
|
185 |
stWLConfig WLConfig; |
|
186 |
}stStoredWLConfig, *pstStoredWLConfig; |
|
187 |
|
|
188 |
|
|
189 |
typedef enum tag_runstep{ |
|
190 |
RS_IDLE, |
|
191 |
RS_MASTER_CAD, |
|
192 |
RS_SENDING, |
|
193 |
RS_SENT, |
|
194 |
RS_RECVING, |
|
195 |
RS_RECVED, |
|
196 |
}enRunStep; |
|
197 |
|
|
198 |
#pragma anon_unions |
|
199 |
|
|
200 |
typedef struct tagKwStatByte |
|
201 |
{ |
|
202 |
uint8_t nSeq :2; |
|
203 |
uint8_t Err1 :1; |
|
204 |
uint8_t Err2 :1; |
|
205 |
uint8_t bHasData :1; |
|
206 |
uint8_t bHeader :1; |
|
207 |
uint8_t bTail :1; |
|
208 |
|
|
209 |
|
|
210 |
}stKwStatByte; |
|
211 |
|
|
212 |
typedef struct tagKwChnStat |
|
213 |
{ |
|
214 |
uint32_t DeviceAddr; |
|
215 |
uint8_t Stat; // 状态字节 |
|
216 |
uint8_t nSeq; // 包序列号 |
|
217 |
uint8_t Tx_Power; |
|
218 |
uint8_t RSSI; |
|
219 |
uint8_t SNR; |
|
220 |
uint8_t tRSSI; |
|
221 |
uint8_t tSNR; |
|
222 |
uint32_t SendCount; |
|
223 |
uint32_t RecvCount; |
|
224 |
// uint32_t SendTime; |
|
225 |
// uint32_t RecvTime; |
|
226 |
uint16_t TxErr; |
|
227 |
uint16_t RxErr; |
|
228 |
|
|
229 |
uint16_t LostCount; |
|
230 |
uint16_t CtnLstPkts; |
|
231 |
uint16_t MaxCtnLstPkts; |
|
232 |
|
|
233 |
uchar * pTxBuffer; |
|
234 |
uint16_t sizetosend; |
|
235 |
uint16_t sentsize; |
|
236 |
|
|
237 |
uchar * pRxBuffer; // and buffer max size; |
|
238 |
// uint16_t SizeToRecv; |
|
239 |
uint16_t RecvedSize; |
|
240 |
|
|
241 |
|
|
242 |
}stKwChnStat; |
|
243 |
|
|
244 |
typedef struct tagClientStat |
|
245 |
{ |
|
246 |
uint32_t sentCount; //发送计数 |
|
247 |
uint32_t recvCount; //接收计数 |
|
248 |
uint32_t lastSendtime; //上次发送时间 |
|
249 |
uint32_t lastSenttime; //上次发完时间 |
|
250 |
uint32_t lastRecvtime; //上次启动接收时间 |
|
251 |
uint32_t lastRecvdtime; //上次收到时间 |
|
252 |
uint32_t lastActTime; //上次动作时间 |
|
253 |
uint32_t lastAckTime; //上次应答时间 |
|
254 |
uint32_t lastErrTime; //上次错误时间 |
|
255 |
uint32_t latancy; //延迟 |
|
256 |
uint32_t cycleTime; //循环时间 |
|
257 |
|
|
258 |
uint16_t LostPackets; //丢包计数 |
|
259 |
uint16_t CtnLstPkts; //连续丢包计数 |
|
260 |
uint16_t MaxCtnLstPkts; //最大连续丢包计数 |
|
261 |
uint16_t TXErr; //发送错误计数 |
|
262 |
uint16_t RXErr; //接收错误计数 |
|
263 |
uint16_t CRCErr; //CRC错误计数 |
|
264 |
uint16_t PktErr; //包错误 ; |
|
265 |
uint16_t ChnErr; //频道错误; |
|
266 |
uint16_t nErrChn; //错误的频道号; |
|
267 |
uint16_t CADDoneCount; //CAD 完成次数 |
|
268 |
uint16_t CADOkCount; //CAD 是次数 |
|
269 |
uint16_t CADNgCount; //CAD 否次数 |
|
270 |
uint16_t CADTimeOut; //CAD 超时次数 |
|
271 |
uint16_t StepErr1; //步骤错误1 |
|
272 |
uint16_t StepErr2; //步骤错误2 |
|
273 |
uint16_t Err1Count; //微闪报警次数 |
|
274 |
uint16_t Err2Count; //大闪报警次数 |
|
275 |
uint16_t Err3Count; //严重丢失信号次数 |
|
276 |
|
|
277 |
int8_t RSSI; //信号强度 |
|
278 |
int8_t SNR; //信噪比 |
|
279 |
int8_t tRSSI; //对方信号强度 |
|
280 |
int8_t tSNR; //对方信噪比 |
|
281 |
|
|
282 |
uint32_t targetSentCount; //对方发送数量 |
|
283 |
uint32_t targetRecvdCount; //对方接受数量 |
|
284 |
|
|
285 |
}stClientStat; |
|
286 |
|
|
287 |
typedef struct tagWLStat |
|
288 |
{ |
|
289 |
union { |
|
290 |
uint32_t Status; //状态 |
|
291 |
struct { |
|
292 |
uint32_t bMasterSent:1; |
|
293 |
uint32_t bMasterRecved:1; |
|
294 |
}; |
|
295 |
}; |
|
296 |
uint16_t curStat; //当前状态 |
|
297 |
uint16_t runStep; //工作步骤 |
|
298 |
|
|
299 |
uint16_t RunStat; //运行状态 |
|
300 |
uint16_t ErrStat; //错误状态 |
|
301 |
|
|
302 |
uint32_t RF_Freq; //运行频率 |
|
303 |
|
|
304 |
uint16_t nTimeOnAir; |
|
305 |
uint16_t DeviceAddr; |
|
306 |
uint8_t nSeq; |
|
307 |
uint8_t NetWorkAddr; |
|
308 |
|
|
309 |
uchar Tx_Power; // dBm 5 - 22 dBm |
|
310 |
uchar LoraBandWidth; // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ] |
|
311 |
uchar LoRaFactor; // [SF5 .. SF 12] |
|
312 |
uchar LoRaCodingRate; // [1 : 4/5, 2: 4/6, 3: 4/7, 4: 4/8 ] |
|
313 |
|
|
314 |
uchar LoRaPreambleLen; // 2 - 12 |
|
315 |
|
|
316 |
uchar bEnableAddr; |
|
317 |
uchar bEnableEncrypt; |
|
318 |
uchar bEnableRelay; |
|
319 |
uchar bAutoReSend; //自动重发 |
|
320 |
uint8_t nCurClient; |
|
321 |
|
|
322 |
stClientStat ClientStat[8]; |
|
323 |
|
|
324 |
unsigned char ttTxBuf1[256]; |
|
325 |
uint16_t sizetosend; |
|
326 |
uint16_t sentsize; |
|
327 |
uint16_t sizesending; |
|
328 |
|
|
329 |
unsigned char ttRxBuf1[256]; |
|
330 |
uint16_t ttRxSize; |
|
331 |
unsigned char ttRxRecved; |
|
332 |
|
|
333 |
unsigned char ttAirBuf[65]; |
|
334 |
uint16_t ttAirSize; |
|
335 |
unsigned char bttAirHeader; |
|
336 |
unsigned char bttAirTail; |
|
337 |
|
|
338 |
unsigned char bttAirData; |
|
339 |
|
|
340 |
}stWLRunStat,*pstWLRunStat; |
|
341 |
|
|
342 |
typedef struct tagKwPktStatByte |
|
343 |
{ |
|
344 |
uchar nSeq :2 ; |
|
345 |
uchar bErr1 :1 ; |
|
346 |
uchar bErr2 :1 ; |
|
347 |
uchar bHasData :1; |
|
348 |
uchar bHead :1; |
|
349 |
uchar bTail :1; |
|
350 |
}stKwPktStatByte; |
|
351 |
|
|
352 |
typedef struct tagKwPacket |
|
353 |
{ |
|
354 |
uchar STSign; // 包开始标记,包类型 |
|
355 |
uchar DstAddr; // 子机组合地址, 包括 频道 和 子机地址 |
|
356 |
uchar Func; // 功能码 |
|
357 |
union{ |
|
358 |
uchar Stat; // 状态字 |
|
359 |
stKwPktStatByte; |
|
360 |
}; |
|
361 |
uchar Data[1]; // 数据 |
|
362 |
|
|
363 |
}stKwPacket, *pstKwPacket; |
|
364 |
|
|
365 |
|
|
366 |
typedef struct tagKwMultiPkt // Kw协议 多机通讯包 |
|
367 |
{ |
|
368 |
uchar STSign1; // 包开始标记包括包类型 |
|
369 |
uchar DstAddr; // 目的组合地址,包括 频道? 和 子机地址 |
|
370 |
uchar SrcAddr; // 源地址,可能为主机或子机。 主机源地址为0 |
|
371 |
uchar nStat; // 状态码 |
|
372 |
uchar Func; // 功能码 |
|
373 |
uchar Data[1]; |
|
374 |
|
|
375 |
}stKwMultiPkt, *pstKwMultiPkt; |
|
376 |
|
|
377 |
typedef struct tagKwMasterCastPkt{ |
|
378 |
uchar STSign1; |
|
379 |
uchar DstAddr; |
|
380 |
uchar SrcAddr; |
|
381 |
uchar nStat; |
|
382 |
uchar Func; |
|
383 |
uchar Data[1]; |
|
384 |
|
|
385 |
}stKwMasterCastPkt, *pstKwMasterCastPkt; |
|
386 |
|
|
387 |
|
|
388 |
int LoadKwConfig(void); |
|
389 |
int SaveKwConfig(void); |
|
390 |
|
|
391 |
int KWireLessInit(bool bRadioEnableMaster, uint32_t nChannel, uint8_t nRadioAddr); |
|
392 |
int KWireLessStart(void); |
|
393 |
|
|
394 |
void OnTxDone( void ); |
|
395 |
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); |
|
396 |
void OnTxTimeout( void ); |
|
397 |
void OnRxTimeout( void ); |
|
398 |
void OnRxError( void ); |
|
399 |
void OnCadDone( bool channelActivityDetected); |
|
400 |
|
|
401 |
|
|
402 |
void LedToggle(void); |
|
403 |
|
|
404 |
|
|
405 |
int KWL_Process(int nChn); |
|
406 |
|
|
407 |
int KWLMasterProc(int nChn); |
|
408 |
int KWLSlaveProc(int nChn); |
|
409 |
|
|
410 |
int KWLMasterParsePkt(int nChn, int nSize); |
|
411 |
int KWLSlaveParsePkt(int nChn, int nSize); |
|
412 |
|
|
413 |
int KWLMasterSendReqPkt(int nChn); |
|
414 |
int KWLSlaveSendRplyPkt(int nChn); |
|
415 |
|
|
416 |
|
|
417 |
int KWMasterProc(void); |
|
418 |
int KWSlaveProc(void); |
|
419 |
int MkKwPkg(void* pPkg, int len); |
|
420 |
|
|
421 |
int KWSendPkg(void* pPkg, int len); |
|
422 |
int KWProcPkg(void); |
|
423 |
|
|
424 |
//extern uchar nRadioChannel; |
|
425 |
//extern uchar nRadioAddr; |
|
426 |
|
|
427 |
extern stWLConfig WLCfg ; |
|
428 |
extern stWLRunStat KwRunStat; |
|
429 |
|
|
430 |
|
|
431 |
//int Kw2Init(stKwInitStruct * pInstance, Kw2Struct * InitStruct); |
|
432 |
//int Kw2Stop(); |
|
433 |
//int Kw2SendThrouPkg; |
|
434 |
//int Kw2RecvThrouPkgCallBack; |
|
435 |
//int Kw2Start(); |
|
436 |
//int Kw2GetStat(); |
|
437 |
//int Kw2GetError(); |
|
438 |
//int |
|
439 |
|
|
440 |
#endif ///* __KBUS_H__ */ |
|
441 |
|