ESP32: 8 MB PSRAM himem?

To access all of the ESP32’s PSRAM in newer WROVER modules with 8 MB, some special code is needed to bank-switch things around a bit, since at most 4 MB can be mapped into the ESP’s address space at any time.

It looks like Espressif has this covered with “himem” support, see: https://github.com/espressif/esp-idf/tree/master/examples/system/himem. In PlatformIO, there’s a header file called esp_himem.h which can be included.

Is himem/bank-switching supported with the current release of PlatformIO? I’m getting error messages of the form “E (131) esp_himem: Cannot allocate memory for meta info. Not initializing!”.

PS. More info on hiram is in this documentation page.

PPS. To follow up, this code:

printf("PsramSize %u\n", ESP.getPsramSize());
printf("spiram size %u\n", esp_spiram_get_size());
printf("himem free %u\n", esp_himem_get_free_size());
printf("himem phys %u\n", esp_himem_get_phys_size());
printf("himem reserved %u\n", esp_himem_reserved_area_size());

Generates this output:

PsramSize 3932108
spiram size 8388608
himem free 0
himem phys 4456448
himem reserved 262144

The one I don’t understand is “himem free”, i.e. esp_himem_get_free_size().

You’ve also modified your sdkconfig.h to include the modified parameters, yeah?

Ah, I forgot to mention that I’m using framework = arduino - does this apply?

From what I can tell, they are already enabled in ~/.platformio/packages/framework-arduinoespressif32//tools/sdk/include/config/sdkconfig.h

(I’m using latest PlatformIO, version 3.6.7 on MacOS)

Does the code work as expected within the latest Arduino IDE with the latest Arduino-ESP32 board version?

Ah, it’s been years since I last used the Arduino IDE. Updated. ESP32 installed from git. Build of test sketch works, but I get a failure running esptool (java.io.IOException: Cannot run program "/Users/jcw/Documents/Arduino/hardware/espressif/esp32/tools/esptool": error=13, Permission denied). I can’t seem to solve this quickly - even though this is off topic: could you give me a quick hint where the IDE places its compiled firmware image on MacOS? Then I can upload manually.

In the Arduino IDE settings, activate the verbose building checkmark and recompile, the final linker command should tell you where it placed the binary. You may also try to sudo chown -R jcw:jcw /Users/jcw/Documents/Arduino/*?

Thanks: chown didn’t fixi it, but yeah, forgot all about the verbose setting. Ok, looks like esptool isn’t being started up correctly - anyway, I can now upload.

No dice:

entry 0x400806a8
E (108) spiram: SPI RAM not initialized
E (108) esp_himem: Cannot allocate memory for meta info. Not initializing!
PsramSize 0
E (23) spiram: SPI RAM not initialized
spiram size 4294967295
himem free 0
E (23) spiram: SPI RAM not initialized
himem phys 4291035135
himem reserved 262144

My entire sketch is:

extern "C" {
#include <esp_spiram.h>
#include <esp_himem.h>
}

void setup() {
  printf("PsramSize %u\n", ESP.getPsramSize());
  printf("spiram size %u\n", esp_spiram_get_size());
  printf("himem free %u\n", esp_himem_get_free_size());
  printf("himem phys %u\n", esp_himem_get_phys_size());
  printf("himem reserved %u\n", esp_himem_reserved_area_size());
}

void loop() {}

Do I need to enable flags somewhere? I’ll dig a bit.

As for the esptool problem, off-topic and FWIW: the IDE is launching what looks like the directory of the tool, not the tool itself. I’m using github head.

Progress, removing ESP.getPsramSize() and adding esp_spiram_init():

entry 0x400806a8
E (121) spiram: SPI RAM not initialized
E (121) esp_himem: Cannot allocate memory for meta info. Not initializing!
spiram size 8388608
himem free 0
himem phys 4456448
himem reserved 262144

But as far as I can tell, it’s now the same as in platformio.

My wild guess is that the himem init code is running at boot time, before spiram has been initialised.

If it’s not able to run properly in the Arduino-IDE distributation of Arduino-ESP32, I think it’s best to open an issue at Issues · espressif/arduino-esp32 · GitHub to let the developers help you, instead of us figuring it out independently and taking longer, and then carrying the fix over to them.

1 Like

Ok, many thanks for your quick help - submitted: https://github.com/espressif/arduino-esp32/issues/2830.

Also submitted here - Banked himem (8 MB psram) support · Issue #185 · platformio/platform-espressif32 · GitHub - since the problem seems to be more involved.

1 Like