My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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