-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataFlashDevice.cpp
388 lines (316 loc) · 7.33 KB
/
DataFlashDevice.cpp
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "DataFlashDevice.h"
#define SPIF_TIMEOUT 10000
enum ops {
DF_ID = 0x9F, // Read ID
DF_STATUS = 0xD7, // Read Status
DF_CAR_HF_2 = 0x1B, // Continuous Array Read (High Frequency)
DF_BUF_1_WRITE = 0x84, // Buffer 1 Write
DF_BUF_1_PROG_W_ERASE = 0x83, // Buffer 1 to Main Memory Page Program with Built-In Erase
DF_BUF_1_WRITE_W_PROG_W_ERASE = 0x82, // Main Memory Page Program through Buffer 1 with Built-In Erase
DF_PAGE_ERASE = 0x81, // Page Erase
};
enum density {
DF_2MB = 0x03, // 2Mbits
DF_4MB = 0x04, // 4Mbits
DF_8MB = 0x05, // 8Mbits
DF_16MB = 0x06, // 16Mbits
DF_32MB = 0x07, // 32Mbits
DF_64MB = 0x08, // 64Mbits
};
DataFlashDevice::DataFlashDevice(PinName mosi, PinName miso, PinName sclk,
PinName cs, PinName wp, int freq) :
_spi(mosi, miso, sclk), _cs(cs), _wp(wp), _size(0) {
_cs = 1;
_wp = 0;
_spi.frequency(freq);
}
int DataFlashDevice::id() {
int id = 0;
_cs = 0;
_spi.write(DF_ID);
id = (_spi.write(0x00) << 8);
id |= _spi.write(0x00);
_cs = 1;
return id;
}
void DataFlashDevice::wren(bool en) {
_wp = en;
}
// return the Status
int DataFlashDevice::status() {
int status = 0;
_cs = 0;
_spi.write(DF_STATUS);
status = (_spi.write(0x00));
_cs = 1;
return status;
}
int DataFlashDevice::init() {
int _id = 0;
int _status = 0;
int density =0;
bool binary = 0;
wren(1);
_id = id();
density = _id & 0x1f;
_status = status();
binary = _status & 0x1;
if (density == DF_2MB) // 2Mbits
{
pages = 1024; // Number of pages
blocks = 128; // Number of blocks
if (binary) {
pagesize = 256;
} else {
pagesize = 264;
}
devicesize = pages * pagesize;
} else if (density == DF_4MB) // 4Mbits
{
pages = 2048;
blocks = 256;
if (binary) {
pagesize = 256;
} else {
pagesize = 264;
}
devicesize = pages * pagesize;
} else if (density == DF_8MB) // 8Mbits
{
pages = 4096;
blocks = 512;
if (binary) {
pagesize = 256;
} else {
pagesize = 264;
}
devicesize = pages * pagesize;
} else if (density == DF_16MB) // 16Mbits
{
pages = 4096;
blocks = 512;
if (binary) {
pagesize = 512;
} else {
pagesize = 528;
}
devicesize = pages * pagesize;
} else if (density == DF_32MB) // 32Mbits
{
pages = 8192;
blocks = 1024;
if (binary) {
pagesize = 512;
} else {
pagesize = 528;
}
devicesize = pages * pagesize;
} else if (density == DF_64MB) // 64Mbits
{
pages = 8192;
blocks = 1024;
if (_status & 0x1) {
pagesize = 1024;
} else {
pagesize = 1056;
}
devicesize = pages * pagesize;
} else {
devicesize = -1;
pages = -1;
pagesize = -1;
blocks = -1;
}
return 0;
}
int DataFlashDevice::deinit() {
// Latch write disable just to keep noise
// from changing the device
wren(false);
return 0;
}
void DataFlashDevice::busy() {
while (isbusy());
}
bool DataFlashDevice::isbusy() {
return (!(status() & 0x80));
}
int DataFlashDevice::read(void *buffer, bd_addr_t addr, bd_size_t len) {
// Check the address and size fit onto the chip.
MBED_ASSERT(is_valid_read(addr, len));
int address;
address = _getpaddr(addr) | _getbaddr(addr);
busy();
_cs = 0;
_spi.write(DF_CAR_HF_2);
_sendaddr(address);
// 2 dummy bytes
_spi.write(0x00);
_spi.write(0x00);
// clock out the data
char *buf;
buf = (char*) buffer;
while (len > 0) {
*buf = _spi.write(0x00);
buf++;
len--;
}
_cs = 1;
busy();
return 0;
}
int DataFlashDevice::program(const void *buffer, bd_addr_t addr,
bd_size_t size) {
//TODO we could check if an enture page can be erased and written saving a bit of time.
int err;
// Check the address and size fit onto the chip.
MBED_ASSERT(is_valid_program(addr, size));
char *buf;
buf = (char*) buffer;
while (size > 0) {
wren(1);
_cs = 0;
_spi.write(DF_BUF_1_WRITE); // write to buffer 1
_sendaddr(0); // write from addr 0
for (int i = 0; i < pagesize; i++) {
_spi.write(*buf);
buf++;
}
_cs = 1;
busy();
// write buffer 1 to memory with erase.
_cs = 0;
_spi.write(DF_BUF_1_PROG_W_ERASE);
_sendaddr(_getpaddr(addr));
_cs = 1;
/*
_cs = 0;
_spi.write(DF_BUF_1_WRITE_W_PROG_W_ERASE); // Main Memory Page Program through Buffer 1 with Built-In Erase
_sendaddr(_getpaddr(addr)); // write from start of page
for (int i = 0; i < pagesize; i++) {
_spi.write(*buf);
buf++;
}
_cs = 1;
*/
busy();
addr += pagesize;
size -= pagesize;
wait_ms(1);
err = _sync();
wren(0);
if (err) {
return err;
}
}
return 0;
}
int DataFlashDevice::_getpaddr(int address) {
int paddr;
if (pagesize == 256) {
paddr = address & 0xffffff00;
} else if (pagesize == 264) {
paddr = (address/264)<<9;
} else if (pagesize == 512) {
paddr = address & 0xfffffe00;
} else if (pagesize == 528) {
paddr = (address/528)<<10;
} else if (pagesize == 1024) {
paddr = address & 0xfffffc00;
} else if (pagesize == 1056) {
paddr = (address/1056)<<10;
} else {
paddr = -1;
}
return (paddr);
}
int DataFlashDevice::_getbaddr(int address) {
int baddr;
if (pagesize == 256) {
baddr = address & 0xff;
}else if (pagesize == 264) {
baddr = address %264;
} else if (pagesize == 512) {
baddr = address & 0x1ff;
}else if (pagesize == 528) {
baddr = address %528;
} else if (pagesize == 1024) {
baddr = address & 0x3ff;
} else if (pagesize == 1056) {
baddr = address %1056;
} else {
baddr = -1;
}
return (baddr);
}
int DataFlashDevice::_sync() {
for (int i = 0; i < SPIF_TIMEOUT; i++) {
if (!isbusy()) {
return 0;
}
wait_ms(1);
return 0;
}
return BD_ERROR_DEVICE_ERROR;
}
void DataFlashDevice::_sendaddr(int address) {
_spi.write(address >> 16);
_spi.write(address >> 8);
_spi.write(address);
}
int DataFlashDevice::erase(bd_addr_t addr, bd_size_t size) {
//we are using Program with Built-In Erase so no need to erase before program.
eraseBlock(addr,size);
}
int DataFlashDevice::eraseBlock(bd_addr_t addr, bd_size_t size) {
// Check the address and size fit onto the chip.
int err;
MBED_ASSERT(is_valid_erase(addr, size));
while (size > 0) {
wren(1);
// Erase sector
// TODO if an entire block or sector can be erases we should do that instead.
busy();
_cs = 0;
_spi.write(DF_PAGE_ERASE);
_sendaddr(_getpaddr(addr));
_cs = 1;
busy();
addr += pagesize;
size -= pagesize;
wren(0);
err = _sync();
if (err) {
return err;
}
}
return 0;
}
//technically smaller reads and non erase writes can be done but not a lot of point.
bd_size_t DataFlashDevice::get_read_size() const {
return pagesize;
}
bd_size_t DataFlashDevice::get_program_size() const {
return pagesize;
}
bd_size_t DataFlashDevice::get_erase_size() const {
return pagesize;
}
bd_size_t DataFlashDevice::size() const {
return devicesize;
}