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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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 UI input / draw events
  584. TERN(DWIN_CREALITY_LCD, DWIN_Update(), ui.update());
  585. // Handle USB Flash Drive insert / remove
  586. TERN_(USB_FLASH_DRIVE_SUPPORT, Sd2Card::idle());
  587. // Announce Host Keepalive state (if any)
  588. TERN_(HOST_KEEPALIVE_FEATURE, gcode.host_keepalive());
  589. // Update the Print Job Timer state
  590. TERN_(PRINTCOUNTER, print_job_timer.tick());
  591. // Update the Beeper queue
  592. TERN_(USE_BEEPER, buzzer.tick());
  593. // Read Buttons and Update the LCD
  594. ui.update();
  595. // Run i2c Position Encoders
  596. #if ENABLED(I2C_POSITION_ENCODERS)
  597. static millis_t i2cpem_next_update_ms;
  598. if (planner.has_blocks_queued()) {
  599. const millis_t ms = millis();
  600. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  601. I2CPEM.update();
  602. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  603. }
  604. }
  605. #endif
  606. // Auto-report Temperatures / SD Status
  607. #if HAS_AUTO_REPORTING
  608. if (!gcode.autoreport_paused) {
  609. TERN_(AUTO_REPORT_TEMPERATURES, thermalManager.auto_report_temperatures());
  610. TERN_(AUTO_REPORT_SD_STATUS, card.auto_report_sd_status());
  611. }
  612. #endif
  613. // Update the Prusa MMU2
  614. TERN_(PRUSA_MMU2, mmu2.mmu_loop());
  615. // Handle Joystick jogging
  616. TERN_(POLL_JOG, joystick.inject_jog_moves());
  617. // Direct Stepping
  618. TERN_(DIRECT_STEPPING, page_manager.write_responses());
  619. #if ENABLED(TFT_LITTLE_VGL_UI)
  620. LV_TASK_HANDLER();
  621. #endif
  622. }
  623. /**
  624. * Kill all activity and lock the machine.
  625. * After this the machine will need to be reset.
  626. */
  627. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  628. thermalManager.disable_all_heaters();
  629. TERN_(HAS_CUTTER, cutter.kill()); // Full cutter shutdown including ISR control
  630. SERIAL_ERROR_MSG(STR_ERR_KILLED);
  631. #if HAS_DISPLAY
  632. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component ?: NUL_STR);
  633. #else
  634. UNUSED(lcd_error);
  635. UNUSED(lcd_component);
  636. #endif
  637. #ifdef ACTION_ON_KILL
  638. host_action_kill();
  639. #endif
  640. minkill(steppers_off);
  641. }
  642. void minkill(const bool steppers_off/*=false*/) {
  643. // Wait a short time (allows messages to get out before shutting down.
  644. for (int i = 1000; i--;) DELAY_US(600);
  645. cli(); // Stop interrupts
  646. // Wait to ensure all interrupts stopped
  647. for (int i = 1000; i--;) DELAY_US(250);
  648. // Reiterate heaters off
  649. thermalManager.disable_all_heaters();
  650. TERN_(HAS_CUTTER, cutter.kill()); // Reiterate cutter shutdown
  651. // Power off all steppers (for M112) or just the E steppers
  652. steppers_off ? disable_all_steppers() : disable_e_steppers();
  653. TERN_(PSU_CONTROL, PSU_OFF());
  654. TERN_(HAS_SUICIDE, suicide());
  655. #if HAS_KILL
  656. // Wait for kill to be released
  657. while (kill_state()) watchdog_refresh();
  658. // Wait for kill to be pressed
  659. while (!kill_state()) watchdog_refresh();
  660. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  661. resetFunc(); // Jump to address 0
  662. #else
  663. for (;;) watchdog_refresh(); // Wait for reset
  664. #endif
  665. }
  666. /**
  667. * Turn off heaters and stop the print in progress
  668. * After a stop the machine may be resumed with M999
  669. */
  670. void stop() {
  671. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  672. print_job_timer.stop();
  673. #if ENABLED(PROBING_FANS_OFF)
  674. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  675. #endif
  676. if (IsRunning()) {
  677. SERIAL_ERROR_MSG(STR_ERR_STOPPED);
  678. LCD_MESSAGEPGM(MSG_STOPPED);
  679. safe_delay(350); // allow enough time for messages to get out before stopping
  680. marlin_state = MF_STOPPED;
  681. }
  682. }
  683. /**
  684. * Marlin entry-point: Set up before the program loop
  685. * - Set up the kill pin, filament runout, power hold
  686. * - Start the serial port
  687. * - Print startup messages and diagnostics
  688. * - Get EEPROM or default settings
  689. * - Initialize managers for:
  690. * • temperature
  691. * • planner
  692. * • watchdog
  693. * • stepper
  694. * • photo pin
  695. * • servos
  696. * • LCD controller
  697. * • Digipot I2C
  698. * • Z probe sled
  699. * • status LEDs
  700. * • Max7219
  701. */
  702. void setup() {
  703. #if ENABLED(MARLIN_DEV_MODE)
  704. auto log_current_ms = [&](PGM_P const msg) {
  705. SERIAL_ECHO_START();
  706. SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHO("] ");
  707. serialprintPGM(msg);
  708. SERIAL_EOL();
  709. };
  710. #define SETUP_LOG(M) log_current_ms(PSTR(M))
  711. #else
  712. #define SETUP_LOG(...) NOOP
  713. #endif
  714. #define SETUP_RUN(C) do{ SETUP_LOG(STRINGIFY(C)); C; }while(0)
  715. #if EITHER(DISABLE_DEBUG, DISABLE_JTAG)
  716. // Disable any hardware debug to free up pins for IO
  717. #if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
  718. JTAGSWD_DISABLE();
  719. #elif defined(JTAG_DISABLE)
  720. JTAG_DISABLE();
  721. #else
  722. #error "DISABLE_(DEBUG|JTAG) is not supported for the selected MCU/Board."
  723. #endif
  724. #endif
  725. #if NUM_SERIAL > 0
  726. MYSERIAL0.begin(BAUDRATE);
  727. uint32_t serial_connect_timeout = millis() + 1000UL;
  728. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  729. #if HAS_MULTI_SERIAL
  730. MYSERIAL1.begin(BAUDRATE);
  731. serial_connect_timeout = millis() + 1000UL;
  732. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  733. #endif
  734. SERIAL_ECHO_MSG("start");
  735. #endif
  736. SETUP_RUN(HAL_init());
  737. #if HAS_L64XX
  738. SETUP_RUN(L64xxManager.init()); // Set up SPI, init drivers
  739. #endif
  740. #if ENABLED(SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  741. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  742. #endif
  743. #if HAS_FILAMENT_SENSOR
  744. SETUP_RUN(runout.setup());
  745. #endif
  746. #if ENABLED(POWER_LOSS_RECOVERY)
  747. SETUP_RUN(recovery.setup());
  748. #endif
  749. SETUP_RUN(setup_killpin());
  750. #if HAS_TMC220x
  751. SETUP_RUN(tmc_serial_begin());
  752. #endif
  753. SETUP_RUN(setup_powerhold());
  754. #if HAS_STEPPER_RESET
  755. SETUP_RUN(disableStepperDrivers());
  756. #endif
  757. #if HAS_TMC_SPI
  758. #if DISABLED(TMC_USE_SW_SPI)
  759. SETUP_RUN(SPI.begin());
  760. #endif
  761. SETUP_RUN(tmc_init_cs_pins());
  762. #endif
  763. #ifdef BOARD_INIT
  764. SETUP_LOG("BOARD_INIT");
  765. BOARD_INIT();
  766. #endif
  767. SETUP_RUN(esp_wifi_init());
  768. // Check startup - does nothing if bootloader sets MCUSR to 0
  769. const byte mcu = HAL_get_reset_source();
  770. if (mcu & 1) SERIAL_ECHOLNPGM(STR_POWERUP);
  771. if (mcu & 2) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  772. if (mcu & 4) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  773. if (mcu & 8) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  774. if (mcu & 32) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  775. HAL_clear_reset_source();
  776. serialprintPGM(GET_TEXT(MSG_MARLIN));
  777. SERIAL_CHAR(' ');
  778. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  779. SERIAL_EOL();
  780. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  781. SERIAL_ECHO_MSG(
  782. STR_CONFIGURATION_VER
  783. STRING_DISTRIBUTION_DATE
  784. STR_AUTHOR STRING_CONFIG_H_AUTHOR
  785. );
  786. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  787. #endif
  788. SERIAL_ECHO_START();
  789. SERIAL_ECHOLNPAIR(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  790. // Set up LEDs early
  791. #if HAS_COLOR_LEDS
  792. SETUP_RUN(leds.setup());
  793. #endif
  794. #if ENABLED(USE_CONTROLLER_FAN) // Set up fan controller to initialize also the default configurations.
  795. SETUP_RUN(controllerFan.setup());
  796. #endif
  797. // UI must be initialized before EEPROM
  798. // (because EEPROM code calls the UI).
  799. #if ENABLED(DWIN_CREALITY_LCD)
  800. delay(800); // Required delay (since boot?)
  801. SERIAL_ECHOPGM("\nDWIN handshake ");
  802. if (DWIN_Handshake()) SERIAL_ECHOLNPGM("ok."); else SERIAL_ECHOLNPGM("error.");
  803. DWIN_Frame_SetDir(1); // Orientation 90°
  804. DWIN_UpdateLCD(); // Show bootscreen (first image)
  805. #else
  806. SETUP_RUN(ui.init());
  807. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  808. SETUP_RUN(ui.show_bootscreen());
  809. #endif
  810. SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)
  811. #endif
  812. #if BOTH(HAS_SPI_LCD, SHOW_BOOTSCREEN)
  813. SETUP_RUN(ui.show_bootscreen());
  814. #endif
  815. #if BOTH(SDSUPPORT, SDCARD_EEPROM_EMULATION)
  816. SETUP_RUN(card.mount()); // Mount media with settings before first_load
  817. #endif
  818. SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
  819. // This also updates variables in the planner, elsewhere
  820. #if HAS_SERVICE_INTERVALS
  821. SETUP_RUN(ui.reset_status(true)); // Show service messages or keep current status
  822. #endif
  823. #if ENABLED(TOUCH_BUTTONS)
  824. SETUP_RUN(touch.init());
  825. #endif
  826. TERN_(HAS_M206_COMMAND, current_position += home_offset); // Init current position based on home_offset
  827. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  828. SETUP_RUN(thermalManager.init()); // Initialize temperature loop
  829. SETUP_RUN(print_job_timer.init()); // Initial setup of print job timer
  830. SETUP_RUN(endstops.init()); // Init endstops and pullups
  831. SETUP_RUN(stepper.init()); // Init stepper. This enables interrupts!
  832. #if HAS_SERVOS
  833. SETUP_RUN(servo_init());
  834. #endif
  835. #if HAS_Z_SERVO_PROBE
  836. SETUP_RUN(probe.servo_probe_init());
  837. #endif
  838. #if HAS_PHOTOGRAPH
  839. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  840. #endif
  841. #if HAS_CUTTER
  842. SETUP_RUN(cutter.init());
  843. #endif
  844. #if ENABLED(COOLANT_MIST)
  845. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  846. #endif
  847. #if ENABLED(COOLANT_FLOOD)
  848. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  849. #endif
  850. #if HAS_BED_PROBE
  851. SETUP_RUN(endstops.enable_z_probe(false));
  852. #endif
  853. #if HAS_STEPPER_RESET
  854. SETUP_RUN(enableStepperDrivers());
  855. #endif
  856. #if HAS_I2C_DIGIPOT
  857. SETUP_RUN(digipot_i2c_init());
  858. #endif
  859. #if ENABLED(DAC_STEPPER_CURRENT)
  860. SETUP_RUN(dac_init());
  861. #endif
  862. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  863. OUT_WRITE(SOL1_PIN, LOW); // OFF
  864. #endif
  865. #if HAS_HOME
  866. SET_INPUT_PULLUP(HOME_PIN);
  867. #endif
  868. #if PIN_EXISTS(STAT_LED_RED)
  869. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  870. #endif
  871. #if PIN_EXISTS(STAT_LED_BLUE)
  872. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  873. #endif
  874. #if HAS_CASE_LIGHT
  875. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  876. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  877. #endif
  878. SETUP_RUN(update_case_light());
  879. #endif
  880. #if ENABLED(MK2_MULTIPLEXER)
  881. SETUP_LOG("MK2_MULTIPLEXER");
  882. SET_OUTPUT(E_MUX0_PIN);
  883. SET_OUTPUT(E_MUX1_PIN);
  884. SET_OUTPUT(E_MUX2_PIN);
  885. #endif
  886. #if HAS_FANMUX
  887. SETUP_RUN(fanmux_init());
  888. #endif
  889. #if ENABLED(MIXING_EXTRUDER)
  890. SETUP_RUN(mixer.init());
  891. #endif
  892. #if ENABLED(BLTOUCH)
  893. SETUP_RUN(bltouch.init(/*set_voltage=*/true));
  894. #endif
  895. #if ENABLED(I2C_POSITION_ENCODERS)
  896. SETUP_RUN(I2CPEM.init());
  897. #endif
  898. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  899. SETUP_LOG("i2c...");
  900. i2c.onReceive(i2c_on_receive);
  901. i2c.onRequest(i2c_on_request);
  902. #endif
  903. #if DO_SWITCH_EXTRUDER
  904. SETUP_RUN(move_extruder_servo(0)); // Initialize extruder servo
  905. #endif
  906. #if ENABLED(SWITCHING_NOZZLE)
  907. SETUP_LOG("SWITCHING_NOZZLE");
  908. // Initialize nozzle servo(s)
  909. #if SWITCHING_NOZZLE_TWO_SERVOS
  910. lower_nozzle(0);
  911. raise_nozzle(1);
  912. #else
  913. move_nozzle_servo(0);
  914. #endif
  915. #endif
  916. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  917. SETUP_RUN(mpe_settings_init());
  918. #endif
  919. #if ENABLED(PARKING_EXTRUDER)
  920. SETUP_RUN(pe_solenoid_init());
  921. #endif
  922. #if ENABLED(SWITCHING_TOOLHEAD)
  923. SETUP_RUN(swt_init());
  924. #endif
  925. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  926. SETUP_RUN(est_init());
  927. #endif
  928. #if ENABLED(USE_WATCHDOG)
  929. SETUP_RUN(watchdog_init()); // Reinit watchdog after HAL_get_reset_source call
  930. #endif
  931. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  932. SETUP_RUN(init_closedloop());
  933. #endif
  934. #ifdef STARTUP_COMMANDS
  935. SETUP_LOG("STARTUP_COMMANDS");
  936. queue.inject_P(PSTR(STARTUP_COMMANDS));
  937. #endif
  938. #if ENABLED(HOST_PROMPT_SUPPORT)
  939. SETUP_RUN(host_action_prompt_end());
  940. #endif
  941. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  942. SETUP_RUN(test_tmc_connection(true, true, true, true));
  943. #endif
  944. #if ENABLED(PRUSA_MMU2)
  945. SETUP_RUN(mmu2.init());
  946. #endif
  947. #if ENABLED(IIC_BL24CXX_EEPROM)
  948. BL24CXX::init();
  949. const uint8_t err = BL24CXX::check();
  950. SERIAL_ECHO_TERNARY(err, "I2C_EEPROM Check ", "failed", "succeeded", "!\n");
  951. #endif
  952. #if ENABLED(DWIN_CREALITY_LCD)
  953. Encoder_Configuration();
  954. HMI_Init();
  955. HMI_StartFrame(true);
  956. #endif
  957. #if HAS_SERVICE_INTERVALS && DISABLED(DWIN_CREALITY_LCD)
  958. ui.reset_status(true); // Show service messages or keep current status
  959. #endif
  960. #if ENABLED(MAX7219_DEBUG)
  961. SETUP_RUN(max7219.init());
  962. #endif
  963. #if ENABLED(DIRECT_STEPPING)
  964. SETUP_RUN(page_manager.init());
  965. #endif
  966. #if ENABLED(TFT_LITTLE_VGL_UI)
  967. SETUP_RUN(tft_lvgl_init());
  968. #endif
  969. marlin_state = MF_RUNNING;
  970. SETUP_LOG("setup() completed.");
  971. }
  972. /**
  973. * The main Marlin program loop
  974. *
  975. * - Call idle() to handle all tasks between G-code commands
  976. * Note that no G-codes from the queue can be executed during idle()
  977. * but many G-codes can be called directly anytime like macros.
  978. * - Check whether SD card auto-start is needed now.
  979. * - Check whether SD print finishing is needed now.
  980. * - Run one G-code command from the immediate or main command queue
  981. * and open up one space. Commands in the main queue may come from sd
  982. * card, host, or by direct injection. The queue will continue to fill
  983. * as long as idle() or manage_inactivity() are being called.
  984. */
  985. void loop() {
  986. do {
  987. idle();
  988. #if ENABLED(SDSUPPORT)
  989. card.checkautostart();
  990. if (card.flag.abort_sd_printing) abortSDPrinting();
  991. if (marlin_state == MF_SD_COMPLETE) finishSDPrinting();
  992. #endif
  993. queue.advance();
  994. endstops.event_handler();
  995. TERN_(TFT_LITTLE_VGL_UI, printer_state_polling());
  996. } while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
  997. }