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

---
 KBus/Src/main.c |  221 +++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 157 insertions(+), 64 deletions(-)

diff --git a/KBus/Src/main.c b/KBus/Src/main.c
index 025920a..52dce20 100644
--- a/KBus/Src/main.c
+++ b/KBus/Src/main.c
@@ -45,23 +45,30 @@
 #include "debug.h"
 #include "Functions.h"
 #include "KMachine.h"
+#if (ENABLE_PLC) 
 #include "PLCfunctions.h"
+#endif
 //#include "KBus.h"
 #include "KLink.h"
 #include "string.h"
 #include "BSP.h"
 #include "ModbusRTU.h"
-#if (BOARD_TYPE == 13)
+#if (ENABLE_NET)
 #include "w5500_port.h"
 #include "../src/Ethernet/socket.h"
 #include "../src/Ethernet/loopback.h"
-#elif (BOARD_TYPE == 14)
-#include "FP0.h"
-#elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+#endif
+
+#if (ENABLE_FPX)
+#include "FPx.h"
+#endif
+
+#if (ENABLE_RF)
 #include "KWireless.h"
+#endif
 //#include "user.h"
 //#include "../src/radio/inc/sx126x-board.h"
-#endif
+
 
 /* USER CODE END Includes */
 
@@ -83,9 +90,13 @@
 unsigned char FastFlicker=0;
 
 unsigned int Uart1IdelTimer = 0;
+#if (ENABLE_PLC)
 stBinProg1 * pProgs = (stBinProg1 *)STORE_PRG_BASE;
-
+#endif
 uint32_t us1,us2,us3,us4,us5,us6;
+
+
+stKBusDef KBus1;							// 
 
 /* USER CODE END PV */
 
@@ -118,9 +129,9 @@
 static int Count=0;
 	CurTickuS += 100;	
 	nCurTick++;
-	nSlaveTick++;
+	KBus1.nSlaveTick++;
 	Count++;
-	if (Count>=10000) 
+	if (Count>=10000) 	//  0.1mS, 10000娆�,  绉掕剦鍐�
 	{
 		Count=0; 
 		KMem.CurTimeSec++;
@@ -131,6 +142,70 @@
 
 	return;
 }
+
+void PendSvCallBack()
+{
+#if (ENABLE_FPX)
+///*	
+		if (bSPI1RecvDone)
+		{
+			bSPI1RecvDone=0;
+			FPxParsePkt(SPI1RecvBuf,nSPI1RecvLenInBuf);
+		}
+//*/	
+#endif		
+	if (Uart2Stat.bPacketRecved)
+	{
+		KBusParsePacket(&KBus1, (pKBPacket)Uart2RecvBuf1, Uart2RecvBuf1DataLen);		
+		Uart2RecvBuf1DataLen=0;
+		Uart2Stat.bPacketRecved=0;
+		Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1));		
+		KMem.WDT[2]++;
+	}
+}
+
+/*
+KBus閫氳鍥炶皟鍑芥暟锛屽綋閫氳鐘舵�佹敼鍙樻垨鏁版嵁鏇存柊鏃惰璋冪敤銆�
+鎴栬�呯郴缁熻姹傛椂銆�
+*/
+void * KBusEvCallBackFunc(void*  pParam,  int nEvent, void *pBuf, int nLen1)
+{
+	switch (nEvent){
+		
+		case KBusEvNone:
+			break;
+		case KBusEvCreate:
+			break;
+		case KBusEvConnected:
+			break;
+		case KBusEvDisConnected:
+			break;
+		case KBusEvClosed:
+			break;
+		case KBusEvStateChange:
+			break;
+		case KBusEvTimeSync:
+			break;
+		case KBusEvDataUpdate:
+			KMem.WDT[10]++;
+			if (KBus1.bMaster) {
+				KMem.WY[0]=KBusMem.WLX[0];			//KBus Master
+				KBusMem.WLY[0]=KMem.WX[0];
+			} else if (KBus1.bSlave) {
+				KMem.WY[0]=KBusMem.WLY[0];			//KBus Slave
+				KBusMem.WLX[0]=KMem.WX[0];
+			}
+			break;
+		case KBusEvCmdResponse:
+			break;
+		
+		default:
+			break;
+	}
+	return 0;
+}
+
+extern 	stDeviceInfo MyDeviceInfo;
 
 /* USER CODE END 0 */
 
@@ -155,22 +230,6 @@
 
   /* USER CODE BEGIN Init */
 
-	for (int i=0;i<9;i++)
-	{
-//		memset(KBusChnStats[i],0,0);		
-		KBusChnStats[i].SendPackets=0;
-		KBusChnStats[i].RecvPackets=0;
-		KBusChnStats[i].LostPackets=0;
-		KBusChnStats[i].CtnLstPkts=0;
-		KBusChnStats[i].MaxCtnLstPkts=0;
-		KBusChnStats[i].NotPkgErr=0;
-		KBusChnStats[i].PkgLenErr=0;
-		KBusChnStats[i].TimeOutErr=0;
-		KBusChnStats[i].BCCErr=0;
-		KBusChnStats[i].Delay=0;
-		KBusChnStats[i].MaxDelay=0;
-	}
-	
 		KMem.LastScanTime=0;
 		KMem.ScanTimeuS=0;
 		KMem.MinScanTimeuS=99999;
