Skip to content

Commit 80e864f

Browse files
committed
rebase eeprom lib and add partition
1 parent 21b8865 commit 80e864f

File tree

7 files changed

+146
-208
lines changed

7 files changed

+146
-208
lines changed
Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,64 @@
11
/*
2-
ESP32 eeprom_class example with EEPROM library
2+
ESP32 eeprom_class example with EEPROM library and custom partiton
3+
34
This simple example demonstrates using EEPROM library to store different data in
4-
ESP32 Flash memory in a multiple user-defined EEPROM class objects.
5+
ESP32 Flash memory in a multiple user-defined EEPROM partition (0x1000 or 4KB max size or less).
6+
Note: You must maintain a minimum sector address/offset of 4KB between partitions
7+
Usage: EEPROMClass ANY_OBJECT_NAME("partition_name", size);
8+
9+
Partition example created in the same sketch folder as this example
10+
(See ./eeprom_class/partitions.csv)
511
12+
#Name, Type, SubType, Offset, Size, Flags
13+
nvs, data, nvs, 0x9000, 0x5000,
14+
otadata, data, ota, 0xe000, 0x2000,
15+
app0, app, ota_0, 0x10000, 0x140000,
16+
app1, app, ota_1, 0x150000, 0x140000,
17+
eeprom0, data, 0x99, 0x290000, 0x1000,
18+
eeprom1, data, 0x9a, 0x291000, 0x500,
19+
eeprom2, data, 0x9b, 0x292000, 0x100,
20+
spiffs, data, spiffs, 0x293000, 0x16d000,
21+
622
Created for arduino-esp32 on 25 Dec, 2017
723
by Elochukwu Ifediora (fedy0)
8-
converted to nvs by lbernstone - 06/22/2019
924
*/
1025

1126
#include "EEPROM.h"
1227

13-
// Instantiate eeprom objects with parameter/argument names and sizes
14-
EEPROMClass NAMES("eeprom0");
15-
EEPROMClass HEIGHT("eeprom1");
16-
EEPROMClass AGE("eeprom2");
28+
// Instantiate eeprom objects with parameter/argument names and size same as in the partition table
29+
EEPROMClass NAMES("eeprom0", 0x1000);
30+
EEPROMClass HEIGHT("eeprom1", 0x500);
31+
EEPROMClass AGE("eeprom2", 0x100);
1732

