My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MarlinCore.cpp 46KB

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