-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom.c
566 lines (475 loc) · 15.2 KB
/
eeprom.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
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
#include "common/eeprom/eeprom.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
header_t g_headers[MAX_HEADER_COUNT] = {0};
eeprom_write_status_t e_wr_stat = {.has_write_started=0, .is_write_complete=0};
uint8_t g_numStructs; //number of entries in header
uint16_t g_eeprom_size;
uint8_t g_device_addr;
I2C_TypeDef *g_eeprom_i2c;
void downloadChunk(uint16_t from_addr, void *to_addr, uint16_t size);
void uploadByte(uint16_t addr, uint8_t val);
void uploadChunkBlocking(void *from_addr, uint16_t to_addr, uint16_t size);
uint8_t uploadChunkPeriodic(void *from_addr, uint16_t to_addr, uint16_t size);
header_t *findHeader(char name[]);
void addHeaderEntry(header_t *newHeader);
void updateHeaderEntry(header_t *header);
void sortHeaders();
uint16_t spaceAvailable(uint16_t address);
uint16_t eepromMalloc(uint16_t size);
void removeFromEeprom(char name[]);
void splitVersion(uint8_t *version, uint8_t *overwrite);
void combineVersion(uint8_t *version, uint8_t *overwrite);
void loadHeaderEntries();
void delay(uint8_t ms);
//reads chunk of data
void downloadChunk(uint16_t from_addr, void *to_addr, uint16_t size)
{
uint8_t ret = 0;
// set cursor
ret = PHAL_I2C_gen_start(g_eeprom_i2c, SET_ADDRESS(g_device_addr, WRITE_ENABLE), 2, PHAL_I2C_MODE_TX);
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_write(g_eeprom_i2c, from_addr >> 8); // High
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_write(g_eeprom_i2c, from_addr & 0xFF); // Low
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_gen_stop(g_eeprom_i2c);
if (!ret) errorFound(COM_ERROR);
while(size > 0xFF) // can only receive 255 at a time
{
ret = PHAL_I2C_gen_start(g_eeprom_i2c, SET_ADDRESS(g_device_addr, READ_ENABLE), 0xFF, PHAL_I2C_MODE_RX);
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_read_multi(g_eeprom_i2c, to_addr, 0xFF);
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_gen_stop(g_eeprom_i2c);
if (!ret) errorFound(COM_ERROR);
size -= 0xFF;
to_addr += 0xFF;
}
ret = PHAL_I2C_gen_start(g_eeprom_i2c, SET_ADDRESS(g_device_addr, READ_ENABLE), (uint8_t) size, PHAL_I2C_MODE_RX);
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_read_multi(g_eeprom_i2c, to_addr, (uint8_t) size);
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_gen_stop(g_eeprom_i2c);
if (!ret) errorFound(COM_ERROR);
}
//writes single byte
void uploadByte(uint16_t addr, uint8_t val)
{
uint8_t ret = 0;
ret = PHAL_I2C_gen_start(g_eeprom_i2c, SET_ADDRESS(g_device_addr, WRITE_ENABLE), 3, PHAL_I2C_MODE_TX);
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_write(g_eeprom_i2c, addr >> 8); // High Addr
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_write(g_eeprom_i2c, addr & 0xFF); // Low Addr
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_write(g_eeprom_i2c, val); // Data
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_gen_stop(g_eeprom_i2c);
}
//uploads chunk ignoring page breaks
void eUploadRaw(void *from_addr, uint16_t to_addr, uint16_t size)
{
uint8_t ret = 0;
ret = PHAL_I2C_gen_start(g_eeprom_i2c, SET_ADDRESS(g_device_addr, WRITE_ENABLE), 2 + size, PHAL_I2C_MODE_TX);
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_write(g_eeprom_i2c, to_addr >> 8); // High Addr
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_write(g_eeprom_i2c, to_addr & 0xFF); // Low Addr
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_write_multi(g_eeprom_i2c, from_addr, size); // Data
if (!ret) errorFound(COM_ERROR);
ret = PHAL_I2C_gen_stop(g_eeprom_i2c);
if (!ret) errorFound(COM_ERROR);
}
//breaks data into chunks to prevent crossing page boundary
void uploadChunkBlocking(void *from_addr, uint16_t to_addr, uint16_t size)
{
uint16_t next_boundary = (to_addr / PAGE_SIZE + 1) * PAGE_SIZE;
uint16_t current_addr = to_addr;
uint16_t end_loc = to_addr + (size - 1);
uint8_t *from = from_addr;
uint8_t chunkSize; //number of bytes copying from mem
do
{
//send from current to boundary or end loc, whichever is less
if (end_loc - current_addr < next_boundary - current_addr)
{
chunkSize = end_loc - current_addr + 1;
}
else
{
chunkSize = next_boundary - current_addr;
}
delay(E_DELAY);
eUploadRaw(from + (current_addr - to_addr), current_addr, chunkSize);
current_addr += chunkSize;
next_boundary = (current_addr / PAGE_SIZE + 1) * PAGE_SIZE;
} while (current_addr < end_loc);
}
//breaks data into chunks to prevent crossing page boundary
uint8_t uploadChunkPeriodic(void *from_addr, uint16_t to_addr, uint16_t size)
{
if (!e_wr_stat.has_write_started)
{
e_wr_stat.has_write_started = 1;
e_wr_stat.is_write_complete = 0;
e_wr_stat.next_boundary = (to_addr / PAGE_SIZE + 1) * PAGE_SIZE;
e_wr_stat.current_addr = to_addr;
e_wr_stat.end_loc = to_addr + (size - 1);
e_wr_stat.from = from_addr;
}
uint8_t chunkSize; //number of bytes copying from mem
//send from current to boundary or end loc, whichever is less
if (e_wr_stat.end_loc - e_wr_stat.current_addr <
e_wr_stat.next_boundary - e_wr_stat.current_addr)
{
chunkSize = e_wr_stat.end_loc - e_wr_stat.current_addr + 1;
}
else
{
chunkSize = e_wr_stat.next_boundary - e_wr_stat.current_addr;
}
eUploadRaw(e_wr_stat.from + (e_wr_stat.current_addr - to_addr), e_wr_stat.current_addr, chunkSize);
e_wr_stat.current_addr += chunkSize;
e_wr_stat.next_boundary = (e_wr_stat.current_addr / PAGE_SIZE + 1) * PAGE_SIZE;
if (e_wr_stat.current_addr < e_wr_stat.end_loc)
{
return false;
}
else
{
e_wr_stat.is_write_complete = 1;
e_wr_stat.has_write_started = 0;
return true;
}
}
//transfers all values to given huart
// TODO: PHAL UART Library
// void eepromDump(UART_HandleTypeDef huart)
// {
// uint8_t MSG[PAGE_SIZE + 1] = {0};
// for (uint16_t i = 0; i < g_eeprom_size; i += PAGE_SIZE)
// {
// downloadChunk(i, MSG, PAGE_SIZE);
// //HAL_UART_Transmit(&huart, MSG, sizeof(MSG) - 1, 100);
// HAL_Delay(10);
// }
// }
//Sets all addresses to 0
void eepromWipe()
{
uint8_t data[PAGE_SIZE] = {0};
for (uint16_t i = 0; i < g_eeprom_size; i += PAGE_SIZE)
{
uploadChunkBlocking(data, i, 32);
}
}
//returns null if none
header_t *findHeader(char name[])
{
//search through headers until name match
for (int i = 0; i < g_numStructs; i++)
{
if (strncmp(name, g_headers[i].name, NAME_SIZE) == 0)
{
return &g_headers[i];
}
}
return NULL;
}
//adds Header to eeprom
void addHeaderEntry(header_t *new_header)
{
new_header->address_on_eeprom = eepromMalloc(new_header->size);
g_numStructs += 1;
delay(E_DELAY); // minimum 5 ms between writing
uploadByte(0, g_numStructs); //increment struct num by 1
if (g_numStructs > MAX_HEADER_COUNT)
{
errorFound(MAX_HEADER);
}
uploadChunkBlocking(new_header, (g_numStructs - 1) * HEADER_SIZE + 1, HEADER_SIZE);
sortHeaders(); //added new item, put it in place
}
//finds location of header in eeprom and updates it
void updateHeaderEntry(header_t *header)
{
//somehow find where its located
//current process is slower due to searching through actual eeprom mem
char name_found[NAME_SIZE];
//converting allows for pointer addition
uint8_t *header_loc = (uint8_t*) header;
for (int i = 0; i < g_numStructs; i++)
{
downloadChunk(i * HEADER_SIZE + 1, &name_found, NAME_SIZE);
if (strncmp(name_found, header->name, NAME_SIZE) == 0)
{
//found the correct header to update
uploadChunkBlocking(header_loc + NAME_SIZE, i * HEADER_SIZE + 1 + NAME_SIZE, HEADER_SIZE - NAME_SIZE);
return;
}
}
//header not found, should never reach this point...
errorFound(HEADER_NOT_FOUND);
}
//links struct ptr with a header from eeprom, overwrite protect active high
//returns 1 if it was not found to currently exist
uint8_t eepromLinkStruct(void *ptr, uint16_t size, char name[], uint8_t version, uint8_t overwrite_protection)
{
uint8_t is_new_struct = 0;
header_t *a_header = NULL;
a_header = findHeader(name);
if (version > MAX_VERSION)
{
version = MAX_VERSION;
}
uint8_t overwrite_previous;
//if node found, extract overwrite bit from version
if (a_header != NULL)
{
splitVersion(&(a_header->version), &overwrite_previous);
}
if (overwrite_protection != 0)
{
overwrite_protection = 1;
}
if (a_header == NULL)
{
//struct not in eeprom in any form
is_new_struct = 1;
a_header = &g_headers[g_numStructs]; //0 based list, no +1
strcpy(a_header->name, name);
combineVersion(&version, &overwrite_protection);
a_header->version = version;
a_header->size = size;
a_header->ptr_to_data = ptr; //link :D
addHeaderEntry(a_header); //update eAddress too
uploadChunkBlocking(a_header->ptr_to_data, a_header->address_on_eeprom, a_header->size);
}
else if (a_header->size != size || a_header->version != version)
{
//overwrite and header change
if (spaceAvailable(a_header->address_on_eeprom) < a_header->size)
{
//can't place struct here, move
a_header->address_on_eeprom = eepromMalloc(a_header->size);
//change of address, sort g_headers
sortHeaders();
}
combineVersion(&version, &overwrite_protection);
a_header->version = version;
a_header->size = size;
a_header->ptr_to_data = ptr; //link :D
updateHeaderEntry(a_header);
uploadChunkBlocking(a_header->ptr_to_data, a_header->address_on_eeprom, a_header->size);
}
else if (overwrite_previous != overwrite_protection)
{
combineVersion(&version, &overwrite_protection);
a_header->version = version;
a_header->ptr_to_data = ptr; //link :D
updateHeaderEntry(a_header);
}
else
{
//struct info matches that in eeprom
a_header->ptr_to_data = ptr; //link :D
}
return is_new_struct;
}
//populate linked list with header info from eeprom
void loadHeaderEntries()
{
for (int i = 0; i < g_numStructs; i++)
{
downloadChunk(i * HEADER_SIZE + 1, &g_headers[i], HEADER_SIZE);
}
}
//sort headers by increasing eaddress
void sortHeaders()
{
header_t temp; //temporary buffer
for (int i = 0; i < g_numStructs; i++)
{
for (int j = 0; j < g_numStructs - i - 1; j++)
{
if (g_headers[j].address_on_eeprom > g_headers[j + 1].address_on_eeprom)
{
temp = g_headers[j + 1];
g_headers[j + 1] = g_headers[j];
g_headers[j] = temp;
}
}
}
}
//returns available space to use at an address
uint16_t spaceAvailable(uint16_t address)
{
if (address > g_eeprom_size)
{
return 0;
}
//find header with first address greater than eAddress
for (int i = 0; i < g_numStructs; i++)
{
if (g_headers[i].address_on_eeprom > address)
{
return g_headers[i].address_on_eeprom - address;
}
}
//no headers with address after said address
return g_eeprom_size - address;
}
/*returns eeprom address with space for set size
null if not available, relies on the fact that
the linked list is sorted by increasing
eaddress*/
uint16_t eepromMalloc(uint16_t size)
{
if (g_numStructs > 0)
{
// header_t *current = g_headers;
//check between end of headers and first node
if (g_headers->address_on_eeprom - (MAX_HEADER_COUNT * HEADER_SIZE + 1) >= size)
{
return MAX_HEADER_COUNT * HEADER_SIZE + 1;
}
//check between individual nodes
for (int i = 0; i < g_numStructs - 1; i++)
{
if (g_headers[i + 1].address_on_eeprom - (g_headers[i].address_on_eeprom + g_headers[i].size) >= size)
{
return g_headers[i].address_on_eeprom + g_headers[i].size;
}
}
//reached last entry, check is space between last and end of eeprom
if (g_eeprom_size - g_headers[g_numStructs].address_on_eeprom + g_headers[g_numStructs].size >= size)
{
return g_headers[g_numStructs - 1].address_on_eeprom + g_headers[g_numStructs - 1].size;
}
errorFound(MAX_MEM);
return 0; //no space available
}
else
{
return MAX_HEADER_COUNT * HEADER_SIZE + 1;
}
}
//remove header from eeprom
void removeFromEeprom(char name[])
{
// This function finds the last header entry in
// eeprom and overwrites the one to be deleted
uint8_t header_buffer[HEADER_SIZE]; //stores last header entry in eeprom
char name_buffer[NAME_SIZE];
// copy last header info into header_buffer
downloadChunk((g_numStructs - 1) * HEADER_SIZE + 1, header_buffer, HEADER_SIZE);
// find unused header pos and overwrite
for (int i = 0; i < g_numStructs; i++)
{
downloadChunk(i * HEADER_SIZE + 1, &name_buffer, NAME_SIZE);
if (strncmp(name_buffer, name, NAME_SIZE) == 0)
{
// found the correct header to update
uploadChunkBlocking(header_buffer, i * HEADER_SIZE + 1, HEADER_SIZE);
i = g_numStructs; // exit loop
}
}
// decrement num g_headers
g_numStructs -= 1;
delay(E_DELAY); // minimum 5 ms delay between write cycles
uploadByte(0, g_numStructs);
}
//removes unused headers (those without a linked pointer) from eeprom
void eepromCleanHeaders()
{
for (int i = 0; i < g_numStructs; i++)
{
if (g_headers[i].ptr_to_data == NULL && !(g_headers[i].version >> OVERWRITE_BIT))
{
//unused header and no overwrite
//DECREMENTS G_NUM_STRUCTS
removeFromEeprom(g_headers[i].name);
//move headers back one to fill gap
for (int j = i; j < g_numStructs; j++)
{
g_headers[j] = g_headers[j + 1]; //intended to reach 1+
}
i-=1; //indexes all shifted back one now
}
}
}
//loads current header info
void eepromInitialize(uint16_t eepromSpace, uint8_t address, I2C_TypeDef *i2c)
{
g_eeprom_size = eepromSpace;
g_device_addr = address;
g_eeprom_i2c = i2c;
downloadChunk(0x00, &g_numStructs, 1);
loadHeaderEntries();
sortHeaders();
//eepromWipe();
}
//loads struct from mem, returns 1 if unknown struct
uint8_t eepromLoadStruct(char name[])
{
for (int i = 0; i < g_numStructs; i++)
{
if (strncmp(name, g_headers[i].name, NAME_SIZE) == 0)
{
//found desired node
downloadChunk(g_headers[i].address_on_eeprom, g_headers[i].ptr_to_data, g_headers[i].size);
return 0;
}
}
return 1;
}
//saves struct to mem, returns 1 if unknown struct
uint8_t eepromSaveStructBlocking(char name[])
{
for (int i = 0; i < g_numStructs; i++)
{
if (strncmp(name, g_headers[i].name, NAME_SIZE) == 0)
{
//found desired node
uploadChunkBlocking(g_headers[i].ptr_to_data, g_headers[i].address_on_eeprom, g_headers[i].size);
return 0;
}
}
return 1;
}
//saves struct to mem, returns 1 if unknown struct, call until eeprom is_write_complete = 1
uint8_t eepromSaveStructPeriodic(char name[])
{
for (int i = 0; i < g_numStructs; i++)
{
if (strncmp(name, g_headers[i].name, NAME_SIZE) == 0)
{
//found desired node
uploadChunkPeriodic(g_headers[i].ptr_to_data, g_headers[i].address_on_eeprom, g_headers[i].size);
return 0;
}
}
return 1;
}
//splits version into overwrite and version
void splitVersion(uint8_t *version, uint8_t *overwrite)
{
*overwrite = *version >> OVERWRITE_BIT;
*version = *version & OVERWRITE_MASK;
}
//combines overwrite with version
void combineVersion(uint8_t *version, uint8_t *overwrite)
{
*version = *version | (*overwrite << OVERWRITE_BIT);
}
void delay(uint8_t ms)
{
uint32_t ticks = (SystemCoreClock / 1000) * ms / 4;
for(int i = 0; i < ticks; i++)
{
__asm__("nop");
}
}