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

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