|
25 | 25 |
|
26 | 26 | #include "EEPROM.h"
|
27 | 27 | #include <nvs.h>
|
| 28 | +#include <esp_partition.h> |
28 | 29 | #include <esp_log.h>
|
29 | 30 |
|
30 | 31 | EEPROMClass::EEPROMClass(void)
|
@@ -111,7 +112,7 @@ bool EEPROMClass::begin(size_t size) {
|
111 | 112 | log_e("Not enough memory to expand EEPROM!");
|
112 | 113 | return false;
|
113 | 114 | }
|
114 |
| - memset(key_data, 0, size); |
| 115 | + memset(key_data, 0xFF, size); |
115 | 116 | if(key_size) {
|
116 | 117 | log_i("Expanding EEPROM from %d to %d", key_size, size);
|
117 | 118 | // hold data while key is deleted
|
@@ -214,6 +215,67 @@ uint16_t EEPROMClass::length ()
|
214 | 215 | return _user_defined_size;
|
215 | 216 | }
|
216 | 217 |
|
| 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 | + |
217 | 279 | /*
|
218 | 280 | Read 'value' from 'address'
|
219 | 281 | */
|
|
0 commit comments