Skip to content

Commit 39bae70

Browse files
author
Owen L - SFE
committed
add configurable EEPROM length
since the flashContent buffer is declared on the stack it may be dynamically sized. the user can take advantage of this to allow for any size EEPROM up to the allocated 1 page of flash. - added 'allowedSize' member to EEPROMClass - added setter for allowedSize 'setLength' - adjusted 'length' to return allowedSize - adjusted 'writeBlockToEEPROM' to use the allowedSize of the EEPROM structure when checking flash memory. - added example usage to Example2_AllFunctions
1 parent ab8ed1c commit 39bae70

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

libraries/EEPROM/examples/Example2_AllFunctions/Example2_AllFunctions.ino

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323

2424
void setup()
2525
{
26+
// You may choose to enable more or less EEPROM -
27+
// Default length is 1024 bytes (if setLength is not called)
28+
EEPROM.setLength(1080); // this would make the length 1080 bytes
29+
// EEPROM.setLength(AP3_EEPROM_MAX_LENGTH); // the maximum length is 8192 bytes (AP3_EEPROM_MAX_LENGTH)
30+
// Note: larger sizes will increase RAM usage and execution time
31+
2632
Serial.begin(115200);
2733
Serial.println("EEPROM Examples");
2834

@@ -44,7 +50,7 @@ void setup()
4450
Serial.println("8 bit tests");
4551
byte myValue1 = 200;
4652
byte myValue2 = 23;
47-
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
53+
randomLocation = random(0, EEPROM.length());
4854

4955
startTime = millis();
5056
EEPROM.write(randomLocation, myValue1); //(location, data)
@@ -72,7 +78,7 @@ void setup()
7278
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
7379
uint16_t myValue3 = 3411;
7480
int16_t myValue4 = -366;
75-
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
81+
randomLocation = random(0, EEPROM.length());
7682

7783
EEPROM.put(randomLocation, myValue3);
7884
EEPROM.put(randomLocation + 2, myValue4);
@@ -93,7 +99,7 @@ void setup()
9399
Serial.printf("Size of int: %d\n", sizeof(int));
94100
int myValue5 = -245000;
95101
unsigned int myValue6 = 400123;
96-
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
102+
randomLocation = random(0, EEPROM.length());
97103

98104
EEPROM.put(randomLocation, myValue5);
99105
EEPROM.put(randomLocation + 4, myValue6);
@@ -110,7 +116,7 @@ void setup()
110116
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
111117
int32_t myValue7 = -341002;
112118
uint32_t myValue8 = 241544;
113-
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
119+
randomLocation = random(0, EEPROM.length());
114120

115121
EEPROM.put(randomLocation, myValue7);
116122
EEPROM.put(randomLocation + 4, myValue8);
@@ -128,7 +134,7 @@ void setup()
128134
Serial.printf("Size of float: %d\n", sizeof(float));
129135
float myValue9 = -7.35;
130136
float myValue10 = 5.22;
131-
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
137+
randomLocation = random(0, EEPROM.length());
132138

133139
EEPROM.put(randomLocation, myValue9);
134140
EEPROM.put(randomLocation + 4, myValue10);
@@ -151,7 +157,7 @@ void setup()
151157
double myValue12 = 384.95734987;
152158
double myValue13 = 917.14159;
153159
double myValue14 = 254.8877;
154-
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
160+
randomLocation = random(0, EEPROM.length());
155161

156162
startTime = millis();
157163
EEPROM.put(randomLocation, myValue11);
@@ -183,7 +189,7 @@ void setup()
183189
//String write test
184190
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
185191
char myString[19] = "How are you today?";
186-
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
192+
randomLocation = random(0, EEPROM.length());
187193
EEPROM.put(randomLocation, myString);
188194

189195
char readMy[19];

libraries/EEPROM/src/EEPROM.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@
4040
#include "EEPROM.h"
4141
#include "Arduino.h"
4242

43+
EEPROMClass EEPROM; // the (magnificent) one and only EEPROM!!!!!
44+
4345
//Write a byte to a given "EEPROM" location
44-
void write(uint16_t eepromLocation, uint8_t dataToWrite)
46+
void write(uint16_t eepromLocation, uint8_t dataToWrite, uint16_t allowedSize)
4547
{
46-
writeBlockToEEPROM(eepromLocation, &dataToWrite, 1);
48+
writeBlockToEEPROM(eepromLocation, &dataToWrite, 1, allowedSize);
4749
}
4850

4951
//Read a byte from a given location in "EEPROM"
@@ -66,23 +68,23 @@ void EEPROMClass::erase()
6668
//3) Check if new data is different from flash.
6769
//4) Erase flash page (8k)
6870
//5) Write SRAM back into flash
69-
void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize)
71+
void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize, uint16_t allowedSize)
7072
{
7173
//Error check
72-
if (eepromLocation + blockSize >= AP3_FLASH_EEPROM_SIZE)
74+
if (eepromLocation + blockSize >= allowedSize)
7375
{
74-
blockSize = AP3_FLASH_EEPROM_SIZE - eepromLocation;
76+
blockSize = allowedSize - eepromLocation;
7577
}
7678

7779
//Read the contents of current "EEPROM" to SRAM
7880
//Flash is written in 32-bit words but user passes in array of bytes
7981
//Create an array of 32-bit words but reference it a byte at a time
80-
uint32_t flashContent[AP3_FLASH_EEPROM_SIZE / 4];
82+
uint32_t flashContent[allowedSize / 4];
8183

8284
//We can't read 32bits at a time because the way flash is oriented (little endian)
8385
//So we read a byte at a time
8486
uint8_t *eepromContents = (uint8_t *)flashContent;
85-
for (uint16_t x = 0; x < AP3_FLASH_EEPROM_SIZE; x++)
87+
for (uint16_t x = 0; x < allowedSize; x++)
8688
{
8789
eepromContents[x] = *(uint8_t *)(AP3_FLASH_EEPROM_START + x);
8890
}
@@ -96,7 +98,7 @@ void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uin
9698
//Run a check here to see if the new data is the same as what's in flash. If it's the same,
9799
//just return, don't erase flash.
98100
bool theSame = true;
99-
for (uint16_t x = 0; x < AP3_FLASH_EEPROM_SIZE; x++)
101+
for (uint16_t x = 0; x < allowedSize; x++)
100102
{
101103
if (eepromContents[x] != *(uint8_t *)(AP3_FLASH_EEPROM_START + x))
102104
{
@@ -116,5 +118,10 @@ void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uin
116118
am_hal_flash_program_main(AM_HAL_FLASH_PROGRAM_KEY,
117119
flashContent,
118120
(uint32_t *)AP3_FLASH_EEPROM_START,
119-
AP3_FLASH_EEPROM_SIZE / 4);
121+
allowedSize / 4);
120122
}
123+
124+
EERef &EERef::operator=(uint8_t in)
125+
{
126+
return write(index, in, EEPROM.length()), *this;
127+
}

libraries/EEPROM/src/EEPROM.h

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@
5959
//The SparkFun Apollo3 linker script has been modified to limit user code space to less than 0xFE000
6060

6161
#define AP3_FLASH_EEPROM_START 0xFE000
62+
#define AP3_FLASH_PAGE_SIZE 8192
63+
#define AP3_EEPROM_MAX_LENGTH AP3_FLASH_PAGE_SIZE
6264

63-
#if AP3_FLASH_EEPROM_START % 8192
65+
#if AP3_FLASH_EEPROM_START % AP3_FLASH_PAGE_SIZE
6466
Error : EEPROM start address must be divisble by 8192
6567
#endif
6668

@@ -69,11 +71,11 @@ Error : EEPROM start address must be divisble by 8192
6971
//to 2048 if needed
7072
//1024 = 19ms update time
7173
//2048 = 23ms update time
72-
const int AP3_FLASH_EEPROM_SIZE = 1024; //In bytes
74+
const uint16_t AP3_DEFAULT_FLASH_EEPROM_SIZE = 1024; //In bytes
7375

7476
uint8_t read(uint16_t eepromLocation);
75-
void write(uint16_t eepromLocation, uint8_t dataToWrite);
76-
void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize);
77+
void write(uint16_t eepromLocation, uint8_t dataToWrite, uint16_t allowedSize);
78+
void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize, uint16_t allowedSize);
7779

