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