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.

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