PSRAM Utilization on ESP32S3-EYE

Polarbear17123
Posts: 10
Joined: Thu Sep 12, 2024 7:55 am

PSRAM Utilization on ESP32S3-EYE

Postby Polarbear17123 » Sat Sep 21, 2024 8:41 am

Hello,
This may be a completely newbie question.

(1) I'm trying to use the 8M PSRAM as "normal" memory,
when I choose in IDF menuconfig to access the PSRAM via heap_caps_malloc -- I can see all 8M of it
but if I integrate into the memory map I can only see about 300K -- why is that and how can it be fixed?

(2) the reason that I want to integrate the PSRAM into the memory is because I want to have a large statically allocated DATA and BSS segments.

(3) a second problem that I have is that when I add a large BSS segments, the build complains of data segment overflow (dram0_0_seg)

In other worlds (and to avoid XY question, because I may be completely off direction) -- How can I run something like this code on the ESP32S-EYE with 8MB PSRAM?

Thank you!

Code: Select all

[code][Codebox=c file=minimal.c]
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"

char bss_seg[3*1024*1024];
char data_seg[2*1024*1024] = {0xA5};

int app_main(void) 
{
    printf("Minimal app main\n");

    printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());

    for (int i = 10; i >= 0; i--) {
        printf("Exit in %d seconds...\n", i);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    printf("Exit now.\n");
    fflush(stdout);
    
    // avoid optimzing data out
    return (bss_seg[1024] < data_seg[1024]) ;
}
[/Codebox]
[/code]

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

Re: PSRAM Utilization on ESP32S3-EYE

Postby MicroController » Sat Sep 21, 2024 1:48 pm

if I integrate into the memory map I can only see about 300K
How do you "see" that memory? -> https://docs.espressif.com/projects/esp ... nformation

Code: Select all

char bss_seg[3*1024*1024];
see Allow .bss Segment to Be Placed in External Memory

Code: Select all

