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/Inc/KMachine.h |  307 +++++++++++++++++++++++++++++++--------------------
 1 files changed, 187 insertions(+), 120 deletions(-)

diff --git a/ComLib/Inc/KMachine.h b/ComLib/Inc/KMachine.h
index f9ec465..4ced75d 100644
--- a/ComLib/Inc/KMachine.h
+++ b/ComLib/Inc/KMachine.h
@@ -28,10 +28,14 @@
 //#define STORE_RUNSTAT_PAGESIZE (0x00000400)							//Page Size = 1K
 #define STORE_RUNSTAT_PAGES 1														//use 1 pages
 
+#define STORE_PLC_ANNO_BASE (FLASH_BASE + 0x0000D000)	//56k 
+#define STORE_PLC_ANNO_PAGES 4												// use 4 pages , 1K /page
 
-#define STORE_LOG_BASE (FLASH_BASE + 0x0000D000)		//52k and FLASH_BANK1_END
+
+#define STORE_LOG_BASE (FLASH_BASE + 0x0000E000)		//52k and FLASH_BANK1_END
 //#define STORE_LOG_PAGESIZE (0x00000400)							//Page Size = 1K
 #define STORE_LOG_PAGES 4														//use 4 pages
+
 
 typedef unsigned char uchar;
 typedef unsigned char UCHAR;
@@ -54,13 +58,19 @@
 //  鐢ㄦ埛/绯荤粺鍙傛暟閰嶇疆鍧�
 //  
 //
-
+#pragma anon_unions
 typedef struct tagInfoBlock // 23 bytes
 {
 //	USHORT nBlockLenth;
 	USHORT nDeviceTypeVer;			//device type		x.y
 //	UCHAR nDevierVer;
-	USHORT ProgVer;					//prog version	x.y
+	union {
+		USHORT nProgVer;					//prog version	x.y
+		struct {
+			UCHAR nProgVerMinor;
+			UCHAR nProgVerMajor;
+		};
+	};
 	USHORT KLinkVer;				//x.y
 	USHORT KBusVer;					//x.y
 //	USHORT KNetVer;					//x.y
@@ -229,30 +239,46 @@
 	BYTE Hold2:4;
 }stOutputHoldParam;	
 
-#pragma anon_unions
+typedef struct tagOutMapping
+{
+			USHORT bitPos:4;
+			USHORT byteAddr:8;
+			USHORT type:4;	
+}stOutMapping;
+
+typedef struct tagCfgBlockInfo
+{
+	UCHAR nBlockType;
+	UCHAR nBlockSize;
+}stCfgBlockInfo;
+
 typedef struct tagKMSysCfg		//120 Bytes total
 {
 	USHORT Version;										// SC0	// 2 Bytes
 	USHORT workmode;									// SC1  // 2 Bytes 0=From jumper  
 	USHORT SwitchFunc;								// SC2  // 2 Bytes 
-	USHORT pad1;											// 2 Bytes
+	USHORT nCfgBlockCount;											// 2 Bytes
 	
 	stComPortParam PortParams[2];							// 8 Bytes
 	stInputFilterParam InputParams[16];				//16 Bytes
 	stOutputHoldParam OutputParams[16];				//16 Bytes
 	
-	USHORT OutMappings[6];										//12 Bytes //杈撳嚭鏄犲皠
+	union{
+		USHORT value;										//12 Bytes //杈撳嚭鏄犲皠
+		stOutMapping;
+	}OutMappings[8];
+
+	USHORT nProgBank;
+	USHORT nProgSize;
+	USHORT nAnnoSize;
+	USHORT nCount;
 	
-	UINT cfgvar3;															// 4 Bytes
-	UINT cfgvar4;															// 4 Bytes
-	UINT cfgvar5;															// 4 Bytes
-	UINT cfgvar6;															// 4 Bytes
-	UINT cfgvar7;															// 4 Bytes
+	stCfgBlockInfo CfgBlockInfos[8];
 	UINT cfgvar8;															// 4 Bytes
 	UINT cfgvar9;															// 4 Bytes
 	UINT cfgvar10;														// 4 Bytes
-	UINT cfgvar11;														// 4 Bytes
-	UINT cfgvar12;														// 4 Bytes
+//	UINT cfgvar11;														// 4 Bytes
+//	UINT cfgvar12;														// 4 Bytes
 //	UINT cfgvar13;														// 4 Bytes
 //	UINT cfgvar14;														// 4 Bytes
 //	UINT cfgvar15;														// 4 Bytes
@@ -372,10 +398,6 @@
 		KLDataTypeTest = 254 | TYPEDATA,
 	};
 
