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(); }