1833
void setup() {
34+
// put your setup code here, to run once:
1935
Serial.begin(115200);
20-
delay(1000);
2136
Serial.println("Testing EEPROMClass\n");
22-
if (!NAMES.begin(0x500)) {
37+
if (!NAMES.begin(NAMES.length())) {
2338
Serial.println("Failed to initialise NAMES");
2439
Serial.println("Restarting...");
2540
delay(1000);
2641
ESP.restart();
2742
}
28-
if (!HEIGHT.begin(0x200)) {
43+
if (!HEIGHT.begin(HEIGHT.length())) {
2944
Serial.println("Failed to initialise HEIGHT");
3045
Serial.println("Restarting...");
3146
delay(1000);
3247
ESP.restart();
3348
}
34-
if (!AGE.begin(0x100)) {
49+
if (!AGE.begin(AGE.length())) {
3550
Serial.println("Failed to initialise AGE");
3651
Serial.println("Restarting...");
3752
delay(1000);
3853
ESP.restart();
3954
}
4055

41-
const char* name = "Teo Swee Ann";
42-
char rname[32];
56+
char* name = "Teo Swee Ann";
4357
double height = 5.8;
4458
uint32_t age = 47;
4559

46-
// Write: Variables ---> EEPROM stores
47-
NAMES.writeString(0, name);
60+
// Write: Variables ---> EEPROM partitions
61+
NAMES.put(0, name);
4862
HEIGHT.put(0, height);
4963
AGE.put(0, age);
5064
Serial.print("name: "); Serial.println(name);
@@ -53,25 +67,26 @@ void setup() {
5367
Serial.println("------------------------------------\n");
5468

5569
// Clear variables
56-
rname[0] = '\0';
70+
name = NULL;
5771
height = 0;
5872
age = 0;
59-
Serial.print("name: "); Serial.println(rname);
73+
Serial.print("name: "); Serial.println(name);
6074
Serial.print("height: "); Serial.println(height);
6175
Serial.print("age: "); Serial.println(age);
6276
Serial.println("------------------------------------\n");
6377

64-
// Read: Variables <--- EEPROM stores
65-
NAMES.get(0, rname);
78+
// Read: Variables <--- EEPROM partitions
79+
NAMES.get(0, name);
6680
HEIGHT.get(0, height);
6781
AGE.get(0, age);
68-
Serial.print("name: "); Serial.println(rname);
82+
Serial.print("name: "); Serial.println(name);
6983
Serial.print("height: "); Serial.println(height);
7084
Serial.print("age: "); Serial.println(age);
7185

7286
Serial.println("Done!");
7387
}
7488

7589
void loop() {
76-
delay(0xFFFFFFFF);
90+
// put your main code here, to run repeatedly:
91+
7792
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#Name, Type, SubType, Offset, Size, Flags
2+
nvs, data, nvs, 0x9000, 0x5000,
3+
otadata, data, ota, 0xe000, 0x2000,
4+
app0, app, ota_0, 0x10000, 0x140000,
5+
app1, app, ota_1, 0x150000, 0x140000,
6+
eeprom0, data, 0x99, 0x290000, 0x1000,
7+
eeprom1, data, 0x9a, 0x291000, 0x500,
8+
eeprom2, data, 0x9b, 0x292000, 0x100,
9+
spiffs, data, spiffs, 0x293000, 0x16d000,

libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/*
22
ESP32 eeprom_extra example with EEPROM library
33
4-
This simple example demonstrates using other EEPROM library resources
4+
This simple example demonstrates using other EEPROM library resources.
5+
6+
You need to define AT LEAST a flash partition for EEPROM with the name below
7+
eeprom, data, 0x99, "start address", 0x1000
8+
9+
Note: For this Arduino sketch, "eeprom" is already defined in ./eeprom_extra/partitions.csv
510
611
Created for arduino-esp32 on 25 Dec, 2017
712
by Elochukwu Ifediora (fedy0)
@@ -13,7 +18,7 @@ void setup() {
1318
// put your setup code here, to run once:
1419
Serial.begin(115200);
1520
Serial.println("\nTesting EEPROM Library\n");
16-
if (!EEPROM.begin(1000)) {
21+
if (!EEPROM.begin(0x1000)) { // Default Size = 4KB
1722
Serial.println("Failed to initialise EEPROM");
1823
Serial.println("Restarting...");
1924
delay(1000);
@@ -22,7 +27,7 @@ void setup() {
2227

2328
int address = 0;
2429

25-
EEPROM.writeByte(address, -128); // -2^7
30+
EEPROM.writeByte(address, 128); // 2^7
2631
address += sizeof(byte);
2732

2833
EEPROM.writeChar(address, 'A'); // Same as writyByte and readByte
@@ -49,11 +54,11 @@ void setup() {
4954
EEPROM.writeULong(address, 4294967295); // Same as writeUInt and readUInt
5055
address += sizeof(unsigned long);
5156

52-
int64_t value = -1223372036854775808LL; // -2^63
57+
int64_t value = -9223372036854775808; // -2^63
5358
EEPROM.writeLong64(address, value);
5459
address += sizeof(int64_t);
5560

56-
uint64_t Value = 18446744073709551615ULL; // 2^64 - 1
61+
uint64_t Value = 18446744073709551615; // 2^64 - 1
5762
EEPROM.writeULong64(address, Value);
5863
address += sizeof(uint64_t);
5964

@@ -75,6 +80,7 @@ void setup() {
7580
address += 21;
7681

7782
// See also the general purpose writeBytes() and readBytes() for BLOB in EEPROM library
83+
7884
EEPROM.commit();
7985
address = 0;
8086

@@ -127,10 +133,9 @@ void setup() {
127133
address += sizeof(bool);
128134

129135
Serial.println(EEPROM.readString(address));
130-
address += sentence.length() + 1;
136+
address += sentence.length() + 1; // To skip NULL termination in the string
131137

132138
Serial.println(EEPROM.readString(address));
133-
address += 21;
134139
}
135140

136141
void loop() {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#Name, Type, SubType, Offset, Size, Flags
2+
nvs, data, nvs, 0x9000, 0x5000,
3+
otadata, data, ota, 0xe000, 0x2000,
4+
app0, app, ota_0, 0x10000, 0x140000,
5+
app1, app, ota_1, 0x150000, 0x140000,
6+
eeprom, data, 0x99, 0x290000, 0x1000,
7+
spiffs, data, spiffs, 0x291000, 0x16F000,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#Name, Type, SubType, Offset, Size, Flags
2+
nvs, data, nvs, 0x9000, 0x5000,
3+
otadata, data, ota, 0xe000, 0x2000,
4+
app0, app, ota_0, 0x10000, 0x140000,
5+
app1, app, ota_1, 0x150000, 0x140000,
6+
eeprom, data, 0x99, 0x290000, 0x1000,
7+
spiffs, data, spiffs, 0x291000, 0x16F000,

0 commit comments

Comments
 (0)