QuakeGod
2024-11-25 9aed5d7e7b3c7bf09da712e9c272ece401a7acc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/**
  ******************************************************************************
  * @file           : KMachine.h
  * @brief          : Header for KMachine.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
    */
#ifndef __KMACHINE_H__
#define __KMACHINE_H__
 
#define FACTORY_DATA_BASE (FLASH_BASE + 0x00007C00)        //31k //and 
//#define FACTORY_DATA_PAGESIZE (0x00000400)                        //Page Size = 1K
#define FACOTRY_DATA_PAGES 1                                                  // use 1 page(s)
 
#define STORE_PRG_BASE (FLASH_BASE + 0x00008000)        //32k //and FLASH_BANK1_END
//#define STORE_PRG_PAGESIZE (0x00000400)                            //Page Size = 1K
#define STORE_PRG_PAGES 8                                                    //use 4 pages
 
#define ALT_PRG_BASE (FLASH_BASE + 0x0000A000)        //40k //and FLASH_BANK1_END
//#define ALT_PRG_PAGESIZE (0x00000400)                            //Page Size = 1K
#define ALT_PRG_PAGES 8                                                    //use 4 pages
 
#define STORE_SYSREG_BASE (FLASH_BASE + 0x0000C000)        //48k //and 
//#define STORE_SYSREG_PAGESIZE (0x00000400)                            //Page Size = 1K
#define STORE_SYSREG_PAGES 1                                                        //use 1 pages
 
#define STORE_RUNSTAT_BASE (FLASH_BASE + 0x0000C800)        //50k K //and 
//#define STORE_RUNSTAT_PAGESIZE (0x00000400)                            //Page Size = 1K
#define STORE_RUNSTAT_PAGES 1                                                        //use 1 pages
 
#define STORE_PLC_ANNO_BASE (FLASH_BASE + 0x0000D000)    //56k 
#define STORE_PLC_ANNO_PAGES 4                                                // use 4 pages , 1K /page
 
 
#define STORE_LOG_BASE (FLASH_BASE + 0x0000E000)        //52k and FLASH_BANK1_END
//#define STORE_LOG_PAGESIZE (0x00000400)                            //Page Size = 1K
#define STORE_LOG_PAGES 4                                                        //use 4 pages
 
 
typedef unsigned char uchar;
typedef unsigned char UCHAR;
typedef unsigned short ushort;
typedef unsigned short USHORT;
typedef unsigned int UINT;
typedef unsigned int uint32_t;
typedef unsigned int DWORD;
typedef unsigned short WORD;
typedef unsigned char BYTE;
 
#define LoBofW(x) ((x)&0xff)
#define HiBofW(x) (((x)>>8)&0xff)
 
#define LoHofB(x) ((x)&0xf)
#define HiHofB(x) (((x)>>4)&0xf)
 
//  信息块
//  工厂参数配置块
//  用户/系统参数配置块
//  
//
#pragma anon_unions
typedef struct tagInfoBlock // 23 bytes
{
//    USHORT nBlockLenth;
    USHORT nDeviceTypeVer;            //device type        x.y
//    UCHAR nDevierVer;
    union {
        USHORT nProgVer;                    //prog version    x.y
        struct {
            UCHAR nProgVerMinor;
            UCHAR nProgVerMajor;
        };
    };
    USHORT KLinkVer;                //x.y
    USHORT KBusVer;                    //x.y
//    USHORT KNetVer;                    //x.y
//    USHORT KWLVer;                    //x.y
    
    UCHAR nCapacity1;                //    ? K
    UCHAR nCapacity2;                //    ? k
    
    UCHAR nDInput;
    UCHAR nDOutput;
    UCHAR nAInput;
    UCHAR nAOutput;
    UCHAR nHInput;
    UCHAR nHOutput;
    UCHAR nExt1;
    UCHAR nExt2;
    UCHAR nLogSize;
    UCHAR nPorts;
    UCHAR nManSize;
    UCHAR nAbility;
    UCHAR nSwitchBits;
    
}stKMInfoBlock;
 
