From 2364f06313efb8eb97d4ae741a31cdd2efc175d2 Mon Sep 17 00:00:00 2001
From: QuakeGod <QuakeGod@sina.com>
Date: 星期三, 01 二月 2023 16:05:50 +0800
Subject: [PATCH] KBus fix after merget together

---
 Src/main.c |  246 +++++++++++++++++++++++++++++--------------------
 1 files changed, 145 insertions(+), 101 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index 81f5051..36214fe 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -51,12 +51,16 @@
 #include "string.h"
 #include "BSP.h"
 #include "ModbusRTU.h"
-#if (BOARD_TYPE == 12)
-#include "FP0.h"
-#elif (BOARD_TYPE == 13)
+#if (BOARD_TYPE == 13)
 #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)
+#include "KWireless.h"
+//#include "user.h"
+//#include "../src/radio/inc/sx126x-board.h"
 #endif
 
 /* USER CODE END Includes */
@@ -66,19 +70,17 @@
 /* USER CODE BEGIN PV */
 /* Private variables ---------------------------------------------------------*/
 
-#define RX2BUFSIZE 128
-#define TX2BUFSIZE 128
+#define RX2BUFSIZE 64
+#define TX2BUFSIZE 64
 
-unsigned char Uart1RxBuf[256];
-unsigned char Uart1TxBuf[512];
+unsigned char Uart1RxBuf[128];
+unsigned char Uart1TxBuf[260];
 
 unsigned char Uart2RxBuf[RX2BUFSIZE];
 unsigned char Uart2TxBuf[TX2BUFSIZE];
 
-unsigned int SlowFlicker=0;
-unsigned int FastFlicker=0;
-
-int ContinueSend=0;
+unsigned char SlowFlicker=0;
+unsigned char FastFlicker=0;
 
 stBinProg1 * pProgs = (stBinProg1 *)STORE_PRG_BASE;
 
@@ -122,6 +124,8 @@
 		Count=0; 
 		KMem.CurTimeSec++;
 		KMem.ThisRunTime++; KMem.TotalRunTime++;
+		if (KMRunStat.bLEDFlick) KMRunStat.bLEDFlick--;
+		if (KMRunStat.bLEDFlick >120) KMRunStat.bLEDFlick=120;
 	}
 
 	return;
@@ -137,9 +141,10 @@
 int main(void)
 {
   /* USER CODE BEGIN 1 */
-
-	InitUartstat(&Uart1Stat,Uart1TxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf));
-	InitUartstat(&Uart2Stat,Uart2TxBuf,sizeof(Uart2RxBuf),Uart2TxBuf,sizeof(Uart2TxBuf));
+	KMRunStat.bLEDFlick = 1;
+	
+	InitUartstat(&Uart1Stat,Uart1RxBuf,sizeof(Uart1RxBuf),Uart1TxBuf,sizeof(Uart1TxBuf));
+	InitUartstat(&Uart2Stat,Uart2RxBuf,sizeof(Uart2RxBuf),Uart2TxBuf,sizeof(Uart2TxBuf));
   /* USER CODE END 1 */
 
   /* MCU Configuration----------------------------------------------------------*/
