QuakeGod
2023-02-22 b6290b94cac76a5634cd90b495b65d91ee92d85e
提交 | 用户 | age
bfc108 1 /**
Q 2   ******************************************************************************
3   * @file           : KMachine.h
4   * @brief          : Header for KMachine.c file.
5   *                   This file contains the common defines of the application.
6   ******************************************************************************
7     */
8 #ifndef __KMACHINE_H__
9 #define __KMACHINE_H__
10
11 #define FACTORY_DATA_BASE (FLASH_BASE + 0x00007C00)        //31k //and 
12 #define FACTORY_DATA_PAGESIZE (0x00000400)                        //Page Size = 1K
13 #define FACOTRY_DATA_PAGES 1                                                  // use 1 page(s)
14
15 #define STORE_PRG_BASE (FLASH_BASE + 0x00008000)        //32k //and FLASH_BANK1_END
16 #define STORE_PRG_PAGESIZE (0x00000400)                            //Page Size = 1K
17 #define STORE_PRG_PAGES 4                                                    //use 4 pages
18
a7db3c 19 #define ALT_PRG_BASE (FLASH_BASE + 0x0000A000)        //36k //and FLASH_BANK1_END
bfc108 20 #define ALT_PRG_PAGESIZE (0x00000400)                            //Page Size = 1K
Q 21 #define ALT_PRG_PAGES 4                                                    //use 4 pages
22
23 #define STORE_SYSREG_BASE (FLASH_BASE + 0x0000A000)        //40k //and 
24 #define STORE_SYSREG_PAGESIZE (0x00000400)                            //Page Size = 1K
25 #define STORE_SYSREG_PAGES 1                                                        //use 1 pages
26
a7db3c 27 #define STORE_RUNSTAT_BASE (FLASH_BASE + 0x0000A800)        //44k K //and 
bfc108 28 #define STORE_RUNSTAT_PAGESIZE (0x00000400)                            //Page Size = 1K
Q 29 #define STORE_RUNSTAT_PAGES 1                                                        //use 1 pages
30
31
32 #define STORE_LOG_BASE (FLASH_BASE + 0x0000C000)        //48k and FLASH_BANK1_END
33 #define STORE_LOG_PAGESIZE (0x00000400)                            //Page Size = 1K
34 #define STORE_LOG_PAGES 4                                                        //use 4 pages
a7db3c 35
bfc108 36
Q 37 typedef unsigned char uchar;
38 typedef unsigned char UCHAR;
39 typedef unsigned short USHORT;
40 typedef unsigned int UINT;
41 typedef unsigned int uint32_t;
42 typedef unsigned int DWORD;
43 typedef unsigned short WORD;
44 typedef unsigned char BYTE;
45
a7db3c 46 #define LoBofW(x) ((x)&0xff)
Q 47 #define HiBofW(x) (((x)>>8)&0xff)
48
49 #define LoHofB(x) ((x)&0xf)
50 #define HiHofB(x) (((x)>>4)&0xf)
51
0fe6b0 52 //  信息块
Q 53 //  工厂参数配置块
54 //  用户/系统参数配置块
a7db3c 55 //  
Q 56 //
57
0fe6b0 58 typedef struct tagInfoBlock // 20 bytes
a7db3c 59 {
0fe6b0 60 //    USHORT nBlockLenth;
Q 61     USHORT nDeviceTypeVer;            //device type        x.y
62 //    UCHAR nDevierVer;
63     USHORT ProgVer;                    //prog version    x.y
64     USHORT KLinkVer;                //x.y
65     USHORT KBusVer;                    //x.y
66     
67     UCHAR nCapacity1;                //    ? K
68     UCHAR nCapacity2;                //    ? k
69     
a7db3c 70     UCHAR nDInput;
Q 71     UCHAR nDOutput;
72     UCHAR nAInput;
73     UCHAR nAOutput;
74     UCHAR nHInput;
75     UCHAR nHOutput;
76     UCHAR nExt1;
77     UCHAR nExt2;
0fe6b0 78     UCHAR nLogSize;
Q 79     UCHAR nPorts;
80     UCHAR nManSize;
81     UCHAR nAbility;
82     UCHAR nSwitchBits;
a7db3c 83     
Q 84 }stKMInfoBlock;
0fe6b0 85
Q 86 /*
87 typedef struct tagFactData
88 {
89     USHORT Sign;                    //
90     USHORT nLength;                //
91     UCHAR LOT_NO[16];            //
92     UINT MANDate;                    //
93     UINT SN;                            //
94     UINT REV1[24];                //
95     USHORT rev9;
96     USHORT CRC;
97     
98 }stFactData;
99 */
100 typedef struct tagFactoryData        //工厂量产参数,数据
101 {
102     USHORT Sign1;
103     USHORT Seq1;
104     USHORT nModelNo;
105     USHORT nModelVer;
106     UINT nLotNo;
107      UINT nProductDateTime;
108     UINT SN1;
109     UINT nProtocalVer;
110     UINT nDefaultFunc;
111     UCHAR ModelStr[16];
112     UCHAR LOT_NO[16];            //
113     UCHAR SNStr[16];
114     unsigned short CRC1;
115     unsigned short EndSign1;
116     
117 }stFactoryData,* pFactoryData;
118
a7db3c 119
bfc108 120 enum enStoreCfg
Q 121 {
a7db3c 122     CFG_VER        = 0x100,
bfc108 123     START_SIGN = 0x55aa,
Q 124     END_SIGN    =    0x5aa5,
125 };
126
127 enum enInputFilter
128 {
129     InputFilter_None =0,
130     InputFilter_1mS =1,
131     InputFilter_2mS =2,
132     InputFilter_4mS =3,
133     InputFilter_8mS =4,
134     InputFilter_16mS =5,
135     InputFilter_32mS =6,
136     InputFilter_64mS =7,
137     InputFilter_128mS =8,
138     InputFilter_256mS =9,
139 };
140
141 enum enOutputHold
142 {
143     Output_Hold = 0,
144     Output_Set_0 = 1,
145     Output_Set_1 = 2,
146 };
147
0fe6b0 148 typedef struct tagPortStatus
Q 149 {
150     USHORT porttype;        //232,485,net , wireless// KLink, KBus, KNet, KWireless
151     UCHAR StationID;
152     UCHAR MaxStations;
153     UCHAR CurStations;
154     UCHAR Running;
155     UCHAR Health;
0c2056 156 }stPortStatus,*pPortStatus;
0fe6b0 157
bfc108 158 enum enPortType
Q 159 {
0fe6b0 160     PortType_Default = 0, //默认
Q 161     PortType_KLink = 1, //KLink通讯
162     PortType_KBus = 2,     //KBus通讯
163     PortType_KNet = 3,     // KNet通讯
164     PortType_ModbusRTU = 4, //Modbus RTU 通讯
165     PortType_Com = 5,    //    计算机通讯
166     PortType_Gen = 6,    //通用通讯,自由口
bfc108 167 };
Q 168
a7db3c 169 typedef struct tagComPortParam            //4 Bytes
bfc108 170 {
0fe6b0 171     USHORT WorkMode;                /* 0-5=Default,KLink,KBus,KNet,RTU,Com,Gen */
a7db3c 172     USHORT Station;                    /* 0=From jumper */
Q 173     USHORT BaudRate;       /* =*100 Baudrate at which running       */
bfc108 174   USHORT ByteSize:2;        /* 0-1=Number of bits/byte, 7-8    */
Q 175     USHORT Parity:4;                    /* 0-4=None,Odd,Even,Mark,Space    */
176   USHORT StopBits:2;        /* 0,1,2 = 1, 1.5, 2               */
a7db3c 177     USHORT EndType:2;                    /* 0=ByChar, 1= ByTime */
Q 178   USHORT EofChar:4;         /* 0,1,2 = None, CR, CR+LF, ETX;  End of character  */
bfc108 179     USHORT SofChar:2;                    /* 0,1,2 = None, STX */
0fe6b0 180     USHORT EndTime;                        /*(*0.01mS) */
a7db3c 181     USHORT RecvAddr;
Q 182     USHORT RecvSize;
0fe6b0 183
Q 184 }stComPortParam, *pstComPortParam;
185
186 //每个模块有 0/1/2/3/4/5/6/7/8个 通讯port
187 //不限于 UART, 网口,无线,单总线等,都是port
188 //甚至可以有虚拟的port
189
190 typedef struct tagPortStat
191 {
192     UCHAR PortType;            //端口工作模式
193     UCHAR nStation;            //自己站号
194     UCHAR bWorking;            //工作中
195     UCHAR bBus;                    //总线?,,全双工?
196     UCHAR bMaster;            //主机
197     UCHAR nDevices;            //连接的设备数量//不包括自己 //device list;
a7db3c 198     
0fe6b0 199 }stPortStat,*pPortStat;
Q 200
201 typedef struct tagKMFuncParam
202 {
203     USHORT EnablePLC:1;        //使能内部PLC功能
204     USHORT RunMode;                //工作模式
205 }stKMFuncParam;
206
207 // 输入输出地址映射
bfc108 208
a7db3c 209 typedef struct tagInputFilterParam        // 1 Bytes
bfc108 210 {
Q 211     BYTE Filter0:4;
212     BYTE Filter1:4;
213
214 }stInputFilterParam;
215
a7db3c 216 typedef struct tagOutputHoldParam            //1 Bytes
bfc108 217 {
Q 218     BYTE Hold1:4;
219     BYTE Hold2:4;
220 }stOutputHoldParam;    
221
222 #pragma anon_unions
a7db3c 223 typedef struct tagKMSysCfg        //120 Bytes total
bfc108 224 {
a7db3c 225     USHORT Version;                                        // SC0    // 2 Bytes
Q 226     USHORT workmode;                                    // SC1  // 2 Bytes 0=From jumper  
227     USHORT SwitchFunc;                                // SC2  // 2 Bytes 
0fe6b0 228     USHORT pad1;                                            // 2 Bytes
a7db3c 229     
Q 230     stComPortParam PortParams[2];                            // 8 Bytes
231     stInputFilterParam InputParams[16];                //16 Bytes
0fe6b0 232     stOutputHoldParam OutputParams[16];                //16 Bytes
Q 233     USHORT OutMappings[6];                //12 Bytes //输出映射
a7db3c 234     
Q 235     UINT cfgvar3;                                                            // 4 Bytes
236     UINT cfgvar4;                                                            // 4 Bytes
237     UINT cfgvar5;                                                            // 4 Bytes
238     UINT cfgvar6;                                                            // 4 Bytes
239     UINT cfgvar7;                                                            // 4 Bytes
240     UINT cfgvar8;                                                            // 4 Bytes
241     UINT cfgvar9;                                                            // 4 Bytes
242     UINT cfgvar10;                                                        // 4 Bytes
243     UINT cfgvar11;                                                        // 4 Bytes
244     UINT cfgvar12;                                                        // 4 Bytes
0fe6b0 245 //    UINT cfgvar13;                                                        // 4 Bytes
Q 246 //    UINT cfgvar14;                                                        // 4 Bytes
247 //    UINT cfgvar15;                                                        // 4 Bytes
248 //    UINT cfgvar16;                                                        // 4 Bytes
249 //    UINT Space1[4];                                                        //16 Bytes
bfc108 250
Q 251 }stKMSysCfg,* pKMSysCfg;
252
a7db3c 253 typedef struct tagStoredKMSysCfg
bfc108 254 {
Q 255     unsigned short Sign1;
256     unsigned short Seq1;
a7db3c 257     stKMSysCfg theKMSysCfg;
Q 258     unsigned short CRC1;
259     unsigned short EndSign1;
260 }stStoredKMSysCfg,*pStoredKMSysCfg;
261
0fe6b0 262 enum enKeventType
a7db3c 263 {
0fe6b0 264     EventTypeNone = 0,
Q 265     EventTypePowerUp = 1,
266     EventTypePowerDown = 2,
267     EventTypePowerRecover = 3,
268     EventTypeConnected = 4,
269     EventTypeLostCon = 5,
270     EventTypeSetTime = 6,
271     EventTypeSysCfg = 7,
272     EventTypeProg = 8,
273     EventTypeForce = 9,
274     EventTypeClearEvent = 10,
a7db3c 275     
bfc108 276     
0fe6b0 277     EventType
Q 278 };
bfc108 279
Q 280 typedef struct tagEventLog
281 {
282     unsigned short Sign1;
283     unsigned short Seq1;
284     unsigned int nTime;
285     unsigned short nType;
286     unsigned short nParam1;
287     unsigned int nParam2;
288 }stEventLog,* pEventLog;
289
290 typedef struct tagRunStat
291 {
292     unsigned short Sign1;
293     unsigned short Seq1;
a7db3c 294     volatile unsigned short PowerCount;    //
Q 295     volatile unsigned short Reserved1;
296     volatile unsigned int UpTime;        //Seconds;
297     volatile unsigned int UserData1;
298     volatile unsigned short WorkMode;
299     volatile unsigned short WorkMode2;
300     volatile unsigned short nBinProgBank;
301     volatile unsigned short nBinProgSize;
0fe6b0 302     unsigned short bLEDFlick;
Q 303     unsigned short Reserved2[1];
bfc108 304     unsigned short CRC1;
Q 305     unsigned short EndSign1;
306 }stRunStat, *pRunStat;
307
308 extern stRunStat KMRunStat;
309
310 //stStoreCfg * GetCurStoreCfgAddr(void );
311 //stStoreCfg * GetNextStoreCfgAddr(stStoreCfg * CurCfg );
312
a7db3c 313 extern stStoredKMSysCfg storedKMSysCfg;
Q 314
315 #define TYPECOIL 0x00
316 #define TYPEDATA 0x80
317
318     enum enKLCoilTypes
319     {
320         KLCoilTypeX = 0 | TYPECOIL ,        //X Input
321         KLCoilTypeY = 1 | TYPECOIL,        //Y Output
322         KLCoilTypeR = 2 | TYPECOIL,        //R register
323         KLCoilTypeLX = 3 | TYPECOIL,        //Link register
324         KLCoilTypeLY = 4 | TYPECOIL,        //Link register
325         KLCoilTypeT = 5 | TYPECOIL,        //Timer
326         KLCoilTypeC = 6 | TYPECOIL,        //Counter
327         KLCoilTypeLR = 7 | TYPECOIL,        //Link register
328         KLCoilTypeSR = 8 | TYPECOIL,        //Link register
329
330     };
331     enum enKLDataTypes
332     {
333         KLDataTypeDEC = 0 | TYPEDATA,
334         KLDataTypeHEX = 1 | TYPEDATA,
335         KLDataTypeFloat = 2 | TYPEDATA,
336         KLDataTypeWX = 3 | TYPEDATA,
337         KLDataTypeWY = 4 | TYPEDATA,
338         KLDataTypeWR = 5 | TYPEDATA,
339         KLDataTypeWLX = 6 | TYPEDATA,
340         KLDataTypeWLY = 7 | TYPEDATA,
341         KLDataTypeDT = 8 | TYPEDATA,
342         KLDataTypeSDT = 9 | TYPEDATA,
343         KLDataTypeWSR = 10 | TYPEDATA,
344         KLDataTypeSV = 11 | TYPEDATA,
345         KLDataTypeEV = 12 | TYPEDATA,
346         KLDataTypeLD = 13 | TYPEDATA,
347         KLDataSysCfg = 25 | TYPEDATA,
348         KLDataTypeFlash = 33 | TYPEDATA,
0fe6b0 349         KLDataTypeWDT = 41 | TYPEDATA,
Q 350         KLDataTypeKBD = 51 | TYPEDATA,
a7db3c 351         KLDataTypeTest = 254 | TYPEDATA,
Q 352     };
353     
354 typedef struct tagTimerStat
355 {
356     unsigned short nScale:2;//Time Scale, 0:1ms 1:10ms 2:100ms 3:1S
357     unsigned short nType:1;    //0 : timer 1:counter ;
358     unsigned short nDir:1;        //0 : count down. 1 count up;
359     unsigned short nInited:1;
360     unsigned short bSet:1;
361     unsigned short bTon:1;
362     
363 }stTimerStat;
364
365 typedef struct tagTimer
366 {
367     unsigned int LastActTime;
368     union {
369         unsigned short StatByte;
370      struct 
371     {
372         unsigned short nScale:2;    //Time Scale, 0:1ms 1:10ms 2:100ms 3:1S
373         unsigned short nType:1;        //0 : timer 1    :    counter ;
374         unsigned short nDir:1;        //0 : count down. 1 count up;
375         unsigned short nInited:1;
376         unsigned short bSet:1;
377         unsigned short bTon:1;
378         
379     };        
380 //        stTimerStat Stat;
381     };
382 }stTimer;
383
0fe6b0 384 #define TOTAL_WDFS (32)        //Total DF Count
Q 385 #define TOTAL_CurVAL (16)        //
386 #define TOTALTIMERS (64)
387
388 enum enKLDataCounts
389     {
390
391         KLDataWXCount = 16,
392         KLDataWYCount = 16,
393         KLDataWRCount = 16,
394         KLDataLDCount = 64,
395         KLDataWLCount = 8, 
396         
397         KLCoilXCount = KLDataWXCount * 16,
398         KLCoilYCount = KLDataWYCount * 16,
399         KLCoilRCount = KLDataWRCount * 16,
400
401         KLCoilTCount = 64,
402         KLCoilCCount = KLCoilTCount,
403
404         KLDataSVCount = KLCoilTCount,
405         KLDataEVCount = KLCoilTCount,
406
407         KLCoilLXCount = 128,
408         KLCoilLYCount = 128,
409         KLCoilLRCount = 128,
410         KLCoilSRCount = 128,
411
412         KLDataDTCount = 128,
413         KLDataSDTCount = 128,
414         KLDataWDTCount = 128,
415
416     };
417
bfc108 418 typedef struct tagKMem
Q 419 {
a7db3c 420     unsigned short WDFs[TOTAL_WDFS];
Q 421     unsigned char CurVALs[TOTAL_CurVAL];
422     unsigned char CurVAL;
423     stTimer Timers[TOTALTIMERS];
424
425     union {
0fe6b0 426     unsigned short WX[KLDataWXCount];        //本机的X和Y
a7db3c 427     unsigned char WXB[KLDataWXCount*2];    
Q 428     };
429     union {
0fe6b0 430     unsigned short WY[KLDataWYCount];        //本机的X和Y
Q 431     unsigned char WYB[KLDataWYCount*2];        //本机的X和Y
a7db3c 432     }; 
Q 433     unsigned short WR[KLDataWRCount];
434     
435     unsigned short WT[16];
436
437     unsigned short WC[16];
438     unsigned short EV[KLDataEVCount];
439     unsigned short SV[KLDataSVCount];
440     
441     
0fe6b0 442     unsigned short WLX[16];        //虚拟的X和Y,远程通讯时映射用。
bfc108 443     unsigned short WLY[16];
a7db3c 444     unsigned short WLR[16];    
bfc108 445     unsigned short WSR[16];
a7db3c 446
Q 447 union {
0fe6b0 448     unsigned int DTD[KLDataDTCount/2];
a7db3c 449     unsigned short DT[KLDataDTCount];
Q 450     unsigned char DTB[KLDataDTCount*2];
f4f290 451 };
a7db3c 452
0fe6b0 453     // 配置寄存器
Q 454     // 系统状态寄存器
455     // 特殊寄存器
456
bfc108 457     union {
a7db3c 458         unsigned int SDD[KLDataSDTCount/2];
Q 459         unsigned short SDT[KLDataSDTCount];
460         unsigned char SDB[KLDataSDTCount*2];
bfc108 461         struct {
Q 462             unsigned int EffJumperSW;
463             unsigned int CurJumperSW;
464             unsigned int haltick;
465             unsigned int nRunCount;
466             unsigned int RunStat;
467             unsigned int ErrStat;
468             unsigned int PwrOnCount;
469             unsigned int ThisRunTime;
470             unsigned int TotalRunTime;
471             unsigned int CurTimeSec;
472             unsigned int PwrFailCount;
473             unsigned int LastPwrFailTime;
474             unsigned int LastScanTime;
475             unsigned int ScanTimeuS;
476             unsigned int MinScanTimeuS;
477             unsigned int MaxScanTimeuS;
478             unsigned int nEventCount;
479             unsigned int nEventMinIndex;
480             unsigned int nEventMaxIndex;
481             unsigned int Rev2[5];
482             unsigned short ADCValues[20];
483         };
484     };
0fe6b0 485     // 调试,监控寄存器
Q 486     union {
487         unsigned int WDD[KLDataWDTCount/2];
488         unsigned short WDT[KLDataWDTCount];
489         unsigned char WDB[KLDataWDTCount*2];
490     };
491     
bfc108 492 }stKMem;
Q 493
494
495 extern stKMem KMem;
a7db3c 496 extern const stKMInfoBlock KMInfoBlock;
Q 497
498 extern volatile int PowerDownEvent;
499 extern volatile int OldPowerDownEvent;
500 extern volatile int OldPowerDownEventTime;
bfc108 501
Q 502 int KMachineInit(void);
a7db3c 503 int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize);
Q 504 int WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
505 int EraseAndWriteToFlashMem(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
bfc108 506
a7db3c 507 int ReadFactoryData(void * pDatabuf, int nByteCount);
Q 508 int WriteFactoryData(void * pDataBuf, int nByteCount);
509
510 // active Program bank
511 int ReadProgram(int nProgByteAddr, void *pBuf, int nByteSize, int nBank);
512 int WriteProgram(int nProgByteAddr, void * pBuf, int nByteSize, int nBank);
513
514 int WriteSysCfgToFlash(pStoredKMSysCfg theStoredKMSysCfg);
515 int ReadSysCfgFromFlash(pStoredKMSysCfg theStoredKMSysCfg);
bfc108 516
Q 517 int AddEventLog(uint32_t nTime, USHORT nEvent, USHORT nParam1, UINT nParam2);
518 pEventLog GetEventLogAddr(int nIndex);
519 int ClearEventLog(void);
520 int LoadRunStat(pRunStat theRunStat);
521 int SaveRunStat(pRunStat theRunStat);
522
a7db3c 523
Q 524 unsigned char GetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr);
525 int SetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr, unsigned char nCoilValue);
526 int GetVarData(int nDataType, int nDataAddr);
527 int SetVarData(int nDataType, int nDataAddr, int nDataValue);
528
bfc108 529 #endif    /* __KLPROTOCOL_H__ */