|
@@ -20,8 +20,15 @@
|
20
|
20
|
*
|
21
|
21
|
*/
|
22
|
22
|
|
|
23
|
+#include "../../inc/MarlinConfig.h"
|
|
24
|
+
|
|
25
|
+#if ENABLED(M100_FREE_MEMORY_WATCHER)
|
|
26
|
+
|
|
27
|
+#include "../gcode.h"
|
23
|
28
|
#include "../../libs/hex_print_routines.h"
|
24
|
29
|
|
|
30
|
+#include "../../Marlin.h" // for idle()
|
|
31
|
+
|
25
|
32
|
/**
|
26
|
33
|
* M100 Free Memory Watcher
|
27
|
34
|
*
|
|
@@ -72,7 +79,7 @@ char* top_of_stack() {
|
72
|
79
|
}
|
73
|
80
|
|
74
|
81
|
// Count the number of test bytes at the specified location.
|
75
|
|
-int16_t count_test_bytes(const char * const ptr) {
|
|
82
|
+inline int16_t count_test_bytes(const char * const ptr) {
|
76
|
83
|
for (uint16_t i = 0; i < 32000; i++)
|
77
|
84
|
if (((char) ptr[i]) != TEST_BYTE)
|
78
|
85
|
return i - 1;
|
|
@@ -94,17 +101,17 @@ int16_t count_test_bytes(const char * const ptr) {
|
94
|
101
|
* the block. If so, it may indicate memory corruption due to a bad pointer.
|
95
|
102
|
* Unexpected bytes are flagged in the right column.
|
96
|
103
|
*/
|
97
|
|
- void dump_free_memory(const char *ptr, const char *sp) {
|
|
104
|
+ inline void dump_free_memory(const char *ptr, const char *sp) {
|
98
|
105
|
//
|
99
|
106
|
// Start and end the dump on a nice 16 byte boundary
|
100
|
107
|
// (even though the values are not 16-byte aligned).
|
101
|
108
|
//
|
102
|
|
- ptr = (char *)((uint16_t)ptr & 0xFFF0); // Align to 16-byte boundary
|
103
|
|
- sp = (char *)((uint16_t)sp | 0x000F); // Align sp to the 15th byte (at or above sp)
|
|
109
|
+ ptr = (char*)((ptr_int_t)((uint32_t)ptr & 0xFFFFFFF0)); // Align to 16-byte boundary
|
|
110
|
+ sp = (char*)((ptr_int_t)((uint32_t)sp | 0x0000000F)); // Align sp to the 15th byte (at or above sp)
|
104
|
111
|
|
105
|
112
|
// Dump command main loop
|
106
|
113
|
while (ptr < sp) {
|
107
|
|
- print_hex_word((uint16_t)ptr); // Print the address
|
|
114
|
+ print_hex_address(ptr); // Print the address
|
108
|
115
|
SERIAL_CHAR(':');
|
109
|
116
|
for (uint8_t i = 0; i < 16; i++) { // and 16 data bytes
|
110
|
117
|
if (i == 8) SERIAL_CHAR('-');
|
|
@@ -135,14 +142,14 @@ int16_t count_test_bytes(const char * const ptr) {
|
135
|
142
|
//
|
136
|
143
|
// Round the start and end locations to produce full lines of output
|
137
|
144
|
//
|
138
|
|
- start = (char*)((uint16_t) start & 0xFFF0);
|
139
|
|
- end = (char*)((uint16_t) end | 0x000F);
|
|
145
|
+ start = (char*)((ptr_int_t)((uint32_t)start & 0xFFFFFFF0)); // Align to 16-byte boundary
|
|
146
|
+ end = (char*)((ptr_int_t)((uint32_t)end | 0x0000000F)); // Align sp to the 15th byte (at or above sp)
|
140
|
147
|
dump_free_memory(start, end);
|
141
|
148
|
}
|
142
|
149
|
|
143
|
150
|
#endif // M100_FREE_MEMORY_DUMPER
|
144
|
151
|
|
145
|
|
-int check_for_free_memory_corruption(const char * const title) {
|
|
152
|
+inline int check_for_free_memory_corruption(const char * const title) {
|
146
|
153
|
SERIAL_ECHO(title);
|
147
|
154
|
|
148
|
155
|
char *ptr = END_OF_HEAP(), *sp = top_of_stack();
|
|
@@ -187,7 +194,7 @@ int check_for_free_memory_corruption(const char * const title) {
|
187
|
194
|
}
|
188
|
195
|
SERIAL_ECHOPAIR(" block_found=", block_cnt);
|
189
|
196
|
|
190
|
|
- if (block_cnt != 1 || __brkval != 0x0000)
|
|
197
|
+ if (block_cnt != 1 || __brkval != NULL)
|
191
|
198
|
SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
|
192
|
199
|
|
193
|
200
|
if (block_cnt == 0) // Make sure the special case of no free blocks shows up as an
|
|
@@ -208,7 +215,7 @@ int check_for_free_memory_corruption(const char * const title) {
|
208
|
215
|
* Return the number of free bytes in the memory pool,
|
209
|
216
|
* with other vital statistics defining the pool.
|
210
|
217
|
*/
|
211
|
|
-void free_memory_pool_report(char * const ptr, const int16_t size) {
|
|
218
|
+inline void free_memory_pool_report(char * const ptr, const int16_t size) {
|
212
|
219
|
int16_t max_cnt = -1, block_cnt = 0;
|
213
|
220
|
char *max_addr = NULL;
|
214
|
221
|
// Find the longest block of test bytes in the buffer
|
|
@@ -242,7 +249,7 @@ void free_memory_pool_report(char * const ptr, const int16_t size) {
|
242
|
249
|
* Corrupt <num> locations in the free memory pool and report the corrupt addresses.
|
243
|
250
|
* This is useful to check the correctness of the M100 D and the M100 F commands.
|
244
|
251
|
*/
|
245
|
|
- void corrupt_free_memory(char *ptr, const uint16_t size) {
|
|
252
|
+ inline void corrupt_free_memory(char *ptr, const uint16_t size) {
|
246
|
253
|
ptr += 8;
|
247
|
254
|
const uint16_t near_top = top_of_stack() - ptr - 250, // -250 to avoid interrupt activity that's altered the stack.
|
248
|
255
|
j = near_top / (size + 1);
|
|
@@ -261,7 +268,7 @@ void free_memory_pool_report(char * const ptr, const int16_t size) {
|
261
|
268
|
* M100 I
|
262
|
269
|
* Init memory for the M100 tests. (Automatically applied on the first M100.)
|
263
|
270
|
*/
|
264
|
|
-void init_free_memory(char *ptr, int16_t size) {
|
|
271
|
+inline void init_free_memory(char *ptr, int16_t size) {
|
265
|
272
|
SERIAL_ECHOLNPGM("Initializing free memory block.\n\n");
|
266
|
273
|
|
267
|
274
|
size -= 250; // -250 to avoid interrupt activity that's altered the stack.
|
|
@@ -289,7 +296,7 @@ void init_free_memory(char *ptr, int16_t size) {
|
289
|
296
|
/**
|
290
|
297
|
* M100: Free Memory Check
|
291
|
298
|
*/
|
292
|
|
-void gcode_M100() {
|
|
299
|
+void GcodeSuite::M100() {
|
293
|
300
|
SERIAL_ECHOPAIR("\n__brkval : ", hex_address(__brkval));
|
294
|
301
|
SERIAL_ECHOPAIR("\n__bss_end : ", hex_address(&__bss_end));
|
295
|
302
|
|
|
@@ -320,3 +327,5 @@ void gcode_M100() {
|
320
|
327
|
|
321
|
328
|
#endif
|
322
|
329
|
}
|
|
330
|
+
|
|
331
|
+#endif // M100_FREE_MEMORY_WATCHER
|