-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSYS.pas
More file actions
1559 lines (1340 loc) · 55 KB
/
Copy pathSYS.pas
File metadata and controls
1559 lines (1340 loc) · 55 KB
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Interface to SYS (Processor Hardware and Runtime System)
// Date 28.05.22
// 25.07.17 nk opt for XE3 (AnsiString <-> string)
// 25.07.17 nk opt use string instead of ShortString (e.g. old=string[MAXBUFF])
unit SYS;
interface
uses
Forms, Windows, Messages, Types, Controls, Graphics, SysUtils, ExtCtrls,
StrUtils, Classes, Math, MMSystem, USystem, UFile, Global, Data, FLog;
const
COMPILER_NAME = 'Embarcadero Delphi'; // 26.07.17 nk old=Borland
COMPILER_VERS = CompilerVersion;
DEVMAX = 19; // number of supported devices
RUN = 0; // processor runtime system
LAB = 1; // plug and play lab device number
PRN = 2; // printer device number
RTC = 3; // real time clock device number
CNT = 4; // counter device number
SER = 5; // serial port device number
ADC = 6; // A/D converter device number
ADF = 7; // A/D fast converter device number
ENC = 8; // encoder device number
PWM = 9; // pulse width modulation device number
LCD = 10; // graphic display device number
FRQ = 11; // frequency counter device number
PLI = 12; // pulse input device number
GEN = 13; // pulse generator device number
TIM = 14; // timer device number
PAR = 15; // parallel input device number
TCH = 16; // touch memory device number
MF2 = 17; // MF2 keyboard device number
PS2 = 18; // PS/2 device number
RUNDRV = 'RUN.SYS'; // processor runtime system
LABDRV = 'LAB.TDD'; // plug and play lab device driver
PRNDRV = 'PRN.TDD'; // printer device driver
RTCDRV = 'RTC.TDD'; // real time clock driver
CNTDRV = 'CNT.TDD'; // counter device driver
SERDRV = 'SER.TDD'; // serial port driver
ADCDRV = 'ADC.TDD'; // A/D converter driver
ADFDRV = 'ADF.TDD'; // A/D fast converter driver
ENCDRV = 'ENC.TDD'; // encoder device driver
PWMDRV = 'PWM.TDD'; // pulse modulation driver
LCDDRV = 'LCD.TDD'; // display device driver
FRQDRV = 'FRQ.TDD'; // frequency meter driver
PLIDRV = 'PLI.TDD'; // pulse in device driver
GENDRV = 'GEN.TDD'; // pulse generator device driver
TIMDRV = 'TIM.TDD'; // timer device driver
PARDRV = 'PAR.TDD'; // parallel port driver
TCHDRV = 'TCH.TDD'; // touch memory driver
MF2DRV = 'MF2.TDD'; // MF2 keyboard driver
PS2DRV = 'PS2.TDD'; // PS/2 device driver
MAXBUFF = 240; // max lenght of message buffers [bytes]
MAXTEMP = 64; // max lenght of temporary buffers [bytes]
MAXSYS = 32; // max lenght of system strings [bytes]
MAXTIG = 8; // max lenght of Tiger strings [bytes]
MAXERR = 64; // max lenght of error strings [bytes]
FREQDIV = 255; // timer max frequency divider
FREQMIN = 610; // timer min frequency (610Hz)
FREQMAX = 25000; // timer max frequency (25kHz)
FREQRES = 400; // timer max resolution [ns]
TIGASCII = 42; // convert HEX->ASCII (A=k, B=l.. Tiger)
HEXASCII = 49; // convert HEX->ASCII (0=a, 1=b.. standard)
cLO = 0; // set pin to low level
cHI = 255; // set pin to high level
DIROUT = 0; // set pin as output
DIRIN = 1; // set pin as input
ALLPIN = 255; // all pins of a port
ACT = 1; // user eport parameter
ACTIVE = 1; // - eport active
NOACTIVE = 0; // - eport not used
RAMTEST = 101; // options for RAM test
SYSTEST = 102; // options for SYS test
PWRTEST = 103; // options for PWR start up delay (1000=1sec)
MEMTEST = 104; // options for MEM test
SYSMEM = 131072; // system memory segment=128kB
SYSMINSTACK = 512; // min stack size=512bytes
SYSMAXSTACK = 8192; // max stack size=8kbytes
TESTNO = 0; // no test
TESTMIN = 1; // minimal (fragmented) test
TESTFAST = 2; // fast (standard) test
TESTFULL = 3; // full (intensive) test
cKEYNONE = 0; // key return codes - no key pressed
cKEYUP = 1; // - up key pressed
cKEYRIGHT = 2; // - right key pressed
cKEYDOWN = 3; // - down key pressed
cKEYLEFT = 4; // - left key pressed
TONESHORT = 50; // short audio tone (50ms)
TONELONG = 250; // long audio tone (250ms)
TONEBELL = 251; // bell audio tones (3*50ms)
TONERING = 252; // ring audio tones (4*40ms)
TONEALARM = 253; // alarm audio tones (5*30ms)
TONEERROR = 254; // error audio tones (6*20ms) 24.05.07 nk add
TONEFREQ = 2500; // DELPHI tone frequency [Hz]
TONEON = 30000; // DELPHI set audio tone on (max. 30s)
TONEOFF = 0; // DELPHI set audio tone off
GENPIN = 86; // GEN output pin (P86 fix defined in GEN.tdd)
GENSTART = 0; // GEN start pulse out (endless)
GENSTOP = -1; // GEN stop pulse out (not interrupted)
GENSETRANGE = $090; // set pulse frequency range (1..3)
GENSETPULSE = $091; // set generator number of pulses (-1=stop, 0=endless)
GENGETPULSE = $0B0; // get generator rest of pulses (-1=stopped)
MEMSTART = 0; // flash memory start address
MEMBYTE = 1; // flash memory cell size=1byte
MEMMODEEMPTY = 0; // flash cells must be empty to write
MEMUNDEF = -1; // undefined flash address
MEMBLOCKNUM = 2; // number of flash memory block buffers
MEMLOGDEL = 500; // log delay time before clear flash (500ms)
MEMBLOCKLEN = 16; // flash memory block buffer length=16words
MEMBLOCKSIZE = 32; // flash data block size (16x2bytes=32bytes)
MEMSECTSIZE = 65536; // flash sector size = 65'536bytes = 64kB 01.06.07 nk mov
MEMERRNONE = 0; // clear flash error (>0=write error address)
MEMERRSOURCE = -1; // source variable is too small
MEMERREOF = -2; // end of flash memory reached
MEMERRBYTES = -3; // no source bytes
MEMERRADDR = -4; // address out of flash memory range
MEMERRPARAM = -16; // wrong parameter
MEMERRPOKEM = -17; // pokem_flash not supported
MEMERRSECTOR = -21; // invalid flash sector size
MEMERRLITTLE = -22; // too little flash memory
MEMERRNOSECT = -23; // unsupported flash sector
MEMERRERASE = -24; // flash memory erasing failed
MEMERRREAD = -25; // flash memory reading failed
MEMERRWRITE = -26; // flash memory writing failed
MEMEMPTYBYTE = $0FF; // empty flash cell (1byte)
MEMEMPTYWORD = $0FFFF; // empty flash word (2bytes)
I2CERROR = $0F0; // I2C error code limit (I2C_result)
I2CDATALEN = 1; // I2C data lenght = 1byte
I2CNAKLIMIT = 3; // I2C NAK conditions limit (I2C_write)
I2CISO = 7816; // ISO 7816 mode for I2C bus (LSB first)
SERIALBYTES = 128; // flash memory size for serial number [bytes]
SERIALLEN = 8; // serial number length
SERIALNONE = 'FFFFFFFF'; // undefined serial number
SERIALTEXT = 'S/N';
SERIALFORM = '%*.*X'; // serial number format (HEX)
ERRFORM = '%.2X'; // error code format (HEX)
VERSFORM = '%.2X'; // version format (HEX)
ADDRFORM = '%.3Xh'; // address format (HEX)
SPACE = ' '; // 28.05.22 nk add
VERSTEXT = ' Vers.';
TIMUNIT = ' Hz'; // TIM standard unit
GENUNIT = ' ns'; // GEN standard unit
MEMUNIT = ' bytes'; // MEM standard unit
// definitions for system internals
SYSNOFUNCT = $00; // (rw) inactive - no function
SYSCLEARIBUF = $01; // ( w) clear input buffer
SYSCLEAROBUF = $21; // ( w) clear output buffer
SYSRESETERROR = $41; // ( w) reset last error code
SYSRESETACTION = $42; // ( w) reset last action code
SYSRESETLOST = $43; // ( w) reset last lost data code
SYSGETIBUFFILL = $01; // (r ) input buffer fill [bytes]
SYSGETIBUFFREE = $02; // (r ) input buffer free space [bytes]
SYSGETIBUFSIZE = $03; // (r ) input buffer size [bytes]
SYSGETOBUFFILL = $21; // (r ) output buffer fill [bytes]
SYSGETOBUFFREE = $22; // (r ) output buffer free space [bytes]
SYSGETOBUFSIZE = $23; // (r ) output buffer size [bytes]
SYSGETERROR = $41; // (r ) last error code
SYSGETACTION = $42; // (r ) last action code
SYSGETLOST = $43; // (r ) last lost data code
SYSGETCPULOAD = $44; // (r ) cpu load (10000=100%)
SYSGETDEVVERS = $63; // (r ) device driver version (106Dh=1.06n)
// return codes from SYSVARN and SYSVAR$
SYSGETTASKERROR = 0; // last runtime error in task
SYSGETERRORCOUNT = 1; // runtime error counter
SYSGETERRORFLAG = 2; // actual error flag (0=ok, 1=error)
SYSGETERRORCODE = 15; // actual error code (reset flag)
SYSGETSTACKLEVEL = 16; // stack level
SYSGETSTACKFILL = 17; // stack fill [bytes]
SYSGETSTACKFREE = 18; // stack free space [bytes]
SYSGETSTACKSIZE = 19; // stack size [bytes]
SYSGETDRAMSIZE = 30; // 0=size, 1=prog free, 2=prog size, 3=free
SYSGETSRAMSIZE = 31; // 0=size, 1=prog free, 2=prog size, 3=free
SYSGETMEMCHIPNUM = 32; // number of flash chips
SYSGETMEMCHIPSIZE = 33; // size of flash chips
SYSGETMEMCHIPSECT = 34; // number of sectors per chip
SYSGETMEMSECTSIZE = 35; // size of flash sectors
SYSGETMEMASECTNUM = 36; // number of flash sectors
SYSGETMEMSIZE = 37; // size of flash memory [bytes]
SYSGETMEMUSERSECT = 38; // number of flash sectors for user data
SYSGETMEMUSERSIZE = 39; // size of flash memory for user data
SYSGETMEMMODE = 40; // flash mode (0=program must wait, 1=program can run)
SYSGETTASKACTNUM = 48; // number of running task
SYSGETTASKACTPRIO = 49; // priority of running task
SYSGETTASKALLPRIO = 50; // total priority of all tasks
SYSGETTASKALLNUM = 51; // total tasks in program
SYSGETTASKALLACTIVE = 52; // total activated tasks
SYSGETBOOTMODE = 65; // 0=reset, 1=watchdog, 2=basic error, 3=system error
SYSGETSYSSPEED = 66; // 0=full speed, 1=low power mode, 2=ultra low power mode
SYSGETPROCMODE = 67; // 0=RUN mode, 1=PC mode (debug mode)
SYSGETPROCVERS = 68; // version of processor module
SYSGETPROCTYPE = 69; // type of processor module (131='T', 170='A')
SYSGETWDOGSTATE = 70; // actual watchdog state (0..6)
SYSGETWDOGMAX = 71; // maximal watchdog state (0..6)
// ErrCode decimal coded - LogError shows HEX value, eg:
// ErrorHandler: ERROR 22: Gerätefehler in task Runtime System
// InitRtc: ERROR 9A: RTC driver installation failed - return 0
// ErrorHandler: ERROR 23: Hier kein PUBLIC-Zugriff in task User Interface
SYSNOERR = 0; // clear error code
SYSTASKAVAILERR = 7; // task number not availlable
SYSSTRINGERR = 8; // string too long
SYSPARAMETERERR = 11; // illegal parameter
SYSCHANNELERR = 32; // device channel error
SYSPORTADRERR = 33; // port address error
SYSDEVICEERR = 34; // device hardware error
SYSPINUSEDERR = 35; // pin already in use
SYSNODEVICEERR = 38; // device driver not installed
SYSMEMCELLERR = 47; // flash cell not empty (DELPHI only)
SYSMEMAVAILERR = 48; // flash not availlable
SYSMEMWRITEERR = 49; // flash write error
SYSMEMBUSYERR = 50; // flash is busy
SYSMEMADRERR = 52; // flash address error
SYSMEMPEEKERR = 53; // flask illegal peek length
SYSMEMTIMEOUT = 55; // flash timeout error
SYSFIFOERR = 64; // fifo error
SYSFIFOEMPTY = 65; // fifo is empty
SYSFIFOFULL = 66; // fifo is full
SYSTASKERRTEXT = 0; // text of last runtime error in task
SYSERRCODETEXT = 1; // text of error code
// 24.05.07 nk add DELPHI constants
FLASH_PATH = 'Flash\';
FLASH_FILE = 'Flash';
FLASH_POST = '.pfd';
FLASH_SERNO = 'Serno';
var // DELPHI variables
FlashOpen: Boolean; // flash memory file status
SoundCard: Boolean; // PC sound card available
SysTickNull: Long; // system tick correction
InitialMem: Int64; // initial process memory [bytes]
SernoFile: string; // simulate user flash area (128 bytes)
FlashFile: string; // simulate flash memory
Flash: file; // binary coded flash file
// module variables
SysBuff: string; // modul message buffer
CompilerName: string; // compiler name
CompilerVers: string; // compiler version
ProgOwner: string; // program owner identification (DELPHI w/o [MAXSYS])
ProgVers: string; // program version and release (DELPHI w/o [MAXSYS])
DeviceName: string; // device identification
SystemName: string; // runtime system name
SystemVers: string; // runtime system version (DELPHI w/o [MAXSYS])
ProcType: string; // processor module type
ProcVers: string; // processor module version (DELPHI w/o [MAXSYS])
DriverVers: string; // device driver version (DELPHI w/o [MAXSYS])
SerialNumber: string; // serial number of device (DELPHI w/o [MAXSYS])
UserNumber: string; // user identification number
TaskName: array[0..MAXTASK] of string; // names of running tasks
DevDriver: array[0..DEVMAX] of string; // name of device drivers
DevVers: array[0..DEVMAX] of Long; // DELPHI version of device drivers
MemBlock: array[0..MEMBLOCKNUM, 0..MEMBLOCKLEN] of Word; // block buffers
ErrCode: Long; // last global error code (decimal)
RamFree: Long; // free sram for user [bytes]
MemFree: Long; // free flash memory for user data [bytes]
MemError: Long; // global flash error code (must be long!)
MemRead: Long; // read pointer to flash address
MemWrite: Long; // write pointer to flash address
MemSize: Long; // total user flash memory size [bytes]
MemSectors: Long; // usable flash memory sectors
MemBlocks: Long; // number of flash memory logging blocks
StackFree: Long; // free program stack size [bytes]
TimeRes: Word; // timer resolution [ns]
PulseMin: Word; // min number of pulses
ErrLog: Byte; // error logger flag (OFF=no error logs)
KeyFlag: Byte; // OFF=no key depressed, <>OFF=number of depressed key
Key1: Byte; // bit value of system keys 1..4
Key2: Byte;
Key3: Byte;
Key4: Byte;
// Tiger interface to SYS device driver (SYS.INC)
procedure InitSys;
procedure LogSys;
procedure ErrorHandler(Task, Error: string);
procedure LogError(ErrModul, ErrText, ErrVal: string; ErrNum: Long);
procedure LogEvent(LogModul, LogText, LogVal: string);
procedure LogTasks;
procedure InitAudio;
procedure SetAudioTone(Dur: Word);
procedure InitI2c;
procedure InitSpi;
procedure SetSpi(Ready: Byte);
procedure InitKey;
procedure GetKeyNum(var KeyNum: Byte);
procedure GetDevice(DevNum: Byte; var Vers: string);
procedure GetSystem(var Vers: string);
procedure GetProcessor(var Vers: string);
procedure GetSerial(var SerNum: string);
procedure GetMemFree(var Free: Long);
procedure GetRamFree(var Free: Long);
procedure GetStackFree(var Free: Long);
procedure CheckStack(Logon: Byte);
procedure GetVers(VersNum: Long; var Vers: string);
procedure InitGen(Range: Byte);
procedure SetGen(Freq: Word);
procedure InitTim(Freq: Word);
procedure InitMem;
procedure ClearMem(Sector: Word);
procedure WriteMemWord(Value: Word; GoAhead: Byte);
procedure ReadMemWord(var Value: Word; GoAhead: Byte);
procedure ClearMemBlock(BlockNum: Byte);
procedure ReadMemBlock(BlockNum: Byte);
procedure WriteMemBlock(BlockNum, ClearSect: Byte); // 01.06.07 nk add ClearSect
// Tiger runtime system function
function Ticks: Long;
function Limit(Val, Min, Max: Long): Long;
function EraseFlash(Addr, Size: Long): Boolean;
function PeekFlash(Addr: Long; var Value: Long; Size: Long): Long;
function PokemFlash(Addr, Value, Index, Size, Mode: Long): Long;
procedure SetBit(var Val: Word; Bit: Byte);
procedure WaitDuration(Msec: Long);
procedure OutAudport(Tones, Dur, Pause: Word);
procedure DisableTsw;
procedure EnableTsw;
implementation
uses FMain, FGui;
//------------------------------------------------------------------------------
// INITSYS - Initialize system and program parameter and clear tick counter
//------------------------------------------------------------------ 17.02.07 --
procedure InitSys;
begin
SysTickNull := GetTickCount; // clear tick counter
TaskName[TASKRUN] := 'Runtime System'; //05.05.07 nk mov from main ff
TaskName[TASKDAQ] := 'Data Acquisition';
TaskName[TASKGUI] := 'User Interface';
TaskName[TASKCOM] := 'Host Communication';
UserNumber := 'P4M01RT56'; //nk//const
CompilerName := COMPILER_NAME;
CompilerVers := FloatToStr(COMPILER_VERS);
ProgOwner := CompanyName;
ProgVers := ProductVersion;
DeviceName := ProductName;
GetSerial(SerialNumber);
end;
//------------------------------------------------------------------------------
// LOGSYS - Log program owner, device name, compiler name, and versions
//------------------------------------------------------------------ 17.02.07 --
procedure LogSys;
begin
LogEvent(DeviceName, VERSTEXT, ProgVers);
LogEvent(ProgOwner, SERIALTEXT, SerialNumber);
LogEvent(CompilerName, VERSTEXT, CompilerVers);
end;
//------------------------------------------------------------------------------
// ERRORHANDLER - Handle program and system errors and get error message
//------------------------------------------------------------------ 25.07.17 --
procedure ErrorHandler(Task, Error: string);
var
errtask: string;
errtext: string;
begin
ErrCode := GetLastError; // get error code (decimal long)
errtext := Trim(Error);
errtask := 'in task ' + Task;
LogError('ErrorHandler', errtext, errtask, ErrCode);
end;
//------------------------------------------------------------------------------
// LOGERROR - Send messages via serial port for error logging
//------------------------------------------------------------------ 17.02.07 --
procedure LogError(ErrModul, ErrText, ErrVal: string; ErrNum: Long);
var
timestamp: Long;
modul: string;
text: string;
val: string;
num: string;
begin
if ErrLog = cOFF then begin // error logging supressed
Exit;
end;
timestamp := Ticks; // get timestamp
modul := Trim(ErrModul);
text := Trim(ErrText);
val := Trim(ErrVal);
if ErrNum > SYSNOERR then begin
num := Format(ERRFORM, [ErrNum]) + sCOLON; // make HEX string
end else begin
num := sEMPTY;
end;
text := IntToStr(timestamp) + sCOLON + modul + sCOLON + 'ERROR ' + num + text + sSPACE + val;
Log.Print(text);
SetAudioTone(TONEERROR); // 24.05.07 nk opt
ErrCode := SYSNOERR;
end;
//------------------------------------------------------------------------------
// LOGEVENT - Send messages via serial port for event logging
//------------------------------------------------------------------ 17.02.07 --
procedure LogEvent(LogModul, LogText, LogVal: string);
var
timestamp: Long;
modul: string;
text: string;
val: string;
begin
timestamp := Ticks; // get timestamp
modul := Trim(LogModul);
text := Trim(LogText);
val := Trim(LogVal);
text := IntToStr(timestamp) + sCOLON + modul + sCOLON + text + sSPACE + val;
Log.Print(text);
end;
//------------------------------------------------------------------------------
// LOGTASKS - Log number, name, and priority of all running tasks
//------------------------------------------------------------------ 25.07.17 --
procedure LogTasks;
var
task: Byte;
tasknum: Byte;
aprio, tprio: Long;
taskprio: string; //old=[6];
begin
aprio := PROCENT; // get total of task priorities
for task := 0 to Main.MdiChildCount do begin
if task = 0 then begin
SysBuff := Main.Name;
tasknum := Main.Tag;
taskprio := Main.HelpKeyword;
if taskprio <> sEMPTY then
tprio := StrToInt(taskprio)
else
tprio := cOFF;
end else begin
SysBuff := Main.MdiChildren[task - 1].Name;
tasknum := Main.MdiChildren[task - 1].Tag;
taskprio := Main.MdiChildren[task - 1].HelpKeyword;
if taskprio <> sEMPTY then
tprio := StrToInt(string(taskprio))
else
tprio := cOFF;
end;
if (aprio > cOFF) and (tprio > cOFF) then begin
tprio := tprio * PROCENT div aprio; // get task priority [%]
taskprio := IntToStr(tprio) + sPROCENT;
SysBuff := TaskName[tasknum] + ' running at priority ';
LogEvent('LogTasks', SysBuff, taskprio);
end;
end;
end;
//------------------------------------------------------------------------------
// INITAUDIO - Initialize audio port for external tone generator
//------------------------------------------------------------------ 17.02.07 --
procedure InitAudio;
begin
if (AUDPORT = cOFF) or (AUDPIN = cOFF) then begin
SoundCard := False;
LogError('InitAudio', 'Audio output not defined', sEMPTY, $96);
Exit;
end;
//enable system beep
SystemParametersInfo(SPI_SETBEEP, cON, nil, SPIF_SENDWININICHANGE);
OutAudport(1, TONELONG, TONEOFF);
SysBuff := IntToStr(AUDPORT) + Trim(IntToStr(AUDPIN));
if SoundCard then begin
LogEvent('InitAudio', 'Audio card initialized at pin', SysBuff);
end else begin
LogEvent('InitAudio', 'Audio speaker initialized at pin', SysBuff);
end;
end;
//------------------------------------------------------------------------------
// SETAUDIOTONE - Generate audio tones for warnings and alarms
// Dur - 0=OFF, 1=ON, >1=tone duration [ms], >250=sound pattern
//------------------------------------------------------------------ 17.02.07--
procedure SetAudioTone(Dur: Word);
begin
if Loudness = cOFF then begin
Exit;
end;
if Dur = cOFF then begin
Windows.Beep(TONEFREQ, TONEOFF); // set audio tone off
Exit;
end;
if Dur = cON then begin
Windows.Beep(TONEFREQ, TONEON); // set audio tone on
Exit;
end;
if Dur = TONEBELL then begin
OutAudport(3, 50, 50);
Exit;
end;
if Dur = TONERING then begin
OutAudport(4, 40, 40);
Exit;
end;
if Dur = TONEALARM then begin
OutAudport(5, 30, 30);
Exit;
end;
if Dur = TONEERROR then begin // 24.05.07 nk add audio error tone
OutAudport(6, 20, 20);
Exit;
end;
OutAudport(1, Dur, TONEOFF); // audio beep tone
end;
//------------------------------------------------------------------------------
// INITI2C - Initialize and setup I2C bus and set both lines to high-impedance
// DELPHI no I2C bus implemented
//------------------------------------------------------------------ 17.02.07 --
procedure InitI2c;
begin
if I2CMODE = cOFF then begin
SysBuff := 'MSB first';
end else begin
SysBuff := 'LSB first (ISO7816)';
end;
LogEvent('InitI2c', 'I2C bus set to mode', SysBuff);
SysBuff := IntToStr(I2CPORT);
LogEvent('InitI2c', 'I2C bus initialized at port', SysBuff);
end;
//------------------------------------------------------------------------------
// INITSPI - Initialize and setup SPI bus
// DELPHI no SPI bus implemented
//------------------------------------------------------------------ 17.02.07 --
procedure InitSpi;
begin
if SPIMODE = cOFF then begin
SysBuff := 'LSB first';
end else begin
SysBuff := 'MSB first';
end;
LogEvent('InitSpi', 'SPI bus set to mode', SysBuff);
SysBuff := IntToStr(SPIPORT);
LogEvent('InitSpi', 'SPI bus initialized at port', SysBuff);
end;
//------------------------------------------------------------------------------
// SETSPI - Set SPI bus to ready or idle state - pin CS (Chip Select) not used
// DELPHI no SPI bus implemented
//------------------------------------------------------------------ 17.02.07 --
procedure SetSpi(Ready: Byte);
begin
// do nothing
end;
//------------------------------------------------------------------------------
// INITKEY - Initialize parallel key port for joystick (DELPHI=Keyboard)
//------------------------------------------------------------------ 17.02.07 --
procedure InitKey;
var
dmask: Long;
begin
dmask := GetKeyboardType(0);
GetKeyState(0); //09.05.07 nk add ff
ClearKeyboardBuffer; // clear keyboard input buffer
if dmask = cOFF then begin
SysBuff := IntToStr(dmask);
LogError('InitKey', 'Keyboard initialisation failed', SysBuff, $91);
end else begin
SysBuff := IntToStr(KEYPORT);
LogEvent('InitKey', 'Keyboard initialized at port', SysBuff);
end;
end;
//------------------------------------------------------------------------------
// GETKEYNUM - Get highest number (4..1) of depressed key or KEYNONE if none
//------------------------------------------------------------------ 17.02.07 --
procedure GetKeyNum(var KeyNum: Byte);
begin
try // may block at start-up
if KeyNum = KEYINIT then begin // DELPHI clear keyboard buffer
KeyNum := cKEYNONE;
Exit;
end;
//09.05.07 nk opt - use negative return value of GetKeyState instead of HiWord
if GetKeyState(VK_LEFT) < cKEYNONE then
KeyNum := cKEYLEFT
else if
GetKeyState(VK_DOWN) < cKEYNONE then
KeyNum := cKEYDOWN
else if
GetKeyState(VK_RIGHT) < cKEYNONE then
KeyNum := cKEYRIGHT
else if
GetKeyState(VK_UP) < cKEYNONE then
KeyNum := cKEYUP
else
KeyNum := cKEYNONE;
except
on E: Exception do begin //09.05.07 nk DELPHI add ff
LogError('GetKeyNum', E.Message, IntToStr(KeyNum), $B0);
KeyNum := cKEYNONE;
Exit;
end;
end;
try //DELPHI: set focus to GUI
if KeyNum > cKEYNONE then begin
Gui.WindowState := wsNormal;
Gui.SetFocus;
end;
except
// ignore
end;
end;
//------------------------------------------------------------------------------
// GETDEVICE - Get actual version of device driver(s)
//------------------------------------------------------------------ 17.02.07 --
procedure GetDevice(DevNum: Byte; var Vers: string);
var
dev, dmin, dmax, elog: Byte;
ver: Long;
begin
DevDriver[RUN] := 'Runtime System';
DevDriver[LAB] := 'Plug and Play Lab';
DevDriver[PRN] := 'Parallel Printer';
DevDriver[RTC] := 'Real Time Clock';
DevDriver[CNT] := 'Counter';
DevDriver[SER] := 'Serial Port';
DevDriver[ADC] := 'A/D Converter';
DevDriver[ADF] := 'A/D Fast Converter';
DevDriver[ENC] := 'Encoder';
DevDriver[PWM] := 'Pulse Width Modulator';
DevDriver[LCD] := 'Graphic Display';
DevDriver[FRQ] := 'Frequency Meter';
DevDriver[PLI] := 'Pulse Input';
DevDriver[GEN] := 'Pulse Generator';
DevDriver[TIM] := 'Timer';
DevDriver[PAR] := 'Parallel Port';
DevDriver[TCH] := 'Touch Memory';
DevDriver[MF2] := 'MF2 Keyboard';
DevDriver[PS2] := 'PS/2 Device';
elog := ErrLog;
if DevNum >= DEVMAX then begin // get version of all loaded device drivers
ErrLog := cOFF; // supress errors from not loaded drivers
dmin := 1;
dmax := DEVMAX - 1;
end else begin // get version of specified device driver
dmin := DevNum;
dmax := DevNum;
end;
for dev := dmin to dmax do begin
ErrCode := SYSNOERR; // get version of device driver
ver := DevVers[dev]; // SYSNOERR if not loaded
if ver <> SYSNOERR then begin // driver has been successfully loaded
GetVers(ver, Vers);
SysBuff := DevDriver[dev] + VERSTEXT;
LogEvent('Device driver loaded', SysBuff, Vers);
end;
end;
ErrCode := SYSNOERR;
ErrLog := elog; // re-activate error logging
end;
//------------------------------------------------------------------------------
// GETSYSTEM - Get name and version of runtime system
//------------------------------------------------------------------ 28.05.22 --
procedure GetSystem(var Vers: string);
begin
Vers := GetSystemVers(vtBuild);
SystemName := GetSystemVers(vtName) + SPACE + UpdateWinVersion; //28.05.22 nk add UpdateWinVersion
SysBuff := SystemName + VERSTEXT;
LogEvent('Runtime system', SysBuff, Vers);
end;
//------------------------------------------------------------------------------
// GETPROCESSOR - Get type and version of processor module
//------------------------------------------------------------------ 17.02.07 --
procedure GetProcessor(var Vers: string);
begin
Vers := GetProcessorVers(vtVers);
ProcType := GetProcessorVers(vtShort);
SysBuff := ProcType + VERSTEXT;
LogEvent('Processor module', SysBuff, Vers);
end;
//------------------------------------------------------------------------------
// GETSERIAL - Get serial number from system harddisk (Tiger from flash area)
//------------------------------------------------------------------ 17.02.07 --
procedure GetSerial(var SerNum: string);
var
maxlen: DWORD;
flags: DWORD;
serno: DWORD;
drive: string;
begin
try
drive := LeftStr(Application.ExeName, 3);
GetVolumeInformation(PChar(drive), nil, MAX_PATH,
@serno, maxlen, flags, nil, 0);
if @serno <> nil then
SerNum := LeftStr(Format(SERIALFORM, [SERIALLEN, SERIALLEN, serno]), SERIALLEN)
else
SerNum := SERIALNONE;
except
SerNum := SERIALNONE;
end;
end;
//------------------------------------------------------------------------------
// GETMEMFREE - Get size and number of free user flash sectors
//------------------------------------------------------------------ 17.02.07 --
procedure GetMemFree(var Free: Long);
var
ret: Long;
begin
try
Free := GetFileSize(FlashFile); // size of user flash
except
Free := MEMSTART;
end;
SysBuff := IntToStr(Free);
LogEvent('User flash size', SysBuff, MEMUNIT);
ret := Free div MEMSECTSIZE; // number of user flash sectors
SysBuff := IntToStr(ret);
LogEvent('User flash sectors', SysBuff, sEMPTY);
ret := MEMSECTSIZE; // size of flash sectors
SysBuff := IntToStr(ret);
LogEvent('Flash sector size', SysBuff, MEMUNIT);
end;
//------------------------------------------------------------------------------
// GETRAMFREE - Get size of used and free working memory (RAM)
//------------------------------------------------------------------ 17.02.07 --
procedure GetRamFree(var Free: Long);
var
ret: Int64;
ram: TMemoryStatus;
begin
try
ram.dwLength := SizeOf(ram);
GlobalMemoryStatus(ram);
except
Free := cCLEAR;
Exit;
end;
ret := ram.dwTotalPhys; // RAM memory size
SysBuff := IntToStr(ret);
LogEvent('RAM memory size', SysBuff, MEMUNIT);
Free := ram.dwAvailPhys; // RAM memory free
SysBuff := IntToStr(Free);
LogEvent('RAM memory free', SysBuff, MEMUNIT);
ret := ret - Free; // RAM memory used
SysBuff := IntToStr(ret);
LogEvent('RAM memory used', SysBuff, MEMUNIT);
end;
//------------------------------------------------------------------------------
// GETSTACKFREE - Get bytes of total, used, and free program stack
// works only on 32bit Windows systems (WINNT, WIN2K, WINXP, VISTA)
//------------------------------------------------------------------ 25.07.17 --
procedure GetStackFree(var Free: Long);
var
ret: Int64;
begin
SysBuff := IntToStr(SYSMAXSTACK); // program stack size [bytes]
LogEvent('Program stack size', SysBuff, MEMUNIT);
ret := GetAllocMemSize - InitialMem; //25.07.17 nk old=AllocMemSize (deprecated)
SysBuff := IntToStr(ret); // program stack used [bytes]
LogEvent('Program stack used', SysBuff, MEMUNIT);
Free := SYSMAXSTACK - ret;
SysBuff := IntToStr(Free); // program stack free [bytes]
LogEvent('Program stack free', SysBuff, MEMUNIT);
end;
//------------------------------------------------------------------------------
// CHECKSTACK - Check if a program stack overflow may occur (< 512 bytes)
//------------------------------------------------------------------ 25.07.17 --
procedure CheckStack(Logon: Byte);
begin
StackFree := SYSMAXSTACK - (GetAllocMemSize - InitialMem); //25.07.17 nk old=AllocMemSize (deprecated)
if StackFree < SYSMINSTACK then begin // program stack free [bytes]
SysBuff := IntToStr(StackFree) + MEMUNIT;
LogError('CheckStack', 'Stack overflow may occur', SysBuff, $A1);
end;
if Logon = cON then begin // program stack free [%]
StackFree := PROCENT * StackFree div SYSMAXSTACK;
SysBuff := IntToStr(StackFree);
LogEvent('Program stack free', SysBuff, sPROCENT);
end;
end;
//------------------------------------------------------------------------------
// GETVERS - Get version string from HEX coded version number
// All Tiger versions are HEX coded (e.g. 106Dh = 1.06n)
//------------------------------------------------------------------ 17.02.07 --
procedure GetVers(VersNum: Long; var Vers: string);
var
ver: Long;
rel: string; //old=[1];
begin
ver := VersNum and MAXWORD; // ignore beta version
Vers := Format(VERSFORM, [ver]);
rel := RightStr(Vers, 1);
ver := Ord(rel[1]);
if ver >= ASCII then begin
ver := ver + TIGASCII; // convert HEX->ASCII (A=k, B=l..)
end else begin
ver := ver + HEXASCII; // convert HEX->ASCII (0=a, 1=b..)
end;
Vers := LeftStr(Vers, 1) + sDOT + Copy(Vers, 2, 2) + Chr(ver);
end;
//------------------------------------------------------------------------------
// INITGEN - Initialize pulse generator and set frequency range (1..3)
//------------------------------------------------------------------ 17.02.07 --
procedure InitGen(Range: Byte);
begin
PulseMin := cOFF;
TimeRes := cOFF;
ErrCode := SYSNOERR;
case Range of
1: begin
TimeRes := 1 * FREQRES; // timer resolution [ns]
PulseMin := 31; // min number of pulses
end;
2: begin
TimeRes := 4 * FREQRES;
PulseMin := 7;
end;
3: begin
TimeRes := 16 * FREQRES;
PulseMin := 2;
end;
else
SysBuff := IntToStr(Range);
LogError('InitGen', 'Unsupported frequency range', SysBuff, $90);
Exit;
end;
// no pulse generator implemented in DELPHI
SysBuff := IntToStr(TimeRes) + GENUNIT;
LogEvent('InitGen', 'Pulse generator initialized with', SysBuff);
end;
//------------------------------------------------------------------------------
// SETGEN - Set pulse generator frequency [Hz] On or Off
//------------------------------------------------------------------ 17.02.07 --
procedure SetGen(Freq: Word);
var
duty, cycle: Word;
peri: Long;
begin
if Freq = cOFF then begin
LogEvent('SetGen', 'Pulse generator stopped', sEMPTY);
Exit;
end;
if (TimeRes = cOFF) or (TimeRes mod FREQRES <> cOFF) then begin
SysBuff := IntToStr(TimeRes) + GENUNIT;
LogError('SetGen', 'Invalid time resolution', SysBuff, $91);
Exit;
end;
if (Freq < FREQMIN) or (Freq > FREQMAX) then begin
SysBuff := IntToStr(Freq) + TIMUNIT;
LogError('SetGen', 'Unsupported frequency', SysBuff, $90);
Exit;
end;
peri := NANO div Freq; // pulse periode [ns]
cycle := peri div TimeRes;
duty := cycle div 2; // duty cycle 50%
peri := cycle * TimeRes;
if (duty < PulseMin) or (cycle > FREQDIV) or (peri <= 0) then begin
SysBuff := IntToStr(Freq) + TIMUNIT;
LogError('SetGen', 'Impossible frequency setting', SysBuff, $98);
Exit;
end;
cycle := NANO div peri;
SysBuff := IntToStr(cycle) + TIMUNIT;
LogEvent('InitGen', 'Pulse generator set to frequency', SysBuff);
end;
//------------------------------------------------------------------------------
// INITTIM - Initialize hardware timer and set frequency [Hz]
//------------------------------------------------------------------ 17.02.07 --
procedure InitTim(Freq: Word);
begin
if Freq = cOFF then begin // switch timer off
LogEvent('InitTim', 'Timer stopped', sEMPTY);
Exit;
end;
if (Freq < FREQMIN) or (Freq > FREQMAX) then begin
SysBuff := IntToStr(Freq) + TIMUNIT;
LogError('InitTim', 'Unsupported frequency', SysBuff, $90);
Exit;
end;
// no hardware timer implemented in DELPHI
SysBuff := IntToStr(Freq) + TIMUNIT;
LogEvent('InitTim', 'Timer initialized with', SysBuff);
end;
//------------------------------------------------------------------------------
// INITMEM - Initialize flash memory sectors available for application use
// and find next free write address in flash memory
//------------------------------------------------------------------ 17.02.07 --
procedure InitMem;
var
lident: Word;
i, j, ret: Long;