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

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