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

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