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

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