7880
struct EERef
7981
{
@@ -86,7 +88,7 @@ struct EERef
8688

8789
//Assignment/write members.
8890
EERef &operator=(const EERef &ref) { return *this = *ref; }
89-
EERef &operator=(uint8_t in) { return write(index, in), *this; }
91+
EERef &operator=(uint8_t in);
9092
EERef &operator+=(uint8_t in) { return *this = **this + in; }
9193
EERef &operator-=(uint8_t in) { return *this = **this - in; }
9294
EERef &operator*=(uint8_t in) { return *this = **this * in; }
@@ -163,7 +165,10 @@ struct EEPROMClass
163165
//Basic user access methods.
164166
EERef operator[](const int idx) { return idx; }
165167
uint8_t read(int idx) { return EERef(idx); }
166-
void write(int idx, uint8_t val) { (EERef(idx)) = val; }
168+
void write(int idx, uint8_t val)
169+
{
170+
(EERef(idx)) = val;
171+
}
167172
void update(int idx, uint8_t val) { EERef(idx).update(val); }
168173
void erase();
169174

@@ -174,7 +179,14 @@ struct EEPROMClass
174179
return 0x00;
175180
}
176181
EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
177-
uint16_t length() { return AP3_FLASH_EEPROM_SIZE; }
182+
uint16_t length()
183+
{
184+
return allowedSize;
185+
}
186+
void setLength(uint16_t length)
187+
{
188+
allowedSize = length;
189+
}
178190

179191
//Functionality to 'get' and 'put' objects to and from EEPROM.
180192
template <typename T>
@@ -192,11 +204,13 @@ struct EEPROMClass
192204
{
193205
const uint8_t *ptr = (const uint8_t *)&t;
194206

195-
writeBlockToEEPROM(idx, ptr, sizeof(T)); //Address, data, sizeOfData
207+
writeBlockToEEPROM(idx, ptr, sizeof(T), allowedSize); //Address, data, sizeOfData
196208

197209
return t;
198210
}
211+
212+
uint16_t allowedSize = AP3_DEFAULT_FLASH_EEPROM_SIZE;
199213
};
200214

201-
static EEPROMClass EEPROM __attribute__((unused));
215+
extern EEPROMClass EEPROM;
202216
#endif //_EEPROM_H

0 commit comments

Comments
 (0)