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
This commit is contained in:
parent
87e5787cf7
commit
ca7106e97e
@ -1,8 +1,8 @@
|
|||||||
name=EEPROM
|
name=EEPROM
|
||||||
version=1.0
|
version=1.0.3
|
||||||
author=Ivan Grokhotkov
|
author=Ivan Grokhotkov
|
||||||
maintainer=Paolo Becchi <pbecchi@aerobusiness.it>
|
maintainer=Paolo Becchi <pbecchi@aerobusiness.it>
|
||||||
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=
|
paragraph=
|
||||||
category=Data Storage
|
category=Data Storage
|
||||||
url=http://arduino.cc/en/Reference/EEPROM
|
url=http://arduino.cc/en/Reference/EEPROM
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
#include "EEPROM.h"
|
#include "EEPROM.h"
|
||||||
#include <nvs.h>
|
#include <nvs.h>
|
||||||
|
#include <esp_partition.h>
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
|
|
||||||
EEPROMClass::EEPROMClass(void)
|
EEPROMClass::EEPROMClass(void)
|
||||||
@ -111,7 +112,7 @@ bool EEPROMClass::begin(size_t size) {
|
|||||||
log_e("Not enough memory to expand EEPROM!");
|
log_e("Not enough memory to expand EEPROM!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
memset(key_data, 0, size);
|
memset(key_data, 0xFF, size);
|
||||||
if(key_size) {
|
if(key_size) {
|
||||||
log_i("Expanding EEPROM from %d to %d", key_size, size);
|
log_i("Expanding EEPROM from %d to %d", key_size, size);
|
||||||
// hold data while key is deleted
|
// hold data while key is deleted
|
||||||
@ -214,6 +215,67 @@ uint16_t EEPROMClass::length ()
|
|||||||
return _user_defined_size;
|
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'
|
Read 'value' from 'address'
|
||||||
*/
|
*/
|
||||||
|
@ -47,6 +47,7 @@ class EEPROMClass {
|
|||||||
void end();
|
void end();
|
||||||
|
|
||||||
uint8_t * getDataPtr();
|
uint8_t * getDataPtr();
|
||||||
|
uint16_t convert(bool clear, const char* EEPROMname = "eeprom", const char* nvsname = "eeprom");
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T &get(int address, T &t) {
|
T &get(int address, T &t) {
|
||||||
|
Loading…
Reference in New Issue
Block a user