-#define TOTAL_WDFS (32)		//Total DF Count
-#define TOTAL_CurVAL (16)		//
-#define TOTALTIMERS (64)
-
 enum enKLDataCounts
 	{
 
@@ -406,44 +428,126 @@
 
 	};
 
-typedef struct tagTimerStat
+
+// 閫氫俊绔彛锛� 娉ㄥ唽鍒� KMachine 閲岋紝 缁熶竴绠$悊
+
+enum enPortHardType
 {
-	unsigned short nScale:2;//Time Scale, 0:1ms 1:10ms 2:100ms 3:1S
-	unsigned short nType:1;	//0 : timer 1:counter ;
-	unsigned short nDir:1;		//0 : count down. 1 count up;
-	unsigned short nInited:1;
-	unsigned short bSet:1;
-	unsigned short bTon:1;
+	PortHardType_None	=	0,	//	绌烘帴鍙�,鏃犵被鍨�
+	PortHardType_SOFT	=	1,	//	杞欢铏氭嫙鎺ュ彛,鍙兘鏄毀閬撶瓑.
+	PortHardType_UART	=	2,	//	涓茶鎺ュ彛
+	PortHardType_RS232	=	3,	//	232鎺ュ彛
+	PortHardType_RS485	=	4,	//	485鎺ュ彛
+	PortHardType_SLP	=	5,	//	鍗曟�荤嚎鎺ュ彛
+	PortHardType_RF		=	6,	//	鏃犵嚎鎺ュ彛
+	PortHardType_LORA	=	7,	//	LoRa鏃犵嚎鎺ュ彛
+	PortHardType_OPTI	=	8,	//	鍏夌氦鎺ュ彛
+	PortHardType_ETH	=	9,	//	浠ュお缃戞帴鍙�
+	PortHardType_WIFI	=	10,	//	WiFi鎺ュ彛
+	PortHardType_BT		=	11,	//	浠ュお缃戞帴鍙�
+
+};
+enum enPortUseType
+{
+	PortUse_Default = 0,	//榛樿
+	PortUse_Console = 1,	//鎺у埗鍙版帴鍙�
+	PortUse_KLink = 2,		//KLink閫氳
+	PortUse_KBus = 3, 		//KBus閫氳
+	PortUse_KNet = 4, 		// KNet閫氳
+	PortUse_SLP = 5,		// SLP鍗曟�荤嚎
+	PortUse_KRF = 6, 		// 鏃犵嚎閫氳
+	PortUse_ModbusRTU = 7, //Modbus RTU 閫氳
+	PortUse_ModbusTCP = 8, //Modbus TCP 閫氳
+	PortUse_Com = 9,		//	璁$畻鏈洪�氳
+	PortUse_Gen = 10,		//閫氱敤閫氳锛岃嚜鐢卞彛
+};
+
+typedef struct tagPortAbility
+{
+	ushort ChildList:1;
+	ushort AccessChild:1;
+	ushort TranProg:1;
+	ushort TranCfg:1;
+	ushort Diag:1;
+	ushort TranOutBandData:1;
+	ushort Tunnel:1;
+	ushort TranFirmware:1;
+	ushort TranBlink:1;
+
+}stPortAbility;
+
+enum enServiceReqs
+{
+	ReqNone,
+	ReqInit,
+	ReqReset,
+	ReqStop,
+	ReqRun,
+	ReqBlinkLED,
+	ReqStartDiag,
+	ReqStopDiag,
+	ReqPortChildInfo,
+	ReqPortChnInfo,
+	ReqUpdateFirm,
+	ReqUpdateFirmInfo,
 	
-}stTimerStat;
+	ReqTransFirmware,
+	ReqTransCfg,
+	ReqTransProg,
+	ReqTransData,
+	ReqTransBlink,
+	ReqTransChild,
+	ReqTransInfo,
+	ReqTransOutBandData,
+	ReqRead1Bit,
+	ReqWrite1Bit,
+	ReqReadBits,
+	ReqWriteBits,
+	ReqReadData,
+	ReqWriteData,
+	ReqRemoteTran,
+	
+};
 
