From 842bb64195f958b050867c50db66fc0aa413dafb Mon Sep 17 00:00:00 2001
From: QuakeGod <quakegod@sina.com>
Date: 星期六, 27 七月 2024 10:42:56 +0800
Subject: [PATCH] KBus upgrade

---
 ComLib/Src/KBus.c |  748 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 589 insertions(+), 159 deletions(-)

diff --git a/ComLib/Src/KBus.c b/ComLib/Src/KBus.c
index 82dee18..972e77e 100644
--- a/ComLib/Src/KBus.c
+++ b/ComLib/Src/KBus.c
@@ -45,6 +45,30 @@
 		pKBus->KBusSendPacket = KBusSendPacket;
 		pKBus->nCurPollId = 1;
 	
+	for (int i=0;i<MAX_CLIENT;i++)
+	{
+		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;
 };
 
@@ -61,11 +85,16 @@
 		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;
 }
 
@@ -92,6 +121,92 @@
 		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;
@@ -104,14 +219,17 @@
 	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;
 }
 
@@ -124,36 +242,29 @@
 	return 0;
 }
 
-int KBusUpdateChildInfo(stKBusDef * pKBus, int nCild, stDeviceInfo * pInfoBlock)
+int KBusUpdateChildInfo(stKBusDef * pKBus, int nChild, stDeviceInfo * pInfoBlock)
 {
-	stDeviceInfo * pDeviceInfo = &pKBus->DeviceInfos[nCild];
-	
-	pDeviceInfo->DeviceType = pInfoBlock->DeviceType;
-	pDeviceInfo->DeviceVer = pInfoBlock->DeviceVer;
-	pDeviceInfo->InBitCount = pInfoBlock->InBitCount;
-	pDeviceInfo->OutBitCount = pInfoBlock->OutBitCount ;
-	pDeviceInfo->AIWCount = pInfoBlock->AIWCount;
-	pDeviceInfo->AQWCount = pInfoBlock->AQWCount;
-	pDeviceInfo->DWStartAddr = pInfoBlock->DWStartAddr;
-	pDeviceInfo->OutDWCount = pInfoBlock->OutDWCount;
-	
+	pKBus->DeviceInfos[nChild] = *pInfoBlock;
 	return 0;
 }
 
-int KBusMakeDefaultClildInfo(stKBusDef * pKBus, int nCild)
+int KBusMakeDefaultClildInfo(stKBusDef * pKBus, int nChild)
 {
-	unsigned DefaultInBitCount = 8;
-	unsigned DefaultOutBitCount = 8;
-	stDeviceInfo * pDeviceInfo = &pKBus->DeviceInfos[nCild];
+//	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
+		};
 
-	pDeviceInfo->DeviceType = 0;
-	pDeviceInfo->DeviceVer = 0;
-	pDeviceInfo->InBitCount = DefaultInBitCount;
-	pDeviceInfo->OutBitCount =  DefaultOutBitCount;
-	pDeviceInfo->AIWCount = 0;
-	pDeviceInfo->AQWCount = 0;
-	pDeviceInfo->DWStartAddr = 0;
-	pDeviceInfo->OutDWCount = 0;		
 	return 0;
 }
 
