NVS access issue when app size >8MB and PSRAM enabled

VinayDand
Posts: 5
Joined: Fri Jun 24, 2022 6:47 am

NVS access issue when app size >8MB and PSRAM enabled

Postby VinayDand » Sat Nov 23, 2024 9:02 am

In the project ESP32-S3R8 with 8MB of PSRAM and external 16MB of Flash is used.

We use custom partition table to have a small NVS partition and a large App partition (15Mb) and no OTA partition.

Project works when App size is less than 8MB and PSRAM is enabled. No error in accessing the NVS partition.
As soon as App size become greater than 8MB and PSRAM is kept enabled. Error generated on accessing the NVS data – (NVS partition not available)
Also even when App size is greater than 8MB but PSRAM is disabled. No error in accessing the NVS partition.

Error message:
E (2081) partition: load_partitions returned 0x101
ESP_ERROR_CHECK failed: esp_err_t 0x105 (ESP_ERR_NOT_FOUND) at 0x420084f8
0x420084f8: app_main at /main.c:18 (discriminator 1)


NVS access error in this code
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "nvs_flash.h"
  5. #include "nvs.h"
  6. #include "esp_log.h"
  7. extern uint8_t MyFile_bin_start[] asm("_binary_MyFile_bin_start");
  8. int cnt=0;
  9. void app_main(void)
  10. {
  11. // Initialize NVS
  12.     esp_err_t err = nvs_flash_init();
  13.     if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
  14.     {
  15.         ESP_ERROR_CHECK(nvs_flash_erase());
  16.         err = nvs_flash_init();
  17.     }
  18.     ESP_ERROR_CHECK(err);
  19.     vTaskDelay(20 / portTICK_PERIOD_MS);
  20.     nvs_handle_t nvs_handle;
  21.  
  22.     // Open NVS handle
  23.     err = nvs_open("nvs", NVS_READWRITE, &nvs_handle);
  24.     if (err != ESP_OK)
  25.     {
  26.         ESP_LOGI("MyLog", "Error (%s) opening write NVS handle!\n", esp_err_to_name(err));
  27.         return;
  28.     }
  29.  
  30.     // Read array from NVS under the provided key
  31.     size_t required_size = 1 * sizeof(int);
  32.     err = nvs_get_blob(nvs_handle, "cntData", &cnt, &required_size);
  33.     if (err == ESP_OK)
  34.     {
  35.         ESP_LOGI("MyLog", "Array for key cntData retrieved successfully!\n");
  36.         // for (int i = 0; i < size; i++) {
  37.         //     ESP_LOGI("MyLog","Array[%d] = %d\n", i, arr[i]);
  38.         // }
  39.     }
  40.     else
  41.     {
  42.         ESP_LOGI("MyLog", "Error (%s) reading key %s!\n", esp_err_to_name(err), "cntData");
  43.     }
  44.  
  45.     cnt++;
  46.     // Write array to NVS under the provided key
  47.     err = nvs_set_blob(nvs_handle, "cntData", &cnt, required_size);
  48.     if (err != ESP_OK)
  49.     {
  50.         ESP_LOGI("MyLog", "Error (%s) writing to key %s!\n", esp_err_to_name(err), "cntData");
  51.     }
  52.  
  53.     // Commit write
  54.     err = nvs_commit(nvs_handle);
  55.     if (err != ESP_OK)
  56.     {
  57.         ESP_LOGI("MyLog", "Error (%s) committing!\n", esp_err_to_name(err));
  58.     }
  59.  
  60.     // Close NVS handle
  61.     nvs_close(nvs_handle);
  62.  
  63.     ESP_LOGI("MyLog", "Cnt value: %d", cnt);
  64.     cnt = MyFile_bin_start[0];  //array from external linked bin file
  65. }
Looks like when App size is greater than 8MB + PSRAM is enabled than NVS partition access generates error (load_partitions returned 0x101).

A small project is attached to demonstrate this effect.
In this project an external bin file (MyFile.bin) containing a large size array is accessed from the App. By varying the data size of the bin file, overall app size can be made greater or less than 8MB.

The PSRAM usage is enabled / disabled using the menu-config option. (menuconfig -- ESP PSRAM -- Support for external, SPI-connected RAM)

In the project, at the beginning, a variable is accessed from the NVS storage area, incremented and stored back to NVS.

