My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MarlinCore.cpp 44KB

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