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.

MarlinCore.cpp 31KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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 <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 "MarlinCore.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_CONFIG && DISABLED(PSU_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 ENABLED(CANCEL_OBJECTS)
  120. #include "feature/cancel_object.h"
  121. #endif
  122. #if HAS_FILAMENT_SENSOR
  123. #include "feature/runout.h"
  124. #endif
  125. #if ENABLED(TEMP_STAT_LEDS)
  126. #include "feature/leds/tempstat.h"
  127. #endif
  128. #if HAS_CASE_LIGHT
  129. #include "feature/caselight.h"
  130. #endif
  131. #if HAS_FANMUX
  132. #include "feature/fanmux.h"
  133. #endif
  134. #if DO_SWITCH_EXTRUDER || ANY(SWITCHING_NOZZLE, PARKING_EXTRUDER, MAGNETIC_PARKING_EXTRUDER, ELECTROMAGNETIC_SWITCHING_TOOLHEAD, SWITCHING_TOOLHEAD)
  135. #include "module/tool_change.h"
  136. #endif
  137. #if ENABLED(USE_CONTROLLER_FAN)
  138. #include "feature/controllerfan.h"
  139. #endif
  140. #if ENABLED(PRUSA_MMU2)
  141. #include "feature/prusa_MMU2/mmu2.h"
  142. #endif
  143. #if HAS_L64XX
  144. #include "libs/L64XX/L64XX_Marlin.h"
  145. #endif
  146. const char NUL_STR[] PROGMEM = "",
  147. M112_KILL_STR[] PROGMEM = "M112 Shutdown",
  148. G28_STR[] PROGMEM = "G28",
  149. M21_STR[] PROGMEM = "M21",
  150. M23_STR[] PROGMEM = "M23 %s",
  151. M24_STR[] PROGMEM = "M24",
  152. SP_P_STR[] PROGMEM = " P",
  153. SP_T_STR[] PROGMEM = " T",
  154. SP_X_STR[] PROGMEM = " X",
  155. SP_Y_STR[] PROGMEM = " Y",
  156. SP_Z_STR[] PROGMEM = " Z",
  157. SP_E_STR[] PROGMEM = " E",
  158. X_LBL[] PROGMEM = "X:",
  159. Y_LBL[] PROGMEM = "Y:",
  160. Z_LBL[] PROGMEM = "Z:",
  161. E_LBL[] PROGMEM = "E:",
  162. SP_X_LBL[] PROGMEM = " X:",
  163. SP_Y_LBL[] PROGMEM = " Y:",
  164. SP_Z_LBL[] PROGMEM = " Z:",
  165. SP_E_LBL[] PROGMEM = " E:";
  166. bool Running = true;
  167. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  168. bool wait_for_heatup = true;
  169. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  170. #if HAS_RESUME_CONTINUE
  171. bool wait_for_user; // = false;
  172. #endif
  173. // Inactivity shutdown
  174. millis_t max_inactive_time, // = 0
  175. stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
  176. #if PIN_EXISTS(CHDK)
  177. extern millis_t chdk_timeout;
  178. #endif
  179. #if ENABLED(I2C_POSITION_ENCODERS)
  180. I2CPositionEncodersMgr I2CPEM;
  181. #endif
  182. /**
  183. * ***************************************************************************
  184. * ******************************** FUNCTIONS ********************************
  185. * ***************************************************************************
  186. */
  187. void setup_killpin() {
  188. #if HAS_KILL
  189. SET_INPUT_PULLUP(KILL_PIN);
  190. #endif
  191. }
  192. void setup_powerhold() {
  193. #if HAS_SUICIDE
  194. OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
  195. #endif
  196. #if ENABLED(PSU_CONTROL)
  197. powersupply_on = ENABLED(PSU_DEFAULT_OFF);
  198. if (ENABLED(PSU_DEFAULT_OFF)) PSU_OFF(); else PSU_ON();
  199. #endif
  200. }
  201. /**
  202. * Stepper Reset (RigidBoard, et.al.)
  203. */
  204. #if HAS_STEPPER_RESET
  205. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  206. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  207. #endif
  208. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  209. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  210. i2c.receive(bytes);
  211. }
  212. void i2c_on_request() { // just send dummy data for now
  213. i2c.reply("Hello World!\n");
  214. }
  215. #endif
  216. /**
  217. * Sensitive pin test for M42, M226
  218. */
  219. #include "pins/sensitive_pins.h"
  220. bool pin_is_protected(const pin_t pin) {
  221. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  222. for (uint8_t i = 0; i < COUNT(sensitive_pins); i++) {
  223. pin_t sensitive_pin;
  224. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  225. if (pin == sensitive_pin) return true;
  226. }
  227. return false;
  228. }
  229. void protected_pin_err() {
  230. SERIAL_ERROR_MSG(STR_ERR_PROTECTED_PIN);
  231. }
  232. void quickstop_stepper() {
  233. planner.quick_stop();
  234. planner.synchronize();
  235. set_current_from_steppers_for_axis(ALL_AXES);
  236. sync_plan_position();
  237. }
  238. void enable_e_steppers() {
  239. #define _ENA_E(N) ENABLE_AXIS_E##N();
  240. REPEAT(E_STEPPERS, _ENA_E)
  241. }
  242. void enable_all_steppers() {
  243. #if ENABLED(AUTO_POWER_CONTROL)
  244. powerManager.power_on();
  245. #endif
  246. ENABLE_AXIS_X();
  247. ENABLE_AXIS_Y();
  248. ENABLE_AXIS_Z();
  249. enable_e_steppers();
  250. }
  251. void disable_e_steppers() {
  252. #define _DIS_E(N) DISABLE_AXIS_E##N();
  253. REPEAT(E_STEPPERS, _DIS_E)
  254. }
  255. void disable_e_stepper(const uint8_t e) {
  256. #define _CASE_DIS_E(N) case N: DISABLE_AXIS_E##N(); break;
  257. switch (e) {
  258. REPEAT(EXTRUDERS, _CASE_DIS_E)
  259. }
  260. }
  261. void disable_all_steppers() {
  262. DISABLE_AXIS_X();
  263. DISABLE_AXIS_Y();
  264. DISABLE_AXIS_Z();
  265. disable_e_steppers();
  266. }
  267. #if ENABLED(G29_RETRY_AND_RECOVER)
  268. void event_probe_failure() {
  269. #ifdef ACTION_ON_G29_FAILURE
  270. host_action(PSTR(ACTION_ON_G29_FAILURE));
  271. #endif
  272. #ifdef G29_FAILURE_COMMANDS
  273. gcode.process_subcommands_now_P(PSTR(G29_FAILURE_COMMANDS));
  274. #endif
  275. #if ENABLED(G29_HALT_ON_FAILURE)
  276. #ifdef ACTION_ON_CANCEL
  277. host_action_cancel();
  278. #endif
  279. kill(GET_TEXT(MSG_LCD_PROBING_FAILED));
  280. #endif
  281. }
  282. void event_probe_recover() {
  283. #if ENABLED(HOST_PROMPT_SUPPORT)
  284. host_prompt_do(PROMPT_INFO, PSTR("G29 Retrying"), DISMISS_STR);
  285. #endif
  286. #ifdef ACTION_ON_G29_RECOVER
  287. host_action(PSTR(ACTION_ON_G29_RECOVER));
  288. #endif
  289. #ifdef G29_RECOVER_COMMANDS
  290. gcode.process_subcommands_now_P(PSTR(G29_RECOVER_COMMANDS));
  291. #endif
  292. }
  293. #endif
  294. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  295. #include "feature/pause.h"
  296. #else
  297. constexpr bool did_pause_print = false;
  298. #endif
  299. /**
  300. * Printing is active when the print job timer is running
  301. */
  302. bool printingIsActive() {
  303. return !did_pause_print && (print_job_timer.isRunning() || IS_SD_PRINTING());
  304. }
  305. /**
  306. * Printing is paused according to SD or host indicators
  307. */
  308. bool printingIsPaused() {
  309. return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
  310. }
  311. void startOrResumeJob() {
  312. if (!printingIsPaused()) {
  313. #if ENABLED(CANCEL_OBJECTS)
  314. cancelable.reset();
  315. #endif
  316. #if ENABLED(LCD_SHOW_E_TOTAL)
  317. e_move_accumulator = 0;
  318. #endif
  319. #if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
  320. ui.reset_remaining_time();
  321. #endif
  322. }
  323. print_job_timer.start();
  324. }
  325. #if ENABLED(SDSUPPORT)
  326. inline void abortSDPrinting() {
  327. card.endFilePrint(
  328. #if SD_RESORT
  329. true
  330. #endif
  331. );
  332. queue.clear();
  333. quickstop_stepper();
  334. print_job_timer.stop();
  335. #if DISABLED(SD_ABORT_NO_COOLDOWN)
  336. thermalManager.disable_all_heaters();
  337. #endif
  338. thermalManager.zero_fan_speeds();
  339. wait_for_heatup = false;
  340. #if ENABLED(POWER_LOSS_RECOVERY)
  341. recovery.purge();
  342. #endif
  343. #ifdef EVENT_GCODE_SD_STOP
  344. queue.inject_P(PSTR(EVENT_GCODE_SD_STOP));
  345. #endif
  346. }
  347. inline void finishSDPrinting() {
  348. bool did_state = true;
  349. switch (card.sdprinting_done_state) {
  350. #if HAS_RESUME_CONTINUE // Display "Click to Continue..."
  351. case 1: // 30 min timeout with LCD, 1 min without
  352. did_state = queue.enqueue_P(PSTR("M0Q1S" TERN(HAS_LCD_MENU, "1800", "60")));
  353. break;
  354. #endif
  355. case 2: print_job_timer.stop(); break;
  356. case 3:
  357. did_state = print_job_timer.duration() < 60 || queue.enqueue_P(PSTR("M31"));
  358. break;
  359. case 4:
  360. #if ENABLED(POWER_LOSS_RECOVERY)
  361. recovery.purge();
  362. #endif
  363. #if ENABLED(SD_FINISHED_STEPPERRELEASE) && defined(SD_FINISHED_RELEASECOMMAND)
  364. planner.finish_and_disable();
  365. #endif
  366. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  367. ui.set_progress_done();
  368. #endif
  369. #if ENABLED(SD_REPRINT_LAST_SELECTED_FILE)
  370. ui.reselect_last_file();
  371. #endif
  372. SERIAL_ECHOLNPGM(STR_FILE_PRINTED);
  373. default:
  374. did_state = false;
  375. card.sdprinting_done_state = 0;
  376. }
  377. if (did_state) ++card.sdprinting_done_state;
  378. }
  379. #endif // SDSUPPORT
  380. /**
  381. * Minimal management of Marlin's core activities:
  382. * - Check for Filament Runout
  383. * - Keep the command buffer full
  384. * - Check for maximum inactive time between commands
  385. * - Check for maximum inactive time between stepper commands
  386. * - Check if CHDK_PIN needs to go LOW
  387. * - Check for KILL button held down
  388. * - Check for HOME button held down
  389. * - Check if cooling fan needs to be switched on
  390. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  391. * - Pulse FET_SAFETY_PIN if it exists
  392. */
  393. inline void manage_inactivity(const bool ignore_stepper_queue=false) {
  394. #if HAS_FILAMENT_SENSOR
  395. runout.run();
  396. #endif
  397. if (queue.length < BUFSIZE) queue.get_available_commands();
  398. const millis_t ms = millis();
  399. if (max_inactive_time && ELAPSED(ms, gcode.previous_move_ms + max_inactive_time)) {
  400. SERIAL_ERROR_START();
  401. SERIAL_ECHOLNPAIR(STR_KILL_INACTIVE_TIME, parser.command_ptr);
  402. kill();
  403. }
  404. // Prevent steppers timing-out in the middle of M600
  405. #define STAY_TEST (BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT) && did_pause_print)
  406. if (stepper_inactive_time) {
  407. static bool already_shutdown_steppers; // = false
  408. if (planner.has_blocks_queued())
  409. gcode.reset_stepper_timeout();
  410. else if (!STAY_TEST && !ignore_stepper_queue && ELAPSED(ms, gcode.previous_move_ms + stepper_inactive_time)) {
  411. if (!already_shutdown_steppers) {
  412. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  413. if (ENABLED(DISABLE_INACTIVE_X)) DISABLE_AXIS_X();
  414. if (ENABLED(DISABLE_INACTIVE_Y)) DISABLE_AXIS_Y();
  415. if (ENABLED(DISABLE_INACTIVE_Z)) DISABLE_AXIS_Z();
  416. if (ENABLED(DISABLE_INACTIVE_E)) disable_e_steppers();
  417. #if HAS_LCD_MENU && ENABLED(AUTO_BED_LEVELING_UBL)
  418. if (ubl.lcd_map_control) {
  419. ubl.lcd_map_control = false;
  420. ui.defer_status_screen(false);
  421. }
  422. #endif
  423. }
  424. }
  425. else
  426. already_shutdown_steppers = false;
  427. }
  428. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  429. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  430. chdk_timeout = 0;
  431. WRITE(CHDK_PIN, LOW);
  432. }
  433. #endif
  434. #if HAS_KILL
  435. // Check if the kill button was pressed and wait just in case it was an accidental
  436. // key kill key press
  437. // -------------------------------------------------------------------------------
  438. static int killCount = 0; // make the inactivity button a bit less responsive
  439. const int KILL_DELAY = 750;
  440. if (!READ(KILL_PIN))
  441. killCount++;
  442. else if (killCount > 0)
  443. killCount--;
  444. // Exceeded threshold and we can confirm that it was not accidental
  445. // KILL the machine
  446. // ----------------------------------------------------------------
  447. if (killCount >= KILL_DELAY) {
  448. SERIAL_ERROR_MSG(STR_KILL_BUTTON);
  449. kill();
  450. }
  451. #endif
  452. #if HAS_HOME
  453. // Handle a standalone HOME button
  454. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  455. static millis_t next_home_key_ms; // = 0
  456. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  457. const millis_t ms = millis();
  458. if (ELAPSED(ms, next_home_key_ms)) {
  459. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  460. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  461. queue.enqueue_now_P(G28_STR);
  462. }
  463. }
  464. #endif
  465. #if ENABLED(USE_CONTROLLER_FAN)
  466. controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
  467. #endif
  468. #if ENABLED(AUTO_POWER_CONTROL)
  469. powerManager.check();
  470. #endif
  471. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  472. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  473. && ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  474. && !planner.has_blocks_queued()
  475. ) {
  476. #if ENABLED(SWITCHING_EXTRUDER)
  477. bool oldstatus;
  478. switch (active_extruder) {
  479. default: oldstatus = E0_ENABLE_READ(); ENABLE_AXIS_E0(); break;
  480. #if E_STEPPERS > 1
  481. case 2: case 3: oldstatus = E1_ENABLE_READ(); ENABLE_AXIS_E1(); break;
  482. #if E_STEPPERS > 2
  483. case 4: case 5: oldstatus = E2_ENABLE_READ(); ENABLE_AXIS_E2(); break;
  484. #if E_STEPPERS > 3
  485. case 6: case 7: oldstatus = E3_ENABLE_READ(); ENABLE_AXIS_E3(); break;
  486. #endif // E_STEPPERS > 3
  487. #endif // E_STEPPERS > 2
  488. #endif // E_STEPPERS > 1
  489. }
  490. #else // !SWITCHING_EXTRUDER
  491. bool oldstatus;
  492. switch (active_extruder) {
  493. default:
  494. #define _CASE_EN(N) case N: oldstatus = E##N##_ENABLE_READ(); ENABLE_AXIS_E##N(); break;
  495. REPEAT(E_STEPPERS, _CASE_EN);
  496. }
  497. #endif
  498. const float olde = current_position.e;
  499. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  500. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  501. current_position.e = olde;
  502. planner.set_e_position_mm(olde);
  503. planner.synchronize();
  504. #if ENABLED(SWITCHING_EXTRUDER)
  505. switch (active_extruder) {
  506. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  507. #if E_STEPPERS > 1
  508. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  509. #if E_STEPPERS > 2
  510. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  511. #endif // E_STEPPERS > 2
  512. #endif // E_STEPPERS > 1
  513. }
  514. #else // !SWITCHING_EXTRUDER
  515. switch (active_extruder) {
  516. #define _CASE_RESTORE(N) case N: E##N##_ENABLE_WRITE(oldstatus); break;
  517. REPEAT(E_STEPPERS, _CASE_RESTORE);
  518. }
  519. #endif // !SWITCHING_EXTRUDER
  520. gcode.reset_stepper_timeout();
  521. }
  522. #endif // EXTRUDER_RUNOUT_PREVENT
  523. #if ENABLED(DUAL_X_CARRIAGE)
  524. // handle delayed move timeout
  525. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  526. // travel moves have been received so enact them
  527. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  528. destination = current_position;
  529. prepare_line_to_destination();
  530. }
  531. #endif
  532. #if ENABLED(TEMP_STAT_LEDS)
  533. handle_status_leds();
  534. #endif
  535. #if ENABLED(MONITOR_DRIVER_STATUS)
  536. monitor_tmc_drivers();
  537. #endif
  538. #if ENABLED(MONITOR_L6470_DRIVER_STATUS)
  539. L64xxManager.monitor_driver();
  540. #endif
  541. // Limit check_axes_activity frequency to 10Hz
  542. static millis_t next_check_axes_ms = 0;
  543. if (ELAPSED(ms, next_check_axes_ms)) {
  544. planner.check_axes_activity();
  545. next_check_axes_ms = ms + 100UL;
  546. }
  547. #if PIN_EXISTS(FET_SAFETY)
  548. static millis_t FET_next;
  549. if (ELAPSED(ms, FET_next)) {
  550. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  551. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  552. DELAY_US(2);
  553. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  554. }
  555. #endif
  556. }
  557. /**
  558. * Standard idle routine keeps the machine alive
  559. */
  560. void idle(
  561. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  562. bool no_stepper_sleep/*=false*/
  563. #endif
  564. ) {
  565. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  566. recovery.outage();
  567. #endif
  568. #if ENABLED(SPI_ENDSTOPS)
  569. if (endstops.tmc_spi_homing.any
  570. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  571. && ELAPSED(millis(), sg_guard_period)
  572. #endif
  573. ) {
  574. for (uint8_t i = 4; i--;) // Read SGT 4 times per idle loop
  575. if (endstops.tmc_spi_homing_check()) break;
  576. }
  577. #endif
  578. #if ENABLED(MAX7219_DEBUG)
  579. max7219.idle_tasks();
  580. #endif
  581. ui.update();
  582. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  583. gcode.host_keepalive();
  584. #endif
  585. manage_inactivity(
  586. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  587. no_stepper_sleep
  588. #endif
  589. );
  590. thermalManager.manage_heater();
  591. #if ENABLED(PRINTCOUNTER)
  592. print_job_timer.tick();
  593. #endif
  594. #if USE_BEEPER
  595. buzzer.tick();
  596. #endif
  597. #if ENABLED(I2C_POSITION_ENCODERS)
  598. static millis_t i2cpem_next_update_ms;
  599. if (planner.has_blocks_queued()) {
  600. const millis_t ms = millis();
  601. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  602. I2CPEM.update();
  603. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  604. }
  605. }
  606. #endif
  607. #ifdef HAL_IDLETASK
  608. HAL_idletask();
  609. #endif
  610. #if HAS_AUTO_REPORTING
  611. if (!gcode.autoreport_paused) {
  612. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  613. thermalManager.auto_report_temperatures();
  614. #endif
  615. #if ENABLED(AUTO_REPORT_SD_STATUS)
  616. card.auto_report_sd_status();
  617. #endif
  618. }
  619. #endif
  620. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  621. Sd2Card::idle();
  622. #endif
  623. #if ENABLED(PRUSA_MMU2)
  624. mmu2.mmu_loop();
  625. #endif
  626. #if ENABLED(POLL_JOG)
  627. joystick.inject_jog_moves();
  628. #endif
  629. }
  630. /**
  631. * Kill all activity and lock the machine.
  632. * After this the machine will need to be reset.
  633. */
  634. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  635. thermalManager.disable_all_heaters();
  636. SERIAL_ERROR_MSG(STR_ERR_KILLED);
  637. #if HAS_DISPLAY
  638. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component ?: NUL_STR);
  639. #else
  640. UNUSED(lcd_error);
  641. UNUSED(lcd_component);
  642. #endif
  643. #ifdef ACTION_ON_KILL
  644. host_action_kill();
  645. #endif
  646. minkill(steppers_off);
  647. }
  648. void minkill(const bool steppers_off/*=false*/) {
  649. // Wait a short time (allows messages to get out before shutting down.
  650. for (int i = 1000; i--;) DELAY_US(600);
  651. cli(); // Stop interrupts
  652. // Wait to ensure all interrupts stopped
  653. for (int i = 1000; i--;) DELAY_US(250);
  654. // Reiterate heaters off
  655. thermalManager.disable_all_heaters();
  656. // Power off all steppers (for M112) or just the E steppers
  657. steppers_off ? disable_all_steppers() : disable_e_steppers();
  658. #if ENABLED(PSU_CONTROL)
  659. PSU_OFF();
  660. #endif
  661. #if HAS_SUICIDE
  662. suicide();
  663. #endif
  664. #if HAS_KILL
  665. // Wait for kill to be released
  666. while (!READ(KILL_PIN)) watchdog_refresh();
  667. // Wait for kill to be pressed
  668. while (READ(KILL_PIN)) watchdog_refresh();
  669. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  670. resetFunc(); // Jump to address 0
  671. #else // !HAS_KILL
  672. for (;;) watchdog_refresh(); // Wait for reset
  673. #endif // !HAS_KILL
  674. }
  675. /**
  676. * Turn off heaters and stop the print in progress
  677. * After a stop the machine may be resumed with M999
  678. */
  679. void stop() {
  680. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  681. print_job_timer.stop();
  682. #if ENABLED(PROBING_FANS_OFF)
  683. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  684. #endif
  685. if (IsRunning()) {
  686. SERIAL_ERROR_MSG(STR_ERR_STOPPED);
  687. LCD_MESSAGEPGM(MSG_STOPPED);
  688. safe_delay(350); // allow enough time for messages to get out before stopping
  689. Running = false;
  690. }
  691. }
  692. /**
  693. * Marlin entry-point: Set up before the program loop
  694. * - Set up the kill pin, filament runout, power hold
  695. * - Start the serial port
  696. * - Print startup messages and diagnostics
  697. * - Get EEPROM or default settings
  698. * - Initialize managers for:
  699. * • temperature
  700. * • planner
  701. * • watchdog
  702. * • stepper
  703. * • photo pin
  704. * • servos
  705. * • LCD controller
  706. * • Digipot I2C
  707. * • Z probe sled
  708. * • status LEDs
  709. * • Max7219
  710. */
  711. void setup() {
  712. HAL_init();
  713. #if HAS_L64XX
  714. L64xxManager.init(); // Set up SPI, init drivers
  715. #endif
  716. #if ENABLED(SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  717. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  718. #endif
  719. #if ENABLED(DISABLE_DEBUG)
  720. // Disable any hardware debug to free up pins for IO
  721. #ifdef JTAGSWD_DISABLE
  722. JTAGSWD_DISABLE();
  723. #elif defined(JTAG_DISABLE)
  724. JTAG_DISABLE();
  725. #else
  726. #error "DISABLE_DEBUG is not supported for the selected MCU/Board"
  727. #endif
  728. #elif ENABLED(DISABLE_JTAG)
  729. // Disable JTAG to free up pins for IO
  730. #ifdef JTAG_DISABLE
  731. JTAG_DISABLE();
  732. #else
  733. #error "DISABLE_JTAG is not supported for the selected MCU/Board"
  734. #endif
  735. #endif
  736. #if HAS_FILAMENT_SENSOR
  737. runout.setup();
  738. #endif
  739. #if ENABLED(POWER_LOSS_RECOVERY)
  740. recovery.setup();
  741. #endif
  742. setup_killpin();
  743. #if HAS_TMC220x
  744. tmc_serial_begin();
  745. #endif
  746. setup_powerhold();
  747. #if HAS_STEPPER_RESET
  748. disableStepperDrivers();
  749. #endif
  750. #if NUM_SERIAL > 0
  751. MYSERIAL0.begin(BAUDRATE);
  752. uint32_t serial_connect_timeout = millis() + 1000UL;
  753. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  754. #if NUM_SERIAL > 1
  755. MYSERIAL1.begin(BAUDRATE);
  756. serial_connect_timeout = millis() + 1000UL;
  757. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  758. #endif
  759. #endif
  760. SERIAL_ECHOLNPGM("start");
  761. SERIAL_ECHO_START();
  762. #if HAS_TMC_SPI
  763. #if DISABLED(TMC_USE_SW_SPI)
  764. SPI.begin();
  765. #endif
  766. tmc_init_cs_pins();
  767. #endif
  768. #ifdef BOARD_INIT
  769. BOARD_INIT();
  770. #endif
  771. // Check startup - does nothing if bootloader sets MCUSR to 0
  772. byte mcu = HAL_get_reset_source();
  773. if (mcu & 1) SERIAL_ECHOLNPGM(STR_POWERUP);
  774. if (mcu & 2) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  775. if (mcu & 4) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  776. if (mcu & 8) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  777. if (mcu & 32) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  778. HAL_clear_reset_source();
  779. serialprintPGM(GET_TEXT(MSG_MARLIN));
  780. SERIAL_CHAR(' ');
  781. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  782. SERIAL_EOL();
  783. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  784. SERIAL_ECHO_MSG(
  785. STR_CONFIGURATION_VER
  786. STRING_DISTRIBUTION_DATE
  787. STR_AUTHOR STRING_CONFIG_H_AUTHOR
  788. );
  789. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  790. #endif
  791. SERIAL_ECHO_START();
  792. SERIAL_ECHOLNPAIR(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  793. // UI must be initialized before EEPROM
  794. // (because EEPROM code calls the UI).
  795. #if ENABLED(MARLIN_DEV_MODE)
  796. auto log_current_ms = [&](PGM_P const msg) {
  797. SERIAL_ECHO_START();
  798. SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHO("] ");
  799. serialprintPGM(msg);
  800. SERIAL_EOL();
  801. };
  802. #define SETUP_LOG(M) log_current_ms(PSTR(M))
  803. #else
  804. #define SETUP_LOG(...) NOOP
  805. #endif
  806. #define SETUP_RUN(C) do{ SETUP_LOG(STRINGIFY(C)); C; }while(0)
  807. // Set up LEDs early
  808. #if HAS_COLOR_LEDS
  809. SETUP_RUN(leds.setup());
  810. #endif
  811. SETUP_RUN(ui.init());
  812. SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)
  813. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  814. SETUP_RUN(ui.show_bootscreen());
  815. #endif
  816. #if ENABLED(SDSUPPORT)
  817. SETUP_RUN(card.mount()); // Mount the SD card before settings.first_load
  818. #endif
  819. SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
  820. // This also updates variables in the planner, elsewhere
  821. #if HAS_SERVICE_INTERVALS
  822. SETUP_RUN(ui.reset_status(true)); // Show service messages or keep current status
  823. #endif
  824. #if ENABLED(TOUCH_BUTTONS)
  825. SETUP_RUN(touch.init());
  826. #endif
  827. #if HAS_M206_COMMAND
  828. current_position += home_offset; // Init current position based on home_offset
  829. #endif
  830. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  831. SETUP_RUN(thermalManager.init()); // Initialize temperature loop
  832. SETUP_RUN(print_job_timer.init()); // Initial setup of print job timer
  833. SETUP_RUN(endstops.init()); // Init endstops and pullups
  834. SETUP_RUN(stepper.init()); // Init stepper. This enables interrupts!
  835. #if HAS_SERVOS
  836. SETUP_RUN(servo_init());
  837. #endif
  838. #if HAS_Z_SERVO_PROBE
  839. SETUP_RUN(probe.servo_probe_init());
  840. #endif
  841. #if HAS_PHOTOGRAPH
  842. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  843. #endif
  844. #if HAS_CUTTER
  845. SETUP_RUN(cutter.init());
  846. #endif
  847. #if ENABLED(COOLANT_MIST)
  848. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  849. #endif
  850. #if ENABLED(COOLANT_FLOOD)
  851. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  852. #endif
  853. #if HAS_BED_PROBE
  854. SETUP_RUN(endstops.enable_z_probe(false));
  855. #endif
  856. #if ENABLED(USE_CONTROLLER_FAN)
  857. SET_OUTPUT(CONTROLLER_FAN_PIN);
  858. #endif
  859. #if HAS_STEPPER_RESET
  860. SETUP_RUN(enableStepperDrivers());
  861. #endif
  862. #if ENABLED(DIGIPOT_I2C)
  863. SETUP_RUN(digipot_i2c_init());
  864. #endif
  865. #if ENABLED(DAC_STEPPER_CURRENT)
  866. SETUP_RUN(dac_init());
  867. #endif
  868. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  869. OUT_WRITE(SOL1_PIN, LOW); // OFF
  870. #endif
  871. #if HAS_HOME
  872. SET_INPUT_PULLUP(HOME_PIN);
  873. #endif
  874. #if PIN_EXISTS(STAT_LED_RED)
  875. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  876. #endif
  877. #if PIN_EXISTS(STAT_LED_BLUE)
  878. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  879. #endif
  880. #if HAS_CASE_LIGHT
  881. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  882. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  883. #endif
  884. SETUP_RUN(update_case_light());
  885. #endif
  886. #if ENABLED(MK2_MULTIPLEXER)
  887. SETUP_LOG("MK2_MULTIPLEXER");
  888. SET_OUTPUT(E_MUX0_PIN);
  889. SET_OUTPUT(E_MUX1_PIN);
  890. SET_OUTPUT(E_MUX2_PIN);
  891. #endif
  892. #if HAS_FANMUX
  893. SETUP_RUN(fanmux_init());
  894. #endif
  895. #if ENABLED(MIXING_EXTRUDER)
  896. SETUP_RUN(mixer.init());
  897. #endif
  898. #if ENABLED(BLTOUCH)
  899. SETUP_RUN(bltouch.init(/*set_voltage=*/true));
  900. #endif
  901. #if ENABLED(I2C_POSITION_ENCODERS)
  902. SETUP_RUN(I2CPEM.init());
  903. #endif
  904. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  905. SETUP_LOG("i2c...");
  906. i2c.onReceive(i2c_on_receive);
  907. i2c.onRequest(i2c_on_request);
  908. #endif
  909. #if DO_SWITCH_EXTRUDER
  910. SETUP_RUN(move_extruder_servo(0)); // Initialize extruder servo
  911. #endif
  912. #if ENABLED(SWITCHING_NOZZLE)
  913. SETUP_LOG("SWITCHING_NOZZLE");
  914. // Initialize nozzle servo(s)
  915. #if SWITCHING_NOZZLE_TWO_SERVOS
  916. lower_nozzle(0);
  917. raise_nozzle(1);
  918. #else
  919. move_nozzle_servo(0);
  920. #endif
  921. #endif
  922. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  923. SETUP_RUN(mpe_settings_init());
  924. #endif
  925. #if ENABLED(PARKING_EXTRUDER)
  926. SETUP_RUN(pe_solenoid_init());
  927. #endif
  928. #if ENABLED(SWITCHING_TOOLHEAD)
  929. swt_init();
  930. #endif
  931. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  932. SETUP_RUN(est_init());
  933. #endif
  934. #if ENABLED(POWER_LOSS_RECOVERY)
  935. SETUP_RUN(recovery.check());
  936. #endif
  937. #if ENABLED(USE_WATCHDOG)
  938. SETUP_RUN(watchdog_init()); // Reinit watchdog after HAL_get_reset_source call
  939. #endif
  940. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  941. SETUP_RUN(init_closedloop());
  942. #endif
  943. #ifdef STARTUP_COMMANDS
  944. SETUP_LOG("STARTUP_COMMANDS");
  945. queue.inject_P(PSTR(STARTUP_COMMANDS));
  946. #endif
  947. #if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
  948. SETUP_RUN(card.beginautostart());
  949. #endif
  950. #if ENABLED(HOST_PROMPT_SUPPORT)
  951. SETUP_RUN(host_action_prompt_end());
  952. #endif
  953. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  954. SETUP_RUN(test_tmc_connection(true, true, true, true));
  955. #endif
  956. #if ENABLED(PRUSA_MMU2)
  957. SETUP_RUN(mmu2.init());
  958. #endif
  959. #if ENABLED(MAX7219_DEBUG)
  960. SETUP_RUN(max7219.init());
  961. #endif
  962. SETUP_LOG("setup() completed.");
  963. }
  964. /**
  965. * The main Marlin program loop
  966. *
  967. * - Call idle() to handle all tasks between G-code commands
  968. * Note that no G-codes from the queue can be executed during idle()
  969. * but many G-codes can be called directly anytime like macros.
  970. * - Check whether SD card auto-start is needed now.
  971. * - Check whether SD print finishing is needed now.
  972. * - Run one G-code command from the immediate or main command queue
  973. * and open up one space. Commands in the main queue may come from sd
  974. * card, host, or by direct injection. The queue will continue to fill
  975. * as long as idle() or manage_inactivity() are being called.
  976. */
  977. void loop() {
  978. do {
  979. idle();
  980. #if ENABLED(SDSUPPORT)
  981. card.checkautostart();
  982. if (card.flag.abort_sd_printing) abortSDPrinting();
  983. if (card.sdprinting_done_state) finishSDPrinting();
  984. #endif
  985. queue.advance();
  986. endstops.event_handler();
  987. } while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
  988. }