-
Notifications
You must be signed in to change notification settings - Fork 1
/
flash.c
309 lines (249 loc) · 5.85 KB
/
flash.c
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
#include "globals.h"
#include "spi.h"
#include "flash.h"
#include "serial.h"
#include "clock.h"
#include <stdio.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#define MOD_ADLER 65521
uint8_t flash_buf[256];
uint16_t flash_addr;
volatile uint16_t flash_buf_ctr;
bool_t flag_flash_power_hold = false;
void flash_enable_write(void);
void flash_wait_for_wel(void);
uint8_t flash_status(void);
void flash_wait_for_idle(void);
bool_t flash_busy(void);
void flash_init(void) {
DDRC |= _BV(DDC4); // /CS
strcpy_P(serial_out, PSTR("\r\nFlash initialized\r\n"));
usart_send();
while (flag_serial_sending);
//flash_powerdown();
}
void flash_doheader() {
if (flag_want_header) {
flash_buf[0] = 0xFF;
flash_buf[1] = 0xEE;
flash_buf[2] = 0;
flash_buf[3] = clock.hours;
flash_buf[4] = clock.minutes;
flash_buf[5] = clock.seconds;
flash_buf[6] = buttonPresses;
flash_buf_ctr = 7;
flag_want_header = false;
}
}
void flash_condwrite() {
if (flash_buf_ctr > 253) { // Leave room for 3 byte writes
//uint8_t i;
//bool_t nonzero = false;
if (flag_flash_verbose) {
sprintf(serial_out, "\r\ncondwrite to flash at %d %ld\r\n", flash_addr, clock_ticks);
usart_send();
while (flag_serial_sending);
}
flash_write(flash_addr++);
flash_buf_ctr = 0;
flag_want_header = true;
}
}
uint8_t flash_status(void) {
uint8_t status;
flash_select();
spi_send(INSTR_READ_STATUS1);
status = spi_send(DONTCARE);
flash_deselect();
//wdt_reset();
return status;
}
void flash_scan() {
uint8_t data;
if (flag_flash_verbose) {
strcpy_P(serial_out, PSTR("\r\nPowering up flash\r\n"));
usart_send();
while (flag_serial_sending);
}
flash_powerup();
if (flag_flash_verbose) {
strcpy_P(serial_out, PSTR("\r\nWaiting for idle\r\n"));
usart_send();
while (flag_serial_sending);
}
flash_wait_for_idle();
for(flash_addr=0; flash_addr < 4096; flash_addr++) {
flash_select();
spi_send(INSTR_READ_DATA);
spi_send(flash_addr >> 8);
spi_send(flash_addr & 0xFF);
spi_send(1); // Check the second byte of each page
data = spi_send(DONTCARE);
flash_deselect();
wdt_reset();
if (data == 0xFF)
break;
}
flash_powerdown();
}
uint32_t flash_adler32(void) {
uint32_t a = 1, b = 0;
uint16_t i;
flash_powerup();
flash_power_hold(1);
for(flash_addr=0; flash_addr < 4096; flash_addr++) {
bool_t verified = false;
while (!verified) {
flash_fast_read(flash_addr);
verified = flash_verify(flash_addr);
if (!verified)
_delay_ms(25);
/*
sprintf(serial_out, "%u %d\r\n", flash_addr, verified);
usart_send();
while (flag_serial_sending);
*/
}
if (flash_buf[1] == 0xFF)
break;
for(i=0; i<256; i++) {
a = (a + flash_buf[i]) % MOD_ADLER;
b = (b + a) % MOD_ADLER;
}
/*
sprintf(serial_out, "%u %lu %lu\r\n", flash_addr, a, b);
usart_send();
while (flag_serial_sending);
*/
wdt_reset();
clock_update();
}
flash_power_hold(0);
flash_powerdown();
return (b << 16) | a;
}
void flash_enable_write(void) {
flash_wait_for_idle();
do {
flash_select();
spi_send(INSTR_WRITE_ENABLE);
flash_deselect();
} while (!(flash_status() & STATUS_WEL));
}
void flash_write(uint16_t addr) {
int i;
uint8_t retval;
if (flag_flash_verbose) {
sprintf(serial_out, "\r\nwrite to flash at %d %ld\r\n", flash_addr, clock_ticks);
usart_send();
while (flag_serial_sending);
}
do {
flash_powerup();
flash_enable_write();
flash_wait_for_idle();
//_delay_ms(50);
flash_select();
spi_send(INSTR_PAGE_PROGRAM);
spi_send(addr >> 8);
spi_send(addr & 0xFF);
spi_send(0);
for(i=0; i<256; i++) {
spi_send(flash_buf[i]);
}
flash_deselect();
// Now verify that the write actually "took"
flash_wait_for_idle();
flash_select();
spi_send(INSTR_READ_DATA);
spi_send(addr >> 8);
spi_send(addr & 0xFF);
spi_send(1);
retval = spi_send(DONTCARE);
flash_deselect();
} while (flash_buf[1] != 0xff && retval == 0xff);
flash_powerdown();
}
void flash_read(uint16_t addr) {
int i;
flash_powerup();
flash_select();
spi_send(INSTR_READ_DATA);
spi_send(addr >> 8);
spi_send(addr & 0xFF);
spi_send(0);
for(i=0; i<256; i++) {
flash_buf[i] = spi_send(DONTCARE);
PORTD ^= _BV(PORTD6);
}
flash_deselect();
flash_powerdown();
}
void flash_fast_read(uint16_t addr) {
int i;
flash_powerup();
flash_select();
spi_send(INSTR_FAST_READ);
spi_send(addr >> 8);
spi_send(addr & 0xFF);
spi_send(0);
spi_send(DONTCARE);
for(i=0; i<256; i++) {
flash_buf[i] = spi_send(DONTCARE);
PORTD ^= _BV(PORTD6);
}
flash_deselect();
flash_powerdown();
}
bool_t flash_verify(uint16_t addr) {
int i;
flash_powerup();
flash_select();
spi_send(INSTR_READ_DATA);
spi_send(addr >> 8);
spi_send(addr & 0xFF);
spi_send(0);
for(i=0; i<256; i++) {
if (flash_buf[i] != spi_send(DONTCARE)) {
return false;
}
PORTD ^= _BV(PORTD6);
}
flash_deselect();
flash_powerdown();
return true;
}
void flash_erase(void) {
flash_powerup();
_delay_ms(50);
flash_enable_write();
flash_wait_for_idle();
flash_select();
spi_send(INSTR_CHIP_ERASE);
flash_deselect();
flash_powerdown();
flag_want_header = true;
flash_buf_ctr = 0;
}
void flash_wait_for_idle(void) {
flash_select();
spi_send(INSTR_READ_STATUS1);
while (spi_send(DONTCARE) & STATUS_BUSY);
flash_deselect();
}
void flash_wait_for_wel(void) {
flash_select();
spi_send(INSTR_READ_STATUS1);
while (!(spi_send(DONTCARE) & STATUS_WEL));
flash_deselect();
}
bool_t flash_busy(void) {
uint8_t status;
flash_select();
spi_send(INSTR_READ_STATUS1);
status = spi_send(DONTCARE);
flash_deselect();
return status & STATUS_BUSY;
}