1 | unit ISP_Lockbits;
|
---|
2 |
|
---|
3 | {$MODE Delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | function ISPReadLockBits(var lb:byte):integer;
|
---|
8 | function ISPWriteLockBits(lb:byte):integer;
|
---|
9 | function AnyLock:boolean;
|
---|
10 |
|
---|
11 | implementation
|
---|
12 |
|
---|
13 | uses Delays, Globals, Processors, SPI;
|
---|
14 |
|
---|
15 | function ISPReadLockBits(var lb:byte):integer;
|
---|
16 | var data:array[0..2] of byte;
|
---|
17 | begin
|
---|
18 | Result:=0;
|
---|
19 | case Signatures[devicenr].algo_lb of
|
---|
20 | ALGO_LB_2323:
|
---|
21 | begin
|
---|
22 | data[0] := $58;
|
---|
23 | data[1] := $00;
|
---|
24 | data[2] := $00;
|
---|
25 | WriteBytes(@data, 3);
|
---|
26 | lb:=ReadByte;
|
---|
27 | lb:=$f9 or ((lb and $40) shr 4) or ((lb and $80) shr 6);
|
---|
28 | end;
|
---|
29 | ALGO_LB_TINY, ALGO_LB_2333, ALGO_LB_MEGA:
|
---|
30 | begin
|
---|
31 | data[0] := $58;
|
---|
32 | data[1] := $00;
|
---|
33 | data[2] := $00;
|
---|
34 | WriteBytes(@data, 3);
|
---|
35 | lb:=ReadByte;
|
---|
36 | end;
|
---|
37 | ALGO_LB_89S51:
|
---|
38 | begin
|
---|
39 | data[0] := $24;
|
---|
40 | data[1] := $00;
|
---|
41 | data[2] := $00;
|
---|
42 | WriteBytes(@data, 3);
|
---|
43 | lb:=ReadByte xor $ff;
|
---|
44 | end;
|
---|
45 | ALGO_LB_89S8253, ALGO_LB_89S2051:
|
---|
46 | begin
|
---|
47 | data[0] := $24;
|
---|
48 | data[1] := $00;
|
---|
49 | data[2] := $00;
|
---|
50 | WriteBytes(@data, 3);
|
---|
51 | lb:=ReadByte;
|
---|
52 | end;
|
---|
53 | else
|
---|
54 | Result:=-1; // no ISP command to read lock bits
|
---|
55 | end;
|
---|
56 | end;
|
---|
57 |
|
---|
58 | function ISPWriteLockBits(lb:byte):integer;
|
---|
59 | var data:array[0..3] of byte;
|
---|
60 | begin
|
---|
61 | Result:=0;
|
---|
62 | case Signatures[devicenr].algo_lb of
|
---|
63 | ALGO_LB_89x: // 89S53/8252
|
---|
64 | begin
|
---|
65 | data[0] := $ac;
|
---|
66 | data[1] := $07 or (lb and $e0);
|
---|
67 | data[2] := $00;
|
---|
68 | WriteBytes(@data, 3);
|
---|
69 | Sync;
|
---|
70 | WaitMS(Signatures[devicenr].prog_time);
|
---|
71 | end;
|
---|
72 | ALGO_LB_89S51: // 89S51/52
|
---|
73 | begin
|
---|
74 | // Mode 1, no lock protection
|
---|
75 | data[0] := $ac;
|
---|
76 | data[1] := $e0;
|
---|
77 | data[2] := $00;
|
---|
78 | data[3] := $00;
|
---|
79 | WriteBytes(@data, 4);
|
---|
80 | Sync;
|
---|
81 | WaitMS(Signatures[devicenr].prog_time);
|
---|
82 | if (lb and $04) = 0 then
|
---|
83 | begin
|
---|
84 | // Mode 2, lock bit 1 activated
|
---|
85 | data[0] := $ac;
|
---|
86 | data[1] := $e1;
|
---|
87 | data[2] := $00;
|
---|
88 | data[3] := $00;
|
---|
89 | WriteBytes(@data, 4);
|
---|
90 | Sync;
|
---|
91 | WaitMS(Signatures[devicenr].prog_time);
|
---|
92 | end;
|
---|
93 | if (lb and $08) = 0 then
|
---|
94 | begin
|
---|
95 | // Mode 3, lock bit 2 activated
|
---|
96 | data[0] := $ac;
|
---|
97 | data[1] := $e2;
|
---|
98 | data[2] := $00;
|
---|
99 | data[3] := $00;
|
---|
100 | WriteBytes(@data, 4);
|
---|
101 | Sync;
|
---|
102 | WaitMS(Signatures[devicenr].prog_time);
|
---|
103 | end;
|
---|
104 | if (lb and $10) = 0 then
|
---|
105 | begin
|
---|
106 | // Mode 4, lock bit 3 activated
|
---|
107 | data[0] := $ac;
|
---|
108 | data[1] := $e3;
|
---|
109 | data[2] := $00;
|
---|
110 | data[3] := $00;
|
---|
111 | WriteBytes(@data, 4);
|
---|
112 | Sync;
|
---|
113 | WaitMS(Signatures[devicenr].prog_time);
|
---|
114 | end;
|
---|
115 | end;
|
---|
116 | ALGO_LB_STD, ALGO_LB_TINY, // AVR
|
---|
117 | ALGO_LB_2323, ALGO_LB_2333:
|
---|
118 | begin
|
---|
119 | data[0] := $ac;
|
---|
120 | data[1] := $f9 or (lb and $06);
|
---|
121 | data[2] := $00;
|
---|
122 | data[3] := $00;
|
---|
123 | WriteBytes(@data, 4);
|
---|
124 | Sync;
|
---|
125 | WaitMS(Signatures[devicenr].prog_time);
|
---|
126 | end;
|
---|
127 | ALGO_LB_MEGA: // ATmega
|
---|
128 | begin
|
---|
129 | data[0] := $ac;
|
---|
130 | data[1] := $e0;
|
---|
131 | data[2] := $00;
|
---|
132 | data[3] := $c0 or (lb and $3f);
|
---|
133 | WriteBytes(@data, 4);
|
---|
134 | Sync;
|
---|
135 | WaitMS(Signatures[devicenr].prog_time);
|
---|
136 | end;
|
---|
137 | ALGO_LB_89S8253, ALGO_LB_89S2051: // AT89S8253, AT89S2051/4051
|
---|
138 | begin
|
---|
139 | data[0] := $ac;
|
---|
140 | data[1] := $e0 or (lb and $07);
|
---|
141 | data[2] := $00;
|
---|
142 | data[3] := $00;
|
---|
143 | WriteBytes(@data, 4);
|
---|
144 | Sync;
|
---|
145 | WaitMS(Signatures[devicenr].prog_time);
|
---|
146 | end;
|
---|
147 | else
|
---|
148 | Result:=-1; // no ISP command to write lock bits
|
---|
149 | end;
|
---|
150 | end;
|
---|
151 |
|
---|
152 | function AnyLock:boolean;
|
---|
153 | var i:integer;
|
---|
154 | begin
|
---|
155 | Result:=true;
|
---|
156 | for i:=0 to 7 do
|
---|
157 | if (Signatures[devicenr].lockbits[i] <> '') then
|
---|
158 | Exit;
|
---|
159 | Result:=false;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | end.
|
---|