QuakeGod
2022-10-17 85d591d38d84b632d5531bfeed761a40bf887116
提交 | 用户 | 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
85d591 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
85d591 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
85d591 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
85d591 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
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
bfc108 75 enum enStoreCfg
Q 76 {
85d591 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 {
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
85d591 114 //每个模块有 0/1/2/3/4/5/6/7/8个 通讯port
Q 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
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 };
85d591 146
bfc108 147 typedef struct tagKMFuncParam
Q 148 {
149     USHORT EnablePLC:1;        //使能内部PLC功能
150     USHORT RunMode;                //工作模式
151 }stKMFuncParam;
152
153 // 输入输出地址映射
154
85d591 155 typedef struct tagComPortParam            //4 Bytes
bfc108 156 {
85d591 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               */
85d591 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 */
85d591 167     USHORT EndTime;
Q 168     USHORT RecvAddr;
169     USHORT RecvSize;
170     
bfc108 171 }stComPortParam;
Q 172
85d591 173 typedef struct tagInputFilterParam        // 1 Bytes
bfc108 174 {
Q 175     BYTE Filter0:4;
176     BYTE Filter1:4;
177
178 }stInputFilterParam;
179
85d591 180 typedef struct tagOutputHoldParam            //1 Bytes
bfc108 181 {
Q 182     BYTE Hold1:4;
183     BYTE Hold2:4;
184 }stOutputHoldParam;    
185
186 #pragma anon_unions
85d591 187 typedef struct tagKMSysCfg        //120 Bytes total
bfc108 188 {
85d591 189     USHORT Version;                                        // SC0    // 2 Bytes
Q 190     USHORT workmode;                                    // SC1  // 2 Bytes 0=From jumper  
191     USHORT SwitchFunc;                                // SC2  // 2 Bytes 
bfc108 192     
85d591 193     USHORT OutMappings[6];                                        //12 Bytes //输出映射
Q 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
bfc108 214
Q 215 }stKMSysCfg,* pKMSysCfg;
216
85d591 217 typedef struct tagStoredKMSysCfg
bfc108 218 {
Q 219     unsigned short Sign1;
220     unsigned short Seq1;
85d591 221     stKMSysCfg theKMSysCfg;
Q 222     unsigned short CRC1;
223     unsigned short EndSign1;
224 }stStoredKMSysCfg,*pStoredKMSysCfg;
225
226
227 /*
228 typedef struct tagFactData
229 {
230     USHORT Sign;                    //
231     USHORT nLength;                //
232     UCHAR LOT_NO[16];            //
233     UINT MANDate;                    //
234     UINT SN;                            //
235     UINT REV1[24];                //
236     USHORT rev9;
237     USHORT CRC;
238     
239 }stFactData;
240 */
241 typedef struct tagFactoryData        //工厂量产参数,数据
242 {
243     USHORT Sign1;
244     USHORT Seq1;
245     USHORT nModelNo;
246     USHORT nModelVer;
247     UINT nLotNo;
248      UINT nProductDateTime;
249     UINT SN1;
250     UINT nProtocalVer;
251     UINT nDefaultFunc;
252     UCHAR ModelStr[16];
253     UCHAR LOT_NO[16];            //
254     UCHAR SNStr[16];
bfc108 255     unsigned short CRC1;
Q 256     unsigned short EndSign1;
257     
258 }stFactoryData,* pFactoryData;
259
260 typedef struct tagEventLog
261 {
262     unsigned short Sign1;
263     unsigned short Seq1;
264     unsigned int nTime;
265     unsigned short nType;
266     unsigned short nParam1;
267     unsigned int nParam2;
268 }stEventLog,* pEventLog;
269
270 typedef struct tagRunStat
271 {
272     unsigned short Sign1;
273     unsigned short Seq1;
85d591 274     volatile unsigned short PowerCount;    //
Q 275     volatile unsigned short Reserved1;
276     volatile unsigned int UpTime;        //Seconds;
277     volatile unsigned int UserData1;
278     volatile unsigned short WorkMode;
279     volatile unsigned short WorkMode2;
280     volatile unsigned short nBinProgBank;
281     volatile unsigned short nBinProgSize;
282     unsigned int Reserved2[1];
bfc108 283     unsigned short CRC1;
Q 284     unsigned short EndSign1;
285 }stRunStat, *pRunStat;
286
287 extern stRunStat KMRunStat;
288
289 //stStoreCfg * GetCurStoreCfgAddr(void );
290 //stStoreCfg * GetNextStoreCfgAddr(stStoreCfg * CurCfg );
291
85d591 292 extern stStoredKMSysCfg storedKMSysCfg;
Q 293
294 #define TYPECOIL 0x00
295 #define TYPEDATA 0x80
296
297     enum enKLCoilTypes
298     {
299         KLCoilTypeX = 0 | TYPECOIL ,        //X Input
300         KLCoilTypeY = 1 | TYPECOIL,        //Y Output
301         KLCoilTypeR = 2 | TYPECOIL,        //R register
302         KLCoilTypeLX = 3 | TYPECOIL,        //Link register
303         KLCoilTypeLY = 4 | TYPECOIL,        //Link register
304         KLCoilTypeT = 5 | TYPECOIL,        //Timer
305         KLCoilTypeC = 6 | TYPECOIL,        //Counter
306         KLCoilTypeLR = 7 | TYPECOIL,        //Link register
307         KLCoilTypeSR = 8 | TYPECOIL,        //Link register
308
309     };
310     enum enKLDataTypes
311     {
312         KLDataTypeDEC = 0 | TYPEDATA,
313         KLDataTypeHEX = 1 | TYPEDATA,
314         KLDataTypeFloat = 2 | TYPEDATA,
315         KLDataTypeWX = 3 | TYPEDATA,
316         KLDataTypeWY = 4 | TYPEDATA,
317         KLDataTypeWR = 5 | TYPEDATA,
318         KLDataTypeWLX = 6 | TYPEDATA,
319         KLDataTypeWLY = 7 | TYPEDATA,
320         KLDataTypeDT = 8 | TYPEDATA,
321         KLDataTypeSDT = 9 | TYPEDATA,
322         KLDataTypeWSR = 10 | TYPEDATA,
323         KLDataTypeSV = 11 | TYPEDATA,
324         KLDataTypeEV = 12 | TYPEDATA,
325         KLDataTypeLD = 13 | TYPEDATA,
326         KLDataSysCfg = 25 | TYPEDATA,
327         KLDataTypeFlash = 33 | TYPEDATA,
328         KLDataTypeTest = 254 | TYPEDATA,
329     };
330     
331 enum enKLDataCounts
332     {
333         KLDataDTCount = 256,
334         KLDataSDTCount = 256,
335
336         KLDataWXCount = 16,
337         KLDataWYCount = 16,
338         KLDataWRCount = 16,
339         KLDataLDCount = 64,
340         KLDataWLCount = 8, 
341         
342         KLCoilXCount = KLDataWXCount * 16,
343         KLCoilYCount = KLDataWYCount * 16,
344         KLCoilRCount = KLDataWRCount * 16,
345
346         KLCoilTCount = 64,
347         KLCoilCCount = KLCoilTCount,
348
349         KLDataSVCount = KLCoilTCount,
350         KLDataEVCount = KLCoilTCount,
351
352         KLCoilLXCount = 128,
353         KLCoilLYCount = 128,
354         KLCoilLRCount = 128,
355         KLCoilSRCount = 128,
356
357     };
358 #define TOTAL_WDFS (16)        //Total DF Count
359 #define TOTAL_CurVAL (16)        //
360 #define TOTALTIMERS (64)
361
362 typedef struct tagTimerStat
363 {
364     unsigned short nScale:2;//Time Scale, 0:1ms 1:10ms 2:100ms 3:1S
365     unsigned short nType:1;    //0 : timer 1:counter ;
366     unsigned short nDir:1;        //0 : count down. 1 count up;
367     unsigned short nInited:1;
368     unsigned short bSet:1;
369     unsigned short bTon:1;
370     
371 }stTimerStat;
372
373 typedef struct tagTimer
374 {
375     unsigned int LastActTime;
376     union {
377         unsigned short StatByte;
378      struct 
379     {
380         unsigned short nScale:2;    //Time Scale, 0:1ms 1:10ms 2:100ms 3:1S
381         unsigned short nType:1;        //0 : timer 1    :    counter ;
382         unsigned short nDir:1;        //0 : count down. 1 count up;
383         unsigned short nInited:1;
384         unsigned short bSet:1;
385         unsigned short bTon:1;
386         
387     };        
388 //        stTimerStat Stat;
389     };
390 }stTimer;
391
bfc108 392 typedef struct tagKMem
Q 393 {
85d591 394     unsigned short WDFs[TOTAL_WDFS];
Q 395     unsigned char CurVALs[TOTAL_CurVAL];
396     unsigned char CurVAL;
397     stTimer Timers[TOTALTIMERS];
398
399     union {
400     unsigned short WX[KLDataWXCount];        //本机的X和Y
401     unsigned char WXB[KLDataWXCount*2];    
402     };
403     union {
404     unsigned short WY[KLDataWYCount];        //本机的X和Y
405     unsigned char WYB[KLDataWYCount*2];        //本机的X和Y
406     }; 
407     unsigned short WR[KLDataWRCount];
408     
409     unsigned short WT[16];
410
411     unsigned short WC[16];
412     unsigned short EV[KLDataEVCount];
413     unsigned short SV[KLDataSVCount];
414     
415     
bfc108 416     unsigned short WLX[16];        //虚拟的X和Y,远程通讯时映射用。
Q 417     unsigned short WLY[16];
85d591 418     unsigned short WLR[16];    
bfc108 419     unsigned short WSR[16];
85d591 420
Q 421 union {
422     unsigned int DTD[KLDataDTCount];
423     unsigned short DT[KLDataDTCount];
424     unsigned char DTB[KLDataDTCount*2];
425 };    
426
bfc108 427     // 配置寄存器
Q 428     // 系统状态寄存器
429     // 特殊寄存器
430     // 调试,监控寄存器
431     union {
85d591 432         unsigned int SDD[KLDataSDTCount/2];
Q 433         unsigned short SDT[KLDataSDTCount];
434         unsigned char SDB[KLDataSDTCount*2];
bfc108 435         struct {
Q 436             unsigned int EffJumperSW;
437             unsigned int CurJumperSW;
438             unsigned int haltick;
439             unsigned int nRunCount;
440             unsigned int RunStat;
441             unsigned int ErrStat;
442             unsigned int PwrOnCount;
443             unsigned int ThisRunTime;
444             unsigned int TotalRunTime;
445             unsigned int CurTimeSec;
446             unsigned int PwrFailCount;
447             unsigned int LastPwrFailTime;
448             unsigned int LastScanTime;
449             unsigned int ScanTimeuS;
450             unsigned int MinScanTimeuS;
451             unsigned int MaxScanTimeuS;
452             unsigned int nEventCount;
453             unsigned int nEventMinIndex;
454             unsigned int nEventMaxIndex;
455             unsigned int Rev2[5];
456             unsigned short ADCValues[20];
457         };
458     };
459 }stKMem;
460
461
462 extern stKMem KMem;
85d591 463 extern const stKMInfoBlock KMInfoBlock;
Q 464
465 extern volatile int PowerDownEvent;
466 extern volatile int OldPowerDownEvent;
467 extern volatile int OldPowerDownEventTime;
bfc108 468
Q 469 int KMachineInit(void);
85d591 470 int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize);
Q 471 int WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
472 int EraseAndWriteToFlashMem(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
bfc108 473
85d591 474 int ReadFactoryData(void * pDatabuf, int nByteCount);
Q 475 int WriteFactoryData(void * pDataBuf, int nByteCount);
476
477 // active Program bank
478 int ReadProgram(int nProgByteAddr, void *pBuf, int nByteSize, int nBank);
479 int WriteProgram(int nProgByteAddr, void * pBuf, int nByteSize, int nBank);
480
481 int WriteSysCfgToFlash(pStoredKMSysCfg theStoredKMSysCfg);
482 int ReadSysCfgFromFlash(pStoredKMSysCfg theStoredKMSysCfg);
bfc108 483
Q 484 int AddEventLog(uint32_t nTime, USHORT nEvent, USHORT nParam1, UINT nParam2);
485 pEventLog GetEventLogAddr(int nIndex);
486 int ClearEventLog(void);
487 int LoadRunStat(pRunStat theRunStat);
488 int SaveRunStat(pRunStat theRunStat);
489
85d591 490
Q 491 unsigned char GetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr);
492 int SetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr, unsigned char nCoilValue);
493 int GetVarData(int nDataType, int nDataAddr);
494 int SetVarData(int nDataType, int nDataAddr, int nDataValue);
495
bfc108 496 #endif    /* __KLPROTOCOL_H__ */