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

---
 Ext_FPx/Src/main.c |  121 +++++++++++++++++++++++++++------------
 1 files changed, 83 insertions(+), 38 deletions(-)

diff --git a/Ext_FPx/Src/main.c b/Ext_FPx/Src/main.c
index a1383e6..5b61bb9 100644
--- a/Ext_FPx/Src/main.c
+++ b/Ext_FPx/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)
+#endif
+
+#if (ENABLE_FPX)
 #include "FPx.h"
-#elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+#endif
+
+#if (ENABLE_RF)
 #include "KWireless.h"
+#endif
 //#include "user.h"
 //#include "../src/radio/inc/sx126x-board.h"
-#endif
+
 
 /* USER CODE END Includes */
 
@@ -83,11 +90,12 @@
 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;
+stKBusDef KBus1;							// 
 
 #define RAM_START_ADDR 0x20000000
 #define VECTOR_SIZE 45 
@@ -164,6 +172,10 @@
 		Uart2Stat.bPacketRecved=0;
 		Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1));		
 	}
+	if (Uart2Stat.bPacketRecved)
+	{
+		KBusPacketSendDone(&KBus1);
+	}
 }
 
 /*
@@ -189,6 +201,8 @@
 		case evFPxDataUpdate:
 			for (int i=0;i<16;i++){
 					KBusMem.WLY[i]=FPxMem.WLY[i];
+					KMem.WLY[i]=FPxMem.WLY[i];
+					KMem.WY[i]=FPxMem.WLY[i];				
 			}
 			for (int i=0;i<16;i++) {
 				FPxMem.WLX[i]=KBusMem.WLX[i];
@@ -228,17 +242,22 @@
 		case KBusEvDataUpdate:
 			for (int i=0;i<16;i++){
 					KBusMem.WLY[i]=FPxMem.WLY[i];
+					KMem.WLY[i]=FPxMem.WLY[i];
 			}
+
 			for (int i=0;i<16;i++) {
 				KMem.WLX[i]=KBusMem.WLX[i];
 				FPxMem.WLX[i]=KBusMem.WLX[i];
+				KMem.WX[i]=KBusMem.WLX[i];
 			}				
+	
 // 			KBusMem.WLY[0]=0x0301;			
 			break;
 		
 		default:
 			break;
 	}
+		KMem.DT[2]++;
 	return 0;
 }
 
@@ -256,8 +275,6 @@
 //	RemapIrqVector();
 ///*	
 	  __set_PRIMASK(0);    //鎵撳紑鍏ㄥ眬涓柇
-	
-	KMRunStat.bLEDFlick = 1;
 	
 	InitUartstat(&Uart1Stat,Uart1RxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf));
 	InitUartstat(&Uart2Stat,Uart2RxBuf,sizeof(Uart2RxBuf),Uart2TxBuf,sizeof(Uart2TxBuf));
@@ -307,26 +324,39 @@
 	ReadSysCfgFromFlash(&storedKMSysCfg);
 	
 	KMRunStat.bLEDFlick = 1;
-	
+
+	KLinkInit(1);  	//娉ㄥ唽KLink绔彛
+
+//	stPortDef PortReg1 = {.nPortHardType = 3,.nPortUseType = 2};
+//	KMRegisterPort(0,&PortReg1);
+
+
 	int bKBusMaster,bKBusSlave,bKBusRepeater;;
-	int nChilds;
+	int nKBusStationId;	
+	int nKBusChilds;
 	KMem.CurJumperSW=ReadJumperSW();
 	KMem.EffJumperSW=KMem.CurJumperSW;
-	nChilds=KMem.EffJumperSW&0x0f;
+	
+	nKBusStationId=KMem.EffJumperSW&0x0f;
+
+	nKBusChilds = nKBusStationId;
 //	Uart2Baud = AlterUart2Baud;
 	
-	KBusSetEvCallBackFunc(&KBus1, &KBusEvCallBackFunc),
-	KBusInitMaster(&KBus1, (KBusSendPktFuncDef)PutStr2, nChilds);
+
+	KBusInitMaster(&KBus1, (KBusSendPktFuncDef)PutStr2, nKBusChilds);
+	KBusSetEvCallBackFunc(&KBus1, &KBusEvCallBackFunc);
 	
 #if (BOARD_TYPE == 14)
-	KMem.EffJumperSW|=0x10;
-	nChilds=KMem.EffJumperSW&0x0f;
-  if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
+	KMem.EffJumperSW|=0x40;
+
+  if ((KMem.EffJumperSW&0x40)!=0) {bKBusMaster=1;bKBusSlave=0;}
 	else{bKBusMaster=0;bKBusSlave=1;}
 	
 	FPxSetCallBackFunc(&FPxCallBackFunc);
-	FPx_Init(nChilds);
-	FPx_SetIOCount(8,5);
+	FPx_Init(nKBusChilds);
+	
+	int IOByteCount = nKBusChilds;
+	FPx_SetIOCount(IOByteCount,IOByteCount);
 	
 #elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
 	nStationID=1 ;//KMem.EffJumperSW&0x0f;
@@ -336,11 +366,11 @@
 		{bKBusMaster=0;bKBusSlave=1;}	
 #else
 	nStationID=KMem.EffJumperSW&0x0f;
-	if (KMem.EffJumperSW == 0x1f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
-  else if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
+	if (KMem.EffJumperSW == 0x3f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
+  else if ((KMem.EffJumperSW&0x20)!=0) {bKBusMaster=1;bKBusSlave=0;}
 	else{bKBusMaster=0;bKBusSlave=1;}
 #endif
-	
+		UNUSED(bKBusRepeater);
 	//if (KMem.EffJumperSW == 0x00)
 		Uart1Baud = DefaultUart1Baud;
   MX_USART1_UART_Init();
@@ -421,14 +451,15 @@
 	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
+	
 #if (BOARD_TYPE == 15 || BOARD_TYPE == 16)
 	KWireLessInit(KMem.EffJumperSW&0x20,KMem.EffJumperSW&0x0f);
 	KWireLessStart();
@@ -447,14 +478,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;
@@ -523,7 +554,7 @@
 #endif
 
 //		pProgs = (stBinProg1 *) STORE_PRG_BASE;
-
+#if (ENABLE_PLC)
 		if (	KMRunStat.WorkMode==1 ) //&& bKBusMaster)
 		{
 			if (KMRunStat.nBinProgBank == 0){
@@ -536,7 +567,8 @@
 			
 			ProcessPLCBinProg(pProgs, nSizeProg1);
 		}
-
+#endif
+		
 		KMem.ScanTimeuS=us2-KMem.LastScanTime;
 		KMem.LastScanTime = us2;
 		if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
@@ -552,7 +584,7 @@
 			for (int i=0;i<FPxStat.nOutputBytes;i++)
 			{KBusMem.WLYB[i]=FPxMem.WLYB[i];}
 #endif
-			if (nChilds>0) {		KBusMasterFunc(&KBus1); }
+			KBusLoopProcess(&KBus1);
 
 		}
 			if (haltick&0x00002000) SlowFlicker=1;
@@ -563,17 +595,13 @@
 		if (bKBusSlave)		
 		{
 
-			KBusSlaveFunc(&KBus1);	
+			KBusLoopProcess(&KBus1);	
 			if (KBus1.nSlaveTick&0x00002000) SlowFlicker=1;
 			else SlowFlicker=0;
 			if (KBus1.nSlaveTick&0x00000800) FastFlicker=1;
 			else FastFlicker=0;			
 		}
 
-//		KMem.WY[0]=nCount2>>5;
-		if (KMem.RunStat) {KMem.RunStat--;}
-		if (KMem.ErrStat) {KMem.ErrStat--;}
-		
 		if (KMRunStat.bLEDFlick)
 		{
 			SetRunLed(FastFlicker);
@@ -584,14 +612,23 @@
 		}
 		else
 		{
+#if (ENABLE_PLC)			
 			if (KMRunStat.WorkMode==1 ) {
 				if (PLCMem.bPLCRunning){SetRunLed(SlowFlicker);}
 					else {SetRunLed(0);}
 			}
-			else {
+			else 
+#endif				
+			{
 					if (!KBus1.RunStat) SetRunLed(SlowFlicker);
 					else SetRunLed(FastFlicker);
 			}
+			if (FPxStat.bConfiged) { 
+				SetErrLed(0);
+			}
+			else {
+				SetErrLed(FastFlicker);
+			}			
 			if (!KBus1.ErrStat) 
 			{
 				SetErrLed(0);
@@ -624,7 +661,11 @@
 		}
 //	KMem.WY[0]=KMem.WLY[0];
 #elif (BOARD_TYPE == 14)
-
+			for (int i=0;i<16;i++) {
+				KMem.WLX[i]=KBusMem.WLX[i];
+				FPxMem.WLX[i]=KBusMem.WLX[i];
+				KMem.WX[i]=KBusMem.WLX[i];
+			}				
 #else
 		
 		KMem.WLX[0]=KMem.WX[0];
@@ -693,14 +734,18 @@
 				// mapping bits.
 			for (int i=0;i<6;i++)
 			{
-				USHORT bitaddr = storedKMSysCfg.theKMSysCfg.OutMappings[i];
+				USHORT bitaddr = storedKMSysCfg.theKMSysCfg.OutMappings[i].value;
 				UCHAR type =  (bitaddr&0xf000) >>12;
 				USHORT byteaddr = (bitaddr&0x0ff0) >>4;
 				UCHAR bitpos = bitaddr &0x0f;
 				UCHAR bitvalue = 0 ;
 				if (byteaddr>0) {
-					if (type == 0) bitvalue = KMem.WXB[byteaddr-1] & ( 1 << bitpos );
-					else if (type == 1 ) bitvalue = KMem.WYB[byteaddr-1] & ( 1 << bitpos );
+					if (type == 0) {bitvalue = KMem.WXB[byteaddr-1] & ( 1 << bitpos );}
+					else if (type == 1 ) {bitvalue = KMem.WYB[byteaddr-1] & ( 1 << bitpos );}
+					//else if (type == 2 ) {bitvalue = KMem.WRB[byteaddr-1] & ( 1 << bitpos );}
+					else if (type == 3 ) {bitvalue = KMem.WLXB[byteaddr-1] & ( 1 << bitpos );}
+					else if (type == 4 ) {bitvalue = KMem.WLYB[byteaddr-1] & ( 1 << bitpos );}
+				//	else if (type == 5 ) {bitvalue = KMem.WYB[byteaddr-1] & ( 1 << bitpos );}
 				}
 				if (bitvalue){ LL_GPIO_SetOutputPin(GPIOB,pins[i]);}
 				else {LL_GPIO_ResetOutputPin(GPIOB,pins[i]);}

--
Gitblit v1.9.1