1 /* Code to measure the stack usage.
2 * Copied from http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=343692
6 #include "stackusage.h"
8 #define STACK_CANARY 0xc5
11 extern uint8_t __stack
;
13 void paint_stack(void)
14 __attribute__ ((naked
))
15 __attribute__ ((used
))
16 __attribute__ ((section (".init1")));
18 void paint_stack(void)
29 __asm
volatile (" ldi r30,lo8(_end)\n"
30 " ldi r31,hi8(_end)\n"
31 " ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */
32 " ldi r25,hi8(__stack)\n"
37 " cpi r30,lo8(__stack)\n"
44 uint16_t stack_usage(void)
46 const uint8_t *p
= &_end
;
49 while(*p
== STACK_CANARY
&& p
<= &__stack
)
55 return &__stack
- &_end
- c
;