Skip to content

Commit b465858

Browse files
committed
Check and format NVS partition if size has changed
1 parent 8032231 commit b465858

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

Diff for: cores/esp32/esp32-hal-misc.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "freertos/task.h"
1919
#include "esp_attr.h"
2020
#include "nvs_flash.h"
21+
#include "nvs.h"
22+
#include "esp_partition.h"
2123
#include <sys/time.h>
2224

2325
void yield()
@@ -76,7 +78,21 @@ void init() {}
7678

7779
void initArduino()
7880
{
79-
nvs_flash_init();
81+
esp_err_t err = nvs_flash_init();
82+
if(err == ESP_ERR_NVS_NO_FREE_PAGES){
83+
const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
84+
if (partition != NULL) {
85+
err = esp_partition_erase_range(partition, 0, partition->size);
86+
if(!err){
87+
err = nvs_flash_init();
88+
} else {
89+
log_e("Failed to format the broken NVS partition!");
90+
}
91+
}
92+
}
93+
if(err) {
94+
log_e("Failed to initialize NVS! Error: %u", err);
95+
}
8096
init();
8197
initVariant();
8298
}

0 commit comments

Comments
 (0)