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