/*
typedef struct tagFactData
{
    USHORT Sign;                    //
    USHORT nLength;                //
    UCHAR LOT_NO[16];            //
    UINT MANDate;                    //
    UINT SN;                            //
    UINT REV1[24];                //
    USHORT rev9;
    USHORT CRC;
    
}stFactData;
*/
typedef struct tagFactoryData        //工厂量产参数,数据
{
    USHORT Sign1;
    USHORT Seq1;
    USHORT nModelNo;
    USHORT nModelVer;
    UINT nLotNo;
     UINT nProductDateTime;
    UINT SN1;
    UINT nProtocalVer;
    UINT nDefaultFunc;
    UCHAR ModelStr[16];
    UCHAR LOT_NO[16];            //
    UCHAR SNStr[16];
    unsigned short CRC1;
    unsigned short EndSign1;
    
}stFactoryData,* pFactoryData;
 
typedef struct tagNewAppInfoBlock
{
    unsigned short Sign;
    unsigned short Version;
    unsigned int Length;
    unsigned int nCRC;
    
}stNewAppInfoBlock,*pNewAppInfoBlock;
 
enum enStoreCfg
{
    CFG_VER        = 0x100,
    START_SIGN = 0x55aa,
    END_SIGN    =    0x5aa5,
};
 
enum enInputFilter
{
    InputFilter_None =0,
    InputFilter_1mS =1,
    InputFilter_2mS =2,
    InputFilter_4mS =3,
    InputFilter_8mS =4,
    InputFilter_16mS =5,
    InputFilter_32mS =6,
    InputFilter_64mS =7,
    InputFilter_128mS =8,
    InputFilter_256mS =9,
};
 
enum enOutputHold
{
    Output_Hold = 0,
    Output_Set_0 = 1,
    Output_Set_1 = 2,
};
 
typedef struct tagPortStatus
{
    USHORT porttype;        //232,485,net , wireless// KLink, KBus, KNet, KWireless
    UCHAR StationID;
    UCHAR MaxStations;
    UCHAR CurStations;
    UCHAR Running;
    UCHAR Health;
}stPortStatus,*pPortStatus;
 
enum enPortType
{
    PortType_Default = 0, //默认
    PortType_KLink = 1, //KLink通讯
    PortType_KBus = 2,     //KBus通讯
    PortType_KNet = 3,     // KNet通讯
    PortType_ModbusRTU = 4, //Modbus RTU 通讯
    PortType_Com = 5,    //    计算机通讯
    PortType_Gen = 6,    //通用通讯,自由口
};
 
typedef struct tagComPortParam            //4 Bytes
{
    USHORT WorkMode;                /* 0-5=Default,KLink,KBus,KNet,RTU,Com,Gen */
    USHORT Station;                    /* 0=From jumper */
    USHORT BaudRate;       /* =*100 Baudrate at which running       */
  USHORT ByteSize:2;        /* 0-1=Number of bits/byte, 7-8    */
    USHORT Parity:4;                    /* 0-4=None,Odd,Even,Mark,Space    */
  USHORT StopBits:2;        /* 0,1,2 = 1, 1.5, 2               */
    USHORT EndType:2;                    /* 0=ByChar, 1= ByTime */
  USHORT EofChar:4;         /* 0,1,2 = None, CR, CR+LF, ETX;  End of character  */
    USHORT SofChar:2;                    /* 0,1,2 = None, STX */
    USHORT EndTime;                        /*(*0.01mS) */
    USHORT RecvAddr;
    USHORT RecvSize;
 
}stComPortParam, *pstComPortParam;
 
//每个模块有 0/1/2/3/4/5/6/7/8个 通讯port
//不限于 UART, 网口,无线,单总线等,都是port
//甚至可以有虚拟的port
 
typedef struct tagPortStat
{
    UCHAR PortType;            //端口工作模式
    UCHAR nStation;            //自己站号
    UCHAR bWorking;            //工作中
    UCHAR bBus;                    //总线?,,全双工?
    UCHAR bMaster;            //主机
    UCHAR nDevices;            //连接的设备数量//不包括自己 //device list;
    
}stPortStat,*pPortStat;
 
typedef struct tagKMFuncParam
{
    USHORT EnablePLC:1;        //使能内部PLC功能
    USHORT RunMode;                //工作模式
}stKMFuncParam;
 
