-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit1.pas
305 lines (249 loc) · 6.69 KB
/
unit1.pas
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
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
StdCtrls, LazSerial, base64, crt;
type
{ TForm1 }
TForm1 = class(TForm)
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
LazSerial1: TLazSerial;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
StaticText1: TStaticText;
StaticText2: TStaticText;
StaticText3: TStaticText;
StaticText4: TStaticText;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure LazSerial1RxData(Sender: TObject);
private
{ private declarations }
LStrings: TStringList;
LastCRC: Byte;
AR_Msg:string;
AR_Status: Integer;
TotalPages: Integer;
CurrentPage: Integer;
public
{ public declarations }
procedure getId();
procedure Write_Page();
//function crc8(pcBlock: PByte; len: Integer):Byte;
function crc8(Buffer:PByte;len:Cardinal):Byte;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.BitBtn1Click(Sender: TObject);
var
i:Integer;
begin
if OpenDialog1.Execute then
begin
StaticText1.Caption:=OpenDialog1.FileName;
LStrings := TStringList.Create;
LStrings.Loadfromfile(OpenDialog1.FileName);
Memo1.Clear;
for i:=0 to 3 do
begin
Memo1.Append(LStrings.Strings[i]);
end;
Memo1.Append(LStrings.Strings[4]);
TotalPages := StrToInt(LStrings.Strings[4]);
BitBtn2.Enabled:=true;
end;
end;
{function TForm1.crc8(pcBlock: PByte; len: Integer):Byte;
var crc:Byte;
i:Byte;
j:integer;
begin
crc := $FF;
j:=0;
while len>0 do
begin
dec(len);
crc:= crc xor pcBlock[j];
Inc(j);
for i := 0 to 7 do
begin
if (crc and $80)<>0 then crc:= (crc shl 1) xor $31 else crc := crc shl 1;
end;
end;
result:=crc;
end;}
function TForm1.crc8(Buffer:PByte;len:Cardinal):Byte;
var
i,j: Integer;
begin
Result:=$FF;
for i:=0 to len-1 do begin
Result:=Result xor buffer[i];
for j:=0 to 7 do begin
if (Result and $80)<>0 then Result:=(Result shl 1) xor $31
else Result:=Result shl 1;
end;
end;
//Result:=Result and $ff;
end;
procedure TForm1.getId();
begin
LazSerial1.WriteData('I');
end;
procedure TForm1.Write_Page();
var bs,ds:string;
crc, i:Integer;
Decoder : TBase64DecodingStream;
EncodedStream : TStringStream;
ar : array[0..255] of Byte;
PT: ^Byte;
s: string;
begin
bs := LStrings.Strings[CurrentPage + 5];
try
//ds:= IdDecoderMIME1.DecodeString(bs);
EncodedStream := TStringStream.Create(bs);
Decoder:= TBase64DecodingStream.Create(EncodedStream);
Decoder.Read(ar,256);
crc:=crc8(ar,256);
for i:=0 to 31 do
begin
s:=s+IntToHex(ar[i],2)+' ';
end;
Memo1.Append(s);
PT := ar;
for i:= 0 to 7 do
begin
LazSerial1.WriteBuffer(PT^,32);
PT:=PT+32;
Delay(10);
end;
LastCRC := crc;
except
Memo1.Append('Error wile write page');
end;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
AR_Status:=0;
CurrentPage:=0;
LazSerial1.ShowSetupDialog;
LazSerial1.Open;
StaticText2.Caption:='Connection...';
Memo1.Append(StaticText2.Caption);
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
if LazSerial1.Active then LazSerial1.Close;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
procedure TForm1.LazSerial1RxData(Sender: TObject);
var
PT: ^Integer;
PB: ^Byte;
begin
AR_Msg:=AR_Msg + LazSerial1.ReadData;
Memo1.Append('l: ' +IntToStr(length(AR_Msg)) + ' d:' + AR_Msg );
if ((AR_Status = 0) and (CompareStr(AR_Msg,'FlUl0002')=0)) then
begin
StaticText2.Caption:='Connected';
Memo1.Append(StaticText2.Caption);
AR_Status:=1;
AR_Msg:= '';
getId();
end;
if (AR_Status = 1) and (length(AR_Msg) = 6) then
begin
writeln(AR_Msg);
StaticText2.Caption:='Chip Id: ' + AR_Msg;
Memo1.Append(StaticText2.Caption);
AR_Status:=9;
AR_Msg:= '';
Delay(50);
LazSerial1.WriteData('C');
end;
if (AR_Status = 9) and (AR_Msg = 'ACK') then
begin
StaticText2.Caption:='Erased chip, send page num' + IntToStr(CurrentPage);
Memo1.Append(StaticText2.Caption);
AR_Status:=2;
AR_Msg:= '';
Delay(50);
LazSerial1.WriteData('U');
Delay(50);
PT := @CurrentPage;
LazSerial1.WriteBuffer(PT^,SizeOf(Integer)); //write page num;
end;
//write page num
if (AR_Status = 2) and (AR_Msg = 'ACK') then
begin
StaticText2.Caption:='Page num ' + IntToStr(CurrentPage) + ' sended, sending data';
Memo1.Append(StaticText2.Caption);
AR_Status:=3;
AR_Msg:= '';
Delay(50);
Write_Page();
end;
//write CRC
if (AR_Status = 3) and (AR_Msg = 'ACK') then
begin
StaticText2.Caption:='Send for ' + IntToStr(CurrentPage) + ' crc ' + IntToHex(LastCRC, 8);
Memo1.Append(StaticText2.Caption);
AR_Status:=5;
AR_Msg:= '';
Delay(50);
//LastCRC := $deadbeef; //recive eeaa00ff
PB := @LastCRC;
LazSerial1.WriteBuffer(PB^,SizeOf(Byte)); //write page crc;
end;
//write CRC
if (AR_Status = 5) and (AR_Msg = 'ACK') then
begin
AR_Msg:= '';
StaticText2.Caption:='Page num ' + IntToStr(CurrentPage) + ' writed';
Memo1.Append(StaticText2.Caption);
if CurrentPage = (TotalPages-1) then
begin
AR_Status := 0; //end
TotalPages := 0;
StaticText2.Caption:='Finished';
Memo1.Append(StaticText2.Caption);
end
else
begin
Inc(CurrentPage);
AR_Status:=2; //while OK
Delay(50);
LazSerial1.WriteData('U');
Delay(50);
PT := @CurrentPage;
LazSerial1.WriteBuffer(PT^,SizeOf(Integer)); //write page num;
end;
end;
if (AR_Status = 5) and (AR_Msg = 'NAK') then
begin
StaticText2.Caption:='Error on status ' + IntToStr(AR_Status);
Memo1.Append(StaticText2.Caption);
//try send page again
AR_Status:=2; //while OK
Delay(10);
LazSerial1.WriteData('U');
Delay(10);
PT := @CurrentPage;
LazSerial1.WriteBuffer(PT^,SizeOf(Integer)); //write page num;
end;
end;
end.