My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

M100_Free_Mem_Chk.cpp 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * M100 Free Memory Watcher
  24. *
  25. * This code watches the free memory block between the bottom of the heap and the top of the stack.
  26. * This memory block is initialized and watched via the M100 command.
  27. *
  28. * M100 I Initializes the free memory block and prints vitals statistics about the area
  29. * M100 F Identifies how much of the free memory block remains free and unused. It also
  30. * detects and reports any corruption within the free memory block that may have
  31. * happened due to errant firmware.
  32. * M100 D Does a hex display of the free memory block along with a flag for any errant
  33. * data that does not match the expected value.
  34. * M100 C x Corrupts x locations within the free memory block. This is useful to check the
  35. * correctness of the M100 F and M100 D commands.
  36. *
  37. * Initial version by Roxy-3D
  38. */
  39. #define M100_FREE_MEMORY_DUMPER // Comment out to remove Dump sub-command
  40. #define M100_FREE_MEMORY_CORRUPTOR // Comment out to remove Corrupt sub-command
  41. #include "Marlin.h"
  42. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  43. extern char* __brkval;
  44. extern size_t __heap_start, __heap_end, __flp;
  45. extern char __bss_end;
  46. //
  47. // Utility functions used by M100 to get its work done.
  48. //
  49. #include "hex_print_routines.h"
  50. char* top_of_stack();
  51. int how_many_E5s_are_here(char*);
  52. void gcode_M100() {
  53. static bool m100_not_initialized = true;
  54. char* sp, *ptr;
  55. int i, j, n;
  56. //
  57. // M100 D dumps the free memory block from __brkval to the stack pointer.
  58. // malloc() eats memory from the start of the block and the stack grows
  59. // up from the bottom of the block. Solid 0xE5's indicate nothing has
  60. // used that memory yet. There should not be anything but 0xE5's within
  61. // the block of 0xE5's. If there is, that would indicate memory corruption
  62. // probably caused by bad pointers. Any unexpected values will be flagged in
  63. // the right hand column to help spotting them.
  64. //
  65. #if ENABLED(M100_FREE_MEMORY_DUMPER) // Disable to remove Dump sub-command
  66. if (code_seen('D')) {
  67. ptr = __brkval ? __brkval : &__bss_end;
  68. //
  69. // We want to start and end the dump on a nice 16 byte boundry even though
  70. // the values we are using are not 16 byte aligned.
  71. //
  72. SERIAL_ECHOPAIR("\nbss_end : 0x", hex_word((uint16_t)ptr));
  73. ptr = (char*)((uint32_t)ptr & 0xfff0);
  74. sp = top_of_stack();
  75. SERIAL_ECHOLNPAIR("\nStack Pointer : 0x", hex_word((uint16_t)sp));
  76. sp = (char*)((uint32_t)sp | 0x000f);
  77. n = sp - ptr;
  78. //
  79. // This is the main loop of the Dump command.
  80. //
  81. while (ptr < sp) {
  82. print_hex_word((uint16_t)ptr); // Print the address
  83. SERIAL_CHAR(':');
  84. for (i = 0; i < 16; i++) { // and 16 data bytes
  85. print_hex_byte(*(ptr + i));
  86. SERIAL_CHAR(' ');
  87. }
  88. SERIAL_CHAR('|'); // now show where non 0xE5's are
  89. for (i = 0; i < 16; i++)
  90. SERIAL_CHAR((*(ptr + i) == (char)0xe5) ? ' ' : '?');
  91. SERIAL_EOL;
  92. ptr += 16;
  93. }
  94. return;
  95. }
  96. #endif
  97. //
  98. // M100 F requests the code to return the number of free bytes in the memory pool along with
  99. // other vital statistics that define the memory pool.
  100. //
  101. if (code_seen('F')) {
  102. #if 0
  103. int max_addr = (int) __brkval ? __brkval : &__bss_end;
  104. int max_cnt = 0;
  105. #endif
  106. int block_cnt = 0;
  107. ptr = __brkval ? __brkval : &__bss_end;
  108. sp = top_of_stack();
  109. n = sp - ptr;
  110. // Scan through the range looking for the biggest block of 0xE5's we can find
  111. for (i = 0; i < n; i++) {
  112. if (*(ptr + i) == (char)0xe5) {
  113. j = how_many_E5s_are_here(ptr + i);
  114. if (j > 8) {
  115. SERIAL_ECHOPAIR("Found ", j);
  116. SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)(ptr + i)));
  117. i += j;
  118. block_cnt++;
  119. }
  120. #if 0
  121. if (j > max_cnt) { // We don't do anything with this information yet
  122. max_cnt = j; // but we do know where the biggest free memory block is.
  123. max_addr = (int) ptr + i;
  124. }
  125. #endif
  126. }
  127. }
  128. if (block_cnt > 1)
  129. SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
  130. return;
  131. }
  132. //
  133. // M100 C x Corrupts x locations in the free memory pool and reports the locations of the corruption.
  134. // This is useful to check the correctness of the M100 D and the M100 F commands.
  135. //
  136. #if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
  137. if (code_seen('C')) {
  138. int x = code_value_int(); // x gets the # of locations to corrupt within the memory pool
  139. SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
  140. ptr = __brkval ? __brkval : &__bss_end;
  141. SERIAL_ECHOPAIR("\nbss_end : ", ptr);
  142. ptr += 8;
  143. sp = top_of_stack();
  144. SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
  145. SERIAL_ECHOLNPGM("\n");
  146. n = sp - ptr - 64; // -64 just to keep us from finding interrupt activity that
  147. // has altered the stack.
  148. j = n / (x + 1);
  149. for (i = 1; i <= x; i++) {
  150. *(ptr + (i * j)) = i;
  151. SERIAL_ECHOPAIR("\nCorrupting address: 0x", hex_word((uint16_t)(ptr + i * j)));
  152. }
  153. SERIAL_ECHOLNPGM("\n");
  154. return;
  155. }
  156. #endif
  157. //
  158. // M100 I Initializes the free memory pool so it can be watched and prints vital
  159. // statistics that define the free memory pool.
  160. //
  161. if (m100_not_initialized || code_seen('I')) { // If no sub-command is specified, the first time
  162. SERIAL_ECHOLNPGM("Initializing free memory block.\n"); // this happens, it will Initialize.
  163. ptr = __brkval ? __brkval : &__bss_end; // Repeated M100 with no sub-command will not destroy the
  164. SERIAL_ECHOPAIR("\nbss_end : ", ptr); // state of the initialized free memory pool.
  165. ptr += 8;
  166. sp = top_of_stack();
  167. SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
  168. SERIAL_ECHOLNPGM("\n");
  169. n = sp - ptr - 64; // -64 just to keep us from finding interrupt activity that
  170. // has altered the stack.
  171. SERIAL_ECHO(n);
  172. SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
  173. for (i = 0; i < n; i++)
  174. *(ptr + i) = (char)0xe5;
  175. for (i = 0; i < n; i++) {
  176. if (*(ptr + i) != (char)0xe5) {
  177. SERIAL_ECHOPAIR("? address : ", ptr + i);
  178. SERIAL_ECHOPAIR("=", *(ptr + i));
  179. SERIAL_ECHOLNPGM("\n");
  180. }
  181. }
  182. m100_not_initialized = false;
  183. return;
  184. }
  185. return;
  186. }
  187. // top_of_stack() returns the location of a variable on its stack frame. The value returned is above
  188. // the stack once the function returns to the caller.
  189. char* top_of_stack() {
  190. char x;
  191. return &x + 1; // x is pulled on return;
  192. }
  193. // how_many_E5s_are_here() is a utility function to easily find out how many 0xE5's are
  194. // at the specified location. Having this logic as a function simplifies the search code.
  195. //
  196. int how_many_E5s_are_here(char* p) {
  197. int n;
  198. for (n = 0; n < 32000; n++) {
  199. if (*(p + n) != (char)0xe5)
  200. return n - 1;
  201. }
  202. return -1;
  203. }
  204. #endif