/** ****************************************************************************** * @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;; stKBusMem KBusMem; int KBusLoadSavedConfig(stKBusDef * pKBus) { int iRet=0; return iRet; } int KBusSaveConfig(int nChn) { int iRet=0; return iRet; } int KBusInitMaster(stKBusDef * pKBus,KBusSendPktFuncDef KBusSendPacket, int nChildCount) { int iRet =0; pKBus->MyStat = KBusStatInited; pKBus->bMaster = 1; pKBus->bSlave = 0; pKBus->bRepeater =0; pKBus->nChildCount = nChildCount; pKBus->nStationId = 0; pKBus->KBusEvCBFunc=0; memset(pKBus->KBusChnStats,0,sizeof(pKBus->KBusChnStats)); pKBus->KBusEvCBFunc=NULL; pKBus->KBusSvCBFunc=NULL; pKBus->KBusSendPacket = KBusSendPacket; pKBus->nCurPollId = 1; for (int i=0;iKBusChnStats[i],0,sizeof(stChnStat)); } for (int i=0;iDeviceInfos[1]; pDeviceInfo->DeviceType = 0; pDeviceInfo->DeviceVer = 0; pDeviceInfo->InBitCount = 16; pDeviceInfo->OutBitCount = 16; pDeviceInfo->AIWCount = 0; pDeviceInfo->AQWCount = 0; pDeviceInfo->DWStartAddr = 0; pDeviceInfo->OutDWCount = 0; */ pKBus->KBusPort = (stPortDef){.nPortHardType = 4, .nPortUseType = 3, .bEnable = 1, .bRunning =1, .StationId = 0, .bMaster = 1, .nMaxStations = nChildCount, .pInstance=pKBus , .ReqCommFunc = (CommFuncDef)KBusReqService}; KMRegisterPort(1,&pKBus->KBusPort); return iRet; }; int KBusInitSlave(stKBusDef * pKBus,KBusSendPktFuncDef KBusSendPacket, int nStationId, stDeviceInfo * pDeviceInfo) { int iRet =0; pKBus->MyStat = KBusStatInited; pKBus->bMaster = 0; pKBus->bSlave = 1; pKBus->bRepeater = 0; pKBus->nChildCount = 0; pKBus->nStationId = nStationId; pKBus->pMyDeviceInfo = pDeviceInfo; pKBus->KBusEvCBFunc = NULL; pKBus->KBusSvCBFunc=NULL; pKBus->KBusSendPacket = KBusSendPacket; /* for (int i=0;iKBusChnStats[i],0,sizeof(stChnStat)); } */ pKBus->KBusPort = (stPortDef){.nPortHardType = 4, .nPortUseType = 3, .bEnable = 1, .bRunning =1, .StationId = nStationId, .bMaster = 0, .pInstance = pKBus}; KMRegisterPort(1,&pKBus->KBusPort); return iRet; } int KBusInit(stKBusDef * pKBus,KBusSendPktFuncDef KBusSendPacket, int bMaster, int nChildCount) { int iRet =0; pKBus->MyStat = KBusStatInited; if (bMaster ) { KBusInitMaster(pKBus,KBusSendPacket,nChildCount); }else { KBusInitSlave(pKBus,KBusSendPacket,nChildCount,0); } // memset(pKBus->KBusChnStats,0,sizeof(pKBus->KBusChnStats)); return iRet; } int KBusSetEvCallBackFunc(stKBusDef * pKBus, KBusEvCBFuncDef CBFunc) { pKBus->KBusEvCBFunc = CBFunc; // pKBus->bKBusEvCBFuncSet=1; return 0; } //unsigned char tempdata [8] = {11,12,13,14,25,26,27,28}; int KBusReqService (stKBusDef * pKBus, int ReqId, int nParam1, int nParam2, void **pData, unsigned short * len1) { switch (ReqId) { case ReqStartDiag: break; case ReqStopDiag: break; case ReqPortChnInfo: *pData = &pKBus->KBusChnStats[nParam1]; *len1 = sizeof(stChnStat); break; case ReqPortChildInfo: *pData = &pKBus->DeviceInfos[nParam1]; *len1 = sizeof(stDeviceInfo); // *pData = tempdata; // *len1 = sizeof(tempdata); break; case ReqBlinkLED: if (pKBus->bReq == 1) return -1; if (nParam1 != 0xff && nParam1> pKBus->nChildCount) return -2; if (pKBus->bReq != 1 ) { pKBus->bReq=1; pKBus->nReqSvrId = ReqId; pKBus->nReqChildId = nParam1; pKBus->nReqParam = nParam2; pKBus->nReqReCount = 0; } break; case ReqTransBlink: if (pKBus->bReq == 1) return -1; if (nParam1 != 0xff && nParam1> pKBus->nChildCount) return -2; if (pKBus->bReq != 1 ) { pKBus->bReq=1; pKBus->nReqSvrId = ReqId; pKBus->nReqChildId = nParam1; pKBus->nReqParam = nParam2; pKBus->nReqReCount = 0; } break; case ReqTransCfg: break; case ReqUpdateFirm: if (pKBus->bReq == 1) return -1; if (nParam1> pKBus->nChildCount) return -2; if (pKBus->bReq != 1 ) { pKBus->bReq=1; pKBus->nReqSvrId = ReqId; pKBus->nReqChildId = nParam1; pKBus->nReqParam = nParam2; pKBus->nReqReCount = 0; if (*len1>0) { memcpy(pKBus->pReqDatas,*pData,*len1); pKBus->nReqDataLen = *len1; pKBus->nReqDataOff = 0; } } break; case ReqTransFirmware: break; default: if (pKBus->bReq == 1) return -1; if (nParam1 != 0xff && nParam1> pKBus->nChildCount) return -2; if (pKBus->bReq != 1 ) { pKBus->bReq=1; pKBus->nReqSvrId = ReqId; pKBus->nReqChildId = nParam1; pKBus->nReqParam = nParam2; pKBus->nReqReCount = 0; if (*len1>0) { memcpy(pKBus->pReqDatas,*pData,*len1); pKBus->nReqDataLen = *len1; pKBus->nReqDataOff = 0; } } break; } return 0; } int KBusStart(stKBusDef * pKBus) { int iRet = 0; return iRet; } int KBusLoopProcess(stKBusDef * pKBus) { int iRet = 0; if (pKBus->bMaster) { if (pKBus->nChildCount>0) KBusMasterFunc(&KBus1); } if (pKBus->bSlave) { KBusSlaveFunc(&KBus1); } if (pKBus->bRepeater) { } if (pKBus->RunStat) {pKBus->RunStat--;} if (pKBus->ErrStat) {pKBus->ErrStat--;} return iRet; } int KBusRepeaterFunc(stKBusDef * pKBus) { if ((KMem.nRunCount &0x7f) == 88) { ToggleRunLed(); } return 0; } int KBusUpdateChildInfo(stKBusDef * pKBus, int nChild, stDeviceInfo * pInfoBlock) { pKBus->DeviceInfos[nChild] = *pInfoBlock; return 0; } int KBusMakeDefaultClildInfo(stKBusDef * pKBus, int nChild) { // unsigned DefaultInBitCount = 8; // unsigned DefaultOutBitCount = 8; // stDeviceInfo * pDeviceInfo = &pKBus->DeviceInfos[nChild]; pKBus->DeviceInfos[nChild] = (stDeviceInfo){ .DeviceType=0, .DeviceVer=0, .InBitCount=8, .OutBitCount=8, .AIWCount=0, .AQWCount=0, .DWStartAddr=0, .OutDWCount=0 }; return 0; } //int KBusSetChildCfg(int nChn, int nChild, ) int KBusAddChildAddrByInfo(stKBusDef * pKBus, int nChild, stDeviceInfo * pDeviceInfo) { int iRet = 0; stClientCfg * pClientCfg = &pKBus->ClientCfg[nChild]; pClientCfg->Addr = pKBus->nCurPollId; pClientCfg->Configed = KBusConfiged; pClientCfg->InStartAddrBit = pKBus->CurXBitAddr; pClientCfg->OutStartAddrBit = pKBus->CurYBitAddr; pClientCfg->AIWStartAddrByte = pKBus->CurAIWAddr; pClientCfg->AQWStartAddrByte = pKBus->CurAQWAddr; pClientCfg->DIWStartAddrByte = pKBus->CurDIWAddr; pClientCfg->DOWStartAddrByte = pKBus->CurDOWAddr; pKBus->CurXBitAddr += pDeviceInfo->InBitCount; pKBus->CurYBitAddr += pDeviceInfo->OutBitCount; pKBus->CurAIWAddr += pDeviceInfo->AIWCount; pKBus->CurAQWAddr += pDeviceInfo->AQWCount; return iRet; } int KBusSearchChildProc(stKBusDef * pKBus) { int iRet = 0; uint32_t tick1=GetTick(); // uint32_t thisuS=GetuS(); int nThisPollId = pKBus->nCurPollId; if (pKBus->bMasterSent) { if (pKBus->bMasterRecved) { pKBus->bMasterSent=0; pKBus->RetryCount=0; nThisPollId++; pKBus->nCurPollId = nThisPollId; if (nThisPollId > pKBus->nChildCount) { // all query done, next stage pKBus->nCurPollId = 1; pKBus->MyStat = KBusStatReady; // CallBack if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvStateChange,0,0); } } if (!pKBus->bMasterRecved && tick1- pKBus->SendTimeTick > KBUS_WAIT_TIME_OUT1) { /* // timeOut, if (pKBus->RetryCount <3) { pKBus->RetryCount++; // resent query packet for curPollId; pKBus->bMasterSent=0; // no wait any more; // pKBus->bMasterSent=1; // pKBus->SendTimeTick = tick1; } if (pKBus->RetryCount >3 ) // */ { // Set Cur Child As Default KBusMakeDefaultClildInfo(pKBus,nThisPollId); KBusAddChildAddrByInfo(pKBus,nThisPollId,&pKBus->DeviceInfos[nThisPollId]); // Next Child; pKBus->RetryCount=0; nThisPollId++; pKBus->nCurPollId = nThisPollId; if (nThisPollId > pKBus->nChildCount) { // all query done, next stage pKBus->nCurPollId = 1; pKBus->MyStat = KBusStatReady; // CallBack if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvStateChange,0,0); } pKBus->bMasterSent=0; } } }else { // ! pKBus->bMasterSent »¹Ã»Óз¢ËÍ // Sent Query Packet for CurPollId; //pKBus->nCurPollId; unStatus ThisStatus; ThisStatus.nSeq = pKBus->nSeq; ThisStatus.nErr1 = (pKBus->KBusChnStats[nThisPollId].MStat==0); int len1=KBusMakePacket((pKBPacket)pKBus->PacketBuf2,0,nThisPollId,cmdQuery,ThisStatus.nStatus,0,NULL); // LL_USART_SetBaudRate(USART2,48000000,LL_USART_OVERSAMPLING_8,DefaultUart2Baud); pKBus->KBusSendPacket(pKBus->PacketBuf2, len1); pKBus->KBusChnStats[nThisPollId].SendPackets++; pKBus->KBusChnStats[nThisPollId].SendTimeInterval=pKBus->SendTimeTick-pKBus->KBusChnStats[nThisPollId].LastSentTimeTick; pKBus->KBusChnStats[nThisPollId].LastSentTimeTick=pKBus->SendTimeTick; // PacketLength = len1; pKBus->SendTime=tick1; pKBus->bMasterRecved=0; pKBus->bMasterRecvOK=0; pKBus->bMasterSent=1; pKBus->SendTimeTick = tick1; } // pKBus->MyStat = KBusStatReady; return iRet; } int KBusPacketSendDone(stKBusDef * pKBus) { switch (pKBus->MyStat){ case KBusStatUnInited: break; case KBusStatInited: break; case KBusStatConfig: break; case KBusStatReady: break; case KBusStatRunning: switch(pKBus->nRunStep) { case KBusRunStepBroadCastCfg: pKBus->nRunStep = KBusRunStepTimeSync; break; case KBusRunStepTimeSync: pKBus->nRunStep = KBusRunStepMultiDataPoll; break; case KBusRunStepMultiDataPoll: pKBus->nRunStep = KBusRunStepUniDataPoll; break; case KBusRunStepUniDataPoll: break; case KBusRunStepUniDataTranster: break; default: break; } // KBusDataPoll(pKBus); break; default: break; } return 0; } int GoStep(stKBusDef * pKBus, enKBusRunStep NextStep) { pKBus->nRunStep = NextStep; pKBus->TimeOutCount=0; return 0; } int KBusMasterFunc(stKBusDef * pKBus) { int len1; uint32_t tick1=GetTick(); ushort DataLen = 0; switch (pKBus->MyStat){ case KBusStatUnInited: KBusLoadSavedConfig(pKBus); pKBus->MyStat = KBusStatInited; pKBus->TimeOutCount=0; break; case KBusStatInited: pKBus->TimeOutCount++; if (pKBus->TimeOutCount>30){ pKBus->MyStat = KBusStatConfig; pKBus->TimeOutCount = 0; } break; case KBusStatConfig: KBusSearchChildProc(pKBus); break; case KBusStatReady: pKBus->MyStat = KBusStatRunning; pKBus->nRunStep = KBusRunStepBroadCastCfg; pKBus->TimeOutCount=0; break; case KBusStatRunning: switch(pKBus->nRunStep) { case KBusRunStepBroadCastCfg: pKBus->TimeOutCount++; if (pKBus->TimeOutCount>4){ pKBus->nRunStep = KBusRunStepTimeSync; pKBus->TimeOutCount = 0; } break; case KBusRunStepTimeSync: pKBus->Datas[0]=tick1&0xff; pKBus->Datas[1]=(tick1>>8)&0xff; pKBus->Datas[2]=(tick1>>16)&0xff; pKBus->Datas[3]=(tick1>>24)&0xff; len1=KBusMakePacket((pKBPacket)pKBus->PacketBuf2,0,0xff,cmdSyncTime,pKBus->nSeq,4,pKBus->Datas); pKBus->KBusSendPacket(pKBus->PacketBuf2, len1); pKBus->nRunStep = KBusRunStepTimeSyncWait; pKBus->TimeOutCount=0; break; case KBusRunStepTimeSyncWait: pKBus->TimeOutCount++; if (pKBus->TimeOutCount>2){ pKBus->nRunStep = KBusRunStepMultiDataPoll; pKBus->TimeOutCount=0; } break; case KBusRunStepMultiDataPoll: pKBus->TimeOutCount++; if (pKBus->TimeOutCount>1){ pKBus->nCurPollId = 1; pKBus->nRunStep = KBusRunStepUniDataPoll; pKBus->TimeOutCount=0; } break; case KBusRunStepUniDataPoll: KBusDataPoll(pKBus); break; case KBusRunStepUniDataTranster: // ¿´ÊÇ·ñÓÐÔ¶³ÌÇëÇó if (pKBus->bReq &&pKBus->nReqReCount<1) { pKBus->nReqDataHdr[0]=0; pKBus->nReqDataHdr[1]=0; pKBus->nReqDataHdr[2]=pKBus->nReqSvrId; pKBus->nReqDataHdr[3]=pKBus->nReqParam; // [4] Addr // [5] Addr // [6] nCount DataLen = pKBus->nReqDataLen+2; len1=KBusMakePacket((pKBPacket)pKBus->PacketBuf2,0,pKBus->nReqChildId,cmdRemoteReq,pKBus->nSeq,DataLen,&pKBus->nReqDataHdr[2]); pKBus->KBusSendPacket(pKBus->PacketBuf2, len1); pKBus->nReqReCount++; }else { } pKBus->nRunStep = KBusRunStepUniDataTransterWait; pKBus->TimeOutCount=0; break; case KBusRunStepUniDataTransterWait: pKBus->TimeOutCount++; if (pKBus->TimeOutCount>4){ if (pKBus->bReq ==1) {pKBus->bReq = 3;} pKBus->nRunStep = KBusRunStepBroadCastCfg; pKBus->TimeOutCount=0; } break; default: break; } break; default: break; } return 0; } int KBusDataPoll(stKBusDef * pKBus) { int iRet = 0; uint32_t tick1=GetTick(); // uint32_t thisuS=GetuS(); int len1=0; int nThisPollId = pKBus->nCurPollId; stChnStat * pChnStat = &pKBus->KBusChnStats[nThisPollId]; if (!pKBus->bMasterSent){ pKBus->Datas[0]=KBusMem.WLYB[nThisPollId -1 ]; for (int i=1; i*8 < pKBus->DeviceInfos[nThisPollId].OutBitCount;i++){ pKBus->Datas[0+i]=KBusMem.WLYB[nThisPollId -1 + i]; } // pKBus->Datas[1]=KBusMem.WLYB[nThisPollId ];; // pKBus->Datas[2]=KBusMem.WLYB[nThisPollId + 1 ]; //KBusChnStats[nCurPollId].Stat; // pKBus->Datas[3]=KBusMem.WLYB[nThisPollId + 2 ]; pKBus->Datas[4]=tick1&0xff; pKBus->Datas[5]=(tick1>>8)&0xff; pKBus->Datas[6]=(tick1>>16)&0xff; pKBus->Datas[7]=(tick1>>24)&0xff; pKBus->SendTimeTick=tick1; unStatus ThisStatus; ThisStatus.nStatus= pChnStat->MStat; ThisStatus.nSeq = pKBus->nSeq; //ThisStatus.nErr1 = (pChnStat->MStat==0); // ThisStatus.nErr1 = 0; len1=KBusMakePacket((pKBPacket)pKBus->PacketBuf2,0,nThisPollId,cmdUniExChgData,ThisStatus.nStatus,8,pKBus->Datas); // LL_USART_SetBaudRate(USART2,48000000,LL_USART_OVERSAMPLING_8,DefaultUart2Baud); pKBus->KBusSendPacket(pKBus->PacketBuf2, len1); pChnStat->SendPackets++; pChnStat->SendTimeInterval=pKBus->SendTimeTick - pChnStat->LastSentTimeTick; pChnStat->LastSentTimeTick=pKBus->SendTimeTick; // PacketLength = len1; pKBus->SendTime=tick1; pKBus->bMasterSent = 1; pKBus->bMasterRecved=0; pKBus->bMasterRecvOK=0; // LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5); //ToggleErrLed(); // ToggleOut8(); }else { if ((pKBus->bMasterRecved && pKBus->bMasterRecvOK && tick1-pKBus->SendTimeTick>1) || tick1-pKBus->SendTimeTick>KBUS_WAIT_TIME_OUT2) { if (!pKBus->bMasterRecvOK) { pKBus->TimeOutCount++; // Uart2Stat.TimeOutErr++; pChnStat->LostPackets++; pChnStat->CtnLstPkts++; if (!pKBus->bMasterRecved) {pChnStat->TimeOutErr++;} if (pChnStat->CtnLstPkts > pChnStat->MaxCtnLstPkts) {pChnStat->MaxCtnLstPkts = pChnStat->CtnLstPkts;} if (pChnStat->CtnLstPkts>3) { pChnStat->MStat = 0; pKBus->ErrStat=200; KBusMem.WLXB[nThisPollId]=0; if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvStateChange,0,0); if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvDataUpdate,0,0); } // LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7); }else { // pChnStat->CtnLstPkts = 0; // pKBus->KBusChnStats[nThisPollId].CtnLstPkts=0; pChnStat->MStat=1; pKBus->RunStat=100; } if (pKBus->DeviceInfos[nThisPollId].InBitCount >8 || pKBus->DeviceInfos[nThisPollId].OutBitCount > 8) { nThisPollId += 2; //nThisPollId ++; }else { nThisPollId ++; } if (nThisPollId > pKBus->nChildCount) { // Êý¾ÝÂÖѯÍê, ÖÜÆÚ¼ä϶,²åÈëÆäËû´¦ÀíÊý¾Ý. ÿ´Î´¦ÀíµÄʱ¼ä¼ä϶, Ò»ÎÊ Ò»´ð ʱ¼ä. // ·Ö³É¼¸ÖÖÀàÐÍ, ÂÖÁ÷½øÐÐ // ʱ¼äͬ²½, ²éѯÐÂ×Ó»ú, ´¦Àí´øÍâÊý¾Ý, ,´¦Àí¶îÍâµÄÊÂÇé, »òÕßÌø¹ý. pKBus->CircleTime=tick1-pKBus->LastCircleStartTime; pKBus->LastCircleStartTime=tick1; pKBus->nSeq++; GoStep(pKBus,KBusRunStepUniDataTranster); //nThisPollId=1; }else { pKBus->nCurPollId = nThisPollId; } pKBus->bMasterSent = 0; } } return iRet; } int KBusSlaveFunc(stKBusDef * pKBus) { // int ThisuS=GetuS(); int ThisTick = GetTick(); int thisRecvTime=pKBus->RecvTimeTick; if (pKBus->nStationId >0) { if (pKBus->bSlaveRecved) { pKBus->RunStat=8000; pKBus->bSlaveRecved=0; }else if ((ThisTick - thisRecvTime) > KBUS_SLAVE_TIME_OUT) // 30u { pKBus->ErrStat=7000; }else if ( ThisTick > (thisRecvTime + KBUS_SLAVE_TIME_OUT)) // 30u { pKBus->ErrStat=7100; } } return 0; } int KBusCheckPacket(stKBusDef * pKBus, pKBPacket p1, int nLen1) { unsigned char * p2 = (unsigned char *)p1; pKBPacket p3=p1; if ((p2[0] == KB_SYN || p2[0] != KBStartSign) && nLen1 > 1) { p3=(pKBPacket)(p2+1); nLen1--; } if (p3->Sign != KBStartSign) { // pKBus->NotPacketErr++; KMem.WDB[0x40]=pKBus->nCurPollId; KMem.WDB[0x41]=nLen1; memcpy(&KMem.WDB[0x42],p1,nLen1); pKBus->KBusChnStats[pKBus->nCurPollId].NotPkgErr++; if (pKBus->KBusDiagnosis) { if (pKBus->KBusSnapPos == 0) { KMem.WDB[0x20]=nLen1; memcpy(&KMem.WDB[0x21],p1,nLen1); pKBus->KBusSnapPos++; } else if (pKBus->KBusSnapPos==1) { KMem.WDB[0x40]=nLen1; memcpy(&KMem.WDB[0x41],p1,nLen1); pKBus->KBusSnapPos++; } else { KMem.WDB[0x60]=nLen1; memcpy(&KMem.WDB[0x61],p1,nLen1); pKBus->KBusSnapPos=0; } } return -1; } int DataLen=p3->DataLen; if (DataLen>KBMaxPacketLength) { // Uart2Stat.LengthErr++; pKBus->KBusChnStats[pKBus->nCurPollId].PkgLenErr++; return -1; } if (nLen1KBusChnStats[pKBus->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++; pKBus->KBusChnStats[pKBus->nCurPollId].BCCErr++; return -4; } return 0; } int KBusSlaveCheckPacket(stKBusDef * pKBus, pKBPacket p1, int nLen1) { unsigned char * p2 = (unsigned char *)p1; pKBPacket p3=p1; if ((p2[0] == KB_SYN || p2[0] != KBStartSign) && nLen1 > 1) { p3=(pKBPacket)(p2+1); nLen1--; } if (p3->Sign != KBStartSign) { // Uart2Stat.NotPacketErr++; pKBus->KBusChnStats[0].ClientNotPktErr++; return -1; } int DataLen=p3->DataLen; if (DataLen>KBMaxPacketLength) { // Uart2Stat.LengthErr++; pKBus->KBusChnStats[0].ClientPkgLenErr++; return -1; } if (nLen1KBusChnStats[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++; pKBus->KBusChnStats[0].ClientBccErr++; return -4; } return 0; } int KBusMasterParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1) { unsigned char * p6 = (unsigned char *)p1; if ((p6[0] == KB_SYN || p6[0] != KBStartSign) && Len1 > 1) { p1=(pKBPacket)(p6+1); Len1--; } int DataLen=p1->DataLen; pKBus->KBusChnStats[pKBus->nCurPollId].RecvPackets++; // pKBPacket p2=(pKBPacket)PacketBuf2; // int PacketLen=0; //LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_6); int nCurPollId = pKBus->nCurPollId; int ChildId=p1->SrcAddr; // int ThisuS = GetuS(); int ThisTick = GetTick(); unsigned char nIndex; switch (p1->nCMD) { case cmdNone: break; case cmdQueryRply: DataLen=sizeof(stDeviceInfo); KBusUpdateChildInfo(pKBus,nCurPollId,(stDeviceInfo *)&p1->data[0]); pKBus->KBusChnStats[nCurPollId].ClientSendPkts++; break; case cmdSetCfgRply: break; case cmdToRunModeRply: break; case cmdBroadCastCfg: break; case cmdMuExchgDataRply: break; case cmdToSafeMode: break; case cmdReHeartBeat: break; case cmdPing: break; case cmdPingReply: pKBus->DelayTick = ThisTick - pKBus->SendTimeTick; if (pKBus->DelayTick > pKBus->nMaxDelayTick) pKBus->nMaxDelayTick = pKBus->DelayTick; KBusMem.WLXB[ChildId]=p1->data[0]; //RunStat=100; pKBus->KBusChnStats[nCurPollId].CtnLstPkts=0; pKBus->KBusChnStats[nCurPollId].Delay=pKBus->DelayTick; if (pKBus->DelayTick > pKBus->KBusChnStats[nCurPollId].MaxDelay) pKBus->KBusChnStats[nCurPollId].MaxDelay=pKBus->DelayTick; //PutOutput(outputvalue); pKBus->bMasterRecvOK=1; break; case cmdRead: break; case cmdReadReply: break; case cmdWrite: break; case cmdWriteReply: pKBus->bMasterRecved=1; break; case cmdGetVersion: break; case cmdVerInfo: break; case cmdUniExChgData: break; case cmdExChgDataReply: pKBus->DelayTick = ThisTick - pKBus->SendTimeTick; if (pKBus->DelayTick > pKBus->nMaxDelayTick) pKBus->nMaxDelayTick = pKBus->DelayTick; KBusMem.WLXB[ChildId - 1 ]=p1->data[0]; for (int i=1; i*8 < pKBus->DeviceInfos[ChildId].InBitCount;i++){ KBusMem.WLXB[ChildId - 1 + i]=p1->data[0 + i];; } // KBusMem.WLXB[ChildId+0]=p1->data[1]; // KBusMem.WLXB[ChildId+1]=p1->data[2]; // KBusMem.WLXB[ChildId+2]=p1->data[3];; // KBusMem.WLXB[ChildId+3]=p1->data[4]; // KBusMem.WLXB[ChildId+4]=p1->data[5]; // KBusMem.WLXB[ChildId+5]=p1->data[6]; // KBusMem.WLXB[ChildId+6]=p1->data[7]; //Call Back if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvDataUpdate,0,0); //RunStat=100; pKBus->KBusChnStats[nCurPollId].CtnLstPkts=0; pKBus->KBusChnStats[nCurPollId].Delay=pKBus->DelayTick; if (pKBus->DelayTick > pKBus->KBusChnStats[nCurPollId].MaxDelay) pKBus->KBusChnStats[nCurPollId].MaxDelay=pKBus->DelayTick; //PutOutput(outputvalue); if (DataLen>=14) { nIndex=p1->data[9]; pKBus->KBusChnStats[nCurPollId].ClientDatas[nIndex]=p1->data[10]|(p1->data[11]<<8)|(p1->data[12]<<16)|(p1->data[13]<<24); } pKBus->bMasterRecvOK=1; break; case cmdRemoteReqReply: pKBus->bReq = 2; break; default: break; } // nCurPollId ++; // if (nCurPollId > nChilds) // { // nCurPollId=1; // } return 0; } int KBusSlaveParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1) { unsigned char * p3 = (unsigned char *)p1; if (p3[0] == KB_SYN && Len1 > 1) { p1=(pKBPacket)(p3+1); Len1--; } // Uart2Stat.OKPacket++; unsigned short Addr; unsigned short DataLen=p1->DataLen; //int nSrcAddr=p1->SrcAddr; int nDstHost=p1->DstHost; // KBusRecvTimeuS=ThisuS; // KBusSlaveRecved=1; pKBPacket p2=(pKBPacket)pKBus->PacketBuf2; void * pData = 0; int PacketLen=0; unsigned char nIndex;// = p1->nStatus & 0x07; if (nDstHost!=pKBus->nStationId && nDstHost != 0xff) { pKBus->KBusChnStats[0].ClientMisIdPkts++; return -1; } // int ThisuS = GetuS(); int ThisTick = GetTick(); if (nDstHost==0xff){ pKBus->RecvTimeTick=ThisTick; pKBus->bSlaveRecved=1; switch (p1->nCMD) { case cmdNone: break; case cmdBroadCastCfg: break; case cmdMuExchgData: break; case cmdToSafeMode: break; case cmdHeartBeat: break; case cmdSyncRead: break; case cmdSyncWrite: break; case cmdSequenRead: break; case cmdSyncTime: pKBus->nSlaveTick=p1->data[0]+(p1->data[1]<<8)+(p1->data[2]<<16)+(p1->data[3]<<24); if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvTimeSync,0,0); break; case cmdRemoteReq: //ÊÕµ½RemoteÇëÇó // KBusSlaveRunRemoteReq(pKBus,p1->data[0],p1->data, DataLen); // data[0] -> reqSvr; // data[1] -> param; switch (p1->data[0]) { case ReqBlinkLED: case ReqTransBlink: KMRunStat.bLEDFlick=p1->data[1]; DataLen = 0; PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data); pKBus->KBusSendPacket((uchar *)p2, PacketLen); break; case ReqUpdateFirm: //37¸ö×Ö½Ú DataLen = 0; PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data); pKBus->KBusSendPacket((uchar *)p2, PacketLen); pData = &p1->data[5]; Addr = p1->data[2] + (p1->data[3] <<8); DataLen = p1->data[4] ; KMRunService(p1->data[0],Addr,0,&pData,&DataLen); DataLen=0; break; case ReqUpdateFirmInfo: DataLen = 0; PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data); pKBus->KBusSendPacket((uchar *)p2, PacketLen); pData = &p1->data[5]; Addr = p1->data[2] + (p1->data[3] <<8); DataLen = p1->data[4] ; KMRunService(p1->data[0],Addr,0,&pData,&DataLen); DataLen=0; break; default: pData = &p1->data[5]; Addr = p1->data[2] + (p1->data[3] <<8); DataLen = p1->data[4] ; KMRunService(p1->data[0],p1->data[1],0,&pData,&DataLen); DataLen = 0; PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data); pKBus->KBusSendPacket((uchar *)p2, PacketLen); DataLen=0; break; } break; default: break; } } if (nDstHost==pKBus->nStationId) { pKBus->RecvTimeTick=ThisTick; pKBus->bSlaveRecved=1; switch (p1->nCMD) { case cmdNone: break; case cmdQuery: DataLen=sizeof(stDeviceInfo); PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdQueryRply,p1->nStatus,DataLen,pKBus->pMyDeviceInfo); pKBus->KBusChnStats[0].ClientSendPkts++; pKBus->KBusSendPacket((uchar *)p2, PacketLen); break; case cmdSetCfg: break; case cmdToRunMode: break; case cmdBroadCastCfg: break; case cmdMuExchgData: break; case cmdToSafeMode: break; case cmdHeartBeat: break; case cmdPing: KBusMem.WLYB[0]=p1->data[0]; //PutOutput(outputvalue); //memcpy(DispBuf,p1->data+2,8); p1->data[0]=KBusMem.WLXB[0]; //pKBus->RecvTimeuS=ThisuS; PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdPingReply,p1->nStatus,DataLen,p1->data); pKBus->KBusChnStats[0].ClientSendPkts++; pKBus->KBusSendPacket((uchar *)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); pKBus->KBusChnStats[0].ClientSendPkts++; pKBus->KBusSendPacket((uchar *)p2, PacketLen); break; case cmdWriteReply: break; case cmdGetVersion: break; case cmdVerInfo: break; case cmdUniExChgData: KBusMem.WLYB[0]=p1->data[0]; KBusMem.WLYB[1]=p1->data[1]; KBusMem.WLYB[2]=p1->data[2]; KBusMem.WLYB[3]=p1->data[3]; // pKBus->nSlaveTick=p1->data[4]+(p1->data[5]<<8);//+(p1->data[6]<<16)+(p1->data[7]<<24); if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvTimeSync,0,0); if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvDataUpdate,0,0); nIndex=pKBus->nClientDataIndex; p1->data[0]=KBusMem.WLXB[0]; p1->data[1]=KBusMem.WLXB[1]; p1->data[2]=KBusMem.WLXB[2]; p1->data[3]=KBusMem.WLXB[3]; p1->data[4]=KBusMem.WLXB[4]; p1->data[5]=KBusMem.WLXB[5]; p1->data[6]=KBusMem.WLXB[6]; p1->data[7]=KBusMem.WLXB[7]; p1->data[9]=nIndex; p1->data[10]=pKBus->KBusChnStats[0].ClientDatas[nIndex]; p1->data[11]=pKBus->KBusChnStats[0].ClientDatas[nIndex]>>8; p1->data[12]=pKBus->KBusChnStats[0].ClientDatas[nIndex]>>16; p1->data[13]=pKBus->KBusChnStats[0].ClientDatas[nIndex]>>24; pKBus->nClientDataIndex++; DataLen = 14; if (pKBus->nClientDataIndex >= 10) { pKBus->nClientDataIndex=0;} unStatus nStatus; nStatus.nStatus = p1->nStatus; if (nStatus.nErr1) { pKBus->ErrStat=8100;} PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdExChgDataReply,p1->nStatus,DataLen,p1->data); pKBus->KBusChnStats[0].ClientSendPkts++; pKBus->KBusSendPacket((uchar *)p2, PacketLen); break; case cmdExChgDataReply: break; case cmdSyncRead: break; case cmdSyncWrite: break; case cmdSequenRead: break; case cmdSyncTime: break; case cmdRemoteReq: //ÊÕµ½RemoteÇëÇó // KBusSlaveRunRemoteReq(pKBus,p1->data[0],p1->data, DataLen); // data[0] -> reqSvr; // data[1] -> param; switch (p1->data[0]) { case ReqBlinkLED: case ReqTransBlink: KMRunStat.bLEDFlick=p1->data[1]; DataLen = 0; PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data); pKBus->KBusSendPacket((uchar *)p2, PacketLen); break; case ReqUpdateFirm: //37¸ö×Ö½Ú DataLen = 0; PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data); pKBus->KBusSendPacket((uchar *)p2, PacketLen); pData = &p1->data[5]; Addr = p1->data[2] + (p1->data[3] <<8); DataLen = p1->data[4] ; KMRunService(p1->data[0],Addr,0,&pData,&DataLen); DataLen=0; break; case ReqUpdateFirmInfo: DataLen = 0; PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data); pKBus->KBusSendPacket((uchar *)p2, PacketLen); pData = &p1->data[5]; Addr = p1->data[2] + (p1->data[3] <<8); DataLen = p1->data[4] ; KMRunService(p1->data[0],Addr,0,&pData,&DataLen); DataLen=0; break; default: pData = &p1->data[5]; Addr = p1->data[2] + (p1->data[3] <<8); DataLen = p1->data[4] ; KMRunService(p1->data[0],p1->data[1],0,&pData,&DataLen); DataLen = 0; PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdRemoteReqReply,p1->nStatus,DataLen,p1->data); pKBus->KBusSendPacket((uchar *)p2, PacketLen); DataLen=0; break; } break; default: break; } } return 0; } int KBusSlaveRunRemoteReq(stKBusDef * pKBus, int nReqSvrId, unsigned char * pData , int Len1) { // int PacketLen; switch (nReqSvrId) { case ReqTransBlink: KMRunStat.bLEDFlick=5; // PacketLen=KBusMakePacket(pKBus->PacketBuf2,pKBus->nStationId,0,cmdRemoteReq,p1->nStatus,DataLen,p1->data); break; default: break; } return 0; } int KBusParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1) { int ThisuS=GetuS(); int Result=0; unsigned char * p2 = (unsigned char *)p1; if (p2[0] == KB_SYN && Len1 > 1) { p1=(pKBPacket)(p2+1); Len1--; } if (pKBus->bMaster) { pKBus->bMasterRecved=1; Result=KBusCheckPacket(pKBus, p1, Len1); if (Result != S_OK) { return Result; } pKBus->bMasterRecvOK=1; Result=KBusMasterParsePacket(pKBus, p1, Len1); return Result; } if (pKBus->bSlave) { pKBus->KBusChnStats[0].ClientRecvPkts++; Result=KBusSlaveCheckPacket(pKBus, p1, Len1); if (Result != S_OK) { return Result; } pKBus->KBusChnStats[0].ClientTimeOutErr=pKBus->RunStat; Result=KBusSlaveParsePacket(pKBus, 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; } */ unsigned char KBusBCC(void * pData, int nSize) { unsigned char k; k=0; for (int i=0;iSign=KBStartSign; 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]=KBEndSign; 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]=KBEndSign; 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]=KBEndSign; 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]=KBEndSign; 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]=KBEndSign; PacketLenth=sizeof(stKBPacket)+DataLen+1; break; case cmdUniExChgData: p1->DataLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); p1->data[DataLen+1]=KBEndSign; 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]=KBEndSign; 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]=KBEndSign; 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]=KBEndSign; 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]=KBEndSign; 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]=KBEndSign; PacketLenth=sizeof(stKBPacket)+DataLen+1; break; default: p1->DataLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); p1->data[DataLen+1]=KBEndSign; PacketLenth=sizeof(stKBPacket)+DataLen+1; break; } return PacketLenth; }