STM32F0 - question about memory (Stack, heap)

120 views Asked by At

I am bit confused about the stack and heap memory of STM32 microcontrolers (cortex M0).

Firstly, they are part of RAM, but are they part of RAM size described in the datasheet ?

Secondly, if I reduce heap memory (I don't use any malloc) by telling it to the linker, could I increase the RAM memory to store more global and static local variables (which are stored in the data section of RAM memory)?

Finally,I saw that somewhere that stack memory was for local variables but I saw then elsewhere that it was for static data.. what's the truth ?

Extra question: what are the reserved spaces shown into the memory map figure of STM32 datasheets?

Could you please bring light to it?

Thank you in advance,

2

There are 2 answers

0
wek On

RAM is a memory, it's a physical thing.

Stack and heap are not part or RAM; they are abstract constructs, similar to variable, array, object and such. These are located into the RAM by the toolchain (IDE, but ultimately by the linker).

Stack, generally, is usually supported directly by hardware (there's one or several stack pointer register(s) and instructions which automatically make use of it) and required for certain functionality, e.g. returning from called functions. Compilers also can and do use stack to store local variables and automatic variables (register spills, i.e. values needed for calculations which don't fit into registers and need to be temporarily stored somewhere).

Static variables are stored together with global variables elsewhere, not in stack.

Stack in ARM Cortex-M processor grows downwards, so stack pointer is usually initialized to top of stack; static/global variables are then located by linker from bottom of stack. The compiler and linker usually don't calculate, how much stack will be used runtime (sometime they do but only as advanced analytics, and with caveats) as in some cases it's very hard or even impossible to calculate it (e.g. when using function pointers). It's assumed, that you know how big the stack grows (given complexity of your program and usage of local variables). A common yet imperfect method to estimate the stack usage is to fill RAM with known pattern (usually a recognizable "magic number" such as 0xDEADBEEF), attempt to exercise all possible execution patterns of the program, and then observe, how much of that pattern has been "destroyed" by the stack.

The value you assign as stack size in some toolchains is only a "tentative" value, used by linker to warn you that the sum of global+static variables plus this number gets above the total RAM available.

Heap is an entirely software construct. If you don't use it, set it to 0 and just forget about it. Yes if it's nonzero it's just added to that sum above, so setting it to 0 will "free up" memory (in that linker won't complain or refuse to generate output).

Extra answer, reserved portions of address space are "no touch", that's all you need to know (and there's nothing else, truly, it's unallocated space, accessing which usually results in hardfault).

0
0___________ On

I am bit confused about the stack and heap memory of STM32 microcontrolers (cortex M0).

Stack and the heap are just abstract terms related to the C implementation. Of course, the hardware stack pointer is present in this uC, but I suggest not focusing on implementation details (judging from the answer you are a very beginner).

What you need to remember is to do not use dynamic allocation (malloc family functions) when programming uCs.

Firstly, they are part of RAM, but are they part of RAM size described in the datasheet ?

Datasheet does not describe an implementation of the C compiler, so you will not find this information there.

Normally the memory map used by your program is deifned in the linker script. Typical implementation places objects in:

  • automatic storage duration objects - on the stack
  • const static storage duration objects - in the .rodata segment
  • initialized static storage duration objects - in the .data segment
  • not initialized static storage duration objects - in the .bss segment
  • the code - in the .text segment