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

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