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 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. int free_memory_is_corrupted(); // int not bool!!!! it will tell us how many blocks of
  53. // free memory it found.
  54. void gcode_M100() {
  55. static bool m100_not_initialized = true;
  56. char* sp, *ptr;
  57. int i, j, n;
  58. //
  59. // M100 D dumps the free memory block from __brkval to the stack pointer.
  60. // malloc() eats memory from the start of the block and the stack grows
  61. // up from the bottom of the block. Solid 0xE5's indicate nothing has
  62. // used that memory yet. There should not be anything but 0xE5's within
  63. // the block of 0xE5's. If there is, that would indicate memory corruption
  64. // probably caused by bad pointers. Any unexpected values will be flagged in
  65. // the right hand column to help spotting them.
  66. //
  67. SERIAL_ECHOPAIR("\n__brkval : 0x", hex_word((uint16_t)__brkval) );
  68. SERIAL_ECHOPAIR("\n__bss_end : 0x", hex_word((uint16_t)&__bss_end));
  69. //
  70. // With out malloc() we need to be smart and use &__bss_end
  71. //
  72. ptr = __brkval ? __brkval : &__bss_end;
  73. SERIAL_ECHOPAIR("\nstart of free space : 0x", hex_word((uint16_t)ptr));
  74. sp = top_of_stack();
  75. SERIAL_ECHOLNPAIR("\nStack Pointer : 0x", hex_word((uint16_t)sp));
  76. #if ENABLED(M100_FREE_MEMORY_DUMPER) // Disable to remove Dump sub-command
  77. if (code_seen('D')) {
  78. //
  79. // We want to start and end the dump on a nice 16 byte boundry even though
  80. // the values we are using are not 16 byte aligned.
  81. //
  82. ptr = (char*) ((uint16_t) ptr & 0xfff0);
  83. sp = (char*) ((uint16_t) sp | 0x000f);
  84. n = sp - ptr;
  85. //
  86. // This is the main loop of the Dump command.
  87. //
  88. while (ptr < sp) {
  89. print_hex_word((uint16_t)ptr); // Print the address
  90. SERIAL_CHAR(':');
  91. for (i = 0; i < 16; i++) { // and 16 data bytes
  92. if (i==8)
  93. SERIAL_CHAR('-');
  94. print_hex_byte(*(ptr + i));
  95. SERIAL_CHAR(' ');
  96. }
  97. SERIAL_CHAR('|'); // now show where non 0xE5's are
  98. for (i = 0; i < 16; i++)
  99. SERIAL_CHAR((*(ptr + i) == (char)0xe5) ? ' ' : '?');
  100. SERIAL_EOL;
  101. ptr += 16;
  102. idle();
  103. }
  104. return;
  105. }
  106. #endif
  107. //
  108. // M100 F requests the code to return the number of free bytes in the memory pool along with
  109. // other vital statistics that define the memory pool.
  110. //
  111. if (code_seen('F')) {
  112. int max_cnt = -1, block_cnt = 0;
  113. uint16_t max_addr=0;
  114. ptr = __brkval ? __brkval : &__bss_end;
  115. sp = top_of_stack();
  116. n = sp - ptr;
  117. // Scan through the range looking for the biggest block of 0xE5's we can find
  118. for (i = 0; i < n; i++) {
  119. if (*(ptr + i) == (char)0xe5) {
  120. j = how_many_E5s_are_here(ptr + i);
  121. if (j > 8) {
  122. SERIAL_ECHOPAIR("Found ", j);
  123. SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)(ptr + i)));
  124. if (j > max_cnt) {
  125. max_cnt = j;
  126. max_addr = (uint16_t) ptr + i;
  127. }
  128. i += j;
  129. block_cnt++;
  130. }
  131. }
  132. }
  133. if (block_cnt > 1) {
  134. SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
  135. SERIAL_ECHOPAIR("\nLargest free block is ", max_cnt);
  136. SERIAL_ECHOLNPAIR(" bytes big at 0x", hex_word(max_addr));
  137. }
  138. SERIAL_ECHOLNPAIR("free_memory_is_corrupted() = ", free_memory_is_corrupted());
  139. return;
  140. }
  141. //
  142. // M100 C x Corrupts x locations in the free memory pool and reports the locations of the corruption.
  143. // This is useful to check the correctness of the M100 D and the M100 F commands.
  144. //
  145. #if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
  146. if (code_seen('C')) {
  147. int x = code_value_int(); // x gets the # of locations to corrupt within the memory pool
  148. SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
  149. ptr += 8;
  150. sp = top_of_stack();
  151. n = sp - ptr - 250; // -250 just to keep us from finding interrupt activity that
  152. // has altered the stack.
  153. j = n / (x + 1);
  154. for (i = 1; i <= x; i++) {
  155. *(ptr + (i * j)) = i;
  156. SERIAL_ECHOPAIR("\nCorrupting address: 0x", hex_word((uint16_t)(ptr + i * j)));
  157. }
  158. SERIAL_ECHOLNPGM("\n");
  159. return;
  160. }
  161. #endif
  162. //
  163. // M100 I Initializes the free memory pool so it can be watched and prints vital
  164. // statistics that define the free memory pool.
  165. //
  166. if (m100_not_initialized || code_seen('I')) { // If no sub-command is specified, the first time
  167. SERIAL_ECHOLNPGM("Initializing free memory block.\n"); // this happens, it will Initialize.
  168. // Repeated M100 with no sub-command will not destroy the
  169. // state of the initialized free memory pool.
  170. ptr += 8;
  171. SERIAL_ECHOLNPGM("\n");
  172. n = sp - ptr - 250; // -250 just to keep us from finding interrupt activity that
  173. // has altered the stack.
  174. SERIAL_ECHO(n);
  175. SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
  176. for (i = 0; i < n; i++)
  177. *(ptr + i) = (char)0xe5;
  178. for (i = 0; i < n; i++) {
  179. if (*(ptr + i) != (char)0xe5) {
  180. SERIAL_ECHOPAIR("? address : ", hex_word(ptr+i) );
  181. SERIAL_ECHOPAIR("=", hex_byte(*(ptr + i)) );
  182. SERIAL_ECHOLNPGM("\n");
  183. }
  184. }
  185. m100_not_initialized = false;
  186. return;
  187. }
  188. return;
  189. }
  190. // top_of_stack() returns the location of a variable on its stack frame. The value returned is above
  191. // the stack once the function returns to the caller.
  192. char* top_of_stack() {
  193. char x;
  194. return &x + 1; // x is pulled on return;
  195. }
  196. // how_many_E5s_are_here() is a utility function to easily find out how many 0xE5's are
  197. // at the specified location. Having this logic as a function simplifies the search code.
  198. //
  199. int how_many_E5s_are_here(char* p) {
  200. int n;
  201. for (n = 0; n < 32000; n++) {
  202. if (*(p + n) != (char)0xe5)
  203. return n - 1;
  204. }
  205. return -1;
  206. }
  207. int free_memory_is_corrupted() {
  208. char *sp, *ptr;
  209. int block_cnt = 0, i, j, n;
  210. ptr = __brkval ? __brkval : &__bss_end;
  211. sp = top_of_stack();
  212. n = sp - ptr;
  213. // Scan through the range looking for the biggest block of 0xE5's we can find
  214. for (i = 0; i < n; i++) {
  215. if (*(ptr + i) == (char)0xe5) {
  216. j = how_many_E5s_are_here(ptr + i);
  217. if (j > 8) {
  218. // SERIAL_ECHOPAIR("Found ", j);
  219. // SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)(ptr + i)));
  220. i += j;
  221. block_cnt++;
  222. }
  223. }
  224. }
  225. // if (block_cnt > 1) {
  226. // SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
  227. // SERIAL_ECHOLNPAIR("\nLargest free block is ", max_cnt);
  228. // }
  229. return block_cnt;
  230. }
  231. #endif