From 7a2ff321965f1f24f3047fedd44a83f451f1f793 Mon Sep 17 00:00:00 2001 From: QuakeGod <QuakeGod@sina.com> Date: 星期三, 01 二月 2023 10:58:10 +0800 Subject: [PATCH] radio add new device type --- Src/KMachine.c | 407 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 376 insertions(+), 31 deletions(-) diff --git a/Src/KMachine.c b/Src/KMachine.c index 02aa814..5a88bf6 100644 --- a/Src/KMachine.c +++ b/Src/KMachine.c @@ -15,7 +15,7 @@ //#define UID_BASE ((uint32_t)0x1FFFF7ACU) /*!< Unique device ID register base address */ -stKMSysCfg KMSysCfg ; +stStoredKMSysCfg storedKMSysCfg ; stKMem KMem; stRunStat KMRunStat; @@ -26,33 +26,67 @@ //uint16_t FlashDatas[16]; //uint32_t * pUID = (uint32_t *)(UID_BASE); - -const char VersionStr[] __attribute__((at(0X8001000))) +const stKMInfoBlock KMInfoBlock = +{ + (BOARD_TYPE<<8) + BOARD_VER, //nDeviceType BOARD_VER, //nDevieVer + 0x0100, //ProgVer + 0x0100, //KLinkVer + 0x0100, //KBusVer +// 0x0100, //KNetVer +// 0x0100, //KWLVer + + 4, //nCapacity1 ?K + 1, //nCapacity2 ?k + + DINPUT, //nDInput; + DOUTPUT, //nDOutput + + 0, //nAInput + 0, //nAOutput + 0, //nHInput + 0, //nHOutput + 0, //nExt1; + 0, //nExt2; + 0, //nLogSize; + 0, //nPorts; + 0, //nManSize; + 0, //nAbility; + 6, //nSwitchBits; +}; +const char VersionStr[] __attribute__((at(FLASH_BASE + 0X1000))) //__attribute__((at(0X8001000))) = "3.00"; -const stKMSysCfg KMStoreSysCfg /*__attribute__((at(STORECFGBASE)))*/ = +const stStoredKMSysCfg KMDefaultSysCfg /*__attribute__((at(STORECFGBASE)))*/ = { - 0x55aa, + START_SIGN, 0x0000, - 0x00000000, + + CFG_VER, + 0x0000, + 0x0000, + {0,0,0,0,0,0}, { { + 1, + 0, + 2304, //Buadrate * 100; PortType_KLink, //PorttType 1, //ByteSize 0, //Parity 0, //StopBits 0, //EofChar 0, //SofChar - 2304, //Buadrate * 100; }, { + 1, + 0, + 2304, //Buadrate * 100; PortType_KBus, //PorttType 1, //ByteSize 0, //Parity 0, //StopBits 0, //EofChar 0, //SofChar - 2304, //Buadrate * 100; } }, {{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}, @@ -73,23 +107,23 @@ 0x0010, {0}, 0x0011, - 0x5aa5, + END_SIGN, }; -const stKMSysCfg KMStoreSysCfg2[7] /*__attribute__((at(STORECFGBASE+sizeof(stKMSysCfg))))*/; +const stKMSysCfg KMDefaultSysCfg2[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 +138,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 +149,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 +163,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 +175,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]); } @@ -161,20 +195,66 @@ return res; } -int LoadDefaultSysCfg(pKMSysCfg theKMSysCfg) +int ReadFactoryData(void * pDatabuf, int nByteCount) { - memcpy(theKMSysCfg,&KMStoreSysCfg,sizeof(stKMSysCfg)); + memcpy(pDatabuf,(stFactoryData *)FACTORY_DATA_BASE,nByteCount); return 0; } -int ReadSysCfgFromFlash(pKMSysCfg theKMSysCfg) +int WriteFactoryData(void * pDataBuf, int nByteCount) { - pKMSysCfg pStoreKMSysCfg = (pKMSysCfg)(STORE_SYSREG_BASE); + 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(pStoredKMSysCfg theStoredKMSysCfg) +{ + memcpy(theStoredKMSysCfg,&KMDefaultSysCfg,sizeof(stKMSysCfg)); + return 0; +} +int ReadSysCfgFromFlash(pStoredKMSysCfg theStoredKMSysCfg) +{ + pStoredKMSysCfg pStoreKMSysCfg = (pStoredKMSysCfg)(STORE_SYSREG_BASE); // find latest Store Cfg int nIndex=-1; int nMaxSeq=-1; for (int i=0;i<8;i++) { - if (pStoreKMSysCfg->Sign1 == 0x55aa && pStoreKMSysCfg->EndSign1 == 0x5aa5) + if (pStoreKMSysCfg->Sign1 == START_SIGN && pStoreKMSysCfg->EndSign1 == END_SIGN) { if (pStoreKMSysCfg->Seq1 > nMaxSeq) { @@ -184,18 +264,18 @@ } if (nIndex>=0 && nIndex <8) { - ReadFlashMem(theKMSysCfg,(void *)(pStoreKMSysCfg+nIndex),sizeof(stKMSysCfg)); + ReadFlashMem(theStoredKMSysCfg,(void *)(&pStoreKMSysCfg[nIndex]),sizeof(stStoredKMSysCfg)); }else { - LoadDefaultSysCfg(theKMSysCfg); + LoadDefaultSysCfg(theStoredKMSysCfg); } //memcpy(theKMSysCfg,(void* )STORECFGBASE,sizeof(KMSysCfg)); return 0; } -int WriteSysCfgToFlash(pKMSysCfg theKMSysCfg) +int WriteSysCfgToFlash(pStoredKMSysCfg theStoredKMSysCfg) { - theKMSysCfg->Seq1++; - theKMSysCfg->cfgvar16++; + theStoredKMSysCfg->Seq1++; +// theKMSysCfg->cfgvar16++; // find the next empty space to write int nIndex=-1; int s2=128; @@ -212,10 +292,10 @@ break; } if (nIndex >=0 && nIndex <8) { - WriteToFlashMemNoErase(theKMSysCfg,(void *)(STORE_SYSREG_BASE + nIndex*s2),sizeof(KMSysCfg)); + WriteToFlashMemNoErase(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE + nIndex*s2),sizeof(theStoredKMSysCfg)); } else { - EraseAndWriteToFlashMem(theKMSysCfg,(void *)STORE_SYSREG_BASE,sizeof(KMSysCfg)); + EraseAndWriteToFlashMem(theStoredKMSysCfg,(void *)STORE_SYSREG_BASE,sizeof(theStoredKMSysCfg)); } return 0; } @@ -412,6 +492,10 @@ unsigned int nEventMaxSeq=0; int nEventNextSpace; int nMaxCurTime=0; +volatile int PowerDownEvent=0; +volatile int OldPowerDownEvent=0; +volatile int OldPowerDownEventTime=0; + int CheckEventLog() { unsigned int nMinEventSeq=999999999; @@ -526,3 +610,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