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

ThomasESP32
Posts: 229
Joined: Thu Jul 14, 2022 5:15 am

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

Postby ThomasESP32 » Wed May 17, 2023 7:33 am

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

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

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

Postby ESP_Sprite » Wed May 17, 2023 7:52 am

You could use heap_caps_get_free_size(MALLOC_CAP_SPIRAM). If there's (free) PSRAM memory available, this will return non-zero.

ThomasESP32
Posts: 229
Joined: Thu Jul 14, 2022 5:15 am

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

Postby ThomasESP32 » Wed May 17, 2023 7:52 am

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,

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

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

Postby MicroController » Wed May 17, 2023 8:05 am

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;

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 88 guests