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 |  281 ++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 214 insertions(+), 67 deletions(-)

diff --git a/ComLib/Inc/KMachine.h b/ComLib/Inc/KMachine.h
index dd6093e..4ced75d 100644
--- a/ComLib/Inc/KMachine.h
+++ b/ComLib/Inc/KMachine.h
@@ -9,33 +9,37 @@
 #define __KMACHINE_H__
 
 #define FACTORY_DATA_BASE (FLASH_BASE + 0x00007C00)		//31k //and 
-#define FACTORY_DATA_PAGESIZE (0x00000400)						//Page Size = 1K
+//#define FACTORY_DATA_PAGESIZE (0x00000400)						//Page Size = 1K
 #define FACOTRY_DATA_PAGES 1												  // use 1 page(s)
 
 #define STORE_PRG_BASE (FLASH_BASE + 0x00008000)		//32k //and FLASH_BANK1_END
-#define STORE_PRG_PAGESIZE (0x00000400)							//Page Size = 1K
+//#define STORE_PRG_PAGESIZE (0x00000400)							//Page Size = 1K
 #define STORE_PRG_PAGES 8													//use 4 pages
 
 #define ALT_PRG_BASE (FLASH_BASE + 0x0000A000)		//40k //and FLASH_BANK1_END
-#define ALT_PRG_PAGESIZE (0x00000400)							//Page Size = 1K
+//#define ALT_PRG_PAGESIZE (0x00000400)							//Page Size = 1K
 #define ALT_PRG_PAGES 8													//use 4 pages
 
 #define STORE_SYSREG_BASE (FLASH_BASE + 0x0000C000)		//48k //and 
-#define STORE_SYSREG_PAGESIZE (0x00000400)							//Page Size = 1K
+//#define STORE_SYSREG_PAGESIZE (0x00000400)							//Page Size = 1K
 #define STORE_SYSREG_PAGES 1														//use 1 pages
 
 #define STORE_RUNSTAT_BASE (FLASH_BASE + 0x0000C800)		//50k K //and 
-#define STORE_RUNSTAT_PAGESIZE (0x00000400)							//Page Size = 1K
+//#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_PAGESIZE (0x00000400)							//Page Size = 1K
+
+#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;
+typedef unsigned short ushort;
 typedef unsigned short USHORT;
 typedef unsigned int UINT;
 typedef unsigned int uint32_t;
@@ -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
@@ -118,6 +128,14 @@
 	
 }stFactoryData,* pFactoryData;
 
+typedef struct tagNewAppInfoBlock
+{
+	unsigned short Sign;
+	unsigned short Version;
+	unsigned int Length;
+	unsigned int nCRC;
+	
+}stNewAppInfoBlock,*pNewAppInfoBlock;
 
 enum enStoreCfg
 {
@@ -221,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
@@ -356,15 +390,13 @@
 		KLDataTypeLD = 13 | TYPEDATA,
 		KLDataSysCfg = 25 | TYPEDATA,
 		KLDataTypeFlash = 33 | TYPEDATA,
-		KLDataTypeWDT = 41 | TYPEDATA,
-		KLDataTypeKBD = 51 | TYPEDATA,
-		KLDataTypeKWLD = 52 | TYPEDATA,
+		KLDataTypeWDT = 41 | TYPEDATA,					//璋冭瘯鐩戞帶鏁版嵁
+		KLDataTypeKBD = 51 | TYPEDATA,					//KBus 璋冭瘯鏁版嵁
+		KLDataTypeKWLD = 52 | TYPEDATA,					//鏃犵嚎璋冭瘯鏁版嵁
+		KLDataTypeNewApp = 61 | TYPEDATA,				// New FirmWare
+		KLDataTypeNewAppInfo = 62 | TYPEDATA, 	// New Firmware Info Block;
 		KLDataTypeTest = 254 | TYPEDATA,
 	};
-
-#define TOTAL_WDFS (32)		//Total DF Count
-#define TOTAL_CurVAL (16)		//
-#define TOTALTIMERS (64)
 
 enum enKLDataCounts
 	{
@@ -396,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];	
@@ -444,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 {
@@ -503,7 +619,11 @@
 		unsigned char WDB[KLDataWDTCount*2];
 	};
 	
+	int nTotalPorts;
+	stPortDef * pPorts[8];
+
 }stKMem;
+
 
 
 extern stKMem KMem;
@@ -515,30 +635,57 @@
 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 WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned 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);
 int LoadRunStat(pRunStat theRunStat);
 int SaveRunStat(pRunStat theRunStat);
 
+int WriteNewApp(int nProgByteAddr, void *pBuf, int nByteSize);
+int WriteNewAppInfo(int nProgByteAddr, void *pBuf, int nByteSize);
 
 unsigned char GetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr);
 int SetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr, unsigned char nCoilValue);
 int GetVarData(int nDataType, int nDataAddr);
 int SetVarData(int nDataType, int nDataAddr, int nDataValue);
 
-#endif	/* __KLPROTOCOL_H__ */
+typedef int (*KMSvCBDef) (int nChn, int nSvType, int ,void *, int);	//Service Req callback func ,param is  int ,int, int, void *,int; return int;
+
+
+#endif	/* __KMACHINE_H__ */

--
Gitblit v1.9.1