char data_seg[2*1024*1024] = {0xA5};
If possible, initializing this kind of data at runtime via code may be more effective. Otherwise gcc may have to include 2MB of initialization data into the firmware binary (if it can't just fill it with a single value).

Polarbear17123
Posts: 10
Joined: Thu Sep 12, 2024 7:55 am

Re: PSRAM Utilization on ESP32S3-EYE

Postby Polarbear17123 » Sat Sep 21, 2024 7:41 pm

Thank you!

(1) I "see" the heap using esp_get_minimum_free_heap_size call. We can also see the log during boot (below)
it doesn't seem to add the PSRAM into available heap.

(2) About " including 2MB of initialization data into the firmware binary", yes - this is my intent. I eventually would like to run AI models that have a large data segment holding the model parameters.

*** Using PSRAM via heap_caps_malloc ***
I (830) heap_init: Initializing. RAM available for dynamic allocation:
I (837) heap_init: At 3FC96790 len 00052F80 (331 KiB): RAM
I (843) heap_init: At 3FCE9710 len 00005724 (21 KiB): RAM
I (849) heap_init: At 600FE100 len 00001EE8 (7 KiB): RTCRAM
I (856) esp_psram: Adding pool of 7488K of PSRAM memory to heap allocator
I (863) spi_flash: detected chip: gd
I (867) spi_flash: flash io: dio
I (871) sleep: Configure to isolate all GPIO pins in sleep state
I (878) sleep: Enable automatic switching of GPIO sleep configuration
I (885) main_task: Started on CPU0
I (895) main_task: Calling app_main()
Clear minimal app main
Minimum free heap size: 8018452 bytes


*** Integrating PSRAM into memory map ***
I (830) heap_init: Initializing. RAM available for dynamic allocation:
I (837) heap_init: At 3FC96790 len 00052F80 (331 KiB): RAM
I (843) heap_init: At 3FCE9710 len 00005724 (21 KiB): RAM
I (849) heap_init: At 600FE100 len 00001EE8 (7 KiB): RTCRAM
I (856) spi_flash: detected chip: gd
I (860) spi_flash: flash io: dio
I (864) sleep: Configure to isolate all GPIO pins in sleep state
I (870) sleep: Enable automatic switching of GPIO sleep configuration
I (878) main_task: Started on CPU0
I (888) main_task: Calling app_main()
Clear minimal app main
Minimum free heap size: 353080 bytes

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

Re: PSRAM Utilization on ESP32S3-EYE

Postby ESP_Sprite » Sun Sep 22, 2024 6:36 am

Polarbear17123 wrote:
Sat Sep 21, 2024 7:41 pm

(2) About " including 2MB of initialization data into the firmware binary", yes - this is my intent. I eventually would like to run AI models that have a large data segment holding the model parameters.
I imagine that data won't change at runtime? If so, suggest making the array 'const' - that way, it stays in flash rather than be copied to psram.

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

Re: PSRAM Utilization on ESP32S3-EYE

Postby MicroController » Sun Sep 22, 2024 8:15 am

Polarbear17123 wrote:
Sat Sep 21, 2024 8:41 am
(1) I'm trying to use the 8M PSRAM as "normal" memory,
when I choose in IDF menuconfig to access the PSRAM via heap_caps_malloc -- I can see all 8M of it
but if I integrate into the memory map I can only see about 300K -- why is that and how can it be fixed?
See https://docs.espressif.com/projects/esp ... ternal-ram

"Integrating into the memory map" means the IDF just makes the PSRAM accessible as RAM to the CPU.
You can use it as "raw" memory by reading or writing data to its address range via pointers.
The heap allocator is not made aware of this memory region.

To make the allocator aware of the PSRAM you should set it to be made available via heap_caps_malloc(). Then you can allocate memory from it using heap_caps_malloc(MALLOC_CAP_SPIRAM) (and free() it when not needed any more).

Polarbear17123
Posts: 10
Joined: Thu Sep 12, 2024 7:55 am

Re: PSRAM Utilization on ESP32S3-EYE

Postby Polarbear17123 » Sun Sep 22, 2024 6:29 pm

I imagine that data won't change at runtime? If so, suggest making the array 'const' - that way, it stays in flash rather than be copied to psram.


Yes. this is correct and it WORKS!
And assuming psram read is faster than flash read, I can copy it to psram after program starts.

Thank you!

Polarbear17123
Posts: 10
Joined: Thu Sep 12, 2024 7:55 am

Re: PSRAM Utilization on ESP32S3-EYE

Postby Polarbear17123 » Sun Sep 22, 2024 6:35 pm


"Integrating into the memory map" means the IDF just makes the PSRAM accessible as RAM to the CPU.
You can use it as "raw" memory by reading or writing data to its address range via pointers.
The heap allocator is not made aware of this memory region


Do you mean it makes it accessible at the HW (or page/segment table) level?
It does not seem to be accessible to the loader, which attempts to use dram only (and fails due to size)
if it is correct it can explain the problem I had before I made the data const.

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

Re: PSRAM Utilization on ESP32S3-EYE

Postby MicroController » Mon Sep 23, 2024 8:15 am

Polarbear17123 wrote:
Sun Sep 22, 2024 6:35 pm
Do you mean it makes it accessible at the HW (or page/segment table) level?
Yes, it only configures the MMU.
It does not seem to be accessible to the loader, which attempts to use dram only (and fails due to size)
if it is correct it can explain the problem I had before I made the data const.
Hmm. From the docs I think it should be possible:
If enabled, the region of the data virtual address space where the PSRAM is mapped to will be used to store zero-initialized data
Have you tried combining "integrate into memory map" with ".bss in PSRAM"?

Polarbear17123
Posts: 10
Joined: Thu Sep 12, 2024 7:55 am

Re: PSRAM Utilization on ESP32S3-EYE

Postby Polarbear17123 » Mon Sep 23, 2024 6:28 pm

>> Have you tried combining "integrate into memory map" with ".bss in PSRAM"?
Yes, but this for zero initialized data (.bss is created by the loader and set to 0), and my data is non-zero initialized (.data) that is part of the binary image.
I looked for option to move the .data into PSRAM, but I couldn't find any in idf.py menuconfig
and I didn't see an option in the document as well. Perhaps .data is meant to remain as const in the flash.
(reading speed is similar to PSRAM, though the FLASH has longer access delay - but this can be mitigated by the OS (and CPU) cache.)

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

Re: PSRAM Utilization on ESP32S3-EYE

Postby MicroController » Tue Sep 24, 2024 6:39 am

PSRAM transfers should be about 2x as fast as flash, if you enable octal SPI for the PSRAM.

If you want to leverage the PSRAM's speed for read-only (const) data, you can have all read-only data copied to PSRAM upon boot, or just memcpy the data you want there yourself.

Who is online

Users browsing this forum: No registered users and 211 guests