QuakeGod
2022-12-10 0fe6b074f3f3994d87af195f37e349a83e27882c
提交 | 用户 | 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"
d69aae 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
0fe6b0 20 stChnStat KBusChnStats[8];
Q 21 unsigned char nStationID=0;
e1f350 22 unsigned char nChilds;
bfc108 23 int ChildStat[16];
e1f350 24 unsigned char nCurPollId=0;
Q 25 unsigned char nSeq=0;
bfc108 26
0fe6b0 27 volatile unsigned char KBusMasterRecved=1;
Q 28 volatile unsigned char KBusMasterRecvOK=1;
e1f350 29
0fe6b0 30 volatile unsigned char KBusSlaveRecved=1;
Q 31 unsigned int KBusSendTimeuS=0;
32 volatile int KBusRecvTimeuS=0;
bfc108 33
0fe6b0 34 int KBusDelayuS=0;
Q 35 int KBusMaxDelayuS=0;
bfc108 36 int ThisuS;
Q 37 volatile unsigned int nSlaveTick=0;
d69aae 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
f4f290 50 unsigned char KBusBCC(void * pData, int nSize)
bfc108 51 {
Q 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;    
d69aae 59 }
Q 60
0fe6b0 61 int KBusMakePacket(pKBPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nSEQ, unsigned char DataLen,void * pData )
bfc108 62 {
Q 63     p1->Sign=StartSign;
e1f350 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);
0fe6b0 76             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 77             p1->data[DataLen+1]=EndSign;
Q 78         
0fe6b0 79             PacketLenth=sizeof(stKBPacket)+DataLen+1;
bfc108 80             break;
Q 81         case cmdPingReply:
82             p1->PacketLen=DataLen;
83             memcpy(p1->data,pData,DataLen);
0fe6b0 84             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 85             p1->data[DataLen+1]=EndSign;        
0fe6b0 86             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
bfc108 87             break;
Q 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);
0fe6b0 97             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 98             p1->data[DataLen+1]=EndSign;
0fe6b0 99             PacketLenth=sizeof(stKBPacket)+DataLen+1;                    
bfc108 100             break;
Q 101         case cmdGetVersion:
102             p1->PacketLen=DataLen;
103             memcpy(p1->data,pData,DataLen);
0fe6b0 104             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 105             p1->data[DataLen+1]=EndSign;
0fe6b0 106             PacketLenth=sizeof(stKBPacket)+DataLen+1;            
bfc108 107             break;
Q 108         case cmdVerInfo:
109             p1->PacketLen=DataLen;
110             memcpy(p1->data,pData,DataLen);
0fe6b0 111             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 112             p1->data[DataLen+1]=EndSign;
0fe6b0 113             PacketLenth=sizeof(stKBPacket)+DataLen+1;            
bfc108 114             break;
Q 115         case cmdExChgData:
116             p1->PacketLen=DataLen;
117             memcpy(p1->data,pData,DataLen);
0fe6b0 118             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 119             p1->data[DataLen+1]=EndSign;
0fe6b0 120             PacketLenth=sizeof(stKBPacket)+DataLen+1;
bfc108 121             break;
Q 122         case cmdExChgDataReply:
123             p1->PacketLen=DataLen;
124             memcpy(p1->data,pData,DataLen);
0fe6b0 125             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 126             p1->data[DataLen+1]=EndSign;        
0fe6b0 127             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
bfc108 128             break;
Q 129                 
130         case cmdSyncRead:
131             p1->PacketLen=DataLen;
132             memcpy(p1->data,pData,DataLen);
0fe6b0 133             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 134             p1->data[DataLen+1]=EndSign;        
0fe6b0 135             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
bfc108 136             break;
Q 137         case cmdSyncWrite:
138             p1->PacketLen=DataLen;
139             memcpy(p1->data,pData,DataLen);
0fe6b0 140             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 141             p1->data[DataLen+1]=EndSign;        
0fe6b0 142             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
bfc108 143             break;
Q 144         case cmdSequenRead:
145             p1->PacketLen=DataLen;
146             memcpy(p1->data,pData,DataLen);
0fe6b0 147             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 148             p1->data[DataLen+1]=EndSign;        
0fe6b0 149             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
bfc108 150             break;        
Q 151         
152         case cmdSyncTime:
153             p1->PacketLen=DataLen;
154             memcpy(p1->data,pData,DataLen);
0fe6b0 155             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 156             p1->data[DataLen+1]=EndSign;        
0fe6b0 157             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
bfc108 158             break;
Q 159         
160         default:
161             break;
162     }
163     
164     return PacketLenth;
165 }
166
0fe6b0 167 int KBusCheckPacket(int nChn, pKBPacket p1, int nLen1)
bfc108 168 {
Q 169     if (p1->Sign != StartSign)
170     {
171         Uart2Stat.NotPacketErr++;
0fe6b0 172         KBusChnStats[nCurPollId].NotPkgErr++;
bfc108 173         return -1;
Q 174     }
175     int DataLen=p1->PacketLen;
176     if (DataLen>MaxPacketLength) 
177     {
178         Uart2Stat.LengthErr++;
0fe6b0 179         KBusChnStats[nCurPollId].PkgLenErr++;
bfc108 180         return -1;
Q 181     }
0fe6b0 182     if (nLen1<DataLen+sizeof(stKBPacket)+1)
bfc108 183     {
0fe6b0 184         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
bfc108 185         //PutStr(str3,len4);
0fe6b0 186         KBusChnStats[nCurPollId].PkgLenErr++;
bfc108 187         Uart2Stat.LengthErr++;
Q 188         return -3;    //not long enough                    
189     }
190 //    if (p1->data[DataLen+1] != EndSign)
191 //    {
0fe6b0 192 //        KBusChnStats[nCurPollId].NoEndErr++;
bfc108 193 //        Uart2Stat.LengthErr++;        
Q 194 //        return -2;
195 //    }
0fe6b0 196     unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 197     if (thisBCC != p1->data[DataLen]) 
Q 198     {//BCC Error;
199         Uart2Stat.BCCerr++;
0fe6b0 200         KBusChnStats[nCurPollId].BCCErr++;
bfc108 201         return -4;
Q 202     }         
203     return 0;
204 }
205
0fe6b0 206 int KBusSlaveCheckPacket(int nChn, pKBPacket p1, int nLen1)
bfc108 207 {
Q 208     if (p1->Sign != StartSign)
209     {
210         Uart2Stat.NotPacketErr++;
0fe6b0 211         KBusChnStats[0].ClientNotPktErr++;
bfc108 212         return -1;
Q 213     }
214     int DataLen=p1->PacketLen;
215     if (DataLen>MaxPacketLength) 
216     {
217         Uart2Stat.LengthErr++;
0fe6b0 218         KBusChnStats[0].ClientPkgLenErr++;
bfc108 219         return -1;
Q 220     }
0fe6b0 221     if (nLen1<DataLen+sizeof(stKBPacket)+1)
bfc108 222     {
0fe6b0 223         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
bfc108 224         //PutStr(str3,len4);
0fe6b0 225         KBusChnStats[0].ClientPkgLenErr++;
bfc108 226         Uart2Stat.LengthErr++;
Q 227         return -3;    //not long enough                    
228     }
229 //    if (p1->data[DataLen+1] != EndSign)
230 //    {
0fe6b0 231 //        KBusChnStats[nCurPollId].NoEndErr++;
bfc108 232 //        Uart2Stat.LengthErr++;        
Q 233 //        return -2;
234 //    }
0fe6b0 235     unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
bfc108 236     if (thisBCC != p1->data[DataLen]) 
Q 237     {//BCC Error;
238         Uart2Stat.BCCerr++;
0fe6b0 239         KBusChnStats[0].ClientBccErr++;
bfc108 240         return -4;
Q 241     }         
242     return 0;
243 }
244
0fe6b0 245 int KBusMasterParsePacket(int nChn, pKBPacket p1, int Len1)
bfc108 246 {
Q 247
248         int DataLen=p1->PacketLen;
0fe6b0 249         KBusChnStats[nCurPollId].RecvPackets++;    
Q 250         pKBPacket p2=(pKBPacket)PacketBuf2;        
bfc108 251         int PacketLen=0;
Q 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];
f4f290 262                 PacketLen=KBusMakePacket(p2,0,nCurPollId,cmdPingReply,p1->nSEQ,DataLen,p1->data);
d69aae 263                 SendPacket(nChn, p2, PacketLen);
bfc108 264                 break;
Q 265             case cmdPingReply:
0fe6b0 266                 KBusDelayuS=ThisuS-KBusSendTimeuS;
Q 267                 if (KBusDelayuS > KBusMaxDelayuS) KBusMaxDelayuS = KBusDelayuS;
bfc108 268                 
Q 269                 BufferIn[ChildId]=p1->data[0];
270                 
271                 //RunStat=100;
0fe6b0 272                 KBusChnStats[nCurPollId].CtnLstPkts=0;
Q 273                 KBusChnStats[nCurPollId].Delay=KBusDelayuS;
274             if (KBusDelayuS > KBusChnStats[nCurPollId].MaxDelay) 
275                 KBusChnStats[nCurPollId].MaxDelay=KBusDelayuS;
bfc108 276                 //PutOutput(outputvalue);
0fe6b0 277                 KBusMasterRecvOK=1;
bfc108 278                 break;
Q 279             case cmdRead:
280                 break;
281             case cmdReadReply:
282                 break;
283             case cmdWrite:
284                 break;
285             case cmdWriteReply:
0fe6b0 286                 KBusMasterRecved=1;
bfc108 287                 break;
Q 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];
0fe6b0 297                 PacketLen=KBusMakePacket(p2,nStationID,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
d69aae 298                 SendPacket(nChn, p2, PacketLen);
bfc108 299                 break;
Q 300             case cmdExChgDataReply:
0fe6b0 301                 KBusDelayuS=ThisuS-KBusSendTimeuS;
Q 302                 if (KBusDelayuS > KBusMaxDelayuS) KBusMaxDelayuS = KBusDelayuS;
d69aae 303
a7db3c 304 #if (BOARD_TYPE == 14)                
d69aae 305                 BufferIn[ChildId]=p1->data[0];
Q 306                 KMem.WXB[ChildId-1]=BufferIn[ChildId];
307                 if (KMRunStat.WorkMode==0) {    
308                 //    KMem.WY[0]= KMem.WX[1]+(KMem.WX[2]<<8)    ;
309                 //    PutOutput (KMem.WY[0]);
310                 }
311 #else
bfc108 312                 BufferIn[ChildId]=p1->data[0];
e1f350 313                 KMem.WX[ChildId]=BufferIn[ChildId];
Q 314                 if (KMRunStat.WorkMode==0) {    
315                     KMem.WY[0]= KMem.WX[1]+(KMem.WX[2]<<8)    ;
316                     PutOutput (KMem.WY[0]);
317                 }
d69aae 318 #endif
Q 319
bfc108 320                 //RunStat=100;
0fe6b0 321                 KBusChnStats[nCurPollId].CtnLstPkts=0;
Q 322                 KBusChnStats[nCurPollId].Delay=KBusDelayuS;
323             if (KBusDelayuS > KBusChnStats[nCurPollId].MaxDelay) 
324                 KBusChnStats[nCurPollId].MaxDelay=KBusDelayuS;
bfc108 325                 //PutOutput(outputvalue);
Q 326                 
327             
328                 nIndex=p1->data[3];
0fe6b0 329                 KBusChnStats[nCurPollId].ClientDatas[nIndex]=p1->data[4]|(p1->data[5]<<8)|(p1->data[6]<<16)|(p1->data[7]<<24);
bfc108 330             
0fe6b0 331                 KBusMasterRecvOK=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 }
e1f350 345 unsigned char nClientDataIndex=0;
0fe6b0 346 int KBusSlaveParsePacket(int nChn, pKBPacket p1, int Len1)
bfc108 347 {
Q 348     Uart2Stat.OKPacket++;                
349     int DataLen=p1->PacketLen;    
350 //int nSrcAddr=p1->SrcAddr;
e1f350 351     int nDstHost=p1->DstHost;
bfc108 352         
0fe6b0 353 //    KBusRecvTimeuS=ThisuS;
Q 354 //    KBusSlaveRecved=1;
bfc108 355     
0fe6b0 356     pKBPacket p2=(pKBPacket)PacketBuf2;
bfc108 357     int PacketLen=0;
Q 358     unsigned char nIndex = p1->nSEQ & 0x07;
0fe6b0 359     if (nDstHost!=nStationID && nDstHost != 0xff)
bfc108 360     {
0fe6b0 361         KBusChnStats[0].ClientMisIdPkts++;
bfc108 362         return -1;
Q 363     }
0fe6b0 364     if (nDstHost==nStationID || nDstHost==0xff)
bfc108 365     {
0fe6b0 366         KBusRecvTimeuS=ThisuS;
Q 367         KBusSlaveRecved=1;
bfc108 368         switch (p1->nCMD)
Q 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];
0fe6b0 377                 KBusRecvTimeuS=ThisuS;
Q 378                 PacketLen=KBusMakePacket(p2,nStationID,0,cmdPingReply,p1->nSEQ,DataLen,p1->data);
379                 KBusChnStats[0].ClientSendPkts++;
d69aae 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);
f4f290 390                 PacketLen=KBusMakePacket(p2,1,0,cmdWriteReply,p1->nSEQ,0,0);
0fe6b0 391                 KBusChnStats[0].ClientSendPkts++;
d69aae 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)
d69aae 404             //    PutOutput(BufferIn[0]);
bfc108 405                 //PutOutput(outputvalue);
Q 406                 //memcpy(DispBuf,p1->data+2,8);
e1f350 407                 nIndex=nClientDataIndex;
0fe6b0 408         //        KBusChnStats[0].ClientDatas[7]++;
e1f350 409 //                BufferOut[0]=GetInput();
d69aae 410         //        BufferOut[0]=GetInput();
Q 411 #else
412                 PutOutput(BufferIn[0]);
413                 //PutOutput(outputvalue);
414                 //memcpy(DispBuf,p1->data+2,8);
415                 nIndex=nClientDataIndex;
0fe6b0 416         //        KBusChnStats[0].ClientDatas[7]++;
e1f350 417 //                BufferOut[0]=GetInput();
Q 418                 BufferOut[0]=GetInput();
d69aae 419 #endif
bfc108 420                 p1->data[0]=BufferOut[0];
Q 421                 p1->data[3]=nIndex;
0fe6b0 422                 p1->data[4]=KBusChnStats[0].ClientDatas[nIndex];
Q 423                 p1->data[5]=KBusChnStats[0].ClientDatas[nIndex]>>8;
424                 p1->data[6]=KBusChnStats[0].ClientDatas[nIndex]>>16;
425                 p1->data[7]=KBusChnStats[0].ClientDatas[nIndex]>>24;
e1f350 426                 nClientDataIndex++;
Q 427                 if (nClientDataIndex >= 10) { nClientDataIndex=0;}
0fe6b0 428                 PacketLen=KBusMakePacket(p2,nStationID,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
Q 429                 KBusChnStats[0].ClientSendPkts++;
d69aae 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 }
0fe6b0 451 int KBusParsePacket(int nChn, pKBPacket p1, int Len1)
bfc108 452 {
Q 453     ThisuS=GetuS();
454     int Result=0;
a7db3c 455     if (bKBusMaster)
bfc108 456     {
0fe6b0 457             KBusMasterRecved=1;
f4f290 458             Result=KBusCheckPacket(nChn, p1, Len1);
bfc108 459             if (Result != S_OK)
Q 460             {
461                 return Result;
462             }
0fe6b0 463             KBusMasterRecvOK=1;
f4f290 464             Result=KBusMasterParsePacket(nChn, p1, Len1);            
bfc108 465             return Result;
Q 466     }
a7db3c 467     if (bKBusSlave)
bfc108 468     {
0fe6b0 469             KBusChnStats[0].ClientRecvPkts++;
f4f290 470             Result=KBusSlaveCheckPacket(nChn, p1, Len1);
bfc108 471             if (Result != S_OK)
Q 472             {
473                 return Result;
474             }
0fe6b0 475             KBusChnStats[0].ClientTimeOutErr=KMem.RunStat;
f4f290 476             Result=KBusSlaveParsePacket(nChn, p1, Len1);
bfc108 477             return Result;
Q 478     }
479     //int len1=p1->PacketLen;
e1f350 480 //    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
0fe6b0 481 //    pKBPacket p2=(pKBPacket)PacketBuf2;
bfc108 482 //            Uart2Stat.OKPacket++;
Q 483
484     return Result;
485 }
f4f290 486 /*
bfc108 487 int InitMachine(stMachineConfig * pConfig)
Q 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 }
f4f290 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
0fe6b0 547         if ((KBusMasterRecved && KBusMasterRecvOK && thisuS-KBusSendTimeuS>50) || thisuS-KBusSendTimeuS>1000u)
a7db3c 548         {
0fe6b0 549             if (!KBusMasterRecvOK) 
a7db3c 550             {
Q 551                 TimeOutCount++;
552                 Uart2Stat.TimeOutErr++; 
0fe6b0 553                 KBusChnStats[nCurPollId].LostPackets++;
Q 554                 KBusChnStats[nCurPollId].CtnLstPkts++;
555                 if (!KBusMasterRecved) {KBusChnStats[nCurPollId].TimeOutErr++;}
556                 if (KBusChnStats[nCurPollId].CtnLstPkts>KBusChnStats[nCurPollId].MaxCtnLstPkts)
557                 {KBusChnStats[nCurPollId].MaxCtnLstPkts=KBusChnStats[nCurPollId].CtnLstPkts;}
558                 if (KBusChnStats[nCurPollId].CtnLstPkts>3)
a7db3c 559                 {
0fe6b0 560                     KBusChnStats[nCurPollId].Stat=0;
a7db3c 561                     KMem.ErrStat=200;
Q 562                      
563                     {BufferIn[nCurPollId]=0;}
564                 }
565             //    LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
566             }else
567             {
0fe6b0 568                 KBusChnStats[nCurPollId].Stat=1;
a7db3c 569                 
Q 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];;
0fe6b0 602             Datas[2]=KBusChnStats[nCurPollId].Stat;
a7db3c 603             Datas[3]=0;
Q 604             Datas[4]=tick1&0xff;
605             Datas[5]=(tick1>>8)&0xff;
606             Datas[6]=(tick1>>16)&0xff;
607             Datas[7]=(tick1>>24)&0xff;
608             
0fe6b0 609             KBusSendTimeuS=thisuS;                
Q 610             len1=KBusMakePacket((pKBPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nSeq,8,Datas);
611             SendPacket(nChn, (pKBPacket)PacketBuf1, len1);
612             KBusChnStats[nCurPollId].SendPackets++;
613             KBusChnStats[nCurPollId].SendTimeInterval=KBusSendTimeuS-KBusChnStats[nCurPollId].LastSentTimeuS;
614             KBusChnStats[nCurPollId].LastSentTimeuS=KBusSendTimeuS;
a7db3c 615 //            PacketLength = len1;
Q 616             SendTime=tick1;
617
0fe6b0 618             KBusMasterRecved=0;
Q 619             KBusMasterRecvOK=0;
a7db3c 620         //    LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);        
Q 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();
0fe6b0 635         int thisRecvTime=KBusRecvTimeuS;
Q 636         if (KBusSlaveRecved)
a7db3c 637         {
Q 638             KMem.RunStat=8000;
0fe6b0 639             KBusSlaveRecved=0;
a7db3c 640         }else if ((ThisuS - thisRecvTime) >12000u)
Q 641         {
642             KMem.ErrStat=8000;
643             KMem.SDD[17]=1;
644             KMem.SDD[18]=ThisuS;
0fe6b0 645             KMem.SDD[19]=KBusRecvTimeuS;
a7db3c 646         }else if ( ThisuS > (thisRecvTime + 12000u))
Q 647         {
648             KMem.ErrStat=8000;
649             KMem.SDD[17]=2;
650             KMem.SDD[18]=ThisuS;
0fe6b0 651             KMem.SDD[19]=KBusRecvTimeuS;
a7db3c 652         }
Q 653         
654     return 0;
655 }