-typedef struct tagTimer
+// 鍚勭鍙� 浜掕仈 閫氫俊
+typedef int (*CommFuncDef)(void * pInstance, int ReqId , int nParam1, int nParam2, void ** pData, unsigned short * len1);
+typedef struct tagPortDef
 {
-	unsigned int LastActTime;
 	union {
-		unsigned short StatByte;
-	 struct 
-	{
-		unsigned short nScale:2;	//Time Scale, 0:1ms 1:10ms 2:100ms 3:1S
-		unsigned short nType:1;		//0 : timer 1	:	counter ;
-		unsigned short nDir:1;		//0 : count down. 1 count up;
-		unsigned short nInited:1;
-		unsigned short bSet:1;
-		unsigned short bTon:1;
-		
-	};		
-//		stTimerStat Stat;
+		USHORT nPortType;
+		struct {
+			UCHAR nPortHardType;
+			UCHAR nPortUseType;
+		};
 	};
-}stTimer;
+	ushort ability;		//鑳藉姏, 鑾峰彇瀛愭満淇℃伅绛夈��
+	uchar bEnable;
+	uchar bRunning;
+	uchar StationId;
+	uchar bMaster;
+	uchar nMaxStations;
+	uchar nCurStations;
+	uchar nHealth;
+	void * pPortConfig;
+	short PortConfigType;
+	short PortConfigSize;
+	void * pPortRunStatus;
+	short PortRunStatType;
+	short PortRunStatSize;
+	void * pInstance;					//閫氫俊缁撴瀯瀹炰綋
+	CommFuncDef ReqCommFunc;	//閫氫俊璇锋眰鍑芥暟鎸囬拡
+	// func2 CommReq;
+	
+}stPortDef,* pstPortDef;
 
+typedef struct tagInterComm
+{
+	int nId;
+	
+}stInterComm;
 
 typedef struct tagKMem
 {
-	unsigned short WDFs[TOTAL_WDFS];
-	unsigned char CurVALs[TOTAL_CurVAL];
-	unsigned char CurVAL;
-	stTimer Timers[TOTALTIMERS];
-
 	union {
 	unsigned short WX[KLDataWXCount];		//鏈満鐨刋鍜孻
 	unsigned char WXB[KLDataWXCount*2];	
@@ -454,18 +558,20 @@
 	}; 
 	unsigned short WR[KLDataWRCount];
 	
-	unsigned short WT[16];
-
-	unsigned short WC[16];
-	unsigned short EV[KLDataEVCount];
-	unsigned short SV[KLDataSVCount];
 	
-	
-	unsigned short WLX[16];		//铏氭嫙鐨刋鍜孻锛岃繙绋嬮�氳鏃舵槧灏勭敤銆�
-	unsigned short WLY[16];
-	unsigned short WLR[16];
+	//铏氭嫙鐨刋鍜孻锛岃繙绋嬮�氳鏃舵槧灏勭敤銆�
+	union{
+	ushort WLX[16];
+	uchar WLXB[32];
+	};
+	union{
+	ushort WLY[16];
+	uchar WLYB[32];
+	};
 	unsigned short WFX[16];
 	unsigned short WFY[16];
+
+	unsigned short WLR[16];
 	unsigned short WSR[16];
 
 union {
@@ -513,72 +619,11 @@
 		unsigned char WDB[KLDataWDTCount*2];
 	};
 	
+	int nTotalPorts;
+	stPortDef * pPorts[8];
+
 }stKMem;
 
