QuakeGod
2024-02-25 95322c84888cbe2e92024d4d65698f59b016cb52
提交 | 用户 | age
483170 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 #include "stm32f0xx.h"
11
12 unsigned char bKBusMaster=0,bKBusSlave=0,bKBusRepeater=0;;
13
14 unsigned char PacketBuf1[128];
15 unsigned char PacketBuf2[128];
16
17 unsigned char BufferIn[16]={0};
18 unsigned char BufferOut[16]={0};
19
20 unsigned char KBusDiagnosis =1;
21 unsigned char KBusSnapPos = 0;
22
23 stChnStat KBusChnStats[8];
24 unsigned char nStationID=0;
25 unsigned char nChilds;
26 int ChildStat[16];
27 unsigned char nCurPollId=0;
28 unsigned char nSeq=0;
29
30 volatile unsigned char KBusMasterRecved=1;
31 volatile unsigned char KBusMasterRecvOK=1;
32
33 volatile unsigned char KBusSlaveRecved=1;
34 unsigned int KBusSendTimeuS=0;
35 volatile int KBusRecvTimeuS=0;
36
37 int KBusDelayuS=0;
38 int KBusMaxDelayuS=0;
39 int ThisuS;
40 volatile unsigned int nSlaveTick=0;
41
42 int Clk3=0;
43 int SendTime,Latancy,LatancyClk,SendClk;    
44
45 volatile int nCount2=0;
46 int TimeOutCount=0;
47 int LastCircleStartTime=0;
48 int CircleTime=0;
49
50 unsigned char Datas[128];        
51 volatile int PacketLength = 0;
52
8b51c7 53 stClientInfo ClientInfo[16];
Q 54
55
95322c 56
483170 57 unsigned char KBusBCC(void * pData, int nSize)
Q 58 {
59     unsigned char k;
60     k=0;
61     for (int i=0;i<nSize;i++)
62     {
63         k^=((unsigned char *)pData)[i];
64     }
65     return k;    
66 }
67
68 int KBusMakePacket(pKBPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nStatus, unsigned char DataLen,void * pData )
69 {
70     p1->Sign=StartSign;
71     p1->DstHost=dst;
72     p1->SrcAddr=src;
73     p1->nCMD=nType;
74     p1->nStatus=nStatus;
75     int PacketLenth=0;
76     switch (nType)
77     {
78         case cmdNone:
79             break;
80         case cmdPing:
eaf5d5 81             p1->DataLen=DataLen;
483170 82             memcpy(p1->data,pData,DataLen);
Q 83             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
84             p1->data[DataLen+1]=EndSign;
85         
86             PacketLenth=sizeof(stKBPacket)+DataLen+1;
87             break;
88         case cmdPingReply:
eaf5d5 89             p1->DataLen=DataLen;
483170 90             memcpy(p1->data,pData,DataLen);
Q 91             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
92             p1->data[DataLen+1]=EndSign;        
93             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
94             break;
95         case cmdRead:
96             break;
97         case cmdReadReply:
98             break;
99         case cmdWrite:
100             break;
101         case cmdWriteReply:
eaf5d5 102             p1->DataLen=DataLen;
483170 103          if (DataLen !=0 )    memcpy(p1->data,pData,DataLen);
Q 104             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
105             p1->data[DataLen+1]=EndSign;
106             PacketLenth=sizeof(stKBPacket)+DataLen+1;                    
107             break;
108         case cmdGetVersion:
eaf5d5 109             p1->DataLen=DataLen;
483170 110             memcpy(p1->data,pData,DataLen);
Q 111             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
112             p1->data[DataLen+1]=EndSign;
113             PacketLenth=sizeof(stKBPacket)+DataLen+1;            
114             break;
115         case cmdVerInfo:
eaf5d5 116             p1->DataLen=DataLen;
483170 117             memcpy(p1->data,pData,DataLen);
Q 118             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
119             p1->data[DataLen+1]=EndSign;
120             PacketLenth=sizeof(stKBPacket)+DataLen+1;            
121             break;
122         case cmdExChgData:
eaf5d5 123             p1->DataLen=DataLen;
483170 124             memcpy(p1->data,pData,DataLen);
Q 125             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
126             p1->data[DataLen+1]=EndSign;
127             PacketLenth=sizeof(stKBPacket)+DataLen+1;
128             break;
129         case cmdExChgDataReply:
eaf5d5 130             p1->DataLen=DataLen;
483170 131             memcpy(p1->data,pData,DataLen);
Q 132             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
133             p1->data[DataLen+1]=EndSign;        
134             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
135             break;
136                 
137         case cmdSyncRead:
eaf5d5 138             p1->DataLen=DataLen;
483170 139             memcpy(p1->data,pData,DataLen);
Q 140             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
141             p1->data[DataLen+1]=EndSign;        
142             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
143             break;
144         case cmdSyncWrite:
eaf5d5 145             p1->DataLen=DataLen;
483170 146             memcpy(p1->data,pData,DataLen);
Q 147             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
148             p1->data[DataLen+1]=EndSign;        
149             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
150             break;
151         case cmdSequenRead:
eaf5d5 152             p1->DataLen=DataLen;
483170 153             memcpy(p1->data,pData,DataLen);
Q 154             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
155             p1->data[DataLen+1]=EndSign;        
156             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
157             break;        
158         
159         case cmdSyncTime:
eaf5d5 160             p1->DataLen=DataLen;
483170 161             memcpy(p1->data,pData,DataLen);
Q 162             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
163             p1->data[DataLen+1]=EndSign;        
164             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
165             break;
166         
167         default:
168             break;
169     }
170     
171     return PacketLenth;
172 }
173
174 int KBusCheckPacket(int nChn, pKBPacket p1, int nLen1)
175 {
176     unsigned char * p2 = (unsigned char *)p1;
177     pKBPacket p3=p1;
178     if ((p2[0] == SYN || p2[0] != StartSign) && nLen1 > 1)
179     {
180         p3=(pKBPacket)(p2+1);
181         nLen1--;
182     }    
183     
184     if (p3->Sign != StartSign)
185     {
186         Uart2Stat.NotPacketErr++;
187         KBusChnStats[nCurPollId].NotPkgErr++;
188         if (KBusDiagnosis) {
189             if (KBusSnapPos == 0) {
190                 KMem.WDB[0x20]=nLen1;
191                 memcpy(&KMem.WDB[0x21],p1,nLen1);
192                 KBusSnapPos++;
193             } else if (KBusSnapPos==1) {
194                 KMem.WDB[0x40]=nLen1;
195                 memcpy(&KMem.WDB[0x41],p1,nLen1);
196                 KBusSnapPos++;
197             } else {
198                 KMem.WDB[0x60]=nLen1;
199                 memcpy(&KMem.WDB[0x61],p1,nLen1);
200                 KBusSnapPos=0;                
201             }
202             
203         }
204         return -1;
205     }
eaf5d5 206     int DataLen=p3->DataLen;
483170 207     if (DataLen>MaxPacketLength) 
Q 208     {
209         Uart2Stat.LengthErr++;
210         KBusChnStats[nCurPollId].PkgLenErr++;
211         return -1;
212     }
213     if (nLen1<DataLen+sizeof(stKBPacket)+1)
214     {
215         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
216         //PutStr(str3,len4);
217         KBusChnStats[nCurPollId].PkgLenErr++;
218         Uart2Stat.LengthErr++;
219         return -3;    //not long enough                    
220     }
221 //    if (p3->data[DataLen+1] != EndSign)
222 //    {
223 //        KBusChnStats[nCurPollId].NoEndErr++;
224 //        Uart2Stat.LengthErr++;        
225 //        return -2;
226 //    }
227     unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
228     if (thisBCC != p3->data[DataLen]) 
229     {//BCC Error;
230         Uart2Stat.BCCerr++;
231         KBusChnStats[nCurPollId].BCCErr++;
232         return -4;
233     }         
234     return 0;
235 }
236
237 int KBusSlaveCheckPacket(int nChn, pKBPacket p1, int nLen1)
238 {
239     unsigned char * p2 = (unsigned char *)p1;
240     pKBPacket p3=p1;
241     if ((p2[0] == SYN || p2[0] != StartSign) && nLen1 > 1)
242     {
243         p3=(pKBPacket)(p2+1);
244         nLen1--;
245     }    
246     
247     if (p3->Sign != StartSign)
248     {
249         Uart2Stat.NotPacketErr++;
250         KBusChnStats[0].ClientNotPktErr++;
251         return -1;
252     }
eaf5d5 253     int DataLen=p3->DataLen;
483170 254     if (DataLen>MaxPacketLength) 
Q 255     {
256         Uart2Stat.LengthErr++;
257         KBusChnStats[0].ClientPkgLenErr++;
258         return -1;
259     }
260     if (nLen1<DataLen+sizeof(stKBPacket)+1)
261     {
262         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
263         //PutStr(str3,len4);
264         KBusChnStats[0].ClientPkgLenErr++;
265         Uart2Stat.LengthErr++;
266         return -3;    //not long enough                    
267     }
268 //    if (p3->data[DataLen+1] != EndSign)
269 //    {
270 //        KBusChnStats[nCurPollId].NoEndErr++;
271 //        Uart2Stat.LengthErr++;        
272 //        return -2;
273 //    }
274     unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
275     if (thisBCC != p3->data[DataLen]) 
276     {//BCC Error;
277         Uart2Stat.BCCerr++;
278         KBusChnStats[0].ClientBccErr++;
279         return -4;
280     }         
281     return 0;
282 }
283
284 int KBusMasterParsePacket(int nChn, pKBPacket p1, int Len1)
285 {
286     unsigned char * p6 = (unsigned char *)p1;
287
288     if ((p6[0] == SYN || p6[0] != StartSign) && Len1 > 1)
289     {
290         p1=(pKBPacket)(p6+1);
291         Len1--;
292     }    
293
eaf5d5 294         int DataLen=p1->DataLen;
483170 295         KBusChnStats[nCurPollId].RecvPackets++;    
Q 296         pKBPacket p2=(pKBPacket)PacketBuf2;        
297         int PacketLen=0;
298         //LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_6);
299         int ChildId=p1->SrcAddr;
300         unsigned char nIndex;
301         switch (p1->nCMD)
302         {
303             
304             case cmdNone:
305                 break;
306             case cmdPing:
307                 BufferIn[ChildId]=p1->data[0];
308                 PacketLen=KBusMakePacket(p2,0,nCurPollId,cmdPingReply,p1->nStatus,DataLen,p1->data);
309                 SendPacket(nChn, p2, PacketLen);
310                 break;
311             case cmdPingReply:
312                 KBusDelayuS=ThisuS-KBusSendTimeuS;
313                 if (KBusDelayuS > KBusMaxDelayuS) KBusMaxDelayuS = KBusDelayuS;
314                 
315                 BufferIn[ChildId]=p1->data[0];
316                 
317                 //RunStat=100;
318                 KBusChnStats[nCurPollId].CtnLstPkts=0;
319                 KBusChnStats[nCurPollId].Delay=KBusDelayuS;
320             if (KBusDelayuS > KBusChnStats[nCurPollId].MaxDelay) 
321                 KBusChnStats[nCurPollId].MaxDelay=KBusDelayuS;
322                 //PutOutput(outputvalue);
323                 KBusMasterRecvOK=1;
324                 break;
325             case cmdRead:
326                 break;
327             case cmdReadReply:
328                 break;
329             case cmdWrite:
330                 break;
331             case cmdWriteReply:
332                 KBusMasterRecved=1;
333                 break;
334             case cmdGetVersion:
335                 break;
336             case cmdVerInfo:
337                 break;
338             case cmdExChgData:
339                 BufferIn[0]=p1->data[0];
340                 //PutOutput(outputvalue);
341                 //memcpy(DispBuf,p1->data+2,8);
342                 p1->data[0]=BufferOut[0];
343                 PacketLen=KBusMakePacket(p2,nStationID,0,cmdExChgDataReply,p1->nStatus,DataLen,p1->data);
344                 SendPacket(nChn, p2, PacketLen);
345                 break;
346             case cmdExChgDataReply:
347                 KBusDelayuS=ThisuS-KBusSendTimeuS;
348                 if (KBusDelayuS > KBusMaxDelayuS) KBusMaxDelayuS = KBusDelayuS;
349
350 #if (BOARD_TYPE == 14)                
351                 BufferIn[ChildId]=p1->data[0];
8b51c7 352                 BufferIn[ChildId + 1] = p1->data[1];
Q 353                 BufferIn[ChildId + 2 ] = p1->data[2];
354             
483170 355                 KMem.WXB[ChildId-1]=BufferIn[ChildId];
8b51c7 356                 KMem.WXB[ChildId]=BufferIn[ChildId+1];
Q 357                 KMem.WXB[ChildId+1]=BufferIn[ChildId+2];
358             
483170 359                 if (KMRunStat.WorkMode==0) {    
Q 360                 //    KMem.WY[0]= KMem.WX[1]+(KMem.WX[2]<<8)    ;
361                 //    PutOutput (KMem.WY[0]);
362                 }
363 #else
364                 BufferIn[ChildId]=p1->data[0];
365                 KMem.WLX[ChildId]=BufferIn[ChildId];
366                 if (KMRunStat.WorkMode==0) {    
367                     KMem.WY[0]= KMem.WLX[1]+(KMem.WLX[2]<<8)    ;
368                     PutOutput (KMem.WY[0]);
369                 }
370 #endif
371
372                 //RunStat=100;
373                 KBusChnStats[nCurPollId].CtnLstPkts=0;
374                 KBusChnStats[nCurPollId].Delay=KBusDelayuS;
375             if (KBusDelayuS > KBusChnStats[nCurPollId].MaxDelay) 
376                 KBusChnStats[nCurPollId].MaxDelay=KBusDelayuS;
377                 //PutOutput(outputvalue);
378                 
379             
380                 nIndex=p1->data[3];
381                 KBusChnStats[nCurPollId].ClientDatas[nIndex]=p1->data[4]|(p1->data[5]<<8)|(p1->data[6]<<16)|(p1->data[7]<<24);
382             
383                 KBusMasterRecvOK=1;
384                 
385                 break;
386                     
387             default:
388                 break;        
389         }
390     //    nCurPollId ++;
391     //    if (nCurPollId > nChilds)
392     //    {
393     //        nCurPollId=1;
394     //    }    
395     return 0;
396 }
397
398 unsigned char nClientDataIndex=0;
399 int KBusSlaveParsePacket(int nChn, pKBPacket p1, int Len1)
400 {
401     unsigned char * p3 = (unsigned char *)p1;
402     if (p3[0] == SYN && Len1 > 1)
403     {
404         p1=(pKBPacket)(p3+1);
405         Len1--;
406     }        
407     
408     Uart2Stat.OKPacket++;                
eaf5d5 409     int DataLen=p1->DataLen;    
483170 410 //int nSrcAddr=p1->SrcAddr;
Q 411     int nDstHost=p1->DstHost;
412         
413 //    KBusRecvTimeuS=ThisuS;
414 //    KBusSlaveRecved=1;
415     
416     pKBPacket p2=(pKBPacket)PacketBuf2;
417     
418     int PacketLen=0;
419     unsigned char nIndex;// = p1->nStatus & 0x07;
420     if (nDstHost!=nStationID && nDstHost != 0xff)
421     {
422         KBusChnStats[0].ClientMisIdPkts++;
423         return -1;
424     }
425     if (nDstHost==nStationID || nDstHost==0xff)
426     {
427         KBusRecvTimeuS=ThisuS;
428         KBusSlaveRecved=1;
429         switch (p1->nCMD)
430         {
431             case cmdNone:
432                 break;
433             case cmdPing:
434                 BufferIn[0]=p1->data[0];
435                 //PutOutput(outputvalue);
436                 //memcpy(DispBuf,p1->data+2,8);
437                 p1->data[0]=BufferOut[0];
438                 KBusRecvTimeuS=ThisuS;
439                 PacketLen=KBusMakePacket(p2,nStationID,0,cmdPingReply,p1->nStatus,DataLen,p1->data);
440                 KBusChnStats[0].ClientSendPkts++;
441                 SendPacket(nChn, p2, PacketLen);
442                 break;
443             case cmdPingReply:
444                 break;
445             case cmdRead:
446                 break;
447             case cmdReadReply:
448                 break;
449             case cmdWrite:
450                 //memcpy(DispBuf,p1->data,DataLen);
451                 PacketLen=KBusMakePacket(p2,1,0,cmdWriteReply,p1->nStatus,0,0);
452                 KBusChnStats[0].ClientSendPkts++;
453                 SendPacket(nChn, p2, PacketLen);                    
454                 break;
455             case cmdWriteReply:
456                 break;
457             case cmdGetVersion:
458                 break;
459             case cmdVerInfo:
460                 break;
461             case cmdExChgData:
462                 BufferIn[0]=p1->data[0];
8b51c7 463                 BufferIn[1]=p1->data[1];
Q 464 //                KMem.WLYB[1] = BufferIn[1];
465         
466                 KMem.WYB[1] = BufferIn[1];
483170 467                 nSlaveTick=p1->data[4]+(p1->data[5]<<8);//+(p1->data[6]<<16)+(p1->data[7]<<24);
Q 468 #if (BOARD_TYPE == 14)
469             //    PutOutput(BufferIn[0]);
470                 //PutOutput(outputvalue);
471                 //memcpy(DispBuf,p1->data+2,8);
472                 nIndex=nClientDataIndex;
473         //        KBusChnStats[0].ClientDatas[7]++;
474 //                BufferOut[0]=GetInput();
475         //        BufferOut[0]=GetInput();
476 #else
477                 PutOutput(BufferIn[0]);
478                 //PutOutput(outputvalue);
479                 //memcpy(DispBuf,p1->data+2,8);
480                 nIndex=nClientDataIndex;
481         //        KBusChnStats[0].ClientDatas[7]++;
482 //                BufferOut[0]=GetInput();
483                 BufferOut[0]=GetInput();
8b51c7 484                 BufferOut[1]=KMem.WXB[1];            
483170 485 #endif
Q 486                 p1->data[0]=BufferOut[0];
8b51c7 487                 p1->data[1]=BufferOut[1];
Q 488                 
483170 489                 p1->data[3]=nIndex;
Q 490                 p1->data[4]=KBusChnStats[0].ClientDatas[nIndex];
491                 p1->data[5]=KBusChnStats[0].ClientDatas[nIndex]>>8;
492                 p1->data[6]=KBusChnStats[0].ClientDatas[nIndex]>>16;
493                 p1->data[7]=KBusChnStats[0].ClientDatas[nIndex]>>24;
494                 nClientDataIndex++;
495                 if (nClientDataIndex >= 10) { nClientDataIndex=0;}
496                 unStatus nStatus;
497                 nStatus.nStatus = p1->nStatus;
498                 if (nStatus.nErr1) {    KMem.ErrStat=8000;}
499                 PacketLen=KBusMakePacket(p2,nStationID,0,cmdExChgDataReply,p1->nStatus,DataLen,p1->data);
500                 KBusChnStats[0].ClientSendPkts++;
501                 SendPacket(nChn, p2, PacketLen);
502                 break;
503             case cmdExChgDataReply:
504                 break;
505             
506             
507             case cmdSyncRead:
508                 break;
509             case cmdSyncWrite:
510                 break;
511             case cmdSequenRead:
512                 break;
513             case cmdSyncTime:
514                 nSlaveTick=p1->data[0]+(p1->data[1]<<8)+(p1->data[2]<<16)+(p1->data[3]<<24);
515                 break;
516             default:
517                 break;        
518         }
519     }    
520     return 0;
521 }
522
523 int KBusParsePacket(int nChn, pKBPacket p1, int Len1)
524 {
525     ThisuS=GetuS();
526     int Result=0;
527     unsigned char * p2 = (unsigned char *)p1;
528     if (p2[0] == SYN && Len1 > 1)
529     {
530         p1=(pKBPacket)(p2+1);
531         Len1--;
532     }
533     
534     if (bKBusMaster)
535     {
536             KBusMasterRecved=1;
537             Result=KBusCheckPacket(nChn, p1, Len1);
538             if (Result != S_OK)
539             {
540                 return Result;
541             }
542             KBusMasterRecvOK=1;
543             Result=KBusMasterParsePacket(nChn, p1, Len1);            
544             return Result;
545     }
546     if (bKBusSlave)
547     {
548             KBusChnStats[0].ClientRecvPkts++;
549             Result=KBusSlaveCheckPacket(nChn, p1, Len1);
550             if (Result != S_OK)
551             {
552                 return Result;
553             }
554             KBusChnStats[0].ClientTimeOutErr=KMem.RunStat;
555             Result=KBusSlaveParsePacket(nChn, p1, Len1);
556             return Result;
557     }
558     //int len1=p1->PacketLen;
559 //    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
560 //    pKBPacket p2=(pKBPacket)PacketBuf2;
561 //            Uart2Stat.OKPacket++;
562
563     return Result;
564 }
565
566 /*
567 int InitMachine(stMachineConfig * pConfig)
568 {
569     return S_OK;
570 }
571
572 int SetConfig(void)
573 {
574     return S_OK;
575 }
576
577 int StartConfig(void)
578 {
579     return S_OK;
580 }
581     
582 int SetMasterConfig(void)
583 {
584     return S_OK;
585 }
586
587 int StartPolling(void)
588 {
589     return S_OK;
590 }
591
592 int ReadData(void)
593 {
594     return S_OK;
595 }
596
597 int WriteData(void)
598 {
599     return S_OK;
600 }
601
602 int GetStat(void)
603 {
604     return S_OK;
605 }
606 */
607 int KBusRepeaterFunc(int nChn)
608 {
609         KMem.WY[0]=KMem.WX[0];
610         if ((KMem.nRunCount &0x7f) == 88) 
611         { 
612             nCount2++;    
613             ToggleRunLed();
614 //        int len1=sprintf(str1,"%d %d Cfg %02X  Input %02X  \r\n",nCount,nCount2,EffJumperSW,MyKeyStat1);
615 //        PutStr(str1,len1);
616         }                
617     return 0;
618 }
619
620 int KBusMasterFunc(int nChn)
621 {
622     uint32_t tick1=HAL_GetTick();
623     uint32_t thisuS=GetuS();
624
625         int len1=0;
626
627         if ((KBusMasterRecved && KBusMasterRecvOK && thisuS-KBusSendTimeuS>50) || thisuS-KBusSendTimeuS>1000u)
628         {
629             if (!KBusMasterRecvOK) 
630             {
631                 TimeOutCount++;
632                 Uart2Stat.TimeOutErr++; 
633                 KBusChnStats[nCurPollId].LostPackets++;
634                 KBusChnStats[nCurPollId].CtnLstPkts++;
635                 if (!KBusMasterRecved) {KBusChnStats[nCurPollId].TimeOutErr++;}
636                 if (KBusChnStats[nCurPollId].CtnLstPkts>KBusChnStats[nCurPollId].MaxCtnLstPkts)
637                 {KBusChnStats[nCurPollId].MaxCtnLstPkts=KBusChnStats[nCurPollId].CtnLstPkts;}
638                 if (KBusChnStats[nCurPollId].CtnLstPkts>3)
639                 {
640                     KBusChnStats[nCurPollId].Stat=0;
641                     KMem.ErrStat=200;
8b51c7 642 #if (BOARD_TYPE == 14)
Q 643                     BufferIn[nCurPollId]=0;
644                     KMem.WXB[nCurPollId-1]=BufferIn[nCurPollId];                
645 #else                    
646                     {BufferIn[nCurPollId]=0;
647                         KMem.WLX[nCurPollId]=BufferIn[nCurPollId];                    
648                     }
649 #endif
483170 650                 }
Q 651             //    LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
652             }else
653             {
654                 KBusChnStats[nCurPollId].Stat=1;
655                 
656                 KMem.RunStat=100;
657             }
658             nCurPollId ++;
659             if (nCurPollId > nChilds)
660             {
661                 CircleTime=thisuS-LastCircleStartTime;
662                 LastCircleStartTime=thisuS;
663                 nSeq++;
664                 nCurPollId=1;
665             }
666 #if (BOARD_TYPE == 14)
667             if (KMRunStat.WorkMode==0)
668             {
669             //    KMem.WX[0]= GetInput();
670             //    KMem.WY[1]=KMem.WX[0]&0xff;
671             //    KMem.WY[2]=(KMem.WX[0]>>8)&0xff;        
672             }
673         //    BufferOut[1]=KMem.WY[1];
674         //    BufferOut[2]=KMem.WY[2];        
675 #else
676             if (KMRunStat.WorkMode==0)
677             {
678                 KMem.WX[0]= GetInput();
679                 KMem.WLY[1]=KMem.WX[0]&0xff;
680                 KMem.WLY[2]=(KMem.WX[0]>>8)&0xff;        
681             }
682             BufferOut[1]=KMem.WLY[1];
683             BufferOut[2]=KMem.WLY[2];        
684 #endif
685
686             Datas[0]=BufferOut[nCurPollId];
687             Datas[1]=BufferOut[nCurPollId+1];;
688             Datas[2]=KBusChnStats[nCurPollId].Stat;
689             Datas[3]=0;
690             Datas[4]=tick1&0xff;
691             Datas[5]=(tick1>>8)&0xff;
692             Datas[6]=(tick1>>16)&0xff;
693             Datas[7]=(tick1>>24)&0xff;
694             
695             KBusSendTimeuS=thisuS;
696             unStatus nStatus;
697             nStatus.nSeq = nSeq;
698             nStatus.nErr1 = (KBusChnStats[nCurPollId].Stat==0);
699             
700             
701             len1=KBusMakePacket((pKBPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nStatus.nStatus,8,Datas);
702 //            LL_USART_SetBaudRate(USART2,48000000,LL_USART_OVERSAMPLING_8,DefaultUart2Baud);
703             
704             SendPacket(nChn, (pKBPacket)PacketBuf1, len1);
705             KBusChnStats[nCurPollId].SendPackets++;
706             KBusChnStats[nCurPollId].SendTimeInterval=KBusSendTimeuS-KBusChnStats[nCurPollId].LastSentTimeuS;
707             KBusChnStats[nCurPollId].LastSentTimeuS=KBusSendTimeuS;
708 //            PacketLength = len1;
709             SendTime=tick1;
710
711             KBusMasterRecved=0;
712             KBusMasterRecvOK=0;
713         //    LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);        
714             //ToggleErrLed();
715 //                ToggleOut8();
716
717         }
718     
719 //        Clk3=SysTick->VAL;
720     //    LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_4);
721 //    HAL_Delay(1);                
722     return 0;
723 }
724
725 int KBusSlaveFunc(int nChn)
726 {
727         int ThisuS=GetuS();
728         int thisRecvTime=KBusRecvTimeuS;
729      if (nStationID >0) {
730             if (KBusSlaveRecved)
731             {
732                 KMem.RunStat=8000;
733                 KBusSlaveRecved=0;
734             }else if ((ThisuS - thisRecvTime) >12000u)
735             {
736                 KMem.ErrStat=8000;
737                 KMem.SDD[17]=1;
738                 KMem.SDD[18]=ThisuS;
739                 KMem.SDD[19]=KBusRecvTimeuS;
740             }else if ( ThisuS > (thisRecvTime + 12000u))
741             {
742                 KMem.ErrStat=8000;
743                 KMem.SDD[17]=2;
744                 KMem.SDD[18]=ThisuS;
745                 KMem.SDD[19]=KBusRecvTimeuS;
746             }
747     }
748     return 0;
749 }