“
Keywords: STM32CubeIDE, Heap, __sbrk
Problem Description
Recently, a client requested to specify the heap address in STM32CubeIDE to a dedicated RAM.
Problem Analysis
The default stack configuration diagram in the project generated by STM32CubeIDE is shown in Figure 1:

Solution
The project generated by STM32CubeIDE will automatically create a file named sysmem.c. In this file, the
__sbrk_heap_end pointer is initially set to NULL, and we only need to change this pointer to the target address to achieve the desired result.
For example: The default configuration is “static uint8_t *__sbrk_heap_end = NULL;” If we want to allocate the heap starting at address 0x20001000, we can do it like this:
“static uint8_t *__sbrk_heap_end = (uint8_t*)0x20001000;”
Note: Since the heap grows upwards and the stack grows downwards, when manually changing the starting address of the heap, if the defined heap and stack spaces overlap, STM32CubeIDE cannot recognize this situation, and users should avoid such settings.
Verification
In the project generated by STM32CubeIDE for serial printing, modify the __sbrk_heap_end parameter in sysmem.c according to the example above.


We can see that the heap_pointer pointer has obtained an address through malloc, which is already pointing within the target RAM range.
Note that we applied for 10 bytes of space starting from the position 0x20001000, theoretically heap_pointer should start from 0x20001000, but since malloc occupies 8 bytes to store status and other information when requesting an address, the heap_pointer pointer starts from 0x20001008. This is due to STM32CubeIDE using the newlib library to implement malloc, which does not affect the user’s usage.
© THE END
▽Click “Read Original” to download the original document