From e1f35018c4dec304b00f50d9dbe12204fd57a623 Mon Sep 17 00:00:00 2001
From: QuakeGod <QuakeGod@sina.com>
Date: 星期四, 29 九月 2022 15:35:10 +0800
Subject: [PATCH] update infoblock step 1

---
 Src/KMachine.c |  343 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 332 insertions(+), 11 deletions(-)

diff --git a/Src/KMachine.c b/Src/KMachine.c
index 02aa814..228848a 100644
--- a/Src/KMachine.c
+++ b/Src/KMachine.c
@@ -26,8 +26,22 @@
 //uint16_t FlashDatas[16];
 
 //uint32_t * pUID = (uint32_t *)(UID_BASE);
-
-const char VersionStr[] __attribute__((at(0X8001000)))
+const stKMInfoBlock KMInfoBlock =
+{
+	0x0008,
+	0x0100,
+	0x0100,
+	0x0100,
+	16,
+	16,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+};
+const char VersionStr[] __attribute__((at(FLASH_BASE + 0X1000))) //__attribute__((at(0X8001000)))
 	= "3.00";
 
 const stKMSysCfg KMStoreSysCfg /*__attribute__((at(STORECFGBASE)))*/ =
@@ -78,18 +92,18 @@
 
 const stKMSysCfg KMStoreSysCfg2[7] /*__attribute__((at(STORECFGBASE+sizeof(stKMSysCfg))))*/;
 	
-int ReadFlashMem(void * pBuf, void * pAddrFlash, int nSize)
+int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize)
 {
 //	memcpy(pBuf,pAddrFlash,nSize);
-	for (int i=0;i<nSize/4;i++)
+	for (int i=0;i<nByteSize/4;i++)
 	{
 		((uint32_t *)pBuf)[i] = ((uint32_t *)pAddrFlash)[i];
 	}
-	for (int i=nSize/4*2;i<nSize/2;i++)
+	for (int i=nByteSize/4*2;i<nByteSize/2;i++)
 	{
 		((uint16_t *)pBuf)[i] = ((uint16_t *)pAddrFlash)[i];
 	}
-	return nSize;
+	return nByteSize;
 }
 int EraseFlashMem(void * pAddrFlash, unsigned int Pages)
 {
@@ -104,7 +118,7 @@
 	res = HAL_FLASH_Lock();
 	return res;
 }
-int WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned int nSize)
+int WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize)
 {
 		HAL_StatusTypeDef res;
 	res = HAL_FLASH_Unlock();
@@ -115,12 +129,12 @@
 	}
 */	
 ///*	
-	for (int i=0;i<nSize/4;i++)
+	for (int i=0;i<nByteSize/4;i++)
 	{
 		res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)pAddrFlash + i*4, ((uint32_t *)pBuf)[i]);
 	}
 	
-	for (int i = nSize/4 * 2 ; i < nSize/2 ; i++)
+	for (int i = nByteSize/4 * 2 ; i < nByteSize/2 ; i++)
 	{
 		res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
 	}
@@ -129,7 +143,7 @@
 	
 	return res;
 }
