Update TODO list
[trut64.git] / avr / stackusage.c
blobc98f582fb0ecb349af6d396f9713b49182d70c14
1 /* Code to measure the stack usage.
2 * Copied from http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=343692
3 */
5 #include <stdint.h>
6 #include "stackusage.h"
8 #define STACK_CANARY 0xc5
10 extern uint8_t _end;
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)
20 #if 0
21 uint8_t *p = &_end;
23 while(p <= &__stack)
25 *p = STACK_CANARY;
26 p++;
28 #else
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"
33 " rjmp .cmp\n"
34 ".loop:\n"
35 " st Z+,r24\n"
36 ".cmp:\n"
37 " cpi r30,lo8(__stack)\n"
38 " cpc r31,r25\n"
39 " brlo .loop\n"
40 " breq .loop"::);
41 #endif
44 uint16_t stack_usage(void)
46 const uint8_t *p = &_end;
47 uint16_t c = 0;
49 while(*p == STACK_CANARY && p <= &__stack)
51 p++;
52 c++;
55 return &__stack - &_end - c;