@@ -206,20 +265,31 @@
 	ReadSysCfgFromFlash(&storedKMSysCfg);
 	
 	KMRunStat.bLEDFlick = 1;
-	
+
+	int bKBusMaster,bKBusSlave,bKBusRepeater;;
+	int nChilds;
+	int nKBusStationID;
 	KMem.CurJumperSW=ReadJumperSW();
 	KMem.EffJumperSW=KMem.CurJumperSW;
 	
+//	Uart2Baud = AlterUart2Baud;	nChilds=KMem.EffJumperSW&0x0f;
 //	Uart2Baud = AlterUart2Baud;
 	
+	nKBusStationID = KMem.EffJumperSW&0x0f;
+	nChilds=nKBusStationID;
+	
+
 #if (BOARD_TYPE == 14)
 	KMem.EffJumperSW|=0x10;
 	nStationID=KMem.EffJumperSW&0x0f;
   if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
 	else{bKBusMaster=0;bKBusSlave=1;}
-	nChilds=nStationID;
-	FP0_Init();
+	FPxSetCallBackFunc(&FPxCallBackFunc);
+	FPx_Init(nChilds);
 
+	int IOByteCount = nChilds;
+	FPx_SetIOCount(IOByteCount,IOByteCount);
+	
 #elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
 	nStationID=1 ;//KMem.EffJumperSW&0x0f;
 //	if (KMem.EffJumperSW == 0x1f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
@@ -227,13 +297,30 @@
 //	else
 		{bKBusMaster=0;bKBusSlave=1;}	
 #else
