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

MarlinCore.cpp 48KB

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