Browse Source

Repair M100

M100 D was running too long - caused watchdog resets.
M100 I showed more free memory than a Arduino Mega has RAM.
AnHardt 8 years ago
parent
commit
23e0134596
1 changed files with 28 additions and 35 deletions
  1. 28
    35
      Marlin/M100_Free_Mem_Chk.cpp

+ 28
- 35
Marlin/M100_Free_Mem_Chk.cpp View File

43
 #include "Marlin.h"
43
 #include "Marlin.h"
44
 
44
 
45
 #if ENABLED(M100_FREE_MEMORY_WATCHER)
45
 #if ENABLED(M100_FREE_MEMORY_WATCHER)
46
-extern void* __brkval;
46
+extern char* __brkval;
47
 extern size_t  __heap_start, __heap_end, __flp;
47
 extern size_t  __heap_start, __heap_end, __flp;
48
-
48
+extern char __bss_end;
49
 
49
 
50
 //
50
 //
51
 // Utility functions used by M100 to get its work done.
51
 // Utility functions used by M100 to get its work done.
52
 //
52
 //
53
 
53
 
54
-unsigned char* top_of_stack();
54
+char* top_of_stack();
55
 void prt_hex_nibble(unsigned int);
55
 void prt_hex_nibble(unsigned int);
56
 void prt_hex_byte(unsigned int);
56
 void prt_hex_byte(unsigned int);
57
 void prt_hex_word(unsigned int);
57
 void prt_hex_word(unsigned int);
58
-int how_many_E5s_are_here(unsigned char*);
58
+int how_many_E5s_are_here(char*);
59
 
59
 
60
 void gcode_M100() {
60
 void gcode_M100() {
61
-  static int m100_not_initialized = 1;
62
-  unsigned char* sp, *ptr;
61
+  static bool m100_not_initialized = true;
62
+  char* sp, *ptr;
63
   int i, j, n;
63
   int i, j, n;
64
   //
64
   //
65
   // M100 D dumps the free memory block from __brkval to the stack pointer.
65
   // M100 D dumps the free memory block from __brkval to the stack pointer.
72
   //
72
   //
73
   #if ENABLED(M100_FREE_MEMORY_DUMPER) // Disable to remove Dump sub-command
73
   #if ENABLED(M100_FREE_MEMORY_DUMPER) // Disable to remove Dump sub-command
74
     if (code_seen('D')) {
74
     if (code_seen('D')) {
75
-      ptr = (unsigned char*) __brkval;
75
+      ptr = __brkval ? __brkval : &__bss_end;
76
       //
76
       //
77
       // We want to start and end the dump on a nice 16 byte boundry even though
77
       // We want to start and end the dump on a nice 16 byte boundry even though
78
       // the values we are using are not 16 byte aligned.
78
       // the values we are using are not 16 byte aligned.
79
       //
79
       //
80
-      SERIAL_ECHOPGM("\n__brkval : ");
80
+      SERIAL_ECHOPGM("\nbss_end : ");
81
       prt_hex_word((unsigned int) ptr);
81
       prt_hex_word((unsigned int) ptr);
82
-      ptr = (unsigned char*)((unsigned long) ptr & 0xfff0);
82
+      ptr = (char*)((unsigned long) ptr & 0xfff0);
83
       sp = top_of_stack();
83
       sp = top_of_stack();
84
       SERIAL_ECHOPGM("\nStack Pointer : ");
84
       SERIAL_ECHOPGM("\nStack Pointer : ");
85
       prt_hex_word((unsigned int) sp);
85
       prt_hex_word((unsigned int) sp);
86
       SERIAL_EOL;
86
       SERIAL_EOL;
87
-      sp = (unsigned char*)((unsigned long) sp | 0x000f);
87
+      sp = (char*)((unsigned long) sp | 0x000f);
88
       n = sp - ptr;
88
       n = sp - ptr;
89
       //
89
       //
90
       // This is the main loop of the Dump command.
90
       // This is the main loop of the Dump command.
95
         for (i = 0; i < 16; i++) {      // and 16 data bytes
95
         for (i = 0; i < 16; i++) {      // and 16 data bytes
96
           prt_hex_byte(*(ptr + i));
96
           prt_hex_byte(*(ptr + i));
97
           SERIAL_CHAR(' ');
97
           SERIAL_CHAR(' ');
98
-          delay(2);
99
         }
98
         }
100
         SERIAL_CHAR('|');         // now show where non 0xE5's are
99
         SERIAL_CHAR('|');         // now show where non 0xE5's are
101
         for (i = 0; i < 16; i++) {
100
         for (i = 0; i < 16; i++) {
102
-          delay(2);
103
-          if (*(ptr + i) == 0xe5)
101
+          if (*(ptr + i) == (char)0xe5)
104
             SERIAL_CHAR(' ');
102
             SERIAL_CHAR(' ');
105
           else
103
           else
106
             SERIAL_CHAR('?');
104
             SERIAL_CHAR('?');
107
         }
105
         }
108
         SERIAL_EOL;
106
         SERIAL_EOL;
109
         ptr += 16;
107
         ptr += 16;
110
-        delay(2);
111
       }
108
       }
112
-      SERIAL_ECHOLNPGM("Done.");
113
       return;
109
       return;
114
     }
110
     }
