global variable assign RAM address
-
13-06-2021 - |
Вопрос
I am writing C code for an MSP430 microcontroller and I want to lock a global variable to a specific address in RAM.
The reason is because I have a stack at the end of the RAM address and grows downwards towards lower addresses. When the stack overflows, it starts overwriting global variables stored in RAM right next to the stack. So I want to write a watermark that is right next to the stack and check it for overflows.
Решение
If you know where the end of the stack should be on your MSP430 device, you could do this, for example:
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
Here's someone that has your same question, on the TI forums... it looks helpful: http://e2e.ti.com/support/development_tools/compiler/f/343/t/92002.aspx
Другие советы
StackWatch & StackIntact MSP430 code for Code Composer Studio
#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
Originally posted here http://forums.hackaday.com/viewtopic.php?f=5&t=2998
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
Also Note this is for CCS (Code Composer Studio) #pragma DATA_SECTION (_stackTAIL, ".stack") is a compiler specific name and pragma so you probably will need to use a different method of grabbing the address such as that suggested above