source: ISPProgrammer/ISPprog/DataFlash.pas

Last change on this file was 363, checked in by chronos, 13 years ago
  • Added: Package ISPProgrammer for in-system programming of various chips. Supports Dallas ISP protocol, Presto, Rabbit RFU and some others Atmel devices.
File size: 3.1 KB
Line 
1unit DataFlash;
2
3{$MODE Delphi}
4
5interface
6
7function DataflashReadStatus:byte;
8procedure DataflashErase;
9procedure DataflashReadFlashPage(adres:integer; dane:pointer);
10procedure DataflashWriteFlashPage(adres:integer; dane:pointer);
11procedure DataflashReadSign(s:pointer);
12
13implementation
14
15uses Delays, Globals, PinsIO, Processors, SPI;
16
17function DataflashReadStatus:byte;
18begin
19 ChipselectOn;
20 WriteByte($d7);
21 Result:=ReadByte;
22 ChipselectOff;
23end;
24
25procedure DataflashReadDeviceID(var b1,b2,b3:byte);
26begin
27 ChipselectOn;
28 WriteByte($9f);
29 b1:=ReadByte;
30 b2:=ReadByte;
31 b3:=ReadByte;
32 ChipselectOff;
33end;
34
35procedure DataflashWaitForReady;
36const
37 t1:Int64 = 0;
38begin
39 Tic(t1);
40 repeat until ((DataflashReadStatus and $80) = $80) or (TocMS(t1) > 100);
41end;
42
43procedure DataflashErase;
44var
45 pagesize_big, pages, blocks, blockshift, i:integer;
46 data:array[0..3] of byte;
47begin
48 if devicenr < 1 then
49 Exit;
50 pagesize_big:=(1 shl (Signatures[devicenr].fpage - 1)) + (1 shl (Signatures[devicenr].fpage - 6));
51 pages:=Signatures[devicenr].fsize div pagesize_big;
52 blocks:=pages div 8;
53 blockshift:=Signatures[devicenr].fpage - 5;
54 for i:=0 to blocks - 1 do
55 begin
56 ChipselectOn;
57 data[0] := $50;
58 data[1] := Hi(word(i shl blockshift));
59 data[2] := Lo(word(i shl blockshift));
60 data[3] := $00;
61 WriteBytes(@data, 4);
62 Sync;
63 ChipselectOff;
64 DataflashWaitForReady;
65 end;
66end;
67
68procedure DataflashReadFlashPage(adres:integer; dane:pointer);
69var
70 pagesize, pagenum:integer;
71 data:array[0..7] of byte;
72begin
73 if devicenr < 1 then
74 Exit;
75 pagesize:=Signatures[devicenr].fpagesize;
76 pagenum:=adres div pagesize;
77 adres:=pagenum shl Signatures[devicenr].fpage;
78 ChipselectOn;
79 data[0] := $D2;
80 data[1] := (adres shr 16) and $FF;
81 data[2] := (adres shr 8) and $FF;
82 data[3] := adres and $FF;
83 data[4] := $00;
84 data[5] := $00;
85 data[6] := $00;
86 data[7] := $00;
87 WriteBytes(@data, 8);
88 ReadBytes(dane, pagesize);
89 ChipselectOff;
90end;
91
92procedure DataflashWriteFlashPage(adres:integer; dane:pointer);
93var
94 pagesize:integer;
95 data:array[0..3] of byte;
96begin
97 if devicenr < 1 then
98 Exit;
99 pagesize:=Signatures[devicenr].fpagesize;
100 adres:=(adres div pagesize) shl Signatures[devicenr].fpage;
101 ChipselectOn;
102 data[0] := $82;
103 data[1] := (adres shr 16) and $FF;
104 data[2] := (adres shr 8) and $FF;
105 data[3] := adres and $FF;
106 WriteBytes(@data, 4);
107 WriteBytes(dane, pagesize);
108 Sync;
109 ChipselectOff;
110 DataflashWaitForReady;
111end;
112
113procedure DataflashReadSign(s:pointer);
114var
115 b:byte;
116 ptr:^byte;
117const
118 data:array[0..2] of byte = (0, 0, 0);
119begin
120 ptr:=s;
121 b:=DataflashReadStatus;
122 DataflashReadDeviceID(data[0], data[1], data[2]);
123 if (data[0] <> $ff) and (data[1] <> $ff) and (data[2] <> $ff) then
124 ptr^:=b and $3d // memory size mask and page size bit
125 else if b <> $ff then
126 ptr^:=b and $3c // memory size mask only
127 else
128 ptr^:=b; // $FF and no Manufacturer/Device ID
129 Inc(ptr);
130 ptr^:=data[0];
131 Inc(ptr);
132 ptr^:=data[1];
133end;
134
135end.
Note: See TracBrowser for help on using the repository browser.