Skip to main content
PMath.4
Senior III
Jan 30, 2021 8:57 AM UTC
Solved

STM32H743: Moving the heap into SDRAM?

Hi

I need a big heap for some json parsing. How do I re-locate the heap into SDRAM? Obviously this needs to happen fairly late in the initialisation sequence once the SDRAM has been set up and initialised.

Is there an equivalent of __set_MSP() ?

Thanks

This topic has been closed for replies.
KnarfB
KnarfBBest answer
Super User
Jan 30, 2021 11:52 AM UTC

For gcc+newlib: Adapt the implementation of void *_sbrk(ptrdiff_t incr) in sysmem.c. _sbrk is called twice for the first malloc. The first call with param incr==0 returns the heap bottom address. The second call tells _sbrk the additional space needed for the new allocation and _sbrk return the (current) top of the heap (or -1 on error).

_sbrk returns increasing pointers at each call, and, in contrast to the malloc/free pair never gives back memory or shrinks the heap space.

It should be not too difficult to insert your own bottom and top limits here instead of using the linker script variables.

hth

KnarfB

4 replies

waclawek.jan
Super User
Jan 30, 2021 11:47 AM UTC

Heap is a purely programming construct, it has absolutely nothing to do with the processor itself.

Read documentation to your toolchain if you desire to use the standard malloc/free, or simply write them yourself.

JW

KnarfB
KnarfBBest answer
Super User
Jan 30, 2021 11:52 AM UTC

For gcc+newlib: Adapt the implementation of void *_sbrk(ptrdiff_t incr) in sysmem.c. _sbrk is called twice for the first malloc. The first call with param incr==0 returns the heap bottom address. The second call tells _sbrk the additional space needed for the new allocation and _sbrk return the (current) top of the heap (or -1 on error).

_sbrk returns increasing pointers at each call, and, in contrast to the malloc/free pair never gives back memory or shrinks the heap space.

It should be not too difficult to insert your own bottom and top limits here instead of using the linker script variables.

hth

KnarfB

Piranha
Principal III
Jan 30, 2021 12:28 PM UTC
PMath.4
PMath.4Author
Senior III
Jan 30, 2021 1:24 PM UTC

Thanks all. I've modified the _sbrk routine which on STM32CubeIDE and the associated HAL libraries is in syscalls.c