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