1

我正在为 MSP430 微控制器编写 C 代码,我想将全局变量锁定到 RAM 中的特定地址。

原因是因为我在 RAM 地址的末尾有一个堆栈,并且向下增长到较低的地址。当堆栈溢出时,它开始覆盖存储在堆栈旁边的 RAM 中的全局变量。所以我想在堆栈旁边写一个水印并检查它是否溢出。

4

2 回答 2

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

于 2012-06-07T05:37:42.867 回答
0

Code Composer Studio 的 StackWatch 和 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, ".stack") 是编译器特定的名称和编译指示,因此您可能需要使用不同的方法来获取地址,例如上面建议的方法

于 2014-03-24T00:06:28.327 回答