Skip to content

Converted EEPROM library to use nvs instead of partition. #2678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libraries/EEPROM/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## EEPROM

EEPROM is deprecated. For new applications on ESP32, use Preferences. EEPROM is provided for backwards compatibility with existing Arduino applications.
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.
4 changes: 2 additions & 2 deletions libraries/EEPROM/examples/eeprom_class/eeprom_class.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
#include "EEPROM.h"

// Instantiate eeprom objects with parameter/argument names and size same as in the partition table
EEPROMClass NAMES("eeprom0", 0x1000);
EEPROMClass HEIGHT("eeprom1", 0x500);
EEPROMClass NAMES("eeprom0", 0x500);
EEPROMClass HEIGHT("eeprom1", 0x200);
EEPROMClass AGE("eeprom2", 0x100);

void setup() {
Expand Down
4 changes: 2 additions & 2 deletions libraries/EEPROM/examples/eeprom_extra/eeprom_extra.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("\nTesting EEPROM Library\n");
if (!EEPROM.begin(EEPROM.length())) {
if (!EEPROM.begin(1000)) {
Serial.println("Failed to initialise EEPROM");
Serial.println("Restarting...");
delay(1000);
Expand Down Expand Up @@ -87,4 +87,4 @@ void setup() {
void loop() {
// put your main code here, to run repeatedly:

}
}
144 changes: 89 additions & 55 deletions libraries/EEPROM/src/EEPROM.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/*
EEPROM.h -ported by Paolo Becchi to Esp32 from esp8266 EEPROM
-Modified by Elochukwu Ifediora <[email protected]>
-Converted to nvs [email protected]

Uses a one sector flash partition defined in partition table
OR
Multiple sector flash partitions defined by the name column in the partition table
Uses a nvs byte array to emulate EEPROM

Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
Expand All @@ -28,36 +27,34 @@

#include <esp_log.h>

EEPROMClass::EEPROMClass(uint32_t sector)
: _sector(sector)
, _data(0)
EEPROMClass::EEPROMClass(void)
: _data(0)
, _size(0)
, _dirty(false)
, _mypart(NULL)
, _handle(NULL)
, _name("eeprom")
, _user_defined_size(0)
{
}

EEPROMClass::EEPROMClass(const char* name, uint32_t user_defined_size)
: _sector(0)
, _data(0)
EEPROMClass::EEPROMClass(uint32_t sector)
// Only for compatiility, no sectors in nvs!
: _data(0)
, _size(0)
, _dirty(false)
, _mypart(NULL)
, _name(name)
, _user_defined_size(user_defined_size)
, _handle(NULL)
, _name("eeprom")
, _user_defined_size(0)
{
}

EEPROMClass::EEPROMClass(void)
: _sector(0)// (((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE))
, _data(0)
EEPROMClass::EEPROMClass(const char* name, uint32_t user_defined_size)
: _data(0)
, _size(0)
, _dirty(false)
, _mypart(NULL)
, _name("eeprom")
, _user_defined_size(0)
, _handle(NULL)
, _name(name)
, _user_defined_size(user_defined_size)
{
}

Expand All @@ -66,31 +63,76 @@ EEPROMClass::~EEPROMClass() {
}

bool EEPROMClass::begin(size_t size) {
if (size <= 0) {
return false;
if (!size) {
return false;
}

esp_err_t res = nvs_open(_name, NVS_READWRITE, &_handle);
if (res != ESP_OK) {
log_e("Unable to open NVS namespace: %d", res);
return false;
}
if (size > SPI_FLASH_SEC_SIZE) {
size = SPI_FLASH_SEC_SIZE;

size_t key_size = 0;
res = nvs_get_blob(_handle, _name, NULL, &key_size);
if(res != ESP_OK && res != ESP_ERR_NVS_NOT_FOUND) {
log_e("Unable to read NVS key: %d", res);
return false;
}
if (size < key_size) { // truncate
log_w("truncating EEPROM from %d to %d", key_size, size);
uint8_t* key_data = (uint8_t*) malloc(key_size);
if(!key_data) {
log_e("Not enough memory to truncate EEPROM!");
return false;
}
nvs_get_blob(_handle, _name, key_data, &key_size);
nvs_set_blob(_handle, _name, key_data, size);
nvs_commit(_handle);
free(key_data);
}
// _mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,ESP_PARTITION_SUBTYPE_ANY, EEPROM_FLASH_PARTITION_NAME);
_mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, _name);
if (_mypart == NULL) {
return false;
else if (size > key_size) { // expand or new
size_t expand_size = size - key_size;
uint8_t* expand_key = (uint8_t*) malloc(expand_size);
if(!expand_key) {
log_e("Not enough memory to expand EEPROM!");
return false;
}
// check for adequate free space
if(nvs_set_blob(_handle, "expand", expand_key, expand_size)) {
log_e("Not enough space to expand EEPROM from %d to %d", key_size, size);
free(expand_key);
return false;
}
free(expand_key);
nvs_erase_key(_handle, "expand");
uint8_t* key_data = (uint8_t*) malloc(size);
if(!key_data) {
log_e("Not enough memory to expand EEPROM!");
return false;
}
memset(key_data, 0, size);
if(key_size) {
log_i("Expanding EEPROM from %d to %d", key_size, size);
// hold data while key is deleted
nvs_get_blob(_handle, _name, key_data, &key_size);
nvs_erase_key(_handle, _name);
} else {
log_i("New EEPROM of %d bytes", size);
}
nvs_commit(_handle);
nvs_set_blob(_handle, _name, key_data, size);
free(key_data);
nvs_commit(_handle);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole block of code needs some comments to explain exactly what is going on :) I got lost with all those reads, writes and erases. What data is where... you need 2 blobs to hold the default 4K EEPROM, but reading through your code I got lost.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no way to expand a key, so in order to do so, it tests for free space, then holds the data while it deletes the key. If it is a new key, it just fills with zeros. I think changing the variable name to key_data will make it clearer.

}
size = (size + 3) & (~3);

if (_data) {
delete[] _data;
}

_data = new uint8_t[size];
_size = size;
bool ret = false;
if (esp_partition_read (_mypart, 0, (void *) _data, _size) == ESP_OK) {
ret = true;
}

return ret;
nvs_get_blob(_handle, _name, _data, &_size);
return true;
}

void EEPROMClass::end() {
Expand Down Expand Up @@ -134,29 +176,21 @@ void EEPROMClass::write(int address, uint8_t value) {

bool EEPROMClass::commit() {
bool ret = false;
if (!_size)
return false;
if (!_dirty)
return true;
if (!_data)
return false;


if (esp_partition_erase_range(_mypart, 0, SPI_FLASH_SEC_SIZE) != ESP_OK)
{
log_e( "partition erase err.");
if (!_size) {
return false;
}
else
{
if (esp_partition_write(_mypart, 0, (void *)_data, _size) == ESP_ERR_INVALID_SIZE)
{
log_e( "error in Write");
}
else
{
if (!_data) {
return false;
}
if (!_dirty) {
return true;
}

if (ESP_OK != nvs_set_blob(_handle, _name, _data, _size)) {
log_e( "error in write");
} else {
_dirty = false;
ret = true;
}
}

return ret;
Expand Down
23 changes: 5 additions & 18 deletions libraries/EEPROM/src/EEPROM.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/*
EEPROM.h -ported by Paolo Becchi to Esp32 from esp8266 EEPROM
-Modified by Elochukwu Ifediora <[email protected]>
-Converted to nvs [email protected]

Uses a one sector flash partition defined in partition table
OR
Multiple sector flash partitions defined by the name column in the partition table
Uses a nvs byte array to emulate EEPROM

Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
Expand All @@ -30,19 +29,8 @@
#define EEPROM_FLASH_PARTITION_NAME "eeprom"
#endif
#include <Arduino.h>
extern "C" {

#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <esp_partition.h>
}

//
// need to define AT LEAST a flash partition for EEPROM with above name
//
// eeprom , data , 0x99, start address, 0x1000
//
#include <nvs.h>

class EEPROMClass {
public:
EEPROMClass(uint32_t sector);
Expand Down Expand Up @@ -117,11 +105,10 @@ class EEPROMClass {
template <class T> T writeAll (int address, const T &);

protected:
uint32_t _sector;
nvs_handle _handle;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you maybe forward declare the nvs_handle so it is not required to include nvs in the header?

uint8_t* _data;
size_t _size;
bool _dirty;
const esp_partition_t * _mypart;
const char* _name;
uint32_t _user_defined_size;
};
Expand Down
3 changes: 1 addition & 2 deletions tools/partitions/default.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x140000,
app1, app, ota_1, 0x150000,0x140000,
eeprom, data, 0x99, 0x290000,0x1000,
spiffs, data, spiffs, 0x291000,0x16F000,
spiffs, data, spiffs, 0x290000,0x170000,
3 changes: 1 addition & 2 deletions tools/partitions/default_16MB.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x640000,
app1, app, ota_1, 0x650000,0x640000,
eeprom, data, 0x99, 0xc90000,0x1000,
spiffs, data, spiffs, 0xc91000,0x36F000,
spiffs, data, spiffs, 0xc90000,0x370000,
3 changes: 1 addition & 2 deletions tools/partitions/default_8MB.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x330000,
app1, app, ota_1, 0x340000,0x330000,
eeprom, data, 0x99, 0x670000,0x1000,
spiffs, data, spiffs, 0x671000,0x18F000,
spiffs, data, spiffs, 0x670000,0x190000,
3 changes: 1 addition & 2 deletions tools/partitions/default_ffat.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x140000,
app1, app, ota_1, 0x150000,0x140000,
eeprom, data, 0x99, 0x290000,0x1000,
ffat, data, fat, 0x291000,0x16F000,
ffat, data, fat, 0x291000,0x170000,
3 changes: 1 addition & 2 deletions tools/partitions/ffat.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x200000,
app1, app, ota_1, 0x210000,0x200000,
eeprom, data, 0x99, 0x410000,0x1000,
ffat, data, fat, 0x411000,0xBEE000,
ffat, data, fat, 0x410000,0xBEF000,
# to create/use ffat, see https://github.com/marcmerlin/esp32_fatfsimage
3 changes: 1 addition & 2 deletions tools/partitions/huge_app.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x300000,
eeprom, data, 0x99, 0x310000,0x1000,
spiffs, data, spiffs, 0x311000,0xEF000,
spiffs, data, spiffs, 0x310000,0xF0000,
3 changes: 1 addition & 2 deletions tools/partitions/large_spiffs_16MB.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x480000,
app1, app, ota_1, 0x490000,0x480000,
eeprom, data, 0x99, 0x910000,0x1000,
spiffs, data, spiffs, 0x911000,0x6EF000,
spiffs, data, spiffs, 0x910000,0x6F0000,
3 changes: 1 addition & 2 deletions tools/partitions/min_spiffs.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x1E0000,
app1, app, ota_1, 0x1F0000,0x1E0000,
eeprom, data, 0x99, 0x3D0000,0x1000,
spiffs, data, spiffs, 0x3D1000,0x2F000,
spiffs, data, spiffs, 0x3D0000,0x30000,
3 changes: 1 addition & 2 deletions tools/partitions/minimal.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x140000,
eeprom, data, 0x99, 0x150000, 0x1000,
spiffs, data, spiffs, 0x151000, 0xAF000,
spiffs, data, spiffs, 0x150000, 0xB0000,
3 changes: 1 addition & 2 deletions tools/partitions/no_ota.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x200000,
eeprom, data, 0x99, 0x210000,0x1000,
spiffs, data, spiffs, 0x211000,0x1EF000,
spiffs, data, spiffs, 0x210000,0x1F0000,
3 changes: 1 addition & 2 deletions tools/partitions/noota_3g.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x100000,
eeprom, data, 0x99, 0x110000,0x1000,
spiffs, data, spiffs, 0x111000,0x2EF000,
spiffs, data, spiffs, 0x110000,0x2F0000,
3 changes: 1 addition & 2 deletions tools/partitions/noota_3gffat.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x100000,
eeprom, data, 0x99, 0x110000,0x1000,
ffat, data, fat, 0x111000,0x2EF000,
ffat, data, fat, 0x110000,0x2F0000,
# to create/use ffat, see https://github.com/marcmerlin/esp32_fatfsimage
3 changes: 1 addition & 2 deletions tools/partitions/noota_ffat.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x200000,
eeprom, data, 0x99, 0x210000,0x1000,
ffat, data, fat, 0x211000,0x1EF000,
ffat, data, fat, 0x210000,0x1F0000,
# to create/use ffat, see https://github.com/marcmerlin/esp32_fatfsimage