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

VinayDand
Posts: 4
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 50 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: 1736
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: 9766
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.

Who is online

Users browsing this forum: Baidu [Spider], NoTan2 and 113 guests