-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi_flash.pde
364 lines (270 loc) · 6.33 KB
/
spi_flash.pde
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
//#define DEBUG
#define DEBUG_LOW
// SPI ports
#define CS 10
#define MOSI 11
#define MISO 12
#define SCLK 13
// opcodes
#define WREN 0x06 // write enable
#define WRDI 0x04 // write disable
#define WRSR 0x01 // write status register
#define REMS 0x90 // read identification
#define RDID 0x9F // read identification
#define RDSR 0x05 // read status register
#define READ 0x03 // read
#define SCTE 0x20 // sector erase
#define BLKE 0xD8 // block erase
#define CHPE 0xC7 // chip erase
#define PAPR 0x02 // page program
// we shift this byte to the SPDR in order to receive a byte from the chip
#define DUMMY 0x00
// minimum performance timings
#define SECTOR_ERASE_TIME 300
#define BLOCK_ERASE_TIME 2000
#define CHIP_ERASE_TIME 15000
#define PAGE_PROGRAM_TIME 5
#define WRITE_STATUS_REGISTER_TIME 100
// start and end addresses
#define LOW_ADDRESS 0x00000000
#define HIGH_ADDRESS 0x000fffff
#define PAGE_SIZE 256
// should be a divisor of the memory area size, we will read
// this could be higher than 256, but then some terminal programs
// on my PC lose bytes, it seems
#define BUFFER_SIZE 256
// speed
#define SERIAL_SPEED 115200
// this is just a dummy variable
byte clr;
void setup() {
Serial.begin(SERIAL_SPEED);
pinMode(MISO, INPUT);
pinMode(MOSI, OUTPUT);
pinMode(SCLK, OUTPUT);
pinMode(CS, OUTPUT);
deselect_chip();
// SPCR = 01010011
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk, 250 kHz rate (slowest)
#ifndef DEBUG
SPCR = (1<<SPE) | (1<<MSTR) | (0<<SPR1) | (0<<SPR0);
// clear the status and data registers
clr = SPSR;
clr = SPDR;
delay(10);
#else // debug code
#ifdef DEBUG_LOW
digitalWrite(CS, LOW);
digitalWrite(MOSI, LOW);
digitalWrite(SCLK, LOW);
#else
digitalWrite(CS, HIGH);
digitalWrite(MOSI, HIGH);
digitalWrite(SCLK, HIGH);
#endif
#endif
}
void loop() {
byte incomingByte = 0;
#ifndef DEBUG
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
switch (incomingByte) {
case READ:
readEeprom();
break;
case SCTE:
eraseSector(LOW_ADDRESS);
break;
case BLKE:
eraseBlock(LOW_ADDRESS);
break;
case CHPE:
eraseChip();
break;
case RDSR:
readStatus();
break;
case REMS:
readManufacturerId();
break;
case PAPR:
programPage();
break;
}
}
#endif
}
void readEeprom() {
byte buffer[BUFFER_SIZE];
unsigned long address;
for (address = LOW_ADDRESS; address <= HIGH_ADDRESS; address = address + BUFFER_SIZE) {
readBuffer(address, buffer);
Serial.write(buffer, BUFFER_SIZE);
}
}
// read BUFFER_SIZE bytes starting at address
void readBuffer(unsigned long address, byte *buffer) {
int i;
select_chip();
delay(150);
//transmit read opcode
spi_transfer(READ);
transferAddress(address);
for (i = 0; i < BUFFER_SIZE; i++) {
// get data byte; transfer dummy byte
buffer[i] = spi_transfer(0xFF);
}
// release chip, signal transfer end
deselect_chip();
}
void programChip() {
// Serial.println("programChip");
byte page[PAGE_SIZE];
unsigned long address;
for (address = LOW_ADDRESS; address <= HIGH_ADDRESS; address = address + PAGE_SIZE) {
for (int i = 0; i < PAGE_SIZE; ) {
if (Serial.available() > 0) {
page[i++] = Serial.read();
}
}
//programPage(address, page);
readStatus();
}
}
void programPage() {
unsigned long address = 0;
byte page[PAGE_SIZE];
for (int i = 2; i >= 0; ) {
if (Serial.available() > 0) {
long b = Serial.read();
// Serial.write(b);
address = address + (b << (i * 8));
i--;
}
}
// Serial.print("address: ");
// Serial.println(address, DEC);
for (int i = 0; i < PAGE_SIZE; ) {
if (Serial.available() > 0) {
byte b = Serial.read();
//Serial.write(b);
page[i++] = b;
}
}
#if 1
enableWrite();
select_chip();
// send page program command
spi_transfer(PAPR);
// send address
transferAddress(address);
// send data
for (int i = 0; i < PAGE_SIZE; i++) {
// get data byte; transfer dummy byte
spi_transfer(page[i]);
}
deselect_chip();
#endif
delay(PAGE_PROGRAM_TIME);
readStatus();
}
void readStatus() {
byte statusRegister;
select_chip();
spi_transfer(RDSR);
statusRegister = spi_transfer(DUMMY);
Serial.write(statusRegister);
deselect_chip();
}
void readManufacturerId() {
byte data;
select_chip();
spi_transfer(REMS);
// 2 dummy bytes
spi_transfer(DUMMY);
spi_transfer(DUMMY);
// 0x00 will output the manufacturer's ID first; 0x01 will output device ID first
spi_transfer(0X00);
// 1 byte manufacturer ID
Serial.write(spi_transfer(DUMMY));
// 1 byte device ID
Serial.write(spi_transfer(DUMMY));
deselect_chip();
}
void eraseSector(unsigned long address) {
readStatus();
enableWrite();
readStatus();
select_chip();
spi_transfer(SCTE);
transferAddress(address);
deselect_chip();
readStatus();
delay(SECTOR_ERASE_TIME);
readStatus();
}
void eraseBlock(unsigned long address) {
readStatus();
enableWrite();
readStatus();
select_chip();
spi_transfer(BLKE);
transferAddress(address);
deselect_chip();
readStatus();
delay(BLOCK_ERASE_TIME);
readStatus();
}
void transferAddress(unsigned long address) {
spi_transfer((byte)(address>>16));
spi_transfer((byte)(address>>8));
spi_transfer((byte)(address));
}
void eraseChip() {
readStatus();
enableWrite();
readStatus();
select_chip();
spi_transfer(CHPE);
deselect_chip();
readStatus();
delay(CHIP_ERASE_TIME);
readStatus();
}
void enableWrite() {
select_chip();
spi_transfer(WREN);
deselect_chip();
delay(WRITE_STATUS_REGISTER_TIME);
}
void disableWrite() {
select_chip();
spi_transfer(WRDI);
deselect_chip();
delay(WRITE_STATUS_REGISTER_TIME);
}
byte spi_transfer(byte data) {
spiWrite(data);
return spiRead();
}
void spiWrite(byte data) {
SPDR = data;
}
byte spiRead() {
while (!(SPSR & (1<<SPIF))) {
// Wait for the end of the transmission
};
// return the received byte
return SPDR;
}
void select_chip() {
digitalWrite(CS, LOW);
delay(1);
}
void deselect_chip() {
digitalWrite(CS, HIGH);
delay(4);
}