Skip to content

Commit 20f9f83

Browse files
committed
Converted EEPROM library to use nvs instead of partition. Removed eeprom partition from all partition table CSV files.
1 parent 6bf1048 commit 20f9f83

18 files changed

+91
-102
lines changed

libraries/EEPROM/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## EEPROM
2+
3+
EEPROM is deprecated. For new applications on ESP32, use Preferences. EEPROM is provided for backwards compatibility with existing Arduino applications.
4+
EEPROM is implemented using a single blob within NVS, so it is a container within a container. As such, it is not going to be a high performance storage method. Preferences will directly use nvs, and store each entry as a single object therein.

libraries/EEPROM/examples/eeprom_class/eeprom_class.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
#include "EEPROM.h"
2727

2828
// 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);
29+
EEPROMClass NAMES("eeprom0", 0x500);
30+
EEPROMClass HEIGHT("eeprom1", 0x200);
3131
EEPROMClass AGE("eeprom2", 0x100);
3232

3333
void setup() {

libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void setup() {
1313
// put your setup code here, to run once:
1414
Serial.begin(115200);
1515
Serial.println("\nTesting EEPROM Library\n");
16-
if (!EEPROM.begin(EEPROM.length())) {
16+
if (!EEPROM.begin(1000)) {
1717
Serial.println("Failed to initialise EEPROM");
1818
Serial.println("Restarting...");
1919
delay(1000);
@@ -87,4 +87,4 @@ void setup() {
8787
void loop() {
8888
// put your main code here, to run repeatedly:
8989

90-
}
90+
}

libraries/EEPROM/src/EEPROM.cpp

+65-54
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/*
22
EEPROM.h -ported by Paolo Becchi to Esp32 from esp8266 EEPROM
33
-Modified by Elochukwu Ifediora <[email protected]>
4+
-Converted to nvs [email protected]
45
5-
Uses a one sector flash partition defined in partition table
6-
OR
7-
Multiple sector flash partitions defined by the name column in the partition table
6+
Uses a nvs byte array to emulate EEPROM
87
98
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
109
This file is part of the esp8266 core for Arduino environment.
@@ -28,36 +27,34 @@
2827

2928
#include <esp_log.h>
3029

31-
EEPROMClass::EEPROMClass(uint32_t sector)
32-
: _sector(sector)
33-
, _data(0)
30+
EEPROMClass::EEPROMClass(void)
31+
: _data(0)
3432
, _size(0)
3533
, _dirty(false)
36-
, _mypart(NULL)
34+
, _handle(NULL)
3735
, _name("eeprom")
3836
, _user_defined_size(0)
3937
{
4038
}
4139

42-
EEPROMClass::EEPROMClass(const char* name, uint32_t user_defined_size)
43-
: _sector(0)
44-
, _data(0)
40+
EEPROMClass::EEPROMClass(uint32_t sector)
41+
// Only for compatiility, no sectors in nvs!
42+
: _data(0)
4543
, _size(0)
4644
, _dirty(false)
47-
, _mypart(NULL)
48-
, _name(name)
49-
, _user_defined_size(user_defined_size)
45+
, _handle(NULL)
46+
, _name("eeprom")
47+
, _user_defined_size(0)
5048
{
5149
}
5250

53-
EEPROMClass::EEPROMClass(void)
54-
: _sector(0)// (((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE))
55-
, _data(0)
51+
EEPROMClass::EEPROMClass(const char* name, uint32_t user_defined_size)
52+
: _data(0)
5653
, _size(0)
5754
, _dirty(false)
58-
, _mypart(NULL)
59-
, _name("eeprom")
60-
, _user_defined_size(0)
55+
, _handle(NULL)
56+
, _name(name)
57+
, _user_defined_size(user_defined_size)
6158
{
6259
}
6360

@@ -66,31 +63,59 @@ EEPROMClass::~EEPROMClass() {
6663
}
6764

6865
bool EEPROMClass::begin(size_t size) {
69-
if (size <= 0) {
70-
return false;
66+
if (size <= 0) return false;
67+
68+
esp_err_t res = nvs_open(_name, NVS_READWRITE, &_handle);
69+
if (res != ESP_OK) {
70+
log_e("Unable to open NVS namespace: %d", res);
71+
return false;
72+
}
73+
74+
size_t key_size = 0;
75+
res = nvs_get_blob(_handle, _name, NULL, &key_size);
76+
if(res != ESP_OK && res != ESP_ERR_NVS_NOT_FOUND) {
77+
log_e("Unable to read NVS key: %d", res);
78+
return false;
7179
}
72-
if (size > SPI_FLASH_SEC_SIZE) {
73-
size = SPI_FLASH_SEC_SIZE;
80+
if (size < key_size) { // truncate
81+
log_w("truncating EEPROM from %d to %d", key_size, size);
82+
uint8_t* trunc_key = (uint8_t*) malloc(size);
83+
nvs_get_blob(_handle, _name, trunc_key, &size);
84+
nvs_set_blob(_handle, _name, trunc_key, size);
85+
nvs_commit(_handle);
86+
free(trunc_key);
7487
}
75-
// _mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,ESP_PARTITION_SUBTYPE_ANY, EEPROM_FLASH_PARTITION_NAME);
76-
_mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, _name);
77-
if (_mypart == NULL) {
78-
return false;
88+
if (size > key_size) { // expand or new
89+
size_t expand_size = size - key_size;
90+
uint8_t* expand_key = (uint8_t*) malloc(expand_size);
91+
if(nvs_set_blob(_handle, "expand", expand_key, expand_size)) {
92+
log_e("Not enough space to expand EEPROM from %d to %d", key_size, size);
93+
free(expand_key);
94+
return false;
95+
}
96+
free(expand_key);
97+
nvs_erase_key(_handle, "expand");
98+
uint8_t* hold_data = (uint8_t*) malloc(size);
99+
memset(hold_data, 0, size);
100+
if(key_size) {
101+
log_i("Expanding EEPROM from %d to %d", key_size, size);
102+
nvs_get_blob(_handle, _name, hold_data, &key_size);
103+
nvs_erase_key(_handle, _name);
104+
}
105+
nvs_commit(_handle);
106+
nvs_set_blob(_handle, _name, hold_data, size);
107+
free(hold_data);
108+
nvs_commit(_handle);
79109
}
80-
size = (size + 3) & (~3);
81110

82111
if (_data) {
83112
delete[] _data;
84113
}
85114

86115
_data = new uint8_t[size];
87116
_size = size;
88-
bool ret = false;
89-
if (esp_partition_read (_mypart, 0, (void *) _data, _size) == ESP_OK) {
90-
ret = true;
91-
}
92-
93-
return ret;
117+
nvs_get_blob(_handle, _name, _data, &_size);
118+
return true;
94119
}
95120

96121
void EEPROMClass::end() {
@@ -134,29 +159,15 @@ void EEPROMClass::write(int address, uint8_t value) {
134159

135160
bool EEPROMClass::commit() {
136161
bool ret = false;
137-
if (!_size)
138-
return false;
139-
if (!_dirty)
140-
return true;
141-
if (!_data)
142-
return false;
143-
162+
if (!_size) return false;
163+
if (!_data) return false;
164+
if (!_dirty) return true;
144165

145-
if (esp_partition_erase_range(_mypart, 0, SPI_FLASH_SEC_SIZE) != ESP_OK)
146-
{
147-
log_e( "partition erase err.");
148-
}
149-
else
150-
{
151-
if (esp_partition_write(_mypart, 0, (void *)_data, _size) == ESP_ERR_INVALID_SIZE)
152-
{
153-
log_e( "error in Write");
154-
}
155-
else
156-
{
166+
if (ESP_OK != nvs_set_blob(_handle, _name, _data, _size)) {
167+
log_e( "error in write");
168+
} else {
157169
_dirty = false;
158170
ret = true;
159-
}
160171
}
161172

162173
return ret;

libraries/EEPROM/src/EEPROM.h

+5-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/*
22
EEPROM.h -ported by Paolo Becchi to Esp32 from esp8266 EEPROM
33
-Modified by Elochukwu Ifediora <[email protected]>
4+
-Converted to nvs [email protected]
45
5-
Uses a one sector flash partition defined in partition table
6-
OR
7-
Multiple sector flash partitions defined by the name column in the partition table
6+
Uses a nvs byte array to emulate EEPROM
87
98
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
109
This file is part of the esp8266 core for Arduino environment.
@@ -30,19 +29,8 @@
3029
#define EEPROM_FLASH_PARTITION_NAME "eeprom"
3130
#endif
3231
#include <Arduino.h>
33-
extern "C" {
34-
35-
#include <stddef.h>
36-
#include <stdint.h>
37-
#include <string.h>
38-
#include <esp_partition.h>
39-
}
40-
41-
//
42-
// need to define AT LEAST a flash partition for EEPROM with above name
43-
//
44-
// eeprom , data , 0x99, start address, 0x1000
45-
//
32+
#include <nvs.h>
33+
4634
class EEPROMClass {
4735
public:
4836
EEPROMClass(uint32_t sector);
@@ -117,11 +105,10 @@ class EEPROMClass {
117105
template <class T> T writeAll (int address, const T &);
118106

119107
protected:
120-
uint32_t _sector;
108+
nvs_handle _handle;
121109
uint8_t* _data;
122110
size_t _size;
123111
bool _dirty;
124-
const esp_partition_t * _mypart;
125112
const char* _name;
126113
uint32_t _user_defined_size;
127114
};

tools/partitions/default.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x140000,
55
app1, app, ota_1, 0x150000,0x140000,
6-
eeprom, data, 0x99, 0x290000,0x1000,
7-
spiffs, data, spiffs, 0x291000,0x16F000,
6+
spiffs, data, spiffs, 0x290000,0x170000,

tools/partitions/default_16MB.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x640000,
55
app1, app, ota_1, 0x650000,0x640000,
6-
eeprom, data, 0x99, 0xc90000,0x1000,
7-
spiffs, data, spiffs, 0xc91000,0x36F000,
6+
spiffs, data, spiffs, 0xc90000,0x370000,

tools/partitions/default_8MB.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x330000,
55
app1, app, ota_1, 0x340000,0x330000,
6-
eeprom, data, 0x99, 0x670000,0x1000,
7-
spiffs, data, spiffs, 0x671000,0x18F000,
6+
spiffs, data, spiffs, 0x670000,0x190000,

tools/partitions/default_ffat.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x140000,
55
app1, app, ota_1, 0x150000,0x140000,
6-
eeprom, data, 0x99, 0x290000,0x1000,
7-
ffat, data, fat, 0x291000,0x16F000,
6+
ffat, data, fat, 0x291000,0x170000,

tools/partitions/ffat.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x200000,
55
app1, app, ota_1, 0x210000,0x200000,
6-
eeprom, data, 0x99, 0x410000,0x1000,
7-
ffat, data, fat, 0x411000,0xBEE000,
6+
ffat, data, fat, 0x410000,0xBEF000,
87
# to create/use ffat, see https://github.com/marcmerlin/esp32_fatfsimage

tools/partitions/huge_app.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x300000,
5-
eeprom, data, 0x99, 0x310000,0x1000,
6-
spiffs, data, spiffs, 0x311000,0xEF000,
5+
spiffs, data, spiffs, 0x310000,0xF0000,

tools/partitions/large_spiffs_16MB.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x480000,
55
app1, app, ota_1, 0x490000,0x480000,
6-
eeprom, data, 0x99, 0x910000,0x1000,
7-
spiffs, data, spiffs, 0x911000,0x6EF000,
6+
spiffs, data, spiffs, 0x910000,0x6F0000,

tools/partitions/min_spiffs.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x1E0000,
55
app1, app, ota_1, 0x1F0000,0x1E0000,
6-
eeprom, data, 0x99, 0x3D0000,0x1000,
7-
spiffs, data, spiffs, 0x3D1000,0x2F000,
6+
spiffs, data, spiffs, 0x3D0000,0x30000,

tools/partitions/minimal.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x140000,
5-
eeprom, data, 0x99, 0x150000, 0x1000,
6-
spiffs, data, spiffs, 0x151000, 0xAF000,
5+
spiffs, data, spiffs, 0x150000, 0xB0000,

tools/partitions/no_ota.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x200000,
5-
eeprom, data, 0x99, 0x210000,0x1000,
6-
spiffs, data, spiffs, 0x211000,0x1EF000,
5+
spiffs, data, spiffs, 0x210000,0x1F0000,

tools/partitions/noota_3g.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x100000,
5-
eeprom, data, 0x99, 0x110000,0x1000,
6-
spiffs, data, spiffs, 0x111000,0x2EF000,
5+
spiffs, data, spiffs, 0x110000,0x2F0000,

tools/partitions/noota_3gffat.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x100000,
5-
eeprom, data, 0x99, 0x110000,0x1000,
6-
ffat, data, fat, 0x111000,0x2EF000,
5+
ffat, data, fat, 0x110000,0x2F0000,
76
# to create/use ffat, see https://github.com/marcmerlin/esp32_fatfsimage

tools/partitions/noota_ffat.csv

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
44
app0, app, ota_0, 0x10000, 0x200000,
5-
eeprom, data, 0x99, 0x210000,0x1000,
6-
ffat, data, fat, 0x211000,0x1EF000,
5+
ffat, data, fat, 0x210000,0x1F0000,
76
# to create/use ffat, see https://github.com/marcmerlin/esp32_fatfsimage

0 commit comments

Comments
 (0)