How to use NVS Partition tool for esp32

roquita
Posts: 4
Joined: Sun Mar 28, 2021 9:55 pm

How to use NVS Partition tool for esp32

Postby roquita » Sun Mar 28, 2021 11:34 pm

[/Codebox]GITHUB URL of the project: https://github.com/roquita/nvs_partition_tool
IDF VERSION: 4.1

Hello everyone,i was using NVS Partition Tool to generate a nvs image, then with parttool.py flashed this image on nvs partition. I was trying to read two strings from nvs partition but just got "ERR_NVS_NOT_FOUND".

Image of settings on NVS Partition Tool:
nvs.PNG
nvs.PNG (46.08 KiB) Viewed 19798 times
CSV file generated after press "Save" button:
nvs_csv.PNG
nvs_csv.PNG (5.38 KiB) Viewed 19798 times
Image of partition table to verify that is congruent with NVS Partition Tool.
partitions.PNG
partitions.PNG (8.05 KiB) Viewed 19798 times


PROCCES TO FLASH NVS IMAGE
  • Pressing "Generate partition" i got an "nvs.bin" (check in my github url) that was flashed using this command :

    Code: Select all

    python C:\esp41\esp-idf\components\partition_table\parttool.py --port COM18 write_partition --partition-name=nvs --input "nvs.bin"

    Result :

    Code: Select all

    C:\esp32\workspace\testing>python C:\esp41\esp-idf\components\partition_table\parttool.py --port COM18 write_partition --partition-name=nvs --input "nvs.bin"
    Running C:\Users\OWNER\.espressif\python_env\idf4.1_py3.7_env\Scripts\python.exe C:\esp41\esp-idf\components\esptool_py\esptool\esptool.py --port COM18 read_flash 32768 3072 C:\Users\OWNER\AppData\Local\Temp\tmp8n66xm53...
    esptool.py v2.9-dev
    Serial port COM18
    Connecting........_
    Detecting chip type... ESP32
    Chip is ESP32D0WDQ6 (revision 1)
    Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
    Crystal is 40MHz
    MAC: 80:7d:3a:cb:5d:84
    Uploading stub...
    Running stub...
    Stub running...
    3072 (100 %)
    3072 (100 %)
    Read 3072 bytes at 0x8000 in 0.3 seconds (85.4 kbit/s)...
    Hard resetting via RTS pin...
    Running C:\Users\OWNER\.espressif\python_env\idf4.1_py3.7_env\Scripts\python.exe C:\esp41\esp-idf\components\esptool_py\esptool\esptool.py --port COM18 erase_region 36864 24576...
    esptool.py v2.9-dev
    Serial port COM18
    Connecting......
    Detecting chip type... ESP32
    Chip is ESP32D0WDQ6 (revision 1)
    Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
    Crystal is 40MHz
    MAC: 80:7d:3a:cb:5d:84
    Uploading stub...
    Running stub...
    Stub running...
    Erasing region (may be slow depending on size)...
    Erase completed successfully in 0.2 seconds.
    Hard resetting via RTS pin...
    Running C:\Users\OWNER\.espressif\python_env\idf4.1_py3.7_env\Scripts\python.exe C:\esp41\esp-idf\components\esptool_py\esptool\esptool.py --port COM18 write_flash 36864 nvs.bin...
    esptool.py v2.9-dev
    Serial port COM18
    Connecting....
    Detecting chip type... ESP32
    Chip is ESP32D0WDQ6 (revision 1)
    Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
    Crystal is 40MHz
    MAC: 80:7d:3a:cb:5d:84
    Uploading stub...
    Running stub...
    Stub running...
    Configuring flash size...
    Auto-detected Flash size: 4MB
    Compressed 24576 bytes to 213...
    Wrote 24576 bytes (213 compressed) at 0x00009000 in 0.0 seconds (effective 6207.8 kbit/s)...
    Hash of data verified.
    
    Leaving...
    Hard resetting via RTS pin...
    Written contents of file 'nvs.bin' at offset 0x9000
  • Then i read the partition only to verify, using this command:

    Code: Select all

    python C:\esp41\esp-idf\components\partition_table\parttool.py read_partition  --partition-name=nvs --output "flashed.bin"
    Result: A file named "flashed.bin"(check github) was created and some text in command line:

    Code: Select all

    C:\esp32\workspace\testing>python C:\esp41\esp-idf\components\partition_table\parttool.py read_partition  --partition-name=nvs --output "flashed.bin"
    Running C:\Users\OWNER\.espressif\python_env\idf4.1_py3.7_env\Scripts\python.exe C:\esp41\esp-idf\components\esptool_py\esptool\esptool.py read_flash 32768 3072 C:\Users\OWNER\AppData\Local\Temp\tmp07vfnfth...
    esptool.py v2.9-dev
    Found 2 serial ports
    Serial port COM18
    Connecting........__
    Detecting chip type... ESP32
    Chip is ESP32D0WDQ6 (revision 1)
    Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
    Crystal is 40MHz
    MAC: 80:7d:3a:cb:5d:84
    Uploading stub...
    Running stub...
    Stub running...
    3072 (100 %)
    3072 (100 %)
    Read 3072 bytes at 0x8000 in 0.3 seconds (84.8 kbit/s)...
    Hard resetting via RTS pin...
    Running C:\Users\OWNER\.espressif\python_env\idf4.1_py3.7_env\Scripts\python.exe C:\esp41\esp-idf\components\esptool_py\esptool\esptool.py read_flash 36864 24576 
    flashed.bin...
    esptool.py v2.9-dev
    Found 2 serial ports
    Serial port COM18
    Connecting.....
    Detecting chip type... ESP32
    Chip is ESP32D0WDQ6 (revision 1)
    Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
    Crystal is 40MHz
    MAC: 80:7d:3a:cb:5d:84
    Uploading stub...
    Running stub...
    Stub running...
    24576 (100 %)
    24576 (100 %)
    Read 24576 bytes at 0x9000 in 2.2 seconds (88.5 kbit/s)...
    Hard resetting via RTS pin...
    Read partition 'nvs' contents from device at offset 0x9000 to file 'flashed.bin'

