From b8f018f37e2b6a64114c1da032f687010c9a5366 Mon Sep 17 00:00:00 2001
From: QuakeGod <quakegod@sina.com>
Date: 星期五, 28 七月 2023 11:36:40 +0800
Subject: [PATCH] fix PLC function for 0701 device

---
 Src/KBus.c                    |   37 ++++++------
 Src/main.c                    |   41 ++++++++++---
 Src/KMachine.c                |   27 +++++----
 Inc/KMachine.h                |   11 +++
 MDK-ARM/F030C8T6_KBus.uvprojx |    4 
 Src/PLCfunctions.c            |    5 +
 Src/KLink.c                   |    3 
 Inc/GlobalDef.h               |    2 
 8 files changed, 84 insertions(+), 46 deletions(-)

diff --git a/Inc/GlobalDef.h b/Inc/GlobalDef.h
index 51016bc..e5a0d6d 100644
--- a/Inc/GlobalDef.h
+++ b/Inc/GlobalDef.h
@@ -43,7 +43,7 @@
 	
 };
 
-#define BOARD_TYPE 9 
+#define BOARD_TYPE 7 
 #define BOARD_VER 1
 
 #if (BOARD_TYPE == 11)
diff --git a/Inc/KMachine.h b/Inc/KMachine.h
index 5a75262..a97ff69 100644
--- a/Inc/KMachine.h
+++ b/Inc/KMachine.h
@@ -14,11 +14,11 @@
 
 #define STORE_PRG_BASE (FLASH_BASE + 0x00008000)		//32k //and FLASH_BANK1_END
 #define STORE_PRG_PAGESIZE (0x00000400)							//Page Size = 1K
-#define STORE_PRG_PAGES 4													//use 4 pages
+#define STORE_PRG_PAGES 8													//use 4 pages
 
 #define ALT_PRG_BASE (FLASH_BASE + 0x0000A000)		//36k //and FLASH_BANK1_END
 #define ALT_PRG_PAGESIZE (0x00000400)							//Page Size = 1K
-#define ALT_PRG_PAGES 4													//use 4 pages
+#define ALT_PRG_PAGES 8													//use 4 pages
 
 #define STORE_SYSREG_BASE (FLASH_BASE + 0x0000C000)		//40k //and 
 #define STORE_SYSREG_PAGESIZE (0x00000400)							//Page Size = 1K
@@ -289,6 +289,13 @@
 	unsigned int nParam2;
 }stEventLog,* pEventLog;
 
+typedef struct WorkStatus
+{
+	unsigned short bPLCRunning:1;
+	unsigned short bPLCError:1;
+	
+}stWorkStatus;
+
 typedef struct tagRunStat
 {
 	unsigned short Sign1;
diff --git a/MDK-ARM/F030C8T6_KBus.uvprojx b/MDK-ARM/F030C8T6_KBus.uvprojx
index bcf4388..b36432f 100644
--- a/MDK-ARM/F030C8T6_KBus.uvprojx
+++ b/MDK-ARM/F030C8T6_KBus.uvprojx
@@ -15,8 +15,8 @@
         <TargetCommonOption>
           <Device>STM32F030C8Tx</Device>
           <Vendor>STMicroelectronics</Vendor>
-          <PackID>Keil.STM32F0xx_DFP.2.1.0</PackID>
-          <PackURL>http://www.keil.com/pack/</PackURL>
+          <PackID>Keil.STM32F0xx_DFP.2.1.1</PackID>
+          <PackURL>https://www.keil.com/pack/</PackURL>
           <Cpu>IRAM(0x20000000,0x00002000) IROM(0x08000000,0x00010000) CPUTYPE("Cortex-M0") CLOCK(12000000) ELITTLE</Cpu>
           <FlashUtilSpec></FlashUtilSpec>
           <StartupFile></StartupFile>
diff --git a/Src/KBus.c b/Src/KBus.c
index 2cf3391..11be170 100644
--- a/Src/KBus.c
+++ b/Src/KBus.c
@@ -633,23 +633,24 @@
 {
 		int ThisuS=GetuS();
 		int thisRecvTime=KBusRecvTimeuS;
-		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;
-		}
-		
+	 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;
+			}
+	}
 	return 0;
 }