-
-// 閫氫俊绔彛锛� 娉ㄥ唽鍒� KMachine 閲岋紝 缁熶竴绠$悊
-
-typedef struct tagPortAbility
-{
-	ushort ChildList:1;
-	ushort AccessChild:1;
-	ushort TranProg:1;
-	ushort TranCfg:1;
-	ushort Diag:1;
-	ushort TranOutBandData:1;
-	ushort Tunnel:1;
-	ushort TranFirmware:1;
-	ushort TranBlink:1;
-	
-	
-}stPortAbility;
-
-enum enServiceReqs
-{
-	ReqNone,
-	ReqInit,
-	ReqReset,
-	ReqStop,
-	ReqRun,
-	ReqBlink,
-	ReqStartDiag,
-	ReqStopDiag,
-	ReqTransFirmware,
-	ReqTransCfg,
-	ReqTransProg,
-	ReqTransData,
-	ReqTransBlink,
-	ReqTransChild,
-	ReqTransInfo,
-	ReqTransOutBandData,
-	ReqRead1Bit,
-	ReqWrite1Bit,
-	ReqReadBits,
-	ReqWriteBits,
-	ReqReadData,
-	ReqWriteData,
-	ReqRemoteTran,
-	
-};
-
-// 鍚勭鍙� 浜掕仈 閫氫俊
-typedef int (*CommFunc)(int n , void *, int len1);
-typedef struct tagPortReg
-{
-	ushort nType;
-	ushort nVer;
-	// func1 GetInfo;
-	// func2 CommReq;
-	ushort ability;		//鑳藉姏, 鑾峰彇瀛愭満淇℃伅绛夈��
-	
-}stPortReg;
-
-typedef struct tagInterComm
-{
-	int nId;
-	
-	
-}stInterComm;
 
 
 extern stKMem KMem;
@@ -590,20 +635,42 @@
 extern volatile int OldPowerDownEventTime;
 
 int KMachineInit(void);
+int KMachineLoopProc(void);
+
+/*
+	鍚慘Machine 娉ㄥ唽 閫氳绔彛,浠ヤ究閫氳绔彛涔嬮棿鐨勫姛鑳戒簰閫�
+*/
+int KMRegisterPort(ushort nType,stPortDef * thePortParam);
+
+int KMPortReqFunc(int nPortIndex,int nReqId, int nParam1, int nParam2, void ** pData, unsigned short * nlen1);
+
+
 int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize);
 int EraseAndWriteToFlashMem(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
 int WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
+int WriteToFlashAutoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
 
 int ReadFactoryData(void * pDatabuf, int nByteCount);
 int WriteFactoryData(void * pDataBuf, int nByteCount);
 
 // active Program bank
-int ReadProgram(int nProgByteAddr, void *pBuf, int nByteSize, int nBank);
-int WriteProgram(int nProgByteAddr, void * pBuf, int nByteSize, int nBank);
+
+int ReadPLCProgram(int nBank, int nProgByteAddr, void *pBuf, int nByteSize);
+int StartPLCProgram(int nBank, int nByteSize, int nCRC);
+int WritePLCProgram(int nBank, int nProgByteAddr, void * pBuf, int nByteSize);
+int FinishiPLCProgram(int nBank, int nByteSize, int nCRC );
+
+int ReadPLCAnno(int nType, int nProgByteAddr, void *pBuf, int nByteSize);
+int StartPLCAnno(int nType, int nByteSize, int nCRC);
+int WritePLCAnno(int nType, int nProgByteAddr, void * pBuf, int nByteSize);
+int FinishiPLCAnno(int nType, int nByteSize, int nCRC );
 
 int WriteSysCfgToFlash(pStoredKMSysCfg theStoredKMSysCfg);
 int ReadSysCfgFromFlash(pStoredKMSysCfg theStoredKMSysCfg);
 
+int KMRunService(int nSvrId, int nParam1, int nParam2, void **pData, unsigned short *nled1);
+
+int CheckEventLog(void);
 int AddEventLog(uint32_t nTime, USHORT nEvent, USHORT nParam1, UINT nParam2);
 pEventLog GetEventLogAddr(int nIndex);
 int ClearEventLog(void);

--
Gitblit v1.9.1