My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

M100.cpp 13KB

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