/**
|
******************************************************************************
|
* @file : KMachine.h
|
* @brief : Header for KMachine.c file.
|
* This file contains the common defines of the application.
|
******************************************************************************
|
*/
|
#ifndef __KMACHINE_H__
|
#define __KMACHINE_H__
|
|
#define FACTORY_DATA_BASE (FLASH_BASE + 0x00007C00) //31k //and
|
#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_PAGES 4 //use 4 pages
|
|
#define ALT_PRG_BASE (FLASH_BASE + 0x0000A000) //36k //and FLASH_BANK1_END
|
#define ALT_PRG_PAGESIZE (0x00000400) //Page Size = 1K
|
#define ALT_PRG_PAGES 4 //use 4 pages
|
|
#define STORE_SYSREG_BASE (FLASH_BASE + 0x0000A000) //40k //and
|
#define STORE_SYSREG_PAGESIZE (0x00000400) //Page Size = 1K
|
#define STORE_SYSREG_PAGES 1 //use 1 pages
|
|
#define STORE_RUNSTAT_BASE (FLASH_BASE + 0x0000A800) //44k K //and
|
#define STORE_RUNSTAT_PAGESIZE (0x00000400) //Page Size = 1K
|
#define STORE_RUNSTAT_PAGES 1 //use 1 pages
|
|
|
#define STORE_LOG_BASE (FLASH_BASE + 0x0000C000) //48k 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 int UINT;
|
typedef unsigned int uint32_t;
|
typedef unsigned int DWORD;
|
typedef unsigned short WORD;
|
typedef unsigned char BYTE;
|
|
#define LoBofW(x) ((x)&0xff)
|
#define HiBofW(x) (((x)>>8)&0xff)
|
|
#define LoHofB(x) ((x)&0xf)
|
#define HiHofB(x) (((x)>>4)&0xf)
|
|
// ÐÅÏ¢¿é
|
// ¹¤³§²ÎÊýÅäÖÿé
|
// Óû§/ϵͳ²ÎÊýÅäÖÿé
|
//
|
//
|
|
typedef struct tagInfoBlock
|
{
|
USHORT nDeviceType;
|
USHORT ProgVer;
|
USHORT KLinkVer;
|
USHORT nCapacity;
|
UCHAR nDInput;
|
UCHAR nDOutput;
|
UCHAR nAInput;
|
UCHAR nAOutput;
|
UCHAR nHInput;
|
UCHAR nHOutput;
|
UCHAR nExt1;
|
UCHAR nExt2;
|
|
}stKMInfoBlock;
|
|
enum enStoreCfg
|
{
|
CFG_VER = 0x100,
|
START_SIGN = 0x55aa,
|
END_SIGN = 0x5aa5,
|
};
|
|
|
enum enInputFilter
|
{
|
InputFilter_None =0,
|
InputFilter_1mS =1,
|
InputFilter_2mS =2,
|
InputFilter_4mS =3,
|
InputFilter_8mS =4,
|
InputFilter_16mS =5,
|
InputFilter_32mS =6,
|
InputFilter_64mS =7,
|
InputFilter_128mS =8,
|
InputFilter_256mS =9,
|
};
|
|
enum enOutputHold
|
{
|
Output_Hold = 0,
|
Output_Set_0 = 1,
|
Output_Set_1 = 2,
|
};
|
|
enum enPortType
|
{
|
PortType_Com = 0, //¼ÆËã»úͨѶ
|
PortType_Gen = 1, //ͨÓÃͨѶ£¬×ÔÓÉ¿Ú
|
PortType_KLink = 2, //KlinkͨѶ
|
PortType_KBus = 3, //KBusͨѶ
|
PortType_KNet = 4, // KNetͨѶ
|
PortType_ModbusRTU = 5, //Modbus RTU ͨѶ
|
};
|
|
//ÿ¸öÄ£¿éÓÐ 0/1/2/3/4/5/6/7/8¸ö ͨѶport
|
//²»ÏÞÓÚ UART, Íø¿Ú£¬ÎÞÏߣ¬µ¥×ÜÏߵȣ¬¶¼ÊÇport
|
//ÉõÖÁ¿ÉÒÔÓÐÐéÄâµÄport
|
|
typedef struct tagPortStat
|
{
|
UCHAR nWorking; //¹¤×÷ÖÐ
|
UCHAR nStation; //×Ô¼ºÕ¾ºÅ
|
UCHAR bBus; //×ÜÏß?,,È«Ë«¹¤?
|
UCHAR bMaster; //Ö÷»ú
|
UCHAR PortType; //¶Ë¿Ú¹¤×÷ģʽ
|
UCHAR nDevices; //Á¬½ÓµÄÉ豸ÊýÁ¿//²»°üÀ¨×Ô¼º //device list;
|
|
}stPortStat,*pPortStat;
|
|
enum enKeventType
|
{
|
EventTypeNone = 0,
|
EventTypePowerUp = 1,
|
EventTypePowerDown = 2,
|
EventTypePowerRecover = 3,
|
EventTypeConnected = 4,
|
EventTypeLostCon = 5,
|
EventTypeSetTime = 6,
|
EventTypeSysCfg = 7,
|
EventTypeProg = 8,
|
EventTypeForce = 9,
|
EventTypeClearEvent = 10,
|
|
|
EventType
|
};
|
|
typedef struct tagKMFuncParam
|
{
|
USHORT EnablePLC:1; //ʹÄÜÄÚ²¿PLC¹¦ÄÜ
|
USHORT RunMode; //¹¤×÷ģʽ
|
}stKMFuncParam;
|
|
// ÊäÈëÊä³öµØÖ·Ó³Éä
|
|
typedef struct tagComPortParam //4 Bytes
|
{
|
USHORT Station; /* 0=From jumper */
|
USHORT WorkMode; /* 0-5=Com,Gen,KLink,KBus,KNet,RTU */
|
USHORT BaudRate; /* =*100 Baudrate at which running */
|
// USHORT PortType:4; /* 0-5=Com,Gen,KLink,KBus,KNet,RTU */
|
USHORT ByteSize:2; /* 0-1=Number of bits/byte, 7-8 */
|
USHORT Parity:4; /* 0-4=None,Odd,Even,Mark,Space */
|
USHORT StopBits:2; /* 0,1,2 = 1, 1.5, 2 */
|
USHORT EndType:2; /* 0=ByChar, 1= ByTime */
|
USHORT EofChar:4; /* 0,1,2 = None, CR, CR+LF, ETX; End of character */
|
USHORT SofChar:2; /* 0,1,2 = None, STX */
|
USHORT EndTime;
|
USHORT RecvAddr;
|
USHORT RecvSize;
|
|
}stComPortParam;
|
|
typedef struct tagInputFilterParam // 1 Bytes
|
{
|
BYTE Filter0:4;
|
BYTE Filter1:4;
|
|
}stInputFilterParam;
|
|
typedef struct tagOutputHoldParam //1 Bytes
|
{
|
BYTE Hold1:4;
|
BYTE Hold2:4;
|
}stOutputHoldParam;
|
|
#pragma anon_unions
|
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 OutMappings[6]; //12 Bytes //Êä³öÓ³Éä
|
|
stComPortParam PortParams[2]; // 8 Bytes
|
stOutputHoldParam OutputParams[16]; //16 Bytes
|
stInputFilterParam InputParams[16]; //16 Bytes
|
|
UINT cfgvar3; // 4 Bytes
|
UINT cfgvar4; // 4 Bytes
|
UINT cfgvar5; // 4 Bytes
|
UINT cfgvar6; // 4 Bytes
|
UINT cfgvar7; // 4 Bytes
|
UINT cfgvar8; // 4 Bytes
|
UINT cfgvar9; // 4 Bytes
|
UINT cfgvar10; // 4 Bytes
|
UINT cfgvar11; // 4 Bytes
|
UINT cfgvar12; // 4 Bytes
|
UINT cfgvar13; // 4 Bytes
|
UINT cfgvar14; // 4 Bytes
|
UINT cfgvar15; // 4 Bytes
|
UINT cfgvar16; // 4 Bytes
|
UINT Space1[5]; //20 Bytes
|
|
}stKMSysCfg,* pKMSysCfg;
|
|
typedef struct tagStoredKMSysCfg
|
{
|
unsigned short Sign1;
|
unsigned short Seq1;
|
stKMSysCfg theKMSysCfg;
|
unsigned short CRC1;
|
unsigned short EndSign1;
|
}stStoredKMSysCfg,*pStoredKMSysCfg;
|
|
/*
|
typedef struct tagFactData
|
{
|
USHORT Sign; //
|
USHORT nLength; //
|
UCHAR LOT_NO[16]; //
|
UINT MANDate; //
|
UINT SN; //
|
UINT REV1[24]; //
|
USHORT rev9;
|
USHORT CRC;
|
|
}stFactData;
|
*/
|
typedef struct tagFactoryData //¹¤³§Á¿²ú²ÎÊý£¬Êý¾Ý
|
{
|
USHORT Sign1;
|
USHORT Seq1;
|
USHORT nModelNo;
|
USHORT nModelVer;
|
UINT nLotNo;
|
UINT nProductDateTime;
|
UINT SN1;
|
UINT nProtocalVer;
|
UINT nDefaultFunc;
|
UCHAR ModelStr[16];
|
UCHAR LOT_NO[16]; //
|
UCHAR SNStr[16];
|
unsigned short CRC1;
|
unsigned short EndSign1;
|
|
}stFactoryData,* pFactoryData;
|
|
typedef struct tagEventLog
|
{
|
unsigned short Sign1;
|
unsigned short Seq1;
|
unsigned int nTime;
|
unsigned short nType;
|
unsigned short nParam1;
|
unsigned int nParam2;
|
}stEventLog,* pEventLog;
|
|
typedef struct tagRunStat
|
{
|
unsigned short Sign1;
|
unsigned short Seq1;
|
volatile unsigned short PowerCount; //
|
volatile unsigned short Reserved1;
|
volatile unsigned int UpTime; //Seconds;
|
volatile unsigned int UserData1;
|
volatile unsigned short WorkMode;
|
volatile unsigned short WorkMode2;
|
volatile unsigned short nBinProgBank;
|
volatile unsigned short nBinProgSize;
|
unsigned int Reserved2[1];
|
unsigned short CRC1;
|
unsigned short EndSign1;
|
}stRunStat, *pRunStat;
|
|
extern stRunStat KMRunStat;
|
|
//stStoreCfg * GetCurStoreCfgAddr(void );
|
//stStoreCfg * GetNextStoreCfgAddr(stStoreCfg * CurCfg );
|
|
extern stStoredKMSysCfg storedKMSysCfg;
|
|
#define TYPECOIL 0x00
|
#define TYPEDATA 0x80
|
|
enum enKLCoilTypes
|
{
|
KLCoilTypeX = 0 | TYPECOIL , //X Input
|
KLCoilTypeY = 1 | TYPECOIL, //Y Output
|
KLCoilTypeR = 2 | TYPECOIL, //R register
|
KLCoilTypeLX = 3 | TYPECOIL, //Link register
|
KLCoilTypeLY = 4 | TYPECOIL, //Link register
|
KLCoilTypeT = 5 | TYPECOIL, //Timer
|
KLCoilTypeC = 6 | TYPECOIL, //Counter
|
KLCoilTypeLR = 7 | TYPECOIL, //Link register
|
KLCoilTypeSR = 8 | TYPECOIL, //Link register
|
|
};
|
enum enKLDataTypes
|
{
|
KLDataTypeDEC = 0 | TYPEDATA,
|
KLDataTypeHEX = 1 | TYPEDATA,
|
KLDataTypeFloat = 2 | TYPEDATA,
|
KLDataTypeWX = 3 | TYPEDATA,
|
KLDataTypeWY = 4 | TYPEDATA,
|
KLDataTypeWR = 5 | TYPEDATA,
|
KLDataTypeWLX = 6 | TYPEDATA,
|
KLDataTypeWLY = 7 | TYPEDATA,
|
KLDataTypeDT = 8 | TYPEDATA,
|
KLDataTypeSDT = 9 | TYPEDATA,
|
KLDataTypeWSR = 10 | TYPEDATA,
|
KLDataTypeSV = 11 | TYPEDATA,
|
KLDataTypeEV = 12 | TYPEDATA,
|
KLDataTypeLD = 13 | TYPEDATA,
|
KLDataSysCfg = 25 | TYPEDATA,
|
KLDataTypeFlash = 33 | TYPEDATA,
|
KLDataTypeTest = 254 | TYPEDATA,
|
};
|
|
enum enKLDataCounts
|
{
|
KLDataDTCount = 256,
|
KLDataSDTCount = 256,
|
|
KLDataWXCount = 16,
|
KLDataWYCount = 16,
|
KLDataWRCount = 16,
|
KLDataLDCount = 64,
|
KLDataWLCount = 8,
|
|
KLCoilXCount = KLDataWXCount * 16,
|
KLCoilYCount = KLDataWYCount * 16,
|
KLCoilRCount = KLDataWRCount * 16,
|
|
KLCoilTCount = 64,
|
KLCoilCCount = KLCoilTCount,
|
|
KLDataSVCount = KLCoilTCount,
|
KLDataEVCount = KLCoilTCount,
|
|
KLCoilLXCount = 128,
|
KLCoilLYCount = 128,
|
KLCoilLRCount = 128,
|
KLCoilSRCount = 128,
|
|
};
|
#define TOTAL_WDFS (16) //Total DF Count
|
#define TOTAL_CurVAL (16) //
|
#define TOTALTIMERS (64)
|
|
typedef struct tagTimerStat
|
{
|
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;
|
|
typedef struct tagTimer
|
{
|
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;
|
};
|
}stTimer;
|
|
typedef struct tagKMem
|
{
|
unsigned short WDFs[TOTAL_WDFS];
|
unsigned char CurVALs[TOTAL_CurVAL];
|
unsigned char CurVAL;
|
stTimer Timers[TOTALTIMERS];
|
|
union {
|
unsigned short WX[KLDataWXCount]; //±¾»úµÄXºÍY
|
unsigned char WXB[KLDataWXCount*2];
|
};
|
union {
|
unsigned short WY[KLDataWYCount]; //±¾»úµÄXºÍY
|
unsigned char WYB[KLDataWYCount*2]; //±¾»úµÄXºÍY
|
};
|
unsigned short WR[KLDataWRCount];
|
|
unsigned short WT[16];
|
|
unsigned short WC[16];
|
unsigned short EV[KLDataEVCount];
|
unsigned short SV[KLDataSVCount];
|
|
|
unsigned short WLX[16]; //ÐéÄâµÄXºÍY£¬Ô¶³ÌͨѶʱӳÉäÓá£
|
unsigned short WLY[16];
|
unsigned short WLR[16];
|
unsigned short WSR[16];
|
|
union {
|
unsigned int DTD[KLDataDTCount];
|
unsigned short DT[KLDataDTCount];
|
unsigned char DTB[KLDataDTCount*2];
|
};
|
|
// ÅäÖüĴæÆ÷
|
// ϵͳ״̬¼Ä´æÆ÷
|
// ÌØÊâ¼Ä´æÆ÷
|
// µ÷ÊÔ£¬¼à¿Ø¼Ä´æÆ÷
|
union {
|
unsigned int SDD[KLDataSDTCount/2];
|
unsigned short SDT[KLDataSDTCount];
|
unsigned char SDB[KLDataSDTCount*2];
|
struct {
|
unsigned int EffJumperSW;
|
unsigned int CurJumperSW;
|
unsigned int haltick;
|
unsigned int nRunCount;
|
unsigned int RunStat;
|
unsigned int ErrStat;
|
unsigned int PwrOnCount;
|
unsigned int ThisRunTime;
|
unsigned int TotalRunTime;
|
unsigned int CurTimeSec;
|
unsigned int PwrFailCount;
|
unsigned int LastPwrFailTime;
|
unsigned int LastScanTime;
|
unsigned int ScanTimeuS;
|
unsigned int MinScanTimeuS;
|
unsigned int MaxScanTimeuS;
|
unsigned int nEventCount;
|
unsigned int nEventMinIndex;
|
unsigned int nEventMaxIndex;
|
unsigned int Rev2[5];
|
unsigned short ADCValues[20];
|
};
|
};
|
}stKMem;
|
|
|
extern stKMem KMem;
|
extern const stKMInfoBlock KMInfoBlock;
|
|
extern volatile int PowerDownEvent;
|
extern volatile int OldPowerDownEvent;
|
extern volatile int OldPowerDownEventTime;
|
|
int KMachineInit(void);
|
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 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 WriteSysCfgToFlash(pStoredKMSysCfg theStoredKMSysCfg);
|
int ReadSysCfgFromFlash(pStoredKMSysCfg theStoredKMSysCfg);
|
|
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);
|
|
|
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__ */
|