/** ****************************************************************************** * @file : KMachine.c * @brief : KMachine program body ****************************************************************************** */ #include "KMachine.h" #include "KBus.h" #include "string.h" #include "Globaldef.h" #include "stm32f0xx.h" #include "stm32f0xx_ll_flash.h" #include "PLCFunctions.h" //#include "stm32f0xx_hal.h" //#define OB_BASE ((uint32_t)0x1FFFF800U) /*!< FLASH Option Bytes base address */ //#define FLASHSIZE_BASE ((uint32_t)0x1FFFF7CCU) /*!< FLASH Size register base address */ //#define UID_BASE ((uint32_t)0x1FFFF7ACU) /*!< Unique device ID register base address */ //#define FLASH_BASE //#define FLASH_PAGE_SIZE 0x00000400U //#define FLASH_BANK1_END ((uint32_t)0x0800FFFFU) /*!< FLASH END address of bank1 */ #define ApplicationAddress 0x08001000 //Ó¦ÓóÌÐòÊ×µØÖ·¶¨Òå #if defined(STM32F030x8) #define NEW_APP_INFOBLOCK_ADDR 0x08008000 // ´æ´¢µÄÐÂÓ¦ÓóÌÐòÐÅÏ¢¿éµÄµØÖ· #define NEW_APP_ADDR ((uint32_t)0x08009000U) // ´æ´¢µÄÐÂÓ¦ÓóÌÐòµÄµØÖ· #endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F051x8 || STM32F042x6 || STM32F048xx || STM32F058xx || STM32F070x6 */ #if defined(STM32F030xC) #define NEW_APP_INFOBLOCK_ADDR 0x08020000 // ´æ´¢µÄÐÂÓ¦ÓóÌÐòÐÅÏ¢¿éµÄµØÖ· #define NEW_APP_ADDR ((uint32_t)0x08021000U) // ´æ´¢µÄÐÂÓ¦ÓóÌÐòµÄµØÖ· #endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F030xC */ stStoredKMSysCfg storedKMSysCfg ; stKMem KMem; stRunStat KMRunStat; extern void SetErrLed(uchar bOn); //uint8_t * pFlash1 = (uint8_t *)(STORECFGBASE); //void * pConfigFlashBase = (uint8_t *)(STORECFGBASE); //uint16_t FlashDatas[16]; //uint32_t * pUID = (uint32_t *)(UID_BASE); /* const char VersionStr[] __attribute__((at(FLASH_BASE + 0X2000))) //__attribute__((at(0X8001000))) = "3.00"; */ const stStoredKMSysCfg KMDefaultSysCfg /*__attribute__((at(STORECFGBASE)))*/ = { START_SIGN, 0x0000, { CFG_VER, 0x0000, //workmode 0x0000, //switchfunc 4, //nCfgBlockCount; { //comportparam[2] { PortType_KLink, //PorttType 1, //Station 1152, //Buadrate = * 100; 1, //ByteSize 0, //Parity 0, //StopBits 1, //endType 0, //EofChar 0, //SofChar 9, //endtime 0, //recvbuf 0, //bufsize }, { PortType_KBus, //PorttType 0, //Station 2304, //Buadrate = * 100; 1, //ByteSize 0, //Parity 0, //StopBits 1, //endType 0, //EofChar 0, //SofChar 1, //endtime 0, //recvbuf 0, //bufsize } }, {{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}, //inputfilterparam {{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1}}, //outputholdparam { //default port mapping 0x0010, 0x0020, 0x0030, 0x0040, 0x0050, 0x0060, 0x0070, 0x0080, }, 0,0,0,0, { {0,sizeof(stKMSysCfg)}, {1,100}, {2,100}, {3,32}, {4,32}, }, 0x0008, //padding s 0x0009, 0x000a, }, 0x0011, //CRC16 END_SIGN, }; int nEventCount=0; int nEventMinIndex; int nEventMaxIndex; unsigned int nEventMaxSeq=0; int nEventNextSpace; int nMaxCurTime=0; volatile int PowerState = 0; volatile int PowerDownEvent=0; volatile int OldPowerDownEvent=0; volatile int OldPowerDownEventTime=0; int nMaxRunStatIndex=-1; unsigned int nMaxRunStatSeq=0; int nNextRunStatSpace=0; int KMRegisterPort(ushort nType,stPortDef * theParam) { int curPortId = KMem.nTotalPorts; KMem.pPorts[curPortId] = theParam; KMem.nTotalPorts++; return curPortId; } int KMPortReqFunc(int nPortIndex,int nReqId, int nParam1, int nParam2, void ** pData, unsigned short * nlen1) { if (KMem.pPorts[nPortIndex]->ReqCommFunc) return KMem.pPorts[nPortIndex]->ReqCommFunc(KMem.pPorts[nPortIndex]->pInstance, nReqId, nParam1, nParam2, pData, nlen1); else return -1; } int KMRunService(int nSvrId, int nParam1, int nParam2, void **pData, unsigned short *nlen1) { int res; switch(nSvrId) { case ReqNone: break; case ReqInit: break; case ReqReset: __set_PRIMASK(1); //¹Ø±ÕÈ«¾ÖÖÐ¶Ï NVIC_SystemReset(); break; case ReqStop: break; case ReqRun: break; case ReqBlinkLED: break; case ReqStartDiag: break; case ReqStopDiag: break; case ReqUpdateFirm: res = WriteNewApp(nParam1,*pData,*nlen1); break; case ReqUpdateFirmInfo: res = WriteNewAppInfo(nParam1,*pData,*nlen1); break; default: res = -1; break; } return res; } int KMachineInit(void) { // ClearEventLog(); KMem.LastScanTime=0; KMem.ScanTimeuS=0; KMem.MinScanTimeuS=99999; KMem.MaxScanTimeuS=0; // KMem.SDD[14]=(unsigned int)&KMStoreSysCfg; // KMem.SDD[15]=(unsigned int)&KMStoreSysCfg1; KMem.SDD[12]=((uint32_t *)UID_BASE)[0]; // KMem.SDD[13]=((uint32_t *)UID_BASE)[1]; // KMem.SDD[14]=((uint32_t *)UID_BASE)[2]; KMem.SDD[13]=PendSvCount; KMem.SDD[14]=RCC->CSR; // KMem.SDD[15]=*(uint32_t *)FLASHSIZE_BASE; // KMem.SDD[16]=(unsigned int)&KMSysCfg; KMem.nTotalPorts = 0; CheckEventLog(); LoadRunStat(&KMRunStat); KMem.CurTimeSec=nMaxCurTime; KMem.TotalRunTime=KMRunStat.UpTime; KMRunStat.PowerCount++; KMem.PwrOnCount=KMRunStat.PowerCount; SaveRunStat(&KMRunStat); KMem.SDD[15]=nMaxRunStatIndex; KMem.SDD[16]=nMaxRunStatSeq; KMem.SDD[17]=nNextRunStatSpace; AddEventLog(KMem.CurTimeSec,EventTypePowerUp,1,12345); KMem.SDD[19]=nEventCount; KMem.SDD[20]=nEventMinIndex; KMem.SDD[21]=nEventMaxIndex; KMem.SDD[22]=nEventMaxSeq; KMem.SDD[23]=nEventNextSpace; return 0; } int KMachineLoopProc(void) { return 0; } //const stKMSysCfg KMDefaultSysCfg2[7] /*__attribute__((at(STORECFGBASE+sizeof(stKMSysCfg))))*/; int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize) { memcpy(pBuf,pAddrFlash,nByteSize); // for (int i=0;i 1){ // ¿çÒ³´æ´¢ // ÏÈÇ°ÃæµÄ²¿·Ö int i; for (i=0;i<(nByteSize+1)/2 && (((int)pAddrFlash + i*2) &(FLASH_PAGE_SIZE -1))!=0 ;i++) { unsigned short value = ((uint8_t *)pBuf)[i*2] + (((uint8_t *)pBuf)[i*2 +1] << 8); res = LL_FLASH_Program(ProgaraType_DATA16, (uint32_t)pAddrFlash + i*2, value); if (res == ERROR) break; } // ²Á³ýºóÃæµÄ²¿·Ö. res = LL_Flash_PageErase((void *)((int)pAddrFlash + i*2),NbPages - 1); // ¼ÌÐø´æ´¢ for ( ;i<(nByteSize+1)/2;i++) { unsigned short value = ((uint8_t *)pBuf)[i*2] + (((uint8_t *)pBuf)[i*2 +1] << 8); res = LL_FLASH_Program(ProgaraType_DATA16, (uint32_t)pAddrFlash + i*2, value); if (res == ERROR) break; } }else { // Õý³£Ð´Èë,²»ÐèÒª²Á³ý for (int i=0;i<(nByteSize+1)/2;i++) { unsigned short value = ((uint8_t *)pBuf)[i*2] + (((uint8_t *)pBuf)[i*2 +1] << 8); res = LL_FLASH_Program(ProgaraType_DATA16, (uint32_t)pAddrFlash + i*2, value); if (res == ERROR) break; } } // __enable_irq(); LL_FLASH_Lock(FLASH); if (res == ERROR) return 1; return 0; } int ReadFactoryData(void * pDatabuf, int nByteCount) { memcpy(pDatabuf,(stFactoryData *)FACTORY_DATA_BASE,nByteCount); return 0; } int WriteFactoryData(void * pDataBuf, int nByteCount) { stFactoryData * p1 = (stFactoryData*) pDataBuf; stFactoryData * p2 = (stFactoryData *)FACTORY_DATA_BASE; p1->Seq1= p2->Seq1+1; EraseAndWriteToFlashMem(pDataBuf, (stFactoryData *)FACTORY_DATA_BASE,nByteCount); return 0; } #if (ENABLE_PLC) int ReadPLCProgram(int nBank, int nProgByteAddr, void *pBuf, int nByteSize) { stStoredBinProgs * pStoredProg; if (nBank==0) { pStoredProg = (void *)(STORE_PRG_BASE); }else if (nBank ==1) { pStoredProg = (void *)(ALT_PRG_BASE); }else if (storedKMSysCfg.theKMSysCfg.nProgBank==0) { pStoredProg = (void *)(STORE_PRG_BASE); } else { pStoredProg = (void *)(ALT_PRG_BASE); } void * progByteAddr; progByteAddr =(unsigned char *)&pStoredProg->BinInstrcns + nProgByteAddr; ReadFlashMem(pBuf, (void *)(progByteAddr), nByteSize); return 0; } int StartPLCProgram(int nBank, int nByteSize, int nCRC) { void * progHdrAddr; int nRes = 0; if (nBank == 0) { progHdrAddr=(void *)(STORE_PRG_BASE); }else if (nBank==1) { progHdrAddr=(void *)(ALT_PRG_BASE); } else if (storedKMSysCfg.theKMSysCfg.nProgBank==0) { progHdrAddr=(void *)(ALT_PRG_BASE); }else{ progHdrAddr=(void *)(STORE_PRG_BASE); } stStoredHdr theHdr; theHdr.nBlockSign = 0xAA55; theHdr.nBlockType = 0; theHdr.nSeq = 1; theHdr.nSize = nByteSize; theHdr.nCRC2 = nCRC; WriteToFlashAutoErase(&theHdr,(void *)progHdrAddr,sizeof(stStoredHdr)); return nRes; } int WritePLCProgram(int nBank, int nProgAddress, void * pBuf, int nByteSize) { // Program Save Address;// // Program 2 Save Address; // stStoredBinProgs * pStoredProg; if (nBank == 0) { pStoredProg=(stStoredBinProgs *)(STORE_PRG_BASE); }else if (nBank==1) { pStoredProg=(stStoredBinProgs *)(ALT_PRG_BASE); } else if (storedKMSysCfg.theKMSysCfg.nProgBank==0) { pStoredProg=(stStoredBinProgs *)(ALT_PRG_BASE); }else{ pStoredProg=(stStoredBinProgs *)(STORE_PRG_BASE); } void * progByteAddr; progByteAddr =(unsigned char *)&pStoredProg->BinInstrcns + nProgAddress; WriteToFlashAutoErase(pBuf,progByteAddr,nByteSize); return 0; } int FinishiPLCProgram(int nBank, int nProgSteps,int nCRC ) { int nRes = 0; if (storedKMSysCfg.theKMSysCfg.nProgBank == 0 ) { storedKMSysCfg.theKMSysCfg.nProgBank = 1; }else { storedKMSysCfg.theKMSysCfg.nProgBank = 0; } storedKMSysCfg.theKMSysCfg.nProgSize = nProgSteps; WriteSysCfgToFlash(&storedKMSysCfg); KMRunStat.nBinProgSize=nProgSteps; KMRunStat.nBinProgBank=storedKMSysCfg.theKMSysCfg.nProgBank; SaveRunStat(&KMRunStat); return nRes; } int ReadPLCAnno(int nBank, int nProgByteAddr, void *pBuf, int nByteSize) { stStoredBinProgs * pStoredProg; if (nBank==0) { pStoredProg = (void *)(STORE_PRG_BASE); }else if (nBank ==1) { pStoredProg = (void *)(ALT_PRG_BASE); }else if (storedKMSysCfg.theKMSysCfg.nProgBank==0) { pStoredProg = (void *)(STORE_PRG_BASE); } else { pStoredProg = (void *)(ALT_PRG_BASE); } void * progByteAddr; progByteAddr =(unsigned char *)&pStoredProg->BinInstrcns + nProgByteAddr; ReadFlashMem(pBuf, (void *)(progByteAddr), nByteSize); return 0; } int StartPLCAnno(int nBank, int nByteSize, int nCRC) { void * StoredAnnoHdrAddr; int nRes = 0; StoredAnnoHdrAddr=(void *)(STORE_PLC_ANNO_BASE); stStoredHdr theHdr; theHdr.nBlockSign = 0xAA55; theHdr.nBlockType = 3; theHdr.nSeq = 1; theHdr.nSize = nByteSize; theHdr.nCRC2 = nCRC; WriteToFlashAutoErase(&theHdr,(void *)StoredAnnoHdrAddr,sizeof(stStoredHdr)); return nRes; } int WritePLCAnno(int nBank, int nByteAddress, void * pBuf, int nByteSize) { // Program Save Address;// // Program 2 Save Address; // stStoredAnno * pStoredAnno; pStoredAnno=(stStoredAnno *)(STORE_PLC_ANNO_BASE); void * nByteAddr; nByteAddr =pStoredAnno->Annos + nByteAddress; WriteToFlashAutoErase(pBuf,nByteAddr,nByteSize); return 0; } int FinishiPLCAnno(int nBank, int nByteSize,int nCRC ) { int nRes = 0; storedKMSysCfg.theKMSysCfg.nAnnoSize = nByteSize; WriteSysCfgToFlash(&storedKMSysCfg); /* KMRunStat.nBinProgSize=nProgSteps; KMRunStat.nBinProgBank=storedKMSysCfg.theKMSysCfg.nProgBank; SaveRunStat(&KMRunStat); */ return nRes; } #endif //ENABLE_PLC int LoadDefaultSysCfg(pStoredKMSysCfg theStoredKMSysCfg) { memcpy(theStoredKMSysCfg,&KMDefaultSysCfg,sizeof(stStoredKMSysCfg)); return 0; } int ReadSysCfgFromFlash(pStoredKMSysCfg theStoredKMSysCfg) { pStoredKMSysCfg pSKMSysCfg = (pStoredKMSysCfg)(STORE_SYSREG_BASE); // find latest Store Cfg int s2=128; int nIndex=-1; int nMaxSeq=0; for (int i=0;i<8;i++) { pSKMSysCfg = (pStoredKMSysCfg)(STORE_SYSREG_BASE + s2*i); if (pSKMSysCfg->Sign1 == START_SIGN && pSKMSysCfg->EndSign1 == END_SIGN) { if (pSKMSysCfg->Seq1 > nMaxSeq) { nIndex=i;nMaxSeq=pSKMSysCfg->Seq1; } } } if (nIndex>=0 && nIndex <8) { ReadFlashMem(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE + nIndex * s2),sizeof(stStoredKMSysCfg)); //ReadFlashMem(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE ),sizeof(stStoredKMSysCfg)); //memcpy(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE ),sizeof(stStoredKMSysCfg)); }else { LoadDefaultSysCfg(theStoredKMSysCfg); } //memcpy(theKMSysCfg,(void* )STORECFGBASE,sizeof(KMSysCfg)); return 0; } int WriteSysCfgToFlash(pStoredKMSysCfg theStoredKMSysCfg) { theStoredKMSysCfg->Seq1++; theStoredKMSysCfg->Sign1 = START_SIGN; theStoredKMSysCfg->EndSign1 = END_SIGN; // EraseAndWriteToFlashMem(theStoredKMSysCfg,(void *)STORE_SYSREG_BASE,sizeof(stStoredKMSysCfg)); // return 0; // theKMSysCfg->cfgvar16++; // find the next empty space to write int nIndex=-1; int s2=128; for (int i=0;i<8;i++) { int skip=0; unsigned char * nAddr2=(unsigned char *)(STORE_SYSREG_BASE + i * s2); for (int j=0;j=0 && nIndex <8) { WriteToFlashMemNoErase(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE + nIndex*s2),sizeof(stStoredKMSysCfg)); } else { EraseAndWriteToFlashMem(theStoredKMSysCfg,(void *)STORE_SYSREG_BASE,sizeof(stStoredKMSysCfg)); } return 0; } int is_pow_of_2(uint32_t x) { return !(x & (x-1)); } uint32_t next_pow_of_2(uint32_t x) { if ( is_pow_of_2(x) ) return x; x |= x>>1; x |= x>>2; x |= x>>4; x |= x>>8; x |= x>>16; return x+1; } //uint8_t * pFlash1; /* stStoreCfg * GetCurStoreCfgAddr(void ) { int s = sizeof(stStoreCfg); int s2=next_pow_of_2(s); stStoreCfg * p1; int nMaxSN=0; int nMaxId=0; for (int i=0; s2*i < STORECFGPAGESIZE ; i++) { p1= (stStoreCfg *)(STORECFGBASE + s2 * i ); if (p1->Sign1 != START_SIGN) continue; if (p1->EndSign1 != END_SIGN) continue; if (p1->Seq1 >= nMaxSN) {nMaxSN = p1->Seq1; nMaxId = i;} } // nMaxId=nMaxId+1; return (stStoreCfg *)(STORECFGBASE + s2 * nMaxId); } stStoreCfg * GetNextStoreCfgAddr(stStoreCfg * CurCfg ) { int s = sizeof(stStoreCfg); int s2=next_pow_of_2(s); uint32_t nAddr1 = (uint32_t) CurCfg; uint32_t nAddr2 = nAddr1 + s2; for (int i=1;i<33;i++) { int skip=0; nAddr2 = nAddr1 + s2*i; if ((nAddr2 + s) > STORECFGBASE + STORECFGPAGESIZE) { nAddr2=STORECFGBASE; break; } for (int j=0;jPowerCount=1; // theRunStat->UpTime=0; // theRunStat->UserData1=0; // theRunStat->WorkMode=0; // theRunStat->WorkMode2=0; // theRunStat->nBinProgBank=0; // theRunStat->nBinInstrcnSize=0; return 0; } int LoadRunStat(pRunStat theRunStat) { uchar * pRunStatStore = (uchar *)STORE_RUNSTAT_BASE; pRunStat pStoreRunStats = (pRunStat)pRunStatStore; // int s = sizeof(stRunStat); for (int i=0;i * sizeof(stRunStat) < (FLASH_PAGE_SIZE * STORE_RUNSTAT_PAGES) ;i++) { if (pStoreRunStats[i].Sign1 == START_SIGN ) { if (pStoreRunStats[i].Seq1 > nMaxRunStatSeq) { nMaxRunStatSeq = pStoreRunStats[i].Seq1; nMaxRunStatIndex=i; nNextRunStatSpace=i+1; } } } if (nMaxRunStatIndex>=0) // && nMaxRunStatIndex <8) { ReadFlashMem(theRunStat,(void *)(pStoreRunStats+nMaxRunStatIndex),sizeof(stRunStat)); }else { LoadDefaultRunStat(theRunStat); } // find Next Space // if Same Page with MaxSeq Index, then not erase, skip and skip. // if next Page of MaxSeq Index, then earse if not empty; if ((nNextRunStatSpace + 1) * sizeof(stRunStat) > FLASH_PAGE_SIZE * STORE_RUNSTAT_PAGES) { nNextRunStatSpace=0; } return 0; } int SaveRunStat(pRunStat theRunStat) { nMaxRunStatSeq++; theRunStat->Sign1=START_SIGN; theRunStat->Seq1 = nMaxRunStatSeq; theRunStat->PowerCount=KMem.PwrOnCount; theRunStat->UpTime=KMem.TotalRunTime; theRunStat->CRC1=0x11; theRunStat->EndSign1=END_SIGN; //check empty unsigned char *pFlash = (unsigned char *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat)); int Skip=0; for (int j=0;j FLASH_PAGE_SIZE * STORE_RUNSTAT_PAGES) { nNextRunStatSpace=0; } return 0; } int WriteNewApp(int nProgByteOffset, void *pBuf, int nByteSize) { int res = -1; int FlashSize = *(ushort *)FLASHSIZE_BASE; int NewAppAddress ; if (FlashSize == 64) { NewAppAddress = 0x08009000U; }else if (FlashSize == 256) { NewAppAddress = 0x08021000U; } if ((nProgByteOffset&(FLASH_PAGE_SIZE-1)) ==0){ // EraseFlashMem((void *)(NewAppAddress + nProgByteAddr),1); res = EraseAndWriteToFlashMem(pBuf,(void *)(NewAppAddress + nProgByteOffset),nByteSize); }else { // if (nByteSize>64) return 0; res = WriteToFlashMemNoErase(pBuf,(void *)(NewAppAddress + nProgByteOffset),nByteSize); } return res; } int WriteNewAppInfo(int nProgByteAddr, void *pBuf, int nByteSize) { int FlashSize = *(ushort *)FLASHSIZE_BASE; int NewAppInfoBlockAddress ; if (FlashSize == 64) { NewAppInfoBlockAddress = 0x08008000; }else if (FlashSize == 256) { NewAppInfoBlockAddress = 0x08020000; } int res = EraseAndWriteToFlashMem(pBuf,(void *)(NewAppInfoBlockAddress + nProgByteAddr),nByteSize); return res; } int CheckEventLog() { unsigned int nMinEventSeq=999999999; uchar * pEventStore = (uchar *)STORE_LOG_BASE; pEventLog theEventLog = (pEventLog) pEventStore; // int s = sizeof(stEventLog); nEventCount=0; for (int i=0;i * sizeof(stEventLog) < (FLASH_PAGE_SIZE * STORE_LOG_PAGES) ;i++) { if (theEventLog[i].Sign1 == START_SIGN ) { nEventCount++; if (theEventLog[i].Seq1 > nEventMaxSeq) { nEventMaxSeq = theEventLog[i].Seq1; nEventMaxIndex=i; nMaxCurTime=theEventLog[i].nTime; nEventNextSpace=i+1; } if (theEventLog[i].Seq1 < nMinEventSeq) { nMinEventSeq = theEventLog[i].Seq1; nEventMinIndex = i; } } } // find Next Space // if Same Page with MaxSeq Index, then not erase, skip and skip. // if next Page of MaxSeq Index, then earse if not empty; if ((nEventNextSpace + 1) * sizeof(stEventLog) > FLASH_PAGE_SIZE * STORE_LOG_PAGES) { nEventNextSpace=0; } return nEventCount; } int AddEventLog(uint32_t nTime, USHORT nEvent, USHORT nParam1, UINT nParam2) { nEventMaxSeq++; stEventLog thisEventLog={START_SIGN, nEventMaxSeq, nTime,nEvent,nParam1,nParam2}; //check empty unsigned char *pFlash = (unsigned char *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog)); int Skip=0; for (int j=0;j FLASH_PAGE_SIZE * STORE_LOG_PAGES) { nEventNextSpace=0; } nEventCount++; KMem.nEventCount=nEventCount; return 0; } pEventLog GetEventLogAddr(int nIndex) { int nEventIndex=nEventMinIndex + nIndex; if (nEventIndex * sizeof(stEventLog) >= (FLASH_PAGE_SIZE * STORE_LOG_PAGES)) { nEventIndex -= (FLASH_PAGE_SIZE * STORE_LOG_PAGES)/sizeof(stEventLog); } unsigned char *pFlash = (unsigned char *)(STORE_LOG_BASE + nEventIndex*sizeof(stEventLog)); return (pEventLog)pFlash; } int ClearEventLog(void) { EraseFlashMem((void *)STORE_LOG_BASE,STORE_LOG_PAGES); nEventMinIndex=0; nEventMaxIndex=0; nEventMaxSeq=0; nEventCount=0; nEventNextSpace=0; return 0; } inline void SetAddrBit(unsigned short * pW, unsigned char bitPos) { (*pW)|=1<<(bitPos&0xf); } inline void ResetBit(unsigned short * pW, unsigned char bitPos) { (*pW)&=~(1<<(bitPos&0xf)); } static inline void SetBitValue(unsigned short * pW, unsigned char bitPos, unsigned char Value) { if (Value) { SetAddrBit(pW, bitPos);} else {ResetBit(pW, bitPos);} } static inline unsigned char GetBitValue(unsigned short W, unsigned char bitPos) { if (W&(1<<(bitPos&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 nBitPos=nCoilAddr&0xf; switch(nCoilType) { case KLCoilTypeX: if (nCoilAddr >= KLCoilXCount) return 0; thisValue = GetBitValue(KMem.WX[nWordAddr], nBitPos); break; case KLCoilTypeY: if (nCoilAddr >= KLCoilYCount) return 0; thisValue = GetBitValue(KMem.WY[nWordAddr], nBitPos); break; case KLCoilTypeR: if (nCoilAddr < KLCoilRCount) { thisValue = GetBitValue(KMem.WR[nWordAddr], nBitPos); }else if (nCoilAddr > 9000) { if (nCoilAddr == 9010) thisValue = 1; if (nCoilAddr == 9011) thisValue = 0; if (nCoilAddr == 9013) thisValue = GetBitValue(KMem.WSR[nWordAddr], 13); } // return thisValue; break; case KLCoilTypeLX: if (nCoilAddr >= KLCoilLXCount) return 0; thisValue = GetBitValue(KMem.WLX[nWordAddr], nBitPos); break; case KLCoilTypeLY: if (nCoilAddr >= KLCoilLYCount) return 0; thisValue = GetBitValue(KMem.WLY[nWordAddr], nBitPos); break; #if (ENABLE_PLC) case KLCoilTypeT: if (nCoilAddr >= KLCoilTCount) return 0; thisValue = GetBitValue(PLCMem.WT[nWordAddr], nBitPos); break; case KLCoilTypeC: if (nCoilAddr >= KLCoilCCount) return 0; thisValue = GetBitValue(PLCMem.WC[nWordAddr], nBitPos); break; #endif case KLCoilTypeLR: if (nCoilAddr >= KLCoilLRCount) return 0; thisValue = GetBitValue(KMem.WLR[nWordAddr], nBitPos); break; case KLCoilTypeSR: if (nCoilAddr >= KLCoilSRCount) return 0; thisValue = GetBitValue(KMem.WSR[nWordAddr], nBitPos); 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; #if (ENABLE_PLC) case KLCoilTypeT: if (nCoilAddr >= KLCoilTCount) return 0; SetBitValue(&PLCMem.WT[nWordAddr], nBitAddr, nCoilValue); break; case KLCoilTypeC: if (nCoilAddr >= KLCoilCCount) return 0; SetBitValue(&PLCMem.WC[nWordAddr], nBitAddr, nCoilValue); break; #endif 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; #if (ENABLE_PLC) case KLDataTypeSV: if (nDataAddr >= KLDataSVCount) return 0; thisValue = PLCMem.SV[nDataAddr]; break; case KLDataTypeEV: if (nDataAddr >= KLDataEVCount) return 0; thisValue = PLCMem.EV[nDataAddr]; break; #endif 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; #if (ENABLE_PLC) case KLDataTypeSV: if (nDataAddr >= KLDataSVCount) return 0; PLCMem.SV[nDataAddr] = nDataValue; break; case KLDataTypeEV: if (nDataAddr >= KLDataEVCount) return 0; PLCMem.EV[nDataAddr] = nDataValue; break; #endif 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; } int KMachineSvFunc (int nChn, int nSvType, int nParam,void * pBuf, int nLen1) { int iRet =0; switch (nSvType){ case ReqNone: break; case ReqInit: break; case ReqReset: break; case ReqStop: break; case ReqRun: break; case ReqBlinkLED: KMRunStat.bLEDFlick=nParam; break; case ReqStartDiag: break; case ReqStopDiag: break; case ReqTransFirmware: break; case ReqTransCfg: break; case ReqTransProg: break; case ReqTransData: break; case ReqTransBlink: break; case ReqTransChild: break; case ReqTransInfo: break; case ReqTransOutBandData: break; case ReqRead1Bit: break; case ReqWrite1Bit: break; case ReqReadBits: break; case ReqWriteBits: break; case ReqReadData: break; case ReqWriteData: break; case ReqRemoteTran: break; default: iRet = -1; break; } return iRet; }