Page 1 of 1

ESP-IDF : Is it possible to detect if SPI_RAM is available ?

Posted: Wed May 17, 2023 7:33 am
by ThomasESP32
Good morning,

I have written a program using an Esp32S3-WROOM-1 module having an external SPI_RAM (2Mo).
In order to use the external SPI_RAM, I enabled it in the firmware and I used the
heap_caps_malloc method with the MALLOC_CAP_SPIRAM parameter.

Here, everyting is ok.

The problem is that I have to change my Esp32S3 component and I need to use an Esp32S3Mini which have no external SPI_RAM.
So, I have disabled the use of external SPI_RAM in the sdkconfig file but I get an error when I
execute the heap_caps_malloc method with the MALLOC_CAP_SPIRAM parameter. => The program crashes.

Is there a way to detect if SPI_RAM is present and enabled in the firmware so that I can execute the
heap_caps_malloc method with the MALLOC_CAP_SPIRAM parameter without any error ?

Or is there a way to execute the method heap_caps_malloc so that it tries to allocate memory from SPI_RAM if it is present and enabled
or from internal DRAM in the other case ?

Thank you for your help.

Best regards,

Thomas TRUILHE

Re: ESP-IDF : Is it possible to detect if SPI_RAM is available ?

Posted: Wed May 17, 2023 7:52 am
by ESP_Sprite
You could use heap_caps_get_free_size(MALLOC_CAP_SPIRAM). If there's (free) PSRAM memory available, this will return non-zero.

Re: ESP-IDF : Is it possible to detect if SPI_RAM is available ?

Posted: Wed May 17, 2023 7:52 am
by ThomasESP32
The best thing would be that the heap_caps_malloc method tries to allocate from SPI_RAM in priority if it is enabled and present and from DRAM in the other cases.
Moreover, if SPI_RAM is not enabled or not present, thenmethods should not crash.

Do you no if a OR : MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT or something like that would be ok ?

Best regards,

Re: ESP-IDF : Is it possible to detect if SPI_RAM is available ?

Posted: Wed May 17, 2023 8:05 am
by MicroController
heap_caps_malloc_prefer(size, 1, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);

Alternatively,

Code: Select all

#if CONFIG_SPIRAM
    #define CAP MALLOC_CAP_SPIRAM
#else
    #define CAP MALLOC_CAP_INTERNAL
#endif
...
ptr = heap_caps_malloc(..., CAP);
or

Code: Select all

const uint32_t cap = esp_psram_is_initialized() ? MALLOC_CAP_SPIRAM : MALLOC_CAP_INTERNAL;