AnsweredAssumed Answered

manually write to stack

Question asked by Dan Puccio on Oct 24, 2016
Latest reply on Oct 26, 2016 by Dan Puccio

I'm working on a module to monitor the stack usage in my KE06 project (in KDS).  The concept is:


To detect the peak stack usage, the initialization function writes 0x00 to each unused memory location in the stack.  It writes from the bottom of the stack up to the stack pointer.  Then when the stack monitor function is called, it goes through the stack and it counts the locations still with 0x00 (presumably these were unused).  It calculates the percentage of the stack used and saves this in a variable for “live” viewing via CAN.  This same strategy is used (and works well) on other projects.  I’m getting a hard fault here during the initialization, however.


In the debugger, the stack pointer resolves correctly.  I get one single write to the bottom of the stack, and on the second write it hard faults.  Any thoughts as to why?


#define STACK_START_ADDR              0x20002C00  // MCU stack starting address

#define STACK_TOP_ADDR                0x20003000  // MCU stack top address (see Memory mapping)

#define STACK_ERASED_VALUE            0x00        // Value used to identify erase stack memory


void STACK_Initialize( void )


    uint32 u32Addr;

      register uint32 sp asm ("sp");


    for ( u32Addr=STACK_START_ADDR; u32Addr < sp; u32Addr++ )  // Fill unused stack with erased values


        *(uint32*)u32Addr = STACK_ERASED_VALUE;   //<----hard faults here the the second time through the loop