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