-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwinbondflash.cpp
More file actions
392 lines (346 loc) · 7.49 KB
/
winbondflash.cpp
File metadata and controls
392 lines (346 loc) · 7.49 KB
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
//#define WB_DEBUG
/*
Winbond spi flash memory chip operating library for Arduino
by WarMonkey (luoshumymail@gmail.com)
for more information, please visit bbs.kechuang.org
latest version available on http://code.google.com/p/winbondflash
*/
#include <Arduino.h>
#include <SPI.h>
#include <errno.h>
#include "winbondflash.h"
//COMMANDS
#define W_EN 0x06 //write enable
#define W_DE 0x04 //write disable
#define R_SR1 0x05 //read status reg 1
#define R_SR2 0x35 //read status reg 2
#define W_SR 0x01 //write status reg
#define PAGE_PGM 0x02 //page program
#define QPAGE_PGM 0x32 //quad input page program
#define BLK_E_64K 0xD8 //block erase 64KB
#define BLK_E_32K 0x52 //block erase 32KB
#define SECTOR_E 0x20 //sector erase 4KB
#define CHIP_ERASE 0xc7 //chip erase
#define CHIP_ERASE2 0x60 //=CHIP_ERASE
#define E_SUSPEND 0x75 //erase suspend
#define E_RESUME 0x7a //erase resume
#define PDWN 0xb9 //power down
#define HIGH_PERF_M 0xa3 //high performance mode
#define CONT_R_RST 0xff //continuous read mode reset
#define RELEASE 0xab //release power down or HPM/Dev ID (deprecated)
#define R_MANUF_ID 0x90 //read Manufacturer and Dev ID (deprecated)
#define R_UNIQUE_ID 0x4b //read unique ID (suggested)
#define R_JEDEC_ID 0x9f //read JEDEC ID = Manuf+ID (suggested)
#define READ 0x03
#define FAST_READ 0x0b
#define SR1_BUSY_MASK 0x01
#define SR1_WEN_MASK 0x02
#define WINBOND_MANUF 0xef
#define DEFAULT_TIMEOUT 200
typedef struct {
winbondFlashClass::partNumberType pn;
uint16_t id;
uint32_t bytes;
uint16_t pages;
uint16_t sectors;
uint16_t blocks;
}partDescriptionType;
static const partDescriptionType partDescription[] PROGMEM = {
{ winbondFlashClass::W25Q80, 0x4014,1048576, 4096, 256, 16 },
{ winbondFlashClass::W25Q16, 0x4015,2097152, 8192, 512, 32 },
{ winbondFlashClass::W25Q32, 0x4016,4194304, 16384,1024,64 },
{ winbondFlashClass::W25Q64, 0x4017,8388608, 32768,2048,128 },
{ winbondFlashClass::W25Q128,0x4018,16777216,65536,4096,256 }
};
uint16_t winbondFlashClass::readSR()
{
uint8_t r1,r2;
select();
transfer(R_SR1);
r1 = transfer(0xff);
deselect();
deselect();//some delay
select();
transfer(R_SR2);
r2 = transfer(0xff);
deselect();
return (((uint16_t)r2)<<8)|r1;
}
uint8_t winbondFlashClass::readManufacturer()
{
uint8_t c;
select();
transfer(R_JEDEC_ID);
c = transfer(0x00);
transfer(0x00);
transfer(0x00);
deselect();
return c;
}
uint64_t winbondFlashClass::readUniqueID()
{
uint64_t uid;
uint8_t *arr;
arr = (uint8_t*)&uid;
select();
transfer(R_UNIQUE_ID);
transfer(0x00);
transfer(0x00);
transfer(0x00);
transfer(0x00);
//for little endian machine only
for(int i=7;i>=0;i--)
{
arr[i] = transfer(0x00);
}
deselect();
return uid;
}
uint16_t winbondFlashClass::readPartID()
{
uint8_t a,b;
select();
transfer(R_JEDEC_ID);
transfer(0x00);
a = transfer(0x00);
b = transfer(0x00);
deselect();
return (a<<8)|b;
}
bool winbondFlashClass::checkPartNo(partNumberType _partno)
{
uint8_t manuf;
uint16_t id;
select();
transfer(R_JEDEC_ID);
manuf = transfer(0x00);
id = transfer(0x00) << 8;
id |= transfer(0x00);
deselect();
#ifdef DEBUG
Serial.print("MANUF=0x");
Serial.print(manuf,HEX);
Serial.print(",ID=0x");
Serial.print(id,HEX);
Serial.println();
#endif
if(manuf != WINBOND_MANUF)
return false;
#ifdef DEBUG
Serial.println("MANUF OK");
#endif
if(_partno == custom)
return true;
#ifdef DEBUG
Serial.println("Not a custom chip type");
#endif
if(_partno == autoDetect)
{
#ifdef DEBUG
Serial.print("Autodetect...");
#endif
for(int i=0;i<sizeof(partDescription)/sizeof(partDescription[0]);i++)
{
if(id == pgm_read_word(&(partDescription[i].id)))
{
partno = (partNumberType)pgm_read_byte(&(partDescription[i].pn));
#ifdef DEBUG
Serial.print("Part no ");
Serial.println(partno);
Serial.println("OK");
#endif
return true;
}
}
if(_partno == autoDetect)
{
#ifdef DEBUG
Serial.println("Failed");
#endif
return false;
}
}
//test chip id and partNo
for(int i=0;i<sizeof(partDescription)/sizeof(partDescription[0]);i++)
{
if(_partno == (partNumberType)pgm_read_byte(&(partDescription[i].pn)))
{
if(id == pgm_read_word(&(partDescription[i].id)))//id equal
return true;
else
return false;
}
}
#ifdef DEBUG
Serial.println("partNumber not found");
#endif
return false;//partNo not found
}
bool winbondFlashClass::busy()
{
uint8_t r1;
select();
transfer(R_SR1);
r1 = transfer(0xff);
deselect();
if(r1 & SR1_BUSY_MASK)
return true;
return false;
}
void winbondFlashClass::setWriteEnable(bool cmd)
{
select();
transfer( cmd ? W_EN : W_DE );
deselect();
}
long winbondFlashClass::bytes()
{
for(int i=0;i<sizeof(partDescription)/sizeof(partDescription[0]);i++)
{
if(partno == (partNumberType)pgm_read_byte(&(partDescription[i].pn)))
{
return pgm_read_dword(&(partDescription[i].bytes));
}
}
return 0;
}
uint16_t winbondFlashClass::pages()
{
for(int i=0;i<sizeof(partDescription)/sizeof(partDescription[0]);i++)
{
if(partno == (partNumberType)pgm_read_byte(&(partDescription[i].pn)))
{
return pgm_read_word(&(partDescription[i].pages));
}
}
return 0;
}
uint16_t winbondFlashClass::sectors()
{
for(int i=0;i<sizeof(partDescription)/sizeof(partDescription[0]);i++)
{
if(partno == (partNumberType)pgm_read_byte(&(partDescription[i].pn)))
{
return pgm_read_word(&(partDescription[i].sectors));
}
}
return 0;
}
uint16_t winbondFlashClass::blocks()
{
for(int i=0;i<sizeof(partDescription)/sizeof(partDescription[0]);i++)
{
if(partno == (partNumberType)pgm_read_byte(&(partDescription[i].pn)))
{
return pgm_read_word(&(partDescription[i].blocks));
}
}
return 0;
}
bool winbondFlashClass::begin(partNumberType _partno)
{
select();
transfer(RELEASE);
deselect();
delayMicroseconds(5);//>3us
#ifdef DEBUG
Serial.println("Chip Released");
#endif
if(!checkPartNo(_partno))
return false;
return true;
}
void winbondFlashClass::end()
{
select();
transfer(PDWN);
deselect();
delayMicroseconds(5);//>3us
}
uint16_t winbondFlashClass::read (uint32_t addr,uint8_t *buf,uint16_t n)
{
if(busy())
return 0;
select();
transfer(READ);
transfer_addr(addr);
for(uint16_t i=0;i<n;i++)
{
buf[i] = transfer(0x00);
}
deselect();
return n;
}
void winbondFlashClass::writePage(uint32_t addr_start,uint8_t *buf)
{
select();
transfer(PAGE_PGM);
transfer(addr_start>>16);
transfer(addr_start>>8);
transfer(0x00);
uint8_t i=0;
do {
transfer(buf[i]);
i++;
} while(i!=0);
deselect();
}
void winbondFlashClass::eraseSector(uint32_t addr_start)
{
select();
transfer(SECTOR_E);
transfer_addr(addr_start);
deselect();
}
void winbondFlashClass::erase32kBlock(uint32_t addr_start)
{
select();
transfer(BLK_E_32K);
transfer_addr(addr_start);
deselect();
}
void winbondFlashClass::erase64kBlock(uint32_t addr_start)
{
select();
transfer(BLK_E_64K);
transfer_addr(addr_start);
deselect();
}
void winbondFlashClass::eraseAll()
{
select();
transfer(CHIP_ERASE);
deselect();
}
void winbondFlashClass::eraseSuspend()
{
select();
transfer(E_SUSPEND);
deselect();
}
void winbondFlashClass::eraseResume()
{
select();
transfer(E_RESUME);
deselect();
}
bool winbondFlashSPI::begin(partNumberType _partno, SPIClass &_spi, uint8_t _nss)
{
spi = _spi;
nss = _nss;
pinMode(MISO,INPUT_PULLUP);
spi.begin();
spi.setBitOrder(MSBFIRST);
spi.setClockDivider(SPI_CLOCK_DIV2);
spi.setDataMode(SPI_MODE0);
deselect();
#ifdef DEBUG
Serial.println(F("SPI OK"));
#endif
return winbondFlashClass::begin(_partno);
}
void winbondFlashSPI::end()
{
winbondFlashClass::end();
spi.end();
}