// 输入输出地址映射
 
typedef struct tagInputFilterParam        // 1 Bytes
{
    BYTE Filter0:4;
    BYTE Filter1:4;
 
}stInputFilterParam;
 
typedef struct tagOutputHoldParam            //1 Bytes
{
    BYTE Hold1:4;
    BYTE Hold2:4;
}stOutputHoldParam;    
 
typedef struct tagOutMapping
{
            USHORT bitPos:4;
            USHORT byteAddr:8;
            USHORT type:4;    
}stOutMapping;
 
typedef struct tagCfgBlockInfo
{
    UCHAR nBlockType;
    UCHAR nBlockSize;
}stCfgBlockInfo;
 
typedef struct tagKMSysCfg        //120 Bytes total
{
    USHORT Version;                                        // SC0    // 2 Bytes
    USHORT workmode;                                    // SC1  // 2 Bytes 0=From jumper  
    USHORT SwitchFunc;                                // SC2  // 2 Bytes 
    USHORT nCfgBlockCount;                                            // 2 Bytes
    
    stComPortParam PortParams[2];                            // 8 Bytes
    stInputFilterParam InputParams[16];                //16 Bytes
    stOutputHoldParam OutputParams[16];                //16 Bytes
    
    union{
        USHORT value;                                        //12 Bytes //输出映射
        stOutMapping;
    }OutMappings[8];
 
    USHORT nProgBank;
    USHORT nProgSize;
    USHORT nAnnoSize;
    USHORT nCount;
    
    stCfgBlockInfo CfgBlockInfos[8];
    UINT cfgvar8;                                                            // 4 Bytes
    UINT cfgvar9;                                                            // 4 Bytes
    UINT cfgvar10;                                                        // 4 Bytes
//    UINT cfgvar11;                                                        // 4 Bytes
//    UINT cfgvar12;                                                        // 4 Bytes
//    UINT cfgvar13;                                                        // 4 Bytes
//    UINT cfgvar14;                                                        // 4 Bytes
//    UINT cfgvar15;                                                        // 4 Bytes
//    UINT cfgvar16;                                                        // 4 Bytes
//    UINT Space1[4];                                                        //16 Bytes
 
}stKMSysCfg,* pKMSysCfg;
 
typedef struct tagStoredKMSysCfg
{
    unsigned short Sign1;
    unsigned short Seq1;
    stKMSysCfg theKMSysCfg;
    unsigned short CRC1;
    unsigned short EndSign1;
}stStoredKMSysCfg,*pStoredKMSysCfg;
 
enum enKeventType
{
    EventTypeNone = 0,
    EventTypePowerUp = 1,
    EventTypePowerDown = 2,
    EventTypePowerRecover = 3,
    EventTypeConnected = 4,
    EventTypeLostCon = 5,
    EventTypeSetTime = 6,
    EventTypeSysCfg = 7,
    EventTypeProg = 8,
    EventTypeForce = 9,
    EventTypeClearEvent = 10,
    
    
    EventType
};
 
typedef struct tagEventLog
{
    unsigned short Sign1;
    unsigned short Seq1;
    unsigned int nTime;
    unsigned short nType;
    unsigned short nParam1;
    unsigned int nParam2;
}stEventLog,* pEventLog;
 
typedef struct WorkStatus
{
    unsigned short bPLCRunning:1;
    unsigned short bPLCError:1;
    
}stWorkStatus;
 
typedef struct tagRunStat
{
    unsigned short Sign1;
    unsigned short Seq1;
    volatile unsigned short PowerCount;    //
    volatile unsigned short Reserved1;
    volatile unsigned int UpTime;        //Seconds;
    volatile unsigned int UserData1;
    volatile unsigned short WorkMode;
    volatile unsigned short WorkMode2;
    volatile unsigned short nBinProgBank;
    volatile unsigned short nBinProgSize;
    unsigned short bLEDFlick;
    unsigned short Reserved2[1];
    unsigned short CRC1;
    unsigned short EndSign1;
}stRunStat, *pRunStat;
 
extern stRunStat KMRunStat;
 
