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.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #include "../../inc/MarlinConfig.h"
  23. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  24. #include "../gcode.h"
  25. #include "../../libs/hex_print_routines.h"
  26. #include "../../Marlin.h" // for idle()
  27. /**
  28. * M100 Free Memory Watcher
  29. *
  30. * This code watches the free memory block between the bottom of the heap and the top of the stack.
  31. * This memory block is initialized and watched via the M100 command.
  32. *
  33. * M100 I Initializes the free memory block and prints vitals statistics about the area
  34. *
  35. * M100 F Identifies how much of the free memory block remains free and unused. It also
  36. * detects and reports any corruption within the free memory block that may have
  37. * happened due to errant firmware.
  38. *
  39. * M100 D Does a hex display of the free memory block along with a flag for any errant
  40. * data that does not match the expected value.
  41. *
  42. * M100 C x Corrupts x locations within the free memory block. This is useful to check the
  43. * correctness of the M100 F and M100 D commands.
  44. *
  45. * Also, there are two support functions that can be called from a developer's C code.
  46. *
  47. * uint16_t check_for_free_memory_corruption(const char * const ptr);
  48. * void M100_dump_routine(const char * const title, const char *start, const char *end);
  49. *
  50. * Initial version by Roxy-3D
  51. */
  52. #define M100_FREE_MEMORY_DUMPER // Enable for the `M110 D` Dump sub-command
  53. #define M100_FREE_MEMORY_CORRUPTOR // Enable for the `M100 C` Corrupt sub-command
  54. #define TEST_BYTE ((char) 0xE5)
  55. extern char command_queue[BUFSIZE][MAX_CMD_SIZE];
  56. extern char* __brkval;
  57. extern size_t __heap_start, __heap_end, __flp;
  58. extern char __bss_end;
  59. //
  60. // Utility functions
  61. //
  62. #define END_OF_HEAP() (__brkval ? __brkval : &__bss_end)
  63. // Location of a variable on its stack frame. Returns a value above
  64. // the stack (once the function returns to the caller).
  65. char* top_of_stack() {
  66. char x;
  67. return &x + 1; // x is pulled on return;
  68. }
  69. // Count the number of test bytes at the specified location.
  70. inline int32_t count_test_bytes(const char * const ptr) {
  71. for (uint32_t i = 0; i < 32000; i++)
  72. if (((char) ptr[i]) != TEST_BYTE)
  73. return i - 1;
  74. return -1;
  75. }
  76. //
  77. // M100 sub-commands
  78. //
  79. #if ENABLED(M100_FREE_MEMORY_DUMPER)
  80. /**
  81. * M100 D
  82. * Dump the free memory block from __brkval to the stack pointer.
  83. * malloc() eats memory from the start of the block and the stack grows
  84. * up from the bottom of the block. Solid test bytes indicate nothing has
  85. * used that memory yet. There should not be anything but test bytes within
  86. * the block. If so, it may indicate memory corruption due to a bad pointer.
  87. * Unexpected bytes are flagged in the right column.
  88. */
  89. inline void dump_free_memory(const char *ptr, const char *sp) {
  90. //
  91. // Start and end the dump on a nice 16 byte boundary
  92. // (even though the values are not 16-byte aligned).
  93. //
  94. ptr = (char*)((ptr_int_t)((uint32_t)ptr & 0xFFFFFFF0)); // Align to 16-byte boundary
  95. sp = (char*)((ptr_int_t)((uint32_t)sp | 0x0000000F)); // Align sp to the 15th byte (at or above sp)
  96. // Dump command main loop
  97. while (ptr < sp) {
  98. print_hex_address(ptr); // Print the address
  99. SERIAL_CHAR(':');
  100. for (uint8_t i = 0; i < 16; i++) { // and 16 data bytes
  101. if (i == 8) SERIAL_CHAR('-');
  102. print_hex_byte(ptr[i]);
  103. SERIAL_CHAR(' ');
  104. }
  105. safe_delay(25);
  106. SERIAL_CHAR('|'); // Point out non test bytes
  107. for (uint8_t i = 0; i < 16; i++) {
  108. char ccc = (char)ptr[i]; // cast to char before automatically casting to char on assignment, in case the compiler is broken
  109. if (&ptr[i] >= (const char*)command_queue && &ptr[i] < (const char*)(command_queue + sizeof(command_queue))) { // Print out ASCII in the command buffer area
  110. if (!WITHIN(ccc, ' ', 0x7E)) ccc = ' ';
  111. }
  112. else { // If not in the command buffer area, flag bytes that don't match the test byte
  113. ccc = (ccc == TEST_BYTE) ? ' ' : '?';
  114. }
  115. SERIAL_CHAR(ccc);
  116. }
  117. SERIAL_EOL();
  118. ptr += 16;
  119. safe_delay(25);
  120. idle();
  121. }
  122. }
  123. void M100_dump_routine(const char * const title, const char *start, const char *end) {
  124. SERIAL_ECHOLN(title);
  125. //
  126. // Round the start and end locations to produce full lines of output
  127. //
  128. start = (char*)((ptr_int_t)((uint32_t)start & 0xFFFFFFF0)); // Align to 16-byte boundary
  129. end = (char*)((ptr_int_t)((uint32_t)end | 0x0000000F)); // Align sp to the 15th byte (at or above sp)
  130. dump_free_memory(start, end);
  131. }
  132. #endif // M100_FREE_MEMORY_DUMPER
  133. inline int check_for_free_memory_corruption(const char * const title) {
  134. SERIAL_ECHO(title);
  135. char *ptr = END_OF_HEAP(), *sp = top_of_stack();
  136. int n = sp - ptr;
  137. SERIAL_ECHOPAIR("\nfmc() n=", n);
  138. SERIAL_ECHOPAIR("\n&__brkval: ", hex_address(&__brkval));
  139. SERIAL_ECHOPAIR("=", hex_address(__brkval));
  140. SERIAL_ECHOPAIR("\n__bss_end: ", hex_address(&__bss_end));
  141. SERIAL_ECHOPAIR(" sp=", hex_address(sp));
  142. if (sp < ptr) {
  143. SERIAL_ECHOPGM(" sp < Heap ");
  144. // SET_INPUT_PULLUP(63); // if the developer has a switch wired up to their controller board
  145. // safe_delay(5); // this code can be enabled to pause the display as soon as the
  146. // while ( READ(63)) // malfunction is detected. It is currently defaulting to a switch
  147. // idle(); // being on pin-63 which is unassigend and available on most controller
  148. // safe_delay(20); // boards.
  149. // while ( !READ(63))
  150. // idle();
  151. safe_delay(20);
  152. #if ENABLED(M100_FREE_MEMORY_DUMPER)
  153. M100_dump_routine(" Memory corruption detected with sp<Heap\n", (char*)0x1B80, (char*)0x21FF);
  154. #endif
  155. }
  156. // Scan through the range looking for the biggest block of 0xE5's we can find
  157. int block_cnt = 0;
  158. for (int i = 0; i < n; i++) {
  159. if (ptr[i] == TEST_BYTE) {
  160. int32_t j = count_test_bytes(ptr + i);
  161. if (j > 8) {
  162. // SERIAL_ECHOPAIR("Found ", j);
  163. // SERIAL_ECHOLNPAIR(" bytes free at ", hex_address(ptr + i));
  164. i += j;
  165. block_cnt++;
  166. SERIAL_ECHOPAIR(" (", block_cnt);
  167. SERIAL_ECHOPAIR(") found=", j);
  168. SERIAL_ECHOPGM(" ");
  169. }
  170. }
  171. }
  172. SERIAL_ECHOPAIR(" block_found=", block_cnt);
  173. if (block_cnt != 1 || __brkval != NULL)
  174. SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
  175. if (block_cnt == 0) // Make sure the special case of no free blocks shows up as an
  176. block_cnt = -1; // error to the calling code!
  177. SERIAL_ECHOPGM(" return=");
  178. if (block_cnt == 1) {
  179. SERIAL_CHAR('0'); // if the block_cnt is 1, nothing has broken up the free memory
  180. SERIAL_EOL(); // area and it is appropriate to say 'no corruption'.
  181. return 0;
  182. }
  183. SERIAL_ECHOLNPGM("true");
  184. return block_cnt;
  185. }
  186. /**
  187. * M100 F
  188. * Return the number of free bytes in the memory pool,
  189. * with other vital statistics defining the pool.
  190. */
  191. inline void free_memory_pool_report(char * const ptr, const int32_t size) {
  192. int32_t max_cnt = -1, block_cnt = 0;
  193. char *max_addr = NULL;
  194. // Find the longest block of test bytes in the buffer
  195. for (int32_t i = 0; i < size; i++) {
  196. char *addr = ptr + i;
  197. if (*addr == TEST_BYTE) {
  198. const int32_t j = count_test_bytes(addr);
  199. if (j > 8) {
  200. SERIAL_ECHOPAIR("Found ", j);
  201. SERIAL_ECHOLNPAIR(" bytes free at ", hex_address(addr));
  202. if (j > max_cnt) {
  203. max_cnt = j;
  204. max_addr = addr;
  205. }
  206. i += j;
  207. block_cnt++;
  208. }
  209. }
  210. }
  211. if (block_cnt > 1) {
  212. SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
  213. SERIAL_ECHOPAIR("\nLargest free block is ", max_cnt);
  214. SERIAL_ECHOLNPAIR(" bytes at ", hex_address(max_addr));
  215. }
  216. SERIAL_ECHOLNPAIR("check_for_free_memory_corruption() = ", check_for_free_memory_corruption("M100 F "));
  217. }
  218. #if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
  219. /**
  220. * M100 C<num>
  221. * Corrupt <num> locations in the free memory pool and report the corrupt addresses.
  222. * This is useful to check the correctness of the M100 D and the M100 F commands.
  223. */
  224. inline void corrupt_free_memory(char *ptr, const uint32_t size) {
  225. ptr += 8;
  226. const uint32_t near_top = top_of_stack() - ptr - 250, // -250 to avoid interrupt activity that's altered the stack.
  227. j = near_top / (size + 1);
  228. SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
  229. for (uint32_t i = 1; i <= size; i++) {
  230. char * const addr = ptr + i * j;
  231. *addr = i;
  232. SERIAL_ECHOPAIR("\nCorrupting address: ", hex_address(addr));
  233. }
  234. SERIAL_EOL();
  235. }
  236. #endif // M100_FREE_MEMORY_CORRUPTOR
  237. /**
  238. * M100 I
  239. * Init memory for the M100 tests. (Automatically applied on the first M100.)
  240. */
  241. inline void init_free_memory(char *ptr, int32_t size) {
  242. SERIAL_ECHOLNPGM("Initializing free memory block.\n\n");
  243. size -= 250; // -250 to avoid interrupt activity that's altered the stack.
  244. if (size < 0) {
  245. SERIAL_ECHOLNPGM("Unable to initialize.\n");
  246. return;
  247. }
  248. ptr += 8; // move a few bytes away from the heap just because we don't want
  249. // to be altering memory that close to it.
  250. memset(ptr, TEST_BYTE, size);
  251. SERIAL_ECHO(size);
  252. SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
  253. for (int32_t i = 0; i < size; i++) {
  254. if (ptr[i] != TEST_BYTE) {
  255. SERIAL_ECHOPAIR("? address : ", hex_address(ptr + i));
  256. SERIAL_ECHOLNPAIR("=", hex_byte(ptr[i]));
  257. SERIAL_EOL();
  258. }
  259. }
  260. }
  261. /**
  262. * M100: Free Memory Check
  263. */
  264. void GcodeSuite::M100() {
  265. SERIAL_ECHOPAIR("\n__brkval : ", hex_address(__brkval));
  266. SERIAL_ECHOPAIR("\n__bss_end : ", hex_address(&__bss_end));
  267. char *ptr = END_OF_HEAP(), *sp = top_of_stack();
  268. SERIAL_ECHOPAIR("\nstart of free space : ", hex_address(ptr));
  269. SERIAL_ECHOLNPAIR("\nStack Pointer : ", hex_address(sp));
  270. // Always init on the first invocation of M100
  271. static bool m100_not_initialized = true;
  272. if (m100_not_initialized || parser.seen('I')) {
  273. m100_not_initialized = false;
  274. init_free_memory(ptr, sp - ptr);
  275. }
  276. #if ENABLED(M100_FREE_MEMORY_DUMPER)
  277. if (parser.seen('D'))
  278. return dump_free_memory(ptr, sp);
  279. #endif
  280. if (parser.seen('F'))
  281. return free_memory_pool_report(ptr, sp - ptr);
  282. #if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
  283. if (parser.seen('C'))
  284. return corrupt_free_memory(ptr, parser.value_int());
  285. #endif
  286. }
  287. #endif // M100_FREE_MEMORY_WATCHER