Any guidance/hint at solving the NVS partition access when App size is greater than 8MB and PSRAM enabled is appreciated.

Custom partition table:
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0xA000, 0x6000,
phy_init, data, phy, , 0x1000,
factory, app, factory, 0x100000, 15M,
Attachments
ESp32s3_NVS_LongApp.zip
project to explore the error
(22.53 KiB) Downloaded 142 times

pacucha42
Posts: 33
Joined: Fri Mar 29, 2019 12:56 pm

Re: NVS access issue when app size >8MB and PSRAM enabled

Postby pacucha42 » Sun Nov 24, 2024 8:55 am

Hi @VinayDand,
error 0x101 means out of memory, which can have various reasons in your case. Quick-fix proposal before checking your logs:
try to enable NVS allocations in PSRAM in the menuconfig (idf.py menuconfig):

Component config > NVS > Prefers allocation of in-memory cache structures in SPI connected PSRAM (CONFIG_NVS_ALLOCATE_CACHE_IN_SPIRAM)

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

Re: NVS access issue when app size >8MB and PSRAM enabled

Postby MicroController » Sun Nov 24, 2024 1:02 pm

May have something to do with memory mapping via the MMU. Both the application in flash and the PSRAM are memory-mapped. And NVS tries to also memory-map part of the NVS partition. Might be that the MMU runs out of pages after 8MB of flash + 8MB of PSRAM are mapped in.

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

Re: NVS access issue when app size >8MB and PSRAM enabled

Postby ESP_Sprite » Mon Nov 25, 2024 2:17 am

MicroController wrote:
Sun Nov 24, 2024 1:02 pm
May have something to do with memory mapping via the MMU. Both the application in flash and the PSRAM are memory-mapped. And NVS tries to also memory-map part of the NVS partition. Might be that the MMU runs out of pages after 8MB of flash + 8MB of PSRAM are mapped in.
The hardware can handle that situation fine, though... iirc the S3 has 32M of address space, so 8+8M should fit fine in there.

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

Re: NVS access issue when app size >8MB and PSRAM enabled

Postby ESP_rrtandler » Mon Dec 09, 2024 12:39 pm

Hi VinayDand,

Could you please extend your code with call to the

Code: Select all

spi_flash_mmap_dump();
Before and after the call to the nvs init. And call it once in case when NVS works fine as well once in the case when it doesn't initialise correctly and post the dumped (logged) information ?

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

Re: NVS access issue when app size >8MB and PSRAM enabled

Postby ESP_rrtandler » Fri Dec 13, 2024 10:52 am

Hi VinayDand,

I tried to reproduce the issue on the ESP32S3 Devkit with 32M Flash and 8M PSRAM but in my case the issue didn't show up.
In order to compile and flash it correctly, I had to adjust some menuconfig settings from your original example. The ones I have touched were:

CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
CONFIG_SPIRAM=y
CONFIG_SPIRAM_MODE_OCT=y
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
CONFIG_ESPTOOLPY_FLASHSIZE="16MB"

Please note that I have used 16MB as flash size for ESPTOOLPY to make the situation as close to your case as possible. I have also tried the settings true for my devkit (32MB) without any impact. See the fragment of monitor log below:


I (2184) esp_psram: Found 8MB PSRAM device
I (2188) esp_psram: Speed: 40MHz
I (2191) cpu_start: Multicore app
I (2925) esp_psram: SPI SRAM memory test OK
....
I (3057) main_task: Calling app_main()
I (3087) MyLog: Array for key cntData retrieved successfully!

I (3097) MyLog: Cnt value: 3
I (3097) main_task: Returned from app_main()

VinayDand
Posts: 5
Joined: Fri Jun 24, 2022 6:47 am

Re: NVS access issue when app size >8MB and PSRAM enabled

Postby VinayDand » Fri Dec 20, 2024 8:09 am

Thanks for the suggestions and verifying our code at your end (@ESP_rrtandler).

We finally nailed down the issue at our end.
We were using ESP-IDF version 5.0.1
After upgrading to version ESP-IDF 5.3.1 it works.
We get the NVS access correctly even with app > 14MB and PSRAM enabled

We will remember to mention ESP-IDF version in any future post also.

Best Regards,
Vinay

Who is online

Users browsing this forum: Google [Bot] and 75 guests