//stStoreCfg * GetCurStoreCfgAddr(void );
//stStoreCfg * GetNextStoreCfgAddr(stStoreCfg * CurCfg );
 
extern stStoredKMSysCfg storedKMSysCfg;
 
#define TYPECOIL 0x00
#define TYPEDATA 0x80
 
    enum enKLCoilTypes
    {
        KLCoilTypeX = 0 | TYPECOIL ,        //X Input
        KLCoilTypeY = 1 | TYPECOIL,        //Y Output
        KLCoilTypeR = 2 | TYPECOIL,        //R register
        KLCoilTypeLX = 3 | TYPECOIL,        //Link register
        KLCoilTypeLY = 4 | TYPECOIL,        //Link register
        KLCoilTypeT = 5 | TYPECOIL,        //Timer
        KLCoilTypeC = 6 | TYPECOIL,        //Counter
        KLCoilTypeLR = 7 | TYPECOIL,        //Link register
        KLCoilTypeSR = 8 | TYPECOIL,        //Link register
 
    };
    enum enKLDataTypes
    {
        KLDataTypeDEC = 0 | TYPEDATA,
        KLDataTypeHEX = 1 | TYPEDATA,
        KLDataTypeFloat = 2 | TYPEDATA,
        KLDataTypeWX = 3 | TYPEDATA,
        KLDataTypeWY = 4 | TYPEDATA,
        KLDataTypeWR = 5 | TYPEDATA,
        KLDataTypeWLX = 6 | TYPEDATA,
        KLDataTypeWLY = 7 | TYPEDATA,
        KLDataTypeDT = 8 | TYPEDATA,
        KLDataTypeSDT = 9 | TYPEDATA,
        KLDataTypeWSR = 10 | TYPEDATA,
        KLDataTypeSV = 11 | TYPEDATA,
        KLDataTypeEV = 12 | TYPEDATA,
        KLDataTypeLD = 13 | TYPEDATA,
        KLDataSysCfg = 25 | TYPEDATA,
        KLDataTypeFlash = 33 | TYPEDATA,
        KLDataTypeWDT = 41 | TYPEDATA,                    //调试监控数据
        KLDataTypeKBD = 51 | TYPEDATA,                    //KBus 调试数据
        KLDataTypeKWLD = 52 | TYPEDATA,                    //无线调试数据
        KLDataTypeNewApp = 61 | TYPEDATA,                // New FirmWare
        KLDataTypeNewAppInfo = 62 | TYPEDATA,     // New Firmware Info Block;
        KLDataTypeTest = 254 | TYPEDATA,
    };
 
enum enKLDataCounts
    {
 
        KLDataWXCount = 16,
        KLDataWYCount = 16,
        KLDataWRCount = 16,
        KLDataLDCount = 64,
        KLDataWLCount = 8, 
        
        KLCoilXCount = KLDataWXCount * 16,
        KLCoilYCount = KLDataWYCount * 16,
        KLCoilRCount = KLDataWRCount * 16,
 
        KLCoilTCount = 64,
        KLCoilCCount = KLCoilTCount,
 
        KLDataSVCount = KLCoilTCount,
        KLDataEVCount = KLCoilTCount,
 
        KLCoilLXCount = 128,
        KLCoilLYCount = 128,
        KLCoilLRCount = 128,
        KLCoilSRCount = 128,
 
        KLDataDTCount = 128,
        KLDataSDTCount = 128,
        KLDataWDTCount = 128,
 
    };
 
 
// 通信端口, 注册到 KMachine 里, 统一管理
 
