Skip to main content
JBond.1
Senior
February 5, 2020
Question

Does new project use all the available RAM?

  • February 5, 2020
  • 6 replies
  • 2121 views

I have created new project with STM32CubeMX for my STM32F091RC. I have noticed that when allocating memory it ends very fast. Do I need to change any settings to use all of the available 32Kb of memory?

In "startup_stm32f091xc.s" I find these values:

Stack_Size      EQU    0x400

Heap_Size     EQU    0x200

Should they be much bigger? Why default project does not use max available memory? What maximum values I can use?

This topic has been closed for replies.

6 replies

waclawek.jan
Super User
February 5, 2020

Stack and heap holds only part of the variables - global and static variables are located in the remaining space.

JW

JBond.1
JBond.1Author
Senior
February 6, 2020

32Kb = 0x7FFF, stack and heap gets only 0x400, 0x200 so ALL the rest 0x79FF is for global and static variables?

Does it really use "ALL" the remaining space?

Pavel A.
Super User
February 5, 2020

Yep, 32K is not much. Be frugal, watch your memory map.

-- pa

Tesla DeLorean
Guru
February 5, 2020

>>Should they be much bigger? 

Do you use malloc(), how much memory do you actually need to dynamically allocate. A lot of embedded systems use very little or no dynamically allocated memory.

Again with the stack, what do you actually use? Have you looked at the call-tree and expectations output by Keil.

Memory in general, look at the .MAP file, get an idea about what your needs/requirements actually are.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
JBond.1
JBond.1Author
Senior
February 6, 2020

Yes, I tried using malloc() for linked list and the app crashed really fast I added like ~100 integers to the list, and there were no more space. I would like to read files from SD card, so I guess I would need more than that.

Also when I tried using default FatFS library v0.11 from STM32CubeMX I had hard fault error. it seems that stack is also too small. I found another FatFS library: https://github.com/SL-RU/stm32_file_viewer/ which seems to work without changing stack size. So I guess it use some kind of optimization or static variables. Which FatFS library would you recommend?

But still I need more memory for reading files and so on. I guess I can use static variables. .MAP file should say if I have enough space for static variables?

I guess the key here is to use STATIC variables as they get all the 0x79FF space?

Tesla DeLorean
Guru
February 6, 2020

The stack and heap should be set based on your usage expectations.

The Linker assigns the open space to your other variables as needed. If you run out of space it will warn you.

Hard Faults can occur for several reasons, one needs to inspect the faulting code to understand the reason.​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
February 6, 2020

Always make the stack size big as when you hit its limit, finding the root cause is time consuming. Note: If your leftover RAM is full, the linker will error. The reverse won't be,

Some linkers can crawl recursively to give you the max stack depth from all the vectors, so you could guess the max stack size with all possible interrupts running.

Unfortunately, once people use function pointers, this recurse analysis (some MAP file will show it) is less reliable.

I would make it for bare metal few kbytes if you declare arrays or big structures in functions (which reduces the need for heap and malloc() )

When using RTOS, that is a totally different story.

Piranha
Principal III
February 8, 2020

> 32Kb = 0x7FFF, stack and heap gets only 0x400, 0x200 so ALL the rest 0x79FF is for global and static variables?

The 0x7FFF and 0x79FF are adresses of the last bytes, but sizes are 0x8000 and 0x7A00 respectively.

S.Ma
Principal
February 9, 2020

Actually, even though there is no convenient way to do it, theoretically, for bare metal applications, all globals could be created as local variables in the stack in main(), which would be a different way to answer your question.