115
   #endif
111
   #endif
119
   //
115
   //
120
   if (code_seen('F')) {
116
   if (code_seen('F')) {
121
     #if 0
117
     #if 0
122
-      int max_addr = (int) __brkval;
118
+      int max_addr = (int)  __brkval ? __brkval : &__bss_end;
123
       int max_cnt = 0;
119
       int max_cnt = 0;
124
     #endif
120
     #endif
125
     int block_cnt = 0;
121
     int block_cnt = 0;
126
-    ptr = (unsigned char*) __brkval;
122
+    ptr =  __brkval ? __brkval : &__bss_end;
127
     sp = top_of_stack();
123
     sp = top_of_stack();
128
     n = sp - ptr;
124
     n = sp - ptr;
129
     // Scan through the range looking for the biggest block of 0xE5's we can find
125
     // Scan through the range looking for the biggest block of 0xE5's we can find
130
     for (i = 0; i < n; i++) {
126
     for (i = 0; i < n; i++) {
131
-      if (*(ptr + i) == (unsigned char) 0xe5) {
132
-        j = how_many_E5s_are_here((unsigned char*) ptr + i);
127
+      if (*(ptr + i) == (char)0xe5) {
128
+        j = how_many_E5s_are_here(ptr + i);
133
         if (j > 8) {
129
         if (j > 8) {
134
           SERIAL_ECHOPAIR("Found ", j);
130
           SERIAL_ECHOPAIR("Found ", j);
135
           SERIAL_ECHOPGM(" bytes free at 0x");
131
           SERIAL_ECHOPGM(" bytes free at 0x");
148
     }
144
     }
149
     if (block_cnt > 1)
145
     if (block_cnt > 1)
150
       SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
146
       SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
151
-    SERIAL_ECHOLNPGM("\nDone.");
152
     return;
147
     return;
153
   }
148
   }
154
   //
149
   //
159
     if (code_seen('C')) {
154
     if (code_seen('C')) {
160
       int x = code_value_int(); // x gets the # of locations to corrupt within the memory pool
155
       int x = code_value_int(); // x gets the # of locations to corrupt within the memory pool
161
       SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
156
       SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
162
-      ptr = (unsigned char*) __brkval;
163
-      SERIAL_ECHOPAIR("\n__brkval : ", ptr);
157
+      ptr = __brkval ? __brkval : &__bss_end;
158
+      SERIAL_ECHOPAIR("\nbss_end : ", ptr);
164
       ptr += 8;
159
       ptr += 8;
165
       sp = top_of_stack();
160
       sp = top_of_stack();
166
       SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
161
       SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
181
   // M100 I    Initializes the free memory pool so it can be watched and prints vital
176
   // M100 I    Initializes the free memory pool so it can be watched and prints vital
182
   // statistics that define the free memory pool.
177
   // statistics that define the free memory pool.
183
   //
178
   //
184
-  if (m100_not_initialized || code_seen('I')) {       // If no sub-command is specified, the first time
185
-    SERIAL_ECHOLNPGM("Initializing free memory block.\n");    // this happens, it will Initialize.
186
-    ptr = (unsigned char*) __brkval;        // Repeated M100 with no sub-command will not destroy the
187
-    SERIAL_ECHOPAIR("\n__brkval : ", ptr);     // state of the initialized free memory pool.
179
+  if (m100_not_initialized || code_seen('I')) {            // If no sub-command is specified, the first time
180
+    SERIAL_ECHOLNPGM("Initializing free memory block.\n"); // this happens, it will Initialize.
181
+    ptr = __brkval ? __brkval : &__bss_end;                // Repeated M100 with no sub-command will not destroy the
182
+    SERIAL_ECHOPAIR("\nbss_end : ", ptr);                  // state of the initialized free memory pool.
188
     ptr += 8;
183
     ptr += 8;
189
     sp = top_of_stack();
184
     sp = top_of_stack();
190
     SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
185
     SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
194
     SERIAL_ECHO(n);
189
     SERIAL_ECHO(n);
195
     SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
190
     SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
196
     for (i = 0; i < n; i++)
191
     for (i = 0; i < n; i++)
197
-      *(ptr + i) = (unsigned char) 0xe5;
192
+      *(ptr + i) = (char)0xe5;
198
     for (i = 0; i < n; i++) {
193
     for (i = 0; i < n; i++) {
199
-      if (*(ptr + i) != (unsigned char) 0xe5) {
194
+      if (*(ptr + i) != (char)0xe5) {
200
         SERIAL_ECHOPAIR("? address : ", ptr + i);
195
         SERIAL_ECHOPAIR("? address : ", ptr + i);
201
         SERIAL_ECHOPAIR("=", *(ptr + i));
196
         SERIAL_ECHOPAIR("=", *(ptr + i));
202
         SERIAL_ECHOLNPGM("\n");
197
         SERIAL_ECHOLNPGM("\n");
203
       }
198
       }
204
     }
199
     }
205
-    m100_not_initialized = 0;
206
-    SERIAL_ECHOLNPGM("Done.\n");
200
+    m100_not_initialized = false;
207
     return;
201
     return;
208
   }
202
   }
209
   return;
203
   return;
212
 // top_of_stack() returns the location of a variable on its stack frame.  The value returned is above
206
 // top_of_stack() returns the location of a variable on its stack frame.  The value returned is above
213
 // the stack once the function returns to the caller.
207
 // the stack once the function returns to the caller.
214
 
208
 
215
-unsigned char* top_of_stack() {
216
-  unsigned char x;
209
+char* top_of_stack() {
210
+  char x;
217
   return &x + 1; // x is pulled on return;
211
   return &x + 1; // x is pulled on return;
218
 }
212
 }
219
 
213
 
226
     SERIAL_ECHO(n);
220
     SERIAL_ECHO(n);
227
   else
221
   else
228
     SERIAL_ECHO((char)('A' + n - 10));
222
     SERIAL_ECHO((char)('A' + n - 10));
229
-  delay(2);
230
 }
223
 }
231
 
224
 
232
 void prt_hex_byte(unsigned int b) {
225
 void prt_hex_byte(unsigned int b) {
242
 // how_many_E5s_are_here() is a utility function to easily find out how many 0xE5's are
235
 // how_many_E5s_are_here() is a utility function to easily find out how many 0xE5's are
243
 // at the specified location.  Having this logic as a function simplifies the search code.
236
 // at the specified location.  Having this logic as a function simplifies the search code.
244
 //
237
 //
245
-int how_many_E5s_are_here(unsigned char* p) {
238
+int how_many_E5s_are_here(char* p) {
246
   int n;
239
   int n;
247
   for (n = 0; n < 32000; n++) {
240
   for (n = 0; n < 32000; n++) {
248
-    if (*(p + n) != (unsigned char) 0xe5)
241
+    if (*(p + n) != (char)0xe5)
249
       return n - 1;
242
       return n - 1;
250
   }
243
   }
251
   return -1;
244
   return -1;

Loading…
Cancel
Save