My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MarlinCore.cpp 47KB

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