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

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