cancel
Showing results for 
Search instead for 
Did you mean: 

Which IAR SW module writes 0xCD to the stack?

johnspeth9
Associate II
Posted on December 02, 2008 at 12:42

Which IAR SW module writes 0xCD to the stack?

4 REPLIES 4
johnspeth9
Associate II
Posted on May 17, 2011 at 12:52

Hi folks-

I'm going to instrument my firmware to probe and report stack usage at user demand. It appears that IAR tools nicely place an 0xCD pattern in the entire stack for this purpose. I need to make sure that this pattern is always written regardless of whatever command line options or configuration I select.

Does anyone know what IAR SW module writes the 0xCD pattern to the stack?

I assumed it was cstartup.s but I can't find any reference to that file in my IDE project settings.

Does anyone know where the default cstartup.s module is located in IAR projects that don't name it in the project files?

Thanks, John Speth

johnspeth9
Associate II
Posted on May 17, 2011 at 12:52

Well, I'll answer my own question since I stumbled upon the answer:

First, it appears that the debug tool writes 0xCD to the stack because when my app is running standalone, I don't see any 0xCD's in the stack.

Second, here's my gift of code to the group:

void zeroStack(void)

{

u8 *p;

#pragma section=''CSTACK''

for(p = __section_begin(''CSTACK''); p < (u8 *)(&p); p++) *p = 'S';

}

void getStackUsage(int *pSize,int *pUsed)

{

u8 *pBegin;

u8 *pEnd;

u8 *p;

#pragma section=''CSTACK''

pBegin = __section_begin(''CSTACK'');

pEnd = __section_end(''CSTACK'');

for(p = pBegin; p < pEnd; p++)

{

if(*p != 'S') break;

}

*pSize = (int)(pEnd - pBegin);

*pUsed = (int)(pEnd - p);

}

Call zeroStack() as soon as possible in main(). I'm not 100% sure the zeroStack() for-loop is kosher (as in SP = &p) but it works!

Thanks, John Speth

jilisegiar
Associate II
Posted on May 17, 2011 at 12:52

Additional info from EWARM5.20 user guide:

''When your application is first loaded, and upon each reset, the memory for the stack area is filled with the dedicated byte value 0xCD before the application starts executing. Whenever execution stops, the stack memory is searched from the end of the stack until a byte with a value different from 0xCD is found, which is assumed to be how far the stack has been used''

johnspeth9
Associate II
Posted on May 17, 2011 at 12:52

Jilisegiar, you're right about the quote from the manual. However I learned by observation that the 0xCD pattern is only placed in the stack during debugging. When one runs the firmware without the debugger, the stack is not filled with 0xCD. Apparently the tool places the byte in the stack prior to startup. I was looking for an in-the-field diagnostic aid. I think my solution will work for that purpose.

JJS