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

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