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

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

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

Postby zliudr » Sat Dec 21, 2019 3:02 am

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 ;)

User avatar
shabtronic
Posts: 49
Joined: Sun Nov 03, 2019 1:33 pm

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

Postby shabtronic » Sat Dec 21, 2019 4:54 am

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!

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

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

Postby zliudr » Sat Dec 21, 2019 8:02 am

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.

User avatar
shabtronic
Posts: 49
Joined: Sun Nov 03, 2019 1:33 pm

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

Postby shabtronic » Sat Dec 21, 2019 1:54 pm

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.

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

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

Postby zliudr » Sun Dec 22, 2019 4:36 am

Thanks again! That makes a lot of sense. I'll keep all my zero-filled arrays there to save regular memory space.

Who is online

Users browsing this forum: Baidu [Spider], martins and 93 guests