/** ****************************************************************************** * @file : KBus.c * @brief : K-Bus Protocol program body ****************************************************************************** */ #include "KBus.h" #include "functions.h" #include "string.h" int bMaster=0,bSlave=0; unsigned char PacketBuf1[128]; unsigned char PacketBuf2[128]; unsigned char BufferIn[16]={0}; unsigned char BufferOut[16]={0}; stChnStat ChnStats[8]; int nAddr=0; int nChilds; int ChildStat[16]; int nCurPollId=0; int nSeq=0; int MasterRecved=1; volatile int SlaveRecved=1; unsigned int SendTimeuS=0; volatile int RecvTimeuS=0; int DelayuS=0; int MaxDelayuS=0; int ThisuS; volatile unsigned int nSlaveTick=0; unsigned char BCC(void * pData, int nSize) { unsigned char k; k=0; for (int i=0;iSign=StartSign; p1->DstAddr=dst; p1->SrcAddr=src; p1->nCMD=nType; p1->nSEQ=nSEQ; int PacketLenth=0; switch (nType) { case cmdNone: break; case cmdPing: p1->PacketLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1); p1->data[DataLen+1]=EndSign; PacketLenth=sizeof(stPacket)+DataLen+1; break; case cmdPingReply: p1->PacketLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1); p1->data[DataLen+1]=EndSign; PacketLenth=sizeof(stPacket)+DataLen+1; break; case cmdRead: break; case cmdReadReply: break; case cmdWrite: break; case cmdWriteReply: p1->PacketLen=DataLen; if (DataLen !=0 ) memcpy(p1->data,pData,DataLen); p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1); p1->data[DataLen+1]=EndSign; PacketLenth=sizeof(stPacket)+DataLen+1; break; case cmdGetVersion: p1->PacketLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1); p1->data[DataLen+1]=EndSign; PacketLenth=sizeof(stPacket)+DataLen+1; break; case cmdVerInfo: p1->PacketLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1); p1->data[DataLen+1]=EndSign; PacketLenth=sizeof(stPacket)+DataLen+1; break; case cmdExChgData: p1->PacketLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1); p1->data[DataLen+1]=EndSign; PacketLenth=sizeof(stPacket)+DataLen+1; break; case cmdExChgDataReply: p1->PacketLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1); p1->data[DataLen+1]=EndSign; PacketLenth=sizeof(stPacket)+DataLen+1; break; case cmdSyncRead: p1->PacketLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1); p1->data[DataLen+1]=EndSign; PacketLenth=sizeof(stPacket)+DataLen+1; break; case cmdSyncWrite: p1->PacketLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1); p1->data[DataLen+1]=EndSign; PacketLenth=sizeof(stPacket)+DataLen+1; break; case cmdSequenRead: p1->PacketLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1); p1->data[DataLen+1]=EndSign; PacketLenth=sizeof(stPacket)+DataLen+1; break; case cmdSyncTime: p1->PacketLen=DataLen; memcpy(p1->data,pData,DataLen); p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1); p1->data[DataLen+1]=EndSign; PacketLenth=sizeof(stPacket)+DataLen+1; break; default: break; } return PacketLenth; } int CheckPacket(pPacket p1,int nLen1) { if (p1->Sign != StartSign) { Uart2Stat.NotPacketErr++; ChnStats[nCurPollId].NotPkgErr++; return -1; } int DataLen=p1->PacketLen; if (DataLen>MaxPacketLength) { Uart2Stat.LengthErr++; ChnStats[nCurPollId].PkgLenErr++; return -1; } if (nLen1data[DataLen+1] != EndSign) // { // ChnStats[nCurPollId].NoEndErr++; // Uart2Stat.LengthErr++; // return -2; // } unsigned char thisBCC=BCC(p1,sizeof(stPacket)+DataLen-1); if (thisBCC != p1->data[DataLen]) {//BCC Error; Uart2Stat.BCCerr++; return -4; } return 0; } int SlaveCheckPacket(pPacket p1,int nLen1) { if (p1->Sign != StartSign) { Uart2Stat.NotPacketErr++; ChnStats[0].NotPkgErr++; return -1; } int DataLen=p1->PacketLen; if (DataLen>MaxPacketLength) { Uart2Stat.LengthErr++; ChnStats[0].PkgLenErr++; return -1; } if (nLen1data[DataLen+1] != EndSign) // { // ChnStats[nCurPollId].NoEndErr++; // Uart2Stat.LengthErr++; // return -2; // } unsigned char thisBCC=BCC(p1,sizeof(stPacket)+DataLen-1); if (thisBCC != p1->data[DataLen]) {//BCC Error; Uart2Stat.BCCerr++; ChnStats[0].BCCErr++; return -4; } return 0; } int MasterParsePacket(pPacket p1,int Len1) { int DataLen=p1->PacketLen; ChnStats[nCurPollId].RecvPackets++; pPacket p2=(pPacket)PacketBuf2; int PacketLen=0; //LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_6); int ChildId=p1->SrcAddr; unsigned char nIndex; switch (p1->nCMD) { case cmdNone: break; case cmdPing: BufferIn[ChildId]=p1->data[0]; PacketLen=MakePacket(p2,0,nCurPollId,cmdPingReply,p1->nSEQ,DataLen,p1->data); SendPacket2(p2,PacketLen); break; case cmdPingReply: DelayuS=ThisuS-SendTimeuS; if (DelayuS > MaxDelayuS) MaxDelayuS = DelayuS; BufferIn[ChildId]=p1->data[0]; //RunStat=100; ChnStats[nCurPollId].CtnLstPkts=0; ChnStats[nCurPollId].Delay=DelayuS; if (DelayuS > ChnStats[nCurPollId].MaxDelay) ChnStats[nCurPollId].MaxDelay=DelayuS; //PutOutput(outputvalue); MasterRecved=1; break; case cmdRead: break; case cmdReadReply: break; case cmdWrite: break; case cmdWriteReply: MasterRecved=1; break; case cmdGetVersion: break; case cmdVerInfo: break; case cmdExChgData: BufferIn[0]=p1->data[0]; //PutOutput(outputvalue); //memcpy(DispBuf,p1->data+2,8); p1->data[0]=BufferOut[0]; PacketLen=MakePacket(p2,nAddr,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data); SendPacket2(p2,PacketLen); break; case cmdExChgDataReply: DelayuS=ThisuS-SendTimeuS; if (DelayuS > MaxDelayuS) MaxDelayuS = DelayuS; BufferIn[ChildId]=p1->data[0]; //RunStat=100; ChnStats[nCurPollId].CtnLstPkts=0; ChnStats[nCurPollId].Delay=DelayuS; if (DelayuS > ChnStats[nCurPollId].MaxDelay) ChnStats[nCurPollId].MaxDelay=DelayuS; //PutOutput(outputvalue); nIndex=p1->data[3]; ChnStats[nCurPollId].ClientDatas[nIndex]=p1->data[4]|(p1->data[5]<<8)|(p1->data[6]<<16)|(p1->data[7]<<24); MasterRecved=1; break; default: break; } // nCurPollId ++; // if (nCurPollId > nChilds) // { // nCurPollId=1; // } return 0; } int SlaveParsePacket(pPacket p1,int Len1) { Uart2Stat.OKPacket++; int DataLen=p1->PacketLen; //int nSrcAddr=p1->SrcAddr; int nDstAddr=p1->DstAddr; // RecvTimeuS=ThisuS; // SlaveRecved=1; pPacket p2=(pPacket)PacketBuf2; int PacketLen=0; unsigned char nIndex = p1->nSEQ & 0x07; if (nDstAddr!=nAddr && nDstAddr != 0xff) { ChnStats[0].ClientMisIdPkts++; return -1; } if (nDstAddr==nAddr || nDstAddr==0xff) { RecvTimeuS=ThisuS; SlaveRecved=1; switch (p1->nCMD) { case cmdNone: break; case cmdPing: BufferIn[0]=p1->data[0]; //PutOutput(outputvalue); //memcpy(DispBuf,p1->data+2,8); p1->data[0]=BufferOut[0]; RecvTimeuS=ThisuS; PacketLen=MakePacket(p2,nAddr,0,cmdPingReply,p1->nSEQ,DataLen,p1->data); ChnStats[0].ClientSendPkts++; SendPacket2(p2,PacketLen); break; case cmdPingReply: break; case cmdRead: break; case cmdReadReply: break; case cmdWrite: //memcpy(DispBuf,p1->data,DataLen); PacketLen=MakePacket(p2,1,0,cmdWriteReply,p1->nSEQ,0,0); ChnStats[0].ClientSendPkts++; SendPacket2(p2,PacketLen); break; case cmdWriteReply: break; case cmdGetVersion: break; case cmdVerInfo: break; case cmdExChgData: BufferIn[0]=p1->data[0]; nSlaveTick=p1->data[4]+(p1->data[5]<<8);//+(p1->data[6]<<16)+(p1->data[7]<<24); //PutOutput(outputvalue); //memcpy(DispBuf,p1->data+2,8); p1->data[0]=BufferOut[0]; p1->data[3]=nIndex; p1->data[4]=ChnStats[0].ClientDatas[nIndex]; p1->data[5]=ChnStats[0].ClientDatas[nIndex]>>8; p1->data[6]=ChnStats[0].ClientDatas[nIndex]>>16; p1->data[7]=ChnStats[0].ClientDatas[nIndex]>>24; nIndex++; PacketLen=MakePacket(p2,nAddr,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data); ChnStats[0].ClientSendPkts++; SendPacket2(p2,PacketLen); break; case cmdExChgDataReply: break; case cmdSyncRead: break; case cmdSyncWrite: break; case cmdSequenRead: break; case cmdSyncTime: nSlaveTick=p1->data[0]+(p1->data[1]<<8)+(p1->data[2]<<16)+(p1->data[3]<<24); break; default: break; } } return 0; } int ParsePacket(pPacket p1,int Len1) { ThisuS=GetuS(); int Result=0; if (bMaster) { Result=CheckPacket(p1,Len1); if (Result != S_OK) { return Result; } Result=MasterParsePacket(p1,Len1); return Result; } if (bSlave) { ChnStats[0].ClientRecvPkts++; Result=CheckPacket(p1,Len1); if (Result != S_OK) { return Result; } Result=SlaveParsePacket(p1,Len1); return Result; } //int len1=p1->PacketLen; // if (p1->DstAddr!=255&&p1->DstAddr!=2) return -3; // pPacket p2=(pPacket)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; }