nvs_flash_init_partition() fails when running concurrently with WIFI

Serdar
Posts: 18
Joined: Fri Sep 02, 2022 7:33 pm

nvs_flash_init_partition() fails when running concurrently with WIFI

Postby Serdar » Mon Jun 12, 2023 5:59 pm

Hi, I have 2 tasks that are working fine separately.
Actually, I know where the problem comes from.

Code: Select all

    // Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    // Initialize WiFi
    tcpip_adapter_init();
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
I use nvs_flash_init() function before starting wifi.

But in my code sometimes I need to save some values to flash memory.
These values can be in the data sent by the device connected via wifi.

The following code is inside a separate task. it may want to access NVS_Flash at a random time.

Code: Select all

       sprintf(str, "storage%i", offset);

        err = nvs_flash_init_partition(str);
        if (err != ESP_OK)
        {
                pr_error_str_msg(LOG_PFX "wiced_dct_get_app_section: nvs_open_from_partition(NVS_READONLY) returned: ", esp_err_to_name(err));
                // it may be that the storage driver is not initialized; if so, initialize it here.
                if (err == ESP_ERR_NVS_NOT_INITIALIZED || err == ESP_ERR_NVS_PART_NOT_FOUND) {
                        rc = nvs_flash_init_partition(str);

                }
                rc = nvs_open_from_partition(str, NVS_APP_NAMESPACE, NVS_READWRITE, &my_handle);
                if (rc != ESP_OK) {
                        return MEMORY_FLASH_WRITE_ERROR;
                }
        }
        else
        {
                // Open
                err = nvs_open_from_partition(str, NVS_APP_NAMESPACE, NVS_READWRITE, &my_handle);
        }

        err = nvs_set_blob(my_handle, "data", data, length);

        if (err != ESP_OK)
                pr_error_str_msg(LOG_PFX "nvs_set_blob returned: ", esp_err_to_name(err));

// Commit
        err = nvs_commit(my_handle);
        if (err != ESP_OK)
                pr_error_str_msg(LOG_PFX "nvs_commit returned: ", esp_err_to_name(err));

// Close
        nvs_close(my_handle);
If I want to write data to flash memory while wifi is working, I get these errors.
nvs_flash_init_partition() returns ESP_ERR_NVS_PART_NOT_FOUND.
nvs_set_blob() returns ESP_ERR_NVS_NOT_ENOUGH_SPACE .


How can I run these two at the same time ?

ESP_rrtandler
Posts: 53
Joined: Wed May 31, 2023 6:54 pm

Re: nvs_flash_init_partition() fails when running concurrently with WIFI

Postby ESP_rrtandler » Thu Nov 21, 2024 12:01 pm

Hi Serdar,

I would like to learn whether this issue is still actual.
If so, please extend the code with logging of the str variable content prior to the call of nvs functions and result code returned by each of the nvs functions called. Please post the values log. Please include also content of the partition table.

snutw_
Posts: 40
Joined: Wed Aug 21, 2024 1:48 pm

Re: nvs_flash_init_partition() fails when running concurrently with WIFI

Postby snutw_ » Fri Dec 20, 2024 7:28 am

Hi Serdar,

I would like to learn whether this issue is still actual.
If so, please extend the code with logging of the str variable content prior to the call of nvs functions and result code returned by each of the nvs functions called. Please post the values log. Please include also content of the partition table.
Well reading your other answers I have found a hint that maybe this could be due to "power off event in sensitive part of the code".
http://esp32.io/viewtopic.php?f=2&t=390 ... c0#p129885
(and also my suspicions about a reason why I have a similar situation getting ESP_ERR_NVS_NOT_ENOUGH_SPACE after a device run overnight).
I hope that I understand correctly.
I got this dump below after running python nvs_tool.py nvs.img -f text:

Code: Select all

←[1mPage Empty, Page address: 0x0←[0m
←[1m Entry state bitmap: ←[0mff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
←[1m 000.←[0m Empty
←[1m ...←[0m
←[1m 125.←[0m Empty

←[1mPage Empty, Page address: 0x1000←[0m
←[1m Entry state bitmap: ←[0mff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
←[1m 000.←[0m Empty
←[1m ...←[0m
←[1m 125.←[0m Empty
...and the same to the end

ESP_rrtandler
Posts: 53
Joined: Wed May 31, 2023 6:54 pm

Re: nvs_flash_init_partition() fails when running concurrently with WIFI

Postby ESP_rrtandler » Tue Jan 07, 2025 2:39 pm

Hi snutw_,

From the information you have provided, it is not clear, what your code looks like and at which moment the ESP_ERR_NVS_NOT_ENOUGH_SPACE error occurs. The nvs_tool.py just shows that the nvs.img is a file populated with 0xff bytes of data.

To figure out, what may go wrong with your application, please post the content of partition table, sdkconfig, code fragments where error occurs and log messages from console.

Who is online

Users browsing this forum: Bing [Bot], ChatGPT-User and 2 guests