@@ -184,74 +295,234 @@
 int KBusSearchChildProc(stKBusDef * pKBus)
 {
 	int iRet = 0;
-	uint32_t tick1=HAL_GetTick();
-	uint32_t thisuS=GetuS();
-	
-	pKBus->MyStat = KBusStatReady;
-	return iRet;
-	if (!pKBus->bMasterSent) {
-		// Sent Query Packet for CurPollId;
-		//pKBus->nCurPollId;
-		pKBus->bMasterSent=1;
-		pKBus->SendTimeuS = thisuS;
-	}
+	uint32_t tick1=GetTick();
+//	uint32_t thisuS=GetuS();
+	int nThisPollId = pKBus->nCurPollId;
 	if (pKBus->bMasterSent) {
 		if (pKBus->bMasterRecved) {
-
-		}
-		
-		if (!pKBus->bMasterRecved && thisuS- pKBus->SendTimeuS > 1000) 
-		{
-			// timeOut,
-				pKBus->bMasterSent=0;  // no wait any more;
-			if (pKBus->RetryCount <3) {
-				// resent query packet for curPollId;	
-				pKBus->bMasterSent=1;
-				pKBus->SendTimeuS = thisuS;				
-			}
-			if (pKBus->RetryCount >3 ) {
-				// Set Cur Child As Default
-				KBusMakeDefaultClildInfo(pKBus,pKBus->nCurPollId);
-				KBusAddChildAddrByInfo(pKBus,pKBus->nCurPollId,&pKBus->DeviceInfos[pKBus->nCurPollId]);
-				
-				// Next Child;
-				pKBus->nCurPollId++;
-				if (pKBus->nCurPollId > pKBus->nChildCount) 
+				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:
-			KBusDataPoll(pKBus);
+			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;
@@ -262,18 +533,56 @@
 int KBusDataPoll(stKBusDef * pKBus)
 {
 	int iRet = 0;
-	uint32_t tick1=HAL_GetTick();
-	uint32_t thisuS=GetuS();	
+	uint32_t tick1=GetTick();
+//	uint32_t thisuS=GetuS();	
 		int len1=0;
 	int nThisPollId = pKBus->nCurPollId;
 	
 	stChnStat * pChnStat = &pKBus->KBusChnStats[nThisPollId];
-		if ((pKBus->bMasterRecved && pKBus->bMasterRecvOK && thisuS-pKBus->SendTimeuS>50) || thisuS-pKBus->SendTimeuS>1000u)
+	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++; 
+//				Uart2Stat.TimeOutErr++; 
 				pChnStat->LostPackets++;
 				pChnStat->CtnLstPkts++;
 				if (!pKBus->bMasterRecved) {pChnStat->TimeOutErr++;}
@@ -281,7 +590,7 @@
 				{pChnStat->MaxCtnLstPkts = pChnStat->CtnLstPkts;}
 				if (pChnStat->CtnLstPkts>3)
 				{
-					pChnStat->Stat = 0;
+					pChnStat->MStat = 0;
 					pKBus->ErrStat=200;
 					KBusMem.WLXB[nThisPollId]=0;
 					if (pKBus->KBusEvCBFunc) pKBus->KBusEvCBFunc(pKBus, KBusEvStateChange,0,0);
@@ -291,13 +600,15 @@
 			//	LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_7);
 			}else
 			{
-				pChnStat->Stat=1;
+			//	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 ++;
+				nThisPollId += 2;
+				//nThisPollId ++;
 			}else {
 				nThisPollId ++;
 			}
@@ -306,71 +617,40 @@
 				// 数据轮询完, 周期间隙,插入其他处理数据.  每次处理的时间间隙, 一问 一答  时间.
 				// 分成几种类型, 轮流进行
 				//	时间同步,			查询新子机,		处理带外数据,			,处理额外的事情,			或者跳过.
-				pKBus->CircleTime=thisuS-pKBus->LastCircleStartTime;
-				pKBus->LastCircleStartTime=thisuS;
+				pKBus->CircleTime=tick1-pKBus->LastCircleStartTime;
+				pKBus->LastCircleStartTime=tick1;
 				pKBus->nSeq++;
-				nThisPollId=1;
+				GoStep(pKBus,KBusRunStepUniDataTranster);
+				//nThisPollId=1;
+			}else {
+				pKBus->nCurPollId = nThisPollId;
 			}
-			pKBus->nCurPollId = nThisPollId;
-
-			pKBus->Datas[0]=KBusMem.WLYB[nThisPollId -1 ];
-			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->SendTimeuS=thisuS;
-			unStatus nStatus;
-			nStatus.nSeq = pKBus->nSeq;
-			nStatus.nErr1 = (pKBus->KBusChnStats[nThisPollId].Stat==0);
-			
-			
-			len1=KBusMakePacket((pKBPacket)pKBus->PacketBuf1,0,nThisPollId,cmdUniExChgData,nStatus.nStatus,8,pKBus->Datas);
-//			LL_USART_SetBaudRate(USART2,48000000,LL_USART_OVERSAMPLING_8,DefaultUart2Baud);
-			
-			pKBus->KBusSendPacket(pKBus->PacketBuf1, len1);
-			pKBus->KBusChnStats[pKBus->nCurPollId].SendPackets++;
-			pKBus->KBusChnStats[pKBus->nCurPollId].SendTimeInterval=pKBus->SendTimeuS-pKBus->KBusChnStats[pKBus->nCurPollId].LastSentTimeuS;
-			pKBus->KBusChnStats[pKBus->nCurPollId].LastSentTimeuS=pKBus->SendTimeuS;
-//			PacketLength = len1;
-			pKBus->SendTime=tick1;
-
-			pKBus->bMasterRecved=0;
-			pKBus->bMasterRecvOK=0;
-		//	LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);		
-			//ToggleErrLed();
-//				ToggleOut8();
-
+			pKBus->bMasterSent = 0;			
 		}	
-	
+	}
 	return iRet;
 }
 
 int KBusSlaveFunc(stKBusDef * pKBus)
 {
-		int ThisuS=GetuS();
-		int thisRecvTime=pKBus->RecvTimeuS;
+//		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 ((ThisuS - thisRecvTime) >12000u)
+			}else if ((ThisTick - thisRecvTime) > KBUS_SLAVE_TIME_OUT) // 30u
 			{
-				pKBus->ErrStat=8000;
-			}else if ( ThisuS > (thisRecvTime + 12000u))
+				pKBus->ErrStat=7000;
+			}else if ( ThisTick > (thisRecvTime + KBUS_SLAVE_TIME_OUT)) // 30u
 			{
-				pKBus->ErrStat=8000;
+				pKBus->ErrStat=7100;
 			}
 	}
 	return 0;
 }
