-
Notifications
You must be signed in to change notification settings - Fork 3
/
Unit1.pas
328 lines (278 loc) · 8.21 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, dcrHexEditor, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.ComCtrls;
type
TForm1 = class( TForm )
btnRead: TButton;
HexFirmware: TMPHexEditor;
btnFlashSize: TRadioGroup;
btnSave: TButton;
btnClose: TButton;
DlgSave: TSaveDialog;
ProgressBar1: TProgressBar;
btnMemory: TRadioGroup;
GroupBox1: TGroupBox;
Label1: TLabel;
cbInterface: TComboBox;
Label2: TLabel;
cbSpeed: TComboBox;
GroupBox2: TGroupBox;
Label3: TLabel;
edtAddress: TEdit;
Label4: TLabel;
edtSize: TEdit;
procedure btnReadClick( Sender: TObject );
procedure btnCloseClick( Sender: TObject );
procedure btnSaveClick( Sender: TObject );
procedure FormCloseQuery( Sender: TObject; var CanClose: Boolean );
procedure FormCreate( Sender: TObject );
procedure btnMemoryClick( Sender: TObject );
procedure btnFlashSizeClick( Sender: TObject );
procedure edtAddressExit( Sender: TObject );
procedure edtSizeExit( Sender: TObject );
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
jlinkarm;
{$R *.dfm}
const
MemroyPageSize = 1024;
MemroyBlockSizeInPage = 64;
MemroyBlockSizeInByte = MemroyPageSize * MemroyBlockSizeInPage;
// btnMemory.ItemIndex
MemoryTypeROM = 0;
MemoryTypeRAM = 1;
MemoryTypeFlash = 2;
MemoryTypeCustom = 3;
var
Reading: Boolean;
MemroyBuffer: array [ 0 .. MemroyPageSize - 1 ] of byte;
MemoryAddr: DWORD;
MemorySize: DWORD;
procedure TForm1.btnCloseClick( Sender: TObject );
begin
Close( );
end;
procedure TForm1.btnFlashSizeClick( Sender: TObject );
begin
if btnMemory.ItemIndex = 2 then
begin
MemoryAddr := $98000000;
MemorySize := 1 * 1024 * 1024 * ( 1 shl btnFlashSize.ItemIndex );
edtAddress.Text := IntToHex( MemoryAddr, 8 );
edtSize.Text := IntToHex( MemorySize, 8 );
end;
end;
procedure TForm1.btnMemoryClick( Sender: TObject );
begin
if btnMemory.ItemIndex = 0 then
begin
MemoryAddr := 0;
MemorySize := 512 * 1024;
end else if btnMemory.ItemIndex = 1 then
begin
MemoryAddr := $10000000;
MemorySize := 448 * 1024;
end else if btnMemory.ItemIndex = 2 then
begin
MemoryAddr := $98000000;
MemorySize := 1 * 1024 * 1024 * ( 1 shl btnFlashSize.ItemIndex );
end else begin
MemoryAddr := $10000000;
MemorySize := 1 * 256;
end;
edtAddress.Text := IntToHex( MemoryAddr, 8 );
edtSize.Text := IntToHex( MemorySize, 8 );
if btnMemory.ItemIndex = 3 then
begin
edtAddress.Enabled := True;
edtSize.Enabled := True;
end else begin
edtAddress.Enabled := False;
edtSize.Enabled := False;
end;
end;
procedure TForm1.btnReadClick( Sender: TObject );
var
I: DWORD;
RegValue: DWORD;
time_beg: Integer;
NumWritten: Integer;
RetValue: Integer;
MemoryBlockCount: Cardinal;
JLinkSpeed: Integer;
JLinkInterface: Integer;
msFirmware: TMemoryStream;
NumToRead: DWORD;
NumToAppend: DWORD;
begin
JLinkInterface := cbInterface.ItemIndex;
if cbSpeed.ItemIndex = cbSpeed.Items.Count - 1 then
JLinkSpeed := 12000000
else if cbSpeed.ItemIndex = cbSpeed.Items.Count - 2 then
JLinkSpeed := 10000000
else
begin
JLinkSpeed := StrToInt( Copy( cbSpeed.Items[ cbSpeed.ItemIndex ], 1, 4 ) ) * 1000;
end;
try
Reading := True;
RetValue := JLINKARM_Open( );
if RetValue = 1 then
begin
Beep;
ShowMessage( 'J-Link not found!' );
Exit;
end;
RetValue := JLINKARM_ExecCommand( 'device = Cortex-M3', 0, 0 );
RetValue := JLINKARM_TIF_Select( JLinkInterface );
RetValue := JLINKARM_SetSpeed( 1000 );
RetValue := JLINKARM_Reset( );
RetValue := JLINKARM_Halt( );
Sleep( 10 );
RetValue := JLINKARM_IsConnected( );
if RetValue = 0 then
begin
Beep;
ShowMessage( 'RTL8710 not found!' );
Exit;
end;
Application.ProcessMessages;
if MemoryAddr >= $98000000 then
begin
RetValue := JLINKARM_WriteU32( $40000230, $0000D3C4 );
RetValue := JLINKARM_WriteU32( $40000210, $00200113 );
RetValue := JLINKARM_WriteU32( $400002C0, $00110001 );
RetValue := JLINKARM_WriteU32( $40006008, 0 );
RetValue := JLINKARM_WriteU32( $4000602C, 0 );
RetValue := JLINKARM_WriteU32( $40006010, 1 );
RetValue := JLINKARM_WriteU32( $40006014, 2 );
RetValue := JLINKARM_WriteU32( $40006018, 0 );
RetValue := JLINKARM_WriteU32( $4000601C, 0 );
RetValue := JLINKARM_WriteU32( $4000604C, 0 );
JLINKARM_WriteU32( $40000014, $01 );
Sleep( 10 );
end;
RetValue := JLINKARM_SetSpeed( JLinkSpeed div 1000 );
RetValue := JLINKARM_GetSpeed( );
msFirmware := TMemoryStream.Create( );
try
ProgressBar1.Min := 0;
ProgressBar1.Max := MemorySize div MemroyPageSize;
ProgressBar1.Position := 0;
if HexFirmware.DataSize > 0 then
begin
HexFirmware.SelectAll;
HexFirmware.DeleteSelection( );
end;
NumToRead := MemorySize;
NumToAppend := 0;
MemoryBlockCount := 0;
msFirmware.Seek( 0, soBeginning );
if MemorySize > MemroyPageSize then // at least 1 Page to Read
begin
for I := 0 to MemorySize div MemroyPageSize - 1 do
begin
RetValue := JLINKARM_ReadMem( MemoryAddr, MemroyPageSize, MemroyBuffer );
msFirmware.Write( MemroyBuffer, MemroyPageSize );
NumToRead := NumToRead - MemroyPageSize;
NumToAppend := NumToAppend + MemroyPageSize;
MemoryBlockCount := MemoryBlockCount + 1;
if MemoryBlockCount = MemroyBlockSizeInPage then
begin
HexFirmware.AppendBuffer( PAnsiChar( msFirmware.Memory ), MemroyBlockSizeInByte );
NumToAppend := 0;
MemoryBlockCount := 0;
msFirmware.Seek( 0, soBeginning );
end;
MemoryAddr := MemoryAddr + MemroyPageSize;
ProgressBar1.Position := I;
Application.ProcessMessages;
end;
end;
if NumToAppend > 0 then // < MemroyBlockSizeInByte
begin
HexFirmware.AppendBuffer( PAnsiChar( msFirmware.Memory ), MemroyBlockSizeInByte );
NumToAppend := 0;
MemoryBlockCount := 0;
msFirmware.Seek( 0, soBeginning );
end;
if NumToRead > 0 then // < MemroyPageSize
begin
RetValue := JLINKARM_ReadMem( MemoryAddr, NumToRead, MemroyBuffer );
msFirmware.Write( MemroyBuffer, NumToRead );
HexFirmware.AppendBuffer( PAnsiChar( msFirmware.Memory ), NumToRead );
end;
if MemoryAddr >= $98000000 then
begin
RetValue := JLINKARM_WriteU32( $40000210, $00211157 );
end;
RetValue := JLINKARM_Reset( );
RetValue := JLINKARM_Close( );
finally
msFirmware.Free;
end;
finally
Reading := False;
HexFirmware.SelStart := 0;
end;
end;
procedure TForm1.btnSaveClick( Sender: TObject );
begin
if HexFirmware.DataSize > 0 then
begin
if btnMemory.ItemIndex = 0 then
begin
DlgSave.FileName := 'RTL8710ROM.bin';
end else if btnMemory.ItemIndex = 1 then
begin
DlgSave.FileName := 'RTL8710RAM.bin';
end else if btnMemory.ItemIndex = 2 then
begin
DlgSave.FileName := 'RTL8710Flash.bin';
end else begin
DlgSave.FileName := 'RTL8710Custom_' + edtAddress.Text + '_' + edtSize.Text + '.bin';
end;
if DlgSave.Execute then
HexFirmware.SaveToFile( DlgSave.FileName );
end;
end;
procedure TForm1.edtAddressExit( Sender: TObject );
begin
try
MemoryAddr := StrToInt( '$' + edtAddress.Text );
except
on E: Exception do
MemoryAddr := $10000000;
end;
edtAddress.Text := IntToHex( MemoryAddr, 8 );
end;
procedure TForm1.edtSizeExit( Sender: TObject );
begin
try
MemorySize := StrToInt( '$' + edtSize.Text );
except
on E: Exception do
MemorySize := $100;
end;
edtSize.Text := IntToHex( MemorySize, 8 );
end;
procedure TForm1.FormCloseQuery( Sender: TObject; var CanClose: Boolean );
begin
CanClose := not Reading;
end;
procedure TForm1.FormCreate( Sender: TObject );
begin
Reading := False;
btnMemory.ItemIndex := 0;
btnMemoryClick( Self );
end;
end.