Page 1 of 1

How to use PSRAM for static definitions and malloc()?

Posted: Sat Dec 21, 2019 3:02 am
by zliudr
If I wish to place a static definition such as a string in PSRAM, what should I do? This doc vaguely mentions EXT_RAM_ATTR but is that the correct keyword? They mention "additional data" and BSS so I want to be clear.

Also, how do I malloc from external PSRAM? I know there is a threshold but I wish to allocate only a couple hundred bytes and don't wish to change the threshold. Is there a special malloc() function that only allocates PSRAM? Thank you! My next question will be how to switch banks but that requires me to know how to allocate first ;)

Re: How to use PSRAM for static definitions and malloc()?

Posted: Sat Dec 21, 2019 4:54 am
by shabtronic
Don't know about the static allocation - but spiram malloc is done with

heap_caps_malloc(size, MALLOC_CAP_SPIRAM)
https://docs.espressif.com/projects/es ... l-ram.html

all depends how you've menuconfig'd it!

Re: How to use PSRAM for static definitions and malloc()?

Posted: Sat Dec 21, 2019 8:02 am
by zliudr
Thanks! That function worked! On the other hand, EXT_RAM_ATTR causes panic. If I add it to this def, it causes panic.

static const char *request="adfadfa";

How do I use this attribute word properly? Thanks.

Re: How to use PSRAM for static definitions and malloc()?

Posted: Sat Dec 21, 2019 1:54 pm
by shabtronic
I think you can only use the EXT_RAM_ATTR on zero'd var's. I think the reason is - the system would have to copy the init data to spiram - making it more complex under the hood as such.

Re: How to use PSRAM for static definitions and malloc()?

Posted: Sun Dec 22, 2019 4:36 am
by zliudr
Thanks again! That makes a lot of sense. I'll keep all my zero-filled arrays there to save regular memory space.