-
-
 
 int KBusCheckPacket(stKBusDef * pKBus, pKBPacket p1, int nLen1)
 {
@@ -384,7 +664,11 @@
 	
 	if (p3->Sign != KBStartSign)
 	{
-		Uart2Stat.NotPacketErr++;
+//		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) {
@@ -407,7 +691,7 @@
 	int DataLen=p3->DataLen;
 	if (DataLen>KBMaxPacketLength) 
 	{
-		Uart2Stat.LengthErr++;
+//		Uart2Stat.LengthErr++;
 		pKBus->KBusChnStats[pKBus->nCurPollId].PkgLenErr++;
 		return -1;
 	}
@@ -416,7 +700,7 @@
 		//len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
 		//PutStr(str3,len4);
 		pKBus->KBusChnStats[pKBus->nCurPollId].PkgLenErr++;
-		Uart2Stat.LengthErr++;
+//		Uart2Stat.LengthErr++;
 		return -3;	//not long enough					
 	}
 //	if (p3->data[DataLen+1] != EndSign)
@@ -428,7 +712,7 @@
 	unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
 	if (thisBCC != p3->data[DataLen]) 
 	{//BCC Error;
-		Uart2Stat.BCCerr++;
+//		Uart2Stat.BCCerr++;
 		pKBus->KBusChnStats[pKBus->nCurPollId].BCCErr++;
 		return -4;
 	} 		
@@ -447,14 +731,14 @@
 	
 	if (p3->Sign != KBStartSign)
 	{
-		Uart2Stat.NotPacketErr++;
+//		Uart2Stat.NotPacketErr++;
 		pKBus->KBusChnStats[0].ClientNotPktErr++;
 		return -1;
 	}
 	int DataLen=p3->DataLen;
 	if (DataLen>KBMaxPacketLength) 
 	{
-		Uart2Stat.LengthErr++;
+//		Uart2Stat.LengthErr++;
 		pKBus->KBusChnStats[0].ClientPkgLenErr++;
 		return -1;
 	}
@@ -463,7 +747,7 @@
 		//len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
 		//PutStr(str3,len4);
 		pKBus->KBusChnStats[0].ClientPkgLenErr++;
-		Uart2Stat.LengthErr++;
+//		Uart2Stat.LengthErr++;
 		return -3;	//not long enough					
 	}
 //	if (p3->data[DataLen+1] != EndSign)
@@ -475,7 +759,7 @@
 	unsigned char thisBCC=KBusBCC(p1,sizeof(stKBPacket)+DataLen-1);
 	if (thisBCC != p3->data[DataLen]) 
 	{//BCC Error;
-		Uart2Stat.BCCerr++;
+//		Uart2Stat.BCCerr++;
 		pKBus->KBusChnStats[0].ClientBccErr++;
 		return -4;
 	} 		
@@ -499,7 +783,8 @@
 		//LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_6);
 		int nCurPollId = pKBus->nCurPollId;
 		int ChildId=p1->SrcAddr;
