QuakeGod
2024-04-12 5dd1b7fdcc52a43867f924e8d220de8467af372a
提交 | 用户 | 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     
48     return iRet;
49 };
50
51 int KBusInitSlave(stKBusDef * pKBus,KBusSendPktFuncDef KBusSendPacket, int nStationId, stDeviceInfo * pDeviceInfo)
52 {
53     int iRet =0;
54     pKBus->MyStat = KBusStatInited;
55         pKBus->bMaster = 0;
56         pKBus->bSlave = 1;
57         pKBus->bRepeater = 0;
58         pKBus->nChildCount = 0;
59         pKBus->nStationId = nStationId;    
60         pKBus->pMyDeviceInfo = pDeviceInfo;
61         pKBus->KBusEvCBFunc = NULL;
62         pKBus->KBusSvCBFunc=NULL;    
63         pKBus->KBusSendPacket = KBusSendPacket;
64     
65     for (int i=0;i<MAX_CLIENT;i++)
483170 66     {
5dd1b7 67         memset(&pKBus->KBusChnStats[i],0,sizeof(stChnStat));            
Q 68     }
69     return iRet;
70 }
71
72
73 int KBusInit(stKBusDef * pKBus,KBusSendPktFuncDef KBusSendPacket, int bMaster, int nChildCount)
74 {
75     int iRet =0;
76     pKBus->MyStat = KBusStatInited;
77     if (bMaster ) {
78         KBusInitMaster(pKBus,KBusSendPacket,nChildCount);
79     }else {
80         KBusInitSlave(pKBus,KBusSendPacket,nChildCount,0);
81     }
82     //    memset(pKBus->KBusChnStats,0,sizeof(pKBus->KBusChnStats));
83     return iRet;
84     
85 }
86
87 int KBusSetEvCallBackFunc(stKBusDef * pKBus, KBusEvCBFuncDef CBFunc)
88 {
89
90     pKBus->KBusEvCBFunc = CBFunc;
91 //    pKBus->bKBusEvCBFuncSet=1;
92         return 0;
93 }
94
95 int KBusStart(stKBusDef * pKBus)
96 {
97     int iRet = 0;
98     
99     return iRet;
100 }
101
102 int KBusLoopProcess(stKBusDef * pKBus)
103 {
104     int iRet = 0;
105     if (pKBus->bMaster) 
106     {
107     }
108     if (pKBus->bSlave)
109     {
110     }
111     if (pKBus->bRepeater)
112     {
113     }
114     
115     return iRet;
116 }
117
118 int KBusRepeaterFunc(stKBusDef * pKBus)
119 {
120         if ((KMem.nRunCount &0x7f) == 88) 
121         { 
122             ToggleRunLed();
123         }                
124     return 0;
125 }
126
127 int KBusUpdateChildInfo(stKBusDef * pKBus, int nCild, stDeviceInfo * pInfoBlock)
128 {
129     stDeviceInfo * pDeviceInfo = &pKBus->DeviceInfos[nCild];
130     
131     pDeviceInfo->DeviceType = pInfoBlock->DeviceType;
132     pDeviceInfo->DeviceVer = pInfoBlock->DeviceVer;
133     pDeviceInfo->InBitCount = pInfoBlock->InBitCount;
134     pDeviceInfo->OutBitCount = pInfoBlock->OutBitCount ;
135     pDeviceInfo->AIWCount = pInfoBlock->AIWCount;
136     pDeviceInfo->AQWCount = pInfoBlock->AQWCount;
137     pDeviceInfo->DWStartAddr = pInfoBlock->DWStartAddr;
138     pDeviceInfo->OutDWCount = pInfoBlock->OutDWCount;
139     
140     return 0;
141 }
142
143 int KBusMakeDefaultClildInfo(stKBusDef * pKBus, int nCild)
144 {
145     unsigned DefaultInBitCount = 8;
146     unsigned DefaultOutBitCount = 8;
147     stDeviceInfo * pDeviceInfo = &pKBus->DeviceInfos[nCild];
148
149     pDeviceInfo->DeviceType = 0;
150     pDeviceInfo->DeviceVer = 0;
151     pDeviceInfo->InBitCount = DefaultInBitCount;
152     pDeviceInfo->OutBitCount =  DefaultOutBitCount;
153     pDeviceInfo->AIWCount = 0;
154     pDeviceInfo->AQWCount = 0;
155     pDeviceInfo->DWStartAddr = 0;
156     pDeviceInfo->OutDWCount = 0;        
157     return 0;
158 }
159
160 //int KBusSetChildCfg(int nChn, int nChild, )
161 int KBusAddChildAddrByInfo(stKBusDef * pKBus, int nChild, stDeviceInfo * pDeviceInfo)
162 {
163     int iRet = 0;
164         stClientCfg * pClientCfg = &pKBus->ClientCfg[nChild];
165     
166         pClientCfg->Addr = pKBus->nCurPollId;
167         pClientCfg->Configed = KBusConfiged;
168         pClientCfg->InStartAddrBit = pKBus->CurXBitAddr;
169         pClientCfg->OutStartAddrBit = pKBus->CurYBitAddr;
170         pClientCfg->AIWStartAddrByte = pKBus->CurAIWAddr;
171         pClientCfg->AQWStartAddrByte = pKBus->CurAQWAddr;
172         pClientCfg->DIWStartAddrByte = pKBus->CurDIWAddr;
173         pClientCfg->DOWStartAddrByte = pKBus->CurDOWAddr;
483170 174         
5dd1b7 175         pKBus->CurXBitAddr += pDeviceInfo->InBitCount;
Q 176         pKBus->CurYBitAddr += pDeviceInfo->OutBitCount;
177         pKBus->CurAIWAddr += pDeviceInfo->AIWCount; 
178         pKBus->CurAQWAddr += pDeviceInfo->AQWCount;
179     
180     return iRet;
181 }
182     
183
184 int KBusSearchChildProc(stKBusDef * pKBus)
185 {
186     int iRet = 0;
187     uint32_t tick1=HAL_GetTick();
188     uint32_t thisuS=GetuS();
189     
190     pKBus->MyStat = KBusStatReady;
191     return iRet;
192     if (!pKBus->bMasterSent) {
193         // Sent Query Packet for CurPollId;
194         //pKBus->nCurPollId;
195         pKBus->bMasterSent=1;
196         pKBus->SendTimeuS = thisuS;
197     }
198     if (pKBus->bMasterSent) {
199         if (pKBus->bMasterRecved) {
200
201         }
202         
203         if (!pKBus->bMasterRecved && thisuS- pKBus->SendTimeuS > 1000) 
204         {
205             // timeOut,
206                 pKBus->bMasterSent=0;  // no wait any more;
207             if (pKBus->RetryCount <3) {
208                 // resent query packet for curPollId;    
209                 pKBus->bMasterSent=1;
210                 pKBus->SendTimeuS = thisuS;                
211             }
212             if (pKBus->RetryCount >3 ) {
213                 // Set Cur Child As Default
214                 KBusMakeDefaultClildInfo(pKBus,pKBus->nCurPollId);
215                 KBusAddChildAddrByInfo(pKBus,pKBus->nCurPollId,&pKBus->DeviceInfos[pKBus->nCurPollId]);
483170 216                 
5dd1b7 217                 // Next Child;
Q 218                 pKBus->nCurPollId++;
219                 if (pKBus->nCurPollId > pKBus->nChildCount) 
220                 {
221                     // all query done, next stage 
222                     pKBus->MyStat = KBusStatReady;
223                     // CallBack
224                     if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvStateChange,0,0);
225                     
226                 }
227             
228             }
229             
230         }
231     }
232
233     return iRet;
234 }
235
236
237 int KBusMasterFunc(stKBusDef * pKBus)
238 {
239     switch (pKBus->MyStat){
240         case KBusStatUnInited:
241             KBusLoadSavedConfig(pKBus);
242             pKBus->MyStat = KBusStatInited;
483170 243             break;
5dd1b7 244         case KBusStatInited:
Q 245             pKBus->MyStat = KBusStatConfig;
483170 246             break;
5dd1b7 247         case KBusStatConfig:
Q 248             KBusSearchChildProc(pKBus);
483170 249             break;
5dd1b7 250         case KBusStatReady:
Q 251             pKBus->MyStat = KBusStatRunning;
252             break;
253         case KBusStatRunning:
254             KBusDataPoll(pKBus);
255             break;
483170 256         default:
Q 257             break;
258     }
5dd1b7 259     return 0;
483170 260 }
Q 261
5dd1b7 262 int KBusDataPoll(stKBusDef * pKBus)
Q 263 {
264     int iRet = 0;
265     uint32_t tick1=HAL_GetTick();
266     uint32_t thisuS=GetuS();    
267         int len1=0;
268     int nThisPollId = pKBus->nCurPollId;
269     
270     stChnStat * pChnStat = &pKBus->KBusChnStats[nThisPollId];
271         if ((pKBus->bMasterRecved && pKBus->bMasterRecvOK && thisuS-pKBus->SendTimeuS>50) || thisuS-pKBus->SendTimeuS>1000u)
272         {
273             if (!pKBus->bMasterRecvOK) 
274             {
275                 pKBus->TimeOutCount++;
276                 Uart2Stat.TimeOutErr++; 
277                 pChnStat->LostPackets++;
278                 pChnStat->CtnLstPkts++;
279                 if (!pKBus->bMasterRecved) {pChnStat->TimeOutErr++;}
280                 if (pChnStat->CtnLstPkts > pChnStat->MaxCtnLstPkts)
281                 {pChnStat->MaxCtnLstPkts = pChnStat->CtnLstPkts;}
282                 if (pChnStat->CtnLstPkts>3)
283                 {
284                     pChnStat->Stat = 0;
285                     pKBus->ErrStat=200;
286                     KBusMem.WLXB[nThisPollId]=0;
287                     if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvStateChange,0,0);
288                     if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvDataUpdate,0,0);
289                     
290                 }
291             //    LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
292             }else
293             {
294                 pChnStat->Stat=1;
295                 
296                 pKBus->RunStat=100;
297             }
298             if (pKBus->DeviceInfos[nThisPollId].InBitCount >8 ||  pKBus->DeviceInfos[nThisPollId].OutBitCount > 8) {
299                 //nThisPollId += 2;
300                 nThisPollId ++;
301             }else {
302                 nThisPollId ++;
303             }
304             if (nThisPollId > pKBus->nChildCount)
305             {
306                 // 数据轮询完, 周期间隙,插入其他处理数据.  每次处理的时间间隙, 一问 一答  时间.
307                 // 分成几种类型, 轮流进行
308                 //    时间同步,            查询新子机,        处理带外数据,            ,处理额外的事情,            或者跳过.
309                 pKBus->CircleTime=thisuS-pKBus->LastCircleStartTime;
310                 pKBus->LastCircleStartTime=thisuS;
311                 pKBus->nSeq++;
312                 nThisPollId=1;
313             }
314             pKBus->nCurPollId = nThisPollId;
315
316             pKBus->Datas[0]=KBusMem.WLYB[nThisPollId -1 ];
317             pKBus->Datas[1]=KBusMem.WLYB[nThisPollId ];;
318
319             pKBus->Datas[2]=KBusMem.WLYB[nThisPollId + 1 ]; //KBusChnStats[nCurPollId].Stat;
320             pKBus->Datas[3]=KBusMem.WLYB[nThisPollId + 2 ];
321             pKBus->Datas[4]=tick1&0xff;
322             pKBus->Datas[5]=(tick1>>8)&0xff;
323             pKBus->Datas[6]=(tick1>>16)&0xff;
324             pKBus->Datas[7]=(tick1>>24)&0xff;
325             
326             pKBus->SendTimeuS=thisuS;
327             unStatus nStatus;
328             nStatus.nSeq = pKBus->nSeq;
329             nStatus.nErr1 = (pKBus->KBusChnStats[nThisPollId].Stat==0);
330             
331             
332             len1=KBusMakePacket((pKBPacket)pKBus->PacketBuf1,0,nThisPollId,cmdUniExChgData,nStatus.nStatus,8,pKBus->Datas);
333 //            LL_USART_SetBaudRate(USART2,48000000,LL_USART_OVERSAMPLING_8,DefaultUart2Baud);
334             
335             pKBus->KBusSendPacket(pKBus->PacketBuf1, len1);
336             pKBus->KBusChnStats[pKBus->nCurPollId].SendPackets++;
337             pKBus->KBusChnStats[pKBus->nCurPollId].SendTimeInterval=pKBus->SendTimeuS-pKBus->KBusChnStats[pKBus->nCurPollId].LastSentTimeuS;
338             pKBus->KBusChnStats[pKBus->nCurPollId].LastSentTimeuS=pKBus->SendTimeuS;
339 //            PacketLength = len1;
340             pKBus->SendTime=tick1;
341
342             pKBus->bMasterRecved=0;
343             pKBus->bMasterRecvOK=0;
344         //    LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);        
345             //ToggleErrLed();
346 //                ToggleOut8();
347
348         }    
349     
350     return iRet;
351 }
352
353 int KBusSlaveFunc(stKBusDef * pKBus)
354 {
355         int ThisuS=GetuS();
356         int thisRecvTime=pKBus->RecvTimeuS;
357      if (pKBus->nStationId >0) {
358             if (pKBus->bSlaveRecved)
359             {
360                 pKBus->RunStat=8000;
361                 pKBus->bSlaveRecved=0;
362             }else if ((ThisuS - thisRecvTime) >12000u)
363             {
364                 pKBus->ErrStat=8000;
365             }else if ( ThisuS > (thisRecvTime + 12000u))
366             {
367                 pKBus->ErrStat=8000;
368             }
369     }
370     return 0;
371 }
372
373
374
375 int KBusCheckPacket(stKBusDef * pKBus, pKBPacket p1, int nLen1)
483170 376 {
Q 377     unsigned char * p2 = (unsigned char *)p1;
378     pKBPacket p3=p1;
5dd1b7 379     if ((p2[0] == KB_SYN || p2[0] != KBStartSign) && nLen1 > 1)
483170 380     {
Q 381         p3=(pKBPacket)(p2+1);
382         nLen1--;
383     }    
384     
5dd1b7 385     if (p3->Sign != KBStartSign)
483170 386     {
Q 387         Uart2Stat.NotPacketErr++;
5dd1b7 388         pKBus->KBusChnStats[pKBus->nCurPollId].NotPkgErr++;
Q 389         if (pKBus->KBusDiagnosis) {
390             if (pKBus->KBusSnapPos == 0) {
483170 391                 KMem.WDB[0x20]=nLen1;
Q 392                 memcpy(&KMem.WDB[0x21],p1,nLen1);
5dd1b7 393                 pKBus->KBusSnapPos++;
Q 394             } else if (pKBus->KBusSnapPos==1) {
483170 395                 KMem.WDB[0x40]=nLen1;
Q 396                 memcpy(&KMem.WDB[0x41],p1,nLen1);
5dd1b7 397                 pKBus->KBusSnapPos++;
483170 398             } else {
Q 399                 KMem.WDB[0x60]=nLen1;
400                 memcpy(&KMem.WDB[0x61],p1,nLen1);
5dd1b7 401                 pKBus->KBusSnapPos=0;                
483170 402             }
Q 403             
404         }
405         return -1;
406     }
eaf5d5 407     int DataLen=p3->DataLen;
5dd1b7 408     if (DataLen>KBMaxPacketLength) 
483170 409     {
Q 410         Uart2Stat.LengthErr++;
5dd1b7 411         pKBus->KBusChnStats[pKBus->nCurPollId].PkgLenErr++;
483170 412         return -1;
Q 413     }
414     if (nLen1<DataLen+sizeof(stKBPacket)+1)
415     {
416         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
417         //PutStr(str3,len4);
5dd1b7 418         pKBus->KBusChnStats[pKBus->nCurPollId].PkgLenErr++;
483170 419         Uart2Stat.LengthErr++;
Q 420         return -3;    //not long enough                    
421     }
422 //    if (p3->data[DataLen+1] != EndSign)
423 //    {
424 //        KBusChnStats[nCurPollId].NoEndErr++;
425 //        Uart2Stat.LengthErr++;        
426 //        return -2;
427 //    }
428     unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
429     if (thisBCC != p3->data[DataLen]) 
430     {//BCC Error;
431         Uart2Stat.BCCerr++;
5dd1b7 432         pKBus->KBusChnStats[pKBus->nCurPollId].BCCErr++;
483170 433         return -4;
Q 434     }         
435     return 0;
436 }
437
5dd1b7 438 int KBusSlaveCheckPacket(stKBusDef * pKBus, pKBPacket p1, int nLen1)
483170 439 {
Q 440     unsigned char * p2 = (unsigned char *)p1;
441     pKBPacket p3=p1;
5dd1b7 442     if ((p2[0] == KB_SYN || p2[0] != KBStartSign) && nLen1 > 1)
483170 443     {
Q 444         p3=(pKBPacket)(p2+1);
445         nLen1--;
446     }    
447     
5dd1b7 448     if (p3->Sign != KBStartSign)
483170 449     {
Q 450         Uart2Stat.NotPacketErr++;
5dd1b7 451         pKBus->KBusChnStats[0].ClientNotPktErr++;
483170 452         return -1;
Q 453     }
eaf5d5 454     int DataLen=p3->DataLen;
5dd1b7 455     if (DataLen>KBMaxPacketLength) 
483170 456     {
Q 457         Uart2Stat.LengthErr++;
5dd1b7 458         pKBus->KBusChnStats[0].ClientPkgLenErr++;
483170 459         return -1;
Q 460     }
461     if (nLen1<DataLen+sizeof(stKBPacket)+1)
462     {
463         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
464         //PutStr(str3,len4);
5dd1b7 465         pKBus->KBusChnStats[0].ClientPkgLenErr++;
483170 466         Uart2Stat.LengthErr++;
Q 467         return -3;    //not long enough                    
468     }
469 //    if (p3->data[DataLen+1] != EndSign)
470 //    {
471 //        KBusChnStats[nCurPollId].NoEndErr++;
472 //        Uart2Stat.LengthErr++;        
473 //        return -2;
474 //    }
475     unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
476     if (thisBCC != p3->data[DataLen]) 
477     {//BCC Error;
478         Uart2Stat.BCCerr++;
5dd1b7 479         pKBus->KBusChnStats[0].ClientBccErr++;
483170 480         return -4;
Q 481     }         
482     return 0;
483 }
484
5dd1b7 485 int KBusMasterParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1)
483170 486 {
Q 487     unsigned char * p6 = (unsigned char *)p1;
488
5dd1b7 489     if ((p6[0] == KB_SYN || p6[0] != KBStartSign) && Len1 > 1)
483170 490     {
Q 491         p1=(pKBPacket)(p6+1);
492         Len1--;
493     }    
494
eaf5d5 495         int DataLen=p1->DataLen;
5dd1b7 496         pKBus->KBusChnStats[pKBus->nCurPollId].RecvPackets++;    
Q 497 //        pKBPacket p2=(pKBPacket)PacketBuf2;        
498 //        int PacketLen=0;
483170 499         //LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_6);
5dd1b7 500         int nCurPollId = pKBus->nCurPollId;
483170 501         int ChildId=p1->SrcAddr;
5dd1b7 502         int ThisuS = GetuS();
483170 503         unsigned char nIndex;
Q 504         switch (p1->nCMD)
505         {
506             
507             case cmdNone:
508                 break;
5dd1b7 509             case cmdQueryRply:
Q 510                 DataLen=sizeof(stDeviceInfo);
511                 KBusUpdateChildInfo(pKBus,pKBus->nCurPollId,(stDeviceInfo *)&p1->data[0]);
512                 pKBus->KBusChnStats[0].ClientSendPkts++;
513                 break;
514             case cmdSetCfgRply:
515                 break;
516             case cmdToRunModeRply:
517                 break;
518             case cmdBroadCastCfg:
519                 break;
520             case cmdMuExchgDataRply:
521                 break;
522             case cmdToSafeMode:
523                 break;
524             case cmdReHeartBeat:
525                 break;
526             
483170 527             case cmdPing:
Q 528                 break;
529             case cmdPingReply:
5dd1b7 530                 pKBus->DelayuS=ThisuS-pKBus->SendTimeuS;
Q 531                 if (pKBus->DelayuS > pKBus->nMaxDelayuS) pKBus->nMaxDelayuS = pKBus->DelayuS;
483170 532                 
5dd1b7 533                 KBusMem.WLXB[ChildId]=p1->data[0];
483170 534                 
Q 535                 //RunStat=100;
5dd1b7 536                 pKBus->KBusChnStats[pKBus->nCurPollId].CtnLstPkts=0;
Q 537                 pKBus->KBusChnStats[pKBus->nCurPollId].Delay=pKBus->DelayuS;
538             if (pKBus->DelayuS > pKBus->KBusChnStats[pKBus->nCurPollId].MaxDelay) 
539                 pKBus->KBusChnStats[pKBus->nCurPollId].MaxDelay=pKBus->DelayuS;
483170 540                 //PutOutput(outputvalue);
5dd1b7 541                 pKBus->bMasterRecvOK=1;
483170 542                 break;
Q 543             case cmdRead:
544                 break;
545             case cmdReadReply:
546                 break;
547             case cmdWrite:
548                 break;
549             case cmdWriteReply:
5dd1b7 550                 pKBus->bMasterRecved=1;
483170 551                 break;
Q 552             case cmdGetVersion:
553                 break;
554             case cmdVerInfo:
555                 break;
5dd1b7 556             case cmdUniExChgData:
Q 557         
483170 558                 break;
Q 559             case cmdExChgDataReply:
5dd1b7 560                 pKBus->DelayuS=ThisuS-pKBus->SendTimeuS;
Q 561                 if (pKBus->DelayuS > pKBus->nMaxDelayuS) pKBus->nMaxDelayuS = pKBus->DelayuS;
483170 562
5dd1b7 563                 KBusMem.WLXB[ChildId-1]=p1->data[0];;
Q 564                 KBusMem.WLXB[ChildId  ]=p1->data[1];
565                 KBusMem.WLXB[ChildId+1]=p1->data[2];
566                 KBusMem.WLXB[ChildId+2]=p1->data[3];;
567                 KBusMem.WLXB[ChildId+3]=p1->data[4];
568                 KBusMem.WLXB[ChildId+4]=p1->data[5];
569                 KBusMem.WLXB[ChildId+5]=p1->data[6];
570                 KBusMem.WLXB[ChildId+6]=p1->data[7];
8b51c7 571             
5dd1b7 572             //Call Back 
Q 573                 if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvDataUpdate,0,0);
483170 574                 //RunStat=100;
5dd1b7 575                 pKBus->KBusChnStats[nCurPollId].CtnLstPkts=0;
Q 576                 pKBus->KBusChnStats[nCurPollId].Delay=pKBus->DelayuS;
577             if (pKBus->DelayuS > pKBus->KBusChnStats[nCurPollId].MaxDelay) 
578                 pKBus->KBusChnStats[nCurPollId].MaxDelay=pKBus->DelayuS;
483170 579                 //PutOutput(outputvalue);
Q 580                 
5dd1b7 581               if (DataLen>=14) {
Q 582                     nIndex=p1->data[9];
583                     pKBus->KBusChnStats[nCurPollId].ClientDatas[nIndex]=p1->data[10]|(p1->data[11]<<8)|(p1->data[12]<<16)|(p1->data[13]<<24);
584                 }
585                 pKBus->bMasterRecvOK=1;
483170 586                 
Q 587                 break;
588                     
589             default:
590                 break;        
591         }
592     //    nCurPollId ++;
593     //    if (nCurPollId > nChilds)
594     //    {
595     //        nCurPollId=1;
596     //    }    
597     return 0;
598 }
599
5dd1b7 600 int KBusSlaveParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1)
483170 601 {
Q 602     unsigned char * p3 = (unsigned char *)p1;
5dd1b7 603     if (p3[0] == KB_SYN && Len1 > 1)
483170 604     {
Q 605         p1=(pKBPacket)(p3+1);
606         Len1--;
607     }        
608     
609     Uart2Stat.OKPacket++;                
eaf5d5 610     int DataLen=p1->DataLen;    
483170 611 //int nSrcAddr=p1->SrcAddr;
Q 612     int nDstHost=p1->DstHost;
613         
614 //    KBusRecvTimeuS=ThisuS;
615 //    KBusSlaveRecved=1;
616     
5dd1b7 617     pKBPacket p2=(pKBPacket)pKBus->PacketBuf2;
483170 618     
Q 619     int PacketLen=0;
620     unsigned char nIndex;// = p1->nStatus & 0x07;
5dd1b7 621     if (nDstHost!=pKBus->nStationId && nDstHost != 0xff)
483170 622     {
5dd1b7 623         pKBus->KBusChnStats[0].ClientMisIdPkts++;
483170 624         return -1;
Q 625     }
5dd1b7 626     int ThisuS = GetuS();
Q 627     if (nDstHost==pKBus->nStationId || nDstHost==0xff)
483170 628     {
5dd1b7 629         pKBus->RecvTimeuS=ThisuS;
Q 630         pKBus->bSlaveRecved=1;
483170 631         switch (p1->nCMD)
Q 632         {
633             case cmdNone:
634                 break;
5dd1b7 635             case cmdQuery:
Q 636                 DataLen=sizeof(stDeviceInfo);
637                 PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdQueryRply,p1->nStatus,DataLen,pKBus->pMyDeviceInfo);
638                 pKBus->KBusChnStats[0].ClientSendPkts++;
639                 pKBus->KBusSendPacket((uchar *)p2, PacketLen);
640                 break;
641             case cmdSetCfg:
642                 break;
643             case cmdToRunMode:
644                 break;
645             case cmdBroadCastCfg:
646                 break;
647             case cmdMuExchgData:
648                 break;
649             case cmdToSafeMode:
650                 break;
651             case cmdHeartBeat:
652                 break;
653             
483170 654             case cmdPing:
5dd1b7 655                 KBusMem.WLYB[0]=p1->data[0];
483170 656                 //PutOutput(outputvalue);
Q 657                 //memcpy(DispBuf,p1->data+2,8);
5dd1b7 658                 p1->data[0]=KBusMem.WLXB[0];
Q 659                 pKBus->RecvTimeuS=ThisuS;
660                 PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdPingReply,p1->nStatus,DataLen,p1->data);
661                 pKBus->KBusChnStats[0].ClientSendPkts++;
662                 pKBus->KBusSendPacket((uchar *)p2, PacketLen);
483170 663                 break;
Q 664             case cmdPingReply:
665                 break;
666             case cmdRead:
667                 break;
668             case cmdReadReply:
669                 break;
670             case cmdWrite:
671                 //memcpy(DispBuf,p1->data,DataLen);
672                 PacketLen=KBusMakePacket(p2,1,0,cmdWriteReply,p1->nStatus,0,0);
5dd1b7 673                 pKBus->KBusChnStats[0].ClientSendPkts++;
Q 674                 pKBus->KBusSendPacket((uchar *)p2, PacketLen);                    
483170 675                 break;
Q 676             case cmdWriteReply:
677                 break;
678             case cmdGetVersion:
679                 break;
680             case cmdVerInfo:
681                 break;
5dd1b7 682             case cmdUniExChgData:
Q 683                 KBusMem.WLYB[0]=p1->data[0];
684                 KBusMem.WLYB[1]=p1->data[1];
685                 KBusMem.WLYB[2]=p1->data[2];
686                 KBusMem.WLYB[3]=p1->data[3];
687             
688                 pKBus->nSlaveTick=p1->data[4]+(p1->data[5]<<8);//+(p1->data[6]<<16)+(p1->data[7]<<24);
689
690                 if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvTimeSync,0,0);
691
692                 if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvDataUpdate,0,0);
693                     
694                 nIndex=pKBus->nClientDataIndex;            
695                 p1->data[0]=KBusMem.WLXB[0];
696                 p1->data[1]=KBusMem.WLXB[1];
697                 p1->data[2]=KBusMem.WLXB[2];
698                 p1->data[3]=KBusMem.WLXB[3];
699                 p1->data[4]=KBusMem.WLXB[4];
700                 p1->data[5]=KBusMem.WLXB[5];                
701                 p1->data[6]=KBusMem.WLXB[6];
702                 p1->data[7]=KBusMem.WLXB[7];                    
703             
704                 p1->data[9]=nIndex;
705                 p1->data[10]=pKBus->KBusChnStats[0].ClientDatas[nIndex];
706                 p1->data[11]=pKBus->KBusChnStats[0].ClientDatas[nIndex]>>8;
707                 p1->data[12]=pKBus->KBusChnStats[0].ClientDatas[nIndex]>>16;
708                 p1->data[13]=pKBus->KBusChnStats[0].ClientDatas[nIndex]>>24;
709                 pKBus->nClientDataIndex++;
710                 DataLen = 14;
711                 if (pKBus->nClientDataIndex >= 10) { pKBus->nClientDataIndex=0;}
483170 712                 unStatus nStatus;
Q 713                 nStatus.nStatus = p1->nStatus;
5dd1b7 714                 if (nStatus.nErr1) {    pKBus->ErrStat=8000;}
Q 715                 PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdExChgDataReply,p1->nStatus,DataLen,p1->data);
716                 pKBus->KBusChnStats[0].ClientSendPkts++;
717                 pKBus->KBusSendPacket((uchar *)p2, PacketLen);
483170 718                 break;
Q 719             case cmdExChgDataReply:
720                 break;
721             
722             
723             case cmdSyncRead:
724                 break;
725             case cmdSyncWrite:
726                 break;
727             case cmdSequenRead:
728                 break;
729             case cmdSyncTime:
5dd1b7 730                 pKBus->nSlaveTick=p1->data[0]+(p1->data[1]<<8)+(p1->data[2]<<16)+(p1->data[3]<<24);
Q 731                 if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvTimeSync,0,0);            
483170 732                 break;
Q 733             default:
734                 break;        
735         }
736     }    
737     return 0;
738 }
739
5dd1b7 740 int KBusParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1)
483170 741 {
5dd1b7 742     int ThisuS=GetuS();
483170 743     int Result=0;
Q 744     unsigned char * p2 = (unsigned char *)p1;
5dd1b7 745     if (p2[0] == KB_SYN && Len1 > 1)
483170 746     {
Q 747         p1=(pKBPacket)(p2+1);
748         Len1--;
749     }
750     
5dd1b7 751     if (pKBus->bMaster)
483170 752     {
5dd1b7 753             pKBus->bMasterRecved=1;
Q 754             Result=KBusCheckPacket(pKBus, p1, Len1);
483170 755             if (Result != S_OK)
Q 756             {
757                 return Result;
758             }
5dd1b7 759             pKBus->bMasterRecvOK=1;
Q 760             Result=KBusMasterParsePacket(pKBus, p1, Len1);            
483170 761             return Result;
Q 762     }
5dd1b7 763     if (pKBus->bSlave)
483170 764     {
5dd1b7 765             pKBus->KBusChnStats[0].ClientRecvPkts++;
Q 766             Result=KBusSlaveCheckPacket(pKBus, p1, Len1);
483170 767             if (Result != S_OK)
Q 768             {
769                 return Result;
770             }
5dd1b7 771             pKBus->KBusChnStats[0].ClientTimeOutErr=pKBus->RunStat;
Q 772             Result=KBusSlaveParsePacket(pKBus, p1, Len1);
483170 773             return Result;
Q 774     }
775     //int len1=p1->PacketLen;
776 //    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
777 //    pKBPacket p2=(pKBPacket)PacketBuf2;
778 //            Uart2Stat.OKPacket++;
779
780     return Result;
781 }
782
783 /*
784 int InitMachine(stMachineConfig * pConfig)
785 {
786     return S_OK;
787 }
788
789 int SetConfig(void)
790 {
791     return S_OK;
792 }
793
794 int StartConfig(void)
795 {
796     return S_OK;
797 }
798     
799 int SetMasterConfig(void)
800 {
801     return S_OK;
802 }
803
804 int StartPolling(void)
805 {
806     return S_OK;
807 }
808
809 int ReadData(void)
810 {
811     return S_OK;
812 }
813
814 int WriteData(void)
815 {
816     return S_OK;
817 }
818
819 int GetStat(void)
820 {
821     return S_OK;
822 }
823 */
5dd1b7 824
Q 825
826
827 unsigned char KBusBCC(void * pData, int nSize)
483170 828 {
5dd1b7 829     unsigned char k;
Q 830     k=0;
831     for (int i=0;i<nSize;i++)
832     {
833         k^=((unsigned char *)pData)[i];
483170 834     }
5dd1b7 835     return k;    
483170 836 }
5dd1b7 837
Q 838 int KBusMakePacket(pKBPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nStatus, unsigned char DataLen,void * pData )
839 {
840     p1->Sign=KBStartSign;
841     p1->DstHost=dst;
842     p1->SrcAddr=src;
843     p1->nCMD=nType;
844     p1->nStatus=nStatus;
845     int PacketLenth=0;
846     switch (nType)
847     {
848         case cmdNone:
849             break;
850         case cmdPing:
851             p1->DataLen=DataLen;
852             memcpy(p1->data,pData,DataLen);
853             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
854             p1->data[DataLen+1]=KBEndSign;
855             PacketLenth=sizeof(stKBPacket)+DataLen+1;
856             break;
857         case cmdPingReply:
858             p1->DataLen=DataLen;
859             memcpy(p1->data,pData,DataLen);
860             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
861             p1->data[DataLen+1]=KBEndSign;        
862             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
863             break;
864         case cmdRead:
865             break;
866         case cmdReadReply:
867             break;
868         case cmdWrite:
869             break;
870         case cmdWriteReply:
871             p1->DataLen=DataLen;
872          if (DataLen !=0 )    memcpy(p1->data,pData,DataLen);
873             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
874             p1->data[DataLen+1]=KBEndSign;
875             PacketLenth=sizeof(stKBPacket)+DataLen+1;                    
876             break;
877         case cmdGetVersion:
878             p1->DataLen=DataLen;
879             memcpy(p1->data,pData,DataLen);
880             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
881             p1->data[DataLen+1]=KBEndSign;
882             PacketLenth=sizeof(stKBPacket)+DataLen+1;            
883             break;
884         case cmdVerInfo:
885             p1->DataLen=DataLen;
886             memcpy(p1->data,pData,DataLen);
887             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
888             p1->data[DataLen+1]=KBEndSign;
889             PacketLenth=sizeof(stKBPacket)+DataLen+1;            
890             break;
891         case cmdUniExChgData:
892             p1->DataLen=DataLen;
893             memcpy(p1->data,pData,DataLen);
894             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
895             p1->data[DataLen+1]=KBEndSign;
896             PacketLenth=sizeof(stKBPacket)+DataLen+1;
897             break;
898         case cmdExChgDataReply:
899             p1->DataLen=DataLen;
900             memcpy(p1->data,pData,DataLen);
901             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
902             p1->data[DataLen+1]=KBEndSign;        
903             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
904             break;
905                 
906         case cmdSyncRead:
907             p1->DataLen=DataLen;
908             memcpy(p1->data,pData,DataLen);
909             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
910             p1->data[DataLen+1]=KBEndSign;        
911             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
912             break;
913         case cmdSyncWrite:
914             p1->DataLen=DataLen;
915             memcpy(p1->data,pData,DataLen);
916             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
917             p1->data[DataLen+1]=KBEndSign;        
918             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
919             break;
920         case cmdSequenRead:
921             p1->DataLen=DataLen;
922             memcpy(p1->data,pData,DataLen);
923             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
924             p1->data[DataLen+1]=KBEndSign;        
925             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
926             break;        
927         
928         case cmdSyncTime:
929             p1->DataLen=DataLen;
930             memcpy(p1->data,pData,DataLen);
931             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
932             p1->data[DataLen+1]=KBEndSign;        
933             PacketLenth=sizeof(stKBPacket)+DataLen+1;        
934             break;
935         
936         default:
937             p1->DataLen=DataLen;
938             memcpy(p1->data,pData,DataLen);
939             p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
940             p1->data[DataLen+1]=KBEndSign;        
941             PacketLenth=sizeof(stKBPacket)+DataLen+1;                    
942             break;
943     }
944     
945     return PacketLenth;
946 }
947