Skip to content

Commit 3fdb296

Browse files
committed
Use buffered access to the EEPROM data on the flash
1 parent 1cfcfcd commit 3fdb296

File tree

10 files changed

+90
-9
lines changed

10 files changed

+90
-9
lines changed

libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
void setup() {
1414
// initialize the LED pin as an output.
1515
pinMode(13, OUTPUT);
16+
17+
EEPROM.begin();
1618

1719
/***
1820
Iterate through each byte of the EEPROM storage.
@@ -30,6 +32,8 @@ void setup() {
3032
EEPROM.write(i, 0);
3133
}
3234

35+
EEPROM.end();
36+
3337
// turn the LED on when we're done
3438
digitalWrite(13, HIGH);
3539
}

libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ void setup() {
1717
while (!Serial) {
1818
; // wait for serial port to connect. Needed for native USB port only
1919
}
20+
EEPROM.begin();
2021

2122
//Print length of data to run CRC on.
2223
Serial.print("EEPROM length: ");
@@ -25,6 +26,8 @@ void setup() {
2526
//Print the result of calling eeprom_crc()
2627
Serial.print("CRC32 of EEPROM data: 0x");
2728
Serial.println(eeprom_crc(), HEX);
29+
30+
EEPROM.end();
2831
Serial.print("\n\nDone!");
2932
}
3033

libraries/EEPROM/examples/eeprom_get/eeprom_get.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ void setup() {
2626
while (!Serial) {
2727
; // wait for serial port to connect. Needed for native USB port only
2828
}
29+
EEPROM.begin();
30+
2931
Serial.print("Read float from EEPROM: ");
3032

3133
//Get the float data from the EEPROM at position 'eeAddress'
@@ -43,6 +45,9 @@ void setup() {
4345
***/
4446

4547
secondTest(); //Run the next test.
48+
49+
EEPROM.end();
50+
Serial.print("\n\nDone!");
4651
}
4752

4853
struct MyObject {

libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
void setup() {
1717

18+
EEPROM.begin();
19+
1820
/***
1921
Iterate the EEPROM using a for loop.
2022
***/
@@ -51,7 +53,8 @@ void setup() {
5153
idx++;
5254
} while (idx < EEPROM.length());
5355

56+
EEPROM.end();
5457

5558
} //End of setup function.
5659

57-
void loop() {}
60+
void loop() {}

libraries/EEPROM/examples/eeprom_put/eeprom_put.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void setup() {
2828
while (!Serial) {
2929
; // wait for serial port to connect. Needed for native USB port only
3030
}
31+
EEPROM.begin();
3132

3233
float f = 123.456f; //Variable to store in EEPROM.
3334
int eeAddress = 0; //Location we want the data to be put.
@@ -51,6 +52,9 @@ void setup() {
5152

5253
EEPROM.put(eeAddress, customVar);
5354
Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");
55+
56+
EEPROM.end();
57+
Serial.print("\n\nDone!");
5458
}
5559

5660
void loop() {

libraries/EEPROM/examples/eeprom_read/eeprom_read.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void setup() {
1818
while (!Serial) {
1919
; // wait for serial port to connect. Needed for native USB port only
2020
}
21+
EEPROM.begin();
2122
}
2223

2324
void loop() {

libraries/EEPROM/examples/eeprom_update/eeprom_update.ino

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
int address = 0;
1818

1919
void setup() {
20-
/** EMpty setup **/
20+
Serial.begin(9600);
21+
while (!Serial) {
22+
; // wait for serial port to connect. Needed for native USB port only
23+
}
24+
EEPROM.begin();
2125
}
2226

2327
void loop() {
@@ -58,6 +62,11 @@ void loop() {
5862
address = address + 1;
5963
if (address == EEPROM.length()) {
6064
address = 0;
65+
if (EEPROM.commit()) {
66+
Serial.println("EEPROM successfully committed");
67+
} else {
68+
Serial.println("ERROR! EEPROM commit failed");
69+
}
6170
}
6271

6372
/***

libraries/EEPROM/examples/eeprom_write/eeprom_write.ino

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
int addr = 0;
1313

1414
void setup() {
15-
/** Empty setup. **/
15+
Serial.begin(9600);
16+
while (!Serial) {
17+
; // wait for serial port to connect. Needed for native USB port only
18+
}
19+
EEPROM.begin();
1620
}
1721

1822
void loop() {
@@ -46,6 +50,11 @@ void loop() {
4650
addr = addr + 1;
4751
if (addr == EEPROM.length()) {
4852
addr = 0;
53+
if (EEPROM.commit()) {
54+
Serial.println("EEPROM successfully committed");
55+
} else {
56+
Serial.println("ERROR! EEPROM commit failed");
57+
}
4958
}
5059

5160
/***

libraries/EEPROM/src/EEPROM.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
EEPROM.cpp - EEPROM library
3+
Original Copyright (c) 2006 David A. Mellis. All right reserved.
4+
New version by Christopher Andrews 2015.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include "EEPROM.h"
22+
23+
EEPROMClass EEPROM;
24+
25+
EERef& EERef::operator=(uint8_t in)
26+
{
27+
if (in != *this) EEPROM.dirty = true;
28+
return eeprom_buffered_write_byte(/*(uint8_t*)*/ index, in), *this;
29+
}

libraries/EEPROM/src/EEPROM.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct EERef {
3939
//Access/read members.
4040
uint8_t operator*() const
4141
{
42-
return eeprom_read_byte(/*(uint8_t*)*/ index);
42+
return eeprom_buffered_read_byte(/*(uint8_t*)*/ index);
4343
}
4444
operator uint8_t() const
4545
{
@@ -51,10 +51,8 @@ struct EERef {
5151
{
5252
return *this = *ref;
5353
}
54-
EERef &operator=(uint8_t in)
55-
{
56-
return eeprom_write_byte(/*(uint8_t*)*/ index, in), *this;
57-
}
54+
EERef &operator=(uint8_t in);
55+
5856
EERef &operator +=(uint8_t in)
5957
{
6058
return *this = **this + in;
@@ -190,6 +188,8 @@ struct EEPtr {
190188

191189
struct EEPROMClass {
192190

191+
EEPROMClass() : dirty(false) {}
192+
193193
//Basic user access methods.
194194
EERef operator[](const int idx)
195195
{
@@ -211,12 +211,23 @@ struct EEPROMClass {
211211
//STL and C++11 iteration capability.
212212
EEPtr begin()
213213
{
214+
eeprom_buffer_fill();
215+
dirty = false;
214216
return 0x00;
215217
}
216218
EEPtr end()
217219
{
220+
commit();
218221
return length(); //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
219222
}
223+
bool commit()
224+
{
225+
if (dirty) {
226+
eeprom_buffer_flush();
227+
dirty = false;
228+
}
229+
return true;
230+
}
220231
uint16_t length()
221232
{
222233
return E2END + 1;
@@ -242,7 +253,10 @@ struct EEPROMClass {
242253
}
243254
return t;
244255
}
256+
257+
bool dirty;
245258
};
246259

247-
static EEPROMClass EEPROM;
260+
extern EEPROMClass EEPROM;
261+
248262
#endif

0 commit comments

Comments
 (0)