Check and format NVS partition if size has changed

This commit is contained in:
me-no-dev 2017-04-04 01:27:26 +03:00
parent 80322311f1
commit b465858563

View File

@ -18,6 +18,8 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_attr.h" #include "esp_attr.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "nvs.h"
#include "esp_partition.h"
#include <sys/time.h> #include <sys/time.h>
void yield() void yield()
@ -76,7 +78,21 @@ void init() {}
void initArduino() 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(); init();
initVariant(); initVariant();
} }