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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  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 <https://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. #if ENABLED(MARLIN_DEV_MODE)
  31. #warning "WARNING! Disable MARLIN_DEV_MODE for the final build!"
  32. #endif
  33. #include "HAL/shared/Delay.h"
  34. #include "HAL/shared/esp_wifi.h"
  35. #include "HAL/shared/cpu_exception/exception_hook.h"
  36. #ifdef ARDUINO
  37. #include <pins_arduino.h>
  38. #endif
  39. #include <math.h>
  40. #include "core/utility.h"
  41. #include "module/motion.h"
  42. #include "module/planner.h"
  43. #include "module/endstops.h"
  44. #include "module/temperature.h"
  45. #include "module/settings.h"
  46. #include "module/printcounter.h" // PrintCounter or Stopwatch
  47. #include "module/stepper.h"
  48. #include "module/stepper/indirection.h"
  49. #include "gcode/gcode.h"
  50. #include "gcode/parser.h"
  51. #include "gcode/queue.h"
  52. #include "feature/pause.h"
  53. #include "sd/cardreader.h"
  54. #include "lcd/marlinui.h"
  55. #if HAS_TOUCH_BUTTONS
  56. #include "lcd/touch/touch_buttons.h"
  57. #endif
  58. #if HAS_TFT_LVGL_UI
  59. #include "lcd/extui/mks_ui/tft_lvgl_configuration.h"
  60. #include "lcd/extui/mks_ui/draw_ui.h"
  61. #include "lcd/extui/mks_ui/mks_hardware_test.h"
  62. #include <lvgl.h>
  63. #endif
  64. #if ENABLED(DWIN_CREALITY_LCD)
  65. #include "lcd/dwin/e3v2/dwin.h"
  66. #include "lcd/dwin/e3v2/rotary_encoder.h"
  67. #endif
  68. #if ENABLED(EXTENSIBLE_UI)
  69. #include "lcd/extui/ui_api.h"
  70. #endif
  71. #if HAS_ETHERNET
  72. #include "feature/ethernet.h"
  73. #endif
  74. #if ENABLED(IIC_BL24CXX_EEPROM)
  75. #include "libs/BL24CXX.h"
  76. #endif
  77. #if ENABLED(DIRECT_STEPPING)
  78. #include "feature/direct_stepping.h"
  79. #endif
  80. #if ENABLED(HOST_ACTION_COMMANDS)
  81. #include "feature/host_actions.h"
  82. #endif
  83. #if USE_BEEPER
  84. #include "libs/buzzer.h"
  85. #endif
  86. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  87. #include "feature/closedloop.h"
  88. #endif
  89. #if HAS_MOTOR_CURRENT_I2C
  90. #include "feature/digipot/digipot.h"
  91. #endif
  92. #if ENABLED(MIXING_EXTRUDER)
  93. #include "feature/mixing.h"
  94. #endif
  95. #if ENABLED(MAX7219_DEBUG)
  96. #include "feature/max7219.h"
  97. #endif
  98. #if HAS_COLOR_LEDS
  99. #include "feature/leds/leds.h"
  100. #endif
  101. #if ENABLED(BLTOUCH)
  102. #include "feature/bltouch.h"
  103. #endif
  104. #if ENABLED(POLL_JOG)
  105. #include "feature/joystick.h"
  106. #endif
  107. #if HAS_SERVOS
  108. #include "module/servo.h"
  109. #endif
  110. #if ENABLED(HAS_MOTOR_CURRENT_DAC)
  111. #include "feature/dac/stepper_dac.h"
  112. #endif
  113. #if ENABLED(EXPERIMENTAL_I2CBUS)
  114. #include "feature/twibus.h"
  115. #endif
  116. #if ENABLED(I2C_POSITION_ENCODERS)
  117. #include "feature/encoder_i2c.h"
  118. #endif
  119. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  120. #include "feature/tmc_util.h"
  121. #endif
  122. #if HAS_CUTTER
  123. #include "feature/spindle_laser.h"
  124. #endif
  125. #if ENABLED(SDSUPPORT)
  126. CardReader card;
  127. #endif
  128. #if ENABLED(G38_PROBE_TARGET)
  129. uint8_t G38_move; // = 0
  130. bool G38_did_trigger; // = false
  131. #endif
  132. #if ENABLED(DELTA)
  133. #include "module/delta.h"
  134. #elif IS_SCARA
  135. #include "module/scara.h"
  136. #endif
  137. #if HAS_LEVELING
  138. #include "feature/bedlevel/bedlevel.h"
  139. #endif
  140. #if ENABLED(GCODE_REPEAT_MARKERS)
  141. #include "feature/repeat.h"
  142. #endif
  143. #if ENABLED(POWER_LOSS_RECOVERY)
  144. #include "feature/powerloss.h"
  145. #endif
  146. #if ENABLED(CANCEL_OBJECTS)
  147. #include "feature/cancel_object.h"
  148. #endif
  149. #if HAS_FILAMENT_SENSOR
  150. #include "feature/runout.h"
  151. #endif
  152. #if EITHER(PROBE_TARE, HAS_Z_SERVO_PROBE)
  153. #include "module/probe.h"
  154. #endif
  155. #if ENABLED(HOTEND_IDLE_TIMEOUT)
  156. #include "feature/hotend_idle.h"
  157. #endif
  158. #if ENABLED(TEMP_STAT_LEDS)
  159. #include "feature/leds/tempstat.h"
  160. #endif
  161. #if ENABLED(CASE_LIGHT_ENABLE)
  162. #include "feature/caselight.h"
  163. #endif
  164. #if HAS_FANMUX
  165. #include "feature/fanmux.h"
  166. #endif
  167. #include "module/tool_change.h"
  168. #if ENABLED(USE_CONTROLLER_FAN)
  169. #include "feature/controllerfan.h"
  170. #endif
  171. #if HAS_PRUSA_MMU2
  172. #include "feature/mmu/mmu2.h"
  173. #endif
  174. #if HAS_L64XX
  175. #include "libs/L64XX/L64XX_Marlin.h"
  176. #endif
  177. #if ENABLED(PASSWORD_FEATURE)
  178. #include "feature/password/password.h"
  179. #endif
  180. #if ENABLED(DGUS_LCD_UI_MKS)
  181. #include "lcd/extui/dgus/DGUSScreenHandler.h"
  182. #endif
  183. #if HAS_DRIVER_SAFE_POWER_PROTECT
  184. #include "feature/stepper_driver_safety.h"
  185. #endif
  186. PGMSTR(M112_KILL_STR, "M112 Shutdown");
  187. MarlinState marlin_state = MF_INITIALIZING;
  188. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  189. bool wait_for_heatup = true;
  190. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  191. #if HAS_RESUME_CONTINUE
  192. bool wait_for_user; // = false;
  193. void wait_for_user_response(millis_t ms/*=0*/, const bool no_sleep/*=false*/) {
  194. UNUSED(no_sleep);
  195. KEEPALIVE_STATE(PAUSED_FOR_USER);
  196. wait_for_user = true;
  197. if (ms) ms += millis(); // expire time
  198. while (wait_for_user && !(ms && ELAPSED(millis(), ms)))
  199. idle(TERN_(ADVANCED_PAUSE_FEATURE, no_sleep));
  200. wait_for_user = false;
  201. }
  202. #endif
  203. /**
  204. * ***************************************************************************
  205. * ******************************** FUNCTIONS ********************************
  206. * ***************************************************************************
  207. */
  208. /**
  209. * Stepper Reset (RigidBoard, et.al.)
  210. */
  211. #if HAS_STEPPER_RESET
  212. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  213. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  214. #endif
  215. /**
  216. * Sensitive pin test for M42, M226
  217. */
  218. #include "pins/sensitive_pins.h"
  219. #pragma GCC diagnostic push
  220. #pragma GCC diagnostic ignored "-Wnarrowing"
  221. #ifndef RUNTIME_ONLY_ANALOG_TO_DIGITAL
  222. template <pin_t ...D>
  223. constexpr pin_t OnlyPins<_SP_END, D...>::table[sizeof...(D)];
  224. #endif
  225. bool pin_is_protected(const pin_t pin) {
  226. #ifdef RUNTIME_ONLY_ANALOG_TO_DIGITAL
  227. static const pin_t sensitive_pins[] PROGMEM = { SENSITIVE_PINS };
  228. const size_t pincount = COUNT(sensitive_pins);
  229. #else
  230. static constexpr size_t pincount = OnlyPins<SENSITIVE_PINS>::size;
  231. static const pin_t (&sensitive_pins)[pincount] PROGMEM = OnlyPins<SENSITIVE_PINS>::table;
  232. #endif
  233. LOOP_L_N(i, pincount) {
  234. const pin_t * const pptr = &sensitive_pins[i];
  235. if (pin == (sizeof(pin_t) == 2 ? (pin_t)pgm_read_word(pptr) : (pin_t)pgm_read_byte(pptr))) return true;
  236. }
  237. return false;
  238. }
  239. #pragma GCC diagnostic pop
  240. void enable_e_steppers() {
  241. #define _ENA_E(N) ENABLE_AXIS_E##N();
  242. REPEAT(E_STEPPERS, _ENA_E)
  243. }
  244. void enable_all_steppers() {
  245. TERN_(AUTO_POWER_CONTROL, powerManager.power_on());
  246. ENABLE_AXIS_X();
  247. ENABLE_AXIS_Y();
  248. ENABLE_AXIS_Z();
  249. ENABLE_AXIS_I(); // Marlin 6-axis support by DerAndere (https://github.com/DerAndere1/Marlin/wiki)
  250. ENABLE_AXIS_J();
  251. ENABLE_AXIS_K();
  252. enable_e_steppers();
  253. TERN_(EXTENSIBLE_UI, ExtUI::onSteppersEnabled());
  254. }
  255. void disable_e_steppers() {
  256. #define _DIS_E(N) DISABLE_AXIS_E##N();
  257. REPEAT(E_STEPPERS, _DIS_E)
  258. }
  259. void disable_e_stepper(const uint8_t e) {
  260. #define _CASE_DIS_E(N) case N: DISABLE_AXIS_E##N(); break;
  261. switch (e) {
  262. REPEAT(E_STEPPERS, _CASE_DIS_E)
  263. }
  264. }
  265. void disable_all_steppers() {
  266. DISABLE_AXIS_X();
  267. DISABLE_AXIS_Y();
  268. DISABLE_AXIS_Z();
  269. DISABLE_AXIS_I();
  270. DISABLE_AXIS_J();
  271. DISABLE_AXIS_K();
  272. disable_e_steppers();
  273. TERN_(EXTENSIBLE_UI, ExtUI::onSteppersDisabled());
  274. }
  275. /**
  276. * A Print Job exists when the timer is running or SD is printing
  277. */
  278. bool printJobOngoing() { return print_job_timer.isRunning() || IS_SD_PRINTING(); }
  279. /**
  280. * Printing is active when a job is underway but not paused
  281. */
  282. bool printingIsActive() { return !did_pause_print && printJobOngoing(); }
  283. /**
  284. * Printing is paused according to SD or host indicators
  285. */
  286. bool printingIsPaused() {
  287. return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
  288. }
  289. void startOrResumeJob() {
  290. if (!printingIsPaused()) {
  291. TERN_(GCODE_REPEAT_MARKERS, repeat.reset());
  292. TERN_(CANCEL_OBJECTS, cancelable.reset());
  293. TERN_(LCD_SHOW_E_TOTAL, e_move_accumulator = 0);
  294. #if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
  295. ui.reset_remaining_time();
  296. #endif
  297. }
  298. print_job_timer.start();
  299. }
  300. #if ENABLED(SDSUPPORT)
  301. inline void abortSDPrinting() {
  302. IF_DISABLED(NO_SD_AUTOSTART, card.autofile_cancel());
  303. card.abortFilePrintNow(TERN_(SD_RESORT, true));
  304. queue.clear();
  305. quickstop_stepper();
  306. print_job_timer.abort();
  307. IF_DISABLED(SD_ABORT_NO_COOLDOWN, thermalManager.disable_all_heaters());
  308. TERN(HAS_CUTTER, cutter.kill(), thermalManager.zero_fan_speeds()); // Full cutter shutdown including ISR control
  309. wait_for_heatup = false;
  310. TERN_(POWER_LOSS_RECOVERY, recovery.purge());
  311. #ifdef EVENT_GCODE_SD_ABORT
  312. queue.inject_P(PSTR(EVENT_GCODE_SD_ABORT));
  313. #endif
  314. TERN_(PASSWORD_AFTER_SD_PRINT_ABORT, password.lock_machine());
  315. }
  316. inline void finishSDPrinting() {
  317. if (queue.enqueue_one_P(PSTR("M1001"))) { // Keep trying until it gets queued
  318. marlin_state = MF_RUNNING; // Signal to stop trying
  319. TERN_(PASSWORD_AFTER_SD_PRINT_END, password.lock_machine());
  320. TERN_(DGUS_LCD_UI_MKS, ScreenHandler.SDPrintingFinished());
  321. }
  322. }
  323. #endif // SDSUPPORT
  324. /**
  325. * Minimal management of Marlin's core activities:
  326. * - Keep the command buffer full
  327. * - Check for maximum inactive time between commands
  328. * - Check for maximum inactive time between stepper commands
  329. * - Check if CHDK_PIN needs to go LOW
  330. * - Check for KILL button held down
  331. * - Check for HOME button held down
  332. * - Check for CUSTOM USER button held down
  333. * - Check if cooling fan needs to be switched on
  334. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  335. * - Pulse FET_SAFETY_PIN if it exists
  336. */
  337. inline void manage_inactivity(const bool no_stepper_sleep=false) {
  338. queue.get_available_commands();
  339. const millis_t ms = millis();
  340. // Prevent steppers timing-out
  341. const bool do_reset_timeout = no_stepper_sleep
  342. || TERN0(PAUSE_PARK_NO_STEPPER_TIMEOUT, did_pause_print);
  343. // Reset both the M18/M84 activity timeout and the M85 max 'kill' timeout
  344. if (do_reset_timeout) gcode.reset_stepper_timeout(ms);
  345. if (gcode.stepper_max_timed_out(ms)) {
  346. SERIAL_ERROR_MSG(STR_KILL_INACTIVE_TIME, parser.command_ptr);
  347. kill();
  348. }
  349. // M18 / M84 : Handle steppers inactive time timeout
  350. if (gcode.stepper_inactive_time) {
  351. static bool already_shutdown_steppers; // = false
  352. // Any moves in the planner? Resets both the M18/M84
  353. // activity timeout and the M85 max 'kill' timeout
  354. if (planner.has_blocks_queued())
  355. gcode.reset_stepper_timeout(ms);
  356. else if (!do_reset_timeout && gcode.stepper_inactive_timeout()) {
  357. if (!already_shutdown_steppers) {
  358. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  359. // Individual axes will be disabled if configured
  360. if (ENABLED(DISABLE_INACTIVE_X)) DISABLE_AXIS_X();
  361. if (ENABLED(DISABLE_INACTIVE_Y)) DISABLE_AXIS_Y();
  362. if (ENABLED(DISABLE_INACTIVE_Z)) DISABLE_AXIS_Z();
  363. if (ENABLED(DISABLE_INACTIVE_I)) DISABLE_AXIS_I();
  364. if (ENABLED(DISABLE_INACTIVE_J)) DISABLE_AXIS_J();
  365. if (ENABLED(DISABLE_INACTIVE_K)) DISABLE_AXIS_K();
  366. if (ENABLED(DISABLE_INACTIVE_E)) disable_e_steppers();
  367. TERN_(AUTO_BED_LEVELING_UBL, ubl.steppers_were_disabled());
  368. }
  369. }
  370. else
  371. already_shutdown_steppers = false;
  372. }
  373. #if ENABLED(PHOTO_GCODE) && PIN_EXISTS(CHDK)
  374. // Check if CHDK should be set to LOW (after M240 set it HIGH)
  375. extern millis_t chdk_timeout;
  376. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  377. chdk_timeout = 0;
  378. WRITE(CHDK_PIN, LOW);
  379. }
  380. #endif
  381. #if HAS_KILL
  382. // Check if the kill button was pressed and wait just in case it was an accidental
  383. // key kill key press
  384. // -------------------------------------------------------------------------------
  385. static int killCount = 0; // make the inactivity button a bit less responsive
  386. const int KILL_DELAY = 750;
  387. if (kill_state())
  388. killCount++;
  389. else if (killCount > 0)
  390. killCount--;
  391. // Exceeded threshold and we can confirm that it was not accidental
  392. // KILL the machine
  393. // ----------------------------------------------------------------
  394. if (killCount >= KILL_DELAY) {
  395. SERIAL_ERROR_MSG(STR_KILL_BUTTON);
  396. kill();
  397. }
  398. #endif
  399. #if HAS_FREEZE_PIN
  400. Stepper::frozen = !READ(FREEZE_PIN);
  401. #endif
  402. #if HAS_HOME
  403. // Handle a standalone HOME button
  404. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  405. static millis_t next_home_key_ms; // = 0
  406. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  407. const millis_t ms = millis();
  408. if (ELAPSED(ms, next_home_key_ms)) {
  409. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  410. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  411. queue.inject_P(G28_STR);
  412. }
  413. }
  414. #endif
  415. #if ENABLED(CUSTOM_USER_BUTTONS)
  416. // Handle a custom user button if defined
  417. const bool printer_not_busy = !printingIsActive();
  418. #define HAS_CUSTOM_USER_BUTTON(N) (PIN_EXISTS(BUTTON##N) && defined(BUTTON##N##_HIT_STATE) && defined(BUTTON##N##_GCODE) && defined(BUTTON##N##_DESC))
  419. #define CHECK_CUSTOM_USER_BUTTON(N) do{ \
  420. constexpr millis_t CUB_DEBOUNCE_DELAY_##N = 250UL; \
  421. static millis_t next_cub_ms_##N; \
  422. if (BUTTON##N##_HIT_STATE == READ(BUTTON##N##_PIN) \
  423. && (ENABLED(BUTTON##N##_WHEN_PRINTING) || printer_not_busy)) { \
  424. const millis_t ms = millis(); \
  425. if (ELAPSED(ms, next_cub_ms_##N)) { \
  426. next_cub_ms_##N = ms + CUB_DEBOUNCE_DELAY_##N; \
  427. if (strlen(BUTTON##N##_DESC)) \
  428. LCD_MESSAGEPGM_P(PSTR(BUTTON##N##_DESC)); \
  429. queue.inject_P(PSTR(BUTTON##N##_GCODE)); \
  430. } \
  431. } \
  432. }while(0)
  433. #if HAS_CUSTOM_USER_BUTTON(1)
  434. CHECK_CUSTOM_USER_BUTTON(1);
  435. #endif
  436. #if HAS_CUSTOM_USER_BUTTON(2)
  437. CHECK_CUSTOM_USER_BUTTON(2);
  438. #endif
  439. #if HAS_CUSTOM_USER_BUTTON(3)
  440. CHECK_CUSTOM_USER_BUTTON(3);
  441. #endif
  442. #if HAS_CUSTOM_USER_BUTTON(4)
  443. CHECK_CUSTOM_USER_BUTTON(4);
  444. #endif
  445. #if HAS_CUSTOM_USER_BUTTON(5)
  446. CHECK_CUSTOM_USER_BUTTON(5);
  447. #endif
  448. #if HAS_CUSTOM_USER_BUTTON(6)
  449. CHECK_CUSTOM_USER_BUTTON(6);
  450. #endif
  451. #if HAS_CUSTOM_USER_BUTTON(7)
  452. CHECK_CUSTOM_USER_BUTTON(7);
  453. #endif
  454. #if HAS_CUSTOM_USER_BUTTON(8)
  455. CHECK_CUSTOM_USER_BUTTON(8);
  456. #endif
  457. #if HAS_CUSTOM_USER_BUTTON(9)
  458. CHECK_CUSTOM_USER_BUTTON(9);
  459. #endif
  460. #if HAS_CUSTOM_USER_BUTTON(10)
  461. CHECK_CUSTOM_USER_BUTTON(10);
  462. #endif
  463. #if HAS_CUSTOM_USER_BUTTON(11)
  464. CHECK_CUSTOM_USER_BUTTON(11);
  465. #endif
  466. #if HAS_CUSTOM_USER_BUTTON(12)
  467. CHECK_CUSTOM_USER_BUTTON(12);
  468. #endif
  469. #if HAS_CUSTOM_USER_BUTTON(13)
  470. CHECK_CUSTOM_USER_BUTTON(13);
  471. #endif
  472. #if HAS_CUSTOM_USER_BUTTON(14)
  473. CHECK_CUSTOM_USER_BUTTON(14);
  474. #endif
  475. #if HAS_CUSTOM_USER_BUTTON(15)
  476. CHECK_CUSTOM_USER_BUTTON(15);
  477. #endif
  478. #if HAS_CUSTOM_USER_BUTTON(16)
  479. CHECK_CUSTOM_USER_BUTTON(16);
  480. #endif
  481. #if HAS_CUSTOM_USER_BUTTON(17)
  482. CHECK_CUSTOM_USER_BUTTON(17);
  483. #endif
  484. #if HAS_CUSTOM_USER_BUTTON(18)
  485. CHECK_CUSTOM_USER_BUTTON(18);
  486. #endif
  487. #if HAS_CUSTOM_USER_BUTTON(19)
  488. CHECK_CUSTOM_USER_BUTTON(19);
  489. #endif
  490. #if HAS_CUSTOM_USER_BUTTON(20)
  491. CHECK_CUSTOM_USER_BUTTON(20);
  492. #endif
  493. #if HAS_CUSTOM_USER_BUTTON(21)
  494. CHECK_CUSTOM_USER_BUTTON(21);
  495. #endif
  496. #if HAS_CUSTOM_USER_BUTTON(22)
  497. CHECK_CUSTOM_USER_BUTTON(22);
  498. #endif
  499. #if HAS_CUSTOM_USER_BUTTON(23)
  500. CHECK_CUSTOM_USER_BUTTON(23);
  501. #endif
  502. #if HAS_CUSTOM_USER_BUTTON(24)
  503. CHECK_CUSTOM_USER_BUTTON(24);
  504. #endif
  505. #if HAS_CUSTOM_USER_BUTTON(25)
  506. CHECK_CUSTOM_USER_BUTTON(25);
  507. #endif
  508. #endif
  509. TERN_(USE_CONTROLLER_FAN, controllerFan.update()); // Check if fan should be turned on to cool stepper drivers down
  510. TERN_(AUTO_POWER_CONTROL, powerManager.check(!ui.on_status_screen() || printJobOngoing() || printingIsPaused()));
  511. TERN_(HOTEND_IDLE_TIMEOUT, hotend_idle.check());
  512. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  513. if (thermalManager.degHotend(active_extruder) > (EXTRUDER_RUNOUT_MINTEMP)
  514. && ELAPSED(ms, gcode.previous_move_ms + SEC_TO_MS(EXTRUDER_RUNOUT_SECONDS))
  515. && !planner.has_blocks_queued()
  516. ) {
  517. #if ENABLED(SWITCHING_EXTRUDER)
  518. bool oldstatus;
  519. switch (active_extruder) {
  520. default: oldstatus = E0_ENABLE_READ(); ENABLE_AXIS_E0(); break;
  521. #if E_STEPPERS > 1
  522. case 2: case 3: oldstatus = E1_ENABLE_READ(); ENABLE_AXIS_E1(); break;
  523. #if E_STEPPERS > 2
  524. case 4: case 5: oldstatus = E2_ENABLE_READ(); ENABLE_AXIS_E2(); break;
  525. #if E_STEPPERS > 3
  526. case 6: case 7: oldstatus = E3_ENABLE_READ(); ENABLE_AXIS_E3(); break;
  527. #endif // E_STEPPERS > 3
  528. #endif // E_STEPPERS > 2
  529. #endif // E_STEPPERS > 1
  530. }
  531. #else // !SWITCHING_EXTRUDER
  532. bool oldstatus;
  533. switch (active_extruder) {
  534. default:
  535. #define _CASE_EN(N) case N: oldstatus = E##N##_ENABLE_READ(); ENABLE_AXIS_E##N(); break;
  536. REPEAT(E_STEPPERS, _CASE_EN);
  537. }
  538. #endif
  539. const float olde = current_position.e;
  540. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  541. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  542. current_position.e = olde;
  543. planner.set_e_position_mm(olde);
  544. planner.synchronize();
  545. #if ENABLED(SWITCHING_EXTRUDER)
  546. switch (active_extruder) {
  547. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  548. #if E_STEPPERS > 1
  549. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  550. #if E_STEPPERS > 2
  551. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  552. #endif // E_STEPPERS > 2
  553. #endif // E_STEPPERS > 1
  554. }
  555. #else // !SWITCHING_EXTRUDER
  556. switch (active_extruder) {
  557. #define _CASE_RESTORE(N) case N: E##N##_ENABLE_WRITE(oldstatus); break;
  558. REPEAT(E_STEPPERS, _CASE_RESTORE);
  559. }
  560. #endif // !SWITCHING_EXTRUDER
  561. gcode.reset_stepper_timeout(ms);
  562. }
  563. #endif // EXTRUDER_RUNOUT_PREVENT
  564. #if ENABLED(DUAL_X_CARRIAGE)
  565. // handle delayed move timeout
  566. if (delayed_move_time && ELAPSED(ms, delayed_move_time) && IsRunning()) {
  567. // travel moves have been received so enact them
  568. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  569. destination = current_position;
  570. prepare_line_to_destination();
  571. planner.synchronize();
  572. }
  573. #endif
  574. TERN_(TEMP_STAT_LEDS, handle_status_leds());
  575. TERN_(MONITOR_DRIVER_STATUS, monitor_tmc_drivers());
  576. TERN_(MONITOR_L6470_DRIVER_STATUS, L64xxManager.monitor_driver());
  577. // Limit check_axes_activity frequency to 10Hz
  578. static millis_t next_check_axes_ms = 0;
  579. if (ELAPSED(ms, next_check_axes_ms)) {
  580. planner.check_axes_activity();
  581. next_check_axes_ms = ms + 100UL;
  582. }
  583. #if PIN_EXISTS(FET_SAFETY)
  584. static millis_t FET_next;
  585. if (ELAPSED(ms, FET_next)) {
  586. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  587. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  588. DELAY_US(2);
  589. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  590. }
  591. #endif
  592. }
  593. /**
  594. * Standard idle routine keeps the machine alive:
  595. * - Core Marlin activities
  596. * - Manage heaters (and Watchdog)
  597. * - Max7219 heartbeat, animation, etc.
  598. *
  599. * Only after setup() is complete:
  600. * - Handle filament runout sensors
  601. * - Run HAL idle tasks
  602. * - Handle Power-Loss Recovery
  603. * - Run StallGuard endstop checks
  604. * - Handle SD Card insert / remove
  605. * - Handle USB Flash Drive insert / remove
  606. * - Announce Host Keepalive state (if any)
  607. * - Update the Print Job Timer state
  608. * - Update the Beeper queue
  609. * - Read Buttons and Update the LCD
  610. * - Run i2c Position Encoders
  611. * - Auto-report Temperatures / SD Status
  612. * - Update the Průša MMU2
  613. * - Handle Joystick jogging
  614. */
  615. void idle(bool no_stepper_sleep/*=false*/) {
  616. #if ENABLED(MARLIN_DEV_MODE)
  617. static uint16_t idle_depth = 0;
  618. if (++idle_depth > 5) SERIAL_ECHOLNPAIR("idle() call depth: ", idle_depth);
  619. #endif
  620. // Core Marlin activities
  621. manage_inactivity(no_stepper_sleep);
  622. // Manage Heaters (and Watchdog)
  623. thermalManager.manage_heater();
  624. // Max7219 heartbeat, animation, etc
  625. TERN_(MAX7219_DEBUG, max7219.idle_tasks());
  626. // Return if setup() isn't completed
  627. if (marlin_state == MF_INITIALIZING) goto IDLE_DONE;
  628. // TODO: Still causing errors
  629. (void)check_tool_sensor_stats(active_extruder, true);
  630. // Handle filament runout sensors
  631. TERN_(HAS_FILAMENT_SENSOR, runout.run());
  632. // Run HAL idle tasks
  633. TERN_(HAL_IDLETASK, HAL_idletask());
  634. // Check network connection
  635. TERN_(HAS_ETHERNET, ethernet.check());
  636. // Handle Power-Loss Recovery
  637. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  638. if (IS_SD_PRINTING()) recovery.outage();
  639. #endif
  640. // Run StallGuard endstop checks
  641. #if ENABLED(SPI_ENDSTOPS)
  642. if (endstops.tmc_spi_homing.any
  643. && TERN1(IMPROVE_HOMING_RELIABILITY, ELAPSED(millis(), sg_guard_period))
  644. ) LOOP_L_N(i, 4) // Read SGT 4 times per idle loop
  645. if (endstops.tmc_spi_homing_check()) break;
  646. #endif
  647. // Handle SD Card insert / remove
  648. TERN_(SDSUPPORT, card.manage_media());
  649. // Handle USB Flash Drive insert / remove
  650. TERN_(USB_FLASH_DRIVE_SUPPORT, card.diskIODriver()->idle());
  651. // Announce Host Keepalive state (if any)
  652. TERN_(HOST_KEEPALIVE_FEATURE, gcode.host_keepalive());
  653. // Update the Print Job Timer state
  654. TERN_(PRINTCOUNTER, print_job_timer.tick());
  655. // Update the Beeper queue
  656. TERN_(USE_BEEPER, buzzer.tick());
  657. // Handle UI input / draw events
  658. TERN(DWIN_CREALITY_LCD, DWIN_Update(), ui.update());
  659. // Run i2c Position Encoders
  660. #if ENABLED(I2C_POSITION_ENCODERS)
  661. {
  662. static millis_t i2cpem_next_update_ms;
  663. if (planner.has_blocks_queued()) {
  664. const millis_t ms = millis();
  665. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  666. I2CPEM.update();
  667. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  668. }
  669. }
  670. }
  671. #endif
  672. // Auto-report Temperatures / SD Status
  673. #if HAS_AUTO_REPORTING
  674. if (!gcode.autoreport_paused) {
  675. TERN_(AUTO_REPORT_TEMPERATURES, thermalManager.auto_reporter.tick());
  676. TERN_(AUTO_REPORT_SD_STATUS, card.auto_reporter.tick());
  677. TERN_(AUTO_REPORT_POSITION, position_auto_reporter.tick());
  678. }
  679. #endif
  680. // Update the Průša MMU2
  681. TERN_(HAS_PRUSA_MMU2, mmu2.mmu_loop());
  682. // Handle Joystick jogging
  683. TERN_(POLL_JOG, joystick.inject_jog_moves());
  684. // Direct Stepping
  685. TERN_(DIRECT_STEPPING, page_manager.write_responses());
  686. // Update the LVGL interface
  687. TERN_(HAS_TFT_LVGL_UI, LV_TASK_HANDLER());
  688. IDLE_DONE:
  689. TERN_(MARLIN_DEV_MODE, idle_depth--);
  690. return;
  691. }
  692. /**
  693. * Kill all activity and lock the machine.
  694. * After this the machine will need to be reset.
  695. */
  696. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  697. thermalManager.disable_all_heaters();
  698. TERN_(HAS_CUTTER, cutter.kill()); // Full cutter shutdown including ISR control
  699. // Echo the LCD message to serial for extra context
  700. if (lcd_error) { SERIAL_ECHO_START(); SERIAL_ECHOLNPGM_P(lcd_error); }
  701. #if HAS_DISPLAY
  702. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component ?: NUL_STR);
  703. #else
  704. UNUSED(lcd_error); UNUSED(lcd_component);
  705. #endif
  706. TERN_(HAS_TFT_LVGL_UI, lv_draw_error_message(lcd_error));
  707. // "Error:Printer halted. kill() called!"
  708. SERIAL_ERROR_MSG(STR_ERR_KILLED);
  709. #ifdef ACTION_ON_KILL
  710. host_action_kill();
  711. #endif
  712. minkill(steppers_off);
  713. }
  714. void minkill(const bool steppers_off/*=false*/) {
  715. // Wait a short time (allows messages to get out before shutting down.
  716. for (int i = 1000; i--;) DELAY_US(600);
  717. cli(); // Stop interrupts
  718. // Wait to ensure all interrupts stopped
  719. for (int i = 1000; i--;) DELAY_US(250);
  720. // Reiterate heaters off
  721. thermalManager.disable_all_heaters();
  722. TERN_(HAS_CUTTER, cutter.kill()); // Reiterate cutter shutdown
  723. // Power off all steppers (for M112) or just the E steppers
  724. steppers_off ? disable_all_steppers() : disable_e_steppers();
  725. TERN_(PSU_CONTROL, PSU_OFF());
  726. TERN_(HAS_SUICIDE, suicide());
  727. #if EITHER(HAS_KILL, SOFT_RESET_ON_KILL)
  728. // Wait for both KILL and ENC to be released
  729. while (TERN0(HAS_KILL, !kill_state()) || TERN0(SOFT_RESET_ON_KILL, !ui.button_pressed()))
  730. watchdog_refresh();
  731. // Wait for either KILL or ENC press
  732. while (TERN1(HAS_KILL, kill_state()) && TERN1(SOFT_RESET_ON_KILL, ui.button_pressed()))
  733. watchdog_refresh();
  734. // Reboot the board
  735. HAL_reboot();
  736. #else
  737. for (;;) watchdog_refresh(); // Wait for RESET button or power-cycle
  738. #endif
  739. }
  740. /**
  741. * Turn off heaters and stop the print in progress
  742. * After a stop the machine may be resumed with M999
  743. */
  744. void stop() {
  745. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  746. print_job_timer.stop();
  747. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  748. thermalManager.set_fans_paused(false); // Un-pause fans for safety
  749. #endif
  750. if (!IsStopped()) {
  751. SERIAL_ERROR_MSG(STR_ERR_STOPPED);
  752. LCD_MESSAGEPGM(MSG_STOPPED);
  753. safe_delay(350); // allow enough time for messages to get out before stopping
  754. marlin_state = MF_STOPPED;
  755. }
  756. }
  757. inline void tmc_standby_setup() {
  758. #if PIN_EXISTS(X_STDBY)
  759. SET_INPUT_PULLDOWN(X_STDBY_PIN);
  760. #endif
  761. #if PIN_EXISTS(X2_STDBY)
  762. SET_INPUT_PULLDOWN(X2_STDBY_PIN);
  763. #endif
  764. #if PIN_EXISTS(Y_STDBY)
  765. SET_INPUT_PULLDOWN(Y_STDBY_PIN);
  766. #endif
  767. #if PIN_EXISTS(Y2_STDBY)
  768. SET_INPUT_PULLDOWN(Y2_STDBY_PIN);
  769. #endif
  770. #if PIN_EXISTS(Z_STDBY)
  771. SET_INPUT_PULLDOWN(Z_STDBY_PIN);
  772. #endif
  773. #if PIN_EXISTS(Z2_STDBY)
  774. SET_INPUT_PULLDOWN(Z2_STDBY_PIN);
  775. #endif
  776. #if PIN_EXISTS(Z3_STDBY)
  777. SET_INPUT_PULLDOWN(Z3_STDBY_PIN);
  778. #endif
  779. #if PIN_EXISTS(Z4_STDBY)
  780. SET_INPUT_PULLDOWN(Z4_STDBY_PIN);
  781. #endif
  782. #if PIN_EXISTS(I_STDBY)
  783. SET_INPUT_PULLDOWN(I_STDBY_PIN);
  784. #endif
  785. #if PIN_EXISTS(J_STDBY)
  786. SET_INPUT_PULLDOWN(J_STDBY_PIN);
  787. #endif
  788. #if PIN_EXISTS(K_STDBY)
  789. SET_INPUT_PULLDOWN(K_STDBY_PIN);
  790. #endif
  791. #if PIN_EXISTS(E0_STDBY)
  792. SET_INPUT_PULLDOWN(E0_STDBY_PIN);
  793. #endif
  794. #if PIN_EXISTS(E1_STDBY)
  795. SET_INPUT_PULLDOWN(E1_STDBY_PIN);
  796. #endif
  797. #if PIN_EXISTS(E2_STDBY)
  798. SET_INPUT_PULLDOWN(E2_STDBY_PIN);
  799. #endif
  800. #if PIN_EXISTS(E3_STDBY)
  801. SET_INPUT_PULLDOWN(E3_STDBY_PIN);
  802. #endif
  803. #if PIN_EXISTS(E4_STDBY)
  804. SET_INPUT_PULLDOWN(E4_STDBY_PIN);
  805. #endif
  806. #if PIN_EXISTS(E5_STDBY)
  807. SET_INPUT_PULLDOWN(E5_STDBY_PIN);
  808. #endif
  809. #if PIN_EXISTS(E6_STDBY)
  810. SET_INPUT_PULLDOWN(E6_STDBY_PIN);
  811. #endif
  812. #if PIN_EXISTS(E7_STDBY)
  813. SET_INPUT_PULLDOWN(E7_STDBY_PIN);
  814. #endif
  815. }
  816. /**
  817. * Marlin Firmware entry-point. Abandon Hope All Ye Who Enter Here.
  818. * Setup before the program loop:
  819. *
  820. * - Call any special pre-init set for the board
  821. * - Put TMC drivers into Low Power Standby mode
  822. * - Init the serial ports (so setup can be debugged)
  823. * - Set up the kill and suicide pins
  824. * - Prepare (disable) board JTAG and Debug ports
  825. * - Init serial for a connected MKS TFT with WiFi
  826. * - Install Marlin custom Exception Handlers, if set.
  827. * - Init Marlin's HAL interfaces (for SPI, i2c, etc.)
  828. * - Init some optional hardware and features:
  829. * • MAX Thermocouple pins
  830. * • Duet Smart Effector
  831. * • Filament Runout Sensor
  832. * • TMC220x Stepper Drivers (Serial)
  833. * • PSU control
  834. * • Power-loss Recovery
  835. * • L64XX Stepper Drivers (SPI)
  836. * • Stepper Driver Reset: DISABLE
  837. * • TMC Stepper Drivers (SPI)
  838. * • Run BOARD_INIT if defined
  839. * • ESP WiFi
  840. * - Get the Reset Reason and report it
  841. * - Print startup messages and diagnostics
  842. * - Calibrate the HAL DELAY for precise timing
  843. * - Init the buzzer, possibly a custom timer
  844. * - Init more optional hardware:
  845. * • Color LED illumination
  846. * • Neopixel illumination
  847. * • Controller Fan
  848. * • Creality DWIN LCD (show boot image)
  849. * • Tare the Probe if possible
  850. * - Mount the (most likely external) SD Card
  851. * - Load settings from EEPROM (or use defaults)
  852. * - Init the Ethernet Port
  853. * - Init Touch Buttons (for emulated DOGLCD)
  854. * - Adjust the (certainly wrong) current position by the home offset
  855. * - Init the Planner::position (steps) based on current (native) position
  856. * - Initialize more managers and peripherals:
  857. * • Temperatures
  858. * • Print Job Timer
  859. * • Endstops and Endstop Interrupts
  860. * • Stepper ISR - Kind of Important!
  861. * • Servos
  862. * • Servo-based Probe
  863. * • Photograph Pin
  864. * • Laser/Spindle tool Power / PWM
  865. * • Coolant Control
  866. * • Bed Probe
  867. * • Stepper Driver Reset: ENABLE
  868. * • Digipot I2C - Stepper driver current control
  869. * • Stepper DAC - Stepper driver current control
  870. * • Solenoid (probe, or for other use)
  871. * • Home Pin
  872. * • Custom User Buttons
  873. * • Red/Blue Status LEDs
  874. * • Case Light
  875. * • Prusa MMU filament changer
  876. * • Fan Multiplexer
  877. * • Mixing Extruder
  878. * • BLTouch Probe
  879. * • I2C Position Encoders
  880. * • Custom I2C Bus handlers
  881. * • Enhanced tools or extruders:
  882. * • Switching Extruder
  883. * • Switching Nozzle
  884. * • Parking Extruder
  885. * • Magnetic Parking Extruder
  886. * • Switching Toolhead
  887. * • Electromagnetic Switching Toolhead
  888. * • Watchdog Timer - Also Kind of Important!
  889. * • Closed Loop Controller
  890. * - Run Startup Commands, if defined
  891. * - Tell host to close Host Prompts
  892. * - Test Trinamic driver connections
  893. * - Init Prusa MMU2 filament changer
  894. * - Init and test BL24Cxx EEPROM
  895. * - Init Creality DWIN encoder, show faux progress bar
  896. * - Reset Status Message / Show Service Messages
  897. * - Init MAX7219 LED Matrix
  898. * - Init Direct Stepping (Klipper-style motion control)
  899. * - Init TFT LVGL UI (with 3D Graphics)
  900. * - Apply Password Lock - Hold for Authentication
  901. * - Open Touch Screen Calibration screen, if not calibrated
  902. * - Set Marlin to RUNNING State
  903. */
  904. void setup() {
  905. #ifdef BOARD_PREINIT
  906. BOARD_PREINIT(); // Low-level init (before serial init)
  907. #endif
  908. tmc_standby_setup(); // TMC Low Power Standby pins must be set early or they're not usable
  909. #if ENABLED(MARLIN_DEV_MODE)
  910. auto log_current_ms = [&](PGM_P const msg) {
  911. SERIAL_ECHO_START();
  912. SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHOPGM("] ");
  913. SERIAL_ECHOLNPGM_P(msg);
  914. };
  915. #define SETUP_LOG(M) log_current_ms(PSTR(M))
  916. #else
  917. #define SETUP_LOG(...) NOOP
  918. #endif
  919. #define SETUP_RUN(C) do{ SETUP_LOG(STRINGIFY(C)); C; }while(0)
  920. MYSERIAL1.begin(BAUDRATE);
  921. millis_t serial_connect_timeout = millis() + 1000UL;
  922. while (!MYSERIAL1.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  923. #if HAS_MULTI_SERIAL && !HAS_ETHERNET
  924. #ifndef BAUDRATE_2
  925. #define BAUDRATE_2 BAUDRATE
  926. #endif
  927. MYSERIAL2.begin(BAUDRATE_2);
  928. serial_connect_timeout = millis() + 1000UL;
  929. while (!MYSERIAL2.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  930. #ifdef SERIAL_PORT_3
  931. #ifndef BAUDRATE_3
  932. #define BAUDRATE_3 BAUDRATE
  933. #endif
  934. MYSERIAL3.begin(BAUDRATE_3);
  935. serial_connect_timeout = millis() + 1000UL;
  936. while (!MYSERIAL3.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  937. #endif
  938. #endif
  939. SERIAL_ECHOLNPGM("start");
  940. // Set up these pins early to prevent suicide
  941. #if HAS_KILL
  942. SETUP_LOG("KILL_PIN");
  943. #if KILL_PIN_STATE
  944. SET_INPUT_PULLDOWN(KILL_PIN);
  945. #else
  946. SET_INPUT_PULLUP(KILL_PIN);
  947. #endif
  948. #endif
  949. #if HAS_FREEZE_PIN
  950. SETUP_LOG("FREEZE_PIN");
  951. SET_INPUT_PULLUP(FREEZE_PIN);
  952. #endif
  953. #if HAS_SUICIDE
  954. SETUP_LOG("SUICIDE_PIN");
  955. OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
  956. #endif
  957. #ifdef JTAGSWD_RESET
  958. SETUP_LOG("JTAGSWD_RESET");
  959. JTAGSWD_RESET();
  960. #endif
  961. #if EITHER(DISABLE_DEBUG, DISABLE_JTAG)
  962. delay(10);
  963. // Disable any hardware debug to free up pins for IO
  964. #if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
  965. SETUP_LOG("JTAGSWD_DISABLE");
  966. JTAGSWD_DISABLE();
  967. #elif defined(JTAG_DISABLE)
  968. SETUP_LOG("JTAG_DISABLE");
  969. JTAG_DISABLE();
  970. #else
  971. #error "DISABLE_(DEBUG|JTAG) is not supported for the selected MCU/Board."
  972. #endif
  973. #endif
  974. #if BOTH(HAS_TFT_LVGL_UI, MKS_WIFI_MODULE)
  975. mks_esp_wifi_init();
  976. WIFISERIAL.begin(WIFI_BAUDRATE);
  977. serial_connect_timeout = millis() + 1000UL;
  978. while (/*!WIFISERIAL && */PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  979. #endif
  980. TERN_(DYNAMIC_VECTORTABLE, hook_cpu_exceptions()); // If supported, install Marlin exception handlers at runtime
  981. SETUP_RUN(HAL_init());
  982. // Init and disable SPI thermocouples; this is still needed
  983. #if TEMP_SENSOR_0_IS_MAX_TC || (TEMP_SENSOR_REDUNDANT_IS_MAX_TC && TEMP_SENSOR_REDUNDANT_SOURCE == 0)
  984. OUT_WRITE(TEMP_0_CS_PIN, HIGH); // Disable
  985. #endif
  986. #if TEMP_SENSOR_1_IS_MAX_TC || (TEMP_SENSOR_REDUNDANT_IS_MAX_TC && TEMP_SENSOR_REDUNDANT_SOURCE == 1)
  987. OUT_WRITE(TEMP_1_CS_PIN, HIGH);
  988. #endif
  989. #if ENABLED(DUET_SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  990. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  991. #endif
  992. #if HAS_FILAMENT_SENSOR
  993. SETUP_RUN(runout.setup());
  994. #endif
  995. #if HAS_TMC220x
  996. SETUP_RUN(tmc_serial_begin());
  997. #endif
  998. #if ENABLED(PSU_CONTROL)
  999. SETUP_LOG("PSU_CONTROL");
  1000. powersupply_on = ENABLED(PSU_DEFAULT_OFF);
  1001. if (ENABLED(PSU_DEFAULT_OFF)) PSU_OFF(); else PSU_ON();
  1002. #endif
  1003. #if ENABLED(POWER_LOSS_RECOVERY)
  1004. SETUP_RUN(recovery.setup());
  1005. #endif
  1006. #if HAS_L64XX
  1007. SETUP_RUN(L64xxManager.init()); // Set up SPI, init drivers
  1008. #endif
  1009. #if HAS_STEPPER_RESET
  1010. SETUP_RUN(disableStepperDrivers());
  1011. #endif
  1012. #if HAS_TMC_SPI
  1013. #if DISABLED(TMC_USE_SW_SPI)
  1014. SETUP_RUN(SPI.begin());
  1015. #endif
  1016. SETUP_RUN(tmc_init_cs_pins());
  1017. #endif
  1018. #ifdef BOARD_INIT
  1019. SETUP_LOG("BOARD_INIT");
  1020. BOARD_INIT();
  1021. #endif
  1022. SETUP_RUN(esp_wifi_init());
  1023. // Check startup - does nothing if bootloader sets MCUSR to 0
  1024. const byte mcu = HAL_get_reset_source();
  1025. if (mcu & RST_POWER_ON) SERIAL_ECHOLNPGM(STR_POWERUP);
  1026. if (mcu & RST_EXTERNAL) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  1027. if (mcu & RST_BROWN_OUT) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  1028. if (mcu & RST_WATCHDOG) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  1029. if (mcu & RST_SOFTWARE) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  1030. HAL_clear_reset_source();
  1031. SERIAL_ECHOPGM_P(GET_TEXT(MSG_MARLIN));
  1032. SERIAL_CHAR(' ');
  1033. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  1034. SERIAL_EOL();
  1035. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  1036. SERIAL_ECHO_MSG(
  1037. " Last Updated: " STRING_DISTRIBUTION_DATE
  1038. " | Author: " STRING_CONFIG_H_AUTHOR
  1039. );
  1040. #endif
  1041. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  1042. SERIAL_ECHO_MSG(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  1043. // Some HAL need precise delay adjustment
  1044. calibrate_delay_loop();
  1045. // Init buzzer pin(s)
  1046. #if USE_BEEPER
  1047. SETUP_RUN(buzzer.init());
  1048. #endif
  1049. // Set up LEDs early
  1050. #if HAS_COLOR_LEDS
  1051. SETUP_RUN(leds.setup());
  1052. #endif
  1053. #if ENABLED(NEOPIXEL2_SEPARATE)
  1054. SETUP_RUN(leds2.setup());
  1055. #endif
  1056. #if ENABLED(USE_CONTROLLER_FAN) // Set up fan controller to initialize also the default configurations.
  1057. SETUP_RUN(controllerFan.setup());
  1058. #endif
  1059. // UI must be initialized before EEPROM
  1060. // (because EEPROM code calls the UI).
  1061. #if ENABLED(DWIN_CREALITY_LCD)
  1062. delay(800); // Required delay (since boot?)
  1063. SERIAL_ECHOPGM("\nDWIN handshake ");
  1064. if (DWIN_Handshake()) SERIAL_ECHOLNPGM("ok."); else SERIAL_ECHOLNPGM("error.");
  1065. DWIN_Frame_SetDir(1); // Orientation 90°
  1066. DWIN_UpdateLCD(); // Show bootscreen (first image)
  1067. #else
  1068. SETUP_RUN(ui.init());
  1069. #if BOTH(HAS_WIRED_LCD, SHOW_BOOTSCREEN)
  1070. SETUP_RUN(ui.show_bootscreen());
  1071. const millis_t bootscreen_ms = millis();
  1072. #endif
  1073. SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)
  1074. #endif
  1075. #if PIN_EXISTS(SAFE_POWER)
  1076. #if HAS_DRIVER_SAFE_POWER_PROTECT
  1077. SETUP_RUN(stepper_driver_backward_check());
  1078. #else
  1079. SETUP_LOG("SAFE_POWER");
  1080. OUT_WRITE(SAFE_POWER_PIN, HIGH);
  1081. #endif
  1082. #endif
  1083. #if ENABLED(PROBE_TARE)
  1084. SETUP_RUN(probe.tare_init());
  1085. #endif
  1086. #if BOTH(SDSUPPORT, SDCARD_EEPROM_EMULATION)
  1087. SETUP_RUN(card.mount()); // Mount media with settings before first_load
  1088. #endif
  1089. SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
  1090. // This also updates variables in the planner, elsewhere
  1091. #if HAS_ETHERNET
  1092. SETUP_RUN(ethernet.init());
  1093. #endif
  1094. #if HAS_TOUCH_BUTTONS
  1095. SETUP_RUN(touch.init());
  1096. #endif
  1097. TERN_(HAS_M206_COMMAND, current_position += home_offset); // Init current position based on home_offset
  1098. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  1099. SETUP_RUN(thermalManager.init()); // Initialize temperature loop
  1100. SETUP_RUN(print_job_timer.init()); // Initial setup of print job timer
  1101. SETUP_RUN(endstops.init()); // Init endstops and pullups
  1102. SETUP_RUN(stepper.init()); // Init stepper. This enables interrupts!
  1103. #if HAS_SERVOS
  1104. SETUP_RUN(servo_init());
  1105. #endif
  1106. #if HAS_Z_SERVO_PROBE
  1107. SETUP_RUN(probe.servo_probe_init());
  1108. #endif
  1109. #if HAS_PHOTOGRAPH
  1110. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  1111. #endif
  1112. #if HAS_CUTTER
  1113. SETUP_RUN(cutter.init());
  1114. #endif
  1115. #if ENABLED(COOLANT_MIST)
  1116. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  1117. #endif
  1118. #if ENABLED(COOLANT_FLOOD)
  1119. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  1120. #endif
  1121. #if HAS_BED_PROBE
  1122. SETUP_RUN(endstops.enable_z_probe(false));
  1123. #endif
  1124. #if HAS_STEPPER_RESET
  1125. SETUP_RUN(enableStepperDrivers());
  1126. #endif
  1127. #if HAS_MOTOR_CURRENT_I2C
  1128. SETUP_RUN(digipot_i2c.init());
  1129. #endif
  1130. #if ENABLED(HAS_MOTOR_CURRENT_DAC)
  1131. SETUP_RUN(stepper_dac.init());
  1132. #endif
  1133. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  1134. OUT_WRITE(SOL1_PIN, LOW); // OFF
  1135. #endif
  1136. #if HAS_HOME
  1137. SET_INPUT_PULLUP(HOME_PIN);
  1138. #endif
  1139. #if ENABLED(CUSTOM_USER_BUTTONS)
  1140. #define INIT_CUSTOM_USER_BUTTON_PIN(N) do{ SET_INPUT(BUTTON##N##_PIN); WRITE(BUTTON##N##_PIN, !BUTTON##N##_HIT_STATE); }while(0)
  1141. #if HAS_CUSTOM_USER_BUTTON(1)
  1142. INIT_CUSTOM_USER_BUTTON_PIN(1);
  1143. #endif
  1144. #if HAS_CUSTOM_USER_BUTTON(2)
  1145. INIT_CUSTOM_USER_BUTTON_PIN(2);
  1146. #endif
  1147. #if HAS_CUSTOM_USER_BUTTON(3)
  1148. INIT_CUSTOM_USER_BUTTON_PIN(3);
  1149. #endif
  1150. #if HAS_CUSTOM_USER_BUTTON(4)
  1151. INIT_CUSTOM_USER_BUTTON_PIN(4);
  1152. #endif
  1153. #if HAS_CUSTOM_USER_BUTTON(5)
  1154. INIT_CUSTOM_USER_BUTTON_PIN(5);
  1155. #endif
  1156. #if HAS_CUSTOM_USER_BUTTON(6)
  1157. INIT_CUSTOM_USER_BUTTON_PIN(6);
  1158. #endif
  1159. #if HAS_CUSTOM_USER_BUTTON(7)
  1160. INIT_CUSTOM_USER_BUTTON_PIN(7);
  1161. #endif
  1162. #if HAS_CUSTOM_USER_BUTTON(8)
  1163. INIT_CUSTOM_USER_BUTTON_PIN(8);
  1164. #endif
  1165. #if HAS_CUSTOM_USER_BUTTON(9)
  1166. INIT_CUSTOM_USER_BUTTON_PIN(9);
  1167. #endif
  1168. #if HAS_CUSTOM_USER_BUTTON(10)
  1169. INIT_CUSTOM_USER_BUTTON_PIN(10);
  1170. #endif
  1171. #if HAS_CUSTOM_USER_BUTTON(11)
  1172. INIT_CUSTOM_USER_BUTTON_PIN(11);
  1173. #endif
  1174. #if HAS_CUSTOM_USER_BUTTON(12)
  1175. INIT_CUSTOM_USER_BUTTON_PIN(12);
  1176. #endif
  1177. #if HAS_CUSTOM_USER_BUTTON(13)
  1178. INIT_CUSTOM_USER_BUTTON_PIN(13);
  1179. #endif
  1180. #if HAS_CUSTOM_USER_BUTTON(14)
  1181. INIT_CUSTOM_USER_BUTTON_PIN(14);
  1182. #endif
  1183. #if HAS_CUSTOM_USER_BUTTON(15)
  1184. INIT_CUSTOM_USER_BUTTON_PIN(15);
  1185. #endif
  1186. #if HAS_CUSTOM_USER_BUTTON(16)
  1187. INIT_CUSTOM_USER_BUTTON_PIN(16);
  1188. #endif
  1189. #if HAS_CUSTOM_USER_BUTTON(17)
  1190. INIT_CUSTOM_USER_BUTTON_PIN(17);
  1191. #endif
  1192. #if HAS_CUSTOM_USER_BUTTON(18)
  1193. INIT_CUSTOM_USER_BUTTON_PIN(18);
  1194. #endif
  1195. #if HAS_CUSTOM_USER_BUTTON(19)
  1196. INIT_CUSTOM_USER_BUTTON_PIN(19);
  1197. #endif
  1198. #if HAS_CUSTOM_USER_BUTTON(20)
  1199. INIT_CUSTOM_USER_BUTTON_PIN(20);
  1200. #endif
  1201. #if HAS_CUSTOM_USER_BUTTON(21)
  1202. INIT_CUSTOM_USER_BUTTON_PIN(21);
  1203. #endif
  1204. #if HAS_CUSTOM_USER_BUTTON(22)
  1205. INIT_CUSTOM_USER_BUTTON_PIN(22);
  1206. #endif
  1207. #if HAS_CUSTOM_USER_BUTTON(23)
  1208. INIT_CUSTOM_USER_BUTTON_PIN(23);
  1209. #endif
  1210. #if HAS_CUSTOM_USER_BUTTON(24)
  1211. INIT_CUSTOM_USER_BUTTON_PIN(24);
  1212. #endif
  1213. #if HAS_CUSTOM_USER_BUTTON(25)
  1214. INIT_CUSTOM_USER_BUTTON_PIN(25);
  1215. #endif
  1216. #endif
  1217. #if PIN_EXISTS(STAT_LED_RED)
  1218. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  1219. #endif
  1220. #if PIN_EXISTS(STAT_LED_BLUE)
  1221. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  1222. #endif
  1223. #if ENABLED(CASE_LIGHT_ENABLE)
  1224. SETUP_RUN(caselight.init());
  1225. #endif
  1226. #if HAS_PRUSA_MMU1
  1227. SETUP_RUN(mmu_init());
  1228. #endif
  1229. #if HAS_FANMUX
  1230. SETUP_RUN(fanmux_init());
  1231. #endif
  1232. #if ENABLED(MIXING_EXTRUDER)
  1233. SETUP_RUN(mixer.init());
  1234. #endif
  1235. #if ENABLED(BLTOUCH)
  1236. SETUP_RUN(bltouch.init(/*set_voltage=*/true));
  1237. #endif
  1238. #if ENABLED(I2C_POSITION_ENCODERS)
  1239. SETUP_RUN(I2CPEM.init());
  1240. #endif
  1241. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  1242. SETUP_LOG("i2c...");
  1243. i2c.onReceive(i2c_on_receive);
  1244. i2c.onRequest(i2c_on_request);
  1245. #endif
  1246. #if DO_SWITCH_EXTRUDER
  1247. SETUP_RUN(move_extruder_servo(0)); // Initialize extruder servo
  1248. #endif
  1249. #if ENABLED(SWITCHING_NOZZLE)
  1250. SETUP_LOG("SWITCHING_NOZZLE");
  1251. // Initialize nozzle servo(s)
  1252. #if SWITCHING_NOZZLE_TWO_SERVOS
  1253. lower_nozzle(0);
  1254. raise_nozzle(1);
  1255. #else
  1256. move_nozzle_servo(0);
  1257. #endif
  1258. #endif
  1259. #if ENABLED(PARKING_EXTRUDER)
  1260. SETUP_RUN(pe_solenoid_init());
  1261. #elif ENABLED(MAGNETIC_PARKING_EXTRUDER)
  1262. SETUP_RUN(mpe_settings_init());
  1263. #elif ENABLED(SWITCHING_TOOLHEAD)
  1264. SETUP_RUN(swt_init());
  1265. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  1266. SETUP_RUN(est_init());
  1267. #endif
  1268. #if ENABLED(USE_WATCHDOG)
  1269. SETUP_RUN(watchdog_init()); // Reinit watchdog after HAL_get_reset_source call
  1270. #endif
  1271. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  1272. SETUP_RUN(closedloop.init());
  1273. #endif
  1274. #ifdef STARTUP_COMMANDS
  1275. SETUP_LOG("STARTUP_COMMANDS");
  1276. queue.inject_P(PSTR(STARTUP_COMMANDS));
  1277. #endif
  1278. #if ENABLED(HOST_PROMPT_SUPPORT)
  1279. SETUP_RUN(host_action_prompt_end());
  1280. #endif
  1281. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  1282. SETUP_RUN(test_tmc_connection());
  1283. #endif
  1284. #if HAS_DRIVER_SAFE_POWER_PROTECT
  1285. SETUP_RUN(stepper_driver_backward_report());
  1286. #endif
  1287. #if HAS_PRUSA_MMU2
  1288. SETUP_RUN(mmu2.init());
  1289. #endif
  1290. #if ENABLED(IIC_BL24CXX_EEPROM)
  1291. BL24CXX::init();
  1292. const uint8_t err = BL24CXX::check();
  1293. SERIAL_ECHO_TERNARY(err, "BL24CXX Check ", "failed", "succeeded", "!\n");
  1294. #endif
  1295. #if ENABLED(DWIN_CREALITY_LCD)
  1296. Encoder_Configuration();
  1297. HMI_Init();
  1298. DWIN_JPG_CacheTo1(Language_English);
  1299. HMI_StartFrame(true);
  1300. DWIN_StatusChanged(GET_TEXT(WELCOME_MSG));
  1301. #endif
  1302. #if HAS_SERVICE_INTERVALS && DISABLED(DWIN_CREALITY_LCD)
  1303. ui.reset_status(true); // Show service messages or keep current status
  1304. #endif
  1305. #if ENABLED(MAX7219_DEBUG)
  1306. SETUP_RUN(max7219.init());
  1307. #endif
  1308. #if ENABLED(DIRECT_STEPPING)
  1309. SETUP_RUN(page_manager.init());
  1310. #endif
  1311. #if HAS_TFT_LVGL_UI
  1312. #if ENABLED(SDSUPPORT)
  1313. if (!card.isMounted()) SETUP_RUN(card.mount()); // Mount SD to load graphics and fonts
  1314. #endif
  1315. SETUP_RUN(tft_lvgl_init());
  1316. #endif
  1317. #if BOTH(HAS_WIRED_LCD, SHOW_BOOTSCREEN)
  1318. const millis_t elapsed = millis() - bootscreen_ms;
  1319. #if ENABLED(MARLIN_DEV_MODE)
  1320. SERIAL_ECHOLNPAIR("elapsed=", elapsed);
  1321. #endif
  1322. SETUP_RUN(ui.bootscreen_completion(elapsed));
  1323. #endif
  1324. #if ENABLED(PASSWORD_ON_STARTUP)
  1325. SETUP_RUN(password.lock_machine()); // Will not proceed until correct password provided
  1326. #endif
  1327. #if BOTH(HAS_LCD_MENU, TOUCH_SCREEN_CALIBRATION) && EITHER(TFT_CLASSIC_UI, TFT_COLOR_UI)
  1328. ui.check_touch_calibration();
  1329. #endif
  1330. marlin_state = MF_RUNNING;
  1331. SETUP_LOG("setup() completed.");
  1332. }
  1333. /**
  1334. * The main Marlin program loop
  1335. *
  1336. * - Call idle() to handle all tasks between G-code commands
  1337. * Note that no G-codes from the queue can be executed during idle()
  1338. * but many G-codes can be called directly anytime like macros.
  1339. * - Check whether SD card auto-start is needed now.
  1340. * - Check whether SD print finishing is needed now.
  1341. * - Run one G-code command from the immediate or main command queue
  1342. * and open up one space. Commands in the main queue may come from sd
  1343. * card, host, or by direct injection. The queue will continue to fill
  1344. * as long as idle() or manage_inactivity() are being called.
  1345. */
  1346. void loop() {
  1347. do {
  1348. idle();
  1349. #if ENABLED(SDSUPPORT)
  1350. if (card.flag.abort_sd_printing) abortSDPrinting();
  1351. if (marlin_state == MF_SD_COMPLETE) finishSDPrinting();
  1352. #endif
  1353. queue.advance();
  1354. endstops.event_handler();
  1355. TERN_(HAS_TFT_LVGL_UI, printer_state_polling());
  1356. } while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
  1357. }