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