forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Storage.h
493 lines (414 loc) · 11.4 KB
/
Storage.h
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
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __STORAGE_H__
#define __STORAGE_H__
#include "Debug.h"
#ifdef ARDUINO_ARCH_STM32F1
#include "flash_stm32.h"
#endif
namespace as {
class InternalEprom {
#ifdef ARDUINO_ARCH_STM32F1
#if MCU_STM32F103CB
#define FlashPageSize 0x400
#define FlashStartAddress 0x0801fc00 // Page127
#elif defined(MCU_STM32F103C8)
#define FlashPageSize 0x400
#define FlashStartAddress 0x0800fc00 // Page63
#else
#error Unknown CPU type
#endif
// we mirror 1 Flash Page into RAM
uint8_t data[FlashPageSize];
void eeprom_read_block(void* buf,const void* addr,size_t size) {
uintptr_t offset = (uintptr_t)addr;
if( offset + size < sizeof(data) ) {
memcpy(buf,&data[offset],size);
}
}
void eeprom_write_block(const void* buf,void* addr,size_t size) {
uintptr_t offset = (uintptr_t)addr;
if( offset + size < sizeof(data) ) {
memcpy(&data[offset],buf,size);
}
}
#endif
#ifndef ARDUINO
// we mirror 1 Flash Page into RAM
uint8_t data[1024];
inline void memcpy(void* dest,const void* src,size_t size) {
uint8_t* d = (uint8_t*)dest;
const uint8_t* s = (const uint8_t*)src;
for( size_t i=0; i<size; ++i ) {
*d++ = *s++;
}
}
void eeprom_read_block(void* buf,const void* addr,size_t size) {
uintptr_t offset = (uintptr_t)addr;
if( offset + size < sizeof(data) ) {
memcpy(buf,&data[offset],size);
}
}
void eeprom_write_block(const void* buf,void* addr,size_t size) {
uintptr_t offset = (uintptr_t)addr;
if( offset + size < sizeof(data) ) {
memcpy(&data[offset],buf,size);
}
}
#endif
public:
InternalEprom () {
#ifdef ARDUINO_ARCH_STM32F1
// copy data from FLASH into RAM
uint16_t* towrite = (uint16_t*)data;
uint16_t *toread = (uint16_t*)(uintptr_t)FlashStartAddress;
for( size_t i=0; i<sizeof(data)/2; ++i ) {
*(towrite + i) = *(toread + i);
}
#endif
}
bool present () {
return true;
}
uint16_t size () {
return 1024;
}
void store () {
#ifdef ARDUINO_ARCH_STM32F1
// copy data from RAM to FLASH
FLASH_Unlock(); //unlock flash writing
FLASH_ErasePage(FlashStartAddress);
uint16_t* toread = (uint16_t*)data;
for( size_t i=0; i<sizeof(data)/2; ++i ) {
FLASH_ProgramHalfWord(FlashStartAddress+i+i,*(toread+i));
}
FLASH_Lock();
#endif
}
uint8_t getByte (uint16_t addr) {
uint8_t b = 0;
eeprom_read_block(&b,(void*)(uintptr_t)addr,1);
return b;
}
bool setByte (uint16_t addr, uint8_t d) {
uint8_t b = d;
eeprom_write_block(&b,(void*)(uintptr_t)addr,1);
return true;
}
bool setData (uint16_t addr,uint8_t* buf,uint16_t size) {
eeprom_write_block(buf,(void*)(uintptr_t)addr,size);
return true;
}
bool getData (uint16_t addr,uint8_t* buf,uint16_t size) {
eeprom_read_block(buf,(const void*)(uintptr_t)addr,size);
return true;
}
bool clearData (uint16_t addr, uint16_t size) {
for( uint16_t i=0; i<size; ++i) {
setByte(addr+i,0);
}
return true;
}
};
#if defined(TwoWire_h) || defined(_WIRE_H_) || defined(_TWOWIRE_H_) || defined(_WIREBASE_H_)
// with help of https://github.com/JChristensen/extEEPROM
template <uint8 ID,uint16_t PAGES,uint8_t PAGESIZE>
class at24cX {
public:
at24cX () {}
bool present () {
Wire.beginTransmission(ID);
Wire.write(0); //high addr byte
Wire.write(0); //low addr byte
return Wire.endTransmission() == 0;
}
uint16_t size () {
return PAGES * PAGESIZE;
}
void store () {}
uint8 getByte (uint16_t addr) {
uint8 b = 0;
Wire.beginTransmission(ID);
Wire.write(addr >> 8);
Wire.write(addr & 0xff);
if( Wire.endTransmission() == 0 ) {
Wire.requestFrom(ID,(uint8_t)1);
b = Wire.read();
}
return b;
}
bool setByte (uint16_t addr, uint8 d) {
bool success = true;
Wire.beginTransmission(ID);
Wire.write(addr >> 8);
Wire.write(addr & 0xff);
Wire.write(d);
success = Wire.endTransmission() == 0;
// wait for write operation finished
if( success == true ) {
success = waitComplete();
}
return success;
}
uint16_t calcBlockSize(uint16_t addr, uint16_t size) {
uint16_t block = PAGESIZE - (addr % PAGESIZE);
// BUFFER_LENGTH from Wire.h - 2 byte address
block = (BUFFER_LENGTH - 2) < block ? BUFFER_LENGTH - 2 : block;
return (size < block) ? size : block;
}
bool setData (uint16_t addr,uint8* buf,uint16_t size) {
// DPRINT("setData: ");DHEX(addr);DPRINT(" ");DDECLN(size);
bool success = true;
while( success == true && size > 0 ) {
uint16_t towrite = calcBlockSize(addr, size);
// DPRINT(" write: ");DHEX(addr);DPRINT(" ");DDECLN(towrite);
Wire.beginTransmission(ID);
Wire.write(addr >> 8);
Wire.write(addr & 0xff);
uint8_t done = 0;
while( done < towrite ) {
done++;
Wire.write(*buf++);
}
success = Wire.endTransmission() == 0;
// wait for write operation finished
if( success == true ) {
success = waitComplete();
}
else {
DPRINTLN(F("ERROR EEPROM WRITE"));
}
size -= towrite;
addr += towrite;
}
return success;
}
bool getData (uint16_t addr,uint8* buf,uint16_t size) {
bool success = true;
while( success == true && size > 0 ) {
uint16_t toread = calcBlockSize(addr, size);
//DPRINT("Read: ");DHEX(addr);DPRINT(" ");DHEXLN(toread);
Wire.beginTransmission(ID);
Wire.write(addr >> 8);
Wire.write(addr & 0xff);
success = Wire.endTransmission() == 0;
if( success == true ) {
Wire.requestFrom(ID,(uint8_t)toread);
uint8_t done = 0;
while( done < toread ) {
done++;
*buf++ = (uint8_t)Wire.read();
}
}
size -= toread;
addr += toread;
}
return success;
}
bool clearData (uint16_t addr, uint16_t size) {
// DPRINT("clearData: ");DHEX(addr);DPRINT(" ");DDECLN(size);
bool success = true;
while( success == true && size > 0 ) {
uint16_t towrite = calcBlockSize(addr, size);
// DPRINT(" clear: ");DHEX(addr);DPRINT(" ");DDECLN(towrite);
Wire.beginTransmission(ID);
Wire.write(addr >> 8);
Wire.write(addr & 0xff);
uint8_t done = 0;
while( done < towrite ) {
done++;
Wire.write(0);
}
success = Wire.endTransmission() == 0;
// wait for write operation finished
if( success == true ) {
success = waitComplete();
}
else {
DPRINTLN(F("ERROR EEPROM CLEAR"));
}
size -= towrite;
addr += towrite;
}
// DPRINTLN("clearData done");
return success;
}
bool waitComplete () {
//wait up to 50ms for the write to complete
for (uint8_t i=50; i; --i) {
_delay_ms(1); //no point in waiting too fast
if( present() == true ) {
return true;
}
}
DPRINTLN(F("ERROR EEPROM WAIT"));
return false;
}
};
template <uint8_t ID,uint16_t PAGES,uint8_t PAGESIZE>
class CachedAt24cX : public at24cX<ID,PAGES,PAGESIZE> {
uint8_t pagecache[PAGESIZE];
uint16_t pageaddr;
bool dirty;
public:
typedef at24cX<ID,PAGES,PAGESIZE> Base;
CachedAt24cX () : pageaddr(0xffff), dirty(false) {}
void store () {
writecache();
}
protected:
void writecache () {
if( pageaddr != 0xffff && dirty == true ) {
// DPRINT("WRITECACHE "); DHEXLN(pageaddr);
Base::setData(pageaddr, pagecache, PAGESIZE);
dirty = false;
}
}
uint8_t* fillcache(uint16_t addr) {
uint16_t paddr = addr & ~(PAGESIZE-1);
if( pageaddr != paddr ) {
writecache();
pageaddr = paddr;
// DPRINT("FILLCACHE "); DHEXLN(pageaddr);
Base::getData(pageaddr,pagecache,PAGESIZE);
dirty = false;
}
return pagecache;
}
void clearcache () {
writecache();
// DPRINT("CLEARCACHE\n");
pageaddr = 0xffff;
}
public:
uint8_t getByte (uint16_t addr) {
fillcache(addr);
return pagecache[addr - pageaddr];
}
bool setByte (uint16_t addr, uint8_t d) {
fillcache(addr);
pagecache[addr - pageaddr] = d;
dirty = true;
return true;
}
bool getData (uint16_t addr,uint8_t* buf,uint16_t size) {
writecache();
return Base::getData(addr, buf, size);
}
bool setData (uint16_t addr,uint8_t* buf,uint16_t size) {
clearcache();
return Base::setData(addr, buf, size);
}
bool clearData (uint16_t addr, uint16_t size) {
clearcache();
return Base::clearData(addr, size);
}
};
// define some known EEPROM types
typedef CachedAt24cX<0x50,128,32> at24c32;
typedef CachedAt24cX<0x50,256,32> at24c64;
#endif
template <class DRIVER=InternalEprom>
class StorageWrapper : public DRIVER {
public:
StorageWrapper () {}
bool setup (uint16_t checksum=0) {
bool firststart = false;
uint32_t mem;
DRIVER::getData(0x0,(uint8_t*)&mem,4);
uint32_t magic = 0xCAFE0000 | checksum;
if(magic != mem) {
DHEXLN(mem);
DPRINT(F("Init Storage: "));
DHEXLN(magic);
// init eeprom
DRIVER::setData(0x0,(uint8_t*)&magic,4);
firststart = true;
}
return firststart;
}
bool setBits (uint16_t addr, uint8_t bits) {
DRIVER::setByte(addr,DRIVER::getByte(addr) | bits);
return true;
}
bool clearBits (uint16_t addr, uint8_t bits) {
DRIVER::setByte(addr,DRIVER::getByte(addr) & ~bits);
return true;
}
bool setData (uint16_t addr,uint8_t* buf,uint16_t size) {
return DRIVER::setData(addr,buf,size);
}
template <class T>
bool setData (uint16_t addr,const T& obj) {
return DRIVER::setData(addr,(uint8_t*)&obj,sizeof(T));
}
bool getData (uint16_t addr,uint8_t* buf,uint16_t size) {
return DRIVER::getData(addr,buf,size);
}
template <class T>
bool getData (uint16_t addr,T* obj) {
return DRIVER::getData(addr,(uint8_t*)obj,sizeof(T));
}
bool clearData (uint16_t addr, uint16_t size) {
return DRIVER::clearData(addr,size);
}
void reset () {
// clear magic
clearData(0x0,4);
}
void dump (uint16_t start, uint16_t num) {
for( uint16_t i=0; i<num; ++i, ++start ) {
DHEX(DRIVER::getByte(start));
DPRINT(F(" "));
}
DPRINT(F("\n"));
}
};
#ifndef STORAGEDRIVER
#define STORAGEDRIVER InternalEprom
#endif
extern void* __gb_store;
class Storage : public StorageWrapper<STORAGEDRIVER > {
public:
Storage () {
__gb_store = this;
}
};
static inline Storage& storage () {
return *((Storage*)__gb_store);
}
#define STORAGE_CFG_START 0x04
class StorageConfig {
uint8_t size;
public:
StorageConfig (uint8_t s) : size(s) {}
uint8_t checksum () const {
uint8_t sum = 0x5e;
for( uint8_t i=0; i<size-1; ++i ) {
sum ^= getByte(i);
}
return sum;
}
void validate () {
setByte(size-1,checksum());
}
bool valid () const {
return getByte(size-1) == checksum();
}
void clear () {
storage().clearData(STORAGE_CFG_START, size);
}
uint8_t getSize () const {
return size-1;
}
uint8_t getByte (uint8_t offset) const {
return storage().getByte(STORAGE_CFG_START+offset);
}
void setByte(uint8_t offset,uint8_t data) {
storage().setByte(STORAGE_CFG_START+offset,data);
}
};
}
#endif