cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic memory allocation in PSRAM

SSG
Associate II

Hi,

Currently I am using STM32L4R9 discovery board, which has 16-Mbit (2MB) PSRAM. I have been able to allocate static memory inside PSRAM by using following code and also by changing linker script file (.ld). I have attached my edited linker script with this post.

int __attribute__((section (".psram_data"))) psram_buffer[50000];

for(int i=0; i<50000; i++)
{
psram_memory[i] = i;
}

But, how can I dynamically allocate memory by using malloc() / calloc() inside PSRAM. If possible, please share some example code with me.

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello,

In your linker file try the following:

MEMORY
{
....

....
PSRAM_HEAP (xrw) : ORIGIN = <put the PSRAM start address>, LENGTH = <put the heap region size>
}

..

..

._user_heap :
{
. = ALIGN(8);
PROVIDE ( s_heap = . );
. = . + Heap_Size;
. = ALIGN(8);
PROVIDE ( e_heap = . );
} >PSRAM_HEAP

 

B.R.

 

 

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

6 REPLIES 6
Nikita91
Lead II

Reserve a block of memory in PSRAM, then allocate subblocks.

You can take inspiration from one of the allocators present in FreeRTOS for example.

FBL
ST Employee

Hello @SSG 

 

You can define a custom memory region in PSRAM as the heap using the linker script. Here is an example project to do this. 

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

SofLit
ST Employee

Hello,

In your linker file try the following:

MEMORY
{
....

....
PSRAM_HEAP (xrw) : ORIGIN = <put the PSRAM start address>, LENGTH = <put the heap region size>
}

..

..

._user_heap :
{
. = ALIGN(8);
PROVIDE ( s_heap = . );
. = . + Heap_Size;
. = ALIGN(8);
PROVIDE ( e_heap = . );
} >PSRAM_HEAP

 

B.R.

 

 

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

That might work if not for the horrific _sbrk implementation you have..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SSG
Associate II

Thanks. I will try this.

Okay. I will have a look on that. Thanks