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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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. #include "HAL/shared/cpu_exception/exception_hook.h"
  33. #ifdef ARDUINO
  34. #include <pins_arduino.h>
  35. #endif
  36. #include <math.h>
  37. #include "core/utility.h"
  38. #include "module/motion.h"
  39. #include "module/planner.h"
  40. #include "module/endstops.h"
  41. #include "module/temperature.h"
  42. #include "module/settings.h"
  43. #include "module/printcounter.h" // PrintCounter or Stopwatch
  44. #include "module/stepper.h"
  45. #include "module/stepper/indirection.h"
  46. #include "gcode/gcode.h"
  47. #include "gcode/parser.h"
  48. #include "gcode/queue.h"
  49. #include "feature/pause.h"
  50. #include "sd/cardreader.h"
  51. #include "lcd/marlinui.h"
  52. #if HAS_TOUCH_BUTTONS
  53. #include "lcd/touch/touch_buttons.h"
  54. #endif
  55. #if HAS_TFT_LVGL_UI
  56. #include "lcd/extui/mks_ui/tft_lvgl_configuration.h"
  57. #include "lcd/extui/mks_ui/draw_ui.h"
  58. #include "lcd/extui/mks_ui/mks_hardware.h"
  59. #include <lvgl.h>
  60. #endif
  61. #if HAS_DWIN_E3V2
  62. #include "lcd/e3v2/common/encoder.h"
  63. #if ENABLED(DWIN_CREALITY_LCD)
  64. #include "lcd/e3v2/creality/dwin.h"
  65. #elif ENABLED(DWIN_LCD_PROUI)
  66. #include "lcd/e3v2/proui/dwin.h"
  67. #elif ENABLED(DWIN_CREALITY_LCD_JYERSUI)
  68. #include "lcd/e3v2/jyersui/dwin.h"
  69. #endif
  70. #endif
  71. #if HAS_ETHERNET
  72. #include "feature/ethernet.h"
  73. #endif
  74. #if ENABLED(IIC_BL24CXX_EEPROM)
  75. #include "libs/BL24CXX.h"
  76. #endif
  77. #if ENABLED(DIRECT_STEPPING)
  78. #include "feature/direct_stepping.h"
  79. #endif
  80. #if ENABLED(HOST_ACTION_COMMANDS)
  81. #include "feature/host_actions.h"
  82. #endif
  83. #if USE_BEEPER
  84. #include "libs/buzzer.h"
  85. #endif
  86. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  87. #include "feature/closedloop.h"
  88. #endif
  89. #if HAS_MOTOR_CURRENT_I2C
  90. #include "feature/digipot/digipot.h"
  91. #endif
  92. #if ENABLED(MIXING_EXTRUDER)
  93. #include "feature/mixing.h"
  94. #endif
  95. #if ENABLED(MAX7219_DEBUG)
  96. #include "feature/max7219.h"
  97. #endif
  98. #if HAS_COLOR_LEDS
  99. #include "feature/leds/leds.h"
  100. #endif
  101. #if ENABLED(BLTOUCH)
  102. #include "feature/bltouch.h"
  103. #endif
  104. #if ENABLED(POLL_JOG)
  105. #include "feature/joystick.h"
  106. #endif
  107. #if HAS_SERVOS
  108. #include "module/servo.h"
  109. #endif
  110. #if HAS_MOTOR_CURRENT_DAC
  111. #include "feature/dac/stepper_dac.h"
  112. #endif
  113. #if ENABLED(EXPERIMENTAL_I2CBUS)
  114. #include "feature/twibus.h"
  115. #endif
  116. #if ENABLED(I2C_POSITION_ENCODERS)
  117. #include "feature/encoder_i2c.h"
  118. #endif
  119. #if (HAS_TRINAMIC_CONFIG || HAS_TMC_SPI) && DISABLED(PSU_DEFAULT_OFF)
  120. #include "feature/tmc_util.h"
  121. #endif
  122. #if HAS_CUTTER
  123. #include "feature/spindle_laser.h"
  124. #endif
  125. #if ENABLED(SDSUPPORT)
  126. CardReader card;
  127. #endif
  128. #if ENABLED(G38_PROBE_TARGET)
  129. uint8_t G38_move; // = 0
  130. bool G38_did_trigger; // = false
  131. #endif
  132. #if ENABLED(DELTA)
  133. #include "module/delta.h"
  134. #elif ENABLED(POLARGRAPH)
  135. #include "module/polargraph.h"
  136. #elif IS_SCARA
  137. #include "module/scara.h"
  138. #endif
  139. #if HAS_LEVELING
  140. #include "feature/bedlevel/bedlevel.h"
  141. #endif
  142. #if ENABLED(GCODE_REPEAT_MARKERS)
  143. #include "feature/repeat.h"
  144. #endif
  145. #if ENABLED(POWER_LOSS_RECOVERY)
  146. #include "feature/powerloss.h"
  147. #endif
  148. #if ENABLED(CANCEL_OBJECTS)
  149. #include "feature/cancel_object.h"
  150. #endif
  151. #if HAS_FILAMENT_SENSOR
  152. #include "feature/runout.h"
  153. #endif
  154. #if EITHER(PROBE_TARE, HAS_Z_SERVO_PROBE)
  155. #include "module/probe.h"
  156. #endif
  157. #if ENABLED(HOTEND_IDLE_TIMEOUT)
  158. #include "feature/hotend_idle.h"
  159. #endif
  160. #if ENABLED(TEMP_STAT_LEDS)
  161. #include "feature/leds/tempstat.h"
  162. #endif
  163. #if ENABLED(CASE_LIGHT_ENABLE)
  164. #include "feature/caselight.h"
  165. #endif
  166. #if HAS_FANMUX
  167. #include "feature/fanmux.h"
  168. #endif
  169. #include "module/tool_change.h"
  170. #if HAS_FANCHECK
  171. #include "feature/fancheck.h"
  172. #endif
  173. #if ENABLED(USE_CONTROLLER_FAN)
  174. #include "feature/controllerfan.h"
  175. #endif
  176. #if HAS_PRUSA_MMU1
  177. #include "feature/mmu/mmu.h"
  178. #endif
  179. #if HAS_PRUSA_MMU2
  180. #include "feature/mmu/mmu2.h"
  181. #endif
  182. #if HAS_L64XX
  183. #include "libs/L64XX/L64XX_Marlin.h"
  184. #endif
  185. #if ENABLED(PASSWORD_FEATURE)
  186. #include "feature/password/password.h"
  187. #endif
  188. #if ENABLED(DGUS_LCD_UI_MKS)
  189. #include "lcd/extui/dgus/DGUSScreenHandler.h"
  190. #endif
  191. #if HAS_DRIVER_SAFE_POWER_PROTECT
  192. #include "feature/stepper_driver_safety.h"
  193. #endif
  194. #if ENABLED(PSU_CONTROL)
  195. #include "feature/power.h"
  196. #endif
  197. #if ENABLED(EASYTHREED_UI)
  198. #include "feature/easythreed_ui.h"
  199. #endif
  200. PGMSTR(M112_KILL_STR, "M112 Shutdown");
  201. MarlinState marlin_state = MF_INITIALIZING;
  202. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  203. bool wait_for_heatup = true;
  204. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  205. #if HAS_RESUME_CONTINUE
  206. bool wait_for_user; // = false;
  207. void wait_for_user_response(millis_t ms/*=0*/, const bool no_sleep/*=false*/) {
  208. UNUSED(no_sleep);
  209. KEEPALIVE_STATE(PAUSED_FOR_USER);
  210. wait_for_user = true;
  211. if (ms) ms += millis(); // expire time
  212. while (wait_for_user && !(ms && ELAPSED(millis(), ms)))
  213. idle(TERN_(ADVANCED_PAUSE_FEATURE, no_sleep));
  214. wait_for_user = false;
  215. }
  216. #endif
  217. /**
  218. * ***************************************************************************
  219. * ******************************** FUNCTIONS ********************************
  220. * ***************************************************************************
  221. */
  222. /**
  223. * Stepper Reset (RigidBoard, et.al.)
  224. */
  225. #if HAS_STEPPER_RESET
  226. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  227. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  228. #endif
  229. /**
  230. * Sensitive pin test for M42, M226
  231. */
  232. #include "pins/sensitive_pins.h"
  233. #pragma GCC diagnostic push
  234. #pragma GCC diagnostic ignored "-Wnarrowing"
  235. #ifndef RUNTIME_ONLY_ANALOG_TO_DIGITAL
  236. template <pin_t ...D>
  237. constexpr pin_t OnlyPins<_SP_END, D...>::table[sizeof...(D)];
  238. #endif
  239. bool pin_is_protected(const pin_t pin) {
  240. #ifdef RUNTIME_ONLY_ANALOG_TO_DIGITAL
  241. static const pin_t sensitive_pins[] PROGMEM = { SENSITIVE_PINS };
  242. const size_t pincount = COUNT(sensitive_pins);
  243. #else
  244. static constexpr size_t pincount = OnlyPins<SENSITIVE_PINS>::size;
  245. static const pin_t (&sensitive_pins)[pincount] PROGMEM = OnlyPins<SENSITIVE_PINS>::table;
  246. #endif
  247. LOOP_L_N(i, pincount) {
  248. const pin_t * const pptr = &sensitive_pins[i];
  249. if (pin == (sizeof(pin_t) == 2 ? (pin_t)pgm_read_word(pptr) : (pin_t)pgm_read_byte(pptr))) return true;
  250. }
  251. return false;
  252. }
  253. #pragma GCC diagnostic pop
  254. /**
  255. * A Print Job exists when the timer is running or SD is printing
  256. */
  257. bool printJobOngoing() { return print_job_timer.isRunning() || IS_SD_PRINTING(); }
  258. /**
  259. * Printing is active when a job is underway but not paused
  260. */
  261. bool printingIsActive() { return !did_pause_print && printJobOngoing(); }
  262. /**
  263. * Printing is paused according to SD or host indicators
  264. */
  265. bool printingIsPaused() {
  266. return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
  267. }
  268. void startOrResumeJob() {
  269. if (!printingIsPaused()) {
  270. TERN_(GCODE_REPEAT_MARKERS, repeat.reset());
  271. TERN_(CANCEL_OBJECTS, cancelable.reset());
  272. TERN_(LCD_SHOW_E_TOTAL, e_move_accumulator = 0);
  273. #if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
  274. ui.reset_remaining_time();
  275. #endif
  276. }
  277. print_job_timer.start();
  278. }
  279. #if ENABLED(SDSUPPORT)
  280. inline void abortSDPrinting() {
  281. IF_DISABLED(NO_SD_AUTOSTART, card.autofile_cancel());
  282. card.abortFilePrintNow(TERN_(SD_RESORT, true));
  283. queue.clear();
  284. quickstop_stepper();
  285. print_job_timer.abort();
  286. IF_DISABLED(SD_ABORT_NO_COOLDOWN, thermalManager.disable_all_heaters());
  287. TERN(HAS_CUTTER, cutter.kill(), thermalManager.zero_fan_speeds()); // Full cutter shutdown including ISR control
  288. wait_for_heatup = false;
  289. TERN_(POWER_LOSS_RECOVERY, recovery.purge());
  290. #ifdef EVENT_GCODE_SD_ABORT
  291. queue.inject(F(EVENT_GCODE_SD_ABORT));
  292. #endif
  293. TERN_(PASSWORD_AFTER_SD_PRINT_ABORT, password.lock_machine());
  294. }
  295. inline void finishSDPrinting() {
  296. if (queue.enqueue_one(F("M1001"))) { // Keep trying until it gets queued
  297. marlin_state = MF_RUNNING; // Signal to stop trying
  298. TERN_(PASSWORD_AFTER_SD_PRINT_END, password.lock_machine());
  299. TERN_(DGUS_LCD_UI_MKS, ScreenHandler.SDPrintingFinished());
  300. }
  301. }
  302. #endif // SDSUPPORT
  303. /**
  304. * Minimal management of Marlin's core activities:
  305. * - Keep the command buffer full
  306. * - Check for maximum inactive time between commands
  307. * - Check for maximum inactive time between stepper commands
  308. * - Check if CHDK_PIN needs to go LOW
  309. * - Check for KILL button held down
  310. * - Check for HOME button held down
  311. * - Check for CUSTOM USER button held down
  312. * - Check if cooling fan needs to be switched on
  313. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  314. * - Pulse FET_SAFETY_PIN if it exists
  315. */
  316. inline void manage_inactivity(const bool no_stepper_sleep=false) {
  317. queue.get_available_commands();
  318. const millis_t ms = millis();
  319. // Prevent steppers timing-out
  320. const bool do_reset_timeout = no_stepper_sleep
  321. || TERN0(PAUSE_PARK_NO_STEPPER_TIMEOUT, did_pause_print);
  322. // Reset both the M18/M84 activity timeout and the M85 max 'kill' timeout
  323. if (do_reset_timeout) gcode.reset_stepper_timeout(ms);
  324. if (gcode.stepper_max_timed_out(ms)) {
  325. SERIAL_ERROR_MSG(STR_KILL_INACTIVE_TIME, parser.command_ptr);
  326. kill();
  327. }
  328. // M18 / M84 : Handle steppers inactive time timeout
  329. if (gcode.stepper_inactive_time) {
  330. static bool already_shutdown_steppers; // = false
  331. // Any moves in the planner? Resets both the M18/M84
  332. // activity timeout and the M85 max 'kill' timeout
  333. if (planner.has_blocks_queued())
  334. gcode.reset_stepper_timeout(ms);
  335. else if (!do_reset_timeout && gcode.stepper_inactive_timeout()) {
  336. if (!already_shutdown_steppers) {
  337. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  338. // Individual axes will be disabled if configured
  339. TERN_(DISABLE_INACTIVE_X, stepper.disable_axis(X_AXIS));
  340. TERN_(DISABLE_INACTIVE_Y, stepper.disable_axis(Y_AXIS));
  341. TERN_(DISABLE_INACTIVE_Z, stepper.disable_axis(Z_AXIS));
  342. TERN_(DISABLE_INACTIVE_I, stepper.disable_axis(I_AXIS));
  343. TERN_(DISABLE_INACTIVE_J, stepper.disable_axis(J_AXIS));
  344. TERN_(DISABLE_INACTIVE_K, stepper.disable_axis(K_AXIS));
  345. TERN_(DISABLE_INACTIVE_U, stepper.disable_axis(U_AXIS));
  346. TERN_(DISABLE_INACTIVE_V, stepper.disable_axis(V_AXIS));
  347. TERN_(DISABLE_INACTIVE_W, stepper.disable_axis(W_AXIS));
  348. TERN_(DISABLE_INACTIVE_E, stepper.disable_e_steppers());
  349. TERN_(AUTO_BED_LEVELING_UBL, ubl.steppers_were_disabled());
  350. }
  351. }
  352. else
  353. already_shutdown_steppers = false;
  354. }
  355. #if ENABLED(PHOTO_GCODE) && PIN_EXISTS(CHDK)
  356. // Check if CHDK should be set to LOW (after M240 set it HIGH)
  357. extern millis_t chdk_timeout;
  358. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  359. chdk_timeout = 0;
  360. WRITE(CHDK_PIN, LOW);
  361. }
  362. #endif
  363. #if HAS_KILL
  364. // Check if the kill button was pressed and wait just in case it was an accidental
  365. // key kill key press
  366. // -------------------------------------------------------------------------------
  367. static int killCount = 0; // make the inactivity button a bit less responsive
  368. const int KILL_DELAY = 750;
  369. if (kill_state())
  370. killCount++;
  371. else if (killCount > 0)
  372. killCount--;
  373. // Exceeded threshold and we can confirm that it was not accidental
  374. // KILL the machine
  375. // ----------------------------------------------------------------
  376. if (killCount >= KILL_DELAY) {
  377. SERIAL_ERROR_MSG(STR_KILL_BUTTON);
  378. kill();
  379. }
  380. #endif
  381. #if HAS_FREEZE_PIN
  382. stepper.frozen = READ(FREEZE_PIN) == FREEZE_STATE;
  383. #endif
  384. #if HAS_HOME
  385. // Handle a standalone HOME button
  386. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  387. static millis_t next_home_key_ms; // = 0
  388. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  389. if (ELAPSED(ms, next_home_key_ms)) {
  390. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  391. LCD_MESSAGE(MSG_AUTO_HOME);
  392. queue.inject_P(G28_STR);
  393. }
  394. }
  395. #endif
  396. #if ENABLED(CUSTOM_USER_BUTTONS)
  397. // Handle a custom user button if defined
  398. const bool printer_not_busy = !printingIsActive();
  399. #define HAS_CUSTOM_USER_BUTTON(N) (PIN_EXISTS(BUTTON##N) && defined(BUTTON##N##_HIT_STATE) && defined(BUTTON##N##_GCODE))
  400. #define HAS_BETTER_USER_BUTTON(N) HAS_CUSTOM_USER_BUTTON(N) && defined(BUTTON##N##_DESC)
  401. #define _CHECK_CUSTOM_USER_BUTTON(N, CODE) do{ \
  402. constexpr millis_t CUB_DEBOUNCE_DELAY_##N = 250UL; \
  403. static millis_t next_cub_ms_##N; \
  404. if (BUTTON##N##_HIT_STATE == READ(BUTTON##N##_PIN) \
  405. && (ENABLED(BUTTON##N##_WHEN_PRINTING) || printer_not_busy)) { \
  406. if (ELAPSED(ms, next_cub_ms_##N)) { \
  407. next_cub_ms_##N = ms + CUB_DEBOUNCE_DELAY_##N; \
  408. CODE; \
  409. queue.inject(F(BUTTON##N##_GCODE)); \
  410. TERN_(HAS_MARLINUI_MENU, ui.quick_feedback()); \
  411. } \
  412. } \
  413. }while(0)
  414. #define CHECK_CUSTOM_USER_BUTTON(N) _CHECK_CUSTOM_USER_BUTTON(N, NOOP)
  415. #define CHECK_BETTER_USER_BUTTON(N) _CHECK_CUSTOM_USER_BUTTON(N, if (strlen(BUTTON##N##_DESC)) LCD_MESSAGE_F(BUTTON##N##_DESC))
  416. #if HAS_BETTER_USER_BUTTON(1)
  417. CHECK_BETTER_USER_BUTTON(1);
  418. #elif HAS_CUSTOM_USER_BUTTON(1)
  419. CHECK_CUSTOM_USER_BUTTON(1);
  420. #endif
  421. #if HAS_BETTER_USER_BUTTON(2)
  422. CHECK_BETTER_USER_BUTTON(2);
  423. #elif HAS_CUSTOM_USER_BUTTON(2)
  424. CHECK_CUSTOM_USER_BUTTON(2);
  425. #endif
  426. #if HAS_BETTER_USER_BUTTON(3)
  427. CHECK_BETTER_USER_BUTTON(3);
  428. #elif HAS_CUSTOM_USER_BUTTON(3)
  429. CHECK_CUSTOM_USER_BUTTON(3);
  430. #endif
  431. #if HAS_BETTER_USER_BUTTON(4)
  432. CHECK_BETTER_USER_BUTTON(4);
  433. #elif HAS_CUSTOM_USER_BUTTON(4)
  434. CHECK_CUSTOM_USER_BUTTON(4);
  435. #endif
  436. #if HAS_BETTER_USER_BUTTON(5)
  437. CHECK_BETTER_USER_BUTTON(5);
  438. #elif HAS_CUSTOM_USER_BUTTON(5)
  439. CHECK_CUSTOM_USER_BUTTON(5);
  440. #endif
  441. #if HAS_BETTER_USER_BUTTON(6)
  442. CHECK_BETTER_USER_BUTTON(6);
  443. #elif HAS_CUSTOM_USER_BUTTON(6)
  444. CHECK_CUSTOM_USER_BUTTON(6);
  445. #endif
  446. #if HAS_BETTER_USER_BUTTON(7)
  447. CHECK_BETTER_USER_BUTTON(7);
  448. #elif HAS_CUSTOM_USER_BUTTON(7)
  449. CHECK_CUSTOM_USER_BUTTON(7);
  450. #endif
  451. #if HAS_BETTER_USER_BUTTON(8)
  452. CHECK_BETTER_USER_BUTTON(8);
  453. #elif HAS_CUSTOM_USER_BUTTON(8)
  454. CHECK_CUSTOM_USER_BUTTON(8);
  455. #endif
  456. #if HAS_BETTER_USER_BUTTON(9)
  457. CHECK_BETTER_USER_BUTTON(9);
  458. #elif HAS_CUSTOM_USER_BUTTON(9)
  459. CHECK_CUSTOM_USER_BUTTON(9);
  460. #endif
  461. #if HAS_BETTER_USER_BUTTON(10)
  462. CHECK_BETTER_USER_BUTTON(10);
  463. #elif HAS_CUSTOM_USER_BUTTON(10)
  464. CHECK_CUSTOM_USER_BUTTON(10);
  465. #endif
  466. #if HAS_BETTER_USER_BUTTON(11)
  467. CHECK_BETTER_USER_BUTTON(11);
  468. #elif HAS_CUSTOM_USER_BUTTON(11)
  469. CHECK_CUSTOM_USER_BUTTON(11);
  470. #endif
  471. #if HAS_BETTER_USER_BUTTON(12)
  472. CHECK_BETTER_USER_BUTTON(12);
  473. #elif HAS_CUSTOM_USER_BUTTON(12)
  474. CHECK_CUSTOM_USER_BUTTON(12);
  475. #endif
  476. #if HAS_BETTER_USER_BUTTON(13)
  477. CHECK_BETTER_USER_BUTTON(13);
  478. #elif HAS_CUSTOM_USER_BUTTON(13)
  479. CHECK_CUSTOM_USER_BUTTON(13);
  480. #endif
  481. #if HAS_BETTER_USER_BUTTON(14)
  482. CHECK_BETTER_USER_BUTTON(14);
  483. #elif HAS_CUSTOM_USER_BUTTON(14)
  484. CHECK_CUSTOM_USER_BUTTON(14);
  485. #endif
  486. #if HAS_BETTER_USER_BUTTON(15)
  487. CHECK_BETTER_USER_BUTTON(15);
  488. #elif HAS_CUSTOM_USER_BUTTON(15)
  489. CHECK_CUSTOM_USER_BUTTON(15);
  490. #endif
  491. #if HAS_BETTER_USER_BUTTON(16)
  492. CHECK_BETTER_USER_BUTTON(16);
  493. #elif HAS_CUSTOM_USER_BUTTON(16)
  494. CHECK_CUSTOM_USER_BUTTON(16);
  495. #endif
  496. #if HAS_BETTER_USER_BUTTON(17)
  497. CHECK_BETTER_USER_BUTTON(17);
  498. #elif HAS_CUSTOM_USER_BUTTON(17)
  499. CHECK_CUSTOM_USER_BUTTON(17);
  500. #endif
  501. #if HAS_BETTER_USER_BUTTON(18)
  502. CHECK_BETTER_USER_BUTTON(18);
  503. #elif HAS_CUSTOM_USER_BUTTON(18)
  504. CHECK_CUSTOM_USER_BUTTON(18);
  505. #endif
  506. #if HAS_BETTER_USER_BUTTON(19)
  507. CHECK_BETTER_USER_BUTTON(19);
  508. #elif HAS_CUSTOM_USER_BUTTON(19)
  509. CHECK_CUSTOM_USER_BUTTON(19);
  510. #endif
  511. #if HAS_BETTER_USER_BUTTON(20)
  512. CHECK_BETTER_USER_BUTTON(20);
  513. #elif HAS_CUSTOM_USER_BUTTON(20)
  514. CHECK_CUSTOM_USER_BUTTON(20);
  515. #endif
  516. #if HAS_BETTER_USER_BUTTON(21)
  517. CHECK_BETTER_USER_BUTTON(21);
  518. #elif HAS_CUSTOM_USER_BUTTON(21)
  519. CHECK_CUSTOM_USER_BUTTON(21);
  520. #endif
  521. #if HAS_BETTER_USER_BUTTON(22)
  522. CHECK_BETTER_USER_BUTTON(22);
  523. #elif HAS_CUSTOM_USER_BUTTON(22)
  524. CHECK_CUSTOM_USER_BUTTON(22);
  525. #endif
  526. #if HAS_BETTER_USER_BUTTON(23)
  527. CHECK_BETTER_USER_BUTTON(23);
  528. #elif HAS_CUSTOM_USER_BUTTON(23)
  529. CHECK_CUSTOM_USER_BUTTON(23);
  530. #endif
  531. #if HAS_BETTER_USER_BUTTON(24)
  532. CHECK_BETTER_USER_BUTTON(24);
  533. #elif HAS_CUSTOM_USER_BUTTON(24)
  534. CHECK_CUSTOM_USER_BUTTON(24);
  535. #endif
  536. #if HAS_BETTER_USER_BUTTON(25)
  537. CHECK_BETTER_USER_BUTTON(25);
  538. #elif HAS_CUSTOM_USER_BUTTON(25)
  539. CHECK_CUSTOM_USER_BUTTON(25);
  540. #endif
  541. #endif
  542. TERN_(EASYTHREED_UI, easythreed_ui.run());
  543. TERN_(USE_CONTROLLER_FAN, controllerFan.update()); // Check if fan should be turned on to cool stepper drivers down
  544. TERN_(AUTO_POWER_CONTROL, powerManager.check(!ui.on_status_screen() || printJobOngoing() || printingIsPaused()));
  545. TERN_(HOTEND_IDLE_TIMEOUT, hotend_idle.check());
  546. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  547. if (thermalManager.degHotend(active_extruder) > (EXTRUDER_RUNOUT_MINTEMP)
  548. && ELAPSED(ms, gcode.previous_move_ms + SEC_TO_MS(EXTRUDER_RUNOUT_SECONDS))
  549. && !planner.has_blocks_queued()
  550. ) {
  551. #if ENABLED(SWITCHING_EXTRUDER)
  552. bool oldstatus;
  553. switch (active_extruder) {
  554. default: oldstatus = stepper.AXIS_IS_ENABLED(E_AXIS, 0); stepper.ENABLE_EXTRUDER(0); break;
  555. #if E_STEPPERS > 1
  556. case 2: case 3: oldstatus = stepper.AXIS_IS_ENABLED(E_AXIS, 1); stepper.ENABLE_EXTRUDER(1); break;
  557. #if E_STEPPERS > 2
  558. case 4: case 5: oldstatus = stepper.AXIS_IS_ENABLED(E_AXIS, 2); stepper.ENABLE_EXTRUDER(2); break;
  559. #if E_STEPPERS > 3
  560. case 6: case 7: oldstatus = stepper.AXIS_IS_ENABLED(E_AXIS, 3); stepper.ENABLE_EXTRUDER(3); break;
  561. #endif // E_STEPPERS > 3
  562. #endif // E_STEPPERS > 2
  563. #endif // E_STEPPERS > 1
  564. }
  565. #else // !SWITCHING_EXTRUDER
  566. bool oldstatus;
  567. switch (active_extruder) {
  568. default:
  569. #define _CASE_EN(N) case N: oldstatus = stepper.AXIS_IS_ENABLED(E_AXIS, N); stepper.ENABLE_EXTRUDER(N); break;
  570. REPEAT(E_STEPPERS, _CASE_EN);
  571. }
  572. #endif
  573. const float olde = current_position.e;
  574. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  575. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  576. current_position.e = olde;
  577. planner.set_e_position_mm(olde);
  578. planner.synchronize();
  579. #if ENABLED(SWITCHING_EXTRUDER)
  580. switch (active_extruder) {
  581. default: if (oldstatus) stepper.ENABLE_EXTRUDER(0); else stepper.DISABLE_EXTRUDER(0); break;
  582. #if E_STEPPERS > 1
  583. case 2: case 3: if (oldstatus) stepper.ENABLE_EXTRUDER(1); else stepper.DISABLE_EXTRUDER(1); break;
  584. #if E_STEPPERS > 2
  585. case 4: case 5: if (oldstatus) stepper.ENABLE_EXTRUDER(2); else stepper.DISABLE_EXTRUDER(2); break;
  586. #endif // E_STEPPERS > 2
  587. #endif // E_STEPPERS > 1
  588. }
  589. #else // !SWITCHING_EXTRUDER
  590. switch (active_extruder) {
  591. #define _CASE_RESTORE(N) case N: if (oldstatus) stepper.ENABLE_EXTRUDER(N); else stepper.DISABLE_EXTRUDER(N); break;
  592. REPEAT(E_STEPPERS, _CASE_RESTORE);
  593. }
  594. #endif // !SWITCHING_EXTRUDER
  595. gcode.reset_stepper_timeout(ms);
  596. }
  597. #endif // EXTRUDER_RUNOUT_PREVENT
  598. #if ENABLED(DUAL_X_CARRIAGE)
  599. // handle delayed move timeout
  600. if (delayed_move_time && ELAPSED(ms, delayed_move_time) && IsRunning()) {
  601. // travel moves have been received so enact them
  602. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  603. destination = current_position;
  604. prepare_line_to_destination();
  605. planner.synchronize();
  606. }
  607. #endif
  608. TERN_(TEMP_STAT_LEDS, handle_status_leds());
  609. TERN_(MONITOR_DRIVER_STATUS, monitor_tmc_drivers());
  610. TERN_(MONITOR_L6470_DRIVER_STATUS, L64xxManager.monitor_driver());
  611. // Limit check_axes_activity frequency to 10Hz
  612. static millis_t next_check_axes_ms = 0;
  613. if (ELAPSED(ms, next_check_axes_ms)) {
  614. planner.check_axes_activity();
  615. next_check_axes_ms = ms + 100UL;
  616. }
  617. #if PIN_EXISTS(FET_SAFETY)
  618. static millis_t FET_next;
  619. if (ELAPSED(ms, FET_next)) {
  620. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  621. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  622. DELAY_US(2);
  623. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  624. }
  625. #endif
  626. }
  627. /**
  628. * Standard idle routine keeps the machine alive:
  629. * - Core Marlin activities
  630. * - Manage heaters (and Watchdog)
  631. * - Max7219 heartbeat, animation, etc.
  632. *
  633. * Only after setup() is complete:
  634. * - Handle filament runout sensors
  635. * - Run HAL idle tasks
  636. * - Handle Power-Loss Recovery
  637. * - Run StallGuard endstop checks
  638. * - Handle SD Card insert / remove
  639. * - Handle USB Flash Drive insert / remove
  640. * - Announce Host Keepalive state (if any)
  641. * - Update the Print Job Timer state
  642. * - Update the Beeper queue
  643. * - Read Buttons and Update the LCD
  644. * - Run i2c Position Encoders
  645. * - Auto-report Temperatures / SD Status
  646. * - Update the Průša MMU2
  647. * - Handle Joystick jogging
  648. */
  649. void idle(bool no_stepper_sleep/*=false*/) {
  650. #if ENABLED(MARLIN_DEV_MODE)
  651. static uint16_t idle_depth = 0;
  652. if (++idle_depth > 5) SERIAL_ECHOLNPGM("idle() call depth: ", idle_depth);
  653. #endif
  654. // Core Marlin activities
  655. manage_inactivity(no_stepper_sleep);
  656. // Manage Heaters (and Watchdog)
  657. thermalManager.manage_heater();
  658. // Max7219 heartbeat, animation, etc
  659. TERN_(MAX7219_DEBUG, max7219.idle_tasks());
  660. // Return if setup() isn't completed
  661. if (marlin_state == MF_INITIALIZING) goto IDLE_DONE;
  662. // TODO: Still causing errors
  663. (void)check_tool_sensor_stats(active_extruder, true);
  664. // Handle filament runout sensors
  665. #if HAS_FILAMENT_SENSOR
  666. if (TERN1(HAS_PRUSA_MMU2, !mmu2.enabled()))
  667. runout.run();
  668. #endif
  669. // Run HAL idle tasks
  670. hal.idletask();
  671. // Check network connection
  672. TERN_(HAS_ETHERNET, ethernet.check());
  673. // Handle Power-Loss Recovery
  674. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  675. if (IS_SD_PRINTING()) recovery.outage();
  676. #endif
  677. // Run StallGuard endstop checks
  678. #if ENABLED(SPI_ENDSTOPS)
  679. if (endstops.tmc_spi_homing.any && TERN1(IMPROVE_HOMING_RELIABILITY, ELAPSED(millis(), sg_guard_period)))
  680. LOOP_L_N(i, 4) if (endstops.tmc_spi_homing_check()) break; // Read SGT 4 times per idle loop
  681. #endif
  682. // Handle SD Card insert / remove
  683. TERN_(SDSUPPORT, card.manage_media());
  684. // Handle USB Flash Drive insert / remove
  685. TERN_(USB_FLASH_DRIVE_SUPPORT, card.diskIODriver()->idle());
  686. // Announce Host Keepalive state (if any)
  687. TERN_(HOST_KEEPALIVE_FEATURE, gcode.host_keepalive());
  688. // Update the Print Job Timer state
  689. TERN_(PRINTCOUNTER, print_job_timer.tick());
  690. // Update the Beeper queue
  691. TERN_(USE_BEEPER, buzzer.tick());
  692. // Handle UI input / draw events
  693. TERN(DWIN_CREALITY_LCD, DWIN_Update(), ui.update());
  694. // Run i2c Position Encoders
  695. #if ENABLED(I2C_POSITION_ENCODERS)
  696. {
  697. static millis_t i2cpem_next_update_ms;
  698. if (planner.has_blocks_queued()) {
  699. const millis_t ms = millis();
  700. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  701. I2CPEM.update();
  702. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  703. }
  704. }
  705. }
  706. #endif
  707. // Auto-report Temperatures / SD Status
  708. #if HAS_AUTO_REPORTING
  709. if (!gcode.autoreport_paused) {
  710. TERN_(AUTO_REPORT_TEMPERATURES, thermalManager.auto_reporter.tick());
  711. TERN_(AUTO_REPORT_FANS, fan_check.auto_reporter.tick());
  712. TERN_(AUTO_REPORT_SD_STATUS, card.auto_reporter.tick());
  713. TERN_(AUTO_REPORT_POSITION, position_auto_reporter.tick());
  714. TERN_(BUFFER_MONITORING, queue.auto_report_buffer_statistics());
  715. }
  716. #endif
  717. // Update the Průša MMU2
  718. TERN_(HAS_PRUSA_MMU2, mmu2.mmu_loop());
  719. // Handle Joystick jogging
  720. TERN_(POLL_JOG, joystick.inject_jog_moves());
  721. // Direct Stepping
  722. TERN_(DIRECT_STEPPING, page_manager.write_responses());
  723. // Update the LVGL interface
  724. TERN_(HAS_TFT_LVGL_UI, LV_TASK_HANDLER());
  725. IDLE_DONE:
  726. TERN_(MARLIN_DEV_MODE, idle_depth--);
  727. return;
  728. }
  729. /**
  730. * Kill all activity and lock the machine.
  731. * After this the machine will need to be reset.
  732. */
  733. void kill(FSTR_P const lcd_error/*=nullptr*/, FSTR_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  734. thermalManager.disable_all_heaters();
  735. TERN_(HAS_CUTTER, cutter.kill()); // Full cutter shutdown including ISR control
  736. // Echo the LCD message to serial for extra context
  737. if (lcd_error) { SERIAL_ECHO_START(); SERIAL_ECHOLNF(lcd_error); }
  738. #if EITHER(HAS_DISPLAY, DWIN_LCD_PROUI)
  739. ui.kill_screen(lcd_error ?: GET_TEXT_F(MSG_KILLED), lcd_component ?: FPSTR(NUL_STR));
  740. #else
  741. UNUSED(lcd_error); UNUSED(lcd_component);
  742. #endif
  743. TERN_(HAS_TFT_LVGL_UI, lv_draw_error_message(lcd_error));
  744. // "Error:Printer halted. kill() called!"
  745. SERIAL_ERROR_MSG(STR_ERR_KILLED);
  746. #ifdef ACTION_ON_KILL
  747. hostui.kill();
  748. #endif
  749. minkill(steppers_off);
  750. }
  751. void minkill(const bool steppers_off/*=false*/) {
  752. // Wait a short time (allows messages to get out before shutting down.
  753. for (int i = 1000; i--;) DELAY_US(600);
  754. cli(); // Stop interrupts
  755. // Wait to ensure all interrupts stopped
  756. for (int i = 1000; i--;) DELAY_US(250);
  757. // Reiterate heaters off
  758. thermalManager.disable_all_heaters();
  759. TERN_(HAS_CUTTER, cutter.kill()); // Reiterate cutter shutdown
  760. // Power off all steppers (for M112) or just the E steppers
  761. steppers_off ? stepper.disable_all_steppers() : stepper.disable_e_steppers();
  762. TERN_(PSU_CONTROL, powerManager.power_off());
  763. TERN_(HAS_SUICIDE, suicide());
  764. #if EITHER(HAS_KILL, SOFT_RESET_ON_KILL)
  765. // Wait for both KILL and ENC to be released
  766. while (TERN0(HAS_KILL, kill_state()) || TERN0(SOFT_RESET_ON_KILL, ui.button_pressed()))
  767. watchdog_refresh();
  768. // Wait for either KILL or ENC to be pressed again
  769. while (TERN1(HAS_KILL, !kill_state()) && TERN1(SOFT_RESET_ON_KILL, !ui.button_pressed()))
  770. watchdog_refresh();
  771. // Reboot the board
  772. hal.reboot();
  773. #else
  774. for (;;) watchdog_refresh(); // Wait for RESET button or power-cycle
  775. #endif
  776. }
  777. /**
  778. * Turn off heaters and stop the print in progress
  779. * After a stop the machine may be resumed with M999
  780. */
  781. void stop() {
  782. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  783. print_job_timer.stop();
  784. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  785. thermalManager.set_fans_paused(false); // Un-pause fans for safety
  786. #endif
  787. if (!IsStopped()) {
  788. SERIAL_ERROR_MSG(STR_ERR_STOPPED);
  789. LCD_MESSAGE(MSG_STOPPED);
  790. safe_delay(350); // allow enough time for messages to get out before stopping
  791. marlin_state = MF_STOPPED;
  792. }
  793. }
  794. inline void tmc_standby_setup() {
  795. #if PIN_EXISTS(X_STDBY)
  796. SET_INPUT_PULLDOWN(X_STDBY_PIN);
  797. #endif
  798. #if PIN_EXISTS(X2_STDBY)
  799. SET_INPUT_PULLDOWN(X2_STDBY_PIN);
  800. #endif
  801. #if PIN_EXISTS(Y_STDBY)
  802. SET_INPUT_PULLDOWN(Y_STDBY_PIN);
  803. #endif
  804. #if PIN_EXISTS(Y2_STDBY)
  805. SET_INPUT_PULLDOWN(Y2_STDBY_PIN);
  806. #endif
  807. #if PIN_EXISTS(Z_STDBY)
  808. SET_INPUT_PULLDOWN(Z_STDBY_PIN);
  809. #endif
  810. #if PIN_EXISTS(Z2_STDBY)
  811. SET_INPUT_PULLDOWN(Z2_STDBY_PIN);
  812. #endif
  813. #if PIN_EXISTS(Z3_STDBY)
  814. SET_INPUT_PULLDOWN(Z3_STDBY_PIN);
  815. #endif
  816. #if PIN_EXISTS(Z4_STDBY)
  817. SET_INPUT_PULLDOWN(Z4_STDBY_PIN);
  818. #endif
  819. #if PIN_EXISTS(I_STDBY)
  820. SET_INPUT_PULLDOWN(I_STDBY_PIN);
  821. #endif
  822. #if PIN_EXISTS(J_STDBY)
  823. SET_INPUT_PULLDOWN(J_STDBY_PIN);
  824. #endif
  825. #if PIN_EXISTS(K_STDBY)
  826. SET_INPUT_PULLDOWN(K_STDBY_PIN);
  827. #endif
  828. #if PIN_EXISTS(U_STDBY)
  829. SET_INPUT_PULLDOWN(U_STDBY_PIN);
  830. #endif
  831. #if PIN_EXISTS(V_STDBY)
  832. SET_INPUT_PULLDOWN(V_STDBY_PIN);
  833. #endif
  834. #if PIN_EXISTS(W_STDBY)
  835. SET_INPUT_PULLDOWN(W_STDBY_PIN);
  836. #endif
  837. #if PIN_EXISTS(E0_STDBY)
  838. SET_INPUT_PULLDOWN(E0_STDBY_PIN);
  839. #endif
  840. #if PIN_EXISTS(E1_STDBY)
  841. SET_INPUT_PULLDOWN(E1_STDBY_PIN);
  842. #endif
  843. #if PIN_EXISTS(E2_STDBY)
  844. SET_INPUT_PULLDOWN(E2_STDBY_PIN);
  845. #endif
  846. #if PIN_EXISTS(E3_STDBY)
  847. SET_INPUT_PULLDOWN(E3_STDBY_PIN);
  848. #endif
  849. #if PIN_EXISTS(E4_STDBY)
  850. SET_INPUT_PULLDOWN(E4_STDBY_PIN);
  851. #endif
  852. #if PIN_EXISTS(E5_STDBY)
  853. SET_INPUT_PULLDOWN(E5_STDBY_PIN);
  854. #endif
  855. #if PIN_EXISTS(E6_STDBY)
  856. SET_INPUT_PULLDOWN(E6_STDBY_PIN);
  857. #endif
  858. #if PIN_EXISTS(E7_STDBY)
  859. SET_INPUT_PULLDOWN(E7_STDBY_PIN);
  860. #endif
  861. }
  862. /**
  863. * Marlin Firmware entry-point. Abandon Hope All Ye Who Enter Here.
  864. * Setup before the program loop:
  865. *
  866. * - Call any special pre-init set for the board
  867. * - Put TMC drivers into Low Power Standby mode
  868. * - Init the serial ports (so setup can be debugged)
  869. * - Set up the kill and suicide pins
  870. * - Prepare (disable) board JTAG and Debug ports
  871. * - Init serial for a connected MKS TFT with WiFi
  872. * - Install Marlin custom Exception Handlers, if set.
  873. * - Init Marlin's HAL interfaces (for SPI, i2c, etc.)
  874. * - Init some optional hardware and features:
  875. * • MAX Thermocouple pins
  876. * • Duet Smart Effector
  877. * • Filament Runout Sensor
  878. * • TMC220x Stepper Drivers (Serial)
  879. * • PSU control
  880. * • Power-loss Recovery
  881. * • L64XX Stepper Drivers (SPI)
  882. * • Stepper Driver Reset: DISABLE
  883. * • TMC Stepper Drivers (SPI)
  884. * • Run hal.init_board() for additional pins setup
  885. * • ESP WiFi
  886. * - Get the Reset Reason and report it
  887. * - Print startup messages and diagnostics
  888. * - Calibrate the HAL DELAY for precise timing
  889. * - Init the buzzer, possibly a custom timer
  890. * - Init more optional hardware:
  891. * • Color LED illumination
  892. * • Neopixel illumination
  893. * • Controller Fan
  894. * • Creality DWIN LCD (show boot image)
  895. * • Tare the Probe if possible
  896. * - Mount the (most likely external) SD Card
  897. * - Load settings from EEPROM (or use defaults)
  898. * - Init the Ethernet Port
  899. * - Init Touch Buttons (for emulated DOGLCD)
  900. * - Adjust the (certainly wrong) current position by the home offset
  901. * - Init the Planner::position (steps) based on current (native) position
  902. * - Initialize more managers and peripherals:
  903. * • Temperatures
  904. * • Print Job Timer
  905. * • Endstops and Endstop Interrupts
  906. * • Stepper ISR - Kind of Important!
  907. * • Servos
  908. * • Servo-based Probe
  909. * • Photograph Pin
  910. * • Laser/Spindle tool Power / PWM
  911. * • Coolant Control
  912. * • Bed Probe
  913. * • Stepper Driver Reset: ENABLE
  914. * • Digipot I2C - Stepper driver current control
  915. * • Stepper DAC - Stepper driver current control
  916. * • Solenoid (probe, or for other use)
  917. * • Home Pin
  918. * • Custom User Buttons
  919. * • Red/Blue Status LEDs
  920. * • Case Light
  921. * • Prusa MMU filament changer
  922. * • Fan Multiplexer
  923. * • Mixing Extruder
  924. * • BLTouch Probe
  925. * • I2C Position Encoders
  926. * • Custom I2C Bus handlers
  927. * • Enhanced tools or extruders:
  928. * • Switching Extruder
  929. * • Switching Nozzle
  930. * • Parking Extruder
  931. * • Magnetic Parking Extruder
  932. * • Switching Toolhead
  933. * • Electromagnetic Switching Toolhead
  934. * • Watchdog Timer - Also Kind of Important!
  935. * • Closed Loop Controller
  936. * - Run Startup Commands, if defined
  937. * - Tell host to close Host Prompts
  938. * - Test Trinamic driver connections
  939. * - Init Prusa MMU2 filament changer
  940. * - Init and test BL24Cxx EEPROM
  941. * - Init Creality DWIN encoder, show faux progress bar
  942. * - Reset Status Message / Show Service Messages
  943. * - Init MAX7219 LED Matrix
  944. * - Init Direct Stepping (Klipper-style motion control)
  945. * - Init TFT LVGL UI (with 3D Graphics)
  946. * - Apply Password Lock - Hold for Authentication
  947. * - Open Touch Screen Calibration screen, if not calibrated
  948. * - Set Marlin to RUNNING State
  949. */
  950. void setup() {
  951. #ifdef FASTIO_INIT
  952. FASTIO_INIT();
  953. #endif
  954. #ifdef BOARD_PREINIT
  955. BOARD_PREINIT(); // Low-level init (before serial init)
  956. #endif
  957. tmc_standby_setup(); // TMC Low Power Standby pins must be set early or they're not usable
  958. // Check startup - does nothing if bootloader sets MCUSR to 0
  959. const byte mcu = hal.get_reset_source();
  960. hal.clear_reset_source();
  961. #if ENABLED(MARLIN_DEV_MODE)
  962. auto log_current_ms = [&](PGM_P const msg) {
  963. SERIAL_ECHO_START();
  964. SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHOPGM("] ");
  965. SERIAL_ECHOLNPGM_P(msg);
  966. };
  967. #define SETUP_LOG(M) log_current_ms(PSTR(M))
  968. #else
  969. #define SETUP_LOG(...) NOOP
  970. #endif
  971. #define SETUP_RUN(C) do{ SETUP_LOG(STRINGIFY(C)); C; }while(0)
  972. MYSERIAL1.begin(BAUDRATE);
  973. millis_t serial_connect_timeout = millis() + 1000UL;
  974. while (!MYSERIAL1.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  975. #if HAS_MULTI_SERIAL && !HAS_ETHERNET
  976. #ifndef BAUDRATE_2
  977. #define BAUDRATE_2 BAUDRATE
  978. #endif
  979. MYSERIAL2.begin(BAUDRATE_2);
  980. serial_connect_timeout = millis() + 1000UL;
  981. while (!MYSERIAL2.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  982. #ifdef SERIAL_PORT_3
  983. #ifndef BAUDRATE_3
  984. #define BAUDRATE_3 BAUDRATE
  985. #endif
  986. MYSERIAL3.begin(BAUDRATE_3);
  987. serial_connect_timeout = millis() + 1000UL;
  988. while (!MYSERIAL3.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  989. #endif
  990. #endif
  991. SERIAL_ECHOLNPGM("start");
  992. // Set up these pins early to prevent suicide
  993. #if HAS_KILL
  994. SETUP_LOG("KILL_PIN");
  995. #if KILL_PIN_STATE
  996. SET_INPUT_PULLDOWN(KILL_PIN);
  997. #else
  998. SET_INPUT_PULLUP(KILL_PIN);
  999. #endif
  1000. #endif
  1001. #if ENABLED(FREEZE_FEATURE)
  1002. SETUP_LOG("FREEZE_PIN");
  1003. #if FREEZE_STATE
  1004. SET_INPUT_PULLDOWN(FREEZE_PIN);
  1005. #else
  1006. SET_INPUT_PULLUP(FREEZE_PIN);
  1007. #endif
  1008. #endif
  1009. #if HAS_SUICIDE
  1010. SETUP_LOG("SUICIDE_PIN");
  1011. OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_STATE);
  1012. #endif
  1013. #ifdef JTAGSWD_RESET
  1014. SETUP_LOG("JTAGSWD_RESET");
  1015. JTAGSWD_RESET();
  1016. #endif
  1017. // Disable any hardware debug to free up pins for IO
  1018. #if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
  1019. delay(10);
  1020. SETUP_LOG("JTAGSWD_DISABLE");
  1021. JTAGSWD_DISABLE();
  1022. #elif ENABLED(DISABLE_JTAG) && defined(JTAG_DISABLE)
  1023. delay(10);
  1024. SETUP_LOG("JTAG_DISABLE");
  1025. JTAG_DISABLE();
  1026. #endif
  1027. TERN_(DYNAMIC_VECTORTABLE, hook_cpu_exceptions()); // If supported, install Marlin exception handlers at runtime
  1028. SETUP_RUN(hal.init());
  1029. // Init and disable SPI thermocouples; this is still needed
  1030. #if TEMP_SENSOR_0_IS_MAX_TC || (TEMP_SENSOR_REDUNDANT_IS_MAX_TC && REDUNDANT_TEMP_MATCH(SOURCE, E0))
  1031. OUT_WRITE(TEMP_0_CS_PIN, HIGH); // Disable
  1032. #endif
  1033. #if TEMP_SENSOR_1_IS_MAX_TC || (TEMP_SENSOR_REDUNDANT_IS_MAX_TC && REDUNDANT_TEMP_MATCH(SOURCE, E1))
  1034. OUT_WRITE(TEMP_1_CS_PIN, HIGH);
  1035. #endif
  1036. #if ENABLED(DUET_SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  1037. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  1038. #endif
  1039. #if HAS_FILAMENT_SENSOR
  1040. SETUP_RUN(runout.setup());
  1041. #endif
  1042. #if HAS_TMC220x
  1043. SETUP_RUN(tmc_serial_begin());
  1044. #endif
  1045. #if ENABLED(PSU_CONTROL)
  1046. SETUP_LOG("PSU_CONTROL");
  1047. powerManager.init();
  1048. #endif
  1049. #if ENABLED(POWER_LOSS_RECOVERY)
  1050. SETUP_RUN(recovery.setup());
  1051. #endif
  1052. #if HAS_L64XX
  1053. SETUP_RUN(L64xxManager.init()); // Set up SPI, init drivers
  1054. #endif
  1055. #if HAS_STEPPER_RESET
  1056. SETUP_RUN(disableStepperDrivers());
  1057. #endif
  1058. #if HAS_TMC_SPI
  1059. #if DISABLED(TMC_USE_SW_SPI)
  1060. SETUP_RUN(SPI.begin());
  1061. #endif
  1062. SETUP_RUN(tmc_init_cs_pins());
  1063. #endif
  1064. SETUP_RUN(hal.init_board());
  1065. SETUP_RUN(esp_wifi_init());
  1066. // Report Reset Reason
  1067. if (mcu & RST_POWER_ON) SERIAL_ECHOLNPGM(STR_POWERUP);
  1068. if (mcu & RST_EXTERNAL) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  1069. if (mcu & RST_BROWN_OUT) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  1070. if (mcu & RST_WATCHDOG) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  1071. if (mcu & RST_SOFTWARE) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  1072. // Identify myself as Marlin x.x.x
  1073. SERIAL_ECHOLNPGM("Marlin " SHORT_BUILD_VERSION);
  1074. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  1075. SERIAL_ECHO_MSG(
  1076. " Last Updated: " STRING_DISTRIBUTION_DATE
  1077. " | Author: " STRING_CONFIG_H_AUTHOR
  1078. );
  1079. #endif
  1080. SERIAL_ECHO_MSG(" Compiled: " __DATE__);
  1081. SERIAL_ECHO_MSG(STR_FREE_MEMORY, hal.freeMemory(), STR_PLANNER_BUFFER_BYTES, sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  1082. // Some HAL need precise delay adjustment
  1083. calibrate_delay_loop();
  1084. // Init buzzer pin(s)
  1085. #if USE_BEEPER
  1086. SETUP_RUN(buzzer.init());
  1087. #endif
  1088. // Set up LEDs early
  1089. #if HAS_COLOR_LEDS
  1090. SETUP_RUN(leds.setup());
  1091. #endif
  1092. #if ENABLED(NEOPIXEL2_SEPARATE)
  1093. SETUP_RUN(leds2.setup());
  1094. #endif
  1095. #if ENABLED(USE_CONTROLLER_FAN) // Set up fan controller to initialize also the default configurations.
  1096. SETUP_RUN(controllerFan.setup());
  1097. #endif
  1098. TERN_(HAS_FANCHECK, fan_check.init());
  1099. // UI must be initialized before EEPROM
  1100. // (because EEPROM code calls the UI).
  1101. SETUP_RUN(ui.init());
  1102. #if PIN_EXISTS(SAFE_POWER)
  1103. #if HAS_DRIVER_SAFE_POWER_PROTECT
  1104. SETUP_RUN(stepper_driver_backward_check());
  1105. #else
  1106. SETUP_LOG("SAFE_POWER");
  1107. OUT_WRITE(SAFE_POWER_PIN, HIGH);
  1108. #endif
  1109. #endif
  1110. #if BOTH(SDSUPPORT, SDCARD_EEPROM_EMULATION)
  1111. SETUP_RUN(card.mount()); // Mount media with settings before first_load
  1112. #endif
  1113. SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
  1114. // This also updates variables in the planner, elsewhere
  1115. #if BOTH(HAS_WIRED_LCD, SHOW_BOOTSCREEN)
  1116. SETUP_RUN(ui.show_bootscreen());
  1117. const millis_t bootscreen_ms = millis();
  1118. #endif
  1119. #if ENABLED(PROBE_TARE)
  1120. SETUP_RUN(probe.tare_init());
  1121. #endif
  1122. #if HAS_ETHERNET
  1123. SETUP_RUN(ethernet.init());
  1124. #endif
  1125. #if HAS_TOUCH_BUTTONS
  1126. SETUP_RUN(touchBt.init());
  1127. #endif
  1128. TERN_(HAS_M206_COMMAND, current_position += home_offset); // Init current position based on home_offset
  1129. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  1130. SETUP_RUN(thermalManager.init()); // Initialize temperature loop
  1131. SETUP_RUN(print_job_timer.init()); // Initial setup of print job timer
  1132. SETUP_RUN(endstops.init()); // Init endstops and pullups
  1133. #if ENABLED(DELTA) && !HAS_SOFTWARE_ENDSTOPS
  1134. SETUP_RUN(refresh_delta_clip_start_height()); // Init safe delta height without soft endstops
  1135. #endif
  1136. SETUP_RUN(stepper.init()); // Init stepper. This enables interrupts!
  1137. #if HAS_SERVOS
  1138. SETUP_RUN(servo_init());
  1139. #endif
  1140. #if HAS_Z_SERVO_PROBE
  1141. SETUP_RUN(probe.servo_probe_init());
  1142. #endif
  1143. #if HAS_PHOTOGRAPH
  1144. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  1145. #endif
  1146. #if HAS_CUTTER
  1147. SETUP_RUN(cutter.init());
  1148. #endif
  1149. #if ENABLED(COOLANT_MIST)
  1150. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  1151. #endif
  1152. #if ENABLED(COOLANT_FLOOD)
  1153. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  1154. #endif
  1155. #if HAS_BED_PROBE
  1156. #if PIN_EXISTS(PROBE_ENABLE)
  1157. OUT_WRITE(PROBE_ENABLE_PIN, LOW); // Disable
  1158. #endif
  1159. SETUP_RUN(endstops.enable_z_probe(false));
  1160. #endif
  1161. #if HAS_STEPPER_RESET
  1162. SETUP_RUN(enableStepperDrivers());
  1163. #endif
  1164. #if HAS_MOTOR_CURRENT_I2C
  1165. SETUP_RUN(digipot_i2c.init());
  1166. #endif
  1167. #if HAS_MOTOR_CURRENT_DAC
  1168. SETUP_RUN(stepper_dac.init());
  1169. #endif
  1170. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  1171. OUT_WRITE(SOL1_PIN, LOW); // OFF
  1172. #endif
  1173. #if HAS_HOME
  1174. SET_INPUT_PULLUP(HOME_PIN);
  1175. #endif
  1176. #if ENABLED(CUSTOM_USER_BUTTONS)
  1177. #define INIT_CUSTOM_USER_BUTTON_PIN(N) do{ SET_INPUT(BUTTON##N##_PIN); WRITE(BUTTON##N##_PIN, !BUTTON##N##_HIT_STATE); }while(0)
  1178. #if HAS_CUSTOM_USER_BUTTON(1)
  1179. INIT_CUSTOM_USER_BUTTON_PIN(1);
  1180. #endif
  1181. #if HAS_CUSTOM_USER_BUTTON(2)
  1182. INIT_CUSTOM_USER_BUTTON_PIN(2);
  1183. #endif
  1184. #if HAS_CUSTOM_USER_BUTTON(3)
  1185. INIT_CUSTOM_USER_BUTTON_PIN(3);
  1186. #endif
  1187. #if HAS_CUSTOM_USER_BUTTON(4)
  1188. INIT_CUSTOM_USER_BUTTON_PIN(4);
  1189. #endif
  1190. #if HAS_CUSTOM_USER_BUTTON(5)
  1191. INIT_CUSTOM_USER_BUTTON_PIN(5);
  1192. #endif
  1193. #if HAS_CUSTOM_USER_BUTTON(6)
  1194. INIT_CUSTOM_USER_BUTTON_PIN(6);
  1195. #endif
  1196. #if HAS_CUSTOM_USER_BUTTON(7)
  1197. INIT_CUSTOM_USER_BUTTON_PIN(7);
  1198. #endif
  1199. #if HAS_CUSTOM_USER_BUTTON(8)
  1200. INIT_CUSTOM_USER_BUTTON_PIN(8);
  1201. #endif
  1202. #if HAS_CUSTOM_USER_BUTTON(9)
  1203. INIT_CUSTOM_USER_BUTTON_PIN(9);
  1204. #endif
  1205. #if HAS_CUSTOM_USER_BUTTON(10)
  1206. INIT_CUSTOM_USER_BUTTON_PIN(10);
  1207. #endif
  1208. #if HAS_CUSTOM_USER_BUTTON(11)
  1209. INIT_CUSTOM_USER_BUTTON_PIN(11);
  1210. #endif
  1211. #if HAS_CUSTOM_USER_BUTTON(12)
  1212. INIT_CUSTOM_USER_BUTTON_PIN(12);
  1213. #endif
  1214. #if HAS_CUSTOM_USER_BUTTON(13)
  1215. INIT_CUSTOM_USER_BUTTON_PIN(13);
  1216. #endif
  1217. #if HAS_CUSTOM_USER_BUTTON(14)
  1218. INIT_CUSTOM_USER_BUTTON_PIN(14);
  1219. #endif
  1220. #if HAS_CUSTOM_USER_BUTTON(15)
  1221. INIT_CUSTOM_USER_BUTTON_PIN(15);
  1222. #endif
  1223. #if HAS_CUSTOM_USER_BUTTON(16)
  1224. INIT_CUSTOM_USER_BUTTON_PIN(16);
  1225. #endif
  1226. #if HAS_CUSTOM_USER_BUTTON(17)
  1227. INIT_CUSTOM_USER_BUTTON_PIN(17);
  1228. #endif
  1229. #if HAS_CUSTOM_USER_BUTTON(18)
  1230. INIT_CUSTOM_USER_BUTTON_PIN(18);
  1231. #endif
  1232. #if HAS_CUSTOM_USER_BUTTON(19)
  1233. INIT_CUSTOM_USER_BUTTON_PIN(19);
  1234. #endif
  1235. #if HAS_CUSTOM_USER_BUTTON(20)
  1236. INIT_CUSTOM_USER_BUTTON_PIN(20);
  1237. #endif
  1238. #if HAS_CUSTOM_USER_BUTTON(21)
  1239. INIT_CUSTOM_USER_BUTTON_PIN(21);
  1240. #endif
  1241. #if HAS_CUSTOM_USER_BUTTON(22)
  1242. INIT_CUSTOM_USER_BUTTON_PIN(22);
  1243. #endif
  1244. #if HAS_CUSTOM_USER_BUTTON(23)
  1245. INIT_CUSTOM_USER_BUTTON_PIN(23);
  1246. #endif
  1247. #if HAS_CUSTOM_USER_BUTTON(24)
  1248. INIT_CUSTOM_USER_BUTTON_PIN(24);
  1249. #endif
  1250. #if HAS_CUSTOM_USER_BUTTON(25)
  1251. INIT_CUSTOM_USER_BUTTON_PIN(25);
  1252. #endif
  1253. #endif
  1254. #if PIN_EXISTS(STAT_LED_RED)
  1255. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  1256. #endif
  1257. #if PIN_EXISTS(STAT_LED_BLUE)
  1258. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  1259. #endif
  1260. #if ENABLED(CASE_LIGHT_ENABLE)
  1261. SETUP_RUN(caselight.init());
  1262. #endif
  1263. #if HAS_PRUSA_MMU1
  1264. SETUP_RUN(mmu_init());
  1265. #endif
  1266. #if HAS_FANMUX
  1267. SETUP_RUN(fanmux_init());
  1268. #endif
  1269. #if ENABLED(MIXING_EXTRUDER)
  1270. SETUP_RUN(mixer.init());
  1271. #endif
  1272. #if ENABLED(BLTOUCH)
  1273. SETUP_RUN(bltouch.init(/*set_voltage=*/true));
  1274. #endif
  1275. #if ENABLED(MAGLEV4)
  1276. OUT_WRITE(MAGLEV_TRIGGER_PIN, LOW);
  1277. #endif
  1278. #if ENABLED(I2C_POSITION_ENCODERS)
  1279. SETUP_RUN(I2CPEM.init());
  1280. #endif
  1281. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  1282. SETUP_LOG("i2c...");
  1283. i2c.onReceive(i2c_on_receive);
  1284. i2c.onRequest(i2c_on_request);
  1285. #endif
  1286. #if DO_SWITCH_EXTRUDER
  1287. SETUP_RUN(move_extruder_servo(0)); // Initialize extruder servo
  1288. #endif
  1289. #if ENABLED(SWITCHING_NOZZLE)
  1290. SETUP_LOG("SWITCHING_NOZZLE");
  1291. // Initialize nozzle servo(s)
  1292. #if SWITCHING_NOZZLE_TWO_SERVOS
  1293. lower_nozzle(0);
  1294. raise_nozzle(1);
  1295. #else
  1296. move_nozzle_servo(0);
  1297. #endif
  1298. #endif
  1299. #if ENABLED(PARKING_EXTRUDER)
  1300. SETUP_RUN(pe_solenoid_init());
  1301. #elif ENABLED(MAGNETIC_PARKING_EXTRUDER)
  1302. SETUP_RUN(mpe_settings_init());
  1303. #elif ENABLED(SWITCHING_TOOLHEAD)
  1304. SETUP_RUN(swt_init());
  1305. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  1306. SETUP_RUN(est_init());
  1307. #endif
  1308. #if ENABLED(USE_WATCHDOG)
  1309. SETUP_RUN(watchdog_init()); // Reinit watchdog after hal.get_reset_source call
  1310. #endif
  1311. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  1312. SETUP_RUN(closedloop.init());
  1313. #endif
  1314. #ifdef STARTUP_COMMANDS
  1315. SETUP_LOG("STARTUP_COMMANDS");
  1316. queue.inject(F(STARTUP_COMMANDS));
  1317. #endif
  1318. #if ENABLED(HOST_PROMPT_SUPPORT)
  1319. SETUP_RUN(hostui.prompt_end());
  1320. #endif
  1321. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  1322. SETUP_RUN(test_tmc_connection());
  1323. #endif
  1324. #if HAS_DRIVER_SAFE_POWER_PROTECT
  1325. SETUP_RUN(stepper_driver_backward_report());
  1326. #endif
  1327. #if HAS_PRUSA_MMU2
  1328. SETUP_RUN(mmu2.init());
  1329. #endif
  1330. #if ENABLED(IIC_BL24CXX_EEPROM)
  1331. BL24CXX::init();
  1332. const uint8_t err = BL24CXX::check();
  1333. SERIAL_ECHO_TERNARY(err, "BL24CXX Check ", "failed", "succeeded", "!\n");
  1334. #endif
  1335. #if HAS_DWIN_E3V2_BASIC
  1336. SETUP_RUN(DWIN_InitScreen());
  1337. #endif
  1338. #if HAS_SERVICE_INTERVALS && !HAS_DWIN_E3V2_BASIC
  1339. SETUP_RUN(ui.reset_status(true)); // Show service messages or keep current status
  1340. #endif
  1341. #if ENABLED(MAX7219_DEBUG)
  1342. SETUP_RUN(max7219.init());
  1343. #endif
  1344. #if ENABLED(DIRECT_STEPPING)
  1345. SETUP_RUN(page_manager.init());
  1346. #endif
  1347. #if HAS_TFT_LVGL_UI
  1348. #if ENABLED(SDSUPPORT)
  1349. if (!card.isMounted()) SETUP_RUN(card.mount()); // Mount SD to load graphics and fonts
  1350. #endif
  1351. SETUP_RUN(tft_lvgl_init());
  1352. #endif
  1353. #if BOTH(HAS_WIRED_LCD, SHOW_BOOTSCREEN)
  1354. const millis_t elapsed = millis() - bootscreen_ms;
  1355. #if ENABLED(MARLIN_DEV_MODE)
  1356. SERIAL_ECHOLNPGM("elapsed=", elapsed);
  1357. #endif
  1358. SETUP_RUN(ui.bootscreen_completion(elapsed));
  1359. #endif
  1360. #if ENABLED(PASSWORD_ON_STARTUP)
  1361. SETUP_RUN(password.lock_machine()); // Will not proceed until correct password provided
  1362. #endif
  1363. #if BOTH(HAS_MARLINUI_MENU, TOUCH_SCREEN_CALIBRATION) && EITHER(TFT_CLASSIC_UI, TFT_COLOR_UI)
  1364. SETUP_RUN(ui.check_touch_calibration());
  1365. #endif
  1366. #if ENABLED(EASYTHREED_UI)
  1367. SETUP_RUN(easythreed_ui.init());
  1368. #endif
  1369. marlin_state = MF_RUNNING;
  1370. SETUP_LOG("setup() completed.");
  1371. }
  1372. /**
  1373. * The main Marlin program loop
  1374. *
  1375. * - Call idle() to handle all tasks between G-code commands
  1376. * Note that no G-codes from the queue can be executed during idle()
  1377. * but many G-codes can be called directly anytime like macros.
  1378. * - Check whether SD card auto-start is needed now.
  1379. * - Check whether SD print finishing is needed now.
  1380. * - Run one G-code command from the immediate or main command queue
  1381. * and open up one space. Commands in the main queue may come from sd
  1382. * card, host, or by direct injection. The queue will continue to fill
  1383. * as long as idle() or manage_inactivity() are being called.
  1384. */
  1385. void loop() {
  1386. do {
  1387. idle();
  1388. #if ENABLED(SDSUPPORT)
  1389. if (card.flag.abort_sd_printing) abortSDPrinting();
  1390. if (marlin_state == MF_SD_COMPLETE) finishSDPrinting();
  1391. #endif
  1392. queue.advance();
  1393. #if EITHER(POWER_OFF_TIMER, POWER_OFF_WAIT_FOR_COOLDOWN)
  1394. powerManager.checkAutoPowerOff();
  1395. #endif
  1396. endstops.event_handler();
  1397. TERN_(HAS_TFT_LVGL_UI, printer_state_polling());
  1398. } while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
  1399. }