Skip to content

Added convert method to EEPROM to transfer data from partition to nvs #2841

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 4 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions libraries/EEPROM/library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=EEPROM
version=1.0
version=1.0.3
author=Ivan Grokhotkov
maintainer=Paolo Becchi <[email protected]>
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
architectures=esp32
architectures=esp32
64 changes: 63 additions & 1 deletion libraries/EEPROM/src/EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "EEPROM.h"
#include <nvs.h>
#include <esp_partition.h>
#include <esp_log.h>

EEPROMClass::EEPROMClass(void)
Expand Down Expand Up @@ -111,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
Expand Down Expand Up @@ -214,6 +215,67 @@ 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 = mypart->size;
uint8_t* 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_e("Unable to read EEPROM partition");
goto exit;
}

bool empty;
empty = true;
for (int x=0; x<size; x++) {
if (data[x] != 0xFF) {
empty = false;
break;
}
}
if (empty) {
log_i("EEPROM partition is empty, will not convert");
goto exit;
}

nvs_handle handle;
if (nvs_open(nvsname, NVS_READWRITE, &handle) != ESP_OK) {
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_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");
}
}
exit:
free(data);
return result;
}

/*
Read 'value' from 'address'
*/
Expand Down
1 change: 1 addition & 0 deletions libraries/EEPROM/src/EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class EEPROMClass {
void end();

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

template<typename T>
T &get(int address, T &t) {
Expand Down