My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MarlinCore.cpp 33KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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. #ifdef ARDUINO
  33. #include <pins_arduino.h>
  34. #endif
  35. #include <math.h>
  36. #include "core/utility.h"
  37. #include "module/motion.h"
  38. #include "module/planner.h"
  39. #include "module/endstops.h"
  40. #include "module/temperature.h"
  41. #include "module/configuration_store.h"
  42. #include "module/printcounter.h" // PrintCounter or Stopwatch
  43. #include "module/stepper.h"
  44. #include "module/stepper/indirection.h"
  45. #include "gcode/gcode.h"
  46. #include "gcode/parser.h"
  47. #include "gcode/queue.h"
  48. #include "sd/cardreader.h"
  49. #include "lcd/ultralcd.h"
  50. #if HAS_TOUCH_XPT2046
  51. #include "lcd/touch/xpt2046.h"
  52. #endif
  53. #if HAS_TFT_LVGL_UI
  54. #include "lcd/extui/lib/mks_ui/tft_lvgl_configuration.h"
  55. #include "lcd/extui/lib/mks_ui/draw_ui.h"
  56. #include "lcd/extui/lib/mks_ui/mks_hardware_test.h"
  57. #include <lvgl.h>
  58. #endif
  59. #if ENABLED(DWIN_CREALITY_LCD)
  60. #include "lcd/dwin/dwin.h"
  61. #include "lcd/dwin/dwin_lcd.h"
  62. #include "lcd/dwin/rotary_encoder.h"
  63. #endif
  64. #if ENABLED(IIC_BL24CXX_EEPROM)
  65. #include "libs/BL24CXX.h"
  66. #endif
  67. #if ENABLED(DIRECT_STEPPING)
  68. #include "feature/direct_stepping.h"
  69. #endif
  70. #if ENABLED(HOST_ACTION_COMMANDS)
  71. #include "feature/host_actions.h"
  72. #endif
  73. #if USE_BEEPER
  74. #include "libs/buzzer.h"
  75. #endif
  76. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  77. #include "feature/closedloop.h"
  78. #endif
  79. #if HAS_I2C_DIGIPOT
  80. #include "feature/digipot/digipot.h"
  81. #endif
  82. #if ENABLED(MIXING_EXTRUDER)
  83. #include "feature/mixing.h"
  84. #endif
  85. #if ENABLED(MAX7219_DEBUG)
  86. #include "feature/max7219.h"
  87. #endif
  88. #if HAS_COLOR_LEDS
  89. #include "feature/leds/leds.h"
  90. #endif
  91. #if ENABLED(BLTOUCH)
  92. #include "feature/bltouch.h"
  93. #endif
  94. #if ENABLED(POLL_JOG)
  95. #include "feature/joystick.h"
  96. #endif
  97. #if HAS_SERVOS
  98. #include "module/servo.h"
  99. #endif
  100. #if ENABLED(DAC_STEPPER_CURRENT)
  101. #include "feature/dac/stepper_dac.h"
  102. #endif
  103. #if ENABLED(EXPERIMENTAL_I2CBUS)
  104. #include "feature/twibus.h"
  105. TWIBus i2c;
  106. #endif
  107. #if ENABLED(I2C_POSITION_ENCODERS)
  108. #include "feature/encoder_i2c.h"
  109. #endif
  110. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  111. #include "feature/tmc_util.h"
  112. #endif
  113. #if HAS_CUTTER
  114. #include "feature/spindle_laser.h"
  115. #endif
  116. #if ENABLED(SDSUPPORT)
  117. CardReader card;
  118. #endif
  119. #if ENABLED(G38_PROBE_TARGET)
  120. uint8_t G38_move; // = 0
  121. bool G38_did_trigger; // = false
  122. #endif
  123. #if ENABLED(DELTA)
  124. #include "module/delta.h"
  125. #elif IS_SCARA
  126. #include "module/scara.h"
  127. #endif
  128. #if HAS_LEVELING
  129. #include "feature/bedlevel/bedlevel.h"
  130. #endif
  131. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  132. #include "feature/pause.h"
  133. #endif
  134. #if ENABLED(POWER_LOSS_RECOVERY)
  135. #include "feature/powerloss.h"
  136. #endif
  137. #if ENABLED(CANCEL_OBJECTS)
  138. #include "feature/cancel_object.h"
  139. #endif
  140. #if HAS_FILAMENT_SENSOR
  141. #include "feature/runout.h"
  142. #endif
  143. #if HAS_Z_SERVO_PROBE
  144. #include "module/probe.h"
  145. #endif
  146. #if ENABLED(HOTEND_IDLE_TIMEOUT)
  147. #include "feature/hotend_idle.h"
  148. #endif
  149. #if ENABLED(TEMP_STAT_LEDS)
  150. #include "feature/leds/tempstat.h"
  151. #endif
  152. #if ENABLED(CASE_LIGHT_ENABLE)
  153. #include "feature/caselight.h"
  154. #endif
  155. #if HAS_FANMUX
  156. #include "feature/fanmux.h"
  157. #endif
  158. #if DO_SWITCH_EXTRUDER || ANY(SWITCHING_NOZZLE, PARKING_EXTRUDER, MAGNETIC_PARKING_EXTRUDER, ELECTROMAGNETIC_SWITCHING_TOOLHEAD, SWITCHING_TOOLHEAD)
  159. #include "module/tool_change.h"
  160. #endif
  161. #if ENABLED(USE_CONTROLLER_FAN)
  162. #include "feature/controllerfan.h"
  163. #endif
  164. #if ENABLED(PRUSA_MMU2)
  165. #include "feature/mmu2/mmu2.h"
  166. #endif
  167. #if HAS_L64XX
  168. #include "libs/L64XX/L64XX_Marlin.h"
  169. #endif
  170. PGMSTR(NUL_STR, "");
  171. PGMSTR(M112_KILL_STR, "M112 Shutdown");
  172. PGMSTR(G28_STR, "G28");
  173. PGMSTR(M21_STR, "M21");
  174. PGMSTR(M23_STR, "M23 %s");
  175. PGMSTR(M24_STR, "M24");
  176. PGMSTR(SP_P_STR, " P"); PGMSTR(SP_T_STR, " T");
  177. PGMSTR(X_STR, "X"); PGMSTR(Y_STR, "Y"); PGMSTR(Z_STR, "Z"); PGMSTR(E_STR, "E");
  178. PGMSTR(X_LBL, "X:"); PGMSTR(Y_LBL, "Y:"); PGMSTR(Z_LBL, "Z:"); PGMSTR(E_LBL, "E:");
  179. PGMSTR(SP_A_STR, " A"); PGMSTR(SP_B_STR, " B"); PGMSTR(SP_C_STR, " C");
  180. PGMSTR(SP_X_STR, " X"); PGMSTR(SP_Y_STR, " Y"); PGMSTR(SP_Z_STR, " Z"); PGMSTR(SP_E_STR, " E");
  181. PGMSTR(SP_X_LBL, " X:"); PGMSTR(SP_Y_LBL, " Y:"); PGMSTR(SP_Z_LBL, " Z:"); PGMSTR(SP_E_LBL, " E:");
  182. MarlinState marlin_state = MF_INITIALIZING;
  183. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  184. bool wait_for_heatup = true;
  185. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  186. #if HAS_RESUME_CONTINUE
  187. bool wait_for_user; // = false;
  188. void wait_for_user_response(millis_t ms/*=0*/, const bool no_sleep/*=false*/) {
  189. TERN(ADVANCED_PAUSE_FEATURE,,UNUSED(no_sleep));
  190. KEEPALIVE_STATE(PAUSED_FOR_USER);
  191. wait_for_user = true;
  192. if (ms) ms += millis(); // expire time
  193. while (wait_for_user && !(ms && ELAPSED(millis(), ms)))
  194. idle(TERN_(ADVANCED_PAUSE_FEATURE, no_sleep));
  195. wait_for_user = false;
  196. }
  197. #endif
  198. #if PIN_EXISTS(CHDK)
  199. extern millis_t chdk_timeout;
  200. #endif
  201. #if ENABLED(I2C_POSITION_ENCODERS)
  202. I2CPositionEncodersMgr I2CPEM;
  203. #endif
  204. /**
  205. * ***************************************************************************
  206. * ******************************** FUNCTIONS ********************************
  207. * ***************************************************************************
  208. */
  209. void setup_killpin() {
  210. #if HAS_KILL
  211. #if KILL_PIN_STATE
  212. SET_INPUT_PULLDOWN(KILL_PIN);
  213. #else
  214. SET_INPUT_PULLUP(KILL_PIN);
  215. #endif
  216. #endif
  217. }
  218. void setup_powerhold() {
  219. #if HAS_SUICIDE
  220. OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
  221. #endif
  222. #if ENABLED(PSU_CONTROL)
  223. powersupply_on = ENABLED(PSU_DEFAULT_OFF);
  224. if (ENABLED(PSU_DEFAULT_OFF)) PSU_OFF(); else PSU_ON();
  225. #endif
  226. }
  227. /**
  228. * Stepper Reset (RigidBoard, et.al.)
  229. */
  230. #if HAS_STEPPER_RESET
  231. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  232. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  233. #endif
  234. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  235. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  236. i2c.receive(bytes);
  237. }
  238. void i2c_on_request() { // just send dummy data for now
  239. i2c.reply("Hello World!\n");
  240. }
  241. #endif
  242. /**
  243. * Sensitive pin test for M42, M226
  244. */
  245. #include "pins/sensitive_pins.h"
  246. #pragma GCC diagnostic push
  247. #pragma GCC diagnostic ignored "-Wnarrowing"
  248. bool pin_is_protected(const pin_t pin) {
  249. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  250. LOOP_L_N(i, COUNT(sensitive_pins)) {
  251. pin_t sensitive_pin;
  252. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  253. if (pin == sensitive_pin) return true;
  254. }
  255. return false;
  256. }
  257. #pragma GCC diagnostic pop
  258. void protected_pin_err() {
  259. SERIAL_ERROR_MSG(STR_ERR_PROTECTED_PIN);
  260. }
  261. void quickstop_stepper() {
  262. planner.quick_stop();
  263. planner.synchronize();
  264. set_current_from_steppers_for_axis(ALL_AXES);
  265. sync_plan_position();
  266. }
  267. void enable_e_steppers() {
  268. #define _ENA_E(N) ENABLE_AXIS_E##N();
  269. REPEAT(E_STEPPERS, _ENA_E)
  270. }
  271. void enable_all_steppers() {
  272. TERN_(AUTO_POWER_CONTROL, powerManager.power_on());
  273. ENABLE_AXIS_X();
  274. ENABLE_AXIS_Y();
  275. ENABLE_AXIS_Z();
  276. enable_e_steppers();
  277. }
  278. void disable_e_steppers() {
  279. #define _DIS_E(N) DISABLE_AXIS_E##N();
  280. REPEAT(E_STEPPERS, _DIS_E)
  281. }
  282. void disable_e_stepper(const uint8_t e) {
  283. #define _CASE_DIS_E(N) case N: DISABLE_AXIS_E##N(); break;
  284. switch (e) {
  285. REPEAT(EXTRUDERS, _CASE_DIS_E)
  286. }
  287. }
  288. void disable_all_steppers() {
  289. DISABLE_AXIS_X();
  290. DISABLE_AXIS_Y();
  291. DISABLE_AXIS_Z();
  292. disable_e_steppers();
  293. }
  294. #if ENABLED(G29_RETRY_AND_RECOVER)
  295. void event_probe_failure() {
  296. #ifdef ACTION_ON_G29_FAILURE
  297. host_action(PSTR(ACTION_ON_G29_FAILURE));
  298. #endif
  299. #ifdef G29_FAILURE_COMMANDS
  300. gcode.process_subcommands_now_P(PSTR(G29_FAILURE_COMMANDS));
  301. #endif
  302. #if ENABLED(G29_HALT_ON_FAILURE)
  303. #ifdef ACTION_ON_CANCEL
  304. host_action_cancel();
  305. #endif
  306. kill(GET_TEXT(MSG_LCD_PROBING_FAILED));
  307. #endif
  308. }
  309. void event_probe_recover() {
  310. TERN_(HOST_PROMPT_SUPPORT, host_prompt_do(PROMPT_INFO, PSTR("G29 Retrying"), DISMISS_STR));
  311. #ifdef ACTION_ON_G29_RECOVER
  312. host_action(PSTR(ACTION_ON_G29_RECOVER));
  313. #endif
  314. #ifdef G29_RECOVER_COMMANDS
  315. gcode.process_subcommands_now_P(PSTR(G29_RECOVER_COMMANDS));
  316. #endif
  317. }
  318. #endif
  319. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  320. #include "feature/pause.h"
  321. #else
  322. constexpr bool did_pause_print = false;
  323. #endif
  324. /**
  325. * A Print Job exists when the timer is running or SD printing
  326. */
  327. bool printJobOngoing() {
  328. return print_job_timer.isRunning() || IS_SD_PRINTING();
  329. }
  330. /**
  331. * Printing is active when the print job timer is running
  332. */
  333. bool printingIsActive() {
  334. return !did_pause_print && (print_job_timer.isRunning() || IS_SD_PRINTING());
  335. }
  336. /**
  337. * Printing is paused according to SD or host indicators
  338. */
  339. bool printingIsPaused() {
  340. return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
  341. }
  342. void startOrResumeJob() {
  343. if (!printingIsPaused()) {
  344. TERN_(CANCEL_OBJECTS, cancelable.reset());
  345. TERN_(LCD_SHOW_E_TOTAL, e_move_accumulator = 0);
  346. #if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
  347. ui.reset_remaining_time();
  348. #endif
  349. }
  350. print_job_timer.start();
  351. }
  352. #if ENABLED(SDSUPPORT)
  353. inline void abortSDPrinting() {
  354. card.endFilePrint(TERN_(SD_RESORT, true));
  355. queue.clear();
  356. quickstop_stepper();
  357. print_job_timer.stop();
  358. #if DISABLED(SD_ABORT_NO_COOLDOWN)
  359. thermalManager.disable_all_heaters();
  360. #endif
  361. #if !HAS_CUTTER
  362. thermalManager.zero_fan_speeds();
  363. #else
  364. cutter.kill(); // Full cutter shutdown including ISR control
  365. #endif
  366. wait_for_heatup = false;
  367. TERN_(POWER_LOSS_RECOVERY, recovery.purge());
  368. #ifdef EVENT_GCODE_SD_STOP
  369. queue.inject_P(PSTR(EVENT_GCODE_SD_STOP));
  370. #endif
  371. }
  372. inline void finishSDPrinting() {
  373. if (queue.enqueue_one_P(PSTR("M1001")))
  374. marlin_state = MF_RUNNING;
  375. }
  376. #endif // SDSUPPORT
  377. /**
  378. * Minimal management of Marlin's core activities:
  379. * - Keep the command buffer full
  380. * - Check for maximum inactive time between commands
  381. * - Check for maximum inactive time between stepper commands
  382. * - Check if CHDK_PIN needs to go LOW
  383. * - Check for KILL button held down
  384. * - Check for HOME button held down
  385. * - Check if cooling fan needs to be switched on
  386. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  387. * - Pulse FET_SAFETY_PIN if it exists
  388. */
  389. inline void manage_inactivity(const bool ignore_stepper_queue=false) {
  390. if (queue.length < BUFSIZE) queue.get_available_commands();
  391. const millis_t ms = millis();
  392. // Prevent steppers timing-out in the middle of M600
  393. // unless PAUSE_PARK_NO_STEPPER_TIMEOUT is disabled
  394. const bool parked_or_ignoring = ignore_stepper_queue ||
  395. (BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT) && did_pause_print);
  396. // Reset both the M18/M84 activity timeout and the M85 max 'kill' timeout
  397. if (parked_or_ignoring) gcode.reset_stepper_timeout(ms);
  398. if (gcode.stepper_max_timed_out(ms)) {
  399. SERIAL_ERROR_START();
  400. SERIAL_ECHOLNPAIR(STR_KILL_INACTIVE_TIME, parser.command_ptr);
  401. kill();
  402. }
  403. // M18 / M94 : Handle steppers inactive time timeout
  404. if (gcode.stepper_inactive_time) {
  405. static bool already_shutdown_steppers; // = false
  406. // Any moves in the planner? Resets both the M18/M84
  407. // activity timeout and the M85 max 'kill' timeout
  408. if (planner.has_blocks_queued())
  409. gcode.reset_stepper_timeout(ms);
  410. else if (!parked_or_ignoring && gcode.stepper_inactive_timeout()) {
  411. if (!already_shutdown_steppers) {
  412. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  413. // Individual axes will be disabled if configured
  414. if (ENABLED(DISABLE_INACTIVE_X)) DISABLE_AXIS_X();
  415. if (ENABLED(DISABLE_INACTIVE_Y)) DISABLE_AXIS_Y();
  416. if (ENABLED(DISABLE_INACTIVE_Z)) DISABLE_AXIS_Z();
  417. if (ENABLED(DISABLE_INACTIVE_E)) disable_e_steppers();
  418. TERN_(AUTO_BED_LEVELING_UBL, ubl.steppers_were_disabled());
  419. }
  420. }
  421. else
  422. already_shutdown_steppers = false;
  423. }
  424. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  425. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  426. chdk_timeout = 0;
  427. WRITE(CHDK_PIN, LOW);
  428. }
  429. #endif
  430. #if HAS_KILL
  431. // Check if the kill button was pressed and wait just in case it was an accidental
  432. // key kill key press
  433. // -------------------------------------------------------------------------------
  434. static int killCount = 0; // make the inactivity button a bit less responsive
  435. const int KILL_DELAY = 750;
  436. if (kill_state())
  437. killCount++;
  438. else if (killCount > 0)
  439. killCount--;
  440. // Exceeded threshold and we can confirm that it was not accidental
  441. // KILL the machine
  442. // ----------------------------------------------------------------
  443. if (killCount >= KILL_DELAY) {
  444. SERIAL_ERROR_MSG(STR_KILL_BUTTON);
  445. kill();
  446. }
  447. #endif
  448. #if HAS_HOME
  449. // Handle a standalone HOME button
  450. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  451. static millis_t next_home_key_ms; // = 0
  452. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  453. const millis_t ms = millis();
  454. if (ELAPSED(ms, next_home_key_ms)) {
  455. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  456. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  457. queue.enqueue_now_P(G28_STR);
  458. }
  459. }
  460. #endif
  461. TERN_(USE_CONTROLLER_FAN, controllerFan.update()); // Check if fan should be turned on to cool stepper drivers down
  462. TERN_(AUTO_POWER_CONTROL, powerManager.check());
  463. TERN_(HOTEND_IDLE_TIMEOUT, hotend_idle.check());
  464. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  465. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  466. && ELAPSED(ms, gcode.previous_move_ms + SEC_TO_MS(EXTRUDER_RUNOUT_SECONDS))
  467. && !planner.has_blocks_queued()
  468. ) {
  469. #if ENABLED(SWITCHING_EXTRUDER)
  470. bool oldstatus;
  471. switch (active_extruder) {
  472. default: oldstatus = E0_ENABLE_READ(); ENABLE_AXIS_E0(); break;
  473. #if E_STEPPERS > 1
  474. case 2: case 3: oldstatus = E1_ENABLE_READ(); ENABLE_AXIS_E1(); break;
  475. #if E_STEPPERS > 2
  476. case 4: case 5: oldstatus = E2_ENABLE_READ(); ENABLE_AXIS_E2(); break;
  477. #if E_STEPPERS > 3
  478. case 6: case 7: oldstatus = E3_ENABLE_READ(); ENABLE_AXIS_E3(); break;
  479. #endif // E_STEPPERS > 3
  480. #endif // E_STEPPERS > 2
  481. #endif // E_STEPPERS > 1
  482. }
  483. #else // !SWITCHING_EXTRUDER
  484. bool oldstatus;
  485. switch (active_extruder) {
  486. default:
  487. #define _CASE_EN(N) case N: oldstatus = E##N##_ENABLE_READ(); ENABLE_AXIS_E##N(); break;
  488. REPEAT(E_STEPPERS, _CASE_EN);
  489. }
  490. #endif
  491. const float olde = current_position.e;
  492. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  493. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  494. current_position.e = olde;
  495. planner.set_e_position_mm(olde);
  496. planner.synchronize();
  497. #if ENABLED(SWITCHING_EXTRUDER)
  498. switch (active_extruder) {
  499. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  500. #if E_STEPPERS > 1
  501. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  502. #if E_STEPPERS > 2
  503. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  504. #endif // E_STEPPERS > 2
  505. #endif // E_STEPPERS > 1
  506. }
  507. #else // !SWITCHING_EXTRUDER
  508. switch (active_extruder) {
  509. #define _CASE_RESTORE(N) case N: E##N##_ENABLE_WRITE(oldstatus); break;
  510. REPEAT(E_STEPPERS, _CASE_RESTORE);
  511. }
  512. #endif // !SWITCHING_EXTRUDER
  513. gcode.reset_stepper_timeout(ms);
  514. }
  515. #endif // EXTRUDER_RUNOUT_PREVENT
  516. #if ENABLED(DUAL_X_CARRIAGE)
  517. // handle delayed move timeout
  518. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  519. // travel moves have been received so enact them
  520. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  521. destination = current_position;
  522. prepare_line_to_destination();
  523. }
  524. #endif
  525. TERN_(TEMP_STAT_LEDS, handle_status_leds());
  526. TERN_(MONITOR_DRIVER_STATUS, monitor_tmc_drivers());
  527. TERN_(MONITOR_L6470_DRIVER_STATUS, L64xxManager.monitor_driver());
  528. // Limit check_axes_activity frequency to 10Hz
  529. static millis_t next_check_axes_ms = 0;
  530. if (ELAPSED(ms, next_check_axes_ms)) {
  531. planner.check_axes_activity();
  532. next_check_axes_ms = ms + 100UL;
  533. }
  534. #if PIN_EXISTS(FET_SAFETY)
  535. static millis_t FET_next;
  536. if (ELAPSED(ms, FET_next)) {
  537. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  538. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  539. DELAY_US(2);
  540. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  541. }
  542. #endif
  543. }
  544. /**
  545. * Standard idle routine keeps the machine alive:
  546. * - Core Marlin activities
  547. * - Manage heaters (and Watchdog)
  548. * - Max7219 heartbeat, animation, etc.
  549. *
  550. * Only after setup() is complete:
  551. * - Handle filament runout sensors
  552. * - Run HAL idle tasks
  553. * - Handle Power-Loss Recovery
  554. * - Run StallGuard endstop checks
  555. * - Handle SD Card insert / remove
  556. * - Handle USB Flash Drive insert / remove
  557. * - Announce Host Keepalive state (if any)
  558. * - Update the Print Job Timer state
  559. * - Update the Beeper queue
  560. * - Read Buttons and Update the LCD
  561. * - Run i2c Position Encoders
  562. * - Auto-report Temperatures / SD Status
  563. * - Update the Prusa MMU2
  564. * - Handle Joystick jogging
  565. */
  566. void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
  567. // Core Marlin activities
  568. manage_inactivity(TERN_(ADVANCED_PAUSE_FEATURE, no_stepper_sleep));
  569. // Manage Heaters (and Watchdog)
  570. thermalManager.manage_heater();
  571. // Max7219 heartbeat, animation, etc
  572. TERN_(MAX7219_DEBUG, max7219.idle_tasks());
  573. // Return if setup() isn't completed
  574. if (marlin_state == MF_INITIALIZING) return;
  575. // Handle filament runout sensors
  576. TERN_(HAS_FILAMENT_SENSOR, runout.run());
  577. // Run HAL idle tasks
  578. #ifdef HAL_IDLETASK
  579. HAL_idletask();
  580. #endif
  581. // Handle Power-Loss Recovery
  582. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  583. if (printJobOngoing()) recovery.outage();
  584. #endif
  585. // Run StallGuard endstop checks
  586. #if ENABLED(SPI_ENDSTOPS)
  587. if (endstops.tmc_spi_homing.any
  588. && TERN1(IMPROVE_HOMING_RELIABILITY, ELAPSED(millis(), sg_guard_period))
  589. ) LOOP_L_N(i, 4) // Read SGT 4 times per idle loop
  590. if (endstops.tmc_spi_homing_check()) break;
  591. #endif
  592. // Handle SD Card insert / remove
  593. TERN_(SDSUPPORT, card.manage_media());
  594. // Handle USB Flash Drive insert / remove
  595. TERN_(USB_FLASH_DRIVE_SUPPORT, Sd2Card::idle());
  596. // Announce Host Keepalive state (if any)
  597. TERN_(HOST_KEEPALIVE_FEATURE, gcode.host_keepalive());
  598. // Update the Print Job Timer state
  599. TERN_(PRINTCOUNTER, print_job_timer.tick());
  600. // Update the Beeper queue
  601. TERN_(USE_BEEPER, buzzer.tick());
  602. // Handle UI input / draw events
  603. TERN(DWIN_CREALITY_LCD, DWIN_Update(), ui.update());
  604. // Run i2c Position Encoders
  605. #if ENABLED(I2C_POSITION_ENCODERS)
  606. static millis_t i2cpem_next_update_ms;
  607. if (planner.has_blocks_queued()) {
  608. const millis_t ms = millis();
  609. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  610. I2CPEM.update();
  611. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  612. }
  613. }
  614. #endif
  615. // Auto-report Temperatures / SD Status
  616. #if HAS_AUTO_REPORTING
  617. if (!gcode.autoreport_paused) {
  618. TERN_(AUTO_REPORT_TEMPERATURES, thermalManager.auto_report_temperatures());
  619. TERN_(AUTO_REPORT_SD_STATUS, card.auto_report_sd_status());
  620. }
  621. #endif
  622. // Update the Prusa MMU2
  623. TERN_(PRUSA_MMU2, mmu2.mmu_loop());
  624. // Handle Joystick jogging
  625. TERN_(POLL_JOG, joystick.inject_jog_moves());
  626. // Direct Stepping
  627. TERN_(DIRECT_STEPPING, page_manager.write_responses());
  628. #if HAS_TFT_LVGL_UI
  629. LV_TASK_HANDLER();
  630. #endif
  631. }
  632. /**
  633. * Kill all activity and lock the machine.
  634. * After this the machine will need to be reset.
  635. */
  636. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  637. thermalManager.disable_all_heaters();
  638. TERN_(HAS_CUTTER, cutter.kill()); // Full cutter shutdown including ISR control
  639. SERIAL_ERROR_MSG(STR_ERR_KILLED);
  640. #if HAS_DISPLAY
  641. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component ?: NUL_STR);
  642. #else
  643. UNUSED(lcd_error);
  644. UNUSED(lcd_component);
  645. #endif
  646. #ifdef ACTION_ON_KILL
  647. host_action_kill();
  648. #endif
  649. minkill(steppers_off);
  650. }
  651. void minkill(const bool steppers_off/*=false*/) {
  652. // Wait a short time (allows messages to get out before shutting down.
  653. for (int i = 1000; i--;) DELAY_US(600);
  654. cli(); // Stop interrupts
  655. // Wait to ensure all interrupts stopped
  656. for (int i = 1000; i--;) DELAY_US(250);
  657. // Reiterate heaters off
  658. thermalManager.disable_all_heaters();
  659. TERN_(HAS_CUTTER, cutter.kill()); // Reiterate cutter shutdown
  660. // Power off all steppers (for M112) or just the E steppers
  661. steppers_off ? disable_all_steppers() : disable_e_steppers();
  662. TERN_(PSU_CONTROL, PSU_OFF());
  663. TERN_(HAS_SUICIDE, suicide());
  664. #if HAS_KILL
  665. // Wait for kill to be released
  666. while (kill_state()) watchdog_refresh();
  667. // Wait for kill to be pressed
  668. while (!kill_state()) watchdog_refresh();
  669. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  670. resetFunc(); // Jump to address 0
  671. #else
  672. for (;;) watchdog_refresh(); // Wait for reset
  673. #endif
  674. }
  675. /**
  676. * Turn off heaters and stop the print in progress
  677. * After a stop the machine may be resumed with M999
  678. */
  679. void stop() {
  680. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  681. print_job_timer.stop();
  682. #if ENABLED(PROBING_FANS_OFF)
  683. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  684. #endif
  685. if (IsRunning()) {
  686. SERIAL_ERROR_MSG(STR_ERR_STOPPED);
  687. LCD_MESSAGEPGM(MSG_STOPPED);
  688. safe_delay(350); // allow enough time for messages to get out before stopping
  689. marlin_state = MF_STOPPED;
  690. }
  691. }
  692. /**
  693. * Marlin entry-point: Set up before the program loop
  694. * - Set up the kill pin, filament runout, power hold
  695. * - Start the serial port
  696. * - Print startup messages and diagnostics
  697. * - Get EEPROM or default settings
  698. * - Initialize managers for:
  699. * • temperature
  700. * • planner
  701. * • watchdog
  702. * • stepper
  703. * • photo pin
  704. * • servos
  705. * • LCD controller
  706. * • Digipot I2C
  707. * • Z probe sled
  708. * • status LEDs
  709. * • Max7219
  710. */
  711. void setup() {
  712. #if ENABLED(MARLIN_DEV_MODE)
  713. auto log_current_ms = [&](PGM_P const msg) {
  714. SERIAL_ECHO_START();
  715. SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHOPGM("] ");
  716. serialprintPGM(msg);
  717. SERIAL_EOL();
  718. };
  719. #define SETUP_LOG(M) log_current_ms(PSTR(M))
  720. #else
  721. #define SETUP_LOG(...) NOOP
  722. #endif
  723. #define SETUP_RUN(C) do{ SETUP_LOG(STRINGIFY(C)); C; }while(0)
  724. #if EITHER(DISABLE_DEBUG, DISABLE_JTAG)
  725. // Disable any hardware debug to free up pins for IO
  726. #if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
  727. JTAGSWD_DISABLE();
  728. #elif defined(JTAG_DISABLE)
  729. JTAG_DISABLE();
  730. #else
  731. #error "DISABLE_(DEBUG|JTAG) is not supported for the selected MCU/Board."
  732. #endif
  733. #endif
  734. #if NUM_SERIAL > 0
  735. MYSERIAL0.begin(BAUDRATE);
  736. uint32_t serial_connect_timeout = millis() + 1000UL;
  737. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  738. #if HAS_MULTI_SERIAL
  739. MYSERIAL1.begin(BAUDRATE);
  740. serial_connect_timeout = millis() + 1000UL;
  741. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  742. #endif
  743. SERIAL_ECHO_MSG("start");
  744. #endif
  745. SETUP_RUN(HAL_init());
  746. #if HAS_L64XX
  747. SETUP_RUN(L64xxManager.init()); // Set up SPI, init drivers
  748. #endif
  749. #if ENABLED(SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  750. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  751. #endif
  752. #if HAS_FILAMENT_SENSOR
  753. SETUP_RUN(runout.setup());
  754. #endif
  755. #if ENABLED(POWER_LOSS_RECOVERY)
  756. SETUP_RUN(recovery.setup());
  757. #endif
  758. SETUP_RUN(setup_killpin());
  759. #if HAS_TMC220x
  760. SETUP_RUN(tmc_serial_begin());
  761. #endif
  762. SETUP_RUN(setup_powerhold());
  763. #if HAS_STEPPER_RESET
  764. SETUP_RUN(disableStepperDrivers());
  765. #endif
  766. #if HAS_TMC_SPI
  767. #if DISABLED(TMC_USE_SW_SPI)
  768. SETUP_RUN(SPI.begin());
  769. #endif
  770. SETUP_RUN(tmc_init_cs_pins());
  771. #endif
  772. #ifdef BOARD_INIT
  773. SETUP_LOG("BOARD_INIT");
  774. BOARD_INIT();
  775. #endif
  776. SETUP_RUN(esp_wifi_init());
  777. // Check startup - does nothing if bootloader sets MCUSR to 0
  778. const byte mcu = HAL_get_reset_source();
  779. if (mcu & RST_POWER_ON) SERIAL_ECHOLNPGM(STR_POWERUP);
  780. if (mcu & RST_EXTERNAL) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  781. if (mcu & RST_BROWN_OUT) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  782. if (mcu & RST_WATCHDOG) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  783. if (mcu & RST_SOFTWARE) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  784. HAL_clear_reset_source();
  785. serialprintPGM(GET_TEXT(MSG_MARLIN));
  786. SERIAL_CHAR(' ');
  787. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  788. SERIAL_EOL();
  789. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  790. SERIAL_ECHO_MSG(
  791. STR_CONFIGURATION_VER
  792. STRING_DISTRIBUTION_DATE
  793. STR_AUTHOR STRING_CONFIG_H_AUTHOR
  794. );
  795. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  796. #endif
  797. SERIAL_ECHO_START();
  798. SERIAL_ECHOLNPAIR(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  799. // Set up LEDs early
  800. #if HAS_COLOR_LEDS
  801. SETUP_RUN(leds.setup());
  802. #endif
  803. #if ENABLED(USE_CONTROLLER_FAN) // Set up fan controller to initialize also the default configurations.
  804. SETUP_RUN(controllerFan.setup());
  805. #endif
  806. // UI must be initialized before EEPROM
  807. // (because EEPROM code calls the UI).
  808. #if ENABLED(DWIN_CREALITY_LCD)
  809. delay(800); // Required delay (since boot?)
  810. SERIAL_ECHOPGM("\nDWIN handshake ");
  811. if (DWIN_Handshake()) SERIAL_ECHOLNPGM("ok."); else SERIAL_ECHOLNPGM("error.");
  812. DWIN_Frame_SetDir(1); // Orientation 90°
  813. DWIN_UpdateLCD(); // Show bootscreen (first image)
  814. #else
  815. SETUP_RUN(ui.init());
  816. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  817. SETUP_RUN(ui.show_bootscreen());
  818. #endif
  819. SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)
  820. #endif
  821. #if BOTH(SDSUPPORT, SDCARD_EEPROM_EMULATION)
  822. SETUP_RUN(card.mount()); // Mount media with settings before first_load
  823. #endif
  824. SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
  825. // This also updates variables in the planner, elsewhere
  826. #if HAS_TOUCH_XPT2046
  827. SETUP_RUN(touch.init());
  828. #endif
  829. TERN_(HAS_M206_COMMAND, current_position += home_offset); // Init current position based on home_offset
  830. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  831. SETUP_RUN(thermalManager.init()); // Initialize temperature loop
  832. SETUP_RUN(print_job_timer.init()); // Initial setup of print job timer
  833. SETUP_RUN(endstops.init()); // Init endstops and pullups
  834. SETUP_RUN(stepper.init()); // Init stepper. This enables interrupts!
  835. #if HAS_SERVOS
  836. SETUP_RUN(servo_init());
  837. #endif
  838. #if HAS_Z_SERVO_PROBE
  839. SETUP_RUN(probe.servo_probe_init());
  840. #endif
  841. #if HAS_PHOTOGRAPH
  842. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  843. #endif
  844. #if HAS_CUTTER
  845. SETUP_RUN(cutter.init());
  846. #endif
  847. #if ENABLED(COOLANT_MIST)
  848. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  849. #endif
  850. #if ENABLED(COOLANT_FLOOD)
  851. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  852. #endif
  853. #if HAS_BED_PROBE
  854. SETUP_RUN(endstops.enable_z_probe(false));
  855. #endif
  856. #if HAS_STEPPER_RESET
  857. SETUP_RUN(enableStepperDrivers());
  858. #endif
  859. #if HAS_I2C_DIGIPOT
  860. SETUP_RUN(digipot_i2c_init());
  861. #endif
  862. #if ENABLED(DAC_STEPPER_CURRENT)
  863. SETUP_RUN(dac_init());
  864. #endif
  865. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  866. OUT_WRITE(SOL1_PIN, LOW); // OFF
  867. #endif
  868. #if HAS_HOME
  869. SET_INPUT_PULLUP(HOME_PIN);
  870. #endif
  871. #if PIN_EXISTS(STAT_LED_RED)
  872. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  873. #endif
  874. #if PIN_EXISTS(STAT_LED_BLUE)
  875. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  876. #endif
  877. #if ENABLED(CASE_LIGHT_ENABLE)
  878. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  879. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  880. #endif
  881. SETUP_RUN(caselight.update_brightness());
  882. #endif
  883. #if ENABLED(MK2_MULTIPLEXER)
  884. SETUP_LOG("MK2_MULTIPLEXER");
  885. SET_OUTPUT(E_MUX0_PIN);
  886. SET_OUTPUT(E_MUX1_PIN);
  887. SET_OUTPUT(E_MUX2_PIN);
  888. #endif
  889. #if HAS_FANMUX
  890. SETUP_RUN(fanmux_init());
  891. #endif
  892. #if ENABLED(MIXING_EXTRUDER)
  893. SETUP_RUN(mixer.init());
  894. #endif
  895. #if ENABLED(BLTOUCH)
  896. SETUP_RUN(bltouch.init(/*set_voltage=*/true));
  897. #endif
  898. #if ENABLED(I2C_POSITION_ENCODERS)
  899. SETUP_RUN(I2CPEM.init());
  900. #endif
  901. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  902. SETUP_LOG("i2c...");
  903. i2c.onReceive(i2c_on_receive);
  904. i2c.onRequest(i2c_on_request);
  905. #endif
  906. #if DO_SWITCH_EXTRUDER
  907. SETUP_RUN(move_extruder_servo(0)); // Initialize extruder servo
  908. #endif
  909. #if ENABLED(SWITCHING_NOZZLE)
  910. SETUP_LOG("SWITCHING_NOZZLE");
  911. // Initialize nozzle servo(s)
  912. #if SWITCHING_NOZZLE_TWO_SERVOS
  913. lower_nozzle(0);
  914. raise_nozzle(1);
  915. #else
  916. move_nozzle_servo(0);
  917. #endif
  918. #endif
  919. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  920. SETUP_RUN(mpe_settings_init());
  921. #endif
  922. #if ENABLED(PARKING_EXTRUDER)
  923. SETUP_RUN(pe_solenoid_init());
  924. #endif
  925. #if ENABLED(SWITCHING_TOOLHEAD)
  926. SETUP_RUN(swt_init());
  927. #endif
  928. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  929. SETUP_RUN(est_init());
  930. #endif
  931. #if ENABLED(USE_WATCHDOG)
  932. SETUP_RUN(watchdog_init()); // Reinit watchdog after HAL_get_reset_source call
  933. #endif
  934. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  935. SETUP_RUN(closedloop.init());
  936. #endif
  937. #ifdef STARTUP_COMMANDS
  938. SETUP_LOG("STARTUP_COMMANDS");
  939. queue.inject_P(PSTR(STARTUP_COMMANDS));
  940. #endif
  941. #if ENABLED(HOST_PROMPT_SUPPORT)
  942. SETUP_RUN(host_action_prompt_end());
  943. #endif
  944. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  945. SETUP_RUN(test_tmc_connection(true, true, true, true));
  946. #endif
  947. #if ENABLED(PRUSA_MMU2)
  948. SETUP_RUN(mmu2.init());
  949. #endif
  950. #if ENABLED(IIC_BL24CXX_EEPROM)
  951. BL24CXX::init();
  952. const uint8_t err = BL24CXX::check();
  953. SERIAL_ECHO_TERNARY(err, "BL24CXX Check ", "failed", "succeeded", "!\n");
  954. #endif
  955. #if ENABLED(DWIN_CREALITY_LCD)
  956. Encoder_Configuration();
  957. HMI_Init();
  958. HMI_StartFrame(true);
  959. #endif
  960. #if HAS_SERVICE_INTERVALS && DISABLED(DWIN_CREALITY_LCD)
  961. ui.reset_status(true); // Show service messages or keep current status
  962. #endif
  963. #if ENABLED(MAX7219_DEBUG)
  964. SETUP_RUN(max7219.init());
  965. #endif
  966. #if ENABLED(DIRECT_STEPPING)
  967. SETUP_RUN(page_manager.init());
  968. #endif
  969. #if HAS_TFT_LVGL_UI
  970. #if ENABLED(SDSUPPORT)
  971. if (!card.isMounted()) SETUP_RUN(card.mount()); // Mount SD to load graphics and fonts
  972. #endif
  973. SETUP_RUN(tft_lvgl_init());
  974. #endif
  975. marlin_state = MF_RUNNING;
  976. SETUP_LOG("setup() completed.");
  977. }
  978. /**
  979. * The main Marlin program loop
  980. *
  981. * - Call idle() to handle all tasks between G-code commands
  982. * Note that no G-codes from the queue can be executed during idle()
  983. * but many G-codes can be called directly anytime like macros.
  984. * - Check whether SD card auto-start is needed now.
  985. * - Check whether SD print finishing is needed now.
  986. * - Run one G-code command from the immediate or main command queue
  987. * and open up one space. Commands in the main queue may come from sd
  988. * card, host, or by direct injection. The queue will continue to fill
  989. * as long as idle() or manage_inactivity() are being called.
  990. */
  991. void loop() {
  992. do {
  993. idle();
  994. #if ENABLED(SDSUPPORT)
  995. card.checkautostart();
  996. if (card.flag.abort_sd_printing) abortSDPrinting();
  997. if (marlin_state == MF_SD_COMPLETE) finishSDPrinting();
  998. #endif
  999. queue.advance();
  1000. endstops.event_handler();
  1001. TERN_(HAS_TFT_LVGL_UI, printer_state_polling());
  1002. } while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
  1003. }