|
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)
|
@@ -214,6 +215,68 @@ 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; |
| 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 | + |
217 | 280 | /*
|
218 | 281 | Read 'value' from 'address'
|
219 | 282 | */
|
|
0 commit comments