From 1d2b78db0e64d98b1607adc81ac7d7cdbc312b23 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Wed, 29 May 2019 09:01:13 -0600 Subject: [PATCH 1/4] Added convert method to transfer data from partition to nvs --- libraries/EEPROM/library.properties | 4 +- libraries/EEPROM/src/EEPROM.cpp | 63 +++++++++++++++++++++++++++++ libraries/EEPROM/src/EEPROM.h | 1 + 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/libraries/EEPROM/library.properties b/libraries/EEPROM/library.properties index bd32cb17706..634cc84d929 100644 --- a/libraries/EEPROM/library.properties +++ b/libraries/EEPROM/library.properties @@ -1,9 +1,9 @@ name=EEPROM -version=1.0 +version=1.0.3 author=Ivan Grokhotkov maintainer=Paolo Becchi sentence=Enables reading and writing data to the permanent FLASH storage, up to 4kb. paragraph= category=Data Storage url=http://arduino.cc/en/Reference/EEPROM -architectures=esp32 \ No newline at end of file +architectures=esp32 diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 562829761c6..58fd5b5b98b 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -25,6 +25,7 @@ #include "EEPROM.h" #include +#include #include EEPROMClass::EEPROMClass(void) @@ -214,6 +215,68 @@ uint16_t EEPROMClass::length () return _user_defined_size; } +/* + Convert EEPROM partition into nvs blob + Call convert before you call begin +*/ +uint16_t EEPROMClass::convert (bool clear, const char* EEPROMname, const char* nvsname) +{ + uint16_t result = 0; + const esp_partition_t* mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, EEPROMname); + if (mypart == NULL) { + log_i("EEPROM partition not found for conversion"); + return result; + } + + size_t size; + size = mypart->size; + uint8_t* data; + data = (uint8_t*) malloc(size); + if (!data) { + log_e("Not enough memory to convert EEPROM!"); + goto exit; + } + + if (esp_partition_read (mypart, 0, (void *) data, size) != ESP_OK) { + log_i("Unable to read EEPROM partition"); + goto exit; + } + + bool empty; + empty = true; + for (int x=0; x T &get(int address, T &t) { From 8560231ba7d5506bc4a1ea763248905427e086e4 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Wed, 29 May 2019 11:14:37 -0600 Subject: [PATCH 2/4] Could have sworn I changed the return when I made the label --- libraries/EEPROM/library.properties | 2 +- libraries/EEPROM/src/EEPROM.cpp | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/libraries/EEPROM/library.properties b/libraries/EEPROM/library.properties index 634cc84d929..e65520588de 100644 --- a/libraries/EEPROM/library.properties +++ b/libraries/EEPROM/library.properties @@ -2,7 +2,7 @@ name=EEPROM version=1.0.3 author=Ivan Grokhotkov maintainer=Paolo Becchi -sentence=Enables reading and writing data to the permanent FLASH storage, up to 4kb. +sentence=Enables reading and writing data a sequential, addressable FLASH storage paragraph= category=Data Storage url=http://arduino.cc/en/Reference/EEPROM diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 58fd5b5b98b..4ffea625ea6 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -228,10 +228,8 @@ uint16_t EEPROMClass::convert (bool clear, const char* EEPROMname, const char* n return result; } - size_t size; - size = mypart->size; - uint8_t* data; - data = (uint8_t*) malloc(size); + size_t size = mypart->size; + uint8_t* data = (uint8_t*) malloc(size); if (!data) { log_e("Not enough memory to convert EEPROM!"); goto exit; @@ -274,7 +272,7 @@ uint16_t EEPROMClass::convert (bool clear, const char* EEPROMname, const char* n } exit: free(data); - return size; + return result; } /* From eb3d82e4484cdd21726b9f2c4bcbb7d17f3f9eb2 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Fri, 31 May 2019 10:16:21 -0600 Subject: [PATCH 3/4] Empty state should be 0xFF. Fixed some logging levels --- libraries/EEPROM/src/EEPROM.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 4ffea625ea6..2e68154e6bb 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -112,7 +112,7 @@ bool EEPROMClass::begin(size_t size) { log_e("Not enough memory to expand EEPROM!"); return false; } - memset(key_data, 0, size); + memset(key_data, 0xFF, size); if(key_size) { log_i("Expanding EEPROM from %d to %d", key_size, size); // hold data while key is deleted @@ -236,7 +236,7 @@ uint16_t EEPROMClass::convert (bool clear, const char* EEPROMname, const char* n } if (esp_partition_read (mypart, 0, (void *) data, size) != ESP_OK) { - log_i("Unable to read EEPROM partition"); + log_e("Unable to read EEPROM partition"); goto exit; } @@ -255,19 +255,19 @@ uint16_t EEPROMClass::convert (bool clear, const char* EEPROMname, const char* n nvs_handle handle; if (nvs_open(nvsname, NVS_READWRITE, &handle) != ESP_OK) { - log_i("Unable to open NVS"); + log_e("Unable to open NVS"); goto exit; } esp_err_t err; err = nvs_set_blob(handle, nvsname, data, size); if (err != ESP_OK) { - log_i("Unable to add EEPROM data to NVS: %s", esp_err_to_name(err)); + log_e("Unable to add EEPROM data to NVS: %s", esp_err_to_name(err)); goto exit; } if (clear) { if (esp_partition_erase_range (mypart, 0, size) != ESP_OK) { - log_i("Unable to clear EEPROM partition"); + log_w("Unable to clear EEPROM partition"); } } exit: From 968e1ba75dbc678a17c77dba1684921b4ced82a7 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Thu, 6 Jun 2019 06:35:24 -0600 Subject: [PATCH 4/4] Set result on success --- libraries/EEPROM/src/EEPROM.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/EEPROM/src/EEPROM.cpp b/libraries/EEPROM/src/EEPROM.cpp index 2e68154e6bb..1bec6127d2c 100644 --- a/libraries/EEPROM/src/EEPROM.cpp +++ b/libraries/EEPROM/src/EEPROM.cpp @@ -264,7 +264,8 @@ uint16_t EEPROMClass::convert (bool clear, const char* EEPROMname, const char* n log_e("Unable to add EEPROM data to NVS: %s", esp_err_to_name(err)); goto exit; } - + result = size; + if (clear) { if (esp_partition_erase_range (mypart, 0, size) != ESP_OK) { log_w("Unable to clear EEPROM partition");