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