I got some errors with the above code from "DrRobotNinja" on the dsPICEP series, mostly made everything use 16 bit variables + increased the address by 2 instead of 1 to stop address error's by keeping things word aligned
#define STACK_VAR_PAD 0x0020
#define TOP_OF_STACK_ADDR SPLIM
#define STACK_GUARD_BYTE 0xA1DE
void WriteStackGuardBytes(void)
{
static __eds__ int * ptr;
static unsigned int start_addr;
static unsigned int addr;
//Use a variable on the stack
int stack_var;
ptr = &stack_var;
//Casting to 32-bit first supresses complier warning
//This chip uses a 16-bit address space
start_addr = (unsigned long int)ptr + STACK_VAR_PAD;
for (addr = start_addr; addr < TOP_OF_STACK_ADDR; addr += 2)
{
*((unsigned int *)(addr)) = STACK_GUARD_BYTE;
}
}
unsigned int ReadStackGuardBytes(void)
{
static __eds__ int * ptr;
static unsigned int start_addr;
static unsigned int addr;
static unsigned int top_addr;
//Use a variable on the stack here
int stack_var;
ptr = &stack_var;
//Casting to 32-bit first supresses complier warning
//This chip uses a 16-bit address space
start_addr = (unsigned long int)ptr;
top_addr = start_addr;
for (addr = start_addr; addr < TOP_OF_STACK_ADDR; addr += 2)
{
if (*((unsigned int *)(addr)) != STACK_GUARD_BYTE)
{
top_addr = addr;
}
else
{
break; //make this more robust and search for 2-3 consecutive
}
}
return TOP_OF_STACK_ADDR - top_addr;
}