How do I use psram in esp32-s2-wrover?

Hi all! Re-soldered the “ESP32-S2-Saola-1” board and replaced the “S2 wroom” on it with “S2 wrover”. I’ve encountered a problem that changing the “platformio.ini” file doesn’t allow to use 2 mb psram when compiling the project. The compiler sees only 320kb of internal psram (log: “Advanced Memory Usage is available via “PlatformIO Home > Project Inspect”
RAM: [==== ] 41.0% (used 134396 bytes from 327680 bytes)”).

Stack: Visual Studio Code + Platformio;
Framework: Arduino;
Core/module: esp32s2-wrover with external 2mb psram.

The code of my “platformio.ini”:

[env:esp32-s2-saola-1]
platform = espressif32
build_flags =
    -DBOARD_HAS_PSRAM
    -mfix-esp32-psram-cache-issue
    -DCONFIG_MBEDTLS_DYNAMIC_BUFFER=1
    -DCONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST=1
    -DCONFIG_SPIRAM_CACHE_WORKAROUND=1

board = esp32-s2-saola-1
board_build.mcu = esp32s2
framework = arduino

monitor_speed = 921600
monitor_port = COM[3]
upload_port = COM[3]

Can you please tell me what I should do so that I can use the external PSRAM?

These values are hardcoded in platform-espressif32/esp32-s2-saola-1.json at develop · platformio/platform-espressif32 · GitHub

You can override them with

[env:esp32-s2-saola-1]
board_upload.maximum_ram_size = xxxx
1 Like

Ivan, thanks for your tip! It partially solved my problem, now the compiler displays the following when starting the build:
“PLATFORM: Espressif 32 (4.2.0) > Esp32-s2-saola-1
HARDWARE: ESP32S2 240MHz, 2.31MB RAM, 4MB Flash
…”. 2.31MB of RAM, cool! But…

But now I have another problem. Although during the build the information about the amount of “available” memory is displayed, in fact there is a build error related to RAM memory overflow:

"…
Scanning dependencies…
Dependency Graph
|-- LovyanGFX @ 0.4.17
|-- SPI @ 2.0.0
|-- wire @ 2.0.0
|-- lvgl @ 8.2.0
Building in release mode
Compiling .pio\build\esp32-s2-saola-1\src\main.cpp.o
Linking .pio\build\esp32-s2-saola-1\firmware.elf
[project way]: .pio/build/esp32-s2-saola-1/firmware.elf section .dram0.bss' will not fit in region dram0_0_seg’
[project way]: DRAM segment data does not fit.
[project way]: region `dram0_0_seg’ overflowed by 93120 bytes
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\esp32-s2-saola-1\firmware.elf] Error 1
".

I understand that only “implicitly” indicated the amount of available memory, but at the stage of compiling the project it is still unavailable. How can this be fixed?

The documentation states that yo ucan mark variables you want to place in PSRAM with EXT_RAM_BSS_ATTR per documentation, if the option CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY. is turned on. However, as you can see in

that is actually turned off.

This means that you have to convert the buffers and variables you want to be placed in PSRAM with a simple pointer and use the PSRAM malloc allocator heap_caps_malloc(size, MALLOC_CAP_SPIRAM) if you want to be 100% sure they are allocated in PSRAM. Otherwise, you can also use the regular malloc() option, since it will use PSRAM if needed, since the option CONFIG_SPIRAM_USE_MALLOC is turned on.

Example. Previous:

uint8_t my_buffer[512 * 1024]; // fills internal RAM by 0.5MByte

After:

uint8_t* my_buffer; // pointer to 'some' memory

void init_buffer() {
   // definitely allocate buffer in PSRAM
   // capabilities: 8/16/…-bit data accesses possible and in SPIRAM.
   my_buffer = heap_caps_malloc(512*1024, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);

   // try allocate buffer anywhere where's space, internal or PSRAM
   my_buffer = malloc(512*1024);
}

void deinit_buffer() {
  // if allocated with heaps_caps_malloc
  heap_caps_free(my_buffer);
  
  // if allocated with malloc
  free(my_buffer);

  // good practice
  my_buffer = nullptr;
}
3 Likes