How to verify safe stack usage on KL17?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to verify safe stack usage on KL17?

1,154 Views
rickstuart
Contributor V

We are about mid point in our development and would like to test if our stack usage is safe (not close to overflowing) on our KL17 processor?  What tools are available to a developer to make this assessment?  We are using the IAR Workbench IDE.

-thanks

0 Kudos
Reply
3 Replies

1,025 Views
bobpaddock
Senior Contributor III

Moving the stack from the top of RAM to near the bottom is an often over looked method.

When it overflows it will generate an exception rather than silently corrupting things.

Explained here:  Are We Shooting Ourselves in the Foot with Stack Overflow? « State Space 

How to read the stack pointer:

In Generic C, a good tools set should complain about returning the address of a local variable:

/*
* void *CheckStackDepth( void )
* {
* volatile uint32_t dummy; // Put a variable on the stack
* return( (void *) &dummy ); // Return its address - therefore the (approx.) present SP value
* }
*/

In GCC:


static __inline__ void *sp_get(void)
{
void *sp;

__asm__ __volatile__ ("mrs %0, msp" : "=r"(sp));

return( sp );
}

I call sp_get() in IRQs then test and save to a global variable (which in general should be avoided) that will show the stack depth.  Something different would need done if have nested IRQs.


0 Kudos
Reply

1,025 Views
rastislav_pavlanin
NXP Employee
NXP Employee

Hello Rick,

I have used this approach to analyses the stack in my applications:

IAR Stack usage analysis for RX 

and get it really usefull.

regards

R.

0 Kudos
Reply

1,025 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Rick,

I think we have not the tools to check if the stack is safe. But I think you can write the stack with a pattern, then running the code and check if pattern has been changed for the out of  range of stack memory.

Hope it can help you.

BR

Xiangjun Rong