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