MSP 430 use CCS to Watch your stack in code. NOT A QUESTION.

Stuck with a problem in your code? Seek help here.

MSP 430 use CCS to Watch your stack in code. NOT A QUESTION.

Postby Willaim » December 16th, 2012, 2:45 pm

Some fruits of my labor
Watch your stack easily with CCS

a way to watch your stack and find some of those mysterious problems on the MSP430
sometime we are all careless when programming
then you get one of those weird errors that overwrite a variable close to the stack what a pain
this should at least help to find out when your stack overflows or perhaps when something else writes into the stack space.

#include <string.h> // C string functions

unsigned int StackWatch(unsigned char fill);
const char stackstr[]="#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=";

#pragma DATA_SECTION (_stackTAIL, ".stack");//locate this variable at the stacks end (lowest address)
const char _stackTAIL;//don't futz with this variable //DANGEROUS

unsigned int stacktail=0;

unsigned int StackWatch(unsigned char fill)
{
if (stacktail==0)
{
stacktail=(unsigned int)&_stackTAIL; //debugging
strncpy((void *)stacktail ,stackstr,fill);
}
if(fill==0)stacktail=0;//discard the pointer
return stacktail;
}



unsigned char StackIntact(unsigned char CHRS)
{
if (stacktail==0)return 0;

if (CHRS>strlen(stackstr))CHRS=strlen(stackstr);
return strncmp((void *)stacktail, stackstr, CHRS);

}

////USE IT LIKE THIS>>
//in main only once as close to the beginning as possible..

//StackWatch(20); ///Make sure you have enough chars in the stackstr const...


//in a loop somewhere>>

//if(StackIntact(20))
//.ERROR CONDITION.//blink led or something..

//if it doesn't ==0 then the stack is not intact for that many characters

//////////////
//WHEN YOU ARE DONE WITH IT>>
//StackWatch(0);//discards the pointer to the end of the stack


I don't know that i'd leave it in production code but it really comes in handy for Debug code
maybe...It would probably come in handy to reset if something went awry but i think it would just happen again so really pointless in production quality code
yes, you can do this through CCS and whatnot but thats a PITA as well
--EDIT-- Updated the title to be more descriptive
Willaim
 
Posts: 26
Joined: August 20th, 2011, 8:06 pm

Return to Help Me! Software

Who is online

Users browsing this forum: No registered users and 2 guests