1 | unit DataFlash;
|
---|
2 |
|
---|
3 | {$MODE Delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | function DataflashReadStatus:byte;
|
---|
8 | procedure DataflashErase;
|
---|
9 | procedure DataflashReadFlashPage(adres:integer; dane:pointer);
|
---|
10 | procedure DataflashWriteFlashPage(adres:integer; dane:pointer);
|
---|
11 | procedure DataflashReadSign(s:pointer);
|
---|
12 |
|
---|
13 | implementation
|
---|
14 |
|
---|
15 | uses Delays, Globals, PinsIO, Processors, SPI;
|
---|
16 |
|
---|
17 | function DataflashReadStatus:byte;
|
---|
18 | begin
|
---|
19 | ChipselectOn;
|
---|
20 | WriteByte($d7);
|
---|
21 | Result:=ReadByte;
|
---|
22 | ChipselectOff;
|
---|
23 | end;
|
---|
24 |
|
---|
25 | procedure DataflashReadDeviceID(var b1,b2,b3:byte);
|
---|
26 | begin
|
---|
27 | ChipselectOn;
|
---|
28 | WriteByte($9f);
|
---|
29 | b1:=ReadByte;
|
---|
30 | b2:=ReadByte;
|
---|
31 | b3:=ReadByte;
|
---|
32 | ChipselectOff;
|
---|
33 | end;
|
---|
34 |
|
---|
35 | procedure DataflashWaitForReady;
|
---|
36 | const
|
---|
37 | t1:Int64 = 0;
|
---|
38 | begin
|
---|
39 | Tic(t1);
|
---|
40 | repeat until ((DataflashReadStatus and $80) = $80) or (TocMS(t1) > 100);
|
---|
41 | end;
|
---|
42 |
|
---|
43 | procedure DataflashErase;
|
---|
44 | var
|
---|
45 | pagesize_big, pages, blocks, blockshift, i:integer;
|
---|
46 | data:array[0..3] of byte;
|
---|
47 | begin
|
---|
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;
|
---|
66 | end;
|
---|
67 |
|
---|
68 | procedure DataflashReadFlashPage(adres:integer; dane:pointer);
|
---|
69 | var
|
---|
70 | pagesize, pagenum:integer;
|
---|
71 | data:array[0..7] of byte;
|
---|
72 | begin
|
---|
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;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | procedure DataflashWriteFlashPage(adres:integer; dane:pointer);
|
---|
93 | var
|
---|
94 | pagesize:integer;
|
---|
95 | data:array[0..3] of byte;
|
---|
96 | begin
|
---|
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;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | procedure DataflashReadSign(s:pointer);
|
---|
114 | var
|
---|
115 | b:byte;
|
---|
116 | ptr:^byte;
|
---|
117 | const
|
---|
118 | data:array[0..2] of byte = (0, 0, 0);
|
---|
119 | begin
|
---|
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];
|
---|
133 | end;
|
---|
134 |
|
---|
135 | end.
|
---|