/**
|
******************************************************************************
|
* @file : KMachine.c
|
* @brief : KMachine program body
|
******************************************************************************
|
*/
|
|
#include "KMachine.h"
|
#include "string.h"
|
#include "Globaldef.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 */
|
|
|
stStoredKMSysCfg storedKMSysCfg ;
|
stKMem KMem;
|
stRunStat KMRunStat;
|
|
//uint8_t * pFlash1 = (uint8_t *)(STORECFGBASE);
|
|
//void * pConfigFlashBase = (uint8_t *)(STORECFGBASE);
|
|
//uint16_t FlashDatas[16];
|
|
//uint32_t * pUID = (uint32_t *)(UID_BASE);
|
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 stStoredKMSysCfg KMDefaultSysCfg /*__attribute__((at(STORECFGBASE)))*/ =
|
{
|
START_SIGN,
|
0x0000,
|
{
|
CFG_VER,
|
0x0000, //workmode
|
0x0000, //switchfunc
|
0x0000, //pad1;
|
{ //comportparam[2]
|
{
|
PortType_KLink, //PorttType
|
1, //Station
|
2304, //Buadrate = * 100;
|
0, //ByteSize
|
0, //Parity
|
0, //StopBits
|
0, //endType
|
0, //EofChar
|
0, //SofChar
|
0, //endtime
|
0, //recvbuf
|
0, //bufsize
|
},
|
{
|
PortType_KBus, //PorttType
|
0, //Station
|
2304, //Buadrate = * 100;
|
0, //ByteSize
|
0, //Parity
|
0, //StopBits
|
0, //endType
|
0, //EofChar
|
0, //SofChar
|
0, //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
|
0,0,0,0,0,0
|
},
|
0x0003, //padding s
|
0x0004,
|
0x0005,
|
0x0006,
|
0x0007,
|
0x0008,
|
0x0009,
|
0x000a,
|
0x000b,
|
0x000c,
|
},
|
0x0011, //CRC16
|
END_SIGN,
|
};
|
|
const stKMSysCfg KMDefaultSysCfg2[7] /*__attribute__((at(STORECFGBASE+sizeof(stKMSysCfg))))*/;
|
|
int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize)
|
{
|
// memcpy(pBuf,pAddrFlash,nSize);
|
for (int i=0;i<nByteSize/4;i++)
|
{
|
((uint32_t *)pBuf)[i] = ((uint32_t *)pAddrFlash)[i];
|
}
|
for (int i=nByteSize/4*2;i<nByteSize/2;i++)
|
{
|
((uint16_t *)pBuf)[i] = ((uint16_t *)pAddrFlash)[i];
|
}
|
return nByteSize;
|
}
|
int EraseFlashMem(void * pAddrFlash, unsigned int Pages)
|
{
|
HAL_StatusTypeDef res;
|
res = HAL_FLASH_Unlock();
|
uint32_t ErrNo;
|
FLASH_EraseInitTypeDef erase1;
|
erase1.NbPages=Pages;
|
erase1.PageAddress=(unsigned int)pAddrFlash;
|
erase1.TypeErase=FLASH_TYPEERASE_PAGES;
|
res = HAL_FLASHEx_Erase(&erase1,&ErrNo);
|
res = HAL_FLASH_Lock();
|
return res;
|
}
|
int WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize)
|
{
|
HAL_StatusTypeDef res;
|
res = HAL_FLASH_Unlock();
|
/*
|
for (int i=0;i<nSize/2;i++)
|
{
|
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[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 = nByteSize/4 * 2 ; i < nByteSize/2 ; i++)
|
{
|
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
|
}
|
//*/
|
res = HAL_FLASH_Lock();
|
|
return res;
|
}
|
int EraseAndWriteToFlashMem(void * pBuf, void * pAddrFlash, unsigned int nByteSize)
|
{
|
|
HAL_StatusTypeDef res;
|
res = HAL_FLASH_Unlock();
|
uint32_t ErrNo;
|
FLASH_EraseInitTypeDef erase1;
|
erase1.NbPages=1;
|
erase1.PageAddress=(unsigned int)pAddrFlash;
|
erase1.TypeErase=FLASH_TYPEERASE_PAGES;
|
res = HAL_FLASHEx_Erase(&erase1,&ErrNo);
|
|
for (int i=0;i<nByteSize/2;i++)
|
{
|
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
|
}
|
/*
|
for (int i=0;i<nSize/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++)
|
{
|
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
|
}
|
*/
|
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)
|
{
|
stFactoryData * p1 = (stFactoryData*) pDataBuf;
|
stFactoryData * p2 = (stFactoryData *)FACTORY_DATA_BASE;
|
p1->Seq1= p2->Seq1+1;
|
|
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 == START_SIGN && pStoreKMSysCfg->EndSign1 == END_SIGN)
|
{
|
if (pStoreKMSysCfg->Seq1 > nMaxSeq)
|
{
|
nIndex=i;nMaxSeq=pStoreKMSysCfg->Seq1;
|
}
|
}
|
}
|
if (nIndex>=0 && nIndex <8)
|
{
|
ReadFlashMem(theStoredKMSysCfg,(void *)(&pStoreKMSysCfg[nIndex]),sizeof(stStoredKMSysCfg));
|
}else {
|
LoadDefaultSysCfg(theStoredKMSysCfg);
|
}
|
//memcpy(theKMSysCfg,(void* )STORECFGBASE,sizeof(KMSysCfg));
|
return 0;
|
}
|
|
int WriteSysCfgToFlash(pStoredKMSysCfg theStoredKMSysCfg)
|
{
|
theStoredKMSysCfg->Seq1++;
|
// 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<s2;j++)
|
{
|
if ((nAddr2)[j] != 0xff) {skip =1;break;}
|
}
|
if (skip==1) {continue;}
|
nIndex=i;
|
break;
|
}
|
if (nIndex >=0 && nIndex <8) {
|
WriteToFlashMemNoErase(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE + nIndex*s2),sizeof(theStoredKMSysCfg));
|
}
|
else {
|
EraseAndWriteToFlashMem(theStoredKMSysCfg,(void *)STORE_SYSREG_BASE,sizeof(theStoredKMSysCfg));
|
}
|
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;j<s2;j++)
|
{
|
if (((unsigned char *)nAddr2)[j] != 0xff)
|
{skip =1;}
|
}
|
if (skip==1) {continue;}
|
break;
|
}
|
stStoreCfg * p1 = (stStoreCfg *)nAddr2;
|
return p1;
|
}
|
|
|
int SaveStoreCfg(stStoreCfg * CurCfg)
|
{
|
return 0;
|
}
|
*/
|
// stStoreCfg Cfg2;
|
|
int LoadFlashDatas()
|
{
|
for (int i=0;i<16;i++)
|
{
|
// FlashDatas[i]=((uint16_t *)pConfigFlashBase)[i];
|
}
|
return 0;
|
}
|
/*
|
int LoadAndUpdateStoreCfg()
|
{
|
stStoreCfg * pFCfg = (stStoreCfg *) GetCurStoreCfgAddr();
|
|
Cfg2.Sign1=START_SIGN;
|
Cfg2.Seq1=pFCfg[0].Seq1+1;
|
Cfg2.CRC1=0x7777;
|
Cfg2.PowerCount=pFCfg[0].PowerCount+1;
|
Cfg2.UpTime=pFCfg[0].UpTime+1;
|
Cfg2.UserData1=pFCfg[0].UserData1;
|
Cfg2.EndSign1=END_SIGN;
|
stStoreCfg * pFCfg2 = GetNextStoreCfgAddr(pFCfg);
|
|
|
HAL_StatusTypeDef res;
|
if (pFCfg2 <= pFCfg)
|
{
|
res = (HAL_StatusTypeDef)EraseAndWriteToFlashMem(&Cfg2,pFCfg2,sizeof(stStoreCfg));
|
|
}else
|
{
|
res = (HAL_StatusTypeDef)WriteToFlashMemNoErase(&Cfg2,pFCfg2,sizeof(stStoreCfg));
|
}
|
return res;
|
}
|
*/
|
int CheckSavedData(void * pStartAddr, int PageSize, int Pages, int DataSize)
|
{
|
return 0;
|
};
|
|
|
int nMaxRunStatIndex=-1;
|
unsigned int nMaxRunStatSeq=0;
|
int nNextRunStatSpace=0;
|
int LoadDefaultRunStat(pRunStat theRunStat)
|
{
|
theRunStat->PowerCount=1;
|
// theRunStat->UpTime=0;
|
// theRunStat->UserData1=0;
|
// theRunStat->WorkMode=0;
|
// theRunStat->WorkMode2=0;
|
// theRunStat->nBinProgBank=0;
|
// theRunStat->nBinProgSize=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) < (STORE_RUNSTAT_PAGESIZE * 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) > STORE_RUNSTAT_PAGESIZE * 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<sizeof(stRunStat);j++)
|
{
|
if (pFlash[j]!=0xff) {Skip =1 ; break;}
|
}
|
if (Skip ==0 )
|
{
|
WriteToFlashMemNoErase(theRunStat,(void *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat)),sizeof(stRunStat));
|
}else
|
{
|
EraseAndWriteToFlashMem(theRunStat,(void *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat)),sizeof(stRunStat));
|
}
|
nMaxRunStatIndex=nNextRunStatSpace;
|
nNextRunStatSpace++;
|
if ((nNextRunStatSpace+1) * sizeof(stRunStat) > STORE_RUNSTAT_PAGESIZE * STORE_RUNSTAT_PAGES)
|
{
|
nNextRunStatSpace=0;
|
}
|
return 0;
|
}
|
|
|
int nEventCount=0;
|
int nEventMinIndex;
|
int nEventMaxIndex;
|
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;
|
uchar * pEventStore = (uchar *)STORE_LOG_BASE;
|
pEventLog theEventLog = (pEventLog) pEventStore;
|
// int s = sizeof(stEventLog);
|
nEventCount=0;
|
|
for (int i=0;i * sizeof(stEventLog) < (STORE_LOG_PAGESIZE * 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) > STORE_LOG_PAGESIZE * 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<sizeof(stEventLog);j++)
|
{
|
if (pFlash[j]!=0xff) {Skip =1 ; break;}
|
}
|
if (Skip ==0 )
|
{
|
WriteToFlashMemNoErase(&thisEventLog,(void *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog)),sizeof(stEventLog));
|
}else
|
{
|
EraseAndWriteToFlashMem(&thisEventLog,(void *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog)),sizeof(stEventLog));
|
|
}
|
nEventMaxIndex=nEventNextSpace;
|
nEventNextSpace++;
|
if ((nEventNextSpace+1) * sizeof(stEventLog) > STORE_LOG_PAGESIZE * STORE_LOG_PAGES)
|
{
|
nEventNextSpace=0;
|
}
|
nEventCount++;
|
KMem.nEventCount=nEventCount;
|
return 0;
|
}
|
|
pEventLog GetEventLogAddr(int nIndex)
|
{
|
int nEventIndex=nEventMinIndex + nIndex;
|
|
if (nEventIndex * sizeof(stEventLog) >= (STORE_LOG_PAGESIZE * STORE_LOG_PAGES))
|
{
|
nEventIndex -= (STORE_LOG_PAGESIZE * 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;
|
}
|
int KMachineInit(void)
|
{
|
// ClearEventLog();
|
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;
|
}
|
|
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;
|
}
|
|