Bläddra i källkod

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 år sedan
förälder
incheckning
23e0134596
1 ändrade filer med 28 tillägg och 35 borttagningar
  1. 28
    35
      Marlin/M100_Free_Mem_Chk.cpp

+ 28
- 35
Marlin/M100_Free_Mem_Chk.cpp Visa fil

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

Laddar…
Avbryt
Spara