-	nStationID=KMem.EffJumperSW&0x0f;
+	nKBusStationID=nChilds;
 	if (KMem.EffJumperSW == 0x1f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
-  else if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
-	else{bKBusMaster=0;bKBusSlave=1;}
+  else if ((KMem.EffJumperSW&0x10)!=0) {
+		bKBusMaster=1;bKBusSlave=0;
+	}
+	else{
+		bKBusMaster=0;bKBusSlave=1;
+	}
+	
 #endif
-	nChilds=nStationID;
-	nCurPollId=1;
+	UNUSED(bKBusRepeater);
+
+	if (bKBusMaster) {
+		KBusInitMaster(&KBus1, (KBusSendPktFuncDef)PutStr2, nChilds);
+	
+	} else if (bKBusSlave) {
+		KBusInitSlave(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusStationID,&MyDeviceInfo);
+	}
+	
+	KBusSetEvCallBackFunc(&KBus1, &KBusEvCallBackFunc);	
+	
+	KMem.WDT[0]= nKBusStationID;
+	KMem.WDT[1] = KBus1.nStationId;
+	
 	//if (KMem.EffJumperSW == 0x00)
 		Uart1Baud = DefaultUart1Baud;
   MX_USART1_UART_Init();
@@ -314,19 +401,20 @@
 	KMRunStat.WorkMode2=0;
 		
 	KMRunStat.WorkMode = storedKMSysCfg.theKMSysCfg.workmode;
-		
+#if (ENABLE_PLC)		
 	if (KMRunStat.WorkMode == 1){
 		InitPLC();
 		KMRunStat.WorkMode2 = KMem.CurJumperSW&0x20 ;
 		if (KMRunStat.WorkMode2) {
 			StartPLC(); }
 	}
+#endif	
 	KMem.WX[7]=0x5a;
 #if (BOARD_TYPE == 15 || BOARD_TYPE == 16)
 	KWireLessInit(KMem.EffJumperSW&0x20,KMem.EffJumperSW&0x0f);
 	KWireLessStart();
 #endif
-	
+	KMem.WY[0]=0;
   while (1)
   {
 		//int MyKeyStat1,MyKeyStat2;
@@ -340,14 +428,14 @@
 		int haltick=HAL_GetTick();
 		
 		int thisJumperSW=ReadJumperSW();
-		
+#if (ENABLE_PLC)		
 		if (KMRunStat.WorkMode&1){
 			if (thisJumperSW&0x20 && !(KMem.CurJumperSW&0x20))  // Run 寮�鍏� 姝� 璺冲彉銆�
 			{StartPLC();}
 			if (!(thisJumperSW&0x20) && (KMem.CurJumperSW&0x20))  // Run 寮�鍏� 璐� 璺冲彉銆�
 			{StopPLC();}
 		}
-		
+#endif		
 		KMem.CurJumperSW=thisJumperSW;
 		KMem.haltick=haltick;
 //		KMem.TotalRunTime=TotalRunTime;
@@ -373,7 +461,6 @@
 #else
 		KMem.WX[0]= GetInput();		
 #endif
-		
 		if (GetBoardType() == 7 || GetBoardType() ==8 
 			|| GetBoardType() == 9 || GetBoardType() ==10 || GetBoardType() ==15 || GetBoardType() ==16) 
 		{
@@ -413,7 +500,7 @@
 #endif
 
 //		pProgs = (stBinProg1 *) STORE_PRG_BASE;
-
+#if (ENABLE_PLC)
 		if (	KMRunStat.WorkMode==1 ) //&& bKBusMaster)
 		{
 			if (KMRunStat.nBinProgBank == 0){
@@ -426,7 +513,8 @@
 			
 			ProcessPLCBinProg(pProgs, nSizeProg1);
 		}
-
+#endif
+		
 		KMem.ScanTimeuS=us2-KMem.LastScanTime;
 		KMem.LastScanTime = us2;
 		if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
@@ -441,17 +529,10 @@
 #if (BOARD_TYPE == 14)
 			for (int i=0;i<nOutputBytes;i++)
 			{BufferOut[i+1]=KMem.WYB[i];}
-#else
-//			BufferOut[1]=KMem.WX[0]&0xff;
-//			BufferOut[2]=(KMem.WX[0]>>8)&0xff;
 #endif
-			if (nChilds>0) {		KBusMasterFunc(2); }
-
-#if (BOARD_TYPE == 14)			
-//			KMem.WX[0]=BufferIn[1]+(BufferIn[2]<<8);
-#else
-//			KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8);
-#endif
+				KBusMem.WLY[0]=KMem.WX[0];
+				KBusLoopProcess(&KBus1);
+				KMem.WY[0]=KBusMem.WLX[0];			//KBus Slave
 
 		}
 			if (haltick&0x00002000) SlowFlicker=1;
@@ -467,20 +548,28 @@
 		//	if (! KMem.RunStat) {BufferIn[0]=0;}
 		//	KMem.WY[0]=BufferIn[0];
 #else
-			KBusSlaveFunc(2);	
-			if (! KMem.RunStat) {BufferIn[0]=0;}
-			KMem.WY[0]=BufferIn[0];
+		//	KBusSlaveFunc(&KBus1);	
+			KBusLoopProcess(&KBus1);
+//			if (! KMem.RunStat) {BufferIn[0]=0;}
+//			KMem.WLY[0]=BufferIn[0];
+			KMem.WDT[2] = KBus1.KBusChnStats[0].ClientRecvPkts;
+			
+			KMem.WDT[3] = KBus1.KBusChnStats[0].ClientSendPkts;
+			
+			
+			KMem.WDT[8] = KBus1.RunStat;
+			KMem.WDT[9] = KBus1.ErrStat;
+			
+			KMem.WDD[20] = KBus1.RecvTimeTick;
+			
 #endif
-			if (nSlaveTick&0x00002000) SlowFlicker=1;
+			if (KBus1.nSlaveTick&0x00002000) SlowFlicker=1;
 			else SlowFlicker=0;
-			if (nSlaveTick&0x00000800) FastFlicker=1;
+			if (KBus1.nSlaveTick&0x00000800) FastFlicker=1;
 			else FastFlicker=0;			
-
+		//	KBusMem.WLX[0]=KMem.WX[0];
+		//	KMem.WY[0]=KBusMem.WLY[0];
 		}
-
-//		KMem.WY[0]=nCount2>>5;
-		if (KMem.RunStat) {KMem.RunStat--;}
-		if (KMem.ErrStat) {KMem.ErrStat--;}
 		
 		if (KMRunStat.bLEDFlick)
 		{
@@ -492,11 +581,15 @@
 		}
 		else
 		{
+						KMem.ErrStat = KBus1.ErrStat;	// + KwRunStat.ErrStat;
+#if (ENABLE_PLC)			
 			if (KMRunStat.WorkMode==1 ) {
 				if (PLCMem.bPLCRunning){SetRunLed(SlowFlicker);}
 					else {SetRunLed(0);}
 			}
-			else {
+			else 
+#endif				
+			{
 					if (!KMem.RunStat) SetRunLed(SlowFlicker);
 					else SetRunLed(FastFlicker);
 			}
@@ -511,7 +604,6 @@
 				SetErrLed(FastFlicker);
 				SetErr2Led(FastFlicker);
 				SetOutStat(0);
-				
 			}
 		}
 		
@@ -533,11 +625,12 @@
 		}
 //	KMem.WY[0]=KMem.WLY[0];
 #else
-		KMem.WLX[0]=KMem.WX[0];
-		KMem.WY[0]=KMem.WLY[0];
+//		KMem.WLX[0]=KMem.WX[0];
+//		KMem.WY[0]=KMem.WLY[0];
 #endif
 
 		us5=GetuS();
+		us5=GetTick();
 		
 #if (BOARD_TYPE == 14)
 //		PutOutput (KMem.WY[0]);
@@ -573,7 +666,7 @@
 			Uart1Stat.bPacketRecved=0;
 			Uart1IdelTimer = 0;
 		}else {
-			if (Uart1IdelTimer>600000) {
+			if (Uart1IdelTimer>600000) { // 瓒呰繃60绉掓病鏈夋暟鎹紶杈擄紝閲嶆柊杩涘叆鑷�傚簲娉㈢壒鐜囩姸鎬�
 				LL_USART_EnableAutoBaudRate(USART1);
 				LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);
 			}else {

--
Gitblit v1.9.1