#include "unity.h"
#include "nvs.h"
#include "nvs_flash.h"
-#include "esp_spi_flash.h"
+#include "esp_partition.h"
+#include "esp_log.h"
#include <string.h>
+static const char* TAG = "test_nvs";
TEST_CASE("various nvs tests", "[nvs]")
{
nvs_handle handle_1;
- TEST_ESP_OK(nvs_flash_init());
+ esp_err_t err = nvs_flash_init();
+ if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
+ ESP_LOGW(TAG, "nvs_flash_init failed (0x%x), erasing partition and retrying", err);
+ const esp_partition_t* nvs_partition = esp_partition_find_first(
+ ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
+ assert(nvs_partition && "partition table must have an NVS partition");
+ ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
+ err = nvs_flash_init();
+ }
+ ESP_ERROR_CHECK( err );
+
TEST_ESP_ERR(nvs_open("test_namespace1", NVS_READONLY, &handle_1), ESP_ERR_NVS_NOT_FOUND);
TEST_ESP_ERR(nvs_set_i32(handle_1, "foo", 0x12345678), ESP_ERR_NVS_INVALID_HANDLE);
{
esp_err_t ret;
- nvs_flash_init();
+ ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi();
esp_bt_controller_init();
void app_main(void)
{
- nvs_flash_init();
+ ESP_ERROR_CHECK( nvs_flash_init() );
wifi_conn_init();
xTaskCreate(coap_demo_thread, "coap", 2048, NULL, 5, NULL);
}
void app_main(void)
{
- nvs_flash_init();
+ ESP_ERROR_CHECK( nvs_flash_init() );
wifi_conn_init();
xTaskCreate(coap_demo_thread, "coap", 2048, NULL, 5, NULL);
void app_main()
{
- nvs_flash_init();
+ ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi();
xTaskCreate(&http_get_task, "http_get_task", 2048, NULL, 5, NULL);
}
void app_main()
{
- nvs_flash_init();
+ ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi();
xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL);
}
void app_main()
{
- nvs_flash_init();
+ ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi();
xTaskCreate(&mdns_task, "mdns_task", 2048, NULL, 5, NULL);
}
void app_main(void)
{
- nvs_flash_init();
+ ESP_ERROR_CHECK( nvs_flash_init() );
wifi_conn_init();
}
void app_main(void)
{
- nvs_flash_init();
+ ESP_ERROR_CHECK( nvs_flash_init() );
wifi_conn_init();
}
static void obtain_time(void)
{
- nvs_flash_init();
+ ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi();
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
false, true, portMAX_DELAY);
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
+#include "esp_partition.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "driver/gpio.h"
void app_main()
{
- nvs_flash_init();
-
- esp_err_t err;
+ esp_err_t err = nvs_flash_init();
+ if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
+ // NVS partition was truncated and needs to be erased
+ const esp_partition_t* nvs_partition = esp_partition_find_first(
+ ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
+ assert(nvs_partition && "partition table must have an NVS partition");
+ ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
+ // Retry nvs_flash_init
+ err = nvs_flash_init();
+ }
+ ESP_ERROR_CHECK( err );
err = print_what_saved();
if (err != ESP_OK) printf("Error (%d) reading data from NVS!\n", err);
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
+#include "esp_partition.h"
#include "nvs_flash.h"
#include "nvs.h"
void app_main()
{
- nvs_flash_init();
-
- nvs_handle my_handle;
- esp_err_t err;
-
- printf("\n");
+ // Initialize NVS
+ esp_err_t err = nvs_flash_init();
+ if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
+ // NVS partition was truncated and needs to be erased
+ const esp_partition_t* nvs_partition = esp_partition_find_first(
+ ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
+ assert(nvs_partition && "partition table must have an NVS partition");
+ ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
+ // Retry nvs_flash_init
+ err = nvs_flash_init();
+ }
+ ESP_ERROR_CHECK( err );
// Open
- printf("Opening Non-Volatile Storage (NVS) ... ");
+ printf("\n");
+ printf("Opening Non-Volatile Storage (NVS) handle... ");
+ nvs_handle my_handle;
err = nvs_open("storage", NVS_READWRITE, &my_handle);
if (err != ESP_OK) {
- printf("Error (%d) opening NVS!\n", err);
+ printf("Error (%d) opening NVS handle!\n", err);
} else {
printf("Done\n");
#include "esp_ota_ops.h"
#include "esp_partition.h"
+#include "nvs.h"
#include "nvs_flash.h"
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
void app_main()
{
- nvs_flash_init();
+ // Initialize NVS.
+ esp_err_t err = nvs_flash_init();
+ if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
+ // OTA app partition table has a smaller NVS partition size than the non-OTA
+ // partition table. This size mismatch may cause NVS initialization to fail.
+ // If this happens, we erase NVS partition and initialize NVS again.
+ const esp_partition_t* nvs_partition = esp_partition_find_first(
+ ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
+ assert(nvs_partition && "partition table must have an NVS partition");
+ ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
+ err = nvs_flash_init();
+ }
+ ESP_ERROR_CHECK( err );
+
initialise_wifi();
xTaskCreate(&main_task, "main_task", 8192, NULL, 5, NULL);
}
void app_main()
{
- nvs_flash_init();
+ ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi();
xTaskCreate(&wpa2_enterprise_task, "wpa2_enterprise_task", 4096, NULL, 5, NULL);
}