Skip to content

Commit 1d2b78d

Browse files
committed
Added convert method to transfer data from partition to nvs
1 parent aff2e42 commit 1d2b78d

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

libraries/EEPROM/library.properties

Lines changed: 2 additions & 2 deletions
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]>
55
sentence=Enables reading and writing data to the permanent FLASH storage, up to 4kb.
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

Lines changed: 63 additions & 0 deletions
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)
@@ -214,6 +215,68 @@ 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;
232+
size = mypart->size;
233+
uint8_t* data;
234+
data = (uint8_t*) malloc(size);
235+
if (!data) {
236+
log_e("Not enough memory to convert EEPROM!");
237+
goto exit;
238+
}
239+
240+
if (esp_partition_read (mypart, 0, (void *) data, size) != ESP_OK) {
241+
log_i("Unable to read EEPROM partition");
242+
goto exit;
243+
}
244+
245+
bool empty;
246+
empty = true;
247+
for (int x=0; x<size; x++) {
248+
if (data[x] != 0xFF) {
249+
empty = false;
250+
break;
251+
}
252+
}
253+
if (empty) {
254+
log_i("EEPROM partition is empty, will not convert");
255+
goto exit;
256+
}
257+
258+
nvs_handle handle;
259+
if (nvs_open(nvsname, NVS_READWRITE, &handle) != ESP_OK) {
260+
log_i("Unable to open NVS");
261+
goto exit;
262+
}
263+
esp_err_t err;
264+
err = nvs_set_blob(handle, nvsname, data, size);
265+
if (err != ESP_OK) {
266+
log_i("Unable to add EEPROM data to NVS: %s", esp_err_to_name(err));
267+
goto exit;
268+
}
269+
270+
if (clear) {
271+
if (esp_partition_erase_range (mypart, 0, size) != ESP_OK) {
272+
log_i("Unable to clear EEPROM partition");
273+
}
274+
}
275+
exit:
276+
free(data);
277+
return size;
278+
}
279+
217280
/*
218281
Read 'value' from 'address'
219282
*/

libraries/EEPROM/src/EEPROM.h

Lines changed: 1 addition & 0 deletions
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)