diff --git a/Src/KLink.c b/Src/KLink.c
index c2ad037..68c1e3e 100644
--- a/Src/KLink.c
+++ b/Src/KLink.c
@@ -344,7 +344,7 @@
 				if (KMRunStat.nBinProgBank ==0) {KMRunStat.nBinProgBank=1;}
 				else {KMRunStat.nBinProgBank=0;}
 				SaveRunStat(&KMRunStat);				
-				PLCMem.bPLCRunning=1;
+				//PLCMem.bPLCRunning=1;
 //				KLBufferIn[0]=p1->Params[0];
 //				p1->Params[0]=KLBufferOut[0];
 				PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishProgram,0,0);
@@ -395,6 +395,7 @@
 				memcpy(pData,p1->Params+4,DataLen);					
 				PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
 				SendPacket(nChn, p2, PacketLen);				
+				WriteSysCfgToFlash(&storedKMSysCfg);
 				break;
 			case KLCmdSaveSysCfg:
 				WriteSysCfgToFlash(&storedKMSysCfg);
diff --git a/Src/KMachine.c b/Src/KMachine.c
index 99c0327..1cdc038 100644
--- a/Src/KMachine.c
+++ b/Src/KMachine.c
@@ -62,7 +62,7 @@
 	0x0000,
 	{
 		CFG_VER,
-		0x0000,				//workmode
+		0x0001,				//workmode
 		0x0000,				//switchfunc
 		0x0000,				//pad1;
 		{				//comportparam[2]
@@ -115,7 +115,7 @@
 	END_SIGN,
 };
 
-const stKMSysCfg KMDefaultSysCfg2[7] /*__attribute__((at(STORECFGBASE+sizeof(stKMSysCfg))))*/;
+//const stKMSysCfg KMDefaultSysCfg2[7] /*__attribute__((at(STORECFGBASE+sizeof(stKMSysCfg))))*/;
 	
 int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize)
 {
@@ -242,7 +242,7 @@
 	}else{
 		progByteAddr=(void *)(STORE_PRG_BASE+nProgAddress);
 	}
-	if (nProgAddress ==0)	{
+	if ( (nProgAddress & (STORE_PRG_PAGESIZE - 1)) ==0)	{
 		EraseAndWriteToFlashMem(pBuf, progByteAddr, nByteSize);
 	}else{
 		WriteToFlashMemNoErase(pBuf, progByteAddr, nByteSize);
@@ -283,7 +283,10 @@
 
 int WriteSysCfgToFlash(pStoredKMSysCfg theStoredKMSysCfg)
 {
-	theStoredKMSysCfg->Seq1++;
+	theStoredKMSysCfg->Seq1++;	
+	theStoredKMSysCfg->Sign1 = START_SIGN;
+	theStoredKMSysCfg->EndSign1 = END_SIGN;
+	
 //	theKMSysCfg->cfgvar16++;
 	// find the next empty space to write
 	int nIndex=-1;
@@ -300,12 +303,12 @@
 		nIndex=i;
 		break;
 	}
-	if (nIndex >=0 && nIndex <8)	{	
-		WriteToFlashMemNoErase(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE + nIndex*s2),sizeof(theStoredKMSysCfg));
-	}
-	else 	{
-		EraseAndWriteToFlashMem(theStoredKMSysCfg,(void *)STORE_SYSREG_BASE,sizeof(theStoredKMSysCfg));
-	}
+//	if (nIndex >=0 && nIndex <8)	{	
+//		WriteToFlashMemNoErase(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE + nIndex*s2),sizeof(stStoredKMSysCfg));
+//	}
+//	else 	{
+		EraseAndWriteToFlashMem(theStoredKMSysCfg,(void *)STORE_SYSREG_BASE,sizeof(stStoredKMSysCfg));
+//	}
 	return 0;
 }
 
@@ -611,13 +614,13 @@
 	KMem.TotalRunTime=KMRunStat.UpTime;
 	KMRunStat.PowerCount++;
 	KMem.PwrOnCount=KMRunStat.PowerCount;
-	SaveRunStat(&KMRunStat);
+//	SaveRunStat(&KMRunStat);
 	KMem.SDD[15]=nMaxRunStatIndex;
 	KMem.SDD[16]=nMaxRunStatSeq;
 	KMem.SDD[17]=nNextRunStatSpace;
 
 	
-	AddEventLog(KMem.CurTimeSec,EventTypePowerUp,1,12345);
+//	AddEventLog(KMem.CurTimeSec,EventTypePowerUp,1,12345);
 	KMem.SDD[19]=nEventCount;
 	KMem.SDD[20]=nEventMinIndex;
 	KMem.SDD[21]=nEventMaxIndex;
