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.

Marlin.cpp 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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. * About Marlin
  24. *
  25. * This firmware is a mashup between Sprinter and grbl.
  26. * - https://github.com/kliment/Sprinter
  27. * - https://github.com/grbl/grbl
  28. */
  29. #include "Marlin.h"
  30. #include "core/utility.h"
  31. #include "lcd/ultralcd.h"
  32. #include "module/motion.h"
  33. #include "module/planner.h"
  34. #include "module/stepper.h"
  35. #include "module/endstops.h"
  36. #include "module/probe.h"
  37. #include "module/temperature.h"
  38. #include "sd/cardreader.h"
  39. #include "module/configuration_store.h"
  40. #include "module/printcounter.h" // PrintCounter or Stopwatch
  41. #include "feature/closedloop.h"
  42. #include "HAL/shared/Delay.h"
  43. #include "module/stepper/indirection.h"
  44. #ifdef ARDUINO
  45. #include <pins_arduino.h>
  46. #endif
  47. #include <math.h>
  48. #include "libs/nozzle.h"
  49. #include "gcode/gcode.h"
  50. #include "gcode/parser.h"
  51. #include "gcode/queue.h"
  52. #if ENABLED(TOUCH_BUTTONS)
  53. #include "feature/touch/xpt2046.h"
  54. #endif
  55. #if ENABLED(HOST_ACTION_COMMANDS)
  56. #include "feature/host_actions.h"
  57. #endif
  58. #if USE_BEEPER
  59. #include "libs/buzzer.h"
  60. #endif
  61. #if ENABLED(DIGIPOT_I2C)
  62. #include "feature/digipot/digipot.h"
  63. #endif
  64. #if ENABLED(MIXING_EXTRUDER)
  65. #include "feature/mixing.h"
  66. #endif
  67. #if ENABLED(MAX7219_DEBUG)
  68. #include "feature/Max7219_Debug_LEDs.h"
  69. #endif
  70. #if HAS_COLOR_LEDS
  71. #include "feature/leds/leds.h"
  72. #endif
  73. #if ENABLED(BLTOUCH)
  74. #include "feature/bltouch.h"
  75. #endif
  76. #if ENABLED(POLL_JOG)
  77. #include "feature/joystick.h"
  78. #endif
  79. #if HAS_SERVOS
  80. #include "module/servo.h"
  81. #endif
  82. #if ENABLED(DAC_STEPPER_CURRENT)
  83. #include "feature/dac/stepper_dac.h"
  84. #endif
  85. #if ENABLED(EXPERIMENTAL_I2CBUS)
  86. #include "feature/twibus.h"
  87. TWIBus i2c;
  88. #endif
  89. #if ENABLED(I2C_POSITION_ENCODERS)
  90. #include "feature/I2CPositionEncoder.h"
  91. #endif
  92. #if HAS_TRINAMIC && DISABLED(PS_DEFAULT_OFF)
  93. #include "feature/tmc_util.h"
  94. #endif
  95. #if HAS_CUTTER
  96. #include "feature/spindle_laser.h"
  97. #endif
  98. #if ENABLED(SDSUPPORT)
  99. CardReader card;
  100. #endif
  101. #if ENABLED(G38_PROBE_TARGET)
  102. uint8_t G38_move; // = 0
  103. bool G38_did_trigger; // = false
  104. #endif
  105. #if ENABLED(DELTA)
  106. #include "module/delta.h"
  107. #elif IS_SCARA
  108. #include "module/scara.h"
  109. #endif
  110. #if HAS_LEVELING
  111. #include "feature/bedlevel/bedlevel.h"
  112. #endif
  113. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  114. #include "feature/pause.h"
  115. #endif
  116. #if ENABLED(POWER_LOSS_RECOVERY)
  117. #include "feature/power_loss_recovery.h"
  118. #endif
  119. #if HAS_FILAMENT_SENSOR
  120. #include "feature/runout.h"
  121. #endif
  122. #if ENABLED(TEMP_STAT_LEDS)
  123. #include "feature/leds/tempstat.h"
  124. #endif
  125. #if HAS_CASE_LIGHT
  126. #include "feature/caselight.h"
  127. #endif
  128. #if HAS_FANMUX
  129. #include "feature/fanmux.h"
  130. #endif
  131. #if DO_SWITCH_EXTRUDER || ANY(SWITCHING_NOZZLE, PARKING_EXTRUDER, MAGNETIC_PARKING_EXTRUDER, ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  132. #include "module/tool_change.h"
  133. #endif
  134. #if ENABLED(USE_CONTROLLER_FAN)
  135. #include "feature/controllerfan.h"
  136. #endif
  137. #if ENABLED(PRUSA_MMU2)
  138. #include "feature/prusa_MMU2/mmu2.h"
  139. #endif
  140. #if ENABLED(EXTENSIBLE_UI)
  141. #include "lcd/extensible_ui/ui_api.h"
  142. #endif
  143. #if HAS_DRIVER(L6470)
  144. #include "libs/L6470/L6470_Marlin.h"
  145. #endif
  146. bool Running = true;
  147. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  148. bool wait_for_heatup = true;
  149. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  150. #if HAS_RESUME_CONTINUE
  151. bool wait_for_user; // = false;
  152. #endif
  153. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  154. bool suspend_auto_report; // = false
  155. #endif
  156. // Inactivity shutdown
  157. millis_t max_inactive_time, // = 0
  158. stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
  159. #if PIN_EXISTS(CHDK)
  160. extern millis_t chdk_timeout;
  161. #endif
  162. #if ENABLED(I2C_POSITION_ENCODERS)
  163. I2CPositionEncodersMgr I2CPEM;
  164. #endif
  165. /**
  166. * ***************************************************************************
  167. * ******************************** FUNCTIONS ********************************
  168. * ***************************************************************************
  169. */
  170. void setup_killpin() {
  171. #if HAS_KILL
  172. SET_INPUT_PULLUP(KILL_PIN);
  173. #endif
  174. }
  175. void setup_powerhold() {
  176. #if HAS_SUICIDE
  177. OUT_WRITE(SUICIDE_PIN, HIGH);
  178. #endif
  179. #if HAS_POWER_SWITCH
  180. #if ENABLED(PS_DEFAULT_OFF)
  181. powersupply_on = true; PSU_OFF();
  182. #else
  183. powersupply_on = false; PSU_ON();
  184. #endif
  185. #endif
  186. }
  187. /**
  188. * Stepper Reset (RigidBoard, et.al.)
  189. */
  190. #if HAS_STEPPER_RESET
  191. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  192. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  193. #endif
  194. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  195. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  196. i2c.receive(bytes);
  197. }
  198. void i2c_on_request() { // just send dummy data for now
  199. i2c.reply("Hello World!\n");
  200. }
  201. #endif
  202. /**
  203. * Sensitive pin test for M42, M226
  204. */
  205. #include "pins/sensitive_pins.h"
  206. bool pin_is_protected(const pin_t pin) {
  207. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  208. for (uint8_t i = 0; i < COUNT(sensitive_pins); i++) {
  209. pin_t sensitive_pin;
  210. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  211. if (pin == sensitive_pin) return true;
  212. }
  213. return false;
  214. }
  215. void protected_pin_err() {
  216. SERIAL_ERROR_MSG(MSG_ERR_PROTECTED_PIN);
  217. }
  218. void quickstop_stepper() {
  219. planner.quick_stop();
  220. planner.synchronize();
  221. set_current_from_steppers_for_axis(ALL_AXES);
  222. sync_plan_position();
  223. }
  224. void enable_all_steppers() {
  225. #if ENABLED(AUTO_POWER_CONTROL)
  226. powerManager.power_on();
  227. #endif
  228. enable_X();
  229. enable_Y();
  230. enable_Z();
  231. enable_E0();
  232. enable_E1();
  233. enable_E2();
  234. enable_E3();
  235. enable_E4();
  236. enable_E5();
  237. }
  238. void enable_e_steppers() {
  239. enable_E0();
  240. enable_E1();
  241. enable_E2();
  242. enable_E3();
  243. enable_E4();
  244. enable_E5();
  245. }
  246. void disable_e_steppers() {
  247. disable_E0();
  248. disable_E1();
  249. disable_E2();
  250. disable_E3();
  251. disable_E4();
  252. disable_E5();
  253. }
  254. void disable_e_stepper(const uint8_t e) {
  255. switch (e) {
  256. case 0: disable_E0(); break;
  257. case 1: disable_E1(); break;
  258. case 2: disable_E2(); break;
  259. case 3: disable_E3(); break;
  260. case 4: disable_E4(); break;
  261. case 5: disable_E5(); break;
  262. }
  263. }
  264. void disable_all_steppers() {
  265. disable_X();
  266. disable_Y();
  267. disable_Z();
  268. disable_e_steppers();
  269. }
  270. #if HAS_FILAMENT_SENSOR
  271. void event_filament_runout() {
  272. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  273. if (did_pause_print) return; // Action already in progress. Purge triggered repeated runout.
  274. #endif
  275. #if ENABLED(EXTENSIBLE_UI)
  276. ExtUI::onFilamentRunout(ExtUI::getActiveTool());
  277. #endif
  278. #if EITHER(HOST_PROMPT_SUPPORT, HOST_ACTION_COMMANDS)
  279. const char tool = '0'
  280. #if NUM_RUNOUT_SENSORS > 1
  281. + active_extruder
  282. #endif
  283. ;
  284. #endif
  285. //action:out_of_filament
  286. #if ENABLED(HOST_PROMPT_SUPPORT)
  287. host_prompt_reason = PROMPT_FILAMENT_RUNOUT;
  288. host_action_prompt_end();
  289. host_action_prompt_begin(PSTR("FilamentRunout T"), false);
  290. SERIAL_CHAR(tool);
  291. SERIAL_EOL();
  292. host_action_prompt_show();
  293. #endif
  294. const bool run_runout_script = !runout.host_handling;
  295. #if ENABLED(HOST_ACTION_COMMANDS)
  296. if (run_runout_script
  297. && ( strstr(FILAMENT_RUNOUT_SCRIPT, "M600")
  298. || strstr(FILAMENT_RUNOUT_SCRIPT, "M125")
  299. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  300. || strstr(FILAMENT_RUNOUT_SCRIPT, "M25")
  301. #endif
  302. )
  303. ) {
  304. host_action_paused(false);
  305. }
  306. else {
  307. // Legacy Repetier command for use until newer version supports standard dialog
  308. // To be removed later when pause command also triggers dialog
  309. #ifdef ACTION_ON_FILAMENT_RUNOUT
  310. host_action(PSTR(ACTION_ON_FILAMENT_RUNOUT " T"), false);
  311. SERIAL_CHAR(tool);
  312. SERIAL_EOL();
  313. #endif
  314. host_action_pause(false);
  315. }
  316. SERIAL_ECHOPGM(" " ACTION_REASON_ON_FILAMENT_RUNOUT " ");
  317. SERIAL_CHAR(tool);
  318. SERIAL_EOL();
  319. #endif // HOST_ACTION_COMMANDS
  320. if (run_runout_script)
  321. queue.inject_P(PSTR(FILAMENT_RUNOUT_SCRIPT));
  322. }
  323. #endif // HAS_FILAMENT_SENSOR
  324. #if ENABLED(G29_RETRY_AND_RECOVER)
  325. void event_probe_failure() {
  326. #ifdef ACTION_ON_G29_FAILURE
  327. host_action(PSTR(ACTION_ON_G29_FAILURE));
  328. #endif
  329. #ifdef G29_FAILURE_COMMANDS
  330. gcode.process_subcommands_now_P(PSTR(G29_FAILURE_COMMANDS));
  331. #endif
  332. #if ENABLED(G29_HALT_ON_FAILURE)
  333. #ifdef ACTION_ON_CANCEL
  334. host_action_cancel();
  335. #endif
  336. kill(PSTR(MSG_ERR_PROBING_FAILED));
  337. #endif
  338. }
  339. void event_probe_recover() {
  340. #if ENABLED(HOST_PROMPT_SUPPORT)
  341. host_prompt_do(PROMPT_INFO, PSTR("G29 Retrying"), PSTR("Dismiss"));
  342. #endif
  343. #ifdef ACTION_ON_G29_RECOVER
  344. host_action(PSTR(ACTION_ON_G29_RECOVER));
  345. #endif
  346. #ifdef G29_RECOVER_COMMANDS
  347. gcode.process_subcommands_now_P(PSTR(G29_RECOVER_COMMANDS));
  348. #endif
  349. }
  350. #endif
  351. /**
  352. * Printing is active when the print job timer is running
  353. */
  354. bool printingIsActive() {
  355. return print_job_timer.isRunning() || IS_SD_PRINTING();
  356. }
  357. /**
  358. * Printing is paused according to SD or host indicators
  359. */
  360. bool printingIsPaused() {
  361. return print_job_timer.isPaused() || IS_SD_PAUSED();
  362. }
  363. /**
  364. * Manage several activities:
  365. * - Check for Filament Runout
  366. * - Keep the command buffer full
  367. * - Check for maximum inactive time between commands
  368. * - Check for maximum inactive time between stepper commands
  369. * - Check if CHDK_PIN needs to go LOW
  370. * - Check for KILL button held down
  371. * - Check for HOME button held down
  372. * - Check if cooling fan needs to be switched on
  373. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  374. * - Pulse FET_SAFETY_PIN if it exists
  375. */
  376. void manage_inactivity(const bool ignore_stepper_queue/*=false*/) {
  377. #if HAS_FILAMENT_SENSOR
  378. runout.run();
  379. #endif
  380. if (queue.length < BUFSIZE) queue.get_available_commands();
  381. const millis_t ms = millis();
  382. if (max_inactive_time && ELAPSED(ms, gcode.previous_move_ms + max_inactive_time)) {
  383. SERIAL_ERROR_START();
  384. SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
  385. kill();
  386. }
  387. // Prevent steppers timing-out in the middle of M600
  388. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  389. #define MOVE_AWAY_TEST !did_pause_print
  390. #else
  391. #define MOVE_AWAY_TEST true
  392. #endif
  393. if (stepper_inactive_time) {
  394. static bool already_shutdown_steppers; // = false
  395. if (planner.has_blocks_queued())
  396. gcode.reset_stepper_timeout();
  397. else if (MOVE_AWAY_TEST && !ignore_stepper_queue && ELAPSED(ms, gcode.previous_move_ms + stepper_inactive_time)) {
  398. if (!already_shutdown_steppers) {
  399. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  400. #if ENABLED(DISABLE_INACTIVE_X)
  401. disable_X();
  402. #endif
  403. #if ENABLED(DISABLE_INACTIVE_Y)
  404. disable_Y();
  405. #endif
  406. #if ENABLED(DISABLE_INACTIVE_Z)
  407. disable_Z();
  408. #endif
  409. #if ENABLED(DISABLE_INACTIVE_E)
  410. disable_e_steppers();
  411. #endif
  412. #if HAS_LCD_MENU && ENABLED(AUTO_BED_LEVELING_UBL)
  413. if (ubl.lcd_map_control) {
  414. ubl.lcd_map_control = false;
  415. ui.defer_status_screen(false);
  416. }
  417. #endif
  418. }
  419. }
  420. else
  421. already_shutdown_steppers = false;
  422. }
  423. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  424. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  425. chdk_timeout = 0;
  426. WRITE(CHDK_PIN, LOW);
  427. }
  428. #endif
  429. #if HAS_KILL
  430. // Check if the kill button was pressed and wait just in case it was an accidental
  431. // key kill key press
  432. // -------------------------------------------------------------------------------
  433. static int killCount = 0; // make the inactivity button a bit less responsive
  434. const int KILL_DELAY = 750;
  435. if (!READ(KILL_PIN))
  436. killCount++;
  437. else if (killCount > 0)
  438. killCount--;
  439. // Exceeded threshold and we can confirm that it was not accidental
  440. // KILL the machine
  441. // ----------------------------------------------------------------
  442. if (killCount >= KILL_DELAY) {
  443. SERIAL_ERROR_MSG(MSG_KILL_BUTTON);
  444. kill();
  445. }
  446. #endif
  447. #if HAS_HOME
  448. // Handle a standalone HOME button
  449. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  450. static millis_t next_home_key_ms; // = 0
  451. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  452. const millis_t ms = millis();
  453. if (ELAPSED(ms, next_home_key_ms)) {
  454. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  455. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  456. queue.enqueue_now_P(PSTR("G28"));
  457. }
  458. }
  459. #endif
  460. #if ENABLED(USE_CONTROLLER_FAN)
  461. controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
  462. #endif
  463. #if ENABLED(AUTO_POWER_CONTROL)
  464. powerManager.check();
  465. #endif
  466. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  467. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  468. && ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  469. && !planner.has_blocks_queued()
  470. ) {
  471. #if ENABLED(SWITCHING_EXTRUDER)
  472. bool oldstatus;
  473. switch (active_extruder) {
  474. default: oldstatus = E0_ENABLE_READ(); enable_E0(); break;
  475. #if E_STEPPERS > 1
  476. case 2: case 3: oldstatus = E1_ENABLE_READ(); enable_E1(); break;
  477. #if E_STEPPERS > 2
  478. case 4: case 5: oldstatus = E2_ENABLE_READ(); enable_E2(); break;
  479. #endif // E_STEPPERS > 2
  480. #endif // E_STEPPERS > 1
  481. }
  482. #else // !SWITCHING_EXTRUDER
  483. bool oldstatus;
  484. switch (active_extruder) {
  485. default: oldstatus = E0_ENABLE_READ(); enable_E0(); break;
  486. #if E_STEPPERS > 1
  487. case 1: oldstatus = E1_ENABLE_READ(); enable_E1(); break;
  488. #if E_STEPPERS > 2
  489. case 2: oldstatus = E2_ENABLE_READ(); enable_E2(); break;
  490. #if E_STEPPERS > 3
  491. case 3: oldstatus = E3_ENABLE_READ(); enable_E3(); break;
  492. #if E_STEPPERS > 4
  493. case 4: oldstatus = E4_ENABLE_READ(); enable_E4(); break;
  494. #if E_STEPPERS > 5
  495. case 5: oldstatus = E5_ENABLE_READ(); enable_E5(); break;
  496. #endif // E_STEPPERS > 5
  497. #endif // E_STEPPERS > 4
  498. #endif // E_STEPPERS > 3
  499. #endif // E_STEPPERS > 2
  500. #endif // E_STEPPERS > 1
  501. }
  502. #endif // !SWITCHING_EXTRUDER
  503. const float olde = current_position.e;
  504. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  505. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  506. current_position.e = olde;
  507. planner.set_e_position_mm(olde);
  508. planner.synchronize();
  509. #if ENABLED(SWITCHING_EXTRUDER)
  510. switch (active_extruder) {
  511. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  512. #if E_STEPPERS > 1
  513. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  514. #if E_STEPPERS > 2
  515. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  516. #endif // E_STEPPERS > 2
  517. #endif // E_STEPPERS > 1
  518. }
  519. #else // !SWITCHING_EXTRUDER
  520. switch (active_extruder) {
  521. case 0: E0_ENABLE_WRITE(oldstatus); break;
  522. #if E_STEPPERS > 1
  523. case 1: E1_ENABLE_WRITE(oldstatus); break;
  524. #if E_STEPPERS > 2
  525. case 2: E2_ENABLE_WRITE(oldstatus); break;
  526. #if E_STEPPERS > 3
  527. case 3: E3_ENABLE_WRITE(oldstatus); break;
  528. #if E_STEPPERS > 4
  529. case 4: E4_ENABLE_WRITE(oldstatus); break;
  530. #if E_STEPPERS > 5
  531. case 5: E5_ENABLE_WRITE(oldstatus); break;
  532. #endif // E_STEPPERS > 5
  533. #endif // E_STEPPERS > 4
  534. #endif // E_STEPPERS > 3
  535. #endif // E_STEPPERS > 2
  536. #endif // E_STEPPERS > 1
  537. }
  538. #endif // !SWITCHING_EXTRUDER
  539. gcode.reset_stepper_timeout();
  540. }
  541. #endif // EXTRUDER_RUNOUT_PREVENT
  542. #if ENABLED(DUAL_X_CARRIAGE)
  543. // handle delayed move timeout
  544. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  545. // travel moves have been received so enact them
  546. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  547. destination = current_position;
  548. prepare_move_to_destination();
  549. }
  550. #endif
  551. #if ENABLED(TEMP_STAT_LEDS)
  552. handle_status_leds();
  553. #endif
  554. #if ENABLED(MONITOR_DRIVER_STATUS)
  555. monitor_tmc_driver();
  556. #endif
  557. #if ENABLED(MONITOR_L6470_DRIVER_STATUS)
  558. L6470.monitor_driver();
  559. #endif
  560. // Limit check_axes_activity frequency to 10Hz
  561. static millis_t next_check_axes_ms = 0;
  562. if (ELAPSED(ms, next_check_axes_ms)) {
  563. planner.check_axes_activity();
  564. next_check_axes_ms = ms + 100UL;
  565. }
  566. #if PIN_EXISTS(FET_SAFETY)
  567. static millis_t FET_next;
  568. if (ELAPSED(ms, FET_next)) {
  569. FET_next = ms + FET_SAFETY_DELAY; // 2uS pulse every FET_SAFETY_DELAY mS
  570. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  571. DELAY_US(2);
  572. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  573. }
  574. #endif
  575. }
  576. /**
  577. * Standard idle routine keeps the machine alive
  578. */
  579. void idle(
  580. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  581. bool no_stepper_sleep/*=false*/
  582. #endif
  583. ) {
  584. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  585. recovery.outage();
  586. #endif
  587. #if ENABLED(SPI_ENDSTOPS)
  588. if (endstops.tmc_spi_homing.any
  589. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  590. && ELAPSED(millis(), sg_guard_period)
  591. #endif
  592. ) {
  593. for (uint8_t i = 4; i--;) // Read SGT 4 times per idle loop
  594. if (endstops.tmc_spi_homing_check()) break;
  595. }
  596. #endif
  597. #if ENABLED(MAX7219_DEBUG)
  598. max7219.idle_tasks();
  599. #endif
  600. ui.update();
  601. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  602. gcode.host_keepalive();
  603. #endif
  604. manage_inactivity(
  605. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  606. no_stepper_sleep
  607. #endif
  608. );
  609. thermalManager.manage_heater();
  610. #if ENABLED(PRINTCOUNTER)
  611. print_job_timer.tick();
  612. #endif
  613. #if USE_BEEPER
  614. buzzer.tick();
  615. #endif
  616. #if ENABLED(I2C_POSITION_ENCODERS)
  617. static millis_t i2cpem_next_update_ms;
  618. if (planner.has_blocks_queued()) {
  619. const millis_t ms = millis();
  620. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  621. I2CPEM.update();
  622. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  623. }
  624. }
  625. #endif
  626. #ifdef HAL_IDLETASK
  627. HAL_idletask();
  628. #endif
  629. #if HAS_AUTO_REPORTING
  630. if (!suspend_auto_report) {
  631. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  632. thermalManager.auto_report_temperatures();
  633. #endif
  634. #if ENABLED(AUTO_REPORT_SD_STATUS)
  635. card.auto_report_sd_status();
  636. #endif
  637. }
  638. #endif
  639. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  640. Sd2Card::idle();
  641. #endif
  642. #if ENABLED(PRUSA_MMU2)
  643. mmu2.mmu_loop();
  644. #endif
  645. #if ENABLED(POLL_JOG)
  646. joystick.inject_jog_moves();
  647. #endif
  648. }
  649. /**
  650. * Kill all activity and lock the machine.
  651. * After this the machine will need to be reset.
  652. */
  653. void kill(PGM_P const lcd_msg/*=nullptr*/, const bool steppers_off/*=false*/) {
  654. thermalManager.disable_all_heaters();
  655. SERIAL_ERROR_MSG(MSG_ERR_KILLED);
  656. #if HAS_DISPLAY
  657. ui.kill_screen(lcd_msg ?: PSTR(MSG_KILLED));
  658. #else
  659. UNUSED(lcd_msg);
  660. #endif
  661. #ifdef ACTION_ON_KILL
  662. host_action_kill();
  663. #endif
  664. minkill(steppers_off);
  665. }
  666. void minkill(const bool steppers_off/*=false*/) {
  667. // Wait a short time (allows messages to get out before shutting down.
  668. for (int i = 1000; i--;) DELAY_US(600);
  669. cli(); // Stop interrupts
  670. // Wait to ensure all interrupts stopped
  671. for (int i = 1000; i--;) DELAY_US(250);
  672. // Reiterate heaters off
  673. thermalManager.disable_all_heaters();
  674. // Power off all steppers (for M112) or just the E steppers
  675. steppers_off ? disable_all_steppers() : disable_e_steppers();
  676. #if HAS_POWER_SWITCH
  677. PSU_OFF();
  678. #endif
  679. #if HAS_SUICIDE
  680. suicide();
  681. #endif
  682. #if HAS_KILL
  683. // Wait for kill to be released
  684. while (!READ(KILL_PIN)) watchdog_refresh();
  685. // Wait for kill to be pressed
  686. while (READ(KILL_PIN)) watchdog_refresh();
  687. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  688. resetFunc(); // Jump to address 0
  689. #else // !HAS_KILL
  690. for (;;) watchdog_refresh(); // Wait for reset
  691. #endif // !HAS_KILL
  692. }
  693. /**
  694. * Turn off heaters and stop the print in progress
  695. * After a stop the machine may be resumed with M999
  696. */
  697. void stop() {
  698. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  699. print_job_timer.stop();
  700. #if ENABLED(PROBING_FANS_OFF)
  701. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  702. #endif
  703. if (IsRunning()) {
  704. queue.stop();
  705. SERIAL_ERROR_MSG(MSG_ERR_STOPPED);
  706. LCD_MESSAGEPGM(MSG_STOPPED);
  707. safe_delay(350); // allow enough time for messages to get out before stopping
  708. Running = false;
  709. }
  710. }
  711. /**
  712. * Marlin entry-point: Set up before the program loop
  713. * - Set up the kill pin, filament runout, power hold
  714. * - Start the serial port
  715. * - Print startup messages and diagnostics
  716. * - Get EEPROM or default settings
  717. * - Initialize managers for:
  718. * • temperature
  719. * • planner
  720. * • watchdog
  721. * • stepper
  722. * • photo pin
  723. * • servos
  724. * • LCD controller
  725. * • Digipot I2C
  726. * • Z probe sled
  727. * • status LEDs
  728. */
  729. void setup() {
  730. HAL_init();
  731. #if HAS_DRIVER(L6470)
  732. L6470.init(); // setup SPI and then init chips
  733. #endif
  734. #if ENABLED(MAX7219_DEBUG)
  735. max7219.init();
  736. #endif
  737. #if ENABLED(DISABLE_DEBUG)
  738. // Disable any hardware debug to free up pins for IO
  739. #ifdef JTAGSWD_DISABLE
  740. JTAGSWD_DISABLE();
  741. #elif defined(JTAG_DISABLE)
  742. JTAG_DISABLE();
  743. #else
  744. #error "DISABLE_DEBUG is not supported for the selected MCU/Board"
  745. #endif
  746. #elif ENABLED(DISABLE_JTAG)
  747. // Disable JTAG to free up pins for IO
  748. #ifdef JTAG_DISABLE
  749. JTAG_DISABLE();
  750. #else
  751. #error "DISABLE_JTAG is not supported for the selected MCU/Board"
  752. #endif
  753. #endif
  754. #if HAS_FILAMENT_SENSOR
  755. runout.setup();
  756. #endif
  757. #if ENABLED(POWER_LOSS_RECOVERY)
  758. recovery.setup();
  759. #endif
  760. setup_killpin();
  761. #if HAS_TMC220x
  762. tmc_serial_begin();
  763. #endif
  764. setup_powerhold();
  765. #if HAS_STEPPER_RESET
  766. disableStepperDrivers();
  767. #endif
  768. #if NUM_SERIAL > 0
  769. MYSERIAL0.begin(BAUDRATE);
  770. #if NUM_SERIAL > 1
  771. MYSERIAL1.begin(BAUDRATE);
  772. #endif
  773. #endif
  774. #if NUM_SERIAL > 0
  775. uint32_t serial_connect_timeout = millis() + 1000UL;
  776. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  777. #if NUM_SERIAL > 1
  778. serial_connect_timeout = millis() + 1000UL;
  779. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  780. #endif
  781. #endif
  782. SERIAL_ECHOLNPGM("start");
  783. SERIAL_ECHO_START();
  784. #if TMC_HAS_SPI
  785. #if DISABLED(TMC_USE_SW_SPI)
  786. SPI.begin();
  787. #endif
  788. tmc_init_cs_pins();
  789. #endif
  790. #ifdef BOARD_INIT
  791. BOARD_INIT();
  792. #endif
  793. // Check startup - does nothing if bootloader sets MCUSR to 0
  794. byte mcu = HAL_get_reset_source();
  795. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  796. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  797. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  798. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  799. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  800. HAL_clear_reset_source();
  801. SERIAL_ECHOPGM(MSG_MARLIN);
  802. SERIAL_CHAR(' ');
  803. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  804. SERIAL_EOL();
  805. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  806. SERIAL_ECHO_MSG(
  807. MSG_CONFIGURATION_VER
  808. STRING_DISTRIBUTION_DATE
  809. MSG_AUTHOR STRING_CONFIG_H_AUTHOR
  810. );
  811. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  812. #endif
  813. SERIAL_ECHO_START();
  814. SERIAL_ECHOLNPAIR(MSG_FREE_MEMORY, freeMemory(), MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  815. // UI must be initialized before EEPROM
  816. // (because EEPROM code calls the UI).
  817. // Set up LEDs early
  818. #if HAS_COLOR_LEDS
  819. leds.setup();
  820. #endif
  821. ui.init();
  822. ui.reset_status();
  823. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  824. ui.show_bootscreen();
  825. #endif
  826. #if ENABLED(SDSUPPORT)
  827. card.mount(); // Mount the SD card before settings.first_load
  828. #endif
  829. // Load data from EEPROM if available (or use defaults)
  830. // This also updates variables in the planner, elsewhere
  831. settings.first_load();
  832. #if ENABLED(TOUCH_BUTTONS)
  833. touch.init();
  834. #endif
  835. #if HAS_M206_COMMAND
  836. // Initialize current position based on home_offset
  837. current_position += home_offset;
  838. #endif
  839. // Vital to init stepper/planner equivalent for current_position
  840. sync_plan_position();
  841. thermalManager.init(); // Initialize temperature loop
  842. print_job_timer.init(); // Initial setup of print job timer
  843. endstops.init(); // Init endstops and pullups
  844. stepper.init(); // Init stepper. This enables interrupts!
  845. #if HAS_SERVOS
  846. servo_init();
  847. #endif
  848. #if HAS_Z_SERVO_PROBE
  849. servo_probe_init();
  850. #endif
  851. #if HAS_PHOTOGRAPH
  852. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  853. #endif
  854. #if HAS_CUTTER
  855. cutter.init();
  856. #endif
  857. #if ENABLED(COOLANT_MIST)
  858. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  859. #endif
  860. #if ENABLED(COOLANT_FLOOD)
  861. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  862. #endif
  863. #if HAS_BED_PROBE
  864. endstops.enable_z_probe(false);
  865. #endif
  866. #if ENABLED(USE_CONTROLLER_FAN)
  867. SET_OUTPUT(CONTROLLER_FAN_PIN);
  868. #endif
  869. #if HAS_STEPPER_RESET
  870. enableStepperDrivers();
  871. #endif
  872. #if ENABLED(DIGIPOT_I2C)
  873. digipot_i2c_init();
  874. #endif
  875. #if ENABLED(DAC_STEPPER_CURRENT)
  876. dac_init();
  877. #endif
  878. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  879. OUT_WRITE(SOL1_PIN, LOW); // OFF
  880. #endif
  881. #if HAS_HOME
  882. SET_INPUT_PULLUP(HOME_PIN);
  883. #endif
  884. #if PIN_EXISTS(STAT_LED_RED)
  885. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  886. #endif
  887. #if PIN_EXISTS(STAT_LED_BLUE)
  888. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  889. #endif
  890. #if HAS_CASE_LIGHT
  891. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  892. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  893. #endif
  894. update_case_light();
  895. #endif
  896. #if ENABLED(MK2_MULTIPLEXER)
  897. SET_OUTPUT(E_MUX0_PIN);
  898. SET_OUTPUT(E_MUX1_PIN);
  899. SET_OUTPUT(E_MUX2_PIN);
  900. #endif
  901. #if HAS_FANMUX
  902. fanmux_init();
  903. #endif
  904. #if ENABLED(MIXING_EXTRUDER)
  905. mixer.init();
  906. #endif
  907. #if ENABLED(BLTOUCH)
  908. bltouch.init(/*set_voltage=*/true);
  909. #endif
  910. #if ENABLED(I2C_POSITION_ENCODERS)
  911. I2CPEM.init();
  912. #endif
  913. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  914. i2c.onReceive(i2c_on_receive);
  915. i2c.onRequest(i2c_on_request);
  916. #endif
  917. #if DO_SWITCH_EXTRUDER
  918. move_extruder_servo(0); // Initialize extruder servo
  919. #endif
  920. #if ENABLED(SWITCHING_NOZZLE)
  921. // Initialize nozzle servo(s)
  922. #if SWITCHING_NOZZLE_TWO_SERVOS
  923. lower_nozzle(0);
  924. raise_nozzle(1);
  925. #else
  926. move_nozzle_servo(0);
  927. #endif
  928. #endif
  929. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  930. mpe_settings_init();
  931. #endif
  932. #if ENABLED(PARKING_EXTRUDER)
  933. pe_solenoid_init();
  934. #endif
  935. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  936. est_init();
  937. #endif
  938. #if ENABLED(POWER_LOSS_RECOVERY)
  939. recovery.check();
  940. #endif
  941. #if ENABLED(USE_WATCHDOG)
  942. watchdog_init(); // Reinit watchdog after HAL_get_reset_source call
  943. #endif
  944. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  945. init_closedloop();
  946. #endif
  947. #ifdef STARTUP_COMMANDS
  948. queue.inject_P(PSTR(STARTUP_COMMANDS));
  949. #endif
  950. #if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
  951. card.beginautostart();
  952. #endif
  953. #if ENABLED(HOST_PROMPT_SUPPORT)
  954. host_action_prompt_end();
  955. #endif
  956. #if HAS_TRINAMIC && DISABLED(PS_DEFAULT_OFF)
  957. test_tmc_connection(true, true, true, true);
  958. #endif
  959. #if ENABLED(PRUSA_MMU2)
  960. mmu2.init();
  961. #endif
  962. }
  963. /**
  964. * The main Marlin program loop
  965. *
  966. * - Save or log commands to SD
  967. * - Process available commands (if not saving)
  968. * - Call endstop manager
  969. * - Call inactivity manager
  970. */
  971. void loop() {
  972. for (;;) {
  973. idle(); // Do an idle first so boot is slightly faster
  974. #if ENABLED(SDSUPPORT)
  975. card.checkautostart();
  976. if (card.flag.abort_sd_printing) {
  977. card.stopSDPrint(
  978. #if SD_RESORT
  979. true
  980. #endif
  981. );
  982. queue.clear();
  983. quickstop_stepper();
  984. print_job_timer.stop();
  985. #if DISABLED(SD_ABORT_NO_COOLDOWN)
  986. thermalManager.disable_all_heaters();
  987. #endif
  988. thermalManager.zero_fan_speeds();
  989. wait_for_heatup = false;
  990. #if ENABLED(POWER_LOSS_RECOVERY)
  991. card.removeJobRecoveryFile();
  992. #endif
  993. #ifdef EVENT_GCODE_SD_STOP
  994. queue.inject_P(PSTR(EVENT_GCODE_SD_STOP));
  995. #endif
  996. }
  997. #endif // SDSUPPORT
  998. queue.advance();
  999. endstops.event_handler();
  1000. }
  1001. }