-int EraseAndWriteToFlashMem(void * pBuf, void * pAddrFlash, unsigned int nSize)
+int EraseAndWriteToFlashMem(void * pBuf, void * pAddrFlash, unsigned int nByteSize)
 {
 	
 	HAL_StatusTypeDef res;
@@ -141,7 +155,7 @@
 	erase1.TypeErase=FLASH_TYPEERASE_PAGES;
 	res = HAL_FLASHEx_Erase(&erase1,&ErrNo);
 	
-	for (int i=0;i<nSize/2;i++)
+	for (int i=0;i<nByteSize/2;i++)
 	{
 		res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
 	}
@@ -159,6 +173,52 @@
 	res = HAL_FLASH_Lock();
 	
 	return res;
+}
+
+int ReadFactoryData(void * pDatabuf, int nByteCount)
+{
+	memcpy(pDatabuf,(stFactoryData *)FACTORY_DATA_BASE,nByteCount);
+	return 0;
+}
+int WriteFactoryData(void * pDataBuf, int nByteCount)
+{
+	EraseAndWriteToFlashMem(pDataBuf, (stFactoryData *)FACTORY_DATA_BASE,nByteCount);
+	return 0;
+}
+
+int ReadProgram(int nProgByteAddr, void *pBuf, int nByteSize, int nBank)
+{
+	if (nBank==0)	{
+		ReadFlashMem(pBuf, (void *)(STORE_PRG_BASE+nProgByteAddr), nByteSize);
+	}else if (nBank ==1) {
+		ReadFlashMem(pBuf, (void *)(ALT_PRG_BASE+nProgByteAddr), nByteSize);
+	}else if (KMRunStat.nBinProgBank==0) {
+		ReadFlashMem(pBuf, (void *)(STORE_PRG_BASE+nProgByteAddr), nByteSize);
+	} else {
+		ReadFlashMem(pBuf, (void *)(ALT_PRG_BASE+nProgByteAddr), nByteSize);
+	}		
+	return 0;
+}
+int WriteProgram(int nProgAddress, void * pBuf, int nByteSize, int nBank)
+{
+			// Program Save Address;//
+		  // Program 2 Save Address; //
+	void * progByteAddr;
+	if (nBank == 0) {
+		progByteAddr=(void *)(STORE_PRG_BASE+nProgAddress);
+	}else if (nBank==1) {
+		progByteAddr=(void *)(ALT_PRG_BASE+nProgAddress);
+	} else if (KMRunStat.nBinProgBank==0) {
+		progByteAddr=(void *)(ALT_PRG_BASE+nProgAddress);
+	}else{
+		progByteAddr=(void *)(STORE_PRG_BASE+nProgAddress);
+	}
+	if (nProgAddress ==0)	{
+		EraseAndWriteToFlashMem(pBuf, progByteAddr, nByteSize);
+	}else{
+		WriteToFlashMemNoErase(pBuf, progByteAddr, nByteSize);
+	}
+	return 0;
 }
 
 int LoadDefaultSysCfg(pKMSysCfg theKMSysCfg)
@@ -526,3 +586,264 @@
 	
 	return 0;
 }