READING STRINGS FROM C CODE
  • Build and flash(using "idf.py app-flash monitor") the code below(is also in github):

    Code: Select all

    /* Hello World Example
    
       This example code is in the Public Domain (or CC0 licensed, at your option.)
    
       Unless required by applicable law or agreed to in writing, this
       software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
       CONDITIONS OF ANY KIND, either express or implied.
    */
    #include <stdio.h>
    #include <string.h>
    #include "freertos/FreeRTOS.h"
    #include "freertos/task.h"
    #include "esp_system.h"
    #include "esp_spi_flash.h"
    #include "nvs_flash.h"
    #include "esp_err.h"
    #include "esp_log.h"
    
    #define TAG "MAIN"
    void init_NVS()
    {
        //vTaskDelay(pdMS_TO_TICKS(10000));
        printf("iniciando...");
        esp_err_t ret = nvs_flash_init();
        /*
        if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
        {
            ESP_LOGE(TAG, "%s, line %d, %s", __func__, __LINE__, esp_err_to_name(ret));
            ESP_ERROR_CHECK(nvs_flash_erase());
            ret = nvs_flash_init();
        }
    */
        ESP_ERROR_CHECK(ret);
    }
    esp_err_t nvs_get_string(char *str, char *namespace, char *key)
    {
        nvs_handle handle;
        esp_err_t err;
        err = nvs_open(namespace, NVS_READWRITE, &handle);
        if (err != ESP_OK)
        {
            ESP_LOGE(TAG, "%s, line %d, %s", __func__, __LINE__, esp_err_to_name(err));
            return err;
        }
    
        size_t size = 0;
        err = nvs_get_blob(handle, key, NULL, &size);
    
        if (err != ESP_OK)
        {
            ESP_LOGE(TAG, "%s, line %d, %s", __func__, __LINE__, esp_err_to_name(err));
            return err;
        }
        if (size <= 0)
        {
            ESP_LOGE(TAG, "%s, line %d, %s", __func__, __LINE__, esp_err_to_name(err));
            return err;
        }
    
        err = nvs_get_blob(handle, key, (void *)str, &size);
    
        if (err != ESP_OK)
        {
            ESP_LOGE(TAG, "%s, line %d, %s", __func__, __LINE__, esp_err_to_name(err));
            return err;
        }
    
        nvs_close(handle);
        return ESP_OK;
    }
    esp_err_t nvs_set_string(char *str, char *namespace, char *key)
    {
        nvs_handle handle;
        esp_err_t err;
        err = nvs_open(namespace, NVS_READWRITE, &handle);
        if (err != ESP_OK)
            return err;
    
        err = nvs_set_blob(handle, key, (void *)str, strlen(str) + 1);
    
        if (err != ESP_OK)
            return err;
    
        err = nvs_commit(handle);
        if (err != ESP_OK)
            return err;
    
        nvs_close(handle);
    
        return ESP_OK;
    }
    void app_main(void)
    {
        init_NVS();
    
        char mistring[20] = {0};
    
        // STRINGS BEEN READED FROM NVS BUT WITH NO SUCCESS
        nvs_get_string(mistring, "main", "ssid");
        printf("ssid:%s\n", mistring);
    
        nvs_get_string(mistring, "main", "pass");
        printf("pass:%s\n", mistring);
    
        // PROOF THAT NVS IS WORKING OK
        nvs_set_string("string_saved", "main", "set");
        nvs_get_string(mistring, "main", "set");
        printf("mistring:%s\n", mistring);
    
        // STARTS HELLO WORDL EXAMPLE FROM IDF FRAMEWORK
        printf("Hello world!\n");
    
        /* Print chip information */
        esp_chip_info_t chip_info;
        esp_chip_info(&chip_info);
        printf("This is ESP32 chip with %d CPU cores, WiFi%s%s, ",
               chip_info.cores,
               (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
               (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
    
        printf("silicon revision %d, ", chip_info.revision);
    
        printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
               (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
    
        for (int i = 10; i >= 0; i--)
        {
            printf("Restarting in %d seconds...\n", i);
            vTaskDelay(1000 / portTICK_PERIOD_MS);
        }
        printf("Restarting now.\n");
        fflush(stdout);
        esp_restart();
    }
    
    Result:

    Code: Select all

    I (29) boot: ESP-IDF v4.1.1-dirty 2nd stage bootloader
    I (29) boot: compile time 19:55:15
    I (29) boot: chip revision: 1
    I (32) boot_comm: chip revision: 1, min. bootloader chip revision: 0
    I (39) boot.esp32: SPI Speed      : 40MHz
    I (44) boot.esp32: SPI Mode       : DIO
    I (48) boot.esp32: SPI Flash Size : 4MB
    I (53) boot: Enabling RNG early entropy source...
    I (58) boot: Partition Table:
    I (62) boot: ## Label            Usage          Type ST Offset   Length
    I (69) boot:  0 nvs              WiFi data        01 02 00009000 00006000
    I (77) boot:  1 phy_init         RF data          01 01 0000f000 00001000
    I (84) boot:  2 factory          factory app      00 00 00010000 00100000
    I (92) boot:  3 storage          Unknown data     01 82 00110000 00010000
    I (99) boot: End of partition table
    I (103) boot_comm: chip revision: 1, min. application chip revision: 0
    I (110) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x06bb4 ( 27572) map
    I (130) esp_image: segment 1: paddr=0x00016bdc vaddr=0x3ffb0000 size=0x02014 (  8212) load
    I (134) esp_image: segment 2: paddr=0x00018bf8 vaddr=0x40080000 size=0x00404 (  1028) load
    0x40080000: _WindowOverflow4 at C:/esp41/esp-idf/components/freertos/xtensa_vectors.S:1778
    
    I (138) esp_image: segment 3: paddr=0x00019004 vaddr=0x40080404 size=0x07014 ( 28692) load
    I (159) esp_image: segment 4: paddr=0x00020020 vaddr=0x400d0020 size=0x181b4 ( 98740) map
    0x400d0020: _stext at ??:?
    
    I (197) esp_image: segment 5: paddr=0x000381dc vaddr=0x40087418 size=0x02ff8 ( 12280) load
    0x40087418: prvProcessTimerOrBlockTask at C:/esp41/esp-idf/components/freertos/timers.c:583
    
    I (208) boot: Loaded app from partition at offset 0x10000
    I (208) boot: Disabling RNG early entropy source...
    I (209) cpu_start: Pro cpu up.
    I (212) cpu_start: Application information:
    I (217) cpu_start: Project name:     testing
    I (222) cpu_start: App version:      1
    I (226) cpu_start: Compile time:     Mar 26 2021 19:55:19
    I (233) cpu_start: ELF file SHA256:  5fabec642c71aaa7...
    I (239) cpu_start: ESP-IDF:          v4.1.1-dirty
    I (244) cpu_start: Starting app cpu, entry point is 0x40081148
    0x40081148: call_start_cpu1 at C:/esp41/esp-idf/components/esp32/cpu_start.c:276
    
    I (0) cpu_start: App cpu up.
    I (254) heap_init: Initializing. RAM available for dynamic allocation:
    I (261) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
    I (267) heap_init: At 3FFB2858 len 0002D7A8 (181 KiB): DRAM
    I (274) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
    I (280) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
    I (286) heap_init: At 4008A410 len 00015BF0 (86 KiB): IRAM
    I (292) cpu_start: Pro cpu start user code
    I (311) spi_flash: detected chip: gd
    I (311) spi_flash: flash io: dio
    I (311) cpu_start: Starting scheduler on PRO CPU.
    I (0) cpu_start: Starting scheduler on APP CPU.
    iniciando...
    E (337) MAIN: nvs_get_string, line 51, ESP_ERR_NVS_NOT_FOUND
    ssid:
    E (337) MAIN: nvs_get_string, line 51, ESP_ERR_NVS_NOT_FOUND
    pass:
    mistring:string_saved
    Hello world!
    This is ESP32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
    Restarting in 10 seconds...
    Restarting in 9 seconds...
    Restarting in 8 seconds...
    Restarting in 7 seconds...
    Restarting in 6 seconds...
    Restarting in 5 seconds...
    Restarting in 4 seconds...
    Restarting in 3 seconds...
    Restarting in 2 seconds...
    Restarting in 1 seconds...
    Restarting in 0 seconds...
    As you can see, error is in line 51, related to "nvs_get_string" function. Reading data that was saved in C Code works very well, but reading that was flashed with NVS IMAGE doesnt work. Maybe problem is my code, i dont know.
    Can you help me with that ,please? . Thanks in advance.

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

Re: How to use NVS Partition tool for esp32

Postby ESP_Sprite » Mon Mar 29, 2021 3:07 am

Why are you reading string data using nvs_get_blob?

roquita
Posts: 4
Joined: Sun Mar 28, 2021 9:55 pm

Re: How to use NVS Partition tool for esp32

Postby roquita » Mon Mar 29, 2021 6:36 am

oh right, it was my mistake. Using nvs_get_string works. Can you help me with one thing more, please?. How can i flash nvs image after flashing app automatically?. I want to flash all partitions in one shot. Thanks

ESP_Minatel
Posts: 364
Joined: Mon Jan 04, 2021 2:06 pm

Re: How to use NVS Partition tool for esp32

Postby ESP_Minatel » Mon Mar 29, 2021 8:21 am

Hi,

If you need to flash only the partition table image, you can use

Code: Select all

idf.py -p PORT partition_table-flash

roquita
Posts: 4
Joined: Sun Mar 28, 2021 9:55 pm

Re: How to use NVS Partition tool for esp32

Postby roquita » Tue Mar 30, 2021 5:25 am

For example: spiffs_create_imagen() in the cmake file helps to flash spiffs partition after flashing app image. There is any function available to flash nvs image in the same way?

ESP_Minatel
Posts: 364
Joined: Mon Jan 04, 2021 2:06 pm

Re: How to use NVS Partition tool for esp32

Postby ESP_Minatel » Tue Mar 30, 2021 8:11 pm

Hi,

Are you looking for some mass production automation tool to customize the configuration data in the NVS?

Take a look at this: mass_mfg.

roquita
Posts: 4
Joined: Sun Mar 28, 2021 9:55 pm

Re: How to use NVS Partition tool for esp32

Postby roquita » Wed Mar 31, 2021 3:59 am

okay, thank you very much esp forum team.

Who is online

Users browsing this forum: No registered users and 14 guests