enum enPortHardType
{
    PortHardType_None    =    0,    //    空接口,无类型
    PortHardType_SOFT    =    1,    //    软件虚拟接口,可能是隧道等.
    PortHardType_UART    =    2,    //    串行接口
    PortHardType_RS232    =    3,    //    232接口
    PortHardType_RS485    =    4,    //    485接口
    PortHardType_SLP    =    5,    //    单总线接口
    PortHardType_RF        =    6,    //    无线接口
    PortHardType_LORA    =    7,    //    LoRa无线接口
    PortHardType_OPTI    =    8,    //    光纤接口
    PortHardType_ETH    =    9,    //    以太网接口
    PortHardType_WIFI    =    10,    //    WiFi接口
    PortHardType_BT        =    11,    //    以太网接口
 
};
enum enPortUseType
{
    PortUse_Default = 0,    //默认
    PortUse_Console = 1,    //控制台接口
    PortUse_KLink = 2,        //KLink通讯
    PortUse_KBus = 3,         //KBus通讯
    PortUse_KNet = 4,         // KNet通讯
    PortUse_SLP = 5,        // SLP单总线
    PortUse_KRF = 6,         // 无线通讯
    PortUse_ModbusRTU = 7, //Modbus RTU 通讯
    PortUse_ModbusTCP = 8, //Modbus TCP 通讯
    PortUse_Com = 9,        //    计算机通讯
    PortUse_Gen = 10,        //通用通讯,自由口
};
 
typedef struct tagPortAbility
{
    ushort ChildList:1;
    ushort AccessChild:1;
    ushort TranProg:1;
    ushort TranCfg:1;
    ushort Diag:1;
    ushort TranOutBandData:1;
    ushort Tunnel:1;
    ushort TranFirmware:1;
    ushort TranBlink:1;
 
}stPortAbility;
 
enum enServiceReqs
{
    ReqNone,
    ReqInit,
    ReqReset,
    ReqStop,
    ReqRun,
    ReqBlinkLED,
    ReqStartDiag,
    ReqStopDiag,
    ReqPortChildInfo,
    ReqPortChnInfo,
    ReqUpdateFirm,
    ReqUpdateFirmInfo,
    
    ReqTransFirmware,
    ReqTransCfg,
    ReqTransProg,
    ReqTransData,
    ReqTransBlink,
    ReqTransChild,
    ReqTransInfo,
    ReqTransOutBandData,
    ReqRead1Bit,
    ReqWrite1Bit,
    ReqReadBits,
    ReqWriteBits,
    ReqReadData,
    ReqWriteData,
    ReqRemoteTran,
    
};
 
// 各端口 互联 通信
typedef int (*CommFuncDef)(void * pInstance, int ReqId , int nParam1, int nParam2, void ** pData, unsigned short * len1);
typedef struct tagPortDef
{
    union {
        USHORT nPortType;
        struct {
            UCHAR nPortHardType;
            UCHAR nPortUseType;
        };
    };
    ushort ability;        //能力, 获取子机信息等。
    uchar bEnable;
    uchar bRunning;
    uchar StationId;
    uchar bMaster;
    uchar nMaxStations;
    uchar nCurStations;
    uchar nHealth;
    short PortConfigType;
    short PortConfigSize;
    short PortRunStatType;
    short PortRunStatSize;
    
    void * pInstance;                    //通信结构实体
    void * pPortConfig;
    void * pPortRunStatus;
    CommFuncDef ReqCommFunc;    //通信请求函数指针
    // func2 CommReq;
    
}stPortDef,* pstPortDef;
 
 
typedef struct tagPortPtDef
{
    void * pt;
    
}stPortPtDef,* pstPortPtDef;
 
typedef struct tagInterComm
{
    int nId;
    
}stInterComm;
 