diff --git a/Src/PLCfunctions.c b/Src/PLCfunctions.c
index 3e699a0..5fd008c 100644
--- a/Src/PLCfunctions.c
+++ b/Src/PLCfunctions.c
@@ -362,7 +362,12 @@
 	for (int i=0;i<256;i++)	{
 		KMem.DT[i]=0;
 	}
+	for (int i=0;i<TOTALTIMERS;i++){
+		KMem.Timers[i].nInited=0;
+	}
+	
 	PLCMem.bPLCRunning=1;
+	KMRunStat.WorkMode2=PLCMem.bPLCRunning;
 	return 0;
 }
 
diff --git a/Src/main.c b/Src/main.c
index e4340a4..ae612ad 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -206,7 +206,9 @@
 	
 	KMRunStat.bLEDFlick = 1;
 	
-	KMem.EffJumperSW=ReadJumperSW();
+	KMem.CurJumperSW=ReadJumperSW();
+	KMem.EffJumperSW=KMem.CurJumperSW;
+	
 #if (BOARD_TYPE == 14)
 	KMem.EffJumperSW|=0x10;
 	nStationID=KMem.EffJumperSW&0x0f;
@@ -221,9 +223,9 @@
   else if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
 	else{bKBusMaster=0;bKBusSlave=1;}	
 #else
-	nStationID=KMem.EffJumperSW&0x7;
-	if (KMem.EffJumperSW == 0x0f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
-  else if ((KMem.EffJumperSW&0x08)!=0) {bKBusMaster=1;bKBusSlave=0;}
+	nStationID=KMem.EffJumperSW&0x0f;
+	if (KMem.EffJumperSW == 0x01f) {bKBusRepeater=1;bKBusMaster=1;bKBusSlave=0;}
+  else if ((KMem.EffJumperSW&0x10)!=0) {bKBusMaster=1;bKBusSlave=0;}
 	else{bKBusMaster=0;bKBusSlave=1;}
 #endif
 	nChilds=nStationID;
@@ -305,9 +307,15 @@
 	KMem.LastScanTime = GetuS();
 
 	KMRunStat.WorkMode=1;
+	KMRunStat.WorkMode2=0;
+		
+	KMRunStat.WorkMode = storedKMSysCfg.theKMSysCfg.workmode;
+		
 	if (KMRunStat.WorkMode == 1){
 		InitPLC();
-		StartPLC();
+		KMRunStat.WorkMode2 = KMem.CurJumperSW&0x20 ;
+		if (KMRunStat.WorkMode2) {
+			StartPLC(); }
 	}
 	KMem.WX[7]=0x5a;
 #if (BOARD_TYPE == 15 || BOARD_TYPE == 16)
@@ -326,8 +334,16 @@
 		us1=GetuS();
 		int haltick=HAL_GetTick();
 		
-//		int CurJumperSW=ReadJumperSW();		
-//		KMem.CurJumperSW=CurJumperSW;
+		int thisJumperSW=ReadJumperSW();
+		
+		if (KMRunStat.WorkMode&1){
+			if (thisJumperSW&0x20 && !(KMem.CurJumperSW&0x20))  // Run 寮�鍏� 姝� 璺冲彉銆�
+			{StartPLC();}
+			if (!(thisJumperSW&0x20) && (KMem.CurJumperSW&0x20))  // Run 寮�鍏� 璐� 璺冲彉銆�
+			{StopPLC();}
+		}
+		
+		KMem.CurJumperSW=thisJumperSW;
 		KMem.haltick=haltick;
 //		KMem.TotalRunTime=TotalRunTime;
 //		KMem.ThisRunTime=ThisRunTime;		
@@ -456,9 +472,14 @@
 		}
 		else
 		{
-			if (!KMem.RunStat) SetRunLed(SlowFlicker);
-			else SetRunLed(FastFlicker);
-			
+			if (KMRunStat.WorkMode==1 ) {
+				if (PLCMem.bPLCRunning){SetRunLed(SlowFlicker);}
+					else {SetRunLed(0);}
+			}
+			else {
+					if (!KMem.RunStat) SetRunLed(SlowFlicker);
+					else SetRunLed(FastFlicker);
+			}
 			if (!KMem.ErrStat) 
 			{
 				SetErrLed(0);

--
Gitblit v1.9.1