cancel
Showing results for 
Search instead for 
Did you mean: 

How to fill ram with pattern in loading and before runtime?

Mrahm.2
Associate II

Hello guys,

I am using the stm32f446re discovery board, my aim is to measure the ram consumption of my application. The first approach is to use the static stack analyzer checking the memory details, the second approach is to fill the memory with a pattern and then check the memory.

the first approach is easy but is not useful for recursive functions, so I go for the second approach.

I need to fill the memory, I realized filling the memory in runtime is not the thing that I want, so I need to fill memory before runtime, I mean in load time also I realized the linker script can do it by "FILL" command but I didn't manage to find how to use.

My question is how to fill memory with a pattern like "0Xdeaddead" before runtime?

4 REPLIES 4

I would do it in the startup code, toolchain dependent.

JW​

Mrahm.2
Associate II

do you know how ?

Paul1
Lead

Its RAM, you can't fill it before runtime, it has to be filled by running code.

(Unless you are talking about doing it with a diagnostic tool like IDE+STLINK, and then resetting the code and running it in the debugger, but wait, that is still using code to load the RAM).

Recommend:

a) in beginning of main() put a few for(;;) loops to load the RAM areas you wish. Careful you do only real RAM or you can trigger memory faults.

b) option: do it in the startup.s assembly file (Yea! assembly code. Have a look at the assembly code used to init the zero'd RAM and Initialized to value RAM sections)

  • Core\Startup\startup*.s
  • CopyDataInit, LoopCopyDataInit, FillZerobss, LoopFillZerobss, etc.

c) If you truly need before runtime:

  • run code, trigger code to go to your own diag mode that fills RAM then goes to an endless for(;;);, then reset your device WITHOUT dropping power so the RAM stays powered.

Question: Why need this before runtime? Very little happens before main() so much easier to do it at beginning of main(). Have a look in Startup.s to see how little happens before main().

Also: I recommend filling RAM with a pattern that is easy to see changes to, such as 0xcccccccc (all short so easier to see any tall (bdf0123456789, and fairly easy to see a or e), if memory can be displayed in lower case.

Also: In runtime have your code so a a periodic scan of stack and heap starting at unused end and stopping at first non-match, then display those will stow unused stack/heap in your real runtime code without manual scanning.

Paul

Nikita91
Lead II

The generation script gives you the memory sizes used in static. Then there are several things to consider:

  • stacks
  • The heap (used by malloc)
  • Any RAM sections placed at specific addresses.

It is possible that overall the memory is not used in a contiguous way, and that it is necessary to analyze each element separately.

For the stacks allocated in static: easy it is already taken into account during the generation. To see how much stack is used, just initialize them before creating the tasks.

For dynamically allocated stacks this amounts to measuring the heap.

For the heap it depends on its management. Maybe instrumentalize sbrk().

If you really want to initialize the memory before main(), do it in the startup code. It is probably in assembler if you use the code generated by ST. But you can call a C function that initializes memory just before calling main(). Be careful not to touch the main stack.