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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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 printing
  262. */
  263. bool printJobOngoing() {
  264. return print_job_timer.isRunning() || IS_SD_PRINTING();
  265. }
  266. /**
  267. * Printing is active when the print job timer is running
  268. */
  269. bool printingIsActive() {
  270. return !did_pause_print && (print_job_timer.isRunning() || IS_SD_PRINTING());
  271. }
  272. /**
  273. * Printing is paused according to SD or host indicators
  274. */
  275. bool printingIsPaused() {
  276. return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
  277. }
  278. void startOrResumeJob() {
  279. if (!printingIsPaused()) {
  280. TERN_(GCODE_REPEAT_MARKERS, repeat.reset());
  281. TERN_(CANCEL_OBJECTS, cancelable.reset());
  282. TERN_(LCD_SHOW_E_TOTAL, e_move_accumulator = 0);
  283. #if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
  284. ui.reset_remaining_time();
  285. #endif
  286. }
  287. print_job_timer.start();
  288. }
  289. #if ENABLED(SDSUPPORT)
  290. inline void abortSDPrinting() {
  291. IF_DISABLED(NO_SD_AUTOSTART, card.autofile_cancel());
  292. card.endFilePrint(TERN_(SD_RESORT, true));
  293. queue.clear();
  294. quickstop_stepper();
  295. print_job_timer.abort();
  296. IF_DISABLED(SD_ABORT_NO_COOLDOWN, thermalManager.disable_all_heaters());
  297. TERN(HAS_CUTTER, cutter.kill(), thermalManager.zero_fan_speeds()); // Full cutter shutdown including ISR control
  298. wait_for_heatup = false;
  299. TERN_(POWER_LOSS_RECOVERY, recovery.purge());
  300. #ifdef EVENT_GCODE_SD_ABORT
  301. queue.inject_P(PSTR(EVENT_GCODE_SD_ABORT));
  302. #endif
  303. TERN_(PASSWORD_AFTER_SD_PRINT_ABORT, password.lock_machine());
  304. }
  305. inline void finishSDPrinting() {
  306. if (queue.enqueue_one_P(PSTR("M1001"))) {
  307. marlin_state = MF_RUNNING;
  308. TERN_(PASSWORD_AFTER_SD_PRINT_END, password.lock_machine());
  309. TERN_(DGUS_LCD_UI_MKS, ScreenHandler.SDPrintingFinished());
  310. }
  311. }
  312. #endif // SDSUPPORT
  313. /**
  314. * Minimal management of Marlin's core activities:
  315. * - Keep the command buffer full
  316. * - Check for maximum inactive time between commands
  317. * - Check for maximum inactive time between stepper commands
  318. * - Check if CHDK_PIN needs to go LOW
  319. * - Check for KILL button held down
  320. * - Check for HOME button held down
  321. * - Check for CUSTOM USER button held down
  322. * - Check if cooling fan needs to be switched on
  323. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  324. * - Pulse FET_SAFETY_PIN if it exists
  325. */
  326. inline void manage_inactivity(const bool ignore_stepper_queue=false) {
  327. queue.get_available_commands();
  328. const millis_t ms = millis();
  329. // Prevent steppers timing-out in the middle of M600
  330. // unless PAUSE_PARK_NO_STEPPER_TIMEOUT is disabled
  331. const bool parked_or_ignoring = ignore_stepper_queue
  332. || TERN0(PAUSE_PARK_NO_STEPPER_TIMEOUT, did_pause_print);
  333. // Reset both the M18/M84 activity timeout and the M85 max 'kill' timeout
  334. if (parked_or_ignoring) gcode.reset_stepper_timeout(ms);
  335. if (gcode.stepper_max_timed_out(ms)) {
  336. SERIAL_ERROR_MSG(STR_KILL_INACTIVE_TIME, parser.command_ptr);
  337. kill();
  338. }
  339. // M18 / M84 : Handle steppers inactive time timeout
  340. if (gcode.stepper_inactive_time) {
  341. static bool already_shutdown_steppers; // = false
  342. // Any moves in the planner? Resets both the M18/M84
  343. // activity timeout and the M85 max 'kill' timeout
  344. if (planner.has_blocks_queued())
  345. gcode.reset_stepper_timeout(ms);
  346. else if (!parked_or_ignoring && gcode.stepper_inactive_timeout()) {
  347. if (!already_shutdown_steppers) {
  348. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  349. // Individual axes will be disabled if configured
  350. if (ENABLED(DISABLE_INACTIVE_X)) DISABLE_AXIS_X();
  351. if (ENABLED(DISABLE_INACTIVE_Y)) DISABLE_AXIS_Y();
  352. if (ENABLED(DISABLE_INACTIVE_Z)) DISABLE_AXIS_Z();
  353. if (ENABLED(DISABLE_INACTIVE_E)) disable_e_steppers();
  354. TERN_(AUTO_BED_LEVELING_UBL, ubl.steppers_were_disabled());
  355. }
  356. }
  357. else
  358. already_shutdown_steppers = false;
  359. }
  360. #if ENABLED(PHOTO_GCODE) && PIN_EXISTS(CHDK)
  361. // Check if CHDK should be set to LOW (after M240 set it HIGH)
  362. extern millis_t chdk_timeout;
  363. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  364. chdk_timeout = 0;
  365. WRITE(CHDK_PIN, LOW);
  366. }
  367. #endif
  368. #if HAS_KILL
  369. // Check if the kill button was pressed and wait just in case it was an accidental
  370. // key kill key press
  371. // -------------------------------------------------------------------------------
  372. static int killCount = 0; // make the inactivity button a bit less responsive
  373. const int KILL_DELAY = 750;
  374. if (kill_state())
  375. killCount++;
  376. else if (killCount > 0)
  377. killCount--;
  378. // Exceeded threshold and we can confirm that it was not accidental
  379. // KILL the machine
  380. // ----------------------------------------------------------------
  381. if (killCount >= KILL_DELAY) {
  382. SERIAL_ERROR_MSG(STR_KILL_BUTTON);
  383. kill();
  384. }
  385. #endif
  386. #if HAS_HOME
  387. // Handle a standalone HOME button
  388. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  389. static millis_t next_home_key_ms; // = 0
  390. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  391. const millis_t ms = millis();
  392. if (ELAPSED(ms, next_home_key_ms)) {
  393. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  394. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  395. queue.inject_P(G28_STR);
  396. }
  397. }
  398. #endif
  399. #if ENABLED(CUSTOM_USER_BUTTONS)
  400. // Handle a custom user button if defined
  401. const bool printer_not_busy = !printingIsActive();
  402. #define HAS_CUSTOM_USER_BUTTON(N) (PIN_EXISTS(BUTTON##N) && defined(BUTTON##N##_HIT_STATE) && defined(BUTTON##N##_GCODE) && defined(BUTTON##N##_DESC))
  403. #define CHECK_CUSTOM_USER_BUTTON(N) do{ \
  404. constexpr millis_t CUB_DEBOUNCE_DELAY_##N = 250UL; \
  405. static millis_t next_cub_ms_##N; \
  406. if (BUTTON##N##_HIT_STATE == READ(BUTTON##N##_PIN) \
  407. && (ENABLED(BUTTON##N##_WHEN_PRINTING) || printer_not_busy)) { \
  408. const millis_t ms = millis(); \
  409. if (ELAPSED(ms, next_cub_ms_##N)) { \
  410. next_cub_ms_##N = ms + CUB_DEBOUNCE_DELAY_##N; \
  411. if (strlen(BUTTON##N##_DESC)) \
  412. LCD_MESSAGEPGM_P(PSTR(BUTTON##N##_DESC)); \
  413. queue.inject_P(PSTR(BUTTON##N##_GCODE)); \
  414. } \
  415. } \
  416. }while(0)
  417. #if HAS_CUSTOM_USER_BUTTON(1)
  418. CHECK_CUSTOM_USER_BUTTON(1);
  419. #endif
  420. #if HAS_CUSTOM_USER_BUTTON(2)
  421. CHECK_CUSTOM_USER_BUTTON(2);
  422. #endif
  423. #if HAS_CUSTOM_USER_BUTTON(3)
  424. CHECK_CUSTOM_USER_BUTTON(3);
  425. #endif
  426. #if HAS_CUSTOM_USER_BUTTON(4)
  427. CHECK_CUSTOM_USER_BUTTON(4);
  428. #endif
  429. #if HAS_CUSTOM_USER_BUTTON(5)
  430. CHECK_CUSTOM_USER_BUTTON(5);
  431. #endif
  432. #if HAS_CUSTOM_USER_BUTTON(6)
  433. CHECK_CUSTOM_USER_BUTTON(6);
  434. #endif
  435. #if HAS_CUSTOM_USER_BUTTON(7)
  436. CHECK_CUSTOM_USER_BUTTON(7);
  437. #endif
  438. #if HAS_CUSTOM_USER_BUTTON(8)
  439. CHECK_CUSTOM_USER_BUTTON(8);
  440. #endif
  441. #if HAS_CUSTOM_USER_BUTTON(9)
  442. CHECK_CUSTOM_USER_BUTTON(9);
  443. #endif
  444. #if HAS_CUSTOM_USER_BUTTON(10)
  445. CHECK_CUSTOM_USER_BUTTON(10);
  446. #endif
  447. #if HAS_CUSTOM_USER_BUTTON(11)
  448. CHECK_CUSTOM_USER_BUTTON(11);
  449. #endif
  450. #if HAS_CUSTOM_USER_BUTTON(12)
  451. CHECK_CUSTOM_USER_BUTTON(12);
  452. #endif
  453. #if HAS_CUSTOM_USER_BUTTON(13)
  454. CHECK_CUSTOM_USER_BUTTON(13);
  455. #endif
  456. #if HAS_CUSTOM_USER_BUTTON(14)
  457. CHECK_CUSTOM_USER_BUTTON(14);
  458. #endif
  459. #if HAS_CUSTOM_USER_BUTTON(15)
  460. CHECK_CUSTOM_USER_BUTTON(15);
  461. #endif
  462. #if HAS_CUSTOM_USER_BUTTON(16)
  463. CHECK_CUSTOM_USER_BUTTON(16);
  464. #endif
  465. #if HAS_CUSTOM_USER_BUTTON(17)
  466. CHECK_CUSTOM_USER_BUTTON(17);
  467. #endif
  468. #if HAS_CUSTOM_USER_BUTTON(18)
  469. CHECK_CUSTOM_USER_BUTTON(18);
  470. #endif
  471. #if HAS_CUSTOM_USER_BUTTON(19)
  472. CHECK_CUSTOM_USER_BUTTON(19);
  473. #endif
  474. #if HAS_CUSTOM_USER_BUTTON(20)
  475. CHECK_CUSTOM_USER_BUTTON(20);
  476. #endif
  477. #if HAS_CUSTOM_USER_BUTTON(21)
  478. CHECK_CUSTOM_USER_BUTTON(21);
  479. #endif
  480. #if HAS_CUSTOM_USER_BUTTON(22)
  481. CHECK_CUSTOM_USER_BUTTON(22);
  482. #endif
  483. #if HAS_CUSTOM_USER_BUTTON(23)
  484. CHECK_CUSTOM_USER_BUTTON(23);
  485. #endif
  486. #if HAS_CUSTOM_USER_BUTTON(24)
  487. CHECK_CUSTOM_USER_BUTTON(24);
  488. #endif
  489. #if HAS_CUSTOM_USER_BUTTON(25)
  490. CHECK_CUSTOM_USER_BUTTON(25);
  491. #endif
  492. #endif
  493. TERN_(USE_CONTROLLER_FAN, controllerFan.update()); // Check if fan should be turned on to cool stepper drivers down
  494. TERN_(AUTO_POWER_CONTROL, powerManager.check());
  495. TERN_(HOTEND_IDLE_TIMEOUT, hotend_idle.check());
  496. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  497. if (thermalManager.degHotend(active_extruder) > (EXTRUDER_RUNOUT_MINTEMP)
  498. && ELAPSED(ms, gcode.previous_move_ms + SEC_TO_MS(EXTRUDER_RUNOUT_SECONDS))
  499. && !planner.has_blocks_queued()
  500. ) {
  501. #if ENABLED(SWITCHING_EXTRUDER)
  502. bool oldstatus;
  503. switch (active_extruder) {
  504. default: oldstatus = E0_ENABLE_READ(); ENABLE_AXIS_E0(); break;
  505. #if E_STEPPERS > 1
  506. case 2: case 3: oldstatus = E1_ENABLE_READ(); ENABLE_AXIS_E1(); break;
  507. #if E_STEPPERS > 2
  508. case 4: case 5: oldstatus = E2_ENABLE_READ(); ENABLE_AXIS_E2(); break;
  509. #if E_STEPPERS > 3
  510. case 6: case 7: oldstatus = E3_ENABLE_READ(); ENABLE_AXIS_E3(); break;
  511. #endif // E_STEPPERS > 3
  512. #endif // E_STEPPERS > 2
  513. #endif // E_STEPPERS > 1
  514. }
  515. #else // !SWITCHING_EXTRUDER
  516. bool oldstatus;
  517. switch (active_extruder) {
  518. default:
  519. #define _CASE_EN(N) case N: oldstatus = E##N##_ENABLE_READ(); ENABLE_AXIS_E##N(); break;
  520. REPEAT(E_STEPPERS, _CASE_EN);
  521. }
  522. #endif
  523. const float olde = current_position.e;
  524. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  525. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  526. current_position.e = olde;
  527. planner.set_e_position_mm(olde);
  528. planner.synchronize();
  529. #if ENABLED(SWITCHING_EXTRUDER)
  530. switch (active_extruder) {
  531. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  532. #if E_STEPPERS > 1
  533. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  534. #if E_STEPPERS > 2
  535. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  536. #endif // E_STEPPERS > 2
  537. #endif // E_STEPPERS > 1
  538. }
  539. #else // !SWITCHING_EXTRUDER
  540. switch (active_extruder) {
  541. #define _CASE_RESTORE(N) case N: E##N##_ENABLE_WRITE(oldstatus); break;
  542. REPEAT(E_STEPPERS, _CASE_RESTORE);
  543. }
  544. #endif // !SWITCHING_EXTRUDER
  545. gcode.reset_stepper_timeout(ms);
  546. }
  547. #endif // EXTRUDER_RUNOUT_PREVENT
  548. #if ENABLED(DUAL_X_CARRIAGE)
  549. // handle delayed move timeout
  550. if (delayed_move_time && ELAPSED(ms, delayed_move_time) && IsRunning()) {
  551. // travel moves have been received so enact them
  552. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  553. destination = current_position;
  554. prepare_line_to_destination();
  555. planner.synchronize();
  556. }
  557. #endif
  558. TERN_(TEMP_STAT_LEDS, handle_status_leds());
  559. TERN_(MONITOR_DRIVER_STATUS, monitor_tmc_drivers());
  560. TERN_(MONITOR_L6470_DRIVER_STATUS, L64xxManager.monitor_driver());
  561. // Limit check_axes_activity frequency to 10Hz
  562. static millis_t next_check_axes_ms = 0;
  563. if (ELAPSED(ms, next_check_axes_ms)) {
  564. planner.check_axes_activity();
  565. next_check_axes_ms = ms + 100UL;
  566. }
  567. #if PIN_EXISTS(FET_SAFETY)
  568. static millis_t FET_next;
  569. if (ELAPSED(ms, FET_next)) {
  570. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  571. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  572. DELAY_US(2);
  573. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  574. }
  575. #endif
  576. }
  577. /**
  578. * Standard idle routine keeps the machine alive:
  579. * - Core Marlin activities
  580. * - Manage heaters (and Watchdog)
  581. * - Max7219 heartbeat, animation, etc.
  582. *
  583. * Only after setup() is complete:
  584. * - Handle filament runout sensors
  585. * - Run HAL idle tasks
  586. * - Handle Power-Loss Recovery
  587. * - Run StallGuard endstop checks
  588. * - Handle SD Card insert / remove
  589. * - Handle USB Flash Drive insert / remove
  590. * - Announce Host Keepalive state (if any)
  591. * - Update the Print Job Timer state
  592. * - Update the Beeper queue
  593. * - Read Buttons and Update the LCD
  594. * - Run i2c Position Encoders
  595. * - Auto-report Temperatures / SD Status
  596. * - Update the Průša MMU2
  597. * - Handle Joystick jogging
  598. */
  599. void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
  600. #if ENABLED(MARLIN_DEV_MODE)
  601. static uint16_t idle_depth = 0;
  602. if (++idle_depth > 5) SERIAL_ECHOLNPAIR("idle() call depth: ", idle_depth);
  603. #endif
  604. // Core Marlin activities
  605. manage_inactivity(TERN_(ADVANCED_PAUSE_FEATURE, no_stepper_sleep));
  606. // Manage Heaters (and Watchdog)
  607. thermalManager.manage_heater();
  608. // Max7219 heartbeat, animation, etc
  609. TERN_(MAX7219_DEBUG, max7219.idle_tasks());
  610. // Return if setup() isn't completed
  611. if (marlin_state == MF_INITIALIZING) goto IDLE_DONE;
  612. // TODO: Still causing errors
  613. (void)check_tool_sensor_stats(active_extruder, true);
  614. // Handle filament runout sensors
  615. TERN_(HAS_FILAMENT_SENSOR, runout.run());
  616. // Run HAL idle tasks
  617. TERN_(HAL_IDLETASK, HAL_idletask());
  618. // Check network connection
  619. TERN_(HAS_ETHERNET, ethernet.check());
  620. // Handle Power-Loss Recovery
  621. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  622. if (printJobOngoing()) recovery.outage();
  623. #endif
  624. // Run StallGuard endstop checks
  625. #if ENABLED(SPI_ENDSTOPS)
  626. if (endstops.tmc_spi_homing.any
  627. && TERN1(IMPROVE_HOMING_RELIABILITY, ELAPSED(millis(), sg_guard_period))
  628. ) LOOP_L_N(i, 4) // Read SGT 4 times per idle loop
  629. if (endstops.tmc_spi_homing_check()) break;
  630. #endif
  631. // Handle SD Card insert / remove
  632. TERN_(SDSUPPORT, card.manage_media());
  633. // Handle USB Flash Drive insert / remove
  634. TERN_(USB_FLASH_DRIVE_SUPPORT, card.diskIODriver()->idle());
  635. // Announce Host Keepalive state (if any)
  636. TERN_(HOST_KEEPALIVE_FEATURE, gcode.host_keepalive());
  637. // Update the Print Job Timer state
  638. TERN_(PRINTCOUNTER, print_job_timer.tick());
  639. // Update the Beeper queue
  640. TERN_(USE_BEEPER, buzzer.tick());
  641. // Handle UI input / draw events
  642. TERN(DWIN_CREALITY_LCD, DWIN_Update(), ui.update());
  643. // Run i2c Position Encoders
  644. #if ENABLED(I2C_POSITION_ENCODERS)
  645. {
  646. static millis_t i2cpem_next_update_ms;
  647. if (planner.has_blocks_queued()) {
  648. const millis_t ms = millis();
  649. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  650. I2CPEM.update();
  651. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  652. }
  653. }
  654. }
  655. #endif
  656. // Auto-report Temperatures / SD Status
  657. #if HAS_AUTO_REPORTING
  658. if (!gcode.autoreport_paused) {
  659. TERN_(AUTO_REPORT_TEMPERATURES, thermalManager.auto_reporter.tick());
  660. TERN_(AUTO_REPORT_SD_STATUS, card.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 (IsRunning()) {
  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. #endif
  902. SERIAL_ECHOLNPGM("start");
  903. // Set up these pins early to prevent suicide
  904. #if HAS_KILL
  905. SETUP_LOG("KILL_PIN");
  906. #if KILL_PIN_STATE
  907. SET_INPUT_PULLDOWN(KILL_PIN);
  908. #else
  909. SET_INPUT_PULLUP(KILL_PIN);
  910. #endif
  911. #endif
  912. #if HAS_SUICIDE
  913. SETUP_LOG("SUICIDE_PIN");
  914. OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
  915. #endif
  916. #if EITHER(DISABLE_DEBUG, DISABLE_JTAG)
  917. // Disable any hardware debug to free up pins for IO
  918. #if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
  919. JTAGSWD_DISABLE();
  920. #elif defined(JTAG_DISABLE)
  921. JTAG_DISABLE();
  922. #else
  923. #error "DISABLE_(DEBUG|JTAG) is not supported for the selected MCU/Board."
  924. #endif
  925. #endif
  926. #if BOTH(HAS_TFT_LVGL_UI, MKS_WIFI_MODULE)
  927. mks_esp_wifi_init();
  928. WIFISERIAL.begin(WIFI_BAUDRATE);
  929. serial_connect_timeout = millis() + 1000UL;
  930. while (/*!WIFISERIAL && */PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  931. #endif
  932. TERN_(DYNAMIC_VECTORTABLE, hook_cpu_exceptions()); // If supported, install Marlin exception handlers at runtime
  933. SETUP_RUN(HAL_init());
  934. // Init and disable SPI thermocouples; this is still needed
  935. #if TEMP_SENSOR_0_IS_MAX_TC
  936. OUT_WRITE(MAX6675_SS_PIN, HIGH); // Disable
  937. #endif
  938. #if TEMP_SENSOR_1_IS_MAX_TC
  939. OUT_WRITE(MAX6675_SS2_PIN, HIGH); // Disable
  940. #endif
  941. #if ENABLED(DUET_SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  942. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  943. #endif
  944. #if HAS_FILAMENT_SENSOR
  945. SETUP_RUN(runout.setup());
  946. #endif
  947. #if HAS_TMC220x
  948. SETUP_RUN(tmc_serial_begin());
  949. #endif
  950. #if ENABLED(PSU_CONTROL)
  951. SETUP_LOG("PSU_CONTROL");
  952. powersupply_on = ENABLED(PSU_DEFAULT_OFF);
  953. if (ENABLED(PSU_DEFAULT_OFF)) PSU_OFF(); else PSU_ON();
  954. #endif
  955. #if ENABLED(POWER_LOSS_RECOVERY)
  956. SETUP_RUN(recovery.setup());
  957. #endif
  958. #if HAS_L64XX
  959. SETUP_RUN(L64xxManager.init()); // Set up SPI, init drivers
  960. #endif
  961. #if HAS_STEPPER_RESET
  962. SETUP_RUN(disableStepperDrivers());
  963. #endif
  964. #if HAS_TMC_SPI
  965. #if DISABLED(TMC_USE_SW_SPI)
  966. SETUP_RUN(SPI.begin());
  967. #endif
  968. SETUP_RUN(tmc_init_cs_pins());
  969. #endif
  970. #ifdef BOARD_INIT
  971. SETUP_LOG("BOARD_INIT");
  972. BOARD_INIT();
  973. #endif
  974. SETUP_RUN(esp_wifi_init());
  975. // Check startup - does nothing if bootloader sets MCUSR to 0
  976. const byte mcu = HAL_get_reset_source();
  977. if (mcu & RST_POWER_ON) SERIAL_ECHOLNPGM(STR_POWERUP);
  978. if (mcu & RST_EXTERNAL) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  979. if (mcu & RST_BROWN_OUT) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  980. if (mcu & RST_WATCHDOG) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  981. if (mcu & RST_SOFTWARE) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  982. HAL_clear_reset_source();
  983. SERIAL_ECHOPGM_P(GET_TEXT(MSG_MARLIN));
  984. SERIAL_CHAR(' ');
  985. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  986. SERIAL_EOL();
  987. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  988. SERIAL_ECHO_MSG(
  989. " Last Updated: " STRING_DISTRIBUTION_DATE
  990. " | Author: " STRING_CONFIG_H_AUTHOR
  991. );
  992. #endif
  993. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  994. SERIAL_ECHO_MSG(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  995. // Some HAL need precise delay adjustment
  996. calibrate_delay_loop();
  997. // Init buzzer pin(s)
  998. #if USE_BEEPER
  999. SETUP_RUN(buzzer.init());
  1000. #endif
  1001. // Set up LEDs early
  1002. #if HAS_COLOR_LEDS
  1003. SETUP_RUN(leds.setup());
  1004. #endif
  1005. #if ENABLED(NEOPIXEL2_SEPARATE)
  1006. SETUP_RUN(leds2.setup());
  1007. #endif
  1008. #if ENABLED(USE_CONTROLLER_FAN) // Set up fan controller to initialize also the default configurations.
  1009. SETUP_RUN(controllerFan.setup());
  1010. #endif
  1011. // UI must be initialized before EEPROM
  1012. // (because EEPROM code calls the UI).
  1013. #if ENABLED(DWIN_CREALITY_LCD)
  1014. delay(800); // Required delay (since boot?)
  1015. SERIAL_ECHOPGM("\nDWIN handshake ");
  1016. if (DWIN_Handshake()) SERIAL_ECHOLNPGM("ok."); else SERIAL_ECHOLNPGM("error.");
  1017. DWIN_Frame_SetDir(1); // Orientation 90°
  1018. DWIN_UpdateLCD(); // Show bootscreen (first image)
  1019. #else
  1020. SETUP_RUN(ui.init());
  1021. #if BOTH(HAS_WIRED_LCD, SHOW_BOOTSCREEN)
  1022. SETUP_RUN(ui.show_bootscreen());
  1023. const millis_t bootscreen_ms = millis();
  1024. #endif
  1025. SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)
  1026. #endif
  1027. #if PIN_EXISTS(SAFE_POWER)
  1028. #if HAS_DRIVER_SAFE_POWER_PROTECT
  1029. SETUP_RUN(stepper_driver_backward_check());
  1030. #else
  1031. SETUP_LOG("SAFE_POWER");
  1032. OUT_WRITE(SAFE_POWER_PIN, HIGH);
  1033. #endif
  1034. #endif
  1035. #if ENABLED(PROBE_TARE)
  1036. SETUP_RUN(probe.tare_init());
  1037. #endif
  1038. #if BOTH(SDSUPPORT, SDCARD_EEPROM_EMULATION)
  1039. SETUP_RUN(card.mount()); // Mount media with settings before first_load
  1040. #endif
  1041. SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
  1042. // This also updates variables in the planner, elsewhere
  1043. #if HAS_ETHERNET
  1044. SETUP_RUN(ethernet.init());
  1045. #endif
  1046. #if HAS_TOUCH_BUTTONS
  1047. SETUP_RUN(touch.init());
  1048. #endif
  1049. TERN_(HAS_M206_COMMAND, current_position += home_offset); // Init current position based on home_offset
  1050. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  1051. SETUP_RUN(thermalManager.init()); // Initialize temperature loop
  1052. SETUP_RUN(print_job_timer.init()); // Initial setup of print job timer
  1053. SETUP_RUN(endstops.init()); // Init endstops and pullups
  1054. SETUP_RUN(stepper.init()); // Init stepper. This enables interrupts!
  1055. #if HAS_SERVOS
  1056. SETUP_RUN(servo_init());
  1057. #endif
  1058. #if HAS_Z_SERVO_PROBE
  1059. SETUP_RUN(probe.servo_probe_init());
  1060. #endif
  1061. #if HAS_PHOTOGRAPH
  1062. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  1063. #endif
  1064. #if HAS_CUTTER
  1065. SETUP_RUN(cutter.init());
  1066. #endif
  1067. #if ENABLED(COOLANT_MIST)
  1068. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  1069. #endif
  1070. #if ENABLED(COOLANT_FLOOD)
  1071. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  1072. #endif
  1073. #if HAS_BED_PROBE
  1074. SETUP_RUN(endstops.enable_z_probe(false));
  1075. #endif
  1076. #if HAS_STEPPER_RESET
  1077. SETUP_RUN(enableStepperDrivers());
  1078. #endif
  1079. #if HAS_MOTOR_CURRENT_I2C
  1080. SETUP_RUN(digipot_i2c.init());
  1081. #endif
  1082. #if ENABLED(HAS_MOTOR_CURRENT_DAC)
  1083. SETUP_RUN(stepper_dac.init());
  1084. #endif
  1085. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  1086. OUT_WRITE(SOL1_PIN, LOW); // OFF
  1087. #endif
  1088. #if HAS_HOME
  1089. SET_INPUT_PULLUP(HOME_PIN);
  1090. #endif
  1091. #if ENABLED(CUSTOM_USER_BUTTONS)
  1092. #define INIT_CUSTOM_USER_BUTTON_PIN(N) do{ SET_INPUT(BUTTON##N##_PIN); WRITE(BUTTON##N##_PIN, !BUTTON##N##_HIT_STATE); }while(0)
  1093. #if HAS_CUSTOM_USER_BUTTON(1)
  1094. INIT_CUSTOM_USER_BUTTON_PIN(1);
  1095. #endif
  1096. #if HAS_CUSTOM_USER_BUTTON(2)
  1097. INIT_CUSTOM_USER_BUTTON_PIN(2);
  1098. #endif
  1099. #if HAS_CUSTOM_USER_BUTTON(3)
  1100. INIT_CUSTOM_USER_BUTTON_PIN(3);
  1101. #endif
  1102. #if HAS_CUSTOM_USER_BUTTON(4)
  1103. INIT_CUSTOM_USER_BUTTON_PIN(4);
  1104. #endif
  1105. #if HAS_CUSTOM_USER_BUTTON(5)
  1106. INIT_CUSTOM_USER_BUTTON_PIN(5);
  1107. #endif
  1108. #if HAS_CUSTOM_USER_BUTTON(6)
  1109. INIT_CUSTOM_USER_BUTTON_PIN(6);
  1110. #endif
  1111. #if HAS_CUSTOM_USER_BUTTON(7)
  1112. INIT_CUSTOM_USER_BUTTON_PIN(7);
  1113. #endif
  1114. #if HAS_CUSTOM_USER_BUTTON(8)
  1115. INIT_CUSTOM_USER_BUTTON_PIN(8);
  1116. #endif
  1117. #if HAS_CUSTOM_USER_BUTTON(9)
  1118. INIT_CUSTOM_USER_BUTTON_PIN(9);
  1119. #endif
  1120. #if HAS_CUSTOM_USER_BUTTON(10)
  1121. INIT_CUSTOM_USER_BUTTON_PIN(10);
  1122. #endif
  1123. #if HAS_CUSTOM_USER_BUTTON(11)
  1124. INIT_CUSTOM_USER_BUTTON_PIN(11);
  1125. #endif
  1126. #if HAS_CUSTOM_USER_BUTTON(12)
  1127. INIT_CUSTOM_USER_BUTTON_PIN(12);
  1128. #endif
  1129. #if HAS_CUSTOM_USER_BUTTON(13)
  1130. INIT_CUSTOM_USER_BUTTON_PIN(13);
  1131. #endif
  1132. #if HAS_CUSTOM_USER_BUTTON(14)
  1133. INIT_CUSTOM_USER_BUTTON_PIN(14);
  1134. #endif
  1135. #if HAS_CUSTOM_USER_BUTTON(15)
  1136. INIT_CUSTOM_USER_BUTTON_PIN(15);
  1137. #endif
  1138. #if HAS_CUSTOM_USER_BUTTON(16)
  1139. INIT_CUSTOM_USER_BUTTON_PIN(16);
  1140. #endif
  1141. #if HAS_CUSTOM_USER_BUTTON(17)
  1142. INIT_CUSTOM_USER_BUTTON_PIN(17);
  1143. #endif
  1144. #if HAS_CUSTOM_USER_BUTTON(18)
  1145. INIT_CUSTOM_USER_BUTTON_PIN(18);
  1146. #endif
  1147. #if HAS_CUSTOM_USER_BUTTON(19)
  1148. INIT_CUSTOM_USER_BUTTON_PIN(19);
  1149. #endif
  1150. #if HAS_CUSTOM_USER_BUTTON(20)
  1151. INIT_CUSTOM_USER_BUTTON_PIN(20);
  1152. #endif
  1153. #if HAS_CUSTOM_USER_BUTTON(21)
  1154. INIT_CUSTOM_USER_BUTTON_PIN(21);
  1155. #endif
  1156. #if HAS_CUSTOM_USER_BUTTON(22)
  1157. INIT_CUSTOM_USER_BUTTON_PIN(22);
  1158. #endif
  1159. #if HAS_CUSTOM_USER_BUTTON(23)
  1160. INIT_CUSTOM_USER_BUTTON_PIN(23);
  1161. #endif
  1162. #if HAS_CUSTOM_USER_BUTTON(24)
  1163. INIT_CUSTOM_USER_BUTTON_PIN(24);
  1164. #endif
  1165. #if HAS_CUSTOM_USER_BUTTON(25)
  1166. INIT_CUSTOM_USER_BUTTON_PIN(25);
  1167. #endif
  1168. #endif
  1169. #if PIN_EXISTS(STAT_LED_RED)
  1170. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  1171. #endif
  1172. #if PIN_EXISTS(STAT_LED_BLUE)
  1173. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  1174. #endif
  1175. #if ENABLED(CASE_LIGHT_ENABLE)
  1176. SETUP_RUN(caselight.init());
  1177. #endif
  1178. #if HAS_PRUSA_MMU1
  1179. SETUP_LOG("Prusa MMU1");
  1180. SET_OUTPUT(E_MUX0_PIN);
  1181. SET_OUTPUT(E_MUX1_PIN);
  1182. SET_OUTPUT(E_MUX2_PIN);
  1183. #endif
  1184. #if HAS_FANMUX
  1185. SETUP_RUN(fanmux_init());
  1186. #endif
  1187. #if ENABLED(MIXING_EXTRUDER)
  1188. SETUP_RUN(mixer.init());
  1189. #endif
  1190. #if ENABLED(BLTOUCH)
  1191. SETUP_RUN(bltouch.init(/*set_voltage=*/true));
  1192. #endif
  1193. #if ENABLED(I2C_POSITION_ENCODERS)
  1194. SETUP_RUN(I2CPEM.init());
  1195. #endif
  1196. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  1197. SETUP_LOG("i2c...");
  1198. i2c.onReceive(i2c_on_receive);
  1199. i2c.onRequest(i2c_on_request);
  1200. #endif
  1201. #if DO_SWITCH_EXTRUDER
  1202. SETUP_RUN(move_extruder_servo(0)); // Initialize extruder servo
  1203. #endif
  1204. #if ENABLED(SWITCHING_NOZZLE)
  1205. SETUP_LOG("SWITCHING_NOZZLE");
  1206. // Initialize nozzle servo(s)
  1207. #if SWITCHING_NOZZLE_TWO_SERVOS
  1208. lower_nozzle(0);
  1209. raise_nozzle(1);
  1210. #else
  1211. move_nozzle_servo(0);
  1212. #endif
  1213. #endif
  1214. #if ENABLED(PARKING_EXTRUDER)
  1215. SETUP_RUN(pe_solenoid_init());
  1216. #elif ENABLED(MAGNETIC_PARKING_EXTRUDER)
  1217. SETUP_RUN(mpe_settings_init());
  1218. #elif ENABLED(SWITCHING_TOOLHEAD)
  1219. SETUP_RUN(swt_init());
  1220. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  1221. SETUP_RUN(est_init());
  1222. #endif
  1223. #if ENABLED(USE_WATCHDOG)
  1224. SETUP_RUN(watchdog_init()); // Reinit watchdog after HAL_get_reset_source call
  1225. #endif
  1226. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  1227. SETUP_RUN(closedloop.init());
  1228. #endif
  1229. #ifdef STARTUP_COMMANDS
  1230. SETUP_LOG("STARTUP_COMMANDS");
  1231. queue.inject_P(PSTR(STARTUP_COMMANDS));
  1232. #endif
  1233. #if ENABLED(HOST_PROMPT_SUPPORT)
  1234. SETUP_RUN(host_action_prompt_end());
  1235. #endif
  1236. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  1237. SETUP_RUN(test_tmc_connection(true, true, true, true));
  1238. #endif
  1239. #if HAS_DRIVER_SAFE_POWER_PROTECT
  1240. SETUP_RUN(stepper_driver_backward_report());
  1241. #endif
  1242. #if HAS_PRUSA_MMU2
  1243. SETUP_RUN(mmu2.init());
  1244. #endif
  1245. #if ENABLED(IIC_BL24CXX_EEPROM)
  1246. BL24CXX::init();
  1247. const uint8_t err = BL24CXX::check();
  1248. SERIAL_ECHO_TERNARY(err, "BL24CXX Check ", "failed", "succeeded", "!\n");
  1249. #endif
  1250. #if ENABLED(DWIN_CREALITY_LCD)
  1251. Encoder_Configuration();
  1252. HMI_Init();
  1253. DWIN_JPG_CacheTo1(Language_English);
  1254. HMI_StartFrame(true);
  1255. DWIN_StatusChanged(GET_TEXT(WELCOME_MSG));
  1256. #endif
  1257. #if HAS_SERVICE_INTERVALS && DISABLED(DWIN_CREALITY_LCD)
  1258. ui.reset_status(true); // Show service messages or keep current status
  1259. #endif
  1260. #if ENABLED(MAX7219_DEBUG)
  1261. SETUP_RUN(max7219.init());
  1262. #endif
  1263. #if ENABLED(DIRECT_STEPPING)
  1264. SETUP_RUN(page_manager.init());
  1265. #endif
  1266. #if HAS_TFT_LVGL_UI
  1267. #if ENABLED(SDSUPPORT)
  1268. if (!card.isMounted()) SETUP_RUN(card.mount()); // Mount SD to load graphics and fonts
  1269. #endif
  1270. SETUP_RUN(tft_lvgl_init());
  1271. #endif
  1272. #if BOTH(HAS_WIRED_LCD, SHOW_BOOTSCREEN)
  1273. const millis_t elapsed = millis() - bootscreen_ms;
  1274. #if ENABLED(MARLIN_DEV_MODE)
  1275. SERIAL_ECHOLNPAIR("elapsed=", elapsed);
  1276. #endif
  1277. SETUP_RUN(ui.bootscreen_completion(elapsed));
  1278. #endif
  1279. #if ENABLED(PASSWORD_ON_STARTUP)
  1280. SETUP_RUN(password.lock_machine()); // Will not proceed until correct password provided
  1281. #endif
  1282. #if BOTH(HAS_LCD_MENU, TOUCH_SCREEN_CALIBRATION) && EITHER(TFT_CLASSIC_UI, TFT_COLOR_UI)
  1283. ui.check_touch_calibration();
  1284. #endif
  1285. marlin_state = MF_RUNNING;
  1286. SETUP_LOG("setup() completed.");
  1287. }
  1288. /**
  1289. * The main Marlin program loop
  1290. *
  1291. * - Call idle() to handle all tasks between G-code commands
  1292. * Note that no G-codes from the queue can be executed during idle()
  1293. * but many G-codes can be called directly anytime like macros.
  1294. * - Check whether SD card auto-start is needed now.
  1295. * - Check whether SD print finishing is needed now.
  1296. * - Run one G-code command from the immediate or main command queue
  1297. * and open up one space. Commands in the main queue may come from sd
  1298. * card, host, or by direct injection. The queue will continue to fill
  1299. * as long as idle() or manage_inactivity() are being called.
  1300. */
  1301. void loop() {
  1302. do {
  1303. idle();
  1304. #if ENABLED(SDSUPPORT)
  1305. if (card.flag.abort_sd_printing) abortSDPrinting();
  1306. if (marlin_state == MF_SD_COMPLETE) finishSDPrinting();
  1307. #endif
  1308. queue.advance();
  1309. endstops.event_handler();
  1310. TERN_(HAS_TFT_LVGL_UI, printer_state_polling());
  1311. } while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
  1312. }