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

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