Skip to main content
Associate II
November 1, 2023
Solved

Dynamic memory allocation in PSRAM

  • November 1, 2023
  • 6 replies
  • 3863 views

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.

This topic has been closed for replies.
Best answer by mƎALLEm

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.

 

 

 

 

6 replies

Nikita91
Lead II
November 1, 2023

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

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

SSGAuthor
Associate II
November 2, 2023

Okay. I will have a look on that. Thanks

ST Technical Moderator
November 1, 2023

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 "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
November 1, 2023

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 "Best answer" on the reply which solved your issue or answered your question.
Tesla DeLorean
Guru
November 1, 2023

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

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..