+
+inline void SetAddrBit(unsigned short * pW, unsigned char bitAddr)
+{
+	(*pW)|=1<<(bitAddr&0xf);
+}
+
+inline void ResetBit(unsigned short * pW, unsigned char bitAddr)
+{
+	(*pW)&=~(1<<(bitAddr&0xf));
+}
+
+static inline void SetBitValue(unsigned short * pW, unsigned char bitAddr, unsigned char Value)
+{
+	if (Value)	{	SetAddrBit(pW, bitAddr);}
+	else {ResetBit(pW, bitAddr);}
+}
+
+static inline unsigned char GetBitValue(unsigned short W, unsigned char bitAddr)
+{
+	if (W&(1<<(bitAddr&0xf))) return 1;
+	else return 0;
+}
+
+
+unsigned char GetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr)
+{
+		unsigned char thisValue=0;
+		unsigned short nWordAddr=(nCoilAddr&0xff0)>>4;
+		unsigned char nBitAddr=nCoilAddr&0xf;
+		switch(nCoilType)
+		{
+		case KLCoilTypeX:
+			if (nCoilAddr >= KLCoilXCount) return 0;
+			thisValue = GetBitValue(KMem.WX[nWordAddr], nBitAddr);
+			break;
+		case KLCoilTypeY:
+			if (nCoilAddr >= KLCoilYCount) return 0;
+			thisValue = GetBitValue(KMem.WY[nWordAddr], nBitAddr);
+			break;
+		case KLCoilTypeR:
+			if (nCoilAddr >= KLCoilRCount) return 0;
+			thisValue = GetBitValue(KMem.WR[nWordAddr], nBitAddr);
+			break;
+		case KLCoilTypeLX:
+			if (nCoilAddr >= KLCoilLXCount) return 0;
+			 thisValue = GetBitValue(KMem.WLX[nWordAddr], nBitAddr);
+			break;
+		case KLCoilTypeLY:
+			if (nCoilAddr >= KLCoilLYCount) return 0;
+			thisValue = GetBitValue(KMem.WLY[nWordAddr], nBitAddr);
+			break;
+		case KLCoilTypeT:
+			if (nCoilAddr >= KLCoilTCount) return 0;
+			thisValue = GetBitValue(KMem.WT[nWordAddr], nBitAddr);
+			break;
+		case KLCoilTypeC:
+			if (nCoilAddr >= KLCoilCCount) return 0;
+			thisValue = GetBitValue(KMem.WC[nWordAddr], nBitAddr);
+			break;
+		case KLCoilTypeLR:
+			if (nCoilAddr >= KLCoilLRCount) return 0;
+			thisValue = GetBitValue(KMem.WLR[nWordAddr], nBitAddr); 
+			break;
+		case KLCoilTypeSR:
+			if (nCoilAddr >= KLCoilSRCount) return 0;
+			thisValue = GetBitValue(KMem.WSR[nWordAddr], nBitAddr);
+			break;
+			default:
+				break;
+		}	
+		return thisValue;
+}
+int SetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr, unsigned char nCoilValue)
+{
+		unsigned short nWordAddr=(nCoilAddr&0xff0)>>4;
+		unsigned char nBitAddr=nCoilAddr&0xf;
+		switch(nCoilType)
+		{
+		case KLCoilTypeX:
+			if (nCoilAddr >= KLCoilXCount) return 0;
+			SetBitValue(&KMem.WX[nWordAddr], nBitAddr, nCoilValue);
+			break;
+		case KLCoilTypeY:
+			if (nCoilAddr >= KLCoilYCount) return 0;
+			SetBitValue(&KMem.WY[nWordAddr], nBitAddr, nCoilValue);
+			break;
+		case KLCoilTypeR:
+			if (nCoilAddr >= KLCoilRCount) return 0;
+			SetBitValue(&KMem.WR[nWordAddr], nBitAddr, nCoilValue);
+			break;
+		case KLCoilTypeLX:
+			if (nCoilAddr >= KLCoilLXCount) return 0;
+			SetBitValue(&KMem.WLX[nWordAddr], nBitAddr, nCoilValue);
+			break;
+		case KLCoilTypeLY:
+			if (nCoilAddr >= KLCoilLYCount) return 0;
+			SetBitValue(&KMem.WLY[nWordAddr], nBitAddr, nCoilValue);
+			break;
+		case KLCoilTypeT:
+			if (nCoilAddr >= KLCoilTCount) return 0;
+			SetBitValue(&KMem.WT[nWordAddr], nBitAddr, nCoilValue);
+			break;
+		case KLCoilTypeC:
+			if (nCoilAddr >= KLCoilCCount) return 0;
+			SetBitValue(&KMem.WC[nWordAddr], nBitAddr, nCoilValue);
+			break;
+		case KLCoilTypeLR:
+			if (nCoilAddr >= KLCoilLRCount) return 0;
+			SetBitValue(&KMem.WLR[nWordAddr], nBitAddr, nCoilValue);
+			break;
+		case KLCoilTypeSR:
+			if (nCoilAddr >= KLCoilSRCount) return 0;
+			SetBitValue(&KMem.WSR[nWordAddr], nBitAddr, nCoilValue);
+			break;
+			default:
+				break;
+		}	
+		return 0;
+}
+
+int GetVarData(int nDataType, int nDataAddr)
+{
+	// TODO: ?????????.
+	int thisValue = 0;
+	
+	switch (nDataType)
+	{
+	case KLDataTypeDEC:
+	case KLDataTypeHEX:
+		thisValue = nDataAddr;
+		break;
+	case KLDataTypeWX:
+		if (nDataAddr >= KLDataWXCount) return 0;
+		thisValue = KMem.WX[nDataAddr];
+		break;
+	case KLDataTypeWY:
+		if (nDataAddr >= KLDataWYCount) return 0;
+		thisValue = KMem.WY[nDataAddr];
+		break;
+	case KLDataTypeWR:
+		if (nDataAddr >= KLDataWRCount) return 0;
+		thisValue = KMem.WR[nDataAddr];
+		break;
+	case KLDataTypeWLX:
+		if (nDataAddr >= KLDataWLCount) return 0;
+		thisValue = KMem.WLX[nDataAddr];
+		break;
+	case KLDataTypeWLY:
+		if (nDataAddr >= KLDataWLCount) return 0;
+		thisValue = KMem.WLY[nDataAddr];
+		break;
+	case KLDataTypeDT:
+		if (nDataAddr >= KLDataDTCount) return 0;
+		thisValue = (signed short)KMem.DT[nDataAddr];
+		break;
+	case KLDataTypeSDT:
+		if (nDataAddr >= KLDataSDTCount) return 0;
+		thisValue = KMem.SDT[nDataAddr];
+		break;
+	case KLDataTypeWSR:
+		if (nDataAddr >= KLCoilLRCount) return 0;
+		thisValue = KMem.WSR[nDataAddr];
+		break;
+	case KLDataTypeSV:
+		if (nDataAddr >= KLDataSVCount) return 0;
+		thisValue = KMem.SV[nDataAddr];
+		break;
+	case KLDataTypeEV:
+		if (nDataAddr >= KLDataEVCount) return 0;
+		thisValue = KMem.EV[nDataAddr];
+		break;
+	case KLDataTypeLD:
+		if (nDataAddr >= KLDataLDCount) return 0;
+		thisValue = KMem.DT[nDataAddr];
+		break;
+	case KLDataSysCfg:
+		if (nDataAddr >= KLCoilSRCount) return 0;
+		thisValue = KMem.SDT[nDataAddr];
+		break;
+	case KLDataTypeFlash:
+		if (nDataAddr >= KLCoilSRCount) return 0;
+		thisValue = KMem.SDT[nDataAddr];
+		break;
+	case KLDataTypeTest:
+		if (nDataAddr >= KLCoilSRCount) return 0;
+		thisValue = KMem.SDT[nDataAddr];
+		break;
+	}
+	return thisValue;
+}
+
+
+int SetVarData(int nDataType, int nDataAddr, int nDataValue)
+{
+	// TODO: ?????????.
+	switch (nDataType)
+	{
+//	case KLDataTypeDEC:
+//	case KLDataTypeHEX:
+//		break;
+	case KLDataTypeWX:
+		if (nDataAddr >= KLDataWXCount) return 0;
+		KMem.WX[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeWY:
+		if (nDataAddr >= KLDataWYCount) return 0;
+		KMem.WY[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeWR:
+		if (nDataAddr >= KLDataWRCount) return 0;
+		KMem.WR[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeWLX:
+		if (nDataAddr >= KLDataWLCount) return 0;
+		KMem.WLX[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeWLY:
+		if (nDataAddr >= KLDataWLCount) return 0;
+		KMem.WLY[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeDT:
+		if (nDataAddr >= KLDataDTCount) return 0;
+		KMem.DT[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeSDT:
+		if (nDataAddr >= KLDataSDTCount) return 0;
+		KMem.SDT[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeWSR:
+		if (nDataAddr >= KLCoilLRCount) return 0;
+		KMem.WSR[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeSV:
+		if (nDataAddr >= KLDataSVCount) return 0;
+		KMem.SV[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeEV:
+		if (nDataAddr >= KLDataEVCount) return 0;
+		KMem.EV[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeLD:
+		if (nDataAddr >= KLDataLDCount) return 0;
+		KMem.DT[nDataAddr] = nDataValue;
+		break;
+	case KLDataSysCfg:
+		if (nDataAddr >= KLCoilSRCount) return 0;
+		KMem.SDT[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeFlash:
+		if (nDataAddr >= KLCoilSRCount) return 0;
+		KMem.SDT[nDataAddr] = nDataValue;
+		break;
+	case KLDataTypeTest:
+		if (nDataAddr >= KLCoilSRCount) return 0;
+		KMem.SDT[nDataAddr] = nDataValue;
+		break;
+	}
+
+	return 0;
+}
+

--
Gitblit v1.9.1