My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MarlinCore.cpp 34KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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_I2C_DIGIPOT
  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(DAC_STEPPER_CURRENT)
  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. #ifdef ACTION_ON_KILL
  656. host_action_kill();
  657. #endif
  658. minkill(steppers_off);
  659. }
  660. void minkill(const bool steppers_off/*=false*/) {
  661. // Wait a short time (allows messages to get out before shutting down.
  662. for (int i = 1000; i--;) DELAY_US(600);
  663. cli(); // Stop interrupts
  664. // Wait to ensure all interrupts stopped
  665. for (int i = 1000; i--;) DELAY_US(250);
  666. // Reiterate heaters off
  667. thermalManager.disable_all_heaters();
  668. TERN_(HAS_CUTTER, cutter.kill()); // Reiterate cutter shutdown
  669. // Power off all steppers (for M112) or just the E steppers
  670. steppers_off ? disable_all_steppers() : disable_e_steppers();
  671. TERN_(PSU_CONTROL, PSU_OFF());
  672. TERN_(HAS_SUICIDE, suicide());
  673. #if HAS_KILL
  674. // Wait for kill to be released
  675. while (kill_state()) watchdog_refresh();
  676. // Wait for kill to be pressed
  677. while (!kill_state()) watchdog_refresh();
  678. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  679. resetFunc(); // Jump to address 0
  680. #else
  681. for (;;) watchdog_refresh(); // Wait for reset
  682. #endif
  683. }
  684. /**
  685. * Turn off heaters and stop the print in progress
  686. * After a stop the machine may be resumed with M999
  687. */
  688. void stop() {
  689. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  690. print_job_timer.stop();
  691. #if ENABLED(PROBING_FANS_OFF)
  692. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  693. #endif
  694. if (IsRunning()) {
  695. SERIAL_ERROR_MSG(STR_ERR_STOPPED);
  696. LCD_MESSAGEPGM(MSG_STOPPED);
  697. safe_delay(350); // allow enough time for messages to get out before stopping
  698. marlin_state = MF_STOPPED;
  699. }
  700. }
  701. /**
  702. * Marlin entry-point: Set up before the program loop
  703. * - Set up the kill pin, filament runout, power hold
  704. * - Start the serial port
  705. * - Print startup messages and diagnostics
  706. * - Get EEPROM or default settings
  707. * - Initialize managers for:
  708. * • temperature
  709. * • planner
  710. * • watchdog
  711. * • stepper
  712. * • photo pin
  713. * • servos
  714. * • LCD controller
  715. * • Digipot I2C
  716. * • Z probe sled
  717. * • status LEDs
  718. * • Max7219
  719. */
  720. void setup() {
  721. #if ENABLED(MARLIN_DEV_MODE)
  722. auto log_current_ms = [&](PGM_P const msg) {
  723. SERIAL_ECHO_START();
  724. SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHOPGM("] ");
  725. serialprintPGM(msg);
  726. SERIAL_EOL();
  727. };
  728. #define SETUP_LOG(M) log_current_ms(PSTR(M))
  729. #else
  730. #define SETUP_LOG(...) NOOP
  731. #endif
  732. #define SETUP_RUN(C) do{ SETUP_LOG(STRINGIFY(C)); C; }while(0)
  733. #if EITHER(DISABLE_DEBUG, DISABLE_JTAG)
  734. // Disable any hardware debug to free up pins for IO
  735. #if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
  736. JTAGSWD_DISABLE();
  737. #elif defined(JTAG_DISABLE)
  738. JTAG_DISABLE();
  739. #else
  740. #error "DISABLE_(DEBUG|JTAG) is not supported for the selected MCU/Board."
  741. #endif
  742. #endif
  743. #if NUM_SERIAL > 0
  744. MYSERIAL0.begin(BAUDRATE);
  745. uint32_t serial_connect_timeout = millis() + 1000UL;
  746. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  747. #if HAS_MULTI_SERIAL
  748. MYSERIAL1.begin(BAUDRATE);
  749. serial_connect_timeout = millis() + 1000UL;
  750. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  751. #endif
  752. SERIAL_ECHO_MSG("start");
  753. #endif
  754. SETUP_RUN(HAL_init());
  755. #if HAS_L64XX
  756. SETUP_RUN(L64xxManager.init()); // Set up SPI, init drivers
  757. #endif
  758. #if ENABLED(DUET_SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  759. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  760. #endif
  761. #if HAS_FILAMENT_SENSOR
  762. SETUP_RUN(runout.setup());
  763. #endif
  764. #if ENABLED(POWER_LOSS_RECOVERY)
  765. SETUP_RUN(recovery.setup());
  766. #endif
  767. SETUP_RUN(setup_killpin());
  768. #if HAS_TMC220x
  769. SETUP_RUN(tmc_serial_begin());
  770. #endif
  771. SETUP_RUN(setup_powerhold());
  772. #if HAS_STEPPER_RESET
  773. SETUP_RUN(disableStepperDrivers());
  774. #endif
  775. #if HAS_TMC_SPI
  776. #if DISABLED(TMC_USE_SW_SPI)
  777. SETUP_RUN(SPI.begin());
  778. #endif
  779. SETUP_RUN(tmc_init_cs_pins());
  780. #endif
  781. #ifdef BOARD_INIT
  782. SETUP_LOG("BOARD_INIT");
  783. BOARD_INIT();
  784. #endif
  785. SETUP_RUN(esp_wifi_init());
  786. // Check startup - does nothing if bootloader sets MCUSR to 0
  787. const byte mcu = HAL_get_reset_source();
  788. if (mcu & RST_POWER_ON) SERIAL_ECHOLNPGM(STR_POWERUP);
  789. if (mcu & RST_EXTERNAL) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  790. if (mcu & RST_BROWN_OUT) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  791. if (mcu & RST_WATCHDOG) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  792. if (mcu & RST_SOFTWARE) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  793. HAL_clear_reset_source();
  794. serialprintPGM(GET_TEXT(MSG_MARLIN));
  795. SERIAL_CHAR(' ');
  796. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  797. SERIAL_EOL();
  798. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  799. SERIAL_ECHO_MSG(
  800. " Last Updated: " STRING_DISTRIBUTION_DATE
  801. " | Author: " STRING_CONFIG_H_AUTHOR
  802. );
  803. #endif
  804. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  805. SERIAL_ECHO_MSG(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  806. // Set up LEDs early
  807. #if HAS_COLOR_LEDS
  808. SETUP_RUN(leds.setup());
  809. #endif
  810. #if ENABLED(NEOPIXEL2_SEPARATE)
  811. SETUP_RUN(leds2.setup());
  812. #endif
  813. #if ENABLED(USE_CONTROLLER_FAN) // Set up fan controller to initialize also the default configurations.
  814. SETUP_RUN(controllerFan.setup());
  815. #endif
  816. // UI must be initialized before EEPROM
  817. // (because EEPROM code calls the UI).
  818. #if ENABLED(DWIN_CREALITY_LCD)
  819. delay(800); // Required delay (since boot?)
  820. SERIAL_ECHOPGM("\nDWIN handshake ");
  821. if (DWIN_Handshake()) SERIAL_ECHOLNPGM("ok."); else SERIAL_ECHOLNPGM("error.");
  822. DWIN_Frame_SetDir(1); // Orientation 90°
  823. DWIN_UpdateLCD(); // Show bootscreen (first image)
  824. #else
  825. SETUP_RUN(ui.init());
  826. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  827. SETUP_RUN(ui.show_bootscreen());
  828. #endif
  829. SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)
  830. #endif
  831. #if BOTH(SDSUPPORT, SDCARD_EEPROM_EMULATION)
  832. SETUP_RUN(card.mount()); // Mount media with settings before first_load
  833. #endif
  834. SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
  835. // This also updates variables in the planner, elsewhere
  836. #if HAS_TOUCH_XPT2046
  837. SETUP_RUN(touch.init());
  838. #endif
  839. TERN_(HAS_M206_COMMAND, current_position += home_offset); // Init current position based on home_offset
  840. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  841. SETUP_RUN(thermalManager.init()); // Initialize temperature loop
  842. SETUP_RUN(print_job_timer.init()); // Initial setup of print job timer
  843. SETUP_RUN(endstops.init()); // Init endstops and pullups
  844. SETUP_RUN(stepper.init()); // Init stepper. This enables interrupts!
  845. #if HAS_SERVOS
  846. SETUP_RUN(servo_init());
  847. #endif
  848. #if HAS_Z_SERVO_PROBE
  849. SETUP_RUN(probe.servo_probe_init());
  850. #endif
  851. #if HAS_PHOTOGRAPH
  852. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  853. #endif
  854. #if HAS_CUTTER
  855. SETUP_RUN(cutter.init());
  856. #endif
  857. #if ENABLED(COOLANT_MIST)
  858. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  859. #endif
  860. #if ENABLED(COOLANT_FLOOD)
  861. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  862. #endif
  863. #if HAS_BED_PROBE
  864. SETUP_RUN(endstops.enable_z_probe(false));
  865. #endif
  866. #if HAS_STEPPER_RESET
  867. SETUP_RUN(enableStepperDrivers());
  868. #endif
  869. #if HAS_I2C_DIGIPOT
  870. SETUP_RUN(digipot_i2c_init());
  871. #endif
  872. #if ENABLED(DAC_STEPPER_CURRENT)
  873. SETUP_RUN(dac_init());
  874. #endif
  875. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  876. OUT_WRITE(SOL1_PIN, LOW); // OFF
  877. #endif
  878. #if HAS_HOME
  879. SET_INPUT_PULLUP(HOME_PIN);
  880. #endif
  881. #if PIN_EXISTS(STAT_LED_RED)
  882. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  883. #endif
  884. #if PIN_EXISTS(STAT_LED_BLUE)
  885. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  886. #endif
  887. #if ENABLED(CASE_LIGHT_ENABLE)
  888. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  889. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  890. #endif
  891. SETUP_RUN(caselight.update_brightness());
  892. #endif
  893. #if ENABLED(MK2_MULTIPLEXER)
  894. SETUP_LOG("MK2_MULTIPLEXER");
  895. SET_OUTPUT(E_MUX0_PIN);
  896. SET_OUTPUT(E_MUX1_PIN);
  897. SET_OUTPUT(E_MUX2_PIN);
  898. #endif
  899. #if HAS_FANMUX
  900. SETUP_RUN(fanmux_init());
  901. #endif
  902. #if ENABLED(MIXING_EXTRUDER)
  903. SETUP_RUN(mixer.init());
  904. #endif
  905. #if ENABLED(BLTOUCH)
  906. SETUP_RUN(bltouch.init(/*set_voltage=*/true));
  907. #endif
  908. #if ENABLED(I2C_POSITION_ENCODERS)
  909. SETUP_RUN(I2CPEM.init());
  910. #endif
  911. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  912. SETUP_LOG("i2c...");
  913. i2c.onReceive(i2c_on_receive);
  914. i2c.onRequest(i2c_on_request);
  915. #endif
  916. #if DO_SWITCH_EXTRUDER
  917. SETUP_RUN(move_extruder_servo(0)); // Initialize extruder servo
  918. #endif
  919. #if ENABLED(SWITCHING_NOZZLE)
  920. SETUP_LOG("SWITCHING_NOZZLE");
  921. // Initialize nozzle servo(s)
  922. #if SWITCHING_NOZZLE_TWO_SERVOS
  923. lower_nozzle(0);
  924. raise_nozzle(1);
  925. #else
  926. move_nozzle_servo(0);
  927. #endif
  928. #endif
  929. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  930. SETUP_RUN(mpe_settings_init());
  931. #endif
  932. #if ENABLED(PARKING_EXTRUDER)
  933. SETUP_RUN(pe_solenoid_init());
  934. #endif
  935. #if ENABLED(SWITCHING_TOOLHEAD)
  936. SETUP_RUN(swt_init());
  937. #endif
  938. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  939. SETUP_RUN(est_init());
  940. #endif
  941. #if ENABLED(USE_WATCHDOG)
  942. SETUP_RUN(watchdog_init()); // Reinit watchdog after HAL_get_reset_source call
  943. #endif
  944. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  945. SETUP_RUN(closedloop.init());
  946. #endif
  947. #ifdef STARTUP_COMMANDS
  948. SETUP_LOG("STARTUP_COMMANDS");
  949. queue.inject_P(PSTR(STARTUP_COMMANDS));
  950. #endif
  951. #if ENABLED(HOST_PROMPT_SUPPORT)
  952. SETUP_RUN(host_action_prompt_end());
  953. #endif
  954. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  955. SETUP_RUN(test_tmc_connection(true, true, true, true));
  956. #endif
  957. #if ENABLED(PRUSA_MMU2)
  958. SETUP_RUN(mmu2.init());
  959. #endif
  960. #if ENABLED(IIC_BL24CXX_EEPROM)
  961. BL24CXX::init();
  962. const uint8_t err = BL24CXX::check();
  963. SERIAL_ECHO_TERNARY(err, "BL24CXX Check ", "failed", "succeeded", "!\n");
  964. #endif
  965. #if ENABLED(DWIN_CREALITY_LCD)
  966. Encoder_Configuration();
  967. HMI_Init();
  968. HMI_StartFrame(true);
  969. #endif
  970. #if HAS_SERVICE_INTERVALS && DISABLED(DWIN_CREALITY_LCD)
  971. ui.reset_status(true); // Show service messages or keep current status
  972. #endif
  973. #if ENABLED(MAX7219_DEBUG)
  974. SETUP_RUN(max7219.init());
  975. #endif
  976. #if ENABLED(DIRECT_STEPPING)
  977. SETUP_RUN(page_manager.init());
  978. #endif
  979. #if HAS_TFT_LVGL_UI
  980. #if ENABLED(SDSUPPORT)
  981. if (!card.isMounted()) SETUP_RUN(card.mount()); // Mount SD to load graphics and fonts
  982. #endif
  983. SETUP_RUN(tft_lvgl_init());
  984. #endif
  985. #if ENABLED(PASSWORD_ON_STARTUP)
  986. SETUP_RUN(password.lock_machine()); // Will not proceed until correct password provided
  987. #endif
  988. marlin_state = MF_RUNNING;
  989. SETUP_LOG("setup() completed.");
  990. }
  991. /**
  992. * The main Marlin program loop
  993. *
  994. * - Call idle() to handle all tasks between G-code commands
  995. * Note that no G-codes from the queue can be executed during idle()
  996. * but many G-codes can be called directly anytime like macros.
  997. * - Check whether SD card auto-start is needed now.
  998. * - Check whether SD print finishing is needed now.
  999. * - Run one G-code command from the immediate or main command queue
  1000. * and open up one space. Commands in the main queue may come from sd
  1001. * card, host, or by direct injection. The queue will continue to fill
  1002. * as long as idle() or manage_inactivity() are being called.
  1003. */
  1004. void loop() {
  1005. do {
  1006. idle();
  1007. #if ENABLED(SDSUPPORT)
  1008. card.checkautostart();
  1009. if (card.flag.abort_sd_printing) abortSDPrinting();
  1010. if (marlin_state == MF_SD_COMPLETE) finishSDPrinting();
  1011. #endif
  1012. queue.advance();
  1013. endstops.event_handler();
  1014. TERN_(HAS_TFT_LVGL_UI, printer_state_polling());
  1015. } while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
  1016. }