-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.c
More file actions
381 lines (307 loc) · 11.3 KB
/
storage.c
File metadata and controls
381 lines (307 loc) · 11.3 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
#include "storage.h"
// location of the last written datapoint in units of sizeof SENSOR_datapoint
uint32_t STORAGE_current_location;
// The WEL bit must be set prior to every Page Program, Quad Page Program, Sector Erase, Block
// Erase, Chip Erase
void STORAGE_write_enable() {
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_WRITE_ENABLE);
PORTB |= (1 << STORAGE_CS_PIN);
}
void STORAGE_write_disable() {
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_WRITE_DISABLE);
PORTB |= (1 << STORAGE_CS_PIN);
}
void STORAGE_init() {
DDRB |= (1 << STORAGE_CS_PIN);
PORTB |= (1 << STORAGE_CS_PIN);
STORAGE_write_disable();
}
// The Page Program instruction allows from one byte to 256 bytes (a page) of data to be programmed at
// previously erased (FFh) memory locations. A Write Enable instruction must be executed before the device
// will accept the Page Program Instruction
void STORAGE_write_data(uint32_t addr, uint8_t *write_buffer, uint16_t size) {
addr = addr & STORAGE_MAX_ADDR;
PORTD |= (1 << LED_STORAGE_WRITE);
STORAGE_write_enable();
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_PAGE_PROGRAM);
SPI_transmit((addr >> 16) & 0xff);
SPI_transmit((addr >> 8) & 0xff);
SPI_transmit((addr >> 0) & 0xff);
SPI_transmit_n(write_buffer, size);
PORTB |= (1 << STORAGE_CS_PIN);
_delay_ms(4); // t_pp <= 3ms
STORAGE_write_disable();
PORTD &= ~(1 << LED_STORAGE_WRITE);
}
// The Read Data instruction allows one or more data bytes to be sequentially read from the memory
void STORAGE_read_data(uint32_t addr, uint8_t *read_buffer, uint32_t size) {
addr = addr & STORAGE_MAX_ADDR;
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_READ_DATA);
SPI_transmit((addr >> 16) & 0xff);
SPI_transmit((addr >> 8) & 0xff);
SPI_transmit((addr >> 0) & 0xff);
SPI_receive_n(read_buffer, size);
PORTB |= (1 << STORAGE_CS_PIN);
}
// allows reading at the flash chips maximum clock frequency by adding 8 dummy clocks before receiving data
void STORAGE_read_data_fast(uint32_t addr, uint8_t *read_buffer, uint32_t size) {
addr = addr & STORAGE_MAX_ADDR;
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_READ_DATA);
SPI_transmit((addr >> 16) & 0xff);
SPI_transmit((addr >> 8) & 0xff);
SPI_transmit((addr >> 0) & 0xff);
SPI_transmit(0xff);
SPI_receive_n(read_buffer, size);
PORTB |= (1 << STORAGE_CS_PIN);
}
// The Sector Erase instruction sets all memory within a specified sector (4K-bytes) to the erased state of all
// 1s (FFh). A Write Enable instruction must be executed before the device will accept the Sector Erase
// Instruction
void STORAGE_sector_erase(uint32_t addr) {
addr = addr & STORAGE_MAX_ADDR;
PORTD |= (1 << LED_STORAGE_WRITE);
STORAGE_write_enable();
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_SECTOR_ERASE);
SPI_transmit((addr >> 16) & 0xff);
SPI_transmit((addr >> 8) & 0xff);
SPI_transmit((addr >> 0) & 0xff);
PORTB |= (1 << STORAGE_CS_PIN);
_delay_ms(500); // t_se <= 400ms
STORAGE_write_disable();
PORTD &= ~(1 << LED_STORAGE_WRITE);
}
// The Block Erase instruction sets all memory within a specified block (32K-bytes) to the erased state of all
// 1s (FFh). A Write Enable instruction must be executed before the device will accept the Block Erase
// Instruction
void STORAGE_block_erase_32k(uint32_t addr) {
addr = addr & STORAGE_MAX_ADDR;
PORTD |= (1 << LED_STORAGE_WRITE);
STORAGE_write_enable();
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_BLOCK_ERASE_32K);
SPI_transmit((addr >> 16) & 0xff);
SPI_transmit((addr >> 8) & 0xff);
SPI_transmit((addr >> 0) & 0xff);
PORTB |= (1 << STORAGE_CS_PIN);
_delay_ms(1700); // t_be1 <= 1600ms
STORAGE_write_disable();
PORTD &= ~(1 << LED_STORAGE_WRITE);
}
// The Block Erase instruction sets all memory within a specified block (64K-bytes) to the erased state of all
// 1s (FFh). A Write Enable instruction must be executed before the device will accept the Block Erase
// Instruction
void STORAGE_block_erase_64k(uint32_t addr) {
addr = addr & STORAGE_MAX_ADDR;
PORTD |= (1 << LED_STORAGE_WRITE);
STORAGE_write_enable();
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_BLOCK_ERASE_64K);
SPI_transmit((addr >> 16) & 0xff);
SPI_transmit((addr >> 8) & 0xff);
SPI_transmit((addr >> 0) & 0xff);
PORTB |= (1 << STORAGE_CS_PIN);
_delay_ms(2100); // t_be1 <= 2000ms
STORAGE_write_disable();
PORTD &= ~(1 << LED_STORAGE_WRITE);
}
// The Chip Erase instruction sets all memory within the device to the erased state of all 1s (FFh). A Write
// Enable instruction must be executed before the device will accept the Chip Erase Instruction
void STORAGE_chip_erase() {
PORTD |= (1 << LED_STORAGE_WRITE);
STORAGE_write_enable();
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_CHIP_ERASE);
PORTB |= (1 << STORAGE_CS_PIN);
// t_ce <= 50s
for (uint8_t i = 0; i < 52; i++) {
_delay_ms(1000);
}
STORAGE_write_disable();
PORTD &= ~(1 << LED_STORAGE_WRITE);
}
uint16_t STORAGE_read_device_id() {
uint16_t result;
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_READ_DEVICE_ID);
SPI_transmit(0x00);
SPI_transmit(0x00);
SPI_transmit(0x00);
result = SPI_receive() << 8;
result |= SPI_receive();
PORTB |= (1 << STORAGE_CS_PIN);
return result;
}
uint8_t STORAGE_read_status_1() {
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_READ_STATUS_1);
uint8_t result = SPI_receive();
PORTB |= (1 << STORAGE_CS_PIN);
return result;
}
uint8_t STORAGE_read_status_2() {
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_READ_STATUS_2);
uint8_t result = SPI_receive();
PORTB |= (1 << STORAGE_CS_PIN);
return result;
}
uint8_t STORAGE_read_status_3() {
PORTB &= ~(1 << STORAGE_CS_PIN);
SPI_transmit(STORAGE_CMD_READ_STATUS_3);
uint8_t result = SPI_receive();
PORTB |= (1 << STORAGE_CS_PIN);
return result;
}
void STORAGE_print_page_data(uint32_t addr) {
uint32_t page = addr / 256;
uint32_t aligned_addr = page * 256;
UART_send_string("\r\nflash storage, page ");
UART_send_number_hex_32(page);
UART_send_string(" at address ");
UART_send_number_hex_32(aligned_addr);
UART_send_string("\r\n");
uint32_t flash_read_sample = 0;
for (uint32_t i = 0; i < 256 / (sizeof flash_read_sample); i++) {
STORAGE_read_data(aligned_addr + i * 4, (uint8_t *)&flash_read_sample, 4);
UART_send_number_hex_32(aligned_addr + i * 4);
UART_send_string(": ");
UART_send_number_hex_32(flash_read_sample);
UART_send_string("\r\n");
}
UART_send_string("\r\n");
}
void STORAGE_print_debug_information() {
UART_send_string("Flash chip device ID: ");
UART_send_number_hex(STORAGE_read_device_id());
UART_send_string("\r\n");
UART_send_string("Flash chip status register 1: ");
UART_send_number_hex(STORAGE_read_status_1());
UART_send_string("\r\n");
UART_send_string("Flash chip status register 2: ");
UART_send_number_hex(STORAGE_read_status_2());
UART_send_string("\r\n");
UART_send_string("Flash chip status register 3: ");
UART_send_number_hex(STORAGE_read_status_3());
UART_send_string("\r\n");
}
void STORAGE_save_datapoint(SENSOR_reading sensor_reading, DATETIME time) {
uint32_t location = ++STORAGE_current_location;
if (location * sizeof(SENSOR_datapoint) > STORAGE_MAX_ADDR)
return;
SENSOR_datapoint datapoint = {.temperature = sensor_reading.temperature,
.humidity = sensor_reading.humidity,
.co2concentration = sensor_reading.co2concentration,
.timestamp = CLOCK_datetime_to_reduced(time)};
uint32_t addr = location * sizeof(SENSOR_datapoint);
STORAGE_write_data(addr, (uint8_t *)&datapoint, sizeof(SENSOR_datapoint));
}
SENSOR_datapoint STORAGE_recall_datapoint(uint32_t location) {
SENSOR_datapoint result = {0};
if (location * sizeof(SENSOR_datapoint) > STORAGE_MAX_ADDR) {
return result;
}
uint32_t addr = location * sizeof(SENSOR_datapoint);
STORAGE_read_data(addr, (uint8_t *)&result, sizeof(SENSOR_datapoint));
return result;
}
// scans the memory of the flash chip to find the location to resume writing
uint32_t STORAGE_scan_location() {
PORTD |= (1 << LED_STORAGE_WRITE);
uint32_t current = 0;
// find the page with end of data
for (uint32_t i = 1; i <= STORAGE_MAX_ADDR / sizeof(SENSOR_datapoint); i += 16) {
uint32_t addr = i * sizeof(SENSOR_datapoint) - 1;
uint8_t readings[16] = {0};
for (uint8_t j = 0; j < 16; j++) {
STORAGE_read_data(addr + 16 * j, readings + j, 1);
}
// the last byte of a SENSOR_datapoint is always zero.
// the first page with a 1 at every end of datapoint is therefore the end of written data
uint8_t page_empty = 1;
for (uint8_t j = 0; j < 16; j++) {
if (!(readings[j] & 0x80)) {
page_empty = 0;
break;
}
}
if (page_empty) {
current = i;
break;
}
}
// edge case: no datapoints in flash
if (current == 1) {
uint32_t addr = sizeof(SENSOR_datapoint) - 1;
uint8_t reading = 0;
STORAGE_read_data(addr, &reading, 1);
if (reading & 0x80) {
PORTD &= ~(1 << LED_STORAGE_WRITE);
return -1;
}
}
// find the actual end of data
for (uint32_t i = current; i < STORAGE_MAX_ADDR / sizeof(SENSOR_datapoint); i--) {
uint32_t addr = i * sizeof(SENSOR_datapoint) - 1;
uint8_t reading = 0;
STORAGE_read_data(addr, &reading, 1);
// the last byte of a SENSOR_datapoint is always zero.
// the first reading with a 1 is therefore the end of written data
if (!(reading & 0x80)) {
PORTD &= ~(1 << LED_STORAGE_WRITE);
return i;
}
}
PORTD &= ~(1 << LED_STORAGE_WRITE);
return STORAGE_MAX_ADDR / sizeof(SENSOR_datapoint);
}
// print the entire flash database in CSV-like format
const PROGMEM char STORAGE_data_header[] = "\r\nEnvironmental sensor data dump ";
const PROGMEM char STORAGE_csv_header[] = "\r\nmin\t;hour\t;day\t;month\t;year\t;T[°C]\t;hum[%]\t;co2[ppm]\r\n";
void STORAGE_dump_datapoints_to_uart() {
if (STORAGE_current_location > STORAGE_MAX_ADDR / sizeof(SENSOR_datapoint)) {
return;
}
DATETIME time = CLOCK_read_time();
char time_str[15];
UART_send_string_P(STORAGE_data_header);
CLOCK_date_tostring(time, time_str, 15);
UART_send_string(time_str);
UART_send_char(' ');
CLOCK_tostring(time, time_str, 9);
UART_send_string(time_str);
UART_send_string_P(STORAGE_csv_header);
for (uint32_t i = 0; i < STORAGE_current_location; i++) {
SENSOR_datapoint datapoint = STORAGE_recall_datapoint(i);
DATETIME time = CLOCK_datetime_from_reduced(datapoint.timestamp);
UART_send_number(time.minutes);
UART_send_char('\t');
UART_send_char(';');
UART_send_number(time.hours);
UART_send_char('\t');
UART_send_char(';');
UART_send_number(time.day);
UART_send_char('\t');
UART_send_char(';');
UART_send_number(time.month);
UART_send_char('\t');
UART_send_char(';');
UART_send_number(time.year);
UART_send_char('\t');
UART_send_char(';');
UART_send_float(datapoint.temperature, 4, 1);
UART_send_char('\t');
UART_send_char(';');
UART_send_float(datapoint.humidity, 4, 1);
UART_send_char('\t');
UART_send_char(';');
UART_send_float(datapoint.co2concentration, 4, 0);
UART_send_char('\r');
UART_send_char('\n');
}
}