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.

gcode_d.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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/MarlinConfigPre.h"
  23. #if ENABLED(MARLIN_DEV_MODE)
  24. #include "gcode.h"
  25. #include "../module/settings.h"
  26. #include "../module/temperature.h"
  27. #include "../libs/hex_print.h"
  28. #include "../HAL/shared/eeprom_if.h"
  29. #include "../HAL/shared/Delay.h"
  30. #include "../sd/cardreader.h"
  31. #include "../MarlinCore.h" // for kill
  32. extern void dump_delay_accuracy_check();
  33. /**
  34. * Dn: G-code for development and testing
  35. *
  36. * See https://reprap.org/wiki/G-code#D:_Debug_codes
  37. *
  38. * Put whatever else you need here to test ongoing development.
  39. */
  40. void GcodeSuite::D(const int16_t dcode) {
  41. switch (dcode) {
  42. case -1:
  43. for (;;) { /* loop forever (watchdog reset) */ }
  44. case 0:
  45. HAL_reboot();
  46. break;
  47. case 10:
  48. kill(PSTR("D10"), PSTR("KILL TEST"), parser.seen('P'));
  49. break;
  50. case 1: {
  51. // Zero or pattern-fill the EEPROM data
  52. #if ENABLED(EEPROM_SETTINGS)
  53. persistentStore.access_start();
  54. size_t total = persistentStore.capacity();
  55. int pos = 0;
  56. const uint8_t value = 0x0;
  57. while (total--) persistentStore.write_data(pos, &value, 1);
  58. persistentStore.access_finish();
  59. #else
  60. settings.reset();
  61. settings.save();
  62. #endif
  63. HAL_reboot();
  64. } break;
  65. case 2: { // D2 Read / Write SRAM
  66. #define SRAM_SIZE 8192
  67. uint8_t *pointer = parser.hex_adr_val('A');
  68. uint16_t len = parser.ushortval('C', 1);
  69. uintptr_t addr = (uintptr_t)pointer;
  70. NOMORE(addr, size_t(SRAM_SIZE - 1));
  71. NOMORE(len, SRAM_SIZE - addr);
  72. if (parser.seenval('X')) {
  73. // Write the hex bytes after the X
  74. uint16_t val = parser.hex_val('X');
  75. while (len--) {
  76. *pointer = val;
  77. pointer++;
  78. }
  79. }
  80. else {
  81. while (len--) print_hex_byte(*(pointer++));
  82. SERIAL_EOL();
  83. }
  84. } break;
  85. #if ENABLED(EEPROM_SETTINGS)
  86. case 3: { // D3 Read / Write EEPROM
  87. uint8_t *pointer = parser.hex_adr_val('A');
  88. uint16_t len = parser.ushortval('C', 1);
  89. uintptr_t addr = (uintptr_t)pointer;
  90. NOMORE(addr, size_t(persistentStore.capacity() - 1));
  91. NOMORE(len, persistentStore.capacity() - addr);
  92. if (parser.seenval('X')) {
  93. uint16_t val = parser.hex_val('X');
  94. #if ENABLED(EEPROM_SETTINGS)
  95. persistentStore.access_start();
  96. while (len--) {
  97. int pos = 0;
  98. persistentStore.write_data(pos, (uint8_t *)&val, sizeof(val));
  99. }
  100. SERIAL_EOL();
  101. persistentStore.access_finish();
  102. #else
  103. SERIAL_ECHOLNPGM("NO EEPROM");
  104. #endif
  105. }
  106. else {
  107. // Read bytes from EEPROM
  108. #if ENABLED(EEPROM_SETTINGS)
  109. persistentStore.access_start();
  110. int pos = 0;
  111. uint8_t val;
  112. while (len--) if (!persistentStore.read_data(pos, &val, 1)) print_hex_byte(val);
  113. SERIAL_EOL();
  114. persistentStore.access_finish();
  115. #else
  116. SERIAL_ECHOLNPGM("NO EEPROM");
  117. len = 0;
  118. #endif
  119. SERIAL_EOL();
  120. }
  121. } break;
  122. #endif
  123. case 4: { // D4 Read / Write PIN
  124. //const bool is_out = parser.boolval('F');
  125. //const uint8_t pin = parser.byteval('P'),
  126. // val = parser.byteval('V', LOW);
  127. if (parser.seenval('X')) {
  128. // TODO: Write the hex bytes after the X
  129. //while (len--) {
  130. //}
  131. }
  132. else {
  133. //while (len--) {
  134. //// TODO: Read bytes from EEPROM
  135. // print_hex_byte(eeprom_read_byte(adr++));
  136. //}
  137. SERIAL_EOL();
  138. }
  139. } break;
  140. case 5: { // D5 Read / Write onboard Flash
  141. #define FLASH_SIZE 1024
  142. uint8_t *pointer = parser.hex_adr_val('A');
  143. uint16_t len = parser.ushortval('C', 1);
  144. uintptr_t addr = (uintptr_t)pointer;
  145. NOMORE(addr, size_t(FLASH_SIZE - 1));
  146. NOMORE(len, FLASH_SIZE - addr);
  147. if (parser.seenval('X')) {
  148. // TODO: Write the hex bytes after the X
  149. //while (len--) {}
  150. }
  151. else {
  152. //while (len--) {
  153. //// TODO: Read bytes from EEPROM
  154. // print_hex_byte(eeprom_read_byte(adr++));
  155. //}
  156. SERIAL_EOL();
  157. }
  158. } break;
  159. case 6: // D6 Check delay loop accuracy
  160. dump_delay_accuracy_check();
  161. break;
  162. case 7: // D7 dump the current serial port type (hence configuration)
  163. SERIAL_ECHOLNPAIR("Current serial configuration RX_BS:", RX_BUFFER_SIZE, ", TX_BS:", TX_BUFFER_SIZE);
  164. SERIAL_ECHOLN(gtn(&SERIAL_IMPL));
  165. break;
  166. case 100: { // D100 Disable heaters and attempt a hard hang (Watchdog Test)
  167. SERIAL_ECHOLNPGM("Disabling heaters and attempting to trigger Watchdog");
  168. SERIAL_ECHOLNPGM("(USE_WATCHDOG " TERN(USE_WATCHDOG, "ENABLED", "DISABLED") ")");
  169. thermalManager.disable_all_heaters();
  170. delay(1000); // Allow time to print
  171. DISABLE_ISRS();
  172. // Use a low-level delay that does not rely on interrupts to function
  173. // Do not spin forever, to avoid thermal risks if heaters are enabled and
  174. // watchdog does not work.
  175. for (int i = 10000; i--;) DELAY_US(1000UL);
  176. ENABLE_ISRS();
  177. SERIAL_ECHOLNPGM("FAILURE: Watchdog did not trigger board reset.");
  178. } break;
  179. #if ENABLED(SDSUPPORT)
  180. case 101: { // D101 Test SD Write
  181. card.openFileWrite("test.gco");
  182. if (!card.isFileOpen()) {
  183. SERIAL_ECHOLNPAIR("Failed to open test.gco to write.");
  184. return;
  185. }
  186. __attribute__((aligned(sizeof(size_t)))) uint8_t buf[512];
  187. uint16_t c;
  188. for (c = 0; c < COUNT(buf); c++)
  189. buf[c] = 'A' + (c % ('Z' - 'A'));
  190. c = 1024 * 4;
  191. while (c--) {
  192. TERN_(USE_WATCHDOG, watchdog_refresh());
  193. card.write(buf, COUNT(buf));
  194. }
  195. SERIAL_ECHOLNPGM(" done");
  196. card.closefile();
  197. } break;
  198. case 102: { // D102 Test SD Read
  199. char testfile[] = "test.gco";
  200. card.openFileRead(testfile);
  201. if (!card.isFileOpen()) {
  202. SERIAL_ECHOLNPAIR("Failed to open test.gco to read.");
  203. return;
  204. }
  205. __attribute__((aligned(sizeof(size_t)))) uint8_t buf[512];
  206. uint16_t c = 1024 * 4;
  207. while (c--) {
  208. TERN_(USE_WATCHDOG, watchdog_refresh());
  209. card.read(buf, COUNT(buf));
  210. bool error = false;
  211. for (uint16_t i = 0; i < COUNT(buf); i++) {
  212. if (buf[i] != ('A' + (i % ('Z' - 'A')))) {
  213. error = true;
  214. break;
  215. }
  216. }
  217. if (error) {
  218. SERIAL_ECHOLNPGM(" Read error!");
  219. break;
  220. }
  221. }
  222. SERIAL_ECHOLNPGM(" done");
  223. card.closefile();
  224. } break;
  225. #endif // SDSUPPORT
  226. #if ENABLED(POSTMORTEM_DEBUGGING)
  227. case 451: { // Trigger all kind of faults to test exception catcher
  228. SERIAL_ECHOLNPGM("Disabling heaters");
  229. thermalManager.disable_all_heaters();
  230. delay(1000); // Allow time to print
  231. volatile uint8_t type[5] = { parser.byteval('T', 1) };
  232. // The code below is obviously wrong and it's full of quirks to fool the compiler from optimizing away the code
  233. switch (type[0]) {
  234. case 1: default: *(int*)0 = 451; break; // Write at bad address
  235. case 2: { volatile int a = 0; volatile int b = 452 / a; *(int*)&a = b; } break; // Divide by zero (some CPUs accept this, like ARM)
  236. case 3: { *(uint32_t*)&type[1] = 453; volatile int a = *(int*)&type[1]; type[0] = a / 255; } break; // Unaligned access (some CPUs accept this)
  237. case 4: { volatile void (*func)() = (volatile void (*)()) 0xE0000000; func(); } break; // Invalid instruction
  238. }
  239. break;
  240. }
  241. #endif
  242. }
  243. }
  244. #endif