@@ -151,18 +156,18 @@
 
 	for (int i=0;i<9;i++)
 	{
-//		memset(ChnStats[i],0,0);		
-		ChnStats[i].SendPackets=0;
-		ChnStats[i].RecvPackets=0;
-		ChnStats[i].LostPackets=0;
-		ChnStats[i].CtnLstPkts=0;
-		ChnStats[i].MaxCtnLstPkts=0;
-		ChnStats[i].NotPkgErr=0;
-		ChnStats[i].PkgLenErr=0;
-		ChnStats[i].TimeOutErr=0;
-		ChnStats[i].BCCErr=0;
-		ChnStats[i].Delay=0;
-		ChnStats[i].MaxDelay=0;
+//		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;
@@ -186,9 +191,9 @@
   SystemClock_Config();
 
   /* USER CODE BEGIN SysInit */
-	TickFreq=10000;		//Tick频率
+	TickFreq=10000;		//Tick棰戠巼
 	InituS(TickFreq);	
- // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/TickFreq);	//重新定义SysTick的频率�
+ // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/TickFreq);	//閲嶆柊瀹氫箟SysTick鐨勯鐜�
 
   /* USER CODE END SysInit */
 
@@ -197,23 +202,31 @@
   MX_DMA_Init();
 	
 	KMachineInit();
-	ReadSysCfgFromFlash(&KMSysCfg);
+	ReadSysCfgFromFlash(&storedKMSysCfg);
+	
+	KMRunStat.bLEDFlick = 1;
 	
 	KMem.EffJumperSW=ReadJumperSW();
-#if (BOARD_TYPE == 12)
+#if (BOARD_TYPE == 14)
 	KMem.EffJumperSW|=0x10;
-	nAddr=KMem.EffJumperSW&0x0f;
-  if ((KMem.EffJumperSW&0x10)!=0) {bMaster=1;bSlave=0;}
-	else{bMaster=0;bSlave=1;}
-	nChilds=nAddr;
+	nStationID=KMem.EffJumperSW&0x0f;
+  if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
+	else{bKBusMaster=0;bKBusSlave=1;}
+	nChilds=nStationID;
 	FP0_Init();
+
+#elif (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+	nStationID=KMem.EffJumperSW&0x0f;
+	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
-	nAddr=KMem.EffJumperSW&0x7;
-	if (KMem.EffJumperSW == 0x0f) {bRepeater=1;bMaster=1;bSlave=0;}
-  else if ((KMem.EffJumperSW&0x08)!=0) {bMaster=1;bSlave=0;}
-	else{bMaster=0;bSlave=1;}
+	nStationID=KMem.EffJumperSW&0x7;
+	if (KMem.EffJumperSW == 0x0f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
+  else if ((KMem.EffJumperSW&0x08)!=0) {bKBusMaster=1;bKBusSlave=0;}
+	else{bKBusMaster=0;bKBusSlave=1;}
 #endif
-	nChilds=nAddr;
+	nChilds=nStationID;
 	nCurPollId=1;
 	//if (KMem.EffJumperSW == 0x00)
 		Uart1Baud = DefaultUart1Baud;
@@ -223,7 +236,7 @@
 	MX_SPI1_Init();
 	LL_SPI_EnableIT_RXNE(SPI1);
 
-#if (BOARD_TYPE == 12)
+#if (BOARD_TYPE == 14)
 //	MX_SPI2_Init();
 //  MX_ADC_Init();
 #else
@@ -255,7 +268,7 @@
 	
 //	res = listen(0);
 #endif	
-//	if (bSlave)
+//	if (bKBusSlave)
 	{
 	//	LL_USART_EnableAutoBaudRate(USART1);
 	//	LL_USART_SetAutoBaudRateMode(USART1, LL_USART_AUTOBAUD_DETECT_ON_FALLINGEDGE);		
@@ -273,7 +286,7 @@
 	SetRunLed(1);				//Turn On Run Led
 	SetErrLed(0);				//Turn Off Err Led
 
-#if (BOARD_TYPE == 12)
+#if (BOARD_TYPE == 14)
 //	PutOutput (0);					//Clear all Output
 //	Enable595(1);						//Enable 595 Output 
 #else
@@ -282,7 +295,7 @@
 #endif
 
 		if (GetBoardType() == 7 || GetBoardType() ==8 
-			|| GetBoardType() == 9 || GetBoardType() ==10 ) 
+			|| GetBoardType() == 9 || GetBoardType() ==10 ||GetBoardType() ==13 ||GetBoardType() ==15 || BOARD_TYPE == 16) 
 		{
 			displayInput(0xffff);		//
 			EnableDisIn(1);				//Input Diaplay Enable 595 
@@ -297,14 +310,15 @@
 		StartPLC();
 	}
 	KMem.WX[7]=0x5a;
-
+#if (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+	KWireLessInit(KMem.EffJumperSW&0x20,KMem.EffJumperSW&0x1f);
+	KWireLessStart();
+#endif
   while (1)
   {
 		//int MyKeyStat1,MyKeyStat2;
 		//MyKeyStat1=GetInput();
-#if (BOARD_TYPE == 12)
-		FP0_Proc();
-#endif
+
 		//*((unsigned int *)&(PLCMem.SDT[10]))=nRunCount;
 	//	KMem.nRunCount=nRunCount;
 		SlowFlicker=0;
@@ -322,42 +336,20 @@
 //		KMem.SDD[13]=PendSvCount;
 //		KMem.SDD[14]=RCC->CSR;		
 
-#if (BOARD_TYPE == 12)
+#if (BOARD_TYPE == 14)
 //		KMem.WX[0]= GetInput();		
+		FP0_Proc();
 #else
 		KMem.WX[0]= GetInput();		
 #endif
 		
 		if (GetBoardType() == 7 || GetBoardType() ==8 
-			|| GetBoardType() == 9 || GetBoardType() ==10 ) 
+			|| GetBoardType() == 9 || GetBoardType() ==10 || GetBoardType() ==15 || GetBoardType() ==16) 
 		{
 			displayInput(KMem.WX[0]);
 		}
 		us2=GetuS();
-		
-		
-//		pProgs = (stBinProg1 *) STORE_PRG_BASE;
-
-		if (	KMRunStat.WorkMode==1)
-		{
-			if (KMRunStat.nBinProgBank == 0){
-				pProgs=(stBinProg1 *)STORE_PRG_BASE;
-			}else {
-				pProgs=(stBinProg1 *)ALT_PRG_BASE;
-			}
-			nSizeProg1=KMRunStat.nBinProgSize;
-			
-			ProcessPLCBinProg(pProgs, nSizeProg1);
-		}
-
-		KMem.ScanTimeuS=us2-KMem.LastScanTime;
-		KMem.LastScanTime = us2;
-		if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
-		if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
-//		if (repeater)		{	RepeaterFunc();	}
-
-		us3=GetuS();
-
+///*
 		if ((KMem.nRunCount &0x1f) == 0x02)
 		{
 			ADCProcess();
@@ -379,19 +371,47 @@
 				}
 			}
 		}
