My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

M100_Free_Mem_Chk.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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-3DPrintBoard
  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 void* __brkval;
  44. extern size_t __heap_start, __heap_end, __flp;
  45. //
  46. // Declare all the functions we need from Marlin_Main.cpp to do the work!
  47. //
  48. float code_value();
  49. long code_value_long();
  50. bool code_seen(char);
  51. void serial_echopair_P(const char*, float);
  52. void serial_echopair_P(const char*, double);
  53. void serial_echopair_P(const char*, unsigned long);
  54. void serial_echopair_P(const char*, int);
  55. void serial_echopair_P(const char*, long);
  56. //
  57. // Utility functions used by M100 to get its work done.
  58. //
  59. unsigned char* top_of_stack();
  60. void prt_hex_nibble(unsigned int);
  61. void prt_hex_byte(unsigned int);
  62. void prt_hex_word(unsigned int);
  63. int how_many_E5s_are_here(unsigned char*);
  64. void gcode_M100() {
  65. static int m100_not_initialized = 1;
  66. unsigned char* sp, *ptr;
  67. int i, j, n;
  68. //
  69. // M100 D dumps the free memory block from __brkval to the stack pointer.
  70. // malloc() eats memory from the start of the block and the stack grows
  71. // up from the bottom of the block. Solid 0xE5's indicate nothing has
  72. // used that memory yet. There should not be anything but 0xE5's within
  73. // the block of 0xE5's. If there is, that would indicate memory corruption
  74. // probably caused by bad pointers. Any unexpected values will be flagged in
  75. // the right hand column to help spotting them.
  76. //
  77. #if ENABLED(M100_FREE_MEMORY_DUMPER) // Disable to remove Dump sub-command
  78. if (code_seen('D')) {
  79. ptr = (unsigned char*) __brkval;
  80. //
  81. // We want to start and end the dump on a nice 16 byte boundry even though
  82. // the values we are using are not 16 byte aligned.
  83. //
  84. SERIAL_ECHOPGM("\n__brkval : ");
  85. prt_hex_word((unsigned int) ptr);
  86. ptr = (unsigned char*)((unsigned long) ptr & 0xfff0);
  87. sp = top_of_stack();
  88. SERIAL_ECHOPGM("\nStack Pointer : ");
  89. prt_hex_word((unsigned int) sp);
  90. SERIAL_ECHOPGM("\n");
  91. sp = (unsigned char*)((unsigned long) sp | 0x000f);
  92. n = sp - ptr;
  93. //
  94. // This is the main loop of the Dump command.
  95. //
  96. while (ptr < sp) {
  97. prt_hex_word((unsigned int) ptr); // Print the address
  98. SERIAL_ECHOPGM(":");
  99. for (i = 0; i < 16; i++) { // and 16 data bytes
  100. prt_hex_byte(*(ptr + i));
  101. SERIAL_ECHOPGM(" ");
  102. delay(2);
  103. }
  104. SERIAL_ECHO("|"); // now show where non 0xE5's are
  105. for (i = 0; i < 16; i++) {
  106. delay(2);
  107. if (*(ptr + i) == 0xe5)
  108. SERIAL_ECHOPGM(" ");
  109. else
  110. SERIAL_ECHOPGM("?");
  111. }
  112. SERIAL_ECHO("\n");
  113. ptr += 16;
  114. delay(2);
  115. }
  116. SERIAL_ECHOLNPGM("Done.\n");
  117. return;
  118. }
  119. #endif
  120. //
  121. // M100 F requests the code to return the number of free bytes in the memory pool along with
  122. // other vital statistics that define the memory pool.
  123. //
  124. if (code_seen('F')) {
  125. int max_addr = (int) __brkval;
  126. int max_cnt = 0;
  127. int block_cnt = 0;
  128. ptr = (unsigned char*) __brkval;
  129. sp = top_of_stack();
  130. n = sp - ptr;
  131. // Scan through the range looking for the biggest block of 0xE5's we can find
  132. for (i = 0; i < n; i++) {
  133. if (*(ptr + i) == (unsigned char) 0xe5) {
  134. j = how_many_E5s_are_here((unsigned char*) ptr + i);
  135. if (j > 8) {
  136. SERIAL_ECHOPAIR("Found ", j);
  137. SERIAL_ECHOPGM(" bytes free at 0x");
  138. prt_hex_word((int) ptr + i);
  139. SERIAL_ECHOPGM("\n");
  140. i += j;
  141. block_cnt++;
  142. }
  143. if (j > max_cnt) { // We don't do anything with this information yet
  144. max_cnt = j; // but we do know where the biggest free memory block is.
  145. max_addr = (int) ptr + i;
  146. }
  147. }
  148. }
  149. if (block_cnt > 1)
  150. SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.\n");
  151. SERIAL_ECHO("\nDone.\n");
  152. return;
  153. }
  154. //
  155. // M100 C x Corrupts x locations in the free memory pool and reports the locations of the corruption.
  156. // This is useful to check the correctness of the M100 D and the M100 F commands.
  157. //
  158. #if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
  159. if (code_seen('C')) {
  160. int x; // x gets the # of locations to corrupt within the memory pool
  161. x = code_value();
  162. SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
  163. ptr = (unsigned char*) __brkval;
  164. SERIAL_ECHOPAIR("\n__brkval : ", (long) ptr);
  165. ptr += 8;
  166. sp = top_of_stack();
  167. SERIAL_ECHOPAIR("\nStack Pointer : ", (long) 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. j = n / (x + 1);
  172. for (i = 1; i <= x; i++) {
  173. *(ptr + (i * j)) = i;
  174. SERIAL_ECHO("\nCorrupting address: 0x");
  175. prt_hex_word((unsigned int)(ptr + (i * j)));
  176. }
  177. SERIAL_ECHOLNPGM("\n");
  178. return;
  179. }
  180. #endif
  181. //
  182. // M100 I Initializes the free memory pool so it can be watched and prints vital
  183. // statistics that define the free memory pool.
  184. //
  185. if (m100_not_initialized || code_seen('I')) { // If no sub-command is specified, the first time
  186. SERIAL_ECHOLNPGM("Initializing free memory block.\n"); // this happens, it will Initialize.
  187. ptr = (unsigned char*) __brkval; // Repeated M100 with no sub-command will not destroy the
  188. SERIAL_ECHOPAIR("\n__brkval : ", (long) ptr); // state of the initialized free memory pool.
  189. ptr += 8;
  190. sp = top_of_stack();
  191. SERIAL_ECHOPAIR("\nStack Pointer : ", (long) sp);
  192. SERIAL_ECHOLNPGM("\n");
  193. n = sp - ptr - 64; // -64 just to keep us from finding interrupt activity that
  194. // has altered the stack.
  195. SERIAL_ECHO(n);
  196. SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
  197. for (i = 0; i < n; i++)
  198. *(ptr + i) = (unsigned char) 0xe5;
  199. for (i = 0; i < n; i++) {
  200. if (*(ptr + i) != (unsigned char) 0xe5) {
  201. SERIAL_ECHOPAIR("? address : ", (unsigned long) ptr + i);
  202. SERIAL_ECHOPAIR("=", *(ptr + i));
  203. SERIAL_ECHOLNPGM("\n");
  204. }
  205. }
  206. m100_not_initialized = 0;
  207. SERIAL_ECHOLNPGM("Done.\n");
  208. return;
  209. }
  210. return;
  211. }
  212. // top_of_stack() returns the location of a variable on its stack frame. The value returned is above
  213. // the stack once the function returns to the caller.
  214. unsigned char* top_of_stack() {
  215. unsigned char x;
  216. return &x + 1; // x is pulled on return;
  217. }
  218. //
  219. // 3 support routines to print hex numbers. We can print a nibble, byte and word
  220. //
  221. void prt_hex_nibble(unsigned int n) {
  222. if (n <= 9)
  223. SERIAL_ECHO(n);
  224. else
  225. SERIAL_ECHO((char)('A' + n - 10));
  226. delay(2);
  227. }
  228. void prt_hex_byte(unsigned int b) {
  229. prt_hex_nibble((b & 0xf0) >> 4);
  230. prt_hex_nibble(b & 0x0f);
  231. }
  232. void prt_hex_word(unsigned int w) {
  233. prt_hex_byte((w & 0xff00) >> 8);
  234. prt_hex_byte(w & 0x0ff);
  235. }
  236. // how_many_E5s_are_here() is a utility function to easily find out how many 0xE5's are
  237. // at the specified location. Having this logic as a function simplifies the search code.
  238. //
  239. int how_many_E5s_are_here(unsigned char* p) {
  240. int n;
  241. for (n = 0; n < 32000; n++) {
  242. if (*(p + n) != (unsigned char) 0xe5)
  243. return n - 1;
  244. }
  245. return -1;
  246. }
  247. #endif