提交 | 用户 | age
|
483170
|
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 |
Demo 程序流程 RadioEnableMaster=true 为主机端,主机端发送一个"PING"数据后切换到接收,等待从机返回的应答"PONG"数据LED闪烁 |
|
22 |
|
|
23 |
RadioEnableMaster=false 为从机端,从机端接收到主机端发过来的"PING"数据后LED闪烁并发送一个"PONG"数据作为应答 |
|
24 |
***************************************************************************************************************************************/ |
|
25 |
typedef enum tag_KWStates |
|
26 |
{ |
|
27 |
KW_PON, |
|
28 |
KW_UNINIT, |
|
29 |
KW_INITED, |
|
30 |
KW_UNCONFIGED, |
|
31 |
KW_CONFIGING, |
|
32 |
KW_CONFIGED, |
|
33 |
KW_READY, |
|
34 |
KW_STARTING, |
|
35 |
KW_OPERATION, |
|
36 |
KW_ERROR1, |
|
37 |
KW_ERROR2, |
|
38 |
KW_ERROR3, |
|
39 |
}KWStates; |
|
40 |
|
842bb6
|
41 |
#define STORE_KWCONFIG_BASE (FLASH_BASE + 0x0000F000) //50k K //and |
Q |
42 |
//#define STORE_RUNSTAT_PAGESIZE (0x00000400) //Page Size = 1K |
|
43 |
#define STORE_KWCONFIG_PAGES 1 //use 1 pages |
|
44 |
|
|
45 |
enum enWLWorkMode |
|
46 |
{ |
|
47 |
WLWorkModeNone, // |
|
48 |
WLWorkModeUni, // 一对一 |
|
49 |
WLWorkModeMul, // 一对多 |
|
50 |
WLWorkModeThr, // 透传 |
|
51 |
|
|
52 |
}; |
|
53 |
|
|
54 |
typedef struct tagWLConfig |
|
55 |
{ |
|
56 |
uint32_t RF_T_Freq; //Hz |
|
57 |
uint32_t RF_R_Freq; //Hz |
|
58 |
uint16_t nChnSpacing; //kHz |
7d8ba5
|
59 |
uint16_t nCycleTime; // |
842bb6
|
60 |
uchar workMode; //0: FSK, 1: LoRa |
7d8ba5
|
61 |
uchar nChannel; |
Q |
62 |
uchar bMaster; |
|
63 |
uchar nRadioAddr; |
|
64 |
uchar bEnableMulti; |
|
65 |
|
842bb6
|
66 |
uchar Tx_Power; // dBm 5 - 22 dBm |
Q |
67 |
uchar LoraBandWidth; // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ] |
|
68 |
uchar LoRaFactor; // [SF5 .. SF 12] |
|
69 |
uchar LoRaCodingRate; // [1 : 4/5, 2: 4/6, 3: 4/7, 4: 4/8 ] |
|
70 |
uint8_t NetWorkAddr; |
|
71 |
uint16_t DeviceAddr; |
|
72 |
uchar bEnableAddr; |
|
73 |
uchar bEnableEncrypt; |
|
74 |
uchar bEnableRelay; |
|
75 |
uchar LoRaPreambleLen; // 2 - 12 |
7d8ba5
|
76 |
uchar bAutoPower; //自动功率 |
842bb6
|
77 |
uchar bAutoReSend; //自动重发 |
Q |
78 |
|
|
79 |
// uchar ; // |
|
80 |
// uchar |
|
81 |
|
|
82 |
}stWLConfig, *pstWLConfig; |
|
83 |
|
|
84 |
typedef struct tagStoredWLConfig |
|
85 |
{ |
|
86 |
unsigned short BlockSign; |
|
87 |
unsigned char BlockType; |
|
88 |
unsigned char nSeq; |
|
89 |
unsigned short nSize; |
|
90 |
unsigned short nCRC16; |
|
91 |
stWLConfig WLConfig; |
|
92 |
}stStoredWLConfig, *pstStoredWLConfig; |
|
93 |
|
|
94 |
|
483170
|
95 |
typedef enum tag_runstep{ |
7d8ba5
|
96 |
RS_IDLE, |
Q |
97 |
RS_MASTER_CAD, |
483170
|
98 |
RS_SENDING, |
7d8ba5
|
99 |
RS_SENT, |
483170
|
100 |
RS_RECVING, |
7d8ba5
|
101 |
RS_RECVED, |
483170
|
102 |
}enRunStep; |
8b51c7
|
103 |
|
842bb6
|
104 |
#pragma anon_unions |
1fb934
|
105 |
|
7eb19e
|
106 |
typedef struct tagKwStatByte |
Q |
107 |
{ |
|
108 |
uint8_t nSeq :2; |
|
109 |
uint8_t Err1 :1; |
|
110 |
uint8_t Err2 :1; |
|
111 |
uint8_t bHasData :1; |
|
112 |
uint8_t bHeader :1; |
|
113 |
uint8_t bTail :1; |
|
114 |
|
|
115 |
|
|
116 |
}stKwStatByte; |
|
117 |
|
1fb934
|
118 |
typedef struct tagKwChnStat |
Q |
119 |
{ |
|
120 |
uint32_t DeviceAddr; |
7eb19e
|
121 |
uint8_t Stat; // 状态字节 |
Q |
122 |
uint8_t nSeq; // 包序列号 |
1fb934
|
123 |
uint8_t Tx_Power; |
Q |
124 |
uint8_t RSSI; |
|
125 |
uint8_t SNR; |
|
126 |
uint8_t tRSSI; |
|
127 |
uint8_t tSNR; |
|
128 |
uint32_t SendCount; |
|
129 |
uint32_t RecvCount; |
|
130 |
// uint32_t SendTime; |
|
131 |
// uint32_t RecvTime; |
|
132 |
uint16_t TxErr; |
|
133 |
uint16_t RxErr; |
|
134 |
|
|
135 |
uint16_t LostCount; |
|
136 |
uint16_t CtnLstPkts; |
|
137 |
uint16_t MaxCtnLstPkts; |
|
138 |
|
7eb19e
|
139 |
uchar * pTxBuffer; |
Q |
140 |
uint16_t sizetosend; |
|
141 |
uint16_t sentsize; |
|
142 |
|
|
143 |
uchar * pRxBuffer; // and buffer max size; |
|
144 |
// uint16_t SizeToRecv; |
|
145 |
uint16_t RecvedSize; |
|
146 |
|
1fb934
|
147 |
|
Q |
148 |
}stKwChnStat; |
|
149 |
|
|
150 |
|
483170
|
151 |
typedef struct tagWLStat |
Q |
152 |
{ |
842bb6
|
153 |
union { |
Q |
154 |
uint32_t Status; //状态 |
|
155 |
struct { |
|
156 |
uint32_t bMasterSent:1; |
|
157 |
uint32_t bMasterRecved:1; |
|
158 |
}; |
|
159 |
}; |
|
160 |
uint16_t curStat; //当前状态 |
|
161 |
uint16_t runStep; //工作步骤 |
|
162 |
|
|
163 |
uint16_t RunStat; //运行状态 |
|
164 |
uint16_t ErrStat; //错误状态 |
|
165 |
|
|
166 |
uint32_t RF_Freq; //运行频率 |
7eb19e
|
167 |
|
842bb6
|
168 |
uint16_t nTimeOnAir; |
Q |
169 |
uint16_t DeviceAddr; |
7eb19e
|
170 |
uint8_t nSeq; |
842bb6
|
171 |
uint8_t NetWorkAddr; |
Q |
172 |
|
|
173 |
uchar Tx_Power; // dBm 5 - 22 dBm |
|
174 |
uchar LoraBandWidth; // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved ] |
|
175 |
uchar LoRaFactor; // [SF5 .. SF 12] |
|
176 |
uchar LoRaCodingRate; // [1 : 4/5, 2: 4/6, 3: 4/7, 4: 4/8 ] |
|
177 |
|
|
178 |
uchar LoRaPreambleLen; // 2 - 12 |
|
179 |
|
|
180 |
uchar bEnableAddr; |
|
181 |
uchar bEnableEncrypt; |
|
182 |
uchar bEnableRelay; |
|
183 |
uchar bAutoReSend; //自动重发 |
|
184 |
|
483170
|
185 |
uint32_t sentCount; //发送计数 |
Q |
186 |
uint32_t recvCount; //接收计数 |
|
187 |
uint32_t lastSendtime; //上次发送时间 |
eaf5d5
|
188 |
uint32_t lastSenttime; //上次发完时间 |
483170
|
189 |
uint32_t lastRecvtime; //上次启动接收时间 |
eaf5d5
|
190 |
uint32_t lastRecvdtime; //上次收到时间 |
483170
|
191 |
uint32_t lastActTime; //上次动作时间 |
Q |
192 |
uint32_t lastAckTime; //上次应答时间 |
eaf5d5
|
193 |
uint32_t lastErrTime; //上次错误时间 |
483170
|
194 |
uint32_t latancy; //延迟 |
Q |
195 |
uint32_t cycleTime; //循环时间 |
7d8ba5
|
196 |
|
842bb6
|
197 |
uint16_t LostPackets; //丢包计数 |
Q |
198 |
uint16_t CtnLstPkts; //连续丢包计数 |
|
199 |
uint16_t MaxCtnLstPkts; //最大连续丢包计数 |
|
200 |
uint16_t TXErr; //发送错误计数 |
|
201 |
uint16_t RXErr; //接收错误计数 |
|
202 |
uint16_t CRCErr; //CRC错误计数 |
7d8ba5
|
203 |
uint16_t PktErr; //包错误 ; |
Q |
204 |
uint16_t ChnErr; //频道错误; |
|
205 |
uint16_t nErrChn; //错误的频道号; |
842bb6
|
206 |
uint16_t CADDoneCount; //CAD 完成次数 |
7d8ba5
|
207 |
uint16_t CADOkCount; //CAD 是次数 |
Q |
208 |
uint16_t CADNgCount; //CAD 否次数 |
|
209 |
uint16_t CADTimeOut; //CAD 超时次数 |
842bb6
|
210 |
uint16_t StepErr1; //步骤错误1 |
Q |
211 |
uint16_t StepErr2; //步骤错误2 |
|
212 |
uint16_t Err1Count; //微闪报警次数 |
|
213 |
uint16_t Err2Count; //大闪报警次数 |
|
214 |
uint16_t Err3Count; //严重丢失信号次数 |
eaf5d5
|
215 |
|
483170
|
216 |
int8_t RSSI; //信号强度 |
Q |
217 |
int8_t SNR; //信噪比 |
|
218 |
int8_t tRSSI; //对方信号强度 |
|
219 |
int8_t tSNR; //对方信噪比 |
|
220 |
|
eaf5d5
|
221 |
uint32_t targetSentCount; //对方发送数量 |
Q |
222 |
uint32_t targetRecvdCount; //对方接受数量 |
|
223 |
|
7eb19e
|
224 |
unsigned char ttTxBuf1[256]; |
Q |
225 |
uint16_t sizetosend; |
|
226 |
uint16_t sentsize; |
|
227 |
uint16_t sizesending; |
|
228 |
|
|
229 |
unsigned char ttRxBuf1[256]; |
|
230 |
uint16_t ttRxSize; |
|
231 |
unsigned char ttRxRecved; |
|
232 |
|
|
233 |
unsigned char ttAirBuf[65]; |
|
234 |
uint16_t ttAirSize; |
|
235 |
unsigned char bttAirHeader; |
|
236 |
unsigned char bttAirTail; |
|
237 |
|
|
238 |
unsigned char bttAirData; |
|
239 |
|
eaf5d5
|
240 |
}stWLRunStat,*pstWLRunStat; |
7eb19e
|
241 |
|
Q |
242 |
enum enKwCMDs |
|
243 |
{ |
|
244 |
|
|
245 |
KwcmdNone = 0x00, //Nothing |
|
246 |
|
|
247 |
KwcmdQuery = 0x01, //Query basic Info |
|
248 |
KwcmdQueryRply = 0x81, //Query Info Reply |
|
249 |
|
|
250 |
KwcmdSetCfg = 0x02, // Set Configuration |
|
251 |
KwcmdSetCfgRply = 0x82, // Set Configuration Response |
|
252 |
|
|
253 |
KwcmdToRunMode = 0x03, |
|
254 |
KwcmdToRunModeRply = 0x83, |
|
255 |
|
|
256 |
KwcmdBroadCastCfg = 0x04, // |
|
257 |
|
|
258 |
KwcmdMuExchgData = 0x05, // |
|
259 |
KwcmdMuExchgDataRply = 0x85, // |
|
260 |
|
|
261 |
KwcmdToSafeMode = 0x06, |
|
262 |
|
|
263 |
KwcmdHeartBeat = 0x07, // |
|
264 |
KwcmdReHeartBeat = 0x87, // |
|
265 |
}; |
|
266 |
|
483170
|
267 |
|
eaf5d5
|
268 |
enum { |
Q |
269 |
enReqSign = 0x55, |
|
270 |
enRplySign = 0xAA, |
|
271 |
|
|
272 |
}; |
|
273 |
|
7eb19e
|
274 |
typedef struct tagKwPktStatByte |
eaf5d5
|
275 |
{ |
7eb19e
|
276 |
uchar nSeq :2 ; |
Q |
277 |
uchar bErr1 :1 ; |
|
278 |
uchar bErr2 :1 ; |
|
279 |
uchar bHasData :1; |
|
280 |
uchar bHead :1; |
|
281 |
uchar bTail :1; |
eaf5d5
|
282 |
|
7eb19e
|
283 |
|
Q |
284 |
}stKwPktStatByte; |
|
285 |
|
|
286 |
typedef struct tagKwPacket |
|
287 |
{ |
|
288 |
uchar STSign; // 包开始标记 |
|
289 |
uchar DstAddr; // 子机组合地址, 包括 频道 和 子机地址 |
|
290 |
uchar Func; // 功能码 |
|
291 |
union{ |
|
292 |
uchar Stat; // 状态字 |
|
293 |
stKwPktStatByte; |
|
294 |
}; |
|
295 |
uchar Data[1]; // 数据 |
|
296 |
|
|
297 |
}stKwPacket, *pstKwPacket; |
483170
|
298 |
|
842bb6
|
299 |
int LoadKwConfig(void); |
Q |
300 |
int SaveKwConfig(void); |
8b51c7
|
301 |
|
483170
|
302 |
int KWireLessInit(bool bRadioEnableMaster, uint32_t nChn); |
Q |
303 |
int KWireLessStart(void); |
|
304 |
|
|
305 |
void OnTxDone( void ); |
|
306 |
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); |
|
307 |
void OnTxTimeout( void ); |
|
308 |
void OnRxTimeout( void ); |
|
309 |
void OnRxError( void ); |
|
310 |
void OnCadDone( bool channelActivityDetected); |
|
311 |
|
|
312 |
|
|
313 |
void LedToggle(void); |
|
314 |
|
|
315 |
|
|
316 |
int KWL_Process(int nChn); |
|
317 |
|
|
318 |
int KWLMasterProc(int nChn); |
|
319 |
int KWLSlaveProc(int nChn); |
|
320 |
|
eaf5d5
|
321 |
int KWLMasterParsePkt(int nChn, int nSize); |
Q |
322 |
int KWLSlaveParsePkt(int nChn, int nSize); |
483170
|
323 |
|
Q |
324 |
int KWLMasterSendReqPkt(int nChn); |
|
325 |
int KWLSlaveSendRplyPkt(int nChn); |
|
326 |
|
|
327 |
|
|
328 |
int KWMasterProc(void); |
|
329 |
int KWSlaveProc(void); |
|
330 |
int MkKwPkg(void* pPkg, int len); |
|
331 |
|
|
332 |
int KWSendPkg(void* pPkg, int len); |
|
333 |
int KWProcPkg(void); |
8b51c7
|
334 |
|
7d8ba5
|
335 |
//extern uchar nRadioChannel; |
Q |
336 |
//extern uchar nRadioAddr; |
8b51c7
|
337 |
|
842bb6
|
338 |
extern stWLConfig WLCfg ; |
8b51c7
|
339 |
extern stWLRunStat KwRunStat; |
Q |
340 |
|
|
341 |
|
483170
|
342 |
#endif ///* __KBUS_H__ */ |
Q |
343 |
|