提交 | 用户 | age
|
bfc108
|
1 |
/**
|
Q |
2 |
******************************************************************************
|
|
3 |
* @file : KBus.c
|
|
4 |
* @brief : K-Bus Protocol program body
|
|
5 |
******************************************************************************
|
|
6 |
*/
|
|
7 |
#include "KBus.h"
|
|
8 |
#include "functions.h"
|
|
9 |
#include "string.h"
|
|
10 |
|
|
11 |
int bMaster=0,bSlave=0;
|
|
12 |
|
|
13 |
unsigned char PacketBuf1[128];
|
|
14 |
unsigned char PacketBuf2[128];
|
|
15 |
|
|
16 |
unsigned char BufferIn[16]={0};
|
|
17 |
unsigned char BufferOut[16]={0};
|
|
18 |
|
|
19 |
stChnStat ChnStats[8];
|
|
20 |
int nAddr=0;
|
|
21 |
int nChilds;
|
|
22 |
int ChildStat[16];
|
|
23 |
int nCurPollId=0;
|
|
24 |
int nSeq=0;
|
|
25 |
|
|
26 |
int MasterRecved=1;
|
|
27 |
volatile int SlaveRecved=1;
|
|
28 |
unsigned int SendTimeuS=0;
|
|
29 |
volatile int RecvTimeuS=0;
|
|
30 |
|
|
31 |
int DelayuS=0;
|
|
32 |
int MaxDelayuS=0;
|
|
33 |
int ThisuS;
|
|
34 |
|
|
35 |
volatile unsigned int nSlaveTick=0;
|
|
36 |
|
|
37 |
unsigned char BCC(void * pData, int nSize)
|
|
38 |
{
|
|
39 |
unsigned char k;
|
|
40 |
k=0;
|
|
41 |
for (int i=0;i<nSize;i++)
|
|
42 |
{
|
|
43 |
k^=((unsigned char *)pData)[i];
|
|
44 |
}
|
|
45 |
return k;
|
|
46 |
}
|
|
47 |
int MakePacket(pPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nSEQ, unsigned char DataLen,void * pData )
|
|
48 |
{
|
|
49 |
p1->Sign=StartSign;
|
|
50 |
p1->DstAddr=dst;
|
|
51 |
p1->SrcAddr=src;
|
|
52 |
p1->nCMD=nType;
|
|
53 |
p1->nSEQ=nSEQ;
|
|
54 |
int PacketLenth=0;
|
|
55 |
switch (nType)
|
|
56 |
{
|
|
57 |
case cmdNone:
|
|
58 |
break;
|
|
59 |
case cmdPing:
|
|
60 |
p1->PacketLen=DataLen;
|
|
61 |
memcpy(p1->data,pData,DataLen);
|
|
62 |
p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
63 |
p1->data[DataLen+1]=EndSign;
|
|
64 |
|
|
65 |
PacketLenth=sizeof(stPacket)+DataLen+1;
|
|
66 |
break;
|
|
67 |
case cmdPingReply:
|
|
68 |
p1->PacketLen=DataLen;
|
|
69 |
memcpy(p1->data,pData,DataLen);
|
|
70 |
p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
71 |
p1->data[DataLen+1]=EndSign;
|
|
72 |
PacketLenth=sizeof(stPacket)+DataLen+1;
|
|
73 |
break;
|
|
74 |
case cmdRead:
|
|
75 |
break;
|
|
76 |
case cmdReadReply:
|
|
77 |
break;
|
|
78 |
case cmdWrite:
|
|
79 |
break;
|
|
80 |
case cmdWriteReply:
|
|
81 |
p1->PacketLen=DataLen;
|
|
82 |
if (DataLen !=0 ) memcpy(p1->data,pData,DataLen);
|
|
83 |
p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
84 |
p1->data[DataLen+1]=EndSign;
|
|
85 |
PacketLenth=sizeof(stPacket)+DataLen+1;
|
|
86 |
break;
|
|
87 |
case cmdGetVersion:
|
|
88 |
p1->PacketLen=DataLen;
|
|
89 |
memcpy(p1->data,pData,DataLen);
|
|
90 |
p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
91 |
p1->data[DataLen+1]=EndSign;
|
|
92 |
PacketLenth=sizeof(stPacket)+DataLen+1;
|
|
93 |
break;
|
|
94 |
case cmdVerInfo:
|
|
95 |
p1->PacketLen=DataLen;
|
|
96 |
memcpy(p1->data,pData,DataLen);
|
|
97 |
p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
98 |
p1->data[DataLen+1]=EndSign;
|
|
99 |
PacketLenth=sizeof(stPacket)+DataLen+1;
|
|
100 |
break;
|
|
101 |
case cmdExChgData:
|
|
102 |
p1->PacketLen=DataLen;
|
|
103 |
memcpy(p1->data,pData,DataLen);
|
|
104 |
p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
105 |
p1->data[DataLen+1]=EndSign;
|
|
106 |
PacketLenth=sizeof(stPacket)+DataLen+1;
|
|
107 |
break;
|
|
108 |
case cmdExChgDataReply:
|
|
109 |
p1->PacketLen=DataLen;
|
|
110 |
memcpy(p1->data,pData,DataLen);
|
|
111 |
p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
112 |
p1->data[DataLen+1]=EndSign;
|
|
113 |
PacketLenth=sizeof(stPacket)+DataLen+1;
|
|
114 |
break;
|
|
115 |
|
|
116 |
case cmdSyncRead:
|
|
117 |
p1->PacketLen=DataLen;
|
|
118 |
memcpy(p1->data,pData,DataLen);
|
|
119 |
p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
120 |
p1->data[DataLen+1]=EndSign;
|
|
121 |
PacketLenth=sizeof(stPacket)+DataLen+1;
|
|
122 |
break;
|
|
123 |
case cmdSyncWrite:
|
|
124 |
p1->PacketLen=DataLen;
|
|
125 |
memcpy(p1->data,pData,DataLen);
|
|
126 |
p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
127 |
p1->data[DataLen+1]=EndSign;
|
|
128 |
PacketLenth=sizeof(stPacket)+DataLen+1;
|
|
129 |
break;
|
|
130 |
case cmdSequenRead:
|
|
131 |
p1->PacketLen=DataLen;
|
|
132 |
memcpy(p1->data,pData,DataLen);
|
|
133 |
p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
134 |
p1->data[DataLen+1]=EndSign;
|
|
135 |
PacketLenth=sizeof(stPacket)+DataLen+1;
|
|
136 |
break;
|
|
137 |
|
|
138 |
case cmdSyncTime:
|
|
139 |
p1->PacketLen=DataLen;
|
|
140 |
memcpy(p1->data,pData,DataLen);
|
|
141 |
p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
142 |
p1->data[DataLen+1]=EndSign;
|
|
143 |
PacketLenth=sizeof(stPacket)+DataLen+1;
|
|
144 |
break;
|
|
145 |
|
|
146 |
default:
|
|
147 |
break;
|
|
148 |
}
|
|
149 |
|
|
150 |
return PacketLenth;
|
|
151 |
}
|
|
152 |
|
|
153 |
int CheckPacket(pPacket p1,int nLen1)
|
|
154 |
{
|
|
155 |
if (p1->Sign != StartSign)
|
|
156 |
{
|
|
157 |
Uart2Stat.NotPacketErr++;
|
|
158 |
ChnStats[nCurPollId].NotPkgErr++;
|
|
159 |
return -1;
|
|
160 |
}
|
|
161 |
int DataLen=p1->PacketLen;
|
|
162 |
if (DataLen>MaxPacketLength)
|
|
163 |
{
|
|
164 |
Uart2Stat.LengthErr++;
|
|
165 |
ChnStats[nCurPollId].PkgLenErr++;
|
|
166 |
return -1;
|
|
167 |
}
|
|
168 |
if (nLen1<DataLen+sizeof(stPacket)+1)
|
|
169 |
{
|
|
170 |
//len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket));
|
|
171 |
//PutStr(str3,len4);
|
|
172 |
ChnStats[nCurPollId].PkgLenErr++;
|
|
173 |
Uart2Stat.LengthErr++;
|
|
174 |
return -3; //not long enough
|
|
175 |
}
|
|
176 |
// if (p1->data[DataLen+1] != EndSign)
|
|
177 |
// {
|
|
178 |
// ChnStats[nCurPollId].NoEndErr++;
|
|
179 |
// Uart2Stat.LengthErr++;
|
|
180 |
// return -2;
|
|
181 |
// }
|
|
182 |
unsigned char thisBCC=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
183 |
if (thisBCC != p1->data[DataLen])
|
|
184 |
{//BCC Error;
|
|
185 |
Uart2Stat.BCCerr++;
|
|
186 |
return -4;
|
|
187 |
}
|
|
188 |
return 0;
|
|
189 |
}
|
|
190 |
|
|
191 |
int SlaveCheckPacket(pPacket p1,int nLen1)
|
|
192 |
{
|
|
193 |
if (p1->Sign != StartSign)
|
|
194 |
{
|
|
195 |
Uart2Stat.NotPacketErr++;
|
|
196 |
ChnStats[0].NotPkgErr++;
|
|
197 |
return -1;
|
|
198 |
}
|
|
199 |
int DataLen=p1->PacketLen;
|
|
200 |
if (DataLen>MaxPacketLength)
|
|
201 |
{
|
|
202 |
Uart2Stat.LengthErr++;
|
|
203 |
ChnStats[0].PkgLenErr++;
|
|
204 |
return -1;
|
|
205 |
}
|
|
206 |
if (nLen1<DataLen+sizeof(stPacket)+1)
|
|
207 |
{
|
|
208 |
//len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket));
|
|
209 |
//PutStr(str3,len4);
|
|
210 |
ChnStats[0].PkgLenErr++;
|
|
211 |
Uart2Stat.LengthErr++;
|
|
212 |
return -3; //not long enough
|
|
213 |
}
|
|
214 |
// if (p1->data[DataLen+1] != EndSign)
|
|
215 |
// {
|
|
216 |
// ChnStats[nCurPollId].NoEndErr++;
|
|
217 |
// Uart2Stat.LengthErr++;
|
|
218 |
// return -2;
|
|
219 |
// }
|
|
220 |
unsigned char thisBCC=BCC(p1,sizeof(stPacket)+DataLen-1);
|
|
221 |
if (thisBCC != p1->data[DataLen])
|
|
222 |
{//BCC Error;
|
|
223 |
Uart2Stat.BCCerr++;
|
|
224 |
ChnStats[0].BCCErr++;
|
|
225 |
return -4;
|
|
226 |
}
|
|
227 |
return 0;
|
|
228 |
}
|
|
229 |
|
|
230 |
int MasterParsePacket(pPacket p1,int Len1)
|
|
231 |
{
|
|
232 |
|
|
233 |
int DataLen=p1->PacketLen;
|
|
234 |
ChnStats[nCurPollId].RecvPackets++;
|
|
235 |
pPacket p2=(pPacket)PacketBuf2;
|
|
236 |
int PacketLen=0;
|
|
237 |
//LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_6);
|
|
238 |
int ChildId=p1->SrcAddr;
|
|
239 |
unsigned char nIndex;
|
|
240 |
switch (p1->nCMD)
|
|
241 |
{
|
|
242 |
|
|
243 |
case cmdNone:
|
|
244 |
break;
|
|
245 |
case cmdPing:
|
|
246 |
BufferIn[ChildId]=p1->data[0];
|
|
247 |
PacketLen=MakePacket(p2,0,nCurPollId,cmdPingReply,p1->nSEQ,DataLen,p1->data);
|
|
248 |
SendPacket2(p2,PacketLen);
|
|
249 |
break;
|
|
250 |
case cmdPingReply:
|
|
251 |
DelayuS=ThisuS-SendTimeuS;
|
|
252 |
if (DelayuS > MaxDelayuS) MaxDelayuS = DelayuS;
|
|
253 |
|
|
254 |
BufferIn[ChildId]=p1->data[0];
|
|
255 |
|
|
256 |
//RunStat=100;
|
|
257 |
ChnStats[nCurPollId].CtnLstPkts=0;
|
|
258 |
ChnStats[nCurPollId].Delay=DelayuS;
|
|
259 |
if (DelayuS > ChnStats[nCurPollId].MaxDelay)
|
|
260 |
ChnStats[nCurPollId].MaxDelay=DelayuS;
|
|
261 |
//PutOutput(outputvalue);
|
|
262 |
MasterRecved=1;
|
|
263 |
break;
|
|
264 |
case cmdRead:
|
|
265 |
break;
|
|
266 |
case cmdReadReply:
|
|
267 |
break;
|
|
268 |
case cmdWrite:
|
|
269 |
break;
|
|
270 |
case cmdWriteReply:
|
|
271 |
MasterRecved=1;
|
|
272 |
break;
|
|
273 |
case cmdGetVersion:
|
|
274 |
break;
|
|
275 |
case cmdVerInfo:
|
|
276 |
break;
|
|
277 |
case cmdExChgData:
|
|
278 |
BufferIn[0]=p1->data[0];
|
|
279 |
//PutOutput(outputvalue);
|
|
280 |
//memcpy(DispBuf,p1->data+2,8);
|
|
281 |
p1->data[0]=BufferOut[0];
|
|
282 |
PacketLen=MakePacket(p2,nAddr,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
|
|
283 |
SendPacket2(p2,PacketLen);
|
|
284 |
break;
|
|
285 |
case cmdExChgDataReply:
|
|
286 |
DelayuS=ThisuS-SendTimeuS;
|
|
287 |
if (DelayuS > MaxDelayuS) MaxDelayuS = DelayuS;
|
|
288 |
|
|
289 |
BufferIn[ChildId]=p1->data[0];
|
|
290 |
|
|
291 |
//RunStat=100;
|
|
292 |
ChnStats[nCurPollId].CtnLstPkts=0;
|
|
293 |
ChnStats[nCurPollId].Delay=DelayuS;
|
|
294 |
if (DelayuS > ChnStats[nCurPollId].MaxDelay)
|
|
295 |
ChnStats[nCurPollId].MaxDelay=DelayuS;
|
|
296 |
//PutOutput(outputvalue);
|
|
297 |
|
|
298 |
|
|
299 |
nIndex=p1->data[3];
|
|
300 |
ChnStats[nCurPollId].ClientDatas[nIndex]=p1->data[4]|(p1->data[5]<<8)|(p1->data[6]<<16)|(p1->data[7]<<24);
|
|
301 |
|
|
302 |
MasterRecved=1;
|
|
303 |
|
|
304 |
break;
|
|
305 |
|
|
306 |
default:
|
|
307 |
break;
|
|
308 |
}
|
|
309 |
// nCurPollId ++;
|
|
310 |
// if (nCurPollId > nChilds)
|
|
311 |
// {
|
|
312 |
// nCurPollId=1;
|
|
313 |
// }
|
|
314 |
return 0;
|
|
315 |
}
|
|
316 |
int SlaveParsePacket(pPacket p1,int Len1)
|
|
317 |
{
|
|
318 |
Uart2Stat.OKPacket++;
|
|
319 |
int DataLen=p1->PacketLen;
|
|
320 |
//int nSrcAddr=p1->SrcAddr;
|
|
321 |
int nDstAddr=p1->DstAddr;
|
|
322 |
|
|
323 |
// RecvTimeuS=ThisuS;
|
|
324 |
// SlaveRecved=1;
|
|
325 |
|
|
326 |
pPacket p2=(pPacket)PacketBuf2;
|
|
327 |
int PacketLen=0;
|
|
328 |
unsigned char nIndex = p1->nSEQ & 0x07;
|
|
329 |
if (nDstAddr!=nAddr && nDstAddr != 0xff)
|
|
330 |
{
|
|
331 |
ChnStats[0].ClientMisIdPkts++;
|
|
332 |
return -1;
|
|
333 |
}
|
|
334 |
if (nDstAddr==nAddr || nDstAddr==0xff)
|
|
335 |
{
|
|
336 |
RecvTimeuS=ThisuS;
|
|
337 |
SlaveRecved=1;
|
|
338 |
switch (p1->nCMD)
|
|
339 |
{
|
|
340 |
case cmdNone:
|
|
341 |
break;
|
|
342 |
case cmdPing:
|
|
343 |
BufferIn[0]=p1->data[0];
|
|
344 |
//PutOutput(outputvalue);
|
|
345 |
//memcpy(DispBuf,p1->data+2,8);
|
|
346 |
p1->data[0]=BufferOut[0];
|
|
347 |
RecvTimeuS=ThisuS;
|
|
348 |
PacketLen=MakePacket(p2,nAddr,0,cmdPingReply,p1->nSEQ,DataLen,p1->data);
|
|
349 |
ChnStats[0].ClientSendPkts++;
|
|
350 |
SendPacket2(p2,PacketLen);
|
|
351 |
break;
|
|
352 |
case cmdPingReply:
|
|
353 |
break;
|
|
354 |
case cmdRead:
|
|
355 |
break;
|
|
356 |
case cmdReadReply:
|
|
357 |
break;
|
|
358 |
case cmdWrite:
|
|
359 |
//memcpy(DispBuf,p1->data,DataLen);
|
|
360 |
PacketLen=MakePacket(p2,1,0,cmdWriteReply,p1->nSEQ,0,0);
|
|
361 |
ChnStats[0].ClientSendPkts++;
|
|
362 |
SendPacket2(p2,PacketLen);
|
|
363 |
break;
|
|
364 |
case cmdWriteReply:
|
|
365 |
break;
|
|
366 |
case cmdGetVersion:
|
|
367 |
break;
|
|
368 |
case cmdVerInfo:
|
|
369 |
break;
|
|
370 |
case cmdExChgData:
|
|
371 |
BufferIn[0]=p1->data[0];
|
|
372 |
nSlaveTick=p1->data[4]+(p1->data[5]<<8);//+(p1->data[6]<<16)+(p1->data[7]<<24);
|
|
373 |
|
|
374 |
//PutOutput(outputvalue);
|
|
375 |
//memcpy(DispBuf,p1->data+2,8);
|
|
376 |
p1->data[0]=BufferOut[0];
|
|
377 |
p1->data[3]=nIndex;
|
|
378 |
p1->data[4]=ChnStats[0].ClientDatas[nIndex];
|
|
379 |
p1->data[5]=ChnStats[0].ClientDatas[nIndex]>>8;
|
|
380 |
p1->data[6]=ChnStats[0].ClientDatas[nIndex]>>16;
|
|
381 |
p1->data[7]=ChnStats[0].ClientDatas[nIndex]>>24;
|
|
382 |
nIndex++;
|
|
383 |
PacketLen=MakePacket(p2,nAddr,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
|
|
384 |
ChnStats[0].ClientSendPkts++;
|
|
385 |
SendPacket2(p2,PacketLen);
|
|
386 |
break;
|
|
387 |
case cmdExChgDataReply:
|
|
388 |
break;
|
|
389 |
|
|
390 |
|
|
391 |
case cmdSyncRead:
|
|
392 |
break;
|
|
393 |
case cmdSyncWrite:
|
|
394 |
break;
|
|
395 |
case cmdSequenRead:
|
|
396 |
break;
|
|
397 |
case cmdSyncTime:
|
|
398 |
nSlaveTick=p1->data[0]+(p1->data[1]<<8)+(p1->data[2]<<16)+(p1->data[3]<<24);
|
|
399 |
break;
|
|
400 |
default:
|
|
401 |
break;
|
|
402 |
}
|
|
403 |
}
|
|
404 |
return 0;
|
|
405 |
}
|
|
406 |
int ParsePacket(pPacket p1,int Len1)
|
|
407 |
{
|
|
408 |
ThisuS=GetuS();
|
|
409 |
int Result=0;
|
|
410 |
if (bMaster)
|
|
411 |
{
|
|
412 |
Result=CheckPacket(p1,Len1);
|
|
413 |
if (Result != S_OK)
|
|
414 |
{
|
|
415 |
return Result;
|
|
416 |
}
|
|
417 |
Result=MasterParsePacket(p1,Len1);
|
|
418 |
return Result;
|
|
419 |
}
|
|
420 |
if (bSlave)
|
|
421 |
{
|
|
422 |
ChnStats[0].ClientRecvPkts++;
|
|
423 |
Result=CheckPacket(p1,Len1);
|
|
424 |
if (Result != S_OK)
|
|
425 |
{
|
|
426 |
return Result;
|
|
427 |
}
|
|
428 |
Result=SlaveParsePacket(p1,Len1);
|
|
429 |
return Result;
|
|
430 |
}
|
|
431 |
//int len1=p1->PacketLen;
|
|
432 |
// if (p1->DstAddr!=255&&p1->DstAddr!=2) return -3;
|
|
433 |
// pPacket p2=(pPacket)PacketBuf2;
|
|
434 |
// Uart2Stat.OKPacket++;
|
|
435 |
|
|
436 |
return Result;
|
|
437 |
}
|
|
438 |
|
|
439 |
int InitMachine(stMachineConfig * pConfig)
|
|
440 |
{
|
|
441 |
return S_OK;
|
|
442 |
}
|
|
443 |
|
|
444 |
int SetConfig(void)
|
|
445 |
{
|
|
446 |
return S_OK;
|
|
447 |
}
|
|
448 |
|
|
449 |
int StartConfig(void)
|
|
450 |
{
|
|
451 |
return S_OK;
|
|
452 |
}
|
|
453 |
|
|
454 |
int SetMasterConfig(void)
|
|
455 |
{
|
|
456 |
return S_OK;
|
|
457 |
}
|
|
458 |
|
|
459 |
int StartPolling(void)
|
|
460 |
{
|
|
461 |
return S_OK;
|
|
462 |
}
|
|
463 |
|
|
464 |
int ReadData(void)
|
|
465 |
{
|
|
466 |
return S_OK;
|
|
467 |
}
|
|
468 |
|
|
469 |
int WriteData(void)
|
|
470 |
{
|
|
471 |
return S_OK;
|
|
472 |
}
|
|
473 |
|
|
474 |
int GetStat(void)
|
|
475 |
{
|
|
476 |
return S_OK;
|
|
477 |
}
|
|
478 |
|