Question

How to determine stack memory needed for a particular programm in a microcontroller? For example lets say i have a programm which internally may have many subroutines or threads.Before i start executing program I want to fix the stack size for this program.How do i mark end point for stack.

Was it helpful?

Solution

I assume you are talking about a C language project, with no dynamic allocation of memory. An exact calculation of the stack usage is very long and complicated. You need to know at least the stack usage for each function and the call-graph (the compiler can provide this information). Furthermore: is there an operating system? Each thread needs its own stack. Are there interrupt routines? The total stack usage is the sum of the contribution of each cuncurrent execution flow.

In many cases an empirical method is more effective, i.e. a run time measure of the usage of the stack. For example, you can fill the whole stack area with fixed pattern, then run a significative test, and then check the size of the "dirty" area. The actual size can be calculated by applying a safety margin, for example +50%.

OTHER TIPS

The limitation on stack comes from amount of memory your micro-controller has. You can do it manually which is very tiresome for large program or can looking into elf dump . There you will find how memory is occupied by your global variables then remaining can be used as stack. Also to find out the maximum stack required you will need to look the both longest nested function call and the size of their arguments. In this way you can get an estimate of maximum stack requirement which you are set beforehand. In worst case of you need more stack then need based on you function calls and size of their argument, then you will need to do some optimization.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top