typedef struct tagKMem
{
    union {
    unsigned short WX[KLDataWXCount];        //本机的X和Y
    unsigned char WXB[KLDataWXCount*2];    
    };
    union {
    unsigned short WY[KLDataWYCount];        //本机的X和Y
    unsigned char WYB[KLDataWYCount*2];        //本机的X和Y
    }; 
    unsigned short WR[KLDataWRCount];
    
    
    //虚拟的X和Y,远程通讯时映射用。
    union{
    ushort WLX[16];
    uchar WLXB[32];
    };
    union{
    ushort WLY[16];
    uchar WLYB[32];
    };
    unsigned short WFX[16];
    unsigned short WFY[16];
 
    unsigned short WLR[16];
    unsigned short WSR[16];
 
union {
    unsigned int DTD[KLDataDTCount/2];
    unsigned short DT[KLDataDTCount];
    unsigned char DTB[KLDataDTCount*2];
};
 
    // 配置寄存器
    // 系统状态寄存器
    // 特殊寄存器
    // 调试,监控寄存器
    union {
        unsigned int SDD[KLDataSDTCount/2];
        unsigned short SDT[KLDataSDTCount];
        unsigned char SDB[KLDataSDTCount*2];
        struct {
            unsigned int EffJumperSW;
            unsigned int CurJumperSW;
            unsigned int haltick;
            unsigned int nRunCount;
            unsigned int RunStat;
            unsigned int ErrStat;
            unsigned int PwrOnCount;
            unsigned int ThisRunTime;
            unsigned int TotalRunTime;
            unsigned int CurTimeSec;
            unsigned int PwrFailCount;
            unsigned int LastPwrFailTime;
            unsigned int LastScanTime;
            unsigned int ScanTimeuS;
            unsigned int MinScanTimeuS;
            unsigned int MaxScanTimeuS;
            unsigned int nEventCount;
            unsigned int nEventMinIndex;
            unsigned int nEventMaxIndex;
            unsigned int Rev2[5];
            unsigned short ADCValues[20];
        };
    };
    // 调试,监控寄存器
    union {
        unsigned int WDD[KLDataWDTCount/2];
        unsigned short WDT[KLDataWDTCount];
        unsigned char WDB[KLDataWDTCount*2];
    };
    
    int nTotalPorts;
    stPortDef * pPorts[8];
 
}stKMem;
 
 
 
extern stKMem KMem;
extern const stKMInfoBlock KMInfoBlock;
extern const stStoredKMSysCfg KMDefaultSysCfg;
 
extern volatile int PowerDownFlag;
extern volatile int OldPowerDownFlag;
extern volatile int OldPowerDownEventTime;
 
int KMachineInit(void);
int KMachineLoopProc(void);
 
/*
    向KMachine 注册 通讯端口,以便通讯端口之间的功能互通
*/
int KMRegisterPort(ushort nType,stPortDef * thePortParam);
 
int KMPortReqFunc(int nPortIndex,int nReqId, int nParam1, int nParam2, void ** pData, unsigned short * nlen1);
 
 
int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize);
int EraseAndWriteToFlashMem(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
int WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
int WriteToFlashAutoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize);
 
int ReadFactoryData(void * pDatabuf, int nByteCount);
int WriteFactoryData(void * pDataBuf, int nByteCount);
 
// active Program bank
 
int ReadPLCProgram(int nBank, int nProgByteAddr, void *pBuf, int nByteSize);
int StartPLCProgram(int nBank, int nByteSize, int nCRC);
int WritePLCProgram(int nBank, int nProgByteAddr, void * pBuf, int nByteSize);
int FinishiPLCProgram(int nBank, int nByteSize, int nCRC );
 
int ReadPLCAnno(int nType, int nProgByteAddr, void *pBuf, int nByteSize);
int StartPLCAnno(int nType, int nByteSize, int nCRC);
int WritePLCAnno(int nType, int nProgByteAddr, void * pBuf, int nByteSize);
int FinishiPLCAnno(int nType, int nByteSize, int nCRC );
 
int WriteSysCfgToFlash(pStoredKMSysCfg theStoredKMSysCfg);
int ReadSysCfgFromFlash(pStoredKMSysCfg theStoredKMSysCfg);
 
int KMRunService(int nSvrId, int nParam1, int nParam2, void **pData, unsigned short *nled1);
 
int CheckEventLog(void);
int AddEventLog(uint32_t nTime, USHORT nEvent, USHORT nParam1, UINT nParam2);
pEventLog GetEventLogAddr(int nIndex);
int ClearEventLog(void);
int LoadRunStat(pRunStat theRunStat);
int SaveRunStat(pRunStat theRunStat);
 
int WriteNewApp(int nProgByteAddr, void *pBuf, int nByteSize);
int WriteNewAppInfo(int nProgByteAddr, void *pBuf, int nByteSize);
 
unsigned char GetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr);
int SetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr, unsigned char nCoilValue);
int GetVarData(int nDataType, int nDataAddr);
int SetVarData(int nDataType, int nDataAddr, int nDataValue);
 
typedef int (*KMSvCBDef) (int nChn, int nSvType, int ,void *, int);    //Service Req callback func ,param is  int ,int, int, void *,int; return int;
 
 
#endif    /* __KMACHINE_H__ */