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 ?

Who is online

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