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

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