+//*/
 
-		if (bMaster)		
+#if (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+		Radio.IrqProcess( ); // Process Radio IRQ
+#endif
+
+//		pProgs = (stBinProg1 *) STORE_PRG_BASE;
+
+		if (	KMRunStat.WorkMode==1 && bKBusMaster)
 		{
-#if (BOARD_TYPE == 12)
+			if (KMRunStat.nBinProgBank == 0){
+				pProgs=(stBinProg1 *)STORE_PRG_BASE;
+			}else {
+				pProgs=(stBinProg1 *)ALT_PRG_BASE;
+			}
+			nSizeProg1=KMRunStat.nBinProgSize;
+			
+			ProcessPLCBinProg(pProgs, nSizeProg1);
+		}
+
+		KMem.ScanTimeuS=us2-KMem.LastScanTime;
+		KMem.LastScanTime = us2;
+		if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
+		if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
+
+		//		if (bKBusRepeater)		{	KBusRepeaterFunc();	}
+
+		us3=GetuS();
+
+		if (bKBusMaster)		
+		{
+#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) {		MasterFunc(2); }
+			if (nChilds>0) {		KBusMasterFunc(2); }
 
-#if (BOARD_TYPE == 12)			
+#if (BOARD_TYPE == 14)			
 //			KMem.WX[0]=BufferIn[1]+(BufferIn[2]<<8);
 #else
 //			KMem.WY[0]=BufferIn[1]+(BufferIn[2]<<8);
@@ -403,13 +423,18 @@
 			if (haltick&0x00000800) FastFlicker=1;
 			else FastFlicker=0;	
 
-		if (bSlave)		
+		if (bKBusSlave)		
 		{
 //			BufferOut[0]=KMem.WX[0];
-			SlaveFunc(2);	
+#if (BOARD_TYPE == 15 || BOARD_TYPE == 16)
+//			KBusSlaveFunc(2);	
+		//	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];
-			
+#endif
 			if (nSlaveTick&0x00002000) SlowFlicker=1;
 			else SlowFlicker=0;
 			if (nSlaveTick&0x00000800) FastFlicker=1;
@@ -420,20 +445,33 @@
 //		KMem.WY[0]=nCount2>>5;
 		if (KMem.RunStat) {KMem.RunStat--;}
 		if (KMem.ErrStat) {KMem.ErrStat--;}
-
-		if (!KMem.RunStat) SetRunLed(SlowFlicker);
-		else SetRunLed(FastFlicker);
 		
-		if (!KMem.ErrStat) 
+		if (KMRunStat.bLEDFlick)
 		{
-			SetErrLed(0);
-			SetOutStat(1);
-		}
-		else 
-		{
+			SetRunLed(FastFlicker);
 			SetErrLed(FastFlicker);
-			SetOutStat(0);
+			SetErr2Led(FastFlicker);
+			SetOutStat(!FastFlicker);
+			//KMRunStat.bLEDFlick-- ;
+		}
+		else
+		{
+			if (!KMem.RunStat) SetRunLed(SlowFlicker);
+			else SetRunLed(FastFlicker);
 			
+			if (!KMem.ErrStat) 
+			{
+				SetErrLed(0);
+				SetErr2Led(0);
+				SetOutStat(1);
+			}
+			else 
+			{
+				SetErrLed(FastFlicker);
+				SetErr2Led(FastFlicker);
+				SetOutStat(0);
+				
+			}
 		}
 		
 //		SetRunLed(RunStat);
@@ -442,17 +480,21 @@
 		us4=GetuS();
 //		EffJumperSW = GetInput(20)&0xff;
 
-#if (BOARD_TYPE == 12)
-		PutOutput (KMem.WY[0]);
-#else
+#if (BOARD_TYPE == 14)
 //		PutOutput (KMem.WY[0]);
+#else
+		PutOutput (KMem.WY[0]);
 #endif
 		//PutOutput (KMem.nRunCount>>8);
 		//PutOutput(0x0f70);
-		
+#if (BOARD_TYPE == 15 || BOARD_TYPE == 16)		
+	//	KMem.WY[1]=KMem.nRunCount>>6;
+		KMem.WY[1]=KMem.WX[0];
+		KMem.WY[0]=KMem.WX[1];
+#endif
 		us5=GetuS();
-//		if (bMaster) ShowInfo();
-//		if (bSlave) ShowInfo();
+//		if (bKBusMaster) ShowInfo();
+//		if (bKBusSlave) ShowInfo();
 		us6=GetuS();
 		add1(10,10);
 		for (int i=0;i<64;i++)
@@ -461,8 +503,8 @@
 		}
 		KMem.nRunCount++;
 //		int nSize=sizeof(stChnStat);
-//		memcpy(&KMem.SDT[64],&ChnStats[1],nSize);
-//		memcpy(&KMem.SDT[64+nSize/2],&ChnStats[2],nSize);
+//		memcpy(&KMem.SDT[64],&KBusChnStats[1],nSize);
+//		memcpy(&KMem.SDT[64+nSize/2],&KBusChnStats[2],nSize);
 //		for (int i=0;i<128;i++)	{		SDT[i]=i;	}
 //		SDT[48]=55;
 		if (Uart1RecvBuf1DataLen >0 && Uart1Stat.bPacketRecved)
@@ -476,7 +518,7 @@
 			Uart1RecvBuf1DataLen=0;
 			Uart1Stat.bPacketRecved=0;
 		}
- if (bSlave)	HAL_Delay(0);
+ if (bKBusSlave)	HAL_Delay(0);
 /*
 		if (!IsEmpty(&Uart1Stat.QRx))
 		{
@@ -488,7 +530,7 @@
 		}
 */
 
-#if (BOARD_TYPE == 12)
+#if (BOARD_TYPE == 14)
 		//process 6 output
 		{
 				// mapping bits.
@@ -509,6 +551,7 @@
 		}
 #endif
 
+/*		
 		{
 		unsigned char pos,seg;
 		unsigned short val;
@@ -527,9 +570,10 @@
 		seg=~seg;
 		//	PutOutputSPI1(pos|(seg<<8));
 	}
+*/
+
 #if (BOARD_TYPE == 13)	
 	 w5500_network_info_show();
-	
 //	 loopback_tcps(0,str1,5000);
 #endif
 

--
Gitblit v1.9.1