From 7d8ba5df7d883c86c24aa38449c4a6dd126e920f Mon Sep 17 00:00:00 2001 From: QuakeGod <quakegod@sina.com> Date: 星期二, 06 八月 2024 14:41:31 +0800 Subject: [PATCH] Radio_LLCC68 1.15, Stable version --- ComLib/Src/KBus.c | 1519 +++++++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 1,074 insertions(+), 445 deletions(-) diff --git a/ComLib/Src/KBus.c b/ComLib/Src/KBus.c index f77d852..972e77e 100644 --- a/ComLib/Src/KBus.c +++ b/ComLib/Src/KBus.c @@ -9,212 +9,698 @@ #include "string.h" #include "stm32f0xx.h" -unsigned char bKBusMaster=0,bKBusSlave=0,bKBusRepeater=0;; +//unsigned char bKBusMaster=0,bKBusSlave=0,bKBusRepeater=0;; -unsigned char PacketBuf1[128]; -unsigned char PacketBuf2[128]; +stKBusMem KBusMem; -unsigned char BufferIn[16]={0}; -unsigned char BufferOut[16]={0}; - -unsigned char KBusDiagnosis =1; -unsigned char KBusSnapPos = 0; - -stChnStat KBusChnStats[8]; -unsigned char nStationID=0; -unsigned char nChilds; -int ChildStat[16]; -unsigned char nCurPollId=0; -unsigned char nSeq=0; - -volatile unsigned char KBusMasterRecved=1; -volatile unsigned char KBusMasterRecvOK=1; - -volatile unsigned char KBusSlaveRecved=1; -unsigned int KBusSendTimeuS=0; -volatile int KBusRecvTimeuS=0; - -int KBusDelayuS=0; -int KBusMaxDelayuS=0; -int ThisuS; -volatile unsigned int nSlaveTick=0; - -int Clk3=0; -int SendTime,Latancy,LatancyClk,SendClk; - -volatile int nCount2=0; -int TimeOutCount=0; -int LastCircleStartTime=0; -int CircleTime=0; - -unsigned char Datas[128]; -volatile int PacketLength = 0; - -stClientInfo ClientInfo[16]; - - -unsigned char KBusBCC(void * pData, int nSize) +int KBusLoadSavedConfig(stKBusDef * pKBus) { - unsigned char k; - k=0; - for (int i=0;i<nSize;i++) - { - k^=((unsigned char *)pData)[i]; - } - return k; + int iRet=0; + + + return iRet; } -int KBusMakePacket(pKBPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nStatus, unsigned char DataLen,void * pData ) +int KBusSaveConfig(int nChn) { - p1->Sign=StartSign; - p1->DstHost=dst; - p1->SrcAddr=src; - p1->nCMD=nType; - p1->nStatus=nStatus; - int PacketLenth=0; - switch (nType) + 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;i<MAX_CLIENT;i++) { - case cmdNone: + memset(&pKBus->KBusChnStats[i],0,sizeof(stChnStat)); + } + for (int i=0;i<MAX_CLIENT;i++) + { + KBusMakeDefaultClildInfo(pKBus, i); + } +/* + stDeviceInfo * pDeviceInfo = &pKBus->DeviceInfos[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;i<MAX_CLIENT;i++) + { + memset(&pKBus->KBusChnStats[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 cmdPing: - p1->DataLen=DataLen; - memcpy(p1->data,pData,DataLen); - p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); - p1->data[DataLen+1]=EndSign; - - PacketLenth=sizeof(stKBPacket)+DataLen+1; + case ReqStopDiag: break; - case cmdPingReply: - p1->DataLen=DataLen; - memcpy(p1->data,pData,DataLen); - p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); - p1->data[DataLen+1]=EndSign; - PacketLenth=sizeof(stKBPacket)+DataLen+1; - break; - case cmdRead: - break; - case cmdReadReply: - break; - case cmdWrite: - break; - case cmdWriteReply: - p1->DataLen=DataLen; - if (DataLen !=0 ) memcpy(p1->data,pData,DataLen); - p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); - p1->data[DataLen+1]=EndSign; - PacketLenth=sizeof(stKBPacket)+DataLen+1; - break; - case cmdGetVersion: - p1->DataLen=DataLen; - memcpy(p1->data,pData,DataLen); - p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); - p1->data[DataLen+1]=EndSign; - PacketLenth=sizeof(stKBPacket)+DataLen+1; - break; - case cmdVerInfo: - p1->DataLen=DataLen; - memcpy(p1->data,pData,DataLen); - p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); - p1->data[DataLen+1]=EndSign; - PacketLenth=sizeof(stKBPacket)+DataLen+1; - break; - case cmdExChgData: - p1->DataLen=DataLen; - memcpy(p1->data,pData,DataLen); - p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); - p1->data[DataLen+1]=EndSign; - PacketLenth=sizeof(stKBPacket)+DataLen+1; - break; - case cmdExChgDataReply: - p1->DataLen=DataLen; - memcpy(p1->data,pData,DataLen); - p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); - p1->data[DataLen+1]=EndSign; - PacketLenth=sizeof(stKBPacket)+DataLen+1; - break; - - case cmdSyncRead: - p1->DataLen=DataLen; - memcpy(p1->data,pData,DataLen); - p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); - p1->data[DataLen+1]=EndSign; - PacketLenth=sizeof(stKBPacket)+DataLen+1; - break; - case cmdSyncWrite: - p1->DataLen=DataLen; - memcpy(p1->data,pData,DataLen); - p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); - p1->data[DataLen+1]=EndSign; - PacketLenth=sizeof(stKBPacket)+DataLen+1; - break; - case cmdSequenRead: - p1->DataLen=DataLen; - memcpy(p1->data,pData,DataLen); - p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); - p1->data[DataLen+1]=EndSign; - PacketLenth=sizeof(stKBPacket)+DataLen+1; + 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; + } + } - case cmdSyncTime: - p1->DataLen=DataLen; - memcpy(p1->data,pData,DataLen); - p1->data[DataLen]=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); - p1->data[DataLen+1]=EndSign; - PacketLenth=sizeof(stKBPacket)+DataLen+1; break; + 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 PacketLenth; + return 0; } -int KBusCheckPacket(int nChn, pKBPacket p1, int nLen1) +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] == SYN || p2[0] != StartSign) && nLen1 > 1) + if ((p2[0] == KB_SYN || p2[0] != KBStartSign) && nLen1 > 1) { p3=(pKBPacket)(p2+1); nLen1--; } - if (p3->Sign != StartSign) + if (p3->Sign != KBStartSign) { - Uart2Stat.NotPacketErr++; - KBusChnStats[nCurPollId].NotPkgErr++; - if (KBusDiagnosis) { - if (KBusSnapPos == 0) { +// 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); - KBusSnapPos++; - } else if (KBusSnapPos==1) { + pKBus->KBusSnapPos++; + } else if (pKBus->KBusSnapPos==1) { KMem.WDB[0x40]=nLen1; memcpy(&KMem.WDB[0x41],p1,nLen1); - KBusSnapPos++; + pKBus->KBusSnapPos++; } else { KMem.WDB[0x60]=nLen1; memcpy(&KMem.WDB[0x61],p1,nLen1); - KBusSnapPos=0; + pKBus->KBusSnapPos=0; } } return -1; } int DataLen=p3->DataLen; - if (DataLen>MaxPacketLength) + if (DataLen>KBMaxPacketLength) { - Uart2Stat.LengthErr++; - KBusChnStats[nCurPollId].PkgLenErr++; +// Uart2Stat.LengthErr++; + pKBus->KBusChnStats[pKBus->nCurPollId].PkgLenErr++; return -1; } if (nLen1<DataLen+sizeof(stKBPacket)+1) { //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket)); //PutStr(str3,len4); - KBusChnStats[nCurPollId].PkgLenErr++; - Uart2Stat.LengthErr++; + pKBus->KBusChnStats[pKBus->nCurPollId].PkgLenErr++; +// Uart2Stat.LengthErr++; return -3; //not long enough } // if (p3->data[DataLen+1] != EndSign) @@ -226,42 +712,42 @@ unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); if (thisBCC != p3->data[DataLen]) {//BCC Error; - Uart2Stat.BCCerr++; - KBusChnStats[nCurPollId].BCCErr++; +// Uart2Stat.BCCerr++; + pKBus->KBusChnStats[pKBus->nCurPollId].BCCErr++; return -4; } return 0; } -int KBusSlaveCheckPacket(int nChn, pKBPacket p1, int nLen1) +int KBusSlaveCheckPacket(stKBusDef * pKBus, pKBPacket p1, int nLen1) { unsigned char * p2 = (unsigned char *)p1; pKBPacket p3=p1; - if ((p2[0] == SYN || p2[0] != StartSign) && nLen1 > 1) + if ((p2[0] == KB_SYN || p2[0] != KBStartSign) && nLen1 > 1) { p3=(pKBPacket)(p2+1); nLen1--; } - if (p3->Sign != StartSign) + if (p3->Sign != KBStartSign) { - Uart2Stat.NotPacketErr++; - KBusChnStats[0].ClientNotPktErr++; +// Uart2Stat.NotPacketErr++; + pKBus->KBusChnStats[0].ClientNotPktErr++; return -1; } int DataLen=p3->DataLen; - if (DataLen>MaxPacketLength) + if (DataLen>KBMaxPacketLength) { - Uart2Stat.LengthErr++; - KBusChnStats[0].ClientPkgLenErr++; +// Uart2Stat.LengthErr++; + pKBus->KBusChnStats[0].ClientPkgLenErr++; return -1; } if (nLen1<DataLen+sizeof(stKBPacket)+1) { //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket)); //PutStr(str3,len4); - KBusChnStats[0].ClientPkgLenErr++; - Uart2Stat.LengthErr++; + pKBus->KBusChnStats[0].ClientPkgLenErr++; +// Uart2Stat.LengthErr++; return -3; //not long enough } // if (p3->data[DataLen+1] != EndSign) @@ -273,53 +759,71 @@ unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1); if (thisBCC != p3->data[DataLen]) {//BCC Error; - Uart2Stat.BCCerr++; - KBusChnStats[0].ClientBccErr++; +// Uart2Stat.BCCerr++; + pKBus->KBusChnStats[0].ClientBccErr++; return -4; } return 0; } -int KBusMasterParsePacket(int nChn, pKBPacket p1, int Len1) +int KBusMasterParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1) { unsigned char * p6 = (unsigned char *)p1; - if ((p6[0] == SYN || p6[0] != StartSign) && Len1 > 1) + if ((p6[0] == KB_SYN || p6[0] != KBStartSign) && Len1 > 1) { p1=(pKBPacket)(p6+1); Len1--; } int DataLen=p1->DataLen; - KBusChnStats[nCurPollId].RecvPackets++; - pKBPacket p2=(pKBPacket)PacketBuf2; - int PacketLen=0; + 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: - BufferIn[ChildId]=p1->data[0]; - PacketLen=KBusMakePacket(p2,0,nCurPollId,cmdPingReply,p1->nStatus,DataLen,p1->data); - SendPacket(nChn, p2, PacketLen); break; case cmdPingReply: - KBusDelayuS=ThisuS-KBusSendTimeuS; - if (KBusDelayuS > KBusMaxDelayuS) KBusMaxDelayuS = KBusDelayuS; + pKBus->DelayTick = ThisTick - pKBus->SendTimeTick; + if (pKBus->DelayTick > pKBus->nMaxDelayTick) pKBus->nMaxDelayTick = pKBus->DelayTick; - BufferIn[ChildId]=p1->data[0]; + KBusMem.WLXB[ChildId]=p1->data[0]; //RunStat=100; - KBusChnStats[nCurPollId].CtnLstPkts=0; - KBusChnStats[nCurPollId].Delay=KBusDelayuS; - if (KBusDelayuS > KBusChnStats[nCurPollId].MaxDelay) - KBusChnStats[nCurPollId].MaxDelay=KBusDelayuS; + 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); - KBusMasterRecvOK=1; + pKBus->bMasterRecvOK=1; break; case cmdRead: break; @@ -328,61 +832,49 @@ case cmdWrite: break; case cmdWriteReply: - KBusMasterRecved=1; + pKBus->bMasterRecved=1; break; case cmdGetVersion: break; case cmdVerInfo: break; - case cmdExChgData: - BufferIn[0]=p1->data[0]; - //PutOutput(outputvalue); - //memcpy(DispBuf,p1->data+2,8); - p1->data[0]=BufferOut[0]; - PacketLen=KBusMakePacket(p2,nStationID,0,cmdExChgDataReply,p1->nStatus,DataLen,p1->data); - SendPacket(nChn, p2, PacketLen); + case cmdUniExChgData: + break; case cmdExChgDataReply: - KBusDelayuS=ThisuS-KBusSendTimeuS; - if (KBusDelayuS > KBusMaxDelayuS) KBusMaxDelayuS = KBusDelayuS; - -#if (BOARD_TYPE == 14) - BufferIn[ChildId]=p1->data[0]; - BufferIn[ChildId + 1] = p1->data[1]; - BufferIn[ChildId + 2 ] = p1->data[2]; - - KMem.WXB[ChildId-1]=BufferIn[ChildId]; - KMem.WXB[ChildId]=BufferIn[ChildId+1]; - KMem.WXB[ChildId+1]=BufferIn[ChildId+2]; - - if (KMRunStat.WorkMode==0) { - // KMem.WY[0]= KMem.WX[1]+(KMem.WX[2]<<8) ; - // PutOutput (KMem.WY[0]); + 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];; } -#else - BufferIn[ChildId]=p1->data[0]; - KMem.WLX[ChildId]=BufferIn[ChildId]; - if (KMRunStat.WorkMode==0) { - KMem.WY[0]= KMem.WLX[1]+(KMem.WLX[2]<<8) ; - PutOutput (KMem.WY[0]); - } -#endif - +// 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; - KBusChnStats[nCurPollId].CtnLstPkts=0; - KBusChnStats[nCurPollId].Delay=KBusDelayuS; - if (KBusDelayuS > KBusChnStats[nCurPollId].MaxDelay) - KBusChnStats[nCurPollId].MaxDelay=KBusDelayuS; + 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); - - nIndex=p1->data[3]; - KBusChnStats[nCurPollId].ClientDatas[nIndex]=p1->data[4]|(p1->data[5]<<8)|(p1->data[6]<<16)|(p1->data[7]<<24); - - KBusMasterRecvOK=1; + 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; } @@ -394,50 +886,150 @@ return 0; } -unsigned char nClientDataIndex=0; -int KBusSlaveParsePacket(int nChn, pKBPacket p1, int Len1) +int KBusSlaveParsePacket(stKBusDef * pKBus, pKBPacket p1, int Len1) { unsigned char * p3 = (unsigned char *)p1; - if (p3[0] == SYN && Len1 > 1) + if (p3[0] == KB_SYN && Len1 > 1) { p1=(pKBPacket)(p3+1); Len1--; } - Uart2Stat.OKPacket++; - int DataLen=p1->DataLen; +// 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)PacketBuf2; - + pKBPacket p2=(pKBPacket)pKBus->PacketBuf2; + void * pData = 0; int PacketLen=0; unsigned char nIndex;// = p1->nStatus & 0x07; - if (nDstHost!=nStationID && nDstHost != 0xff) + if (nDstHost!=pKBus->nStationId && nDstHost != 0xff) { - KBusChnStats[0].ClientMisIdPkts++; + pKBus->KBusChnStats[0].ClientMisIdPkts++; return -1; } - if (nDstHost==nStationID || nDstHost==0xff) - { - KBusRecvTimeuS=ThisuS; - KBusSlaveRecved=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: - BufferIn[0]=p1->data[0]; + KBusMem.WLYB[0]=p1->data[0]; //PutOutput(outputvalue); //memcpy(DispBuf,p1->data+2,8); - p1->data[0]=BufferOut[0]; - KBusRecvTimeuS=ThisuS; - PacketLen=KBusMakePacket(p2,nStationID,0,cmdPingReply,p1->nStatus,DataLen,p1->data); - KBusChnStats[0].ClientSendPkts++; - SendPacket(nChn, p2, PacketLen); + 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; @@ -448,8 +1040,8 @@ case cmdWrite: //memcpy(DispBuf,p1->data,DataLen); PacketLen=KBusMakePacket(p2,1,0,cmdWriteReply,p1->nStatus,0,0); - KBusChnStats[0].ClientSendPkts++; - SendPacket(nChn, p2, PacketLen); + pKBus->KBusChnStats[0].ClientSendPkts++; + pKBus->KBusSendPacket((uchar *)p2, PacketLen); break; case cmdWriteReply: break; @@ -457,47 +1049,42 @@ break; case cmdVerInfo: break; - case cmdExChgData: - BufferIn[0]=p1->data[0]; - BufferIn[1]=p1->data[1]; -// KMem.WLYB[1] = BufferIn[1]; - - KMem.WYB[1] = BufferIn[1]; - nSlaveTick=p1->data[4]+(p1->data[5]<<8);//+(p1->data[6]<<16)+(p1->data[7]<<24); -#if (BOARD_TYPE == 14) - // PutOutput(BufferIn[0]); - //PutOutput(outputvalue); - //memcpy(DispBuf,p1->data+2,8); - nIndex=nClientDataIndex; - // KBusChnStats[0].ClientDatas[7]++; -// BufferOut[0]=GetInput(); - // BufferOut[0]=GetInput(); -#else - PutOutput(BufferIn[0]); - //PutOutput(outputvalue); - //memcpy(DispBuf,p1->data+2,8); - nIndex=nClientDataIndex; - // KBusChnStats[0].ClientDatas[7]++; -// BufferOut[0]=GetInput(); - BufferOut[0]=GetInput(); - BufferOut[1]=KMem.WXB[1]; -#endif - p1->data[0]=BufferOut[0]; - p1->data[1]=BufferOut[1]; - - p1->data[3]=nIndex; - p1->data[4]=KBusChnStats[0].ClientDatas[nIndex]; - p1->data[5]=KBusChnStats[0].ClientDatas[nIndex]>>8; - p1->data[6]=KBusChnStats[0].ClientDatas[nIndex]>>16; - p1->data[7]=KBusChnStats[0].ClientDatas[nIndex]>>24; - nClientDataIndex++; - if (nClientDataIndex >= 10) { nClientDataIndex=0;} + 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) { KMem.ErrStat=8000;} - PacketLen=KBusMakePacket(p2,nStationID,0,cmdExChgDataReply,p1->nStatus,DataLen,p1->data); - KBusChnStats[0].ClientSendPkts++; - SendPacket(nChn, p2, PacketLen); + 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; @@ -510,8 +1097,54 @@ case cmdSequenRead: break; case cmdSyncTime: - nSlaveTick=p1->data[0]+(p1->data[1]<<8)+(p1->data[2]<<16)+(p1->data[3]<<24); 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; } @@ -519,39 +1152,54 @@ return 0; } -int KBusParsePacket(int nChn, pKBPacket p1, int Len1) +int KBusSlaveRunRemoteReq(stKBusDef * pKBus, int nReqSvrId, unsigned char * pData , int Len1) { - ThisuS=GetuS(); + 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] == SYN && Len1 > 1) + if (p2[0] == KB_SYN && Len1 > 1) { p1=(pKBPacket)(p2+1); Len1--; } - if (bKBusMaster) + if (pKBus->bMaster) { - KBusMasterRecved=1; - Result=KBusCheckPacket(nChn, p1, Len1); + pKBus->bMasterRecved=1; + Result=KBusCheckPacket(pKBus, p1, Len1); if (Result != S_OK) { + return Result; } - KBusMasterRecvOK=1; - Result=KBusMasterParsePacket(nChn, p1, Len1); + pKBus->bMasterRecvOK=1; + Result=KBusMasterParsePacket(pKBus, p1, Len1); return Result; } - if (bKBusSlave) + if (pKBus->bSlave) { - KBusChnStats[0].ClientRecvPkts++; - Result=KBusSlaveCheckPacket(nChn, p1, Len1); + pKBus->KBusChnStats[0].ClientRecvPkts++; + Result=KBusSlaveCheckPacket(pKBus, p1, Len1); if (Result != S_OK) { return Result; } - KBusChnStats[0].ClientTimeOutErr=KMem.RunStat; - Result=KBusSlaveParsePacket(nChn, p1, Len1); + pKBus->KBusChnStats[0].ClientTimeOutErr=pKBus->RunStat; + Result=KBusSlaveParsePacket(pKBus, p1, Len1); return Result; } //int len1=p1->PacketLen; @@ -603,146 +1251,127 @@ return S_OK; } */ -int KBusRepeaterFunc(int nChn) + + + +unsigned char KBusBCC(void * pData, int nSize) { - KMem.WY[0]=KMem.WX[0]; - if ((KMem.nRunCount &0x7f) == 88) - { - nCount2++; - ToggleRunLed(); -// int len1=sprintf(str1,"%d %d Cfg %02X Input %02X \r\n",nCount,nCount2,EffJumperSW,MyKeyStat1); -// PutStr(str1,len1); - } - return 0; -} - -int KBusMasterFunc(int nChn) -{ - uint32_t tick1=HAL_GetTick(); - uint32_t thisuS=GetuS(); - - int len1=0; - - if ((KBusMasterRecved && KBusMasterRecvOK && thisuS-KBusSendTimeuS>50) || thisuS-KBusSendTimeuS>1000u) - { - if (!KBusMasterRecvOK) - { - TimeOutCount++; - Uart2Stat.TimeOutErr++; - KBusChnStats[nCurPollId].LostPackets++; - KBusChnStats[nCurPollId].CtnLstPkts++; - if (!KBusMasterRecved) {KBusChnStats[nCurPollId].TimeOutErr++;} - if (KBusChnStats[nCurPollId].CtnLstPkts>KBusChnStats[nCurPollId].MaxCtnLstPkts) - {KBusChnStats[nCurPollId].MaxCtnLstPkts=KBusChnStats[nCurPollId].CtnLstPkts;} - if (KBusChnStats[nCurPollId].CtnLstPkts>3) - { - KBusChnStats[nCurPollId].Stat=0; - KMem.ErrStat=200; -#if (BOARD_TYPE == 14) - BufferIn[nCurPollId]=0; - KMem.WXB[nCurPollId-1]=BufferIn[nCurPollId]; -#else - {BufferIn[nCurPollId]=0; - KMem.WLX[nCurPollId]=BufferIn[nCurPollId]; - } -#endif - } - // LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7); - }else - { - KBusChnStats[nCurPollId].Stat=1; - - KMem.RunStat=100; - } - nCurPollId ++; - if (nCurPollId > nChilds) - { - CircleTime=thisuS-LastCircleStartTime; - LastCircleStartTime=thisuS; - nSeq++; - nCurPollId=1; - } -#if (BOARD_TYPE == 14) - if (KMRunStat.WorkMode==0) - { - // KMem.WX[0]= GetInput(); - // KMem.WY[1]=KMem.WX[0]&0xff; - // KMem.WY[2]=(KMem.WX[0]>>8)&0xff; - } - // BufferOut[1]=KMem.WY[1]; - // BufferOut[2]=KMem.WY[2]; -#else - if (KMRunStat.WorkMode==0) - { - KMem.WX[0]= GetInput(); - KMem.WLY[1]=KMem.WX[0]&0xff; - KMem.WLY[2]=(KMem.WX[0]>>8)&0xff; - } - BufferOut[1]=KMem.WLY[1]; - BufferOut[2]=KMem.WLY[2]; -#endif - - Datas[0]=BufferOut[nCurPollId]; - Datas[1]=BufferOut[nCurPollId+1];; - Datas[2]=KBusChnStats[nCurPollId].Stat; - Datas[3]=0; - Datas[4]=tick1&0xff; - Datas[5]=(tick1>>8)&0xff; - Datas[6]=(tick1>>16)&0xff; - Datas[7]=(tick1>>24)&0xff; - - KBusSendTimeuS=thisuS; - unStatus nStatus; - nStatus.nSeq = nSeq; - nStatus.nErr1 = (KBusChnStats[nCurPollId].Stat==0); - - - len1=KBusMakePacket((pKBPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nStatus.nStatus,8,Datas); -// LL_USART_SetBaudRate(USART2,48000000,LL_USART_OVERSAMPLING_8,DefaultUart2Baud); - - SendPacket(nChn, (pKBPacket)PacketBuf1, len1); - KBusChnStats[nCurPollId].SendPackets++; - KBusChnStats[nCurPollId].SendTimeInterval=KBusSendTimeuS-KBusChnStats[nCurPollId].LastSentTimeuS; - KBusChnStats[nCurPollId].LastSentTimeuS=KBusSendTimeuS; -// PacketLength = len1; - SendTime=tick1; - - KBusMasterRecved=0; - KBusMasterRecvOK=0; - // LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5); - //ToggleErrLed(); -// ToggleOut8(); - - } - -// Clk3=SysTick->VAL; - // LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_4); -// HAL_Delay(1); - return 0; -} - -int KBusSlaveFunc(int nChn) -{ - int ThisuS=GetuS(); - int thisRecvTime=KBusRecvTimeuS; - if (nStationID >0) { - if (KBusSlaveRecved) - { - KMem.RunStat=8000; - KBusSlaveRecved=0; - }else if ((ThisuS - thisRecvTime) >12000u) - { - KMem.ErrStat=8000; - KMem.SDD[17]=1; - KMem.SDD[18]=ThisuS; - KMem.SDD[19]=KBusRecvTimeuS; - }else if ( ThisuS > (thisRecvTime + 12000u)) - { - KMem.ErrStat=8000; - KMem.SDD[17]=2; - KMem.SDD[18]=ThisuS; - KMem.SDD[19]=KBusRecvTimeuS; - } + unsigned char k; + k=0; + for (int i=0;i<nSize;i++) + { + k^=((unsigned char *)pData)[i]; } - return 0; + return k; } + +int KBusMakePacket(pKBPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nStatus, unsigned char DataLen,void * pData ) +{ + p1->Sign=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; +} + -- Gitblit v1.9.1