forked from thijse/Arduino-EEPROMEx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEEPROMex.h
362 lines (316 loc) · 10.2 KB
/
EEPROMex.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
/*
EEPROMEx.h - Extended EEPROM library
Copyright (c) 2012 Thijs Elenbaas. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef EEPROMEX_h
#define EEPROMEX_h
#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <inttypes.h>
#include <avr/eeprom.h>
#define EEPROMSize1k 1024
#define EEPROMSize2k 2048
#define EEPROMSize4k 4096
#define EEPROMSize8k 8192
#define EEPROMSize16k 16384
#define EEPROMSize32k 32768
#define EEPROMSize64k 65536
#define EEPROMSizeATmega168 512
#define EEPROMSizeATmega328 1024
#define EEPROMSizeATmega1280 4096
#define EEPROMSizeATmega32u4 1024
#define EEPROMSizeAT90USB1286 4096
#define EEPROMSizeMK20DX128 2048
#define EEPROMSizeMK20DX256 2048
#define EEPROMSizeATSAMD21G18 16384
#define EEPROMSizeUno EEPROMSizeATmega328
#define EEPROMSizeUnoSMD EEPROMSizeATmega328
#define EEPROMSizeLilypad EEPROMSizeATmega328
#define EEPROMSizeDuemilanove EEPROMSizeATmega328
#define EEPROMSizePro EEPROMSizeATmega328
#define EEPROMSizeFio EEPROMSizeATmega328
#define EEPROMSizeMega EEPROMSizeATmega1280
#define EEPROMSizeDiecimila EEPROMSizeATmega168
#define EEPROMSizeNano EEPROMSizeATmega168
#define EEPROMSizeTeensy2 EEPROMSizeATmega32u4
#define EEPROMSizeLeonardo EEPROMSizeATmega32u4
#define EEPROMSizeMicro EEPROMSizeATmega32u4
#define EEPROMSizeEsplora EEPROMSizeATmega32u4
#define EEPROMSizeYun EEPROMSizeATmega32u4
#define EEPROMSizeTre EEPROMSizeATmega32u4
#define EEPROMSizeZero EEPROMSizeATSAMD21G18
#define EEPROMSizeTeensy2pp EEPROMSizeAT90USB1286
#define EEPROMSizeTeensy3 EEPROMSizeMK20DX128
#define EEPROMSizeTeensy31 EEPROMSizeMK20DX256
/*
* FROM:
* - https://github.com/AMPWorks/ArduinoLibs/blob/master/EEPromUtils/EEPromUtils.cpp
* FROM:
* - http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
* The width of the CRC calculation and result.
* Modify the typedef for a 16 or 32-bit CRC standard.
*/
typedef uint8_t crc_t;
#define POLYNOMIAL 0xD8 /* 11011 followed by 0's */
#define WIDTH (8 * sizeof(crc_t))
#define TOPBIT (1 << (WIDTH - 1))
// Start byte for safe mode.
#define EEPROM_START_BYTE 0xAF
class EEPROMClassEx
{
public:
EEPROMClassEx();
bool isReady();
int writtenBytes();
void setMemPool(int base, int memSize);
void setMaxAllowedWrites(int allowedWrites);
int getAddress(int noOfBytes);
uint8_t read(int);
bool readBit(int, byte);
uint8_t readByte(int);
uint16_t readInt16(int);
uint32_t readInt32(int);
float readFloat(int);
double readDouble(int);
bool write(int, uint8_t);
bool writeBit(int , uint8_t, bool);
bool writeByte(int, uint8_t);
bool writeInt16(int, uint16_t);
bool writeInt32(int, uint32_t);
bool writeFloat(int, float);
bool writeDouble(int, double);
bool update(int, uint8_t);
bool updateBit(int , uint8_t, bool);
bool updateByte(int, uint8_t);
bool updateInt16(int, uint16_t);
bool updateInt32(int, uint32_t);
bool updateFloat(int, float);
bool updateDouble(int, double);
// Use template for other data formats
/**
* Template function to read multiple items of any type of variable, such as structs
* Retained for compatability with previous versions
*/
template <class T> int readBlock(int address, const T value[], int items)
{
return readBlock(address, value, items, false);
}
/**
* Template function to read multiple items of any type of variable, such as structs
*/
template <class T> int readBlock(int address, const T value[], int items, bool safeWrite)
{
unsigned int i;
for (i = 0; i < (unsigned int)items; i++)
if (safeWrite)
{
int bytes = readBlock<T>(address+(i*sizeof(T) + 5),value[i], safeWrite); // account for extra bytes like crc, length and start
if (bytes == 0)
delete value[i];
value[i] = NULL;
}
else
readBlock<T>(address+(i*sizeof(T)),value[i], safeWrite);
return i;
}
/**
* Template function to read any type of variable, such as structs
* Retained for compatability with previous versions
*/
template <class T> int readBlock(int address, const T& value)
{
return readBlock(address, value, false);
}
/**
* Template function to read any type of variable, such as structs
*/
template <class T> int readBlock(int address, const T& value, bool safeWrite)
{
if (safeWrite)
{
if (read(address) != EEPROM_START_BYTE)
{
return -1;
}
if (readInt16(address + 1) != sizeof(value))
{
return -2;
}
eeprom_read_block((void*)&value, (const void*)(address + 3), sizeof(value));
if (read(address + 3 + sizeof(value)) != CRC((void*)&value, sizeof(value)))
{
return -3;
}
return sizeof(value);
}
else
{
eeprom_read_block((void*)&value, (const void*)address, sizeof(value));
return sizeof(value);
}
}
/**
* Template function to write multiple items of any type of variable, such as structs
* Retained for compatability with previous versions
*/
template <class T> int writeBlock(int address, const T value[], int items)
{
return writeBlock(address, value, items, false);
}
/**
* Template function to write multiple items of any type of variable, such as structs
*/
template <class T> int writeBlock(int address, const T value[], int items, bool safeWrite)
{
if (!isWriteOk(address+items*sizeof(T))) return 0;
unsigned int i;
for (i = 0; i < (unsigned int)items; i++)
writeBlock<T>(address+(i*(sizeof(T) + 5)),value[i], safeWrite);
return i;
}
/**
* Template function to write any type of variable, such as structs.
* Retained for compatability with previous versions
*/
template <class T> int writeBlock(int address, const T& value)
{
return writeBlock(address, value, false);
}
/**
* Template function to write any type of variable, such as structs.
* When safeWrite is on the written data becomes:
* 1 Byte: Start
* 1 Byte: Datalen
* N Byte: data
* 1 Byte: CRC
*/
template <class T> int writeBlock(int address, const T& value, bool safeWrite)
{
if (safeWrite)
{
if (!isWriteOk(address+sizeof(value) + 5)) return 0; // Add three bytes for Start, dataLenght en CRC
write(address, EEPROM_START_BYTE);
writeInt16(address + 1, sizeof(value));
eeprom_write_block((void*)&value, (void*)(address + 3), sizeof(value));
write(address + sizeof(value) + 3, CRC((void*)&value, sizeof(value)));
return sizeof(value);
}
else
{
if (!isWriteOk(address+sizeof(value))) return 0;
eeprom_write_block((void*)&value, (void*)address, sizeof(value));
return sizeof(value);
}
}
/**
* Template function to update multiple items of any type of variable, such as structs
* The EEPROM will only be overwritten if different. This will reduce wear.
* Retained for compatability with previous versions
*/
template <class T> int updateBlock(int address, const T value[], int items)
{
return updateBlock(address, value, items, false);
}
/**
* Template function to update multiple items of any type of variable, such as structs
* The EEPROM will only be overwritten if different. This will reduce wear.
*/
template <class T> int updateBlock(int address, const T value[], int items, bool safeWrite)
{
int writeCount=0;
if (!isWriteOk(address+items*sizeof(T))) return 0;
unsigned int i;
for (i = 0; i < (unsigned int)items; i++)
if(safeWrite)
{
writeCount+= updateBlock<T>(address+(i * (sizeof(T) + 5)), value[i], safeWrite);
}
else
{
writeCount+= updateBlock<T>(address+(i*sizeof(T)),value[i], safeWrite);
}
return writeCount;
}
/**
* Template function to update any type of variable, such as structs
* The EEPROM will only be overwritten if different. This will reduce wear.
* Retained for compatability with previous versions
*/
template <class T> int updateBlock(int address, const T& value)
{
return updateBlock(address, value, false);
}
/**
* Template function to update any type of variable, such as structs
* The EEPROM will only be overwritten if different. This will reduce wear.
*/
template <class T> int updateBlock(int address, const T& value, bool safeWrite)
{
int writeCount=0;
if (safeWrite)
{
if (!isWriteOk(address + sizeof(value) + 5)) return 0; // Add three bytes for Start, dataLenght en CRC
if (read(address) != EEPROM_START_BYTE)
return 0; // Do not write if we are not on a value boundary
if (readInt16(address) != sizeof(value))
writeInt16(address + 1, sizeof(value));
const byte* bytePointer = (const byte*)(const void*)&value;
address += 3;
for (unsigned int i = 0; i < (unsigned int)sizeof(value); i++) {
if (read(address)!=*bytePointer) {
write(address, *bytePointer);
writeCount++;
}
address++;
bytePointer++;
}
crc_t crc = CRC((void*)&value, sizeof(value));
if (read(address) != crc)
{
write(address, crc);
}
Serial.println("end test");
}
else
{
if (!isWriteOk(address+sizeof(value))) return 0;
const byte* bytePointer = (const byte*)(const void*)&value;
for (unsigned int i = 0; i < (unsigned int)sizeof(value); i++) {
if (read(address)!=*bytePointer) {
write(address, *bytePointer);
writeCount++;
}
address++;
bytePointer++;
}
}
return writeCount;
}
private:
//Private variables
static int _base;
static int _memSize;
static int _nextAvailableaddress;
static int _writeCounts;
int _allowedWrites;
bool checkWrite(int base,int noOfBytes);
bool isWriteOk(int address);
bool isReadOk(int address);
crc_t CRC(const void *data, int noOfBytes);
};
extern EEPROMClassEx EEPROM;
#endif