forked from PaulStoffregen/SerialFlash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialFlashChip.cpp
746 lines (628 loc) · 19.8 KB
/
SerialFlashChip.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
;/* SerialFlash Library - for filesystem-like access to SPI Serial Flash memory
* https://github.com/PaulStoffregen/SerialFlash
* Copyright (C) 2015, Paul Stoffregen, paul@pjrc.com
*
* Development of this library was funded by PJRC.COM, LLC by sales of Teensy.
* Please support PJRC's efforts to develop open source software by purchasing
* Teensy or other genuine PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "SerialFlash.h"
#include "util/SerialFlash_directwrite.h"
#define CSASSERT() DIRECT_WRITE_LOW(cspin_basereg, cspin_bitmask)
#define CSRELEASE() DIRECT_WRITE_HIGH(cspin_basereg, cspin_bitmask)
#define SPICONFIG SPISettings(26000000, MSBFIRST, SPI_MODE0)
uint16_t SerialFlashChip::dirindex = 0;
uint8_t SerialFlashChip::flags = 0;
uint8_t SerialFlashChip::busy = 0;
static volatile IO_REG_TYPE *cspin_basereg;
static IO_REG_TYPE cspin_bitmask;
static SPIClass& SPIPORT = SPI;
#define FLAG_32BIT_ADDR 0x01 // larger than 16 MByte address
#define FLAG_STATUS_CMD70 0x02 // requires special busy flag check
#define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands
#define FLAG_MULTI_DIE 0x08 // multiple die, don't read cross 32M barrier
#define FLAG_256K_BLOCKS 0x10 // has 256K erase blocks
#define FLAG_DIE_MASK 0xC0 // top 2 bits count during multi-die erase
#define CMD_PAGE_DATA_READ 0x13
#define CMD_READ 0x03
#define CMD_FAST_READ 0x0B
#define CMD_PROG_DATA_WRITE 0x02 //write to internal block buffer (2048 bytes + 64ECC)
#define CMD_RAND_PROG_DATA_WRITE 0x84
#define CMD_PROG_EXECUTE 0x10 //write internal block buffer to array
#define CMD_SOFT_DIE_SELECT 0xC2
#define CMD_RESET_DEVICE 0xFF
#define CMD_BLOCK128K_ERASE 0xD8
#define CMD_BADBLOCK_MANAGE 0xA1
#define CMD_READ_BADBLOCK_TABLE 0xA5
bool DIE_NAND = true; // Set true or get detect function first if you use WINBOND SPI serial NAND W25M02GV
int Act_Die = 0; //Current active DIE
uint16_t block = 0;
uint8_t page6 = 0;
uint8_t page16 = 0;
uint16_t column = 0;
uint16_t MaxColumn = 2048;
uint16_t Curr_Page = 0; //In buffer page address
bool PageRenew = true;
bool BuffRDReady = true;
bool BuffWRReady = false;
void SerialFlashChip::reset(void)
{ //Software esetting only for W25M0xGV series NAND!!!!!
SPI.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(CMD_RESET_DEVICE); //up to 500uS wait to become ready after this
CSRELEASE();
SPIPORT.endTransaction();
}
void SerialFlashChip::wait(void)
{
uint32_t status;
//Serial.print("wait-");
while (1) {
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
// all others work by simply reading the status reg
SPIPORT.transfer(0x05);
SPIPORT.transfer(0xC0); //Status register addr for NAND
status = SPIPORT.transfer(0);
CSRELEASE();
SPIPORT.endTransaction();
// Serial.printf("b=%02x.", status & 0xFF);
if (!(status & 1)) break;
}
busy = 0;
}
void SerialFlashChip::read(uint32_t addr, void *buf, uint32_t len)
{
uint8_t *p = (uint8_t *)buf;
uint8_t b, f, status, cmd;
uint32_t rdlen = 0;
memset(p, 0, len);
status = 1;
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(0x05);
SPIPORT.transfer(0xC0);
while (busy){
busy = SPIPORT.transfer(0) & 1;
}
CSRELEASE();
SPIPORT.endTransaction();
do {
// if (DIE_NAND)
// { //W25M0XGV series NAND read
column = addr & 0x7FF;
uint32_t maxlen = 2048 - column;
rdlen = (len <= maxlen) ? len : maxlen;
if ((addr >> 27) != Act_Die)
{ //Change active DIE acording to address
Serial.print("Current DIE = ");Serial.println(Act_Die);
Serial.print("need DIE = ");Serial.print(addr >> 27);Serial.print(" changing..");
Act_Die = addr >> 27;
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(CMD_SOFT_DIE_SELECT);
SPIPORT.transfer(Act_Die);
CSRELEASE();
SPIPORT.endTransaction();
delayMicroseconds(1);
} else if ((addr >> 11) != Curr_Page || PageRenew)
{ //Load page in internal buffer if need
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(CMD_PAGE_DATA_READ);
SPIPORT.transfer(0); //Dummy byte
SPIPORT.transfer16(addr >> 11);
CSRELEASE();
SPIPORT.endTransaction();
Curr_Page = addr >> 11;
PageRenew = false;
// delayMicroseconds(60);
busy = 1; //???????????????????????????????????????????????
if (busy) {
wait();
// Serial.println("Wait...."); ///?????????????????????????????
}
} else {
// SPIPORT.beginTransaction(SPICONFIG);
}
// SPIPORT.beginTransaction(SPICONFIG);
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(CMD_READ);
// SPIPORT.transfer(CMD_FAST_READ);
SPIPORT.transfer16(addr & 0x7FF);
SPIPORT.transfer(0);//Dummy byte
// }
SPIPORT.transfer(p, rdlen);
CSRELEASE();
SPIPORT.endTransaction();
p += rdlen;
addr += rdlen;
len -= rdlen;
} while (len > 0);
SPIPORT.endTransaction();
busy = 1; //???????????????????????????????????????????????
if (busy) {
wait();
// Serial.println("Wait...."); ///?????????????????????????????
}
}
void SerialFlashChip::write(uint32_t addr, const void *buf, uint32_t len)
{
const uint8_t *p = (const uint8_t *)buf;
uint32_t max, pagelen;
//Serial.printf("WR: addr %08X, len %d\n", addr, len);
do {
if (busy) wait();
max = 2048 - (addr & 0x7FF);
pagelen = (len <= max) ? len : max;
//Serial.printf("WR: addr %08X, pagelen %d\n", addr, pagelen);
if ((addr >> 27) != Act_Die)
{ //Select DIE
Serial.print("Current DIE = ");Serial.println(Act_Die);
Serial.print("need DIE = ");Serial.println(addr >> 27);
Act_Die = addr >> 27;
SPI.beginTransaction(SPICONFIG);
CSASSERT();
SPI.transfer(CMD_SOFT_DIE_SELECT);
SPI.transfer(Act_Die);
CSRELEASE();
SPI.endTransaction();
delayMicroseconds(1);
}
SPI.beginTransaction(SPICONFIG);
CSASSERT();
SPI.transfer(CMD_PAGE_DATA_READ);
SPI.transfer(0); //Dummy byte
SPI.transfer16(addr >> 11);
CSRELEASE();
SPI.endTransaction();
delayMicroseconds(50);
Curr_Page = addr >> 11;
busy = 1;
if (busy) wait();
SPI.beginTransaction(SPICONFIG);
CSASSERT();
SPI.transfer(0x06); // write enable command
CSRELEASE();
SPI.endTransaction();
delayMicroseconds(1);
SPI.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(CMD_RAND_PROG_DATA_WRITE);// random program buffer page command
SPIPORT.transfer16(addr & 0x7FF);
addr += pagelen;
len -= pagelen;
do {
SPIPORT.transfer(*p++);
} while (--pagelen > 0);
CSRELEASE();
delayMicroseconds(1);
CSASSERT();
SPIPORT.transfer(CMD_PROG_EXECUTE);
SPIPORT.transfer(0); //Dummy byte
SPIPORT.transfer16((addr - 1) >> 11);
CSRELEASE();
SPIPORT.endTransaction();
delayMicroseconds(50);
PageRenew = true;
busy = 1;
} while (len > 0);
PageRenew = true;
}
void SerialFlashChip::eraseAll()
{
Serial.println("Init erase ALL prog....");
if (busy) wait();
uint8_t id[5];
readID(id);
//Serial.printf("ID: %02X %02X %02X\n", id[0], id[1], id[2]);
if (id[0] == 0x20 && id[2] >= 0x20 && id[2] <= 0x22) {
// Micron's multi-die chips require special die erase commands
// N25Q512A 20 BA 20 2 dies 32 Mbyte/die 65 nm transitors
// N25Q00AA 20 BA 21 4 dies 32 Mbyte/die 65 nm transitors
// MT25QL02GC 20 BA 22 2 dies 128 Mbyte/die 45 nm transitors
uint8_t die_count = 2;
if (id[2] == 0x21) die_count = 4;
uint8_t die_index = flags >> 6;
//Serial.printf("Micron die erase %d\n", die_index);
flags &= 0x3F;
if (die_index >= die_count) return; // all dies erased :-)
uint8_t die_size = 2; // in 16 Mbyte units
if (id[2] == 0x22) die_size = 8;
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(0x06); // write enable command
CSRELEASE();
delayMicroseconds(1);
CSASSERT();
// die erase command
SPIPORT.transfer(0xC4);
SPIPORT.transfer16((die_index * die_size) << 8);
SPIPORT.transfer16(0x0000);
CSRELEASE();
//Serial.printf("Micron erase begin\n");
flags |= (die_index + 1) << 6;
} else if (DIE_NAND){
for(uint16_t i = 0; i < 2048; i++){
uint32_t address = i*64;
eraseBlock(address);
}
} else {
// All other chips support the bulk erase command
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
// write enable command
SPIPORT.transfer(0x06);
CSRELEASE();
delayMicroseconds(1);
CSASSERT();
// bulk erase command
SPIPORT.transfer(0xC7);
CSRELEASE();
SPIPORT.endTransaction();
}
busy = 3;
}
void SerialFlashChip::eraseBlock(uint32_t addr)
{
uint8_t f = flags;
if (busy) wait();
SPI.beginTransaction(SPICONFIG);
CSASSERT();
SPI.transfer(0x06); // write enable command
CSRELEASE();
SPI.endTransaction();
if (DIE_NAND){
if (addr >> 16 != Act_Die)
{ //Select DIE
Serial.print("Current DIE = ");Serial.println(Act_Die);
Serial.print("need DIE = ");Serial.println(addr >> 16);
Act_Die = addr >> 16;
SPI.beginTransaction(SPICONFIG);
CSASSERT();
SPI.transfer(CMD_SOFT_DIE_SELECT);
SPI.transfer(Act_Die);
CSRELEASE();
SPI.endTransaction();
//push write enable command
SPI.beginTransaction(SPICONFIG);
CSASSERT();
SPI.transfer(0x06); // write enable command
CSRELEASE();
SPI.endTransaction();
}
SPI.beginTransaction(SPICONFIG);
CSASSERT();
SPI.transfer(CMD_BLOCK128K_ERASE);
SPI.transfer(0);//Dummy
SPI.transfer16(addr & 0xFFFF);
}
CSRELEASE();
SPI.endTransaction();
//Serial.println("Block erase...");
busy = 1;
PageRenew = true;
}
/*
void SerialFlashChip::eraseBlock(uint32_t addr)
{
uint8_t f = flags;
if (busy) wait();
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(0x06); // write enable command
CSRELEASE();
delayMicroseconds(1);
CSASSERT();
if (f & FLAG_32BIT_ADDR) {
SPIPORT.transfer(0xD8);
SPIPORT.transfer16(addr >> 16);
SPIPORT.transfer16(addr);
} else {
SPIPORT.transfer16(0xD800 | ((addr >> 16) & 255));
SPIPORT.transfer16(addr);
}
CSRELEASE();
SPIPORT.endTransaction();
busy = 2;
}
*/
bool SerialFlashChip::ready()
{
uint32_t status;
if (!busy) return true;
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
if (flags & FLAG_STATUS_CMD70) {
// some Micron chips require this different
// command to detect program and erase completion
SPIPORT.transfer(0x70);
status = SPIPORT.transfer(0);
CSRELEASE();
SPIPORT.endTransaction();
//Serial.printf("ready=%02x\n", status & 0xFF);
if ((status & 0x80) == 0) return false;
} else {
// all others work by simply reading the status reg
SPIPORT.transfer(0x05);
if(DIE_NAND) SPIPORT.transfer(0xC0); //status register address C0 for NAND
status = SPIPORT.transfer(0);
CSRELEASE();
SPIPORT.endTransaction();
//Serial.printf("ready=%02x\n", status & 0xFF);
if ((status & 1)) return false;
}
busy = 0;
if (flags & 0xC0) {
// continue a multi-die erase
Serial.println("Erase All function start.....");
eraseAll();
return false;
}
return true;
}
#define ID0_WINBOND 0xEF
#define ID0_SPANSION 0x01
#define ID0_MICRON 0x20
#define ID0_MACRONIX 0xC2
#define ID0_SST 0xBF
#define ID1_WIN_NAND 0xAB
#define ID2_WIN_NAND 0x21
//#define FLAG_32BIT_ADDR 0x01 // larger than 16 MByte address
//#define FLAG_STATUS_CMD70 0x02 // requires special busy flag check
//#define FLAG_DIFF_SUSPEND 0x04 // uses 2 different suspend commands
//#define FLAG_256K_BLOCKS 0x10 // has 256K erase blocks
bool SerialFlashChip::begin(SPIClass& device, uint8_t pin)
{
SPIPORT = device;
return begin(pin);
}
bool SerialFlashChip::begin(uint8_t pin)
{
uint8_t id[5];
uint8_t f;
uint32_t size;
cspin_basereg = PIN_TO_BASEREG(pin);
cspin_bitmask = PIN_TO_BITMASK(pin);
SPIPORT.begin();
pinMode(pin, OUTPUT);
CSRELEASE();
readID(id);
// f = 0;
// f |= FLAG_32BIT_ADDR;
// SPIPORT.beginTransaction(SPICONFIG);
/*
if (size > 16777216) {
// more than 16 Mbyte requires 32 bit addresses
f |= FLAG_32BIT_ADDR;
SPIPORT.beginTransaction(SPICONFIG);
if (id[0] == ID0_SPANSION) {
// spansion uses MSB of bank register
CSASSERT();
SPIPORT.transfer16(0x1780); // bank register write
CSRELEASE();
} else {
// micron & winbond & macronix use command
CSASSERT();
SPIPORT.transfer(0x06); // write enable
CSRELEASE();
delayMicroseconds(1);
CSASSERT();
SPIPORT.transfer(0xB7); // enter 4 byte addr mode
CSRELEASE();
}
SPIPORT.endTransaction();
if (id[0] == ID0_MICRON) f |= FLAG_MULTI_DIE;
}*/
if (id[0] == ID0_SPANSION) {
// Spansion has separate suspend commands
f |= FLAG_DIFF_SUSPEND;
if (!id[4]) {
// Spansion chips with id[4] == 0 use 256K sectors
f |= FLAG_256K_BLOCKS;
}
}
if (id[0] == ID0_MICRON) {
// Micron requires busy checks with a different command
f |= FLAG_STATUS_CMD70; // TODO: all or just multi-die chips?
}
if (id[0] == ID0_WINBOND && id[1] == ID1_WIN_NAND && id[2] == ID2_WIN_NAND) {
//Change to first DIE
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPI.transfer(CMD_SOFT_DIE_SELECT);
// Serial.print(" Change DIE, sending SPI cmd CMD_SOFT_DIE_SELECT");
SPI.transfer(0x00);
// Act_Die = 0;
CSRELEASE();
SPIPORT.endTransaction();
delayMicroseconds(50);
//unlock for write
Serial.println("Found Winbond NAND flash 1, unlocking ...");
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(0x1F); //Write status reg
SPIPORT.transfer(0xA0);
SPIPORT.transfer(0x00);
CSRELEASE();
SPIPORT.endTransaction();
delayMicroseconds(50);
//Change to second DIE
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPI.transfer(CMD_SOFT_DIE_SELECT);
// Serial.print(" Change DIE, sending SPI cmd CMD_SOFT_DIE_SELECT");
SPI.transfer(0x01);
// Act_Die = 1;
CSRELEASE();
SPIPORT.endTransaction();
delayMicroseconds(50);
//unlock for write
Serial.println("Found Winbond NAND flash 2, unlocking ...");
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(0x1F);
SPIPORT.transfer(0xA0);
SPIPORT.transfer(0x00);
CSRELEASE();
SPIPORT.endTransaction();
delayMicroseconds(50);
//Change to first DIE
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPI.transfer(CMD_SOFT_DIE_SELECT);
// Serial.print(" Change DIE, sending SPI cmd CMD_SOFT_DIE_SELECT");
SPI.transfer(0x00);
Act_Die = 0;
CSRELEASE();
SPIPORT.endTransaction();
delayMicroseconds(50);
}
flags = f;
readID(id);
return true;
}
// chips tested: https://github.com/PaulStoffregen/SerialFlash/pull/12#issuecomment-169596992
//
/*
void SerialFlashChip::sleep()
{
if (busy) wait();
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(0xB9); // Deep power down command
CSRELEASE();
}
void SerialFlashChip::wakeup()
{
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(0xAB); // Wake up from deep power down command
CSRELEASE();
}
*/
void SerialFlashChip::readID(uint8_t *buf)
{
if (busy) wait();
SPI.beginTransaction(SPICONFIG);
CSASSERT();
SPI.transfer(0x9F);
buf[0] = SPI.transfer(0); // manufacturer ID
buf[1] = SPI.transfer(0); // memory type
if(!buf[0] && buf[1]){ //skip dummy byte for W25M02GVZEIG
buf[0] = buf [1];
buf[1] = SPI.transfer(0);
buf[2] = SPI.transfer(0);
DIE_NAND = true;
}else {
buf[2] = SPI.transfer(0); // capacity
}
CSRELEASE();
SPI.endTransaction();
//Serial.printf("ID: %02X %02X %02X\n", buf[0], buf[1], buf[2]);
}
void SerialFlashChip::readSerialNumber(uint8_t *buf) //needs room for 8 bytes
{
if (busy) wait();
SPIPORT.beginTransaction(SPICONFIG);
CSASSERT();
SPIPORT.transfer(0x4B);
SPIPORT.transfer16(0);
SPIPORT.transfer16(0);
for (int i=0; i<8; i++) {
buf[i] = SPIPORT.transfer(0);
}
CSRELEASE();
SPIPORT.endTransaction();
// Serial.printf("Serial Number: %02X %02X %02X %02X %02X %02X %02X %02X\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
}
uint32_t SerialFlashChip::capacity(const uint8_t *id)
{
uint32_t n = 1048576; // unknown chips, default to 1 MByte
if (id[2] >= 16 && id[2] <= 31) {
n = 1ul << id[2];
} else
if (id[2] >= 32 && id[2] <= 37) {
n = 1ul << (id[2] - 6);
} else
if ((id[0]==0 && id[1]==0 && id[2]==0) ||
(id[0]==255 && id[1]==255 && id[2]==255)) {
n = 0;
}
if (id[0] == 0xEF && id[1] == 0xAB && id[2] == 0x21) {
n = n*2;
}
//Serial.printf("capacity %lu\n", n);
return n;
}
uint32_t SerialFlashChip::blockSize()
{
// Spansion chips >= 512 mbit use 256K sectors
if (flags & FLAG_256K_BLOCKS) return 262144;
// everything else seems to have 64K sectors
if (DIE_NAND) return 2*65536;
return 65536;
}
/*
Chip Uniform Sector Erase
20/21 52 D8/DC
----- -- -----
W25Q64CV 4 32 64
W25Q128FV 4 32 64
S25FL127S 64
N25Q512A 4 64
N25Q00AA 4 64
S25FL512S 256
SST26VF032 4
W25M02GVZEIG 128
*/
// size sector busy pgm/erase chip
// Part Mbyte kbyte ID bytes cmd suspend erase
// ---- ---- ----- -------- --- ------- -----
// Winbond W25Q64CV 8 64 EF 40 17
// Winbond W25Q128FV 16 64 EF 40 18 05 single 60 & C7
// Winbond W25Q256FV 32 64 EF 40 19
// Winbond W25M02GVZEIG 2x128 128 EF AB 21 05 C0 none none
// Winbond W25M01GV 1x128 128 EF AA 21 05 C0
// Spansion S25FL064A 8 ? 01 02 16
// Spansion S25FL127S 16 64 01 20 18 05
// Spansion S25FL128P 16 64 01 20 18
// Spansion S25FL256S 32 64 01 02 19 05 60 & C7
// Spansion S25FL512S 64 256 01 02 20
// Macronix MX25L12805D 16 ? C2 20 18
// Macronix MX66L51235F 64 C2 20 1A
// Numonyx M25P128 16 ? 20 20 18
// Micron M25P80 1 ? 20 20 14
// Micron N25Q128A 16 64 20 BA 18
// Micron N25Q512A 64 ? 20 BA 20 70 single C4 x2
// Micron N25Q00AA 128 64 20 BA 21 single C4 x4
// Micron MT25QL02GC 256 64 20 BA 22 70 C4 x2
// SST SST25WF010 1/8 ? BF 25 02
// SST SST25WF020 1/4 ? BF 25 03
// SST SST25WF040 1/2 ? BF 25 04
// SST SST25VF016B 1 ? BF 25 41
// SST26VF016 ? BF 26 01
// SST26VF032 ? BF 26 02
// SST25VF032 4 64 BF 25 4A
// SST26VF064 8 ? BF 26 43
// LE25U40CMC 1/2 64 62 06 13
SerialFlashChip SerialFlash;