QuakeGod
2024-08-06 7d8ba5df7d883c86c24aa38449c4a6dd126e920f
提交 | 用户 | 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
5dd1b7 12 //unsigned char bKBusMaster=0,bKBusSlave=0,bKBusRepeater=0;;
483170 13
5dd1b7 14 stKBusMem KBusMem;
483170 15
5dd1b7 16 int KBusLoadSavedConfig(stKBusDef * pKBus)
483170 17 {
5dd1b7 18     int iRet=0;
Q 19     
20     
21     return iRet;
483170 22 }
Q 23
5dd1b7 24 int KBusSaveConfig(int nChn)
483170 25 {
5dd1b7 26     int iRet=0;
Q 27     
28     return iRet;
29     
30 }
31
32 int KBusInitMaster(stKBusDef * pKBus,KBusSendPktFuncDef KBusSendPacket, int nChildCount)
33 {
34     int iRet =0;
35         pKBus->MyStat = KBusStatInited;
36         pKBus->bMaster = 1;
37         pKBus->bSlave = 0;
38         pKBus->bRepeater =0;
39         pKBus->nChildCount = nChildCount;
40         pKBus->nStationId = 0;        
41         pKBus->KBusEvCBFunc=0;
42         memset(pKBus->KBusChnStats,0,sizeof(pKBus->KBusChnStats));        
43         pKBus->KBusEvCBFunc=NULL;
44         pKBus->KBusSvCBFunc=NULL;
45         pKBus->KBusSendPacket = KBusSendPacket;
46         pKBus->nCurPollId = 1;
47     
842bb6 48     for (int i=0;i<MAX_CLIENT;i++)
Q 49     {
50         memset(&pKBus->KBusChnStats[i],0,sizeof(stChnStat));            
51     }    
52     for (int i=0;i<MAX_CLIENT;i++)
53     {
54         KBusMakeDefaultClildInfo(pKBus, i);        
55     }
56 /*    
57     stDeviceInfo * pDeviceInfo = &pKBus->DeviceInfos[1];
58
59     pDeviceInfo->DeviceType = 0;
60     pDeviceInfo->DeviceVer = 0;
61     pDeviceInfo->InBitCount = 16;
62     pDeviceInfo->OutBitCount =  16;
63     pDeviceInfo->AIWCount = 0;
64     pDeviceInfo->AQWCount = 0;
65     pDeviceInfo->DWStartAddr = 0;
66     pDeviceInfo->OutDWCount = 0;        
67 */
68     pKBus->KBusPort = (stPortDef){.nPortHardType = 4, .nPortUseType = 3, .bEnable = 1, .bRunning =1, .StationId = 0, .bMaster = 1, .nMaxStations = nChildCount,
69     .pInstance=pKBus , .ReqCommFunc = (CommFuncDef)KBusReqService};
70
71     KMRegisterPort(1,&pKBus->KBusPort);
5dd1b7 72     return iRet;
Q 73 };
74
75 int KBusInitSlave(stKBusDef * pKBus,KBusSendPktFuncDef KBusSendPacket, int nStationId, stDeviceInfo * pDeviceInfo)
76 {
77     int iRet =0;
78     pKBus->MyStat = KBusStatInited;
79         pKBus->bMaster = 0;
80         pKBus->bSlave = 1;
81         pKBus->bRepeater = 0;
82         pKBus->nChildCount = 0;
83         pKBus->nStationId = nStationId;    
84         pKBus->pMyDeviceInfo = pDeviceInfo;
85         pKBus->KBusEvCBFunc = NULL;
86         pKBus->KBusSvCBFunc=NULL;    
87         pKBus->KBusSendPacket = KBusSendPacket;
842bb6 88 /*    
5dd1b7 89     for (int i=0;i<MAX_CLIENT;i++)
483170 90     {
5dd1b7 91         memset(&pKBus->KBusChnStats[i],0,sizeof(stChnStat));            
842bb6 92     }    
Q 93 */
94     
95     pKBus->KBusPort = (stPortDef){.nPortHardType = 4, .nPortUseType = 3, .bEnable = 1, .bRunning =1, .StationId = nStationId, .bMaster = 0, .pInstance = pKBus};
96     KMRegisterPort(1,&pKBus->KBusPort);    
97
5dd1b7 98     return iRet;
Q 99 }
100
101
102 int KBusInit(stKBusDef * pKBus,KBusSendPktFuncDef KBusSendPacket, int bMaster, int nChildCount)
103 {
104     int iRet =0;
105     pKBus->MyStat = KBusStatInited;
106     if (bMaster ) {
107         KBusInitMaster(pKBus,KBusSendPacket,nChildCount);
108     }else {
109         KBusInitSlave(pKBus,KBusSendPacket,nChildCount,0);
110     }
111     //    memset(pKBus->KBusChnStats,0,sizeof(pKBus->KBusChnStats));
112     return iRet;
113     
114 }
115
116 int KBusSetEvCallBackFunc(stKBusDef * pKBus, KBusEvCBFuncDef CBFunc)
117 {
118
119     pKBus->KBusEvCBFunc = CBFunc;
120 //    pKBus->bKBusEvCBFuncSet=1;
121         return 0;
122 }
123
842bb6 124 //unsigned char tempdata [8] = {11,12,13,14,25,26,27,28};
Q 125 int KBusReqService (stKBusDef * pKBus, int ReqId, int nParam1, int nParam2, void **pData, unsigned short * len1)
126 {
127     
128     switch (ReqId) {
129         case ReqStartDiag:
130             break;
131         case ReqStopDiag:
132             break;
133         case ReqPortChnInfo:
134             *pData = &pKBus->KBusChnStats[nParam1];
135             *len1 = sizeof(stChnStat);            
136             break;        
137         case ReqPortChildInfo:
138             *pData = &pKBus->DeviceInfos[nParam1];
139             *len1 = sizeof(stDeviceInfo);
140
141 //            *pData = tempdata;
142 //            *len1 = sizeof(tempdata);
143             break;
144         case ReqBlinkLED:
145             if (pKBus->bReq == 1) return -1;
146             if (nParam1 != 0xff && nParam1> pKBus->nChildCount) return -2;
147             if (pKBus->bReq != 1 ) {
148                 pKBus->bReq=1;
149                 pKBus->nReqSvrId = ReqId;
150                 pKBus->nReqChildId = nParam1;
151                 pKBus->nReqParam = nParam2;
152                 pKBus->nReqReCount = 0;
153             }
154             break;
155             
156         case ReqTransBlink:
157             if (pKBus->bReq == 1) return -1;
158             if (nParam1 != 0xff && nParam1> pKBus->nChildCount) return -2;
159             if (pKBus->bReq != 1 ) {
160                 pKBus->bReq=1;
161                 pKBus->nReqSvrId = ReqId;
162                 pKBus->nReqChildId = nParam1;
163                 pKBus->nReqParam = nParam2;
164                 pKBus->nReqReCount = 0;                
165             }
166             break;
167
168         case ReqTransCfg:
169             break;
170         case ReqUpdateFirm:
171             if (pKBus->bReq == 1) return -1;
172             if (nParam1> pKBus->nChildCount) return -2;
173             if (pKBus->bReq != 1 ) {
174                 pKBus->bReq=1;
175                 pKBus->nReqSvrId = ReqId;
176                 pKBus->nReqChildId = nParam1;
177                 pKBus->nReqParam = nParam2;
178                 pKBus->nReqReCount = 0;
179                 if (*len1>0) {
180                     memcpy(pKBus->pReqDatas,*pData,*len1);
181                     pKBus->nReqDataLen = *len1;
182                     pKBus->nReqDataOff = 0;
183                 }
184             }                    
185         
186             break;
187         
188         case ReqTransFirmware:
189             break;
190         default:
191             if (pKBus->bReq == 1) return -1;
192             if (nParam1 != 0xff && nParam1> pKBus->nChildCount) return -2;
193             if (pKBus->bReq != 1 ) {
194                 pKBus->bReq=1;
195                 pKBus->nReqSvrId = ReqId;
196                 pKBus->nReqChildId = nParam1;
197                 pKBus->nReqParam = nParam2;
198                 pKBus->nReqReCount = 0;
199                 if (*len1>0) {
200                     memcpy(pKBus->pReqDatas,*pData,*len1);
201                     pKBus->nReqDataLen = *len1;
202                     pKBus->nReqDataOff = 0;
203                 }
204             }            
205             break;
206     }
207     return 0;
208 }
209
5dd1b7 210 int KBusStart(stKBusDef * pKBus)
Q 211 {
212     int iRet = 0;
213     
214     return iRet;
215 }
216
217 int KBusLoopProcess(stKBusDef * pKBus)
218 {
219     int iRet = 0;
220     if (pKBus->bMaster) 
221     {
842bb6 222         if (pKBus->nChildCount>0) KBusMasterFunc(&KBus1);
5dd1b7 223     }
Q 224     if (pKBus->bSlave)
225     {
842bb6 226         KBusSlaveFunc(&KBus1);    
5dd1b7 227     }
Q 228     if (pKBus->bRepeater)
229     {
230     }
842bb6 231         if (pKBus->RunStat) {pKBus->RunStat--;}
Q 232         if (pKBus->ErrStat) {pKBus->ErrStat--;}    
5dd1b7 233     return iRet;
Q 234 }
235
236 int KBusRepeaterFunc(stKBusDef * pKBus)
237 {
238         if ((KMem.nRunCount &0x7f) == 88) 
239         { 
240             ToggleRunLed();
241         }                
242     return 0;
243 }
244
842bb6 245 int KBusUpdateChildInfo(stKBusDef * pKBus, int nChild, stDeviceInfo * pInfoBlock)
5dd1b7 246 {
842bb6 247     pKBus->DeviceInfos[nChild] = *pInfoBlock;
5dd1b7 248     return 0;
Q 249 }
250
842bb6 251 int KBusMakeDefaultClildInfo(stKBusDef * pKBus, int nChild)
5dd1b7 252 {
842bb6 253 //    unsigned DefaultInBitCount = 8;
Q 254 //    unsigned DefaultOutBitCount = 8;
255     
256 //    stDeviceInfo * pDeviceInfo = &pKBus->DeviceInfos[nChild];
257     pKBus->DeviceInfos[nChild] = (stDeviceInfo){
258         .DeviceType=0,
259         .DeviceVer=0,
260         .InBitCount=8,
261         .OutBitCount=8,
262         .AIWCount=0,
263         .AQWCount=0,
264         .DWStartAddr=0,
265         .OutDWCount=0
266         };
5dd1b7 267
Q 268     return 0;
269 }
270
271 //int KBusSetChildCfg(int nChn, int nChild, )
272 int KBusAddChildAddrByInfo(stKBusDef * pKBus, int nChild, stDeviceInfo * pDeviceInfo)
273 {
274     int iRet = 0;
275         stClientCfg * pClientCfg = &pKBus->ClientCfg[nChild];
276     
277         pClientCfg->Addr = pKBus->nCurPollId;
278         pClientCfg->Configed = KBusConfiged;
279         pClientCfg->InStartAddrBit = pKBus->CurXBitAddr;
280         pClientCfg->OutStartAddrBit = pKBus->CurYBitAddr;
281         pClientCfg->AIWStartAddrByte = pKBus->CurAIWAddr;
282         pClientCfg->AQWStartAddrByte = pKBus->CurAQWAddr;
283         pClientCfg->DIWStartAddrByte = pKBus->CurDIWAddr;
284         pClientCfg->DOWStartAddrByte = pKBus->CurDOWAddr;
483170 285         
5dd1b7 286         pKBus->CurXBitAddr += pDeviceInfo->InBitCount;
Q 287         pKBus->CurYBitAddr += pDeviceInfo->OutBitCount;
288         pKBus->CurAIWAddr += pDeviceInfo->AIWCount; 
289         pKBus->CurAQWAddr += pDeviceInfo->AQWCount;
290     
291     return iRet;
292 }
293     
294
295 int KBusSearchChildProc(stKBusDef * pKBus)
296 {
297     int iRet = 0;
842bb6 298     uint32_t tick1=GetTick();
Q 299 //    uint32_t thisuS=GetuS();
300     int nThisPollId = pKBus->nCurPollId;
5dd1b7 301     if (pKBus->bMasterSent) {
Q 302         if (pKBus->bMasterRecved) {
842bb6 303                 pKBus->bMasterSent=0;            
Q 304                 pKBus->RetryCount=0;
305                 nThisPollId++;
306                 pKBus->nCurPollId = nThisPollId;
307                 if (nThisPollId > pKBus->nChildCount) 
5dd1b7 308                 {
Q 309                     // all query done, next stage 
842bb6 310                     pKBus->nCurPollId = 1;
5dd1b7 311                     pKBus->MyStat = KBusStatReady;
Q 312                     // CallBack
313                     if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvStateChange,0,0);
314                 }
842bb6 315         }
Q 316         
317         if (!pKBus->bMasterRecved && tick1- pKBus->SendTimeTick > KBUS_WAIT_TIME_OUT1) 
318         {
319 /*            
320             // timeOut,
321             if (pKBus->RetryCount <3) {
322                 pKBus->RetryCount++;
323                 // resent query packet for curPollId;    
324                 pKBus->bMasterSent=0;  // no wait any more;
325 //            pKBus->bMasterSent=1;
326 //            pKBus->SendTimeTick = tick1;                
327             }
328             if (pKBus->RetryCount >3 ) 
329 // */
330             {
331                 // Set Cur Child As Default
332                 KBusMakeDefaultClildInfo(pKBus,nThisPollId);
333                 KBusAddChildAddrByInfo(pKBus,nThisPollId,&pKBus->DeviceInfos[nThisPollId]);
334                 
335                 // Next Child;
336                 pKBus->RetryCount=0;
337                 nThisPollId++;
338                 pKBus->nCurPollId = nThisPollId;
339                 if (nThisPollId > pKBus->nChildCount) 
340                 {
341                     // all query done, next stage 
342                     pKBus->nCurPollId = 1;
343                     pKBus->MyStat = KBusStatReady;
344                     // CallBack
345                     if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvStateChange,0,0);
346                 }
347                 pKBus->bMasterSent=0;          
5dd1b7 348             }
Q 349             
350         }
842bb6 351     }else {  // ! pKBus->bMasterSent  还没有发送
Q 352         // Sent Query Packet for CurPollId;
353         //pKBus->nCurPollId;
354         
355             unStatus ThisStatus;
356             ThisStatus.nSeq = pKBus->nSeq;
357             ThisStatus.nErr1 = (pKBus->KBusChnStats[nThisPollId].MStat==0);
358         
359             int len1=KBusMakePacket((pKBPacket)pKBus->PacketBuf2,0,nThisPollId,cmdQuery,ThisStatus.nStatus,0,NULL);
360 //            LL_USART_SetBaudRate(USART2,48000000,LL_USART_OVERSAMPLING_8,DefaultUart2Baud);
361             pKBus->KBusSendPacket(pKBus->PacketBuf2, len1);
362             pKBus->KBusChnStats[nThisPollId].SendPackets++;
363             pKBus->KBusChnStats[nThisPollId].SendTimeInterval=pKBus->SendTimeTick-pKBus->KBusChnStats[nThisPollId].LastSentTimeTick;
364             pKBus->KBusChnStats[nThisPollId].LastSentTimeTick=pKBus->SendTimeTick;
365 //            PacketLength = len1;
366             pKBus->SendTime=tick1;
367
368             pKBus->bMasterRecved=0;
369             pKBus->bMasterRecvOK=0;        
370         
371         pKBus->bMasterSent=1;
372         pKBus->SendTimeTick = tick1;
5dd1b7 373     }
842bb6 374     
Q 375     
376 //    pKBus->MyStat = KBusStatReady;
5dd1b7 377
Q 378     return iRet;
379 }
380
842bb6 381 int KBusPacketSendDone(stKBusDef * pKBus)
Q 382 {
383     switch (pKBus->MyStat){
384         case KBusStatUnInited:
385             break;
386         case KBusStatInited:
387             break;
388         case KBusStatConfig:
389             break;
390         case KBusStatReady:
391             break;
392         case KBusStatRunning:
393             switch(pKBus->nRunStep) {
394                 case KBusRunStepBroadCastCfg:
395                     pKBus->nRunStep = KBusRunStepTimeSync;
396                     break;
397                 case    KBusRunStepTimeSync:
398                     pKBus->nRunStep = KBusRunStepMultiDataPoll;
399                     break;
400                 case    KBusRunStepMultiDataPoll:
401                     pKBus->nRunStep = KBusRunStepUniDataPoll;
402                     break;
403                 case    KBusRunStepUniDataPoll:
404                     
405                     break;
406                 case    KBusRunStepUniDataTranster:
407                     
408                     break;
409                 default:
410                     break;
411             }
412 //            KBusDataPoll(pKBus);
413             break;
414         default:
415             break;
416     }    
417     return 0;
418 }
419
420
421
422 int GoStep(stKBusDef * pKBus, enKBusRunStep NextStep)
423 {
424         pKBus->nRunStep = NextStep;
425         pKBus->TimeOutCount=0;
426         return 0;
427 }
5dd1b7 428
Q 429 int KBusMasterFunc(stKBusDef * pKBus)
430 {
842bb6 431     int len1;
Q 432     uint32_t tick1=GetTick();    
433     ushort DataLen = 0;
5dd1b7 434     switch (pKBus->MyStat){
Q 435         case KBusStatUnInited:
436             KBusLoadSavedConfig(pKBus);
437             pKBus->MyStat = KBusStatInited;
842bb6 438             pKBus->TimeOutCount=0;        
483170 439             break;
5dd1b7 440         case KBusStatInited:
842bb6 441             pKBus->TimeOutCount++;
Q 442             if (pKBus->TimeOutCount>30){        
5dd1b7 443             pKBus->MyStat = KBusStatConfig;
842bb6 444                         pKBus->TimeOutCount    = 0;                        
Q 445             }
483170 446             break;
5dd1b7 447         case KBusStatConfig:
Q 448             KBusSearchChildProc(pKBus);
483170 449             break;
5dd1b7 450         case KBusStatReady:
Q 451             pKBus->MyStat = KBusStatRunning;
842bb6 452             pKBus->nRunStep = KBusRunStepBroadCastCfg;
Q 453             pKBus->TimeOutCount=0;
5dd1b7 454             break;
Q 455         case KBusStatRunning:
842bb6 456             switch(pKBus->nRunStep) {
Q 457                 case KBusRunStepBroadCastCfg:
458                     pKBus->TimeOutCount++;
459                     if (pKBus->TimeOutCount>4){
460                         pKBus->nRunStep = KBusRunStepTimeSync;
461                         pKBus->TimeOutCount    = 0;
462                     }
463                     break;
464                 case    KBusRunStepTimeSync:
465                     pKBus->Datas[0]=tick1&0xff;
466                     pKBus->Datas[1]=(tick1>>8)&0xff;
467                     pKBus->Datas[2]=(tick1>>16)&0xff;
468                     pKBus->Datas[3]=(tick1>>24)&0xff;
469                     len1=KBusMakePacket((pKBPacket)pKBus->PacketBuf2,0,0xff,cmdSyncTime,pKBus->nSeq,4,pKBus->Datas);
470                     pKBus->KBusSendPacket(pKBus->PacketBuf2, len1);    
471                 
472                     pKBus->nRunStep = KBusRunStepTimeSyncWait;
473                     pKBus->TimeOutCount=0;
474                     break;
475                 case    KBusRunStepTimeSyncWait:
476                     pKBus->TimeOutCount++;
477                     if (pKBus->TimeOutCount>2){
478                         pKBus->nRunStep = KBusRunStepMultiDataPoll;
479                         pKBus->TimeOutCount=0;
480                     }
481                     break;
482                 case    KBusRunStepMultiDataPoll:
483                     pKBus->TimeOutCount++;
484                     if (pKBus->TimeOutCount>1){
485                         pKBus->nCurPollId = 1;
486                         pKBus->nRunStep = KBusRunStepUniDataPoll;
487                         pKBus->TimeOutCount=0;
488                     }
489                     break;
490                 case    KBusRunStepUniDataPoll:
491                             KBusDataPoll(pKBus);
492                     break;
493                 case    KBusRunStepUniDataTranster:
494                         // 看是否有远程请求 
495                     if (pKBus->bReq &&pKBus->nReqReCount<1)
496                     {
497                         pKBus->nReqDataHdr[0]=0;
498                         pKBus->nReqDataHdr[1]=0;
499                         pKBus->nReqDataHdr[2]=pKBus->nReqSvrId;
500                         pKBus->nReqDataHdr[3]=pKBus->nReqParam;
501                         // [4]            Addr
502                         //  [5]            Addr
503                         // [6]            nCount
504                         DataLen = pKBus->nReqDataLen+2;
505                         len1=KBusMakePacket((pKBPacket)pKBus->PacketBuf2,0,pKBus->nReqChildId,cmdRemoteReq,pKBus->nSeq,DataLen,&pKBus->nReqDataHdr[2]);
506                         pKBus->KBusSendPacket(pKBus->PacketBuf2, len1);    
507                         pKBus->nReqReCount++;
508                     }else {
509                         
510                     }
511                         pKBus->nRunStep = KBusRunStepUniDataTransterWait;
512                         pKBus->TimeOutCount=0;
513                     break;
514                 case KBusRunStepUniDataTransterWait:
515                     pKBus->TimeOutCount++;
516                     if (pKBus->TimeOutCount>4){
517                         if (pKBus->bReq ==1) {pKBus->bReq = 3;}
518                         pKBus->nRunStep = KBusRunStepBroadCastCfg;
519                         pKBus->TimeOutCount=0;
520                         
521                     }
522                     break;
523                 default:
524                     break;
525             }
5dd1b7 526             break;
483170 527         default:
Q 528             break;
529     }
5dd1b7 530     return 0;
483170 531 }
Q 532
5dd1b7 533 int KBusDataPoll(stKBusDef * pKBus)
Q 534 {
535     int iRet = 0;
842bb6 536     uint32_t tick1=GetTick();
Q 537 //    uint32_t thisuS=GetuS();    
5dd1b7 538         int len1=0;
Q 539     int nThisPollId = pKBus->nCurPollId;
540     
541     stChnStat * pChnStat = &pKBus->KBusChnStats[nThisPollId];
842bb6 542     if (!pKBus->bMasterSent){
Q 543             pKBus->Datas[0]=KBusMem.WLYB[nThisPollId -1 ];
544                 for (int i=1; i*8 < pKBus->DeviceInfos[nThisPollId].OutBitCount;i++){
545                         pKBus->Datas[0+i]=KBusMem.WLYB[nThisPollId -1 + i];
546                 }
547                 
548 //            pKBus->Datas[1]=KBusMem.WLYB[nThisPollId ];;
549 //            pKBus->Datas[2]=KBusMem.WLYB[nThisPollId + 1 ]; //KBusChnStats[nCurPollId].Stat;
550 //            pKBus->Datas[3]=KBusMem.WLYB[nThisPollId + 2 ];
551             pKBus->Datas[4]=tick1&0xff;
552             pKBus->Datas[5]=(tick1>>8)&0xff;
553             pKBus->Datas[6]=(tick1>>16)&0xff;
554             pKBus->Datas[7]=(tick1>>24)&0xff;
555             
556             pKBus->SendTimeTick=tick1;
557             unStatus ThisStatus;
558             ThisStatus.nStatus= pChnStat->MStat;
559             ThisStatus.nSeq = pKBus->nSeq;
560             //ThisStatus.nErr1 = (pChnStat->MStat==0);
561 //            ThisStatus.nErr1 = 0;
562             len1=KBusMakePacket((pKBPacket)pKBus->PacketBuf2,0,nThisPollId,cmdUniExChgData,ThisStatus.nStatus,8,pKBus->Datas);
563 //            LL_USART_SetBaudRate(USART2,48000000,LL_USART_OVERSAMPLING_8,DefaultUart2Baud);
564             
565             pKBus->KBusSendPacket(pKBus->PacketBuf2, len1);
566             pChnStat->SendPackets++;
567             pChnStat->SendTimeInterval=pKBus->SendTimeTick - pChnStat->LastSentTimeTick;
568             pChnStat->LastSentTimeTick=pKBus->SendTimeTick;
569 //            PacketLength = len1;
570             pKBus->SendTime=tick1;
571             pKBus->bMasterSent = 1;
572             pKBus->bMasterRecved=0;
573             pKBus->bMasterRecvOK=0;
574         //    LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);        
575             //ToggleErrLed();
576 //                ToggleOut8();
577     }else 
578     {
579     
580         if ((pKBus->bMasterRecved && pKBus->bMasterRecvOK && tick1-pKBus->SendTimeTick>1) || tick1-pKBus->SendTimeTick>KBUS_WAIT_TIME_OUT2)
5dd1b7 581         {
Q 582             if (!pKBus->bMasterRecvOK) 
583             {
584                 pKBus->TimeOutCount++;
842bb6 585 //                Uart2Stat.TimeOutErr++; 
5dd1b7 586                 pChnStat->LostPackets++;
Q 587                 pChnStat->CtnLstPkts++;
588                 if (!pKBus->bMasterRecved) {pChnStat->TimeOutErr++;}
589                 if (pChnStat->CtnLstPkts > pChnStat->MaxCtnLstPkts)
590                 {pChnStat->MaxCtnLstPkts = pChnStat->CtnLstPkts;}
591                 if (pChnStat->CtnLstPkts>3)
592                 {
842bb6 593                     pChnStat->MStat = 0;
5dd1b7 594                     pKBus->ErrStat=200;
Q 595                     KBusMem.WLXB[nThisPollId]=0;
596                     if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvStateChange,0,0);
597                     if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvDataUpdate,0,0);
598                     
599                 }
600             //    LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
601             }else
602             {
842bb6 603             //    pChnStat->CtnLstPkts = 0;
Q 604             //    pKBus->KBusChnStats[nThisPollId].CtnLstPkts=0;
605                 pChnStat->MStat=1;
5dd1b7 606                 
Q 607                 pKBus->RunStat=100;
608             }
609             if (pKBus->DeviceInfos[nThisPollId].InBitCount >8 ||  pKBus->DeviceInfos[nThisPollId].OutBitCount > 8) {
842bb6 610                 nThisPollId += 2;
Q 611                 //nThisPollId ++;
5dd1b7 612             }else {
Q 613                 nThisPollId ++;
614             }
615             if (nThisPollId > pKBus->nChildCount)
616             {
617                 // 数据轮询完, 周期间隙,插入其他处理数据.  每次处理的时间间隙, 一问 一答  时间.
618                 // 分成几种类型, 轮流进行
619                 //    时间同步,            查询新子机,        处理带外数据,            ,处理额外的事情,            或者跳过.
842bb6 620                 pKBus->CircleTime=tick1-pKBus->LastCircleStartTime;
Q 621                 pKBus->LastCircleStartTime=tick1;
5dd1b7 622                 pKBus->nSeq++;
842bb6 623                 GoStep(pKBus,KBusRunStepUniDataTranster);
Q 624                 //nThisPollId=1;
625             }else {
626                 pKBus->nCurPollId = nThisPollId;
5dd1b7 627             }
842bb6 628             pKBus->bMasterSent = 0;            
5dd1b7 629         }    
842bb6 630     }
5dd1b7 631     return iRet;
Q 632 }
633
634 int KBusSlaveFunc(stKBusDef * pKBus)
635 {
842bb6 636 //        int ThisuS=GetuS();
Q 637         int ThisTick = GetTick();
638         int thisRecvTime=pKBus->RecvTimeTick;
5dd1b7 639      if (pKBus->nStationId >0) {
Q 640             if (pKBus->bSlaveRecved)
641             {
642                 pKBus->RunStat=8000;
643                 pKBus->bSlaveRecved=0;
842bb6 644             }else if ((ThisTick - thisRecvTime) > KBUS_SLAVE_TIME_OUT) // 30u
5dd1b7 645             {
842bb6 646                 pKBus->ErrStat=7000;
Q 647             }else if ( ThisTick > (thisRecvTime + KBUS_SLAVE_TIME_OUT)) // 30u
5dd1b7 648             {
842bb6 649                 pKBus->ErrStat=7100;
5dd1b7 650             }
Q 651     }
652     return 0;
653 }
654
655 int KBusCheckPacket(stKBusDef * pKBus, pKBPacket p1, int nLen1)
483170 656 {
Q 657     unsigned char * p2 = (unsigned char *)p1;
658     pKBPacket p3=p1;
5dd1b7 659     if ((p2[0] == KB_SYN || p2[0] != KBStartSign) && nLen1 > 1)
483170 660     {
Q 661         p3=(pKBPacket)(p2+1);
662         nLen1--;
663     }    
664     
5dd1b7 665     if (p3->Sign != KBStartSign)
483170 666     {
842bb6 667 //        pKBus->NotPacketErr++;
Q 668                 KMem.WDB[0x40]=pKBus->nCurPollId;    
669                 KMem.WDB[0x41]=nLen1;
670                 memcpy(&KMem.WDB[0x42],p1,nLen1);
671         
5dd1b7 672         pKBus->KBusChnStats[pKBus->nCurPollId].NotPkgErr++;
Q 673         if (pKBus->KBusDiagnosis) {
674             if (pKBus->KBusSnapPos == 0) {
483170 675                 KMem.WDB[0x20]=nLen1;
Q 676                 memcpy(&KMem.WDB[0x21],p1,nLen1);
5dd1b7 677                 pKBus->KBusSnapPos++;
Q 678             } else if (pKBus->KBusSnapPos==1) {
483170 679                 KMem.WDB[0x40]=nLen1;
Q 680                 memcpy(&KMem.WDB[0x41],p1,nLen1);
5dd1b7 681                 pKBus->KBusSnapPos++;
483170 682             } else {
Q 683                 KMem.WDB[0x60]=nLen1;
684                 memcpy(&KMem.WDB[0x61],p1,nLen1);
5dd1b7 685                 pKBus->KBusSnapPos=0;                
483170 686             }
Q 687             
688         }
689         return -1;
690     }
eaf5d5 691     int DataLen=p3->DataLen;
5dd1b7 692     if (DataLen>KBMaxPacketLength) 
483170 693     {
842bb6 694 //        Uart2Stat.LengthErr++;
5dd1b7 695         pKBus->KBusChnStats[pKBus->nCurPollId].PkgLenErr++;
483170 696         return -1;
Q 697     }
698     if (nLen1<DataLen+sizeof(stKBPacket)+1)
699     {
700         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
701         //PutStr(str3,len4);
5dd1b7 702         pKBus->KBusChnStats[pKBus->nCurPollId].PkgLenErr++;
842bb6 703 //        Uart2Stat.LengthErr++;
483170 704         return -3;    //not long enough                    
Q 705     }
706 //    if (p3->data[DataLen+1] != EndSign)
707 //    {
708 //        KBusChnStats[nCurPollId].NoEndErr++;
709 //        Uart2Stat.LengthErr++;        
710 //        return -2;
711 //    }
712     unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
713     if (thisBCC != p3->data[DataLen]) 
714     {//BCC Error;
842bb6 715 //        Uart2Stat.BCCerr++;
5dd1b7 716         pKBus->KBusChnStats[pKBus->nCurPollId].BCCErr++;
483170 717         return -4;
Q 718     }         
719     return 0;
720 }
721
5dd1b7 722 int KBusSlaveCheckPacket(stKBusDef * pKBus, pKBPacket p1, int nLen1)
483170 723 {
Q 724     unsigned char * p2 = (unsigned char *)p1;
725     pKBPacket p3=p1;
5dd1b7 726     if ((p2[0] == KB_SYN || p2[0] != KBStartSign) && nLen1 > 1)
483170 727     {
Q 728         p3=(pKBPacket)(p2+1);
729         nLen1--;
730     }    
731     
5dd1b7 732     if (p3->Sign != KBStartSign)
483170 733     {
842bb6 734 //        Uart2Stat.NotPacketErr++;
5dd1b7 735         pKBus->KBusChnStats[0].ClientNotPktErr++;
483170 736         return -1;
Q 737     }
eaf5d5 738     int DataLen=p3->DataLen;
5dd1b7 739     if (DataLen>KBMaxPacketLength) 
483170 740     {
842bb6 741 //        Uart2Stat.LengthErr++;
5dd1b7 742         pKBus->KBusChnStats[0].ClientPkgLenErr++;
483170 743         return -1;
Q 744     }
745     if (nLen1<DataLen+sizeof(stKBPacket)+1)
746     {
747         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
748         //PutStr(str3,len4);
5dd1b7 749         pKBus->KBusChnStats[0].ClientPkgLenErr++;
842bb6 750 //        Uart2Stat.LengthErr++;
483170 751         return -3;    //not long enough                    
Q 752     }
753 //    if (p3->data[DataLen+1] != EndSign)
754 //    {
755 //        KBusChnStats[nCurPollId].NoEndErr++;
756 //        Uart2Stat.LengthErr++;        
757 //        return -2;
758 //    }
759     unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
760     if (thisBCC != p3->data[DataLen]) 
761     {//BCC Error;
842bb6 762 //        Uart2Stat.BCCerr++;
5dd1b7 763         pKBus->KBusChnStats[0].ClientBccErr++;
483170 764         return -4;
Q 765     }         
766     return 0;
767 }
768
5dd1b7 769 int KBusMasterParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1)
483170 770 {
Q 771     unsigned char * p6 = (unsigned char *)p1;
772
5dd1b7 773     if ((p6[0] == KB_SYN || p6[0] != KBStartSign) && Len1 > 1)
483170 774     {
Q 775         p1=(pKBPacket)(p6+1);
776         Len1--;
777     }    
778
eaf5d5 779         int DataLen=p1->DataLen;
5dd1b7 780         pKBus->KBusChnStats[pKBus->nCurPollId].RecvPackets++;    
Q 781 //        pKBPacket p2=(pKBPacket)PacketBuf2;        
782 //        int PacketLen=0;
483170 783         //LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_6);
5dd1b7 784         int nCurPollId = pKBus->nCurPollId;
483170 785         int ChildId=p1->SrcAddr;
842bb6 786 //        int ThisuS = GetuS();
Q 787         int ThisTick = GetTick();
483170 788         unsigned char nIndex;
Q 789         switch (p1->nCMD)
790         {
791             
792             case cmdNone:
793                 break;
5dd1b7 794             case cmdQueryRply:
Q 795                 DataLen=sizeof(stDeviceInfo);
842bb6 796                 KBusUpdateChildInfo(pKBus,nCurPollId,(stDeviceInfo *)&p1->data[0]);
Q 797                 pKBus->KBusChnStats[nCurPollId].ClientSendPkts++;
5dd1b7 798                 break;
Q 799             case cmdSetCfgRply:
800                 break;
801             case cmdToRunModeRply:
802                 break;
803             case cmdBroadCastCfg:
804                 break;
805             case cmdMuExchgDataRply:
806                 break;
807             case cmdToSafeMode:
808                 break;
809             case cmdReHeartBeat:
810                 break;
811             
483170 812             case cmdPing:
Q 813                 break;
814             case cmdPingReply:
842bb6 815                 pKBus->DelayTick = ThisTick - pKBus->SendTimeTick;
Q 816                 if (pKBus->DelayTick > pKBus->nMaxDelayTick) pKBus->nMaxDelayTick = pKBus->DelayTick;
483170 817                 
5dd1b7 818                 KBusMem.WLXB[ChildId]=p1->data[0];
483170 819                 
Q 820                 //RunStat=100;
842bb6 821                 pKBus->KBusChnStats[nCurPollId].CtnLstPkts=0;
Q 822                 pKBus->KBusChnStats[nCurPollId].Delay=pKBus->DelayTick;
823             if (pKBus->DelayTick > pKBus->KBusChnStats[nCurPollId].MaxDelay) 
824                 pKBus->KBusChnStats[nCurPollId].MaxDelay=pKBus->DelayTick;
483170 825                 //PutOutput(outputvalue);
5dd1b7 826                 pKBus->bMasterRecvOK=1;
483170 827                 break;
Q 828             case cmdRead:
829                 break;
830             case cmdReadReply:
831                 break;
832             case cmdWrite:
833                 break;
834             case cmdWriteReply:
5dd1b7 835                 pKBus->bMasterRecved=1;
483170 836                 break;
Q 837             case cmdGetVersion:
838                 break;
839             case cmdVerInfo:
840                 break;
5dd1b7 841             case cmdUniExChgData:
Q 842         
483170 843                 break;
Q 844             case cmdExChgDataReply:
842bb6 845                 pKBus->DelayTick = ThisTick - pKBus->SendTimeTick;
Q 846                 if (pKBus->DelayTick > pKBus->nMaxDelayTick) pKBus->nMaxDelayTick = pKBus->DelayTick;
847                     KBusMem.WLXB[ChildId - 1 ]=p1->data[0];
848                 for (int i=1; i*8 < pKBus->DeviceInfos[ChildId].InBitCount;i++){
849                     KBusMem.WLXB[ChildId - 1 + i]=p1->data[0 + i];;
850                 }
851 //                KBusMem.WLXB[ChildId+0]=p1->data[1];
852 //                KBusMem.WLXB[ChildId+1]=p1->data[2];
853 //                KBusMem.WLXB[ChildId+2]=p1->data[3];;
854 //                KBusMem.WLXB[ChildId+3]=p1->data[4];
855 //                KBusMem.WLXB[ChildId+4]=p1->data[5];
856 //                KBusMem.WLXB[ChildId+5]=p1->data[6];
857 //                KBusMem.WLXB[ChildId+6]=p1->data[7];            
5dd1b7 858             //Call Back 
Q 859                 if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvDataUpdate,0,0);
483170 860                 //RunStat=100;
5dd1b7 861                 pKBus->KBusChnStats[nCurPollId].CtnLstPkts=0;
842bb6 862                 pKBus->KBusChnStats[nCurPollId].Delay=pKBus->DelayTick;
Q 863             if (pKBus->DelayTick > pKBus->KBusChnStats[nCurPollId].MaxDelay) 
864                 pKBus->KBusChnStats[nCurPollId].MaxDelay=pKBus->DelayTick;
483170 865                 //PutOutput(outputvalue);
Q 866                 
5dd1b7 867               if (DataLen>=14) {
Q 868                     nIndex=p1->data[9];
869                     pKBus->KBusChnStats[nCurPollId].ClientDatas[nIndex]=p1->data[10]|(p1->data[11]<<8)|(p1->data[12]<<16)|(p1->data[13]<<24);
870                 }
871                 pKBus->bMasterRecvOK=1;
483170 872                 
Q 873                 break;
842bb6 874             case cmdRemoteReqReply:
Q 875                 pKBus->bReq = 2;
876             
877                 break;
483170 878             default:
Q 879                 break;        
880         }
881     //    nCurPollId ++;
882     //    if (nCurPollId > nChilds)
883     //    {
884     //        nCurPollId=1;
885     //    }    
886     return 0;
887 }
888
5dd1b7 889 int KBusSlaveParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1)
483170 890 {
Q 891     unsigned char * p3 = (unsigned char *)p1;
5dd1b7 892     if (p3[0] == KB_SYN && Len1 > 1)
483170 893     {
Q 894         p1=(pKBPacket)(p3+1);
895         Len1--;
896     }        
897     
842bb6 898 //    Uart2Stat.OKPacket++;                
Q 899     unsigned short Addr;    
900     unsigned short DataLen=p1->DataLen;    
483170 901 //int nSrcAddr=p1->SrcAddr;
Q 902     int nDstHost=p1->DstHost;
903         
904 //    KBusRecvTimeuS=ThisuS;
905 //    KBusSlaveRecved=1;
906     
5dd1b7 907     pKBPacket p2=(pKBPacket)pKBus->PacketBuf2;
842bb6 908     void * pData = 0;
483170 909     int PacketLen=0;
Q 910     unsigned char nIndex;// = p1->nStatus & 0x07;
5dd1b7 911     if (nDstHost!=pKBus->nStationId && nDstHost != 0xff)
483170 912     {
5dd1b7 913         pKBus->KBusChnStats[0].ClientMisIdPkts++;
483170 914         return -1;
Q 915     }
842bb6 916 //    int ThisuS = GetuS();
Q 917     int ThisTick = GetTick();
918     if (nDstHost==0xff){
919         pKBus->RecvTimeTick=ThisTick;
920         pKBus->bSlaveRecved=1;
921         switch (p1->nCMD)
922         {
923             case cmdNone:
924                 break;
925             case cmdBroadCastCfg:
926                 
927                 break;
928             case cmdMuExchgData:
929                 
930                 break;
931             case cmdToSafeMode:
932                 
933                 break;
934             case cmdHeartBeat:
935                 break;
936             case cmdSyncRead:
937                 break;
938             case cmdSyncWrite:
939                 break;
940             case cmdSequenRead:
941                 break;
942             case cmdSyncTime:
943                 pKBus->nSlaveTick=p1->data[0]+(p1->data[1]<<8)+(p1->data[2]<<16)+(p1->data[3]<<24);
944                 if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvTimeSync,0,0);            
945                 break;
946             case cmdRemoteReq:            //收到Remote请求
947 //                KBusSlaveRunRemoteReq(pKBus,p1->data[0],p1->data, DataLen);
948                     // data[0] -> reqSvr;
949                     // data[1] -> param;
950                     switch (p1->data[0]) {
951                         case ReqBlinkLED:
952                         case ReqTransBlink:
953                             KMRunStat.bLEDFlick=p1->data[1];
954                             DataLen = 0;
955                             PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data);        
956                             pKBus->KBusSendPacket((uchar *)p2, PacketLen);
957                             break;
958                         case ReqUpdateFirm:        //37个字节
959                             DataLen = 0;
960                             PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data);        
961                             pKBus->KBusSendPacket((uchar *)p2, PacketLen);
962                             pData = &p1->data[5];
963                             Addr = p1->data[2] + (p1->data[3] <<8);
964                             DataLen = p1->data[4] ;
965                             KMRunService(p1->data[0],Addr,0,&pData,&DataLen);
966                             DataLen=0;
967                             break;
968                         case ReqUpdateFirmInfo:
969                             DataLen = 0;
970                             PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data);        
971                             pKBus->KBusSendPacket((uchar *)p2, PacketLen);
972                             pData = &p1->data[5];
973                             Addr = p1->data[2] + (p1->data[3] <<8);
974                             DataLen = p1->data[4] ;
975                             KMRunService(p1->data[0],Addr,0,&pData,&DataLen);
976                             DataLen=0;
977                             break;
978                         default:
979                             pData = &p1->data[5];
980                             Addr = p1->data[2] + (p1->data[3] <<8);
981                             DataLen = p1->data[4] ;
982                             KMRunService(p1->data[0],p1->data[1],0,&pData,&DataLen);
983                             DataLen = 0;
984                             PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data);        
985                             pKBus->KBusSendPacket((uchar *)p2, PacketLen);
986                             DataLen=0;
987
988                             break;
989                     }            
990                 break;            
991             default:
992                 break;        
993         }    
994     
995     }
996     
997     if (nDstHost==pKBus->nStationId)
483170 998     {
842bb6 999         pKBus->RecvTimeTick=ThisTick;
5dd1b7 1000         pKBus->bSlaveRecved=1;
483170 1001         switch (p1->nCMD)
Q 1002         {
1003             case cmdNone:
1004                 break;
5dd1b7 1005             case cmdQuery:
Q 1006                 DataLen=sizeof(stDeviceInfo);
1007                 PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdQueryRply,p1->nStatus,DataLen,pKBus->pMyDeviceInfo);
1008                 pKBus->KBusChnStats[0].ClientSendPkts++;
1009                 pKBus->KBusSendPacket((uchar *)p2, PacketLen);
1010                 break;
1011             case cmdSetCfg:
1012                 break;
1013             case cmdToRunMode:
1014                 break;
1015             case cmdBroadCastCfg:
1016                 break;
1017             case cmdMuExchgData:
1018                 break;
1019             case cmdToSafeMode:
1020                 break;
1021             case cmdHeartBeat:
1022                 break;
1023             
483170 1024             case cmdPing:
5dd1b7 1025                 KBusMem.WLYB[0]=p1->data[0];
483170 1026                 //PutOutput(outputvalue);
Q 1027                 //memcpy(DispBuf,p1->data+2,8);
5dd1b7 1028                 p1->data[0]=KBusMem.WLXB[0];
842bb6 1029                 //pKBus->RecvTimeuS=ThisuS;
5dd1b7 1030                 PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdPingReply,p1->nStatus,DataLen,p1->data);
Q 1031                 pKBus->KBusChnStats[0].ClientSendPkts++;
1032                 pKBus->KBusSendPacket((uchar *)p2, PacketLen);
483170 1033                 break;
Q 1034             case cmdPingReply:
1035                 break;
1036             case cmdRead:
1037                 break;
1038             case cmdReadReply:
1039                 break;
1040             case cmdWrite:
1041                 //memcpy(DispBuf,p1->data,DataLen);
1042                 PacketLen=KBusMakePacket(p2,1,0,cmdWriteReply,p1->nStatus,0,0);
5dd1b7 1043                 pKBus->KBusChnStats[0].ClientSendPkts++;
Q 1044                 pKBus->KBusSendPacket((uchar *)p2, PacketLen);                    
483170 1045                 break;
Q 1046             case cmdWriteReply:
1047                 break;
1048             case cmdGetVersion:
1049                 break;
1050             case cmdVerInfo:
1051                 break;
5dd1b7 1052             case cmdUniExChgData:
Q 1053                 KBusMem.WLYB[0]=p1->data[0];
1054                 KBusMem.WLYB[1]=p1->data[1];
1055                 KBusMem.WLYB[2]=p1->data[2];
1056                 KBusMem.WLYB[3]=p1->data[3];
1057             
842bb6 1058             //    pKBus->nSlaveTick=p1->data[4]+(p1->data[5]<<8);//+(p1->data[6]<<16)+(p1->data[7]<<24);
5dd1b7 1059
Q 1060                 if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvTimeSync,0,0);
1061
1062                 if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvDataUpdate,0,0);
1063                     
1064                 nIndex=pKBus->nClientDataIndex;            
1065                 p1->data[0]=KBusMem.WLXB[0];
1066                 p1->data[1]=KBusMem.WLXB[1];
1067                 p1->data[2]=KBusMem.WLXB[2];
1068                 p1->data[3]=KBusMem.WLXB[3];
1069                 p1->data[4]=KBusMem.WLXB[4];
1070                 p1->data[5]=KBusMem.WLXB[5];                
1071                 p1->data[6]=KBusMem.WLXB[6];
1072                 p1->data[7]=KBusMem.WLXB[7];                    
1073             
1074                 p1->data[9]=nIndex;
1075                 p1->data[10]=pKBus->KBusChnStats[0].ClientDatas[nIndex];
1076                 p1->data[11]=pKBus->KBusChnStats[0].ClientDatas[nIndex]>>8;
1077                 p1->data[12]=pKBus->KBusChnStats[0].ClientDatas[nIndex]>>16;
1078                 p1->data[13]=pKBus->KBusChnStats[0].ClientDatas[nIndex]>>24;
1079                 pKBus->nClientDataIndex++;
1080                 DataLen = 14;
1081                 if (pKBus->nClientDataIndex >= 10) { pKBus->nClientDataIndex=0;}
483170 1082                 unStatus nStatus;
Q 1083                 nStatus.nStatus = p1->nStatus;
842bb6 1084                 if (nStatus.nErr1) {    pKBus->ErrStat=8100;}
5dd1b7 1085                 PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdExChgDataReply,p1->nStatus,DataLen,p1->data);
Q 1086                 pKBus->KBusChnStats[0].ClientSendPkts++;
1087                 pKBus->KBusSendPacket((uchar *)p2, PacketLen);
483170 1088                 break;
Q 1089             case cmdExChgDataReply:
1090                 break;
1091             
1092             
1093             case cmdSyncRead:
1094                 break;
1095             case cmdSyncWrite:
1096                 break;
1097             case cmdSequenRead:
1098                 break;
1099             case cmdSyncTime:
1100                 break;
842bb6 1101             
Q 1102             case cmdRemoteReq:            //收到Remote请求
1103 //                KBusSlaveRunRemoteReq(pKBus,p1->data[0],p1->data, DataLen);
1104                     // data[0] -> reqSvr;
1105                     // data[1] -> param;
1106                     switch (p1->data[0]) {
1107                         case ReqBlinkLED:
1108                         case ReqTransBlink:
1109                             KMRunStat.bLEDFlick=p1->data[1];
1110                             DataLen = 0;
1111                             PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data);        
1112                             pKBus->KBusSendPacket((uchar *)p2, PacketLen);
1113                             break;
1114                         case ReqUpdateFirm:        //37个字节
1115                             DataLen = 0;
1116                             PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data);        
1117                             pKBus->KBusSendPacket((uchar *)p2, PacketLen);
1118                             pData = &p1->data[5];
1119                             Addr = p1->data[2] + (p1->data[3] <<8);
1120                             DataLen = p1->data[4] ;
1121                             KMRunService(p1->data[0],Addr,0,&pData,&DataLen);
1122                             DataLen=0;
1123                             break;
1124                         case ReqUpdateFirmInfo:
1125                             DataLen = 0;
1126                             PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data);        
1127                             pKBus->KBusSendPacket((uchar *)p2, PacketLen);
1128                             pData = &p1->data[5];
1129                             Addr = p1->data[2] + (p1->data[3] <<8);
1130                             DataLen = p1->data[4] ;
1131                             KMRunService(p1->data[0],Addr,0,&pData,&DataLen);
1132                             DataLen=0;
1133                             break;
1134                         default:
1135                             pData = &p1->data[5];
1136                             Addr = p1->data[2] + (p1->data[3] <<8);
1137                             DataLen = p1->data[4] ;
1138                             KMRunService(p1->data[0],p1->data[1],0,&pData,&DataLen);
1139                             DataLen = 0;
1140                             PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data);        
1141                             pKBus->KBusSendPacket((uchar *)p2, PacketLen);
1142                             DataLen=0;
1143
1144                             break;
1145                     }            
1146                 break;
1147                     
483170 1148             default:
Q 1149                 break;        
1150         }
1151     }    
842bb6 1152     return 0;
Q 1153 }
1154
1155 int KBusSlaveRunRemoteReq(stKBusDef * pKBus, int nReqSvrId, unsigned char * pData , int Len1)
1156 {
1157     int PacketLen;
1158     switch (nReqSvrId) {
1159         case ReqTransBlink:
1160             KMRunStat.bLEDFlick=5;
1161             //    PacketLen=KBusMakePacket(pKBus->PacketBuf2,pKBus->nStationId,0,cmdRemoteReq,p1->nStatus,DataLen,p1->data);        
1162             break;
1163         default:
1164             break;
1165     }
483170 1166     return 0;
Q 1167 }
1168
5dd1b7 1169 int KBusParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1)
483170 1170 {
5dd1b7 1171     int ThisuS=GetuS();
483170 1172     int Result=0;
Q 1173     unsigned char * p2 = (unsigned char *)p1;
5dd1b7 1174     if (p2[0] == KB_SYN && Len1 > 1)
483170 1175     {
Q 1176         p1=(pKBPacket)(p2+1);
1177         Len1--;
1178     }
1179     
5dd1b7 1180     if (pKBus->bMaster)
483170 1181     {
5dd1b7 1182             pKBus->bMasterRecved=1;
Q 1183             Result=KBusCheckPacket(pKBus, p1, Len1);
483170 1184             if (Result != S_OK)
Q 1185             {
842bb6 1186                 
483170 1187                 return Result;
Q 1188             }
5dd1b7 1189             pKBus->bMasterRecvOK=1;
Q 1190             Result=KBusMasterParsePacket(pKBus, p1, Len1);            
483170 1191             return Result;
Q 1192     }
5dd1b7 1193     if (pKBus->bSlave)
483170 1194     {
5dd1b7 1195             pKBus->KBusChnStats[0].ClientRecvPkts++;
Q 1196             Result=KBusSlaveCheckPacket(pKBus, p1, Len1);
483170 1197             if (Result != S_OK)
Q 1198             {
1199                 return Result;
1200             }
5dd1b7 1201             pKBus->KBusChnStats[0].ClientTimeOutErr=pKBus->RunStat;
Q 1202             Result=KBusSlaveParsePacket(pKBus, p1, Len1);
483170 1203             return Result;
Q 1204     }
1205     //int len1=p1->PacketLen;
1206 //    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
1207 //    pKBPacket p2=(pKBPacket)PacketBuf2;
1208 //            Uart2Stat.OKPacket++;
1209
1210     return Result;
1211 }
1212
1213 /*
1214 int InitMachine(stMachineConfig * pConfig)
1215 {
1216     return S_OK;
1217 }
1218
1219 int SetConfig(void)
1220 {
1221     return S_OK;
1222 }
1223
1224 int StartConfig(void)
1225 {
1226     return S_OK;
1227 }
1228     
1229 int SetMasterConfig(void)
1230 {
1231     return S_OK;
1232 }
1233
1234 int StartPolling(void)
1235 {
1236     return S_OK;
1237 }
1238
1239 int ReadData(void)
1240 {
1241     return S_OK;
1242 }
1243
1244 int WriteData(void)
1245 {
1246     return S_OK;
1247 }
1248
1249 int GetStat(void)
1250 {
1251     return S_OK;
1252 }
1253 */
5dd1b7 1254
Q 1255
1256
1257 unsigned char KBusBCC(void * pData, int nSize)
483170 1258 {
5dd1b7 1259     unsigned char k;
Q 1260     k=0;
1261     for (int i=0;i<nSize;i++)
1262     {
1263         k^=((unsigned char *)pData)[i];
483170 1264     }
5dd1b7 1265     return k;    
483170 1266 }
5dd1b7 1267
Q 1268 int KBusMakePacket(pKBPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nStatus, unsigned char DataLen,void * pData )
1269 {
1270     p1->Sign=KBStartSign;
1271     p1->DstHost=dst;
1272     p1->SrcAddr=src;
1273     p1->nCMD=nType;
1274     p1->nStatus=nStatus;
1275     int PacketLenth=0;
1276     switch (nType)
1277     {
1278         case cmdNone:
1279             break;
1280         case cmdPing:
1281             p1->DataLen=DataLen;
1282             memcpy(p1->data,pData,DataLen);
1283             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1284             p1->data[DataLen+1]=KBEndSign;
1285             PacketLenth=sizeof(stKBPacket)+DataLen+1;
1286             break;
1287         case cmdPingReply:
1288             p1->DataLen=DataLen;
1289             memcpy(p1->data,pData,DataLen);
1290             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1291             p1->data[DataLen+1]=KBEndSign;        
1292             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
1293             break;
1294         case cmdRead:
1295             break;
1296         case cmdReadReply:
1297             break;
1298         case cmdWrite:
1299             break;
1300         case cmdWriteReply:
1301             p1->DataLen=DataLen;
1302          if (DataLen !=0 )    memcpy(p1->data,pData,DataLen);
1303             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1304             p1->data[DataLen+1]=KBEndSign;
1305             PacketLenth=sizeof(stKBPacket)+DataLen+1;                    
1306             break;
1307         case cmdGetVersion:
1308             p1->DataLen=DataLen;
1309             memcpy(p1->data,pData,DataLen);
1310             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1311             p1->data[DataLen+1]=KBEndSign;
1312             PacketLenth=sizeof(stKBPacket)+DataLen+1;            
1313             break;
1314         case cmdVerInfo:
1315             p1->DataLen=DataLen;
1316             memcpy(p1->data,pData,DataLen);
1317             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1318             p1->data[DataLen+1]=KBEndSign;
1319             PacketLenth=sizeof(stKBPacket)+DataLen+1;            
1320             break;
1321         case cmdUniExChgData:
1322             p1->DataLen=DataLen;
1323             memcpy(p1->data,pData,DataLen);
1324             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1325             p1->data[DataLen+1]=KBEndSign;
1326             PacketLenth=sizeof(stKBPacket)+DataLen+1;
1327             break;
1328         case cmdExChgDataReply:
1329             p1->DataLen=DataLen;
1330             memcpy(p1->data,pData,DataLen);
1331             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1332             p1->data[DataLen+1]=KBEndSign;        
1333             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
1334             break;
1335                 
1336         case cmdSyncRead:
1337             p1->DataLen=DataLen;
1338             memcpy(p1->data,pData,DataLen);
1339             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1340             p1->data[DataLen+1]=KBEndSign;        
1341             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
1342             break;
1343         case cmdSyncWrite:
1344             p1->DataLen=DataLen;
1345             memcpy(p1->data,pData,DataLen);
1346             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1347             p1->data[DataLen+1]=KBEndSign;        
1348             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
1349             break;
1350         case cmdSequenRead:
1351             p1->DataLen=DataLen;
1352             memcpy(p1->data,pData,DataLen);
1353             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1354             p1->data[DataLen+1]=KBEndSign;        
1355             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
1356             break;        
1357         
1358         case cmdSyncTime:
1359             p1->DataLen=DataLen;
1360             memcpy(p1->data,pData,DataLen);
1361             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1362             p1->data[DataLen+1]=KBEndSign;        
1363             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
1364             break;
1365         
1366         default:
1367             p1->DataLen=DataLen;
1368             memcpy(p1->data,pData,DataLen);
1369             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
1370             p1->data[DataLen+1]=KBEndSign;        
1371             PacketLenth=sizeof(stKBPacket)+DataLen+1;                    
1372             break;
1373     }
1374     
1375     return PacketLenth;
1376 }
1377