2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
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.
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.
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
18 * Revision : $Id: stack_space.c,v 1.7.4.3 2008-05-09 08:23:52 zer0 Exp $
26 #include <diagnostic.h>
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. */
36 // call this function at the beginning of program
37 void fill_mem_with_mark(void) __attribute__ ((naked)) \
38 __attribute__ ((section (".init1")));
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)
45 register int i asm("r16");
46 register int end asm("r18");
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)" );
59 asm( "ldi r18,lo8(__stack)" );
60 asm( "ldi r19,hi8(__stack)" );
62 // fill ram with the spacified pattern
64 * ( (volatile unsigned char* )(i) ) = MARK;
69 uint16_t min_stack_space_available(void)
71 register int i asm("r16");
72 register int end asm("r18");
75 // where is the beginning of the stack space ?
76 asm( "ldi r16,lo8(__heap_start)" );
77 asm( "ldi r17,hi8(__heap_start)" );
79 asm( "ldi r18,lo8(__stack)" );
80 asm( "ldi r19,hi8(__stack)" );
82 /* the algorithm finds the size of the biggest zone filled
83 * with the mark, which is normally the stack space left */
88 if (MARK == * ( (volatile unsigned char* )(i) )) {
95 count = 0; // reset counter