--- In AVR-Chat@yahoogroups.com, David VanHorn <microbrix@...> wrote:
> How can I assign a variable to a specific point in SRAM?
The simplest way to accomplish this is to use a pointer and initialize the pointer to the address of interest.
// assign the sentinel address
uint8_t *sentinel = (uint8_t *)0x234;
// set the sentinel value
*sentinel = 0xcc;
// check the sentinel value
if (*sentinel != 0xcc)
{
// stack overrun occurred
}
Another alternative is to initialize all of the RAM between the end of statically allocated data and the end of RAM to a given value. After this is done, you can scan upward from the end of statically allocated data to find the first byte that is not the initialization value, thus giving you the "high water mark" for the stack at that time. Some example code for initializing the stack area is shown below. This code assumes standard stack location and would need to be modified if you're using some non-standard setup.
#include <inttypes.h>
#include <avr/io.h>
#define INIT2 __attribute__((section(".init2")))
#define USED __attribute__((used))
#define NAKED __attribute__((naked))
extern uint8_t _end;
extern uint8_t __stack;
void myStackInit(void) USED NAKED INIT2;
void myStackInit(void)
{
uint8_t *p = &_end;
while (p != &__stack)
*p++ = 0xcc;
}Message
Re: WinAVR / GCC question re Stack
2009-03-24 by Don Kinzer
Attachments
- No local attachments were found for this message.