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