My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MarlinCore.cpp 31KB

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