My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

M100_Free_Mem_Chk.cpp 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. *
  30. * M100 F Identifies how much of the free memory block remains free and unused. It also
  31. * detects and reports any corruption within the free memory block that may have
  32. * happened due to errant firmware.
  33. *
  34. * M100 D Does a hex display of the free memory block along with a flag for any errant
  35. * data that does not match the expected value.
  36. *
  37. * M100 C x Corrupts x locations within the free memory block. This is useful to check the
  38. * correctness of the M100 F and M100 D commands.
  39. *
  40. * Initial version by Roxy-3D
  41. */
  42. #define M100_FREE_MEMORY_DUMPER // Enable for the `M110 D` Dump sub-command
  43. #define M100_FREE_MEMORY_CORRUPTOR // Enable for the `M100 C` Corrupt sub-command
  44. #include "MarlinConfig.h"
  45. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  46. #define TEST_BYTE 0xE5
  47. extern char* __brkval;
  48. extern size_t __heap_start, __heap_end, __flp;
  49. extern char __bss_end;
  50. #include "Marlin.h"
  51. #include "hex_print_routines.h"
  52. //
  53. // Utility functions
  54. //
  55. #define END_OF_HEAP() (__brkval ? __brkval : &__bss_end)
  56. // Location of a variable on its stack frame. Returns a value above
  57. // the stack (once the function returns to the caller).
  58. char* top_of_stack() {
  59. char x;
  60. return &x + 1; // x is pulled on return;
  61. }
  62. // Count the number of test bytes at the specified location.
  63. int16_t count_test_bytes(const char * const ptr) {
  64. for (uint16_t i = 0; i < 32000; i++)
  65. if (ptr[i] != TEST_BYTE)
  66. return i - 1;
  67. return -1;
  68. }
  69. // Return a count of free memory blocks.
  70. uint16_t free_memory_is_corrupted(char * const ptr, const uint16_t size) {
  71. // Find the longest block of test bytes in the given buffer
  72. uint16_t block_cnt = 0;
  73. for (uint16_t i = 0; i < size; i++) {
  74. if (ptr[i] == TEST_BYTE) {
  75. const uint16_t j = count_test_bytes(ptr + i);
  76. if (j > 8) {
  77. //SERIAL_ECHOPAIR("Found ", j);
  78. //SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)ptr + i));
  79. i += j;
  80. block_cnt++;
  81. }
  82. }
  83. }
  84. //if (block_cnt > 1) {
  85. // SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
  86. // SERIAL_ECHOLNPAIR("\nLargest free block is ", max_cnt);
  87. //}
  88. return block_cnt;
  89. }
  90. //
  91. // M100 sub-commands
  92. //
  93. #if ENABLED(M100_FREE_MEMORY_DUMPER)
  94. /**
  95. * M100 D
  96. * Dump the free memory block from __brkval to the stack pointer.
  97. * malloc() eats memory from the start of the block and the stack grows
  98. * up from the bottom of the block. Solid test bytes indicate nothing has
  99. * used that memory yet. There should not be anything but test bytes within
  100. * the block. If so, it may indicate memory corruption due to a bad pointer.
  101. * Unexpected bytes are flagged in the right column.
  102. */
  103. void dump_free_memory(char *ptr, char *sp) {
  104. //
  105. // Start and end the dump on a nice 16 byte boundary
  106. // (even though the values are not 16-byte aligned).
  107. //
  108. ptr = (char*)((uint16_t)ptr & 0xFFF0); // Align to 16-byte boundary
  109. sp = (char*)((uint16_t)sp | 0x000F); // Align sp to the 15th byte (at or above sp)
  110. // Dump command main loop
  111. while (ptr < sp) {
  112. print_hex_word((uint16_t)ptr); // Print the address
  113. SERIAL_CHAR(':');
  114. for (uint8_t i = 0; i < 16; i++) { // and 16 data bytes
  115. if (i == 8) SERIAL_CHAR('-');
  116. print_hex_byte(ptr[i]);
  117. SERIAL_CHAR(' ');
  118. }
  119. SERIAL_CHAR('|'); // Point out non test bytes
  120. for (uint8_t i = 0; i < 16; i++)
  121. SERIAL_CHAR(ptr[i] == TEST_BYTE ? ' ' : '?');
  122. SERIAL_EOL;
  123. ptr += 16;
  124. idle();
  125. }
  126. }
  127. #endif // M100_FREE_MEMORY_DUMPER
  128. /**
  129. * M100 F
  130. * Return the number of free bytes in the memory pool,
  131. * with other vital statistics defining the pool.
  132. */
  133. void free_memory_pool_report(const char * const ptr, const uint16_t size) {
  134. int16_t max_cnt = -1;
  135. uint16_t block_cnt = 0;
  136. char *max_addr = NULL;
  137. // Find the longest block of test bytes in the buffer
  138. for (uint16_t i = 0; i < size; i++) {
  139. char * const addr = ptr + i;
  140. if (*addr == TEST_BYTE) {
  141. const uint16_t j = count_test_bytes(addr);
  142. if (j > 8) {
  143. SERIAL_ECHOPAIR("Found ", j);
  144. SERIAL_ECHOLNPAIR(" bytes free at 0x", hex_word((uint16_t)addr));
  145. if (j > max_cnt) {
  146. max_cnt = j;
  147. max_addr = addr;
  148. }
  149. i += j;
  150. block_cnt++;
  151. }
  152. }
  153. }
  154. if (block_cnt > 1) {
  155. SERIAL_ECHOLNPGM("\nMemory Corruption detected in free memory area.");
  156. SERIAL_ECHOPAIR("\nLargest free block is ", max_cnt);
  157. SERIAL_ECHOLNPAIR(" bytes at 0x", hex_word((uint16_t)max_addr));
  158. }
  159. SERIAL_ECHOLNPAIR("free_memory_is_corrupted() = ", free_memory_is_corrupted(ptr, size));
  160. }
  161. #if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
  162. /**
  163. * M100 C<num>
  164. * Corrupt <num> locations in the free memory pool and report the corrupt addresses.
  165. * This is useful to check the correctness of the M100 D and the M100 F commands.
  166. */
  167. void corrupt_free_memory(char *ptr, const uint16_t size) {
  168. if (code_seen('C')) {
  169. ptr += 8;
  170. const uint16_t near_top = top_of_stack() - ptr - 250, // -250 to avoid interrupt activity that's altered the stack.
  171. j = near_top / (size + 1);
  172. SERIAL_ECHOLNPGM("Corrupting free memory block.\n");
  173. for (uint16_t i = 1; i <= size; i++) {
  174. char * const addr = ptr + i * j;
  175. *addr = i;
  176. SERIAL_ECHOPAIR("\nCorrupting address: 0x", hex_word((uint16_t)addr));
  177. }
  178. SERIAL_EOL;
  179. }
  180. }
  181. #endif // M100_FREE_MEMORY_CORRUPTOR
  182. /**
  183. * M100 I
  184. * Init memory for the M100 tests. (Automatically applied on the first M100.)
  185. */
  186. void init_free_memory(char *ptr, int16_t size) {
  187. SERIAL_ECHOLNPGM("Initializing free memory block.\n\n");
  188. size -= 250; // -250 to avoid interrupt activity that's altered the stack.
  189. if (size < 0) return;
  190. ptr += 8;
  191. memset(ptr, TEST_BYTE, size);
  192. SERIAL_ECHO(size);
  193. SERIAL_ECHOLNPGM(" bytes of memory initialized.\n");
  194. for (uint16_t i = 0; i < size; i++) {
  195. if (ptr[i] != TEST_BYTE) {
  196. SERIAL_ECHOPAIR("? address : 0x", hex_word((uint16_t)ptr + i));
  197. SERIAL_ECHOPAIR("=", hex_byte(ptr[i]));
  198. SERIAL_EOL; SERIAL_EOL;
  199. }
  200. }
  201. }
  202. /**
  203. * M100: Free Memory Check
  204. */
  205. void gcode_M100() {
  206. SERIAL_ECHOPAIR("\n__brkval : 0x", hex_word((uint16_t)__brkval));
  207. SERIAL_ECHOPAIR("\n__bss_end : 0x", hex_word((uint16_t)&__bss_end));
  208. char *ptr = END_OF_HEAP(), *sp = top_of_stack();
  209. SERIAL_ECHOPAIR("\nstart of free space : 0x", hex_word((uint16_t)ptr));
  210. SERIAL_ECHOLNPAIR("\nStack Pointer : 0x", hex_word((uint16_t)sp));
  211. // Always init on the first invocation of M100
  212. static bool m100_not_initialized = true;
  213. if (m100_not_initialized || code_seen('I')) {
  214. m100_not_initialized = false;
  215. init_free_memory(ptr, sp - ptr);
  216. }
  217. #if ENABLED(M100_FREE_MEMORY_DUMPER)
  218. if (code_seen('D'))
  219. return dump_free_memory(ptr, sp);
  220. #endif
  221. if (code_seen('F'))
  222. return free_memory_pool_report(ptr, sp - ptr);
  223. #if ENABLED(M100_FREE_MEMORY_CORRUPTOR)
  224. if (code_seen('C'))
  225. return corrupt_free_memory(ptr, code_value_int());
  226. #endif
  227. }
  228. #endif // M100_FREE_MEMORY_WATCHER