My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

M100_Free_Mem_Chk.cpp 11KB

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