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

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