QuakeGod
2022-10-17 8587c5cbb40fd60b37d762dfafa8fef683eb4fea
提交 | 用户 | 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];
402                 nSlaveTick=p1->data[4]+(p1->data[5]<<8);//+(p1->data[6]<<16)+(p1->data[7]<<24);
a7db3c 403 #if (BOARD_TYPE == 14)
Q 404             //    PutOutput(BufferIn[0]);
bfc108 405                 //PutOutput(outputvalue);
Q 406                 //memcpy(DispBuf,p1->data+2,8);
a7db3c 407                 nIndex=nClientDataIndex;
Q 408         //        ChnStats[0].ClientDatas[7]++;
409 //                BufferOut[0]=GetInput();
410         //        BufferOut[0]=GetInput();
411 #else
412                 PutOutput(BufferIn[0]);
413                 //PutOutput(outputvalue);
414                 //memcpy(DispBuf,p1->data+2,8);
415                 nIndex=nClientDataIndex;
416         //        ChnStats[0].ClientDatas[7]++;
417 //                BufferOut[0]=GetInput();
418                 BufferOut[0]=GetInput();
419 #endif
bfc108 420                 p1->data[0]=BufferOut[0];
Q 421                 p1->data[3]=nIndex;
422                 p1->data[4]=ChnStats[0].ClientDatas[nIndex];
423                 p1->data[5]=ChnStats[0].ClientDatas[nIndex]>>8;
424                 p1->data[6]=ChnStats[0].ClientDatas[nIndex]>>16;
425                 p1->data[7]=ChnStats[0].ClientDatas[nIndex]>>24;
a7db3c 426                 nClientDataIndex++;
Q 427                 if (nClientDataIndex >= 10) { nClientDataIndex=0;}
bfc108 428                 PacketLen=MakePacket(p2,nAddr,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
Q 429                 ChnStats[0].ClientSendPkts++;
a7db3c 430                 SendPacket(nChn, p2, PacketLen);
bfc108 431                 break;
Q 432             case cmdExChgDataReply:
433                 break;
434             
435             
436             case cmdSyncRead:
437                 break;
438             case cmdSyncWrite:
439                 break;
440             case cmdSequenRead:
441                 break;
442             case cmdSyncTime:
443                 nSlaveTick=p1->data[0]+(p1->data[1]<<8)+(p1->data[2]<<16)+(p1->data[3]<<24);
444                 break;
445             default:
446                 break;        
447         }
448     }    
449     return 0;
450 }
a7db3c 451 int ParsePacket(int nChn, pPacket p1, int Len1)
bfc108 452 {
Q 453     ThisuS=GetuS();
454     int Result=0;
a7db3c 455     if (bKBusMaster)
bfc108 456     {
a7db3c 457             MasterRecved=1;
Q 458             Result=CheckPacket(nChn, p1, Len1);
bfc108 459             if (Result != S_OK)
Q 460             {
461                 return Result;
462             }
a7db3c 463             MasterRecvOK=1;
Q 464             Result=MasterParsePacket(nChn, p1, Len1);            
bfc108 465             return Result;
Q 466     }
a7db3c 467     if (bKBusSlave)
bfc108 468     {
Q 469             ChnStats[0].ClientRecvPkts++;
a7db3c 470             Result=SlaveCheckPacket(nChn, p1, Len1);
bfc108 471             if (Result != S_OK)
Q 472             {
473                 return Result;
474             }
a7db3c 475             ChnStats[0].ClientTimeOutErr=KMem.RunStat;
Q 476             Result=SlaveParsePacket(nChn, p1, Len1);
bfc108 477             return Result;
Q 478     }
479     //int len1=p1->PacketLen;
a7db3c 480 //    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
bfc108 481 //    pPacket p2=(pPacket)PacketBuf2;
Q 482 //            Uart2Stat.OKPacket++;
483
484     return Result;
485 }
486
487 int InitMachine(stMachineConfig * pConfig)
488 {
489     return S_OK;
490 }
491
492 int SetConfig(void)
493 {
494     return S_OK;
495 }
496
497 int StartConfig(void)
498 {
499     return S_OK;
500 }
501     
502 int SetMasterConfig(void)
503 {
504     return S_OK;
505 }
506
507 int StartPolling(void)
508 {
509     return S_OK;
510 }
511
512 int ReadData(void)
513 {
514     return S_OK;
515 }
516
517 int WriteData(void)
518 {
519     return S_OK;
520 }
521
522 int GetStat(void)
523 {
524     return S_OK;
525 }
526
a7db3c 527 int KBusRepeaterFunc(int nChn)
Q 528 {
529         KMem.WY[0]=KMem.WX[0];
530         if ((KMem.nRunCount &0x7f) == 88) 
531         { 
532             nCount2++;    
533             ToggleRunLed();
534 //        int len1=sprintf(str1,"%d %d Cfg %02X  Input %02X  \r\n",nCount,nCount2,EffJumperSW,MyKeyStat1);
535 //        PutStr(str1,len1);
536         }                
537     return 0;
538 }
539
540 int KBusMasterFunc(int nChn)
541 {
542     uint32_t tick1=HAL_GetTick();
543     uint32_t thisuS=GetuS();
544
545         int len1=0;
546
547         if ((MasterRecved && MasterRecvOK && thisuS-SendTimeuS>50) || thisuS-SendTimeuS>1000u)
548         {
549             if (!MasterRecvOK) 
550             {
551                 TimeOutCount++;
552                 Uart2Stat.TimeOutErr++; 
553                 ChnStats[nCurPollId].LostPackets++;
554                 ChnStats[nCurPollId].CtnLstPkts++;
555                 if (!MasterRecved) {ChnStats[nCurPollId].TimeOutErr++;}
556                 if (ChnStats[nCurPollId].CtnLstPkts>ChnStats[nCurPollId].MaxCtnLstPkts)
557                 {ChnStats[nCurPollId].MaxCtnLstPkts=ChnStats[nCurPollId].CtnLstPkts;}
558                 if (ChnStats[nCurPollId].CtnLstPkts>3)
559                 {
560                     ChnStats[nCurPollId].Stat=0;
561                     KMem.ErrStat=200;
562                      
563                     {BufferIn[nCurPollId]=0;}
564                 }
565             //    LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
566             }else
567             {
568                 ChnStats[nCurPollId].Stat=1;
569                 
570                 KMem.RunStat=100;
571             }
572             nCurPollId ++;
573             if (nCurPollId > nChilds)
574             {
575                 CircleTime=thisuS-LastCircleStartTime;
576                 LastCircleStartTime=thisuS;
577                 nSeq++;
578                 nCurPollId=1;
579             }
580 #if (BOARD_TYPE == 14)
581             if (KMRunStat.WorkMode==0)
582             {
583             //    KMem.WX[0]= GetInput();
584             //    KMem.WY[1]=KMem.WX[0]&0xff;
585             //    KMem.WY[2]=(KMem.WX[0]>>8)&0xff;        
586             }
587         //    BufferOut[1]=KMem.WY[1];
588         //    BufferOut[2]=KMem.WY[2];        
589 #else
590             if (KMRunStat.WorkMode==0)
591             {
592                 KMem.WX[0]= GetInput();
593                 KMem.WY[1]=KMem.WX[0]&0xff;
594                 KMem.WY[2]=(KMem.WX[0]>>8)&0xff;        
595             }
596             BufferOut[1]=KMem.WY[1];
597             BufferOut[2]=KMem.WY[2];        
598 #endif
599
600             Datas[0]=BufferOut[nCurPollId];
601             Datas[1]=BufferOut[nCurPollId+1];;
602             Datas[2]=ChnStats[nCurPollId].Stat;
603             Datas[3]=0;
604             Datas[4]=tick1&0xff;
605             Datas[5]=(tick1>>8)&0xff;
606             Datas[6]=(tick1>>16)&0xff;
607             Datas[7]=(tick1>>24)&0xff;
608             
609             SendTimeuS=thisuS;                
610             len1=MakePacket((pPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nSeq,8,Datas);
611             SendPacket(nChn, (pPacket)PacketBuf1, len1);
612             ChnStats[nCurPollId].SendPackets++;
613             ChnStats[nCurPollId].SendTimeInterval=SendTimeuS-ChnStats[nCurPollId].LastSentTimeuS;
614             ChnStats[nCurPollId].LastSentTimeuS=SendTimeuS;
615 //            PacketLength = len1;
616             SendTime=tick1;
617
618             MasterRecved=0;
619             MasterRecvOK=0;
620         //    LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);        
621             //ToggleErrLed();
622 //                ToggleOut8();
623
624         }
625     
626 //        Clk3=SysTick->VAL;
627     //    LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_4);
628 //    HAL_Delay(1);                
629     return 0;
630 }
631
632 int KBusSlaveFunc(int nChn)
633 {
634         int ThisuS=GetuS();
635         int thisRecvTime=RecvTimeuS;
636         if (SlaveRecved)
637         {
638             KMem.RunStat=8000;
639             SlaveRecved=0;
640         }else if ((ThisuS - thisRecvTime) >12000u)
641         {
642             KMem.ErrStat=8000;
643             KMem.SDD[17]=1;
644             KMem.SDD[18]=ThisuS;
645             KMem.SDD[19]=RecvTimeuS;
646         }else if ( ThisuS > (thisRecvTime + 12000u))
647         {
648             KMem.ErrStat=8000;
649             KMem.SDD[17]=2;
650             KMem.SDD[18]=ThisuS;
651             KMem.SDD[19]=RecvTimeuS;
652         }
653         
654     return 0;
655 }