command: replace printf printf_P
[protos/xbee-avr.git] / stack_space.c
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: stack_space.c,v 1.7.4.3 2008-05-09 08:23:52 zer0 Exp $
19  *
20  */
21
22 #include <avr/io.h>
23 #include <aversive.h>
24
25
26 #include <diagnostic.h>
27
28
29 /** This diagnostical software fills the RAM with a mark, and counts
30  * how many of these marks are unmodified, in order to avaluate the
31  * min stack space available after code execution.  You can then know
32  * how much your stack your program needed in a peak, without looking
33  * espacially when this peak arises. you see the minmal stack space
34  * left since the reset of the microcontroller. */
35
36 // call this function at the beginning of program
37 void fill_mem_with_mark(void) __attribute__ ((naked)) \
38 __attribute__ ((section (".init1")));
39
40
41 /** this functions fills the ram with a predefined pattern after a
42  * reset and BEFORE any other operation */
43 void fill_mem_with_mark(void)
44 {
45 /*      register int i   asm("r16"); */
46 /*      register int end asm("r18"); */
47
48
49 /*      // where is the beginning of the RAM memory ? */
50 /* #ifdef DIAG_FILL_ENTIRE_RAM // fill entire RAM */
51 /*      asm(  "ldi r16,lo8(__data_start)" ); */
52 /*      asm(  "ldi r17,hi8(__data_start)" ); */
53 /* #else                       // fill only stack and heap spaces */
54 /*      asm(  "ldi r16,lo8(__heap_start)" ); */
55 /*      asm(  "ldi r17,hi8(__heap_start)" ); */
56 /* #endif */
57
58 /*      // end of RAM */
59 /*      asm(  "ldi r18,lo8(__stack)" ); */
60 /*      asm(  "ldi r19,hi8(__stack)" ); */
61   
62 /*      // fill ram with the spacified pattern */
63 /*      for(; i< end ; i++) */
64 /*              * ( (volatile unsigned char* )(i) ) = MARK; */
65
66 }
67
68
69 uint16_t min_stack_space_available(void)
70 {
71         /* register int i   asm("r16"); */
72         /* register int end asm("r18"); */
73         /* uint16_t count , max; */
74
75         /* // where is the beginning of the stack space ? */
76         /* asm(  "ldi r16,lo8(__heap_start)" ); */
77         /* asm(  "ldi r17,hi8(__heap_start)" ); */
78         /* // end of RAM */
79         /* asm(  "ldi r18,lo8(__stack)" ); */
80         /* asm(  "ldi r19,hi8(__stack)" ); */
81   
82         /* /\* the algorithm finds the size of the biggest zone filled */
83         /*  * with the mark, which is normally the stack space left *\/ */
84         /* count = 0; */
85         /* max  = 0; */
86         /* for(; i<end; i++) { */
87         /*      // = mark? */
88         /*      if (MARK == * ( (volatile unsigned char* )(i) )) { */
89         /*              // count */
90         /*              count ++; */
91         /*              if (count > max) */
92         /*                      max = count; */
93         /*      } */
94         /*      else */
95         /*              count = 0; // reset counter */
96         /* } */
97         
98         /* return max; */
99         return 0;
100 }