我正在为MSP430微控制器编写C代码,我想将全局变量锁定到RAM中的特定地址。
原因是因为我在RAM地址的末尾有一个堆栈,并向下向低地址增长。当堆栈溢出时,它会开始覆盖存储在堆栈旁边的RAM中的全局变量。所以我想写一个紧靠堆栈旁边的水印并检查它是否有溢出。
答案 0 :(得分:1)
如果你知道MSP430设备上堆栈的末端应该在哪里,你可以这样做,例如:
int* ptr; // will point to end of stack
ptr = (int*)0x0600; // memory address of the end of the stack
*ptr = 0x1234; // value to assign to memory
这是TI论坛上有同样问题的人......看起来很有帮助:http://e2e.ti.com/support/development_tools/compiler/f/343/t/92002.aspx
答案 1 :(得分:0)
StackWatch&用于Code Composer Studio的StackIntact MSP430代码
#include <string.h> // C string functions
unsigned int StackWatch(unsigned char fill);//prototype
const char stackstr[]="#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=";
//^stackstr needs to have enough characters to cover the size of stack 'intactness' you desire
#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;
}//StackWatch
unsigned char StackIntact(unsigned char CHRS)
{
if (stacktail==0)return 0;
if (CHRS>strlen(stackstr))CHRS=strlen(stackstr);
return strncmp((void *)stacktail, stackstr, CHRS);
}//StackIntact
////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
最初发布在此http://forums.hackaday.com/viewtopic.php?f=5&t=2998
我不知道我将其保留在生产代码中,但它确实对调试代码非常方便
也许......如果出现问题,重置可能会派上用场,但我认为它会再次发生,因此在生产质量代码中毫无意义 是的,你可以通过CCS和诸如此类的东西来做到这一点,但那也是一个PITA
另请注意,这适用于CCS(Code Composer Studio)#pragma DATA_SECTION(_stackTAIL,&#34; .stack&#34;)是编译器特定的名称和编译指示,因此您可能需要使用不同的方法来获取如上所述的地址