Error (ESP_ERR_NVS_PART_NOT_FOUND) when opening a custom NVS partition

_K00CT_
Posts: 7
Joined: Thu Feb 23, 2023 9:18 am

Error (ESP_ERR_NVS_PART_NOT_FOUND) when opening a custom NVS partition

Postby _K00CT_ » Wed Sep 06, 2023 9:59 am

Hi there

I am running the example https://github.com/espressif/esp-idf/tr ... s_rw_value on an ESP32.

When I use the default NVS storage (partition: "nvs") the code works as expected. When I try to use a custom partition, the device runs into the error ESP_ERR_NVS_PART_NOT_FOUND.

I would appreciate any help understanding what step(s) I have missed to get this functioning.

I have made the following changes to the example:
1. Custom name is 'nvsa' and I am using the function nvs_flash_init_partition() instead of nvs_flash_init.

Code: Select all

const char* NVS_PARTITION_NAME = "nvsa";
// Initialize NVS
// esp_err_t err = nvs_flash_init();
esp_err_t err = nvs_flash_init_partition(NVS_PARTITION_NAME);
2. Custom partition table, I have configured the project to use my own partition table.

Code: Select all

# ESP-IDF Partition Table
# Name,   Type, SubType, Offset,  Size, Flags
nvsa,      data, nvs,     0x9000,  0x6000,
phy_init, data, phy,     0xf000,  0x1000,
factory,  app,  factory, 0x10000, 1M,

I can verify that the partition binary is indeed created:
Partition table binary generated. Contents:

Code: Select all

*******************************************************************************
# ESP-IDF Partition Table
# Name, Type, SubType, Offset, Size, Flags
nvsa,data,nvs,0x9000,24K,
phy_init,data,phy,0xf000,4K,
factory,app,factory,0x10000,1M,
*******************************************************************************

The output I get is:

Code: Select all

I (328) app_start: Starting scheduler on CPU0
I (332) app_start: Starting scheduler on CPU1
I (332) main_task: Started on CPU0
I (342) main_task: Calling app_main()

Opening Non-Volatile Storage (NVS) handle... Error (ESP_ERR_NVS_PART_NOT_FOUND) opening NVS handle!

Finally, if I change the char array NVS_PARTITION_NAME to just 'nvs' (and update the partition table), it functions properly. I perform a flash erase between changes to partitions.csv and a clean build before flashing.

Any help is much appreciated!


============================

I have further verified that the partition indeed exists (and I have tried using the partition pointer to initialize it), but still no success:

Code: Select all

void app_main(void)
{
    const char *NVS_PARTITION_NAME = "nvsa";

    const esp_partition_t *partitionNvs = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NVS_PARTITION_NAME);
    if (partitionNvs == NULL)
    {
        ESP_LOGI(TAG, "NVS Partition \"%s\" not found", NVS_PARTITION_NAME);
    }
    else
    {
        ESP_LOGI(TAG, "NVS Partition \"%s\" found", NVS_PARTITION_NAME);
    }

    // Initialize NVS
    // esp_err_t err = nvs_flash_init();
    // esp_err_t err = nvs_flash_init_partition(NVS_PARTITION_NAME);
    esp_err_t err = nvs_flash_init_partition_ptr(partitionNvs);
    if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        // NVS partition was truncated and needs to be erased
        // Retry nvs_flash_init
        ESP_ERROR_CHECK(nvs_flash_erase());
        err = nvs_flash_init();
    }
    ESP_ERROR_CHECK(err);

Code: Select all

I (0) cpu_start: App cpu up.
I (221) cpu_start: Pro cpu start user code
I (221) cpu_start: cpu freq: 160000000 Hz
I (221) cpu_start: Application information:
I (226) cpu_start: Project name:     nvs-c-storage
I (231) cpu_start: App version:      1
I (235) cpu_start: Compile time:     Sep  7 2023 09:09:17
I (242) cpu_start: ELF file SHA256:  16f9ba6a86602b46...
I (248) cpu_start: ESP-IDF:          v5.1-dirty
I (253) cpu_start: Min chip rev:     v0.0
I (257) cpu_start: Max chip rev:     v3.99 
I (262) cpu_start: Chip rev:         v3.1
I (267) heap_init: Initializing. RAM available for dynamic allocation:
I (274) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (280) heap_init: At 3FFB29D0 len 0002D630 (181 KiB): DRAM
I (287) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (293) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (299) heap_init: At 4008C688 len 00013978 (78 KiB): IRAM
I (307) spi_flash: detected chip: generic
I (310) spi_flash: flash io: dio
W (314) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (328) app_start: Starting scheduler on CPU0
I (332) app_start: Starting scheduler on CPU1
I (332) main_task: Started on CPU0
I (342) main_task: Calling app_main()
I (342) NVS-Storage-Example: NVS Partition "nvsa" found

