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