Skip to content

Commit ca7106e

Browse files
lbernstoneme-no-dev
authored andcommitted
Added convert method to EEPROM to transfer data from partition to nvs (#2841)
* Added convert method to transfer data from partition to nvs * Could have sworn I changed the return when I made the label * Empty state should be 0xFF. Fixed some logging levels * Set result on success
1 parent 87e5787 commit ca7106e

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

libraries/EEPROM/library.properties

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=EEPROM
2-
version=1.0
2+
version=1.0.3
33
author=Ivan Grokhotkov
44
maintainer=Paolo Becchi <[email protected]>
5-
sentence=Enables reading and writing data to the permanent FLASH storage, up to 4kb.
5+
sentence=Enables reading and writing data a sequential, addressable FLASH storage
66
paragraph=
77
category=Data Storage
88
url=http://arduino.cc/en/Reference/EEPROM
9-
architectures=esp32
9+
architectures=esp32

libraries/EEPROM/src/EEPROM.cpp

+63-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "EEPROM.h"
2727
#include <nvs.h>
28+
#include <esp_partition.h>
2829
#include <esp_log.h>
2930

3031
EEPROMClass::EEPROMClass(void)
@@ -111,7 +112,7 @@ bool EEPROMClass::begin(size_t size) {
111112
log_e("Not enough memory to expand EEPROM!");
112113
return false;
113114
}
114-
memset(key_data, 0, size);
115+
memset(key_data, 0xFF, size);
115116
if(key_size) {
116117
log_i("Expanding EEPROM from %d to %d", key_size, size);
117118
// hold data while key is deleted
@@ -214,6 +215,67 @@ uint16_t EEPROMClass::length ()
214215
return _user_defined_size;
215216
}
216217

218+
/*
219+
Convert EEPROM partition into nvs blob
220+
Call convert before you call begin
221+
*/
222+
uint16_t EEPROMClass::convert (bool clear, const char* EEPROMname, const char* nvsname)
223+
{
224+
uint16_t result = 0;
225+
const esp_partition_t* mypart = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, EEPROMname);
226+
if (mypart == NULL) {
227+
log_i("EEPROM partition not found for conversion");
228+
return result;
229+
}
230+
231+
size_t size = mypart->size;
232+
uint8_t* data = (uint8_t*) malloc(size);
233+
if (!data) {
234+
log_e("Not enough memory to convert EEPROM!");
235+
goto exit;
236+
}
237+
238+
if (esp_partition_read (mypart, 0, (void *) data, size) != ESP_OK) {
239+
log_e("Unable to read EEPROM partition");
240+
goto exit;
241+
}
242+
243+
bool empty;
244+
empty = true;
245+
for (int x=0; x<size; x++) {
246+
if (data[x] != 0xFF) {
247+
empty = false;
248+
break;
249+
}
250+
}
251+
if (empty) {
252+
log_i("EEPROM partition is empty, will not convert");
253+
goto exit;
254+
}
255+
256+
nvs_handle handle;
257+
if (nvs_open(nvsname, NVS_READWRITE, &handle) != ESP_OK) {
258+
log_e("Unable to open NVS");
259+
goto exit;
260+
}
261+
esp_err_t err;
262+
err = nvs_set_blob(handle, nvsname, data, size);
263+
if (err != ESP_OK) {
264+
log_e("Unable to add EEPROM data to NVS: %s", esp_err_to_name(err));
265+
goto exit;
266+
}
267+
result = size;
268+
269+
if (clear) {
270+
if (esp_partition_erase_range (mypart, 0, size) != ESP_OK) {
271+
log_w("Unable to clear EEPROM partition");
272+
}
273+
}
274+
exit:
275+
free(data);
276+
return result;
277+
}
278+
217279
/*
218280
Read 'value' from 'address'
219281
*/

libraries/EEPROM/src/EEPROM.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class EEPROMClass {
4747
void end();
4848

4949
uint8_t * getDataPtr();
50+
uint16_t convert(bool clear, const char* EEPROMname = "eeprom", const char* nvsname = "eeprom");
5051

5152
template<typename T>
5253
T &get(int address, T &t) {

0 commit comments

Comments
 (0)