QuakeGod
2023-10-08 483170e190a0dd4666b2a63e5d31466052ba0c6a
提交 | 用户 | age
483170 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
19 #define ALT_PRG_BASE (FLASH_BASE + 0x0000A000)        //36k //and FLASH_BANK1_END
20 #define ALT_PRG_PAGESIZE (0x00000400)                            //Page Size = 1K
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
27 #define STORE_RUNSTAT_BASE (FLASH_BASE + 0x0000A800)        //44k K //and 
28 #define STORE_RUNSTAT_PAGESIZE (0x00000400)                            //Page Size = 1K
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
35
36
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
46 #define LoBofW(x) ((x)&0xff)
47 #define HiBofW(x) (((x)>>8)&0xff)
48
49 #define LoHofB(x) ((x)&0xf)
50 #define HiHofB(x) (((x)>>4)&0xf)
51
52 //  信息块
53 //  工厂参数配置块
54 //  用户/系统参数配置块
55 //  
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
75 enum enStoreCfg
76 {
77     CFG_VER        = 0x100,
78     START_SIGN = 0x55aa,
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 {
106     PortType_Com = 0,    //计算机通讯
107     PortType_Gen = 1,    //通用通讯,自由口
108     PortType_KLink = 2, //KlinkͨѶ
109     PortType_KBus = 3,     //KBusͨѶ
110     PortType_KNet = 4,     // KNetͨѶ
111     PortType_ModbusRTU = 5, //Modbus RTU ͨѶ
112 };
113
114 //每个模块有 0/1/2/3/4/5/6/7/8个 通讯port
115 //不限于 UART, 网口,无线,单总线等,都是port
116 //甚至可以有虚拟的port
117
118 typedef struct tagPortStat
119 {
120     UCHAR nWorking;            //工作中
121     UCHAR nStation;            //自己站号
122     UCHAR bBus;                    //总线?,,全双工?
123     UCHAR bMaster;            //主机
124     UCHAR PortType;            //端口工作模式
125     UCHAR nDevices;            //连接的设备数量//不包括自己 //device list;
126     
127 }stPortStat,*pPortStat;
128
129 enum enKeventType
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 };
146
147 typedef struct tagKMFuncParam
148 {
149     USHORT EnablePLC:1;        //使能内部PLC功能
150     USHORT RunMode;                //工作模式
151 }stKMFuncParam;
152
153 // 输入输出地址映射
154
155 typedef struct tagComPortParam            //4 Bytes
156 {
157     USHORT Station;                    /* 0=From jumper */
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 */
161   USHORT ByteSize:2;        /* 0-1=Number of bits/byte, 7-8    */
162     USHORT Parity:4;                    /* 0-4=None,Odd,Even,Mark,Space    */
163   USHORT StopBits:2;        /* 0,1,2 = 1, 1.5, 2               */
164     USHORT EndType:2;                    /* 0=ByChar, 1= ByTime */
165   USHORT EofChar:4;         /* 0,1,2 = None, CR, CR+LF, ETX;  End of character  */
166     USHORT SofChar:2;                    /* 0,1,2 = None, STX */
167     USHORT EndTime;
168     USHORT RecvAddr;
169     USHORT RecvSize;
170     
171 }stComPortParam;
172
173 typedef struct tagInputFilterParam        // 1 Bytes
174 {
175     BYTE Filter0:4;
176     BYTE Filter1:4;
177
178 }stInputFilterParam;
179
180 typedef struct tagOutputHoldParam            //1 Bytes
181 {
182     BYTE Hold1:4;
183     BYTE Hold2:4;
184 }stOutputHoldParam;    
185
186 #pragma anon_unions
187 typedef struct tagKMSysCfg        //120 Bytes total
188 {
189     USHORT Version;                                        // SC0    // 2 Bytes
190     USHORT workmode;                                    // SC1  // 2 Bytes 0=From jumper  
191     USHORT SwitchFunc;                                // SC2  // 2 Bytes 
192     
193     USHORT OutMappings[6];                                        //12 Bytes //输出映射
194     
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
214
215 }stKMSysCfg,* pKMSysCfg;
216
217 typedef struct tagStoredKMSysCfg
218 {
219     unsigned short Sign1;
220     unsigned short Seq1;
221     stKMSysCfg theKMSysCfg;
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 */
240 typedef struct tagFactoryData        //工厂量产参数,数据
241 {
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];
254     unsigned short CRC1;
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;
273     volatile unsigned short PowerCount;    //
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];
282     unsigned short CRC1;
283     unsigned short EndSign1;
284 }stRunStat, *pRunStat;
285
286 extern stRunStat KMRunStat;
287
288 //stStoreCfg * GetCurStoreCfgAddr(void );
289 //stStoreCfg * GetNextStoreCfgAddr(stStoreCfg * CurCfg );
290
291 extern stStoredKMSysCfg storedKMSysCfg;
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
391 typedef struct tagKMem
392 {
393     unsigned short WDFs[TOTAL_WDFS];
394     unsigned char CurVALs[TOTAL_CurVAL];
395     unsigned char CurVAL;
396     stTimer Timers[TOTALTIMERS];
397
398     union {
399     unsigned short WX[KLDataWXCount];        //本机的X和Y
400     unsigned char WXB[KLDataWXCount*2];    
401     };
402     union {
403     unsigned short WY[KLDataWYCount];        //本机的X和Y
404     unsigned char WYB[KLDataWYCount*2];        //本机的X和Y
405     }; 
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     
415     unsigned short WLX[16];        //虚拟的X和Y,远程通讯时映射用。
416     unsigned short WLY[16];
417     unsigned short WLR[16];    
418     unsigned short WSR[16];
419
420 union {
421     unsigned int DTD[KLDataDTCount];
422     unsigned short DT[KLDataDTCount];
423     unsigned char DTB[KLDataDTCount*2];
424 };
425
426     // 配置寄存器
427     // 系统状态寄存器
428     // 特殊寄存器
429     // 调试,监控寄存器
430     union {
431         unsigned int SDD[KLDataSDTCount/2];
432         unsigned short SDT[KLDataSDTCount];
433         unsigned char SDB[KLDataSDTCount*2];
434         struct {
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;
462 extern const stKMInfoBlock KMInfoBlock;
463
464 extern volatile int PowerDownEvent;
465 extern volatile int OldPowerDownEvent;
466 extern volatile int OldPowerDownEventTime;
467
468 int KMachineInit(void);
469 int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize);
470 int WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
471 int EraseAndWriteToFlashMem(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
472
473 int ReadFactoryData(void * pDatabuf, int nByteCount);
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);
482
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
489
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
495 #endif    /* __KLPROTOCOL_H__ */