From b465858563b05addf25e91e1e9407883cd4256f7 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Tue, 4 Apr 2017 01:27:26 +0300 Subject: [PATCH] Check and format NVS partition if size has changed --- cores/esp32/esp32-hal-misc.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-misc.c b/cores/esp32/esp32-hal-misc.c index b5ea9d67..a2535500 100644 --- a/cores/esp32/esp32-hal-misc.c +++ b/cores/esp32/esp32-hal-misc.c @@ -18,6 +18,8 @@ #include "freertos/task.h" #include "esp_attr.h" #include "nvs_flash.h" +#include "nvs.h" +#include "esp_partition.h" #include void yield() @@ -76,7 +78,21 @@ void init() {} void initArduino() { - nvs_flash_init(); + esp_err_t err = nvs_flash_init(); + if(err == ESP_ERR_NVS_NO_FREE_PAGES){ + const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL); + if (partition != NULL) { + err = esp_partition_erase_range(partition, 0, partition->size); + if(!err){ + err = nvs_flash_init(); + } else { + log_e("Failed to format the broken NVS partition!"); + } + } + } + if(err) { + log_e("Failed to initialize NVS! Error: %u", err); + } init(); initVariant(); }