Opening Non-Volatile Storage (NVS) handle... Error (ESP_ERR_NVS_PART_NOT_FOUND) opening NVS handle!

Restarting in 4 seconds...
Restarting in 3 seconds...
Restarting in 2 seconds...
Restarting in 1 seconds...
Restarting in 0 seconds...
Restarting now.
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)

MicroController
Posts: 1701
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Error (ESP_ERR_NVS_PART_NOT_FOUND) when opening a custom NVS partition

Postby MicroController » Fri Sep 08, 2023 7:03 pm

Try this:

Code: Select all

esp_err_t err = nvs_flash_init_partition(NVS_PARTITION_NAME);

if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  ESP_ERROR_CHECK(nvs_flash_erase_partition(NVS_PARTITION_NAME));
  err = nvs_flash_init_partition(NVS_PARTITION_NAME);
}

ESP_ERROR_CHECK(err);

_K00CT_
Posts: 7
Joined: Thu Feb 23, 2023 9:18 am

Re: Error (ESP_ERR_NVS_PART_NOT_FOUND) when opening a custom NVS partition

Postby _K00CT_ » Wed Sep 13, 2023 8:57 am

Solved

Thank you MicroController for your response! I have revisited my code to try your suggestion and found the actual cause of the problem.

I should have shared more code in my original post because the error was actually being generated just a couple of lines down!

Here was my mistake:

Code: Select all

// Open
    printf("\n");
    printf("Opening Non-Volatile Storage (NVS) handle...\n");
    nvs_handle_t my_handle;
    err = nvs_open("storage", NVS_READWRITE, &my_handle); // <- MISTAKE - this only applies to the partition 'nvs' which doesn't exist
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "Error (%s) opening NVS handle!", esp_err_to_name(err));
    }
    else
    {
To fix, I needed to use:

Code: Select all

err = nvs_open_from_partition(NVS_PARTITION_NAME, "storage", NVS_READWRITE, &my_handle);
Working code, in context:

Code: Select all

const char *NVS_PARTITION_NAME = "nvsa";

    const esp_partition_t *partitionNvs = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NVS_PARTITION_NAME);
    if (partitionNvs == NULL)
    {
        ESP_LOGI(TAG, "NVS Partition with name \"%s\" not found", NVS_PARTITION_NAME);
    }
    else
    {
        ESP_LOGI(TAG, "NVS Partition labeled \"%s\" found", partitionNvs->label);
    }

    // Initialize NVS - options
    // esp_err_t err = nvs_flash_init(); // default - only for use with partition 'nvs'
    // esp_err_t err = nvs_flash_init_partition(NVS_PARTITION_NAME); // given a partition name char*
    // esp_err_t err = nvs_flash_init_partition_ptr(partitionNvs); // given a pointer to an esp_partition_t

    esp_err_t err = nvs_flash_init_partition_ptr(partitionNvs);
    if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        // NVS partition was truncated and needs to be erased
        // Retry nvs_flash_init
        ESP_ERROR_CHECK(nvs_flash_erase());
        err = nvs_flash_init();
    }
    ESP_ERROR_CHECK(err);

    if (err == ESP_OK)
    {
        ESP_LOGI(TAG, "Flash initialized success");
    }

    // Open
    printf("\n");
    printf("Opening Non-Volatile Storage (NVS) handle...\n");
    nvs_handle_t my_handle;
    err = nvs_open_from_partition(NVS_PARTITION_NAME, "storage", NVS_READWRITE, &my_handle);
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "Error (%s) opening NVS handle!", esp_err_to_name(err));
    }
    else
    {
        ESP_LOGI(TAG, "Opened 'storage' namespace from NVS");
        // code continues from here....

Who is online

Users browsing this forum: Majestic-12 [Bot] and 236 guests