My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

M100_Free_Mem_Chk.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_ECHOPGM("\nbss_end : ");
  73. prt_hex_word((unsigned int) ptr);
  74. ptr = (char*)((unsigned long) ptr & 0xfff0);
  75. sp = top_of_stack();
  76. SERIAL_ECHOPGM("\nStack Pointer : ");
  77. prt_hex_word((unsigned int) sp);
  78. SERIAL_EOL;
  79. sp = (char*)((unsigned long) sp | 0x000f);
  80. n = sp - ptr;
  81. //
  82. // This is the main loop of the Dump command.
  83. //
  84. while (ptr < sp) {
  85. prt_hex_word((unsigned int) ptr); // Print the address
  86. SERIAL_CHAR(':');
  87. for (i = 0; i < 16; i++) { // and 16 data bytes
  88. prt_hex_byte(*(ptr + i));
  89. SERIAL_CHAR(' ');
  90. }
  91. SERIAL_CHAR('|'); // now show where non 0xE5's are
  92. for (i = 0; i < 16; i++) {
  93. if (*(ptr + i) == (char)0xe5)
  94. SERIAL_CHAR(' ');
  95. else
  96. SERIAL_CHAR('?');
  97. }
  98. SERIAL_EOL;
  99. ptr += 16;
  100. }
  101. return;
  102. }
  103. #endif
  104. //
  105. // M100 F requests the code to return the number of free bytes in the memory pool along with
  106. // other vital statistics that define the memory pool.
  107. //
  108. if (code_seen('F')) {
  109. #if 0
  110. int max_addr = (int) __brkval ? __brkval : &__bss_end;
  111. int max_cnt = 0;
  112. #endif
  113. int block_cnt = 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_ECHOPGM(" bytes free at 0x");
  124. prt_hex_word((int) ptr + i);
  125. SERIAL_EOL;
  126. i += j;
  127. block_cnt++;
  128. }
  129. #if 0
  130. if (j > max_cnt) { // We don't do anything with this information yet
  131. max_cnt = j; // but we do know where the biggest free memory block is.
  132. max_addr = (int) ptr + i;
  133. }
  134. #endif
  135. }
  136. }
  137. if (block_cnt > 1)
  138. SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
  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 = __brkval ? __brkval : &__bss_end;
  150. SERIAL_ECHOPAIR("\nbss_end : ", ptr);
  151. ptr += 8;
  152. sp = top_of_stack();
  153. SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
  154. SERIAL_ECHOLNPGM("\n");
  155. n = sp - ptr - 64; // -64 just to keep us from finding interrupt activity that
  156. // has altered the stack.
  157. j = n / (x + 1);
  158. for (i = 1; i <= x; i++) {
  159. *(ptr + (i * j)) = i;
  160. SERIAL_ECHOPGM("\nCorrupting address: 0x");
  161. prt_hex_word((unsigned int)(ptr + (i * j)));
  162. }
  163. SERIAL_ECHOLNPGM("\n");
  164. return;
  165. }
  166. #endif
  167. //
  168. // M100 I Initializes the free memory pool so it can be watched and prints vital
  169. // statistics that define the free memory pool.
  170. //
  171. if (m100_not_initialized || code_seen('I')) { // If no sub-command is specified, the first time
  172. SERIAL_ECHOLNPGM("Initializing free memory block.\n"); // this happens, it will Initialize.
  173. ptr = __brkval ? __brkval : &__bss_end; // Repeated M100 with no sub-command will not destroy the
  174. SERIAL_ECHOPAIR("\nbss_end : ", ptr); // state of the initialized free memory pool.
  175. ptr += 8;
  176. sp = top_of_stack();
  177. SERIAL_ECHOPAIR("\nStack Pointer : ", sp);
  178. SERIAL_ECHOLNPGM("\n");
  179. n = sp - ptr - 64; // -64 just to keep us from finding interrupt activity that
  180. // has altered the stack.
  181. SERIAL_ECHO(n);
  182. SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
  183. for (i = 0; i < n; i++)
  184. *(ptr + i) = (char)0xe5;
  185. for (i = 0; i < n; i++) {
  186. if (*(ptr + i) != (char)0xe5) {
  187. SERIAL_ECHOPAIR("? address : ", ptr + i);
  188. SERIAL_ECHOPAIR("=", *(ptr + i));
  189. SERIAL_ECHOLNPGM("\n");
  190. }
  191. }
  192. m100_not_initialized = false;
  193. return;
  194. }
  195. return;
  196. }
  197. // top_of_stack() returns the location of a variable on its stack frame. The value returned is above
  198. // the stack once the function returns to the caller.
  199. char* top_of_stack() {
  200. char x;
  201. return &x + 1; // x is pulled on return;
  202. }
  203. // how_many_E5s_are_here() is a utility function to easily find out how many 0xE5's are
  204. // at the specified location. Having this logic as a function simplifies the search code.
  205. //
  206. int how_many_E5s_are_here(char* p) {
  207. int n;
  208. for (n = 0; n < 32000; n++) {
  209. if (*(p + n) != (char)0xe5)
  210. return n - 1;
  211. }
  212. return -1;
  213. }
  214. #endif