My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MarlinCore.cpp 36KB

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