-		int ThisuS = GetuS();
+//		int ThisuS = GetuS();
+		int ThisTick = GetTick();
 		unsigned char nIndex;
 		switch (p1->nCMD)
 		{
@@ -508,8 +793,8 @@
 				break;
 			case cmdQueryRply:
 				DataLen=sizeof(stDeviceInfo);
-				KBusUpdateChildInfo(pKBus,pKBus->nCurPollId,(stDeviceInfo *)&p1->data[0]);
-				pKBus->KBusChnStats[0].ClientSendPkts++;
+				KBusUpdateChildInfo(pKBus,nCurPollId,(stDeviceInfo *)&p1->data[0]);
+				pKBus->KBusChnStats[nCurPollId].ClientSendPkts++;
 				break;
 			case cmdSetCfgRply:
 				break;
@@ -527,16 +812,16 @@
 			case cmdPing:
 				break;
 			case cmdPingReply:
-				pKBus->DelayuS=ThisuS-pKBus->SendTimeuS;
-				if (pKBus->DelayuS > pKBus->nMaxDelayuS) pKBus->nMaxDelayuS = pKBus->DelayuS;
+				pKBus->DelayTick = ThisTick - pKBus->SendTimeTick;
+				if (pKBus->DelayTick > pKBus->nMaxDelayTick) pKBus->nMaxDelayTick = pKBus->DelayTick;
 				
 				KBusMem.WLXB[ChildId]=p1->data[0];
 				
 				//RunStat=100;
-				pKBus->KBusChnStats[pKBus->nCurPollId].CtnLstPkts=0;
-				pKBus->KBusChnStats[pKBus->nCurPollId].Delay=pKBus->DelayuS;
-			if (pKBus->DelayuS > pKBus->KBusChnStats[pKBus->nCurPollId].MaxDelay) 
-				pKBus->KBusChnStats[pKBus->nCurPollId].MaxDelay=pKBus->DelayuS;
+				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;
@@ -557,25 +842,26 @@
 		
 				break;
 			case cmdExChgDataReply:
-				pKBus->DelayuS=ThisuS-pKBus->SendTimeuS;
-				if (pKBus->DelayuS > pKBus->nMaxDelayuS) pKBus->nMaxDelayuS = pKBus->DelayuS;
-
-				KBusMem.WLXB[ChildId-1]=p1->data[0];;
-				KBusMem.WLXB[ChildId  ]=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];
-			
+				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->DelayuS;
-			if (pKBus->DelayuS > pKBus->KBusChnStats[nCurPollId].MaxDelay) 
-				pKBus->KBusChnStats[nCurPollId].MaxDelay=pKBus->DelayuS;
+				pKBus->KBusChnStats[nCurPollId].Delay=pKBus->DelayTick;
+			if (pKBus->DelayTick > pKBus->KBusChnStats[nCurPollId].MaxDelay) 
+				pKBus->KBusChnStats[nCurPollId].MaxDelay=pKBus->DelayTick;
 				//PutOutput(outputvalue);
 				
 			  if (DataLen>=14) {
@@ -585,7 +871,10 @@
 				pKBus->bMasterRecvOK=1;
 				
 				break;
-					
+			case cmdRemoteReqReply:
+				pKBus->bReq = 2;
+			
+				break;
 			default:
 				break;		
 		}
@@ -606,8 +895,9 @@
 		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;
 		
@@ -615,7 +905,7 @@
 //	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)
@@ -623,10 +913,90 @@
 		pKBus->KBusChnStats[0].ClientMisIdPkts++;
 		return -1;
 	}
-	int ThisuS = GetuS();
-	if (nDstHost==pKBus->nStationId || nDstHost==0xff)
+//	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->RecvTimeuS=ThisuS;
+		pKBus->RecvTimeTick=ThisTick;
 		pKBus->bSlaveRecved=1;
 		switch (p1->nCMD)
 		{
@@ -656,7 +1026,7 @@
 				//PutOutput(outputvalue);
 				//memcpy(DispBuf,p1->data+2,8);
 				p1->data[0]=KBusMem.WLXB[0];
-				pKBus->RecvTimeuS=ThisuS;
+				//pKBus->RecvTimeuS=ThisuS;
 				PacketLen=KBusMakePacket(p2,pKBus->nStationId,0,cmdPingReply,p1->nStatus,DataLen,p1->data);
 				pKBus->KBusChnStats[0].ClientSendPkts++;
 				pKBus->KBusSendPacket((uchar *)p2, PacketLen);
@@ -685,7 +1055,7 @@
 				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);
+			//	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);
 
@@ -711,7 +1081,7 @@
 				if (pKBus->nClientDataIndex >= 10) { pKBus->nClientDataIndex=0;}
 				unStatus nStatus;
 				nStatus.nStatus = p1->nStatus;
-				if (nStatus.nErr1) {	pKBus->ErrStat=8000;}
+				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);
@@ -727,13 +1097,72 @@
 			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;		
 		}
 	}	
+	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;
 }
 
@@ -754,6 +1183,7 @@
 			Result=KBusCheckPacket(pKBus, p1, Len1);
 			if (Result != S_OK)
 			{
+				
 				return Result;
 			}
 			pKBus->bMasterRecvOK=1;

--
Gitblit v1.9.1