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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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. #ifdef ARDUINO
  36. #include <pins_arduino.h>
  37. #endif
  38. #include <math.h>
  39. #include "core/utility.h"
  40. #include "module/motion.h"
  41. #include "module/planner.h"
  42. #include "module/endstops.h"
  43. #include "module/temperature.h"
  44. #include "module/settings.h"
  45. #include "module/printcounter.h" // PrintCounter or Stopwatch
  46. #include "module/stepper.h"
  47. #include "module/stepper/indirection.h"
  48. #include "gcode/gcode.h"
  49. #include "gcode/parser.h"
  50. #include "gcode/queue.h"
  51. #include "sd/cardreader.h"
  52. #include "lcd/marlinui.h"
  53. #if HAS_TOUCH_XPT2046
  54. #include "lcd/touch/touch_buttons.h"
  55. #endif
  56. #if HAS_TFT_LVGL_UI
  57. #include "lcd/extui/lib/mks_ui/tft_lvgl_configuration.h"
  58. #include "lcd/extui/lib/mks_ui/draw_ui.h"
  59. #include "lcd/extui/lib/mks_ui/mks_hardware_test.h"
  60. #include <lvgl.h>
  61. #endif
  62. #if ENABLED(DWIN_CREALITY_LCD)
  63. #include "lcd/dwin/e3v2/dwin.h"
  64. #include "lcd/dwin/dwin_lcd.h"
  65. #include "lcd/dwin/e3v2/rotary_encoder.h"
  66. #endif
  67. #if HAS_ETHERNET
  68. #include "feature/ethernet.h"
  69. #endif
  70. #if ENABLED(IIC_BL24CXX_EEPROM)
  71. #include "libs/BL24CXX.h"
  72. #endif
  73. #if ENABLED(DIRECT_STEPPING)
  74. #include "feature/direct_stepping.h"
  75. #endif
  76. #if ENABLED(HOST_ACTION_COMMANDS)
  77. #include "feature/host_actions.h"
  78. #endif
  79. #if USE_BEEPER
  80. #include "libs/buzzer.h"
  81. #endif
  82. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  83. #include "feature/closedloop.h"
  84. #endif
  85. #if HAS_MOTOR_CURRENT_I2C
  86. #include "feature/digipot/digipot.h"
  87. #endif
  88. #if ENABLED(MIXING_EXTRUDER)
  89. #include "feature/mixing.h"
  90. #endif
  91. #if ENABLED(MAX7219_DEBUG)
  92. #include "feature/max7219.h"
  93. #endif
  94. #if HAS_COLOR_LEDS
  95. #include "feature/leds/leds.h"
  96. #endif
  97. #if ENABLED(BLTOUCH)
  98. #include "feature/bltouch.h"
  99. #endif
  100. #if ENABLED(POLL_JOG)
  101. #include "feature/joystick.h"
  102. #endif
  103. #if HAS_SERVOS
  104. #include "module/servo.h"
  105. #endif
  106. #if ENABLED(HAS_MOTOR_CURRENT_DAC)
  107. #include "feature/dac/stepper_dac.h"
  108. #endif
  109. #if ENABLED(EXPERIMENTAL_I2CBUS)
  110. #include "feature/twibus.h"
  111. TWIBus i2c;
  112. #endif
  113. #if ENABLED(I2C_POSITION_ENCODERS)
  114. #include "feature/encoder_i2c.h"
  115. #endif
  116. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  117. #include "feature/tmc_util.h"
  118. #endif
  119. #if HAS_CUTTER
  120. #include "feature/spindle_laser.h"
  121. #endif
  122. #if ENABLED(SDSUPPORT)
  123. CardReader card;
  124. #endif
  125. #if ENABLED(G38_PROBE_TARGET)
  126. uint8_t G38_move; // = 0
  127. bool G38_did_trigger; // = false
  128. #endif
  129. #if ENABLED(DELTA)
  130. #include "module/delta.h"
  131. #elif IS_SCARA
  132. #include "module/scara.h"
  133. #endif
  134. #if HAS_LEVELING
  135. #include "feature/bedlevel/bedlevel.h"
  136. #endif
  137. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  138. #include "feature/pause.h"
  139. #endif
  140. #if ENABLED(POWER_LOSS_RECOVERY)
  141. #include "feature/powerloss.h"
  142. #endif
  143. #if ENABLED(CANCEL_OBJECTS)
  144. #include "feature/cancel_object.h"
  145. #endif
  146. #if HAS_FILAMENT_SENSOR
  147. #include "feature/runout.h"
  148. #endif
  149. #if HAS_Z_SERVO_PROBE
  150. #include "module/probe.h"
  151. #endif
  152. #if ENABLED(HOTEND_IDLE_TIMEOUT)
  153. #include "feature/hotend_idle.h"
  154. #endif
  155. #if ENABLED(TEMP_STAT_LEDS)
  156. #include "feature/leds/tempstat.h"
  157. #endif
  158. #if ENABLED(CASE_LIGHT_ENABLE)
  159. #include "feature/caselight.h"
  160. #endif
  161. #if HAS_FANMUX
  162. #include "feature/fanmux.h"
  163. #endif
  164. #if DO_SWITCH_EXTRUDER || ANY(SWITCHING_NOZZLE, PARKING_EXTRUDER, MAGNETIC_PARKING_EXTRUDER, ELECTROMAGNETIC_SWITCHING_TOOLHEAD, SWITCHING_TOOLHEAD)
  165. #include "module/tool_change.h"
  166. #endif
  167. #if ENABLED(USE_CONTROLLER_FAN)
  168. #include "feature/controllerfan.h"
  169. #endif
  170. #if ENABLED(PRUSA_MMU2)
  171. #include "feature/mmu2/mmu2.h"
  172. #endif
  173. #if HAS_L64XX
  174. #include "libs/L64XX/L64XX_Marlin.h"
  175. #endif
  176. #if ENABLED(PASSWORD_FEATURE)
  177. #include "feature/password/password.h"
  178. #endif
  179. PGMSTR(NUL_STR, "");
  180. PGMSTR(M112_KILL_STR, "M112 Shutdown");
  181. PGMSTR(G28_STR, "G28");
  182. PGMSTR(M21_STR, "M21");
  183. PGMSTR(M23_STR, "M23 %s");
  184. PGMSTR(M24_STR, "M24");
  185. PGMSTR(SP_P_STR, " P"); PGMSTR(SP_T_STR, " T");
  186. PGMSTR(X_STR, "X"); PGMSTR(Y_STR, "Y"); PGMSTR(Z_STR, "Z"); PGMSTR(E_STR, "E");
  187. PGMSTR(X_LBL, "X:"); PGMSTR(Y_LBL, "Y:"); PGMSTR(Z_LBL, "Z:"); PGMSTR(E_LBL, "E:");
  188. PGMSTR(SP_A_STR, " A"); PGMSTR(SP_B_STR, " B"); PGMSTR(SP_C_STR, " C");
  189. PGMSTR(SP_X_STR, " X"); PGMSTR(SP_Y_STR, " Y"); PGMSTR(SP_Z_STR, " Z"); PGMSTR(SP_E_STR, " E");
  190. PGMSTR(SP_X_LBL, " X:"); PGMSTR(SP_Y_LBL, " Y:"); PGMSTR(SP_Z_LBL, " Z:"); PGMSTR(SP_E_LBL, " E:");
  191. MarlinState marlin_state = MF_INITIALIZING;
  192. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  193. bool wait_for_heatup = true;
  194. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  195. #if HAS_RESUME_CONTINUE
  196. bool wait_for_user; // = false;
  197. void wait_for_user_response(millis_t ms/*=0*/, const bool no_sleep/*=false*/) {
  198. TERN(ADVANCED_PAUSE_FEATURE,,UNUSED(no_sleep));
  199. KEEPALIVE_STATE(PAUSED_FOR_USER);
  200. wait_for_user = true;
  201. if (ms) ms += millis(); // expire time
  202. while (wait_for_user && !(ms && ELAPSED(millis(), ms)))
  203. idle(TERN_(ADVANCED_PAUSE_FEATURE, no_sleep));
  204. wait_for_user = false;
  205. }
  206. #endif
  207. #if PIN_EXISTS(CHDK)
  208. extern millis_t chdk_timeout;
  209. #endif
  210. #if ENABLED(I2C_POSITION_ENCODERS)
  211. I2CPositionEncodersMgr I2CPEM;
  212. #endif
  213. /**
  214. * ***************************************************************************
  215. * ******************************** FUNCTIONS ********************************
  216. * ***************************************************************************
  217. */
  218. void setup_killpin() {
  219. #if HAS_KILL
  220. #if KILL_PIN_STATE
  221. SET_INPUT_PULLDOWN(KILL_PIN);
  222. #else
  223. SET_INPUT_PULLUP(KILL_PIN);
  224. #endif
  225. #endif
  226. }
  227. void setup_powerhold() {
  228. #if HAS_SUICIDE
  229. OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
  230. #endif
  231. #if ENABLED(PSU_CONTROL)
  232. powersupply_on = ENABLED(PSU_DEFAULT_OFF);
  233. if (ENABLED(PSU_DEFAULT_OFF)) PSU_OFF(); else PSU_ON();
  234. #endif
  235. }
  236. /**
  237. * Stepper Reset (RigidBoard, et.al.)
  238. */
  239. #if HAS_STEPPER_RESET
  240. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  241. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  242. #endif
  243. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  244. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  245. i2c.receive(bytes);
  246. }
  247. void i2c_on_request() { // just send dummy data for now
  248. i2c.reply("Hello World!\n");
  249. }
  250. #endif
  251. /**
  252. * Sensitive pin test for M42, M226
  253. */
  254. #include "pins/sensitive_pins.h"
  255. #pragma GCC diagnostic push
  256. #pragma GCC diagnostic ignored "-Wnarrowing"
  257. bool pin_is_protected(const pin_t pin) {
  258. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  259. LOOP_L_N(i, COUNT(sensitive_pins)) {
  260. pin_t sensitive_pin;
  261. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  262. if (pin == sensitive_pin) return true;
  263. }
  264. return false;
  265. }
  266. #pragma GCC diagnostic pop
  267. void protected_pin_err() {
  268. SERIAL_ERROR_MSG(STR_ERR_PROTECTED_PIN);
  269. }
  270. void quickstop_stepper() {
  271. planner.quick_stop();
  272. planner.synchronize();
  273. set_current_from_steppers_for_axis(ALL_AXES);
  274. sync_plan_position();
  275. }
  276. void enable_e_steppers() {
  277. #define _ENA_E(N) ENABLE_AXIS_E##N();
  278. REPEAT(E_STEPPERS, _ENA_E)
  279. }
  280. void enable_all_steppers() {
  281. TERN_(AUTO_POWER_CONTROL, powerManager.power_on());
  282. ENABLE_AXIS_X();
  283. ENABLE_AXIS_Y();
  284. ENABLE_AXIS_Z();
  285. enable_e_steppers();
  286. }
  287. void disable_e_steppers() {
  288. #define _DIS_E(N) DISABLE_AXIS_E##N();
  289. REPEAT(E_STEPPERS, _DIS_E)
  290. }
  291. void disable_e_stepper(const uint8_t e) {
  292. #define _CASE_DIS_E(N) case N: DISABLE_AXIS_E##N(); break;
  293. switch (e) {
  294. REPEAT(EXTRUDERS, _CASE_DIS_E)
  295. }
  296. }
  297. void disable_all_steppers() {
  298. DISABLE_AXIS_X();
  299. DISABLE_AXIS_Y();
  300. DISABLE_AXIS_Z();
  301. disable_e_steppers();
  302. }
  303. #if ENABLED(G29_RETRY_AND_RECOVER)
  304. void event_probe_failure() {
  305. #ifdef ACTION_ON_G29_FAILURE
  306. host_action(PSTR(ACTION_ON_G29_FAILURE));
  307. #endif
  308. #ifdef G29_FAILURE_COMMANDS
  309. gcode.process_subcommands_now_P(PSTR(G29_FAILURE_COMMANDS));
  310. #endif
  311. #if ENABLED(G29_HALT_ON_FAILURE)
  312. #ifdef ACTION_ON_CANCEL
  313. host_action_cancel();
  314. #endif
  315. kill(GET_TEXT(MSG_LCD_PROBING_FAILED));
  316. #endif
  317. }
  318. void event_probe_recover() {
  319. TERN_(HOST_PROMPT_SUPPORT, host_prompt_do(PROMPT_INFO, PSTR("G29 Retrying"), DISMISS_STR));
  320. #ifdef ACTION_ON_G29_RECOVER
  321. host_action(PSTR(ACTION_ON_G29_RECOVER));
  322. #endif
  323. #ifdef G29_RECOVER_COMMANDS
  324. gcode.process_subcommands_now_P(PSTR(G29_RECOVER_COMMANDS));
  325. #endif
  326. }
  327. #endif
  328. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  329. #include "feature/pause.h"
  330. #else
  331. constexpr bool did_pause_print = false;
  332. #endif
  333. /**
  334. * A Print Job exists when the timer is running or SD printing
  335. */
  336. bool printJobOngoing() {
  337. return print_job_timer.isRunning() || IS_SD_PRINTING();
  338. }
  339. /**
  340. * Printing is active when the print job timer is running
  341. */
  342. bool printingIsActive() {
  343. return !did_pause_print && (print_job_timer.isRunning() || IS_SD_PRINTING());
  344. }
  345. /**
  346. * Printing is paused according to SD or host indicators
  347. */
  348. bool printingIsPaused() {
  349. return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
  350. }
  351. void startOrResumeJob() {
  352. if (!printingIsPaused()) {
  353. TERN_(CANCEL_OBJECTS, cancelable.reset());
  354. TERN_(LCD_SHOW_E_TOTAL, e_move_accumulator = 0);
  355. #if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
  356. ui.reset_remaining_time();
  357. #endif
  358. }
  359. print_job_timer.start();
  360. }
  361. #if ENABLED(SDSUPPORT)
  362. inline void abortSDPrinting() {
  363. card.endFilePrint(TERN_(SD_RESORT, true));
  364. queue.clear();
  365. quickstop_stepper();
  366. print_job_timer.stop();
  367. #if DISABLED(SD_ABORT_NO_COOLDOWN)
  368. thermalManager.disable_all_heaters();
  369. #endif
  370. #if !HAS_CUTTER
  371. thermalManager.zero_fan_speeds();
  372. #else
  373. cutter.kill(); // Full cutter shutdown including ISR control
  374. #endif
  375. wait_for_heatup = false;
  376. TERN_(POWER_LOSS_RECOVERY, recovery.purge());
  377. #ifdef EVENT_GCODE_SD_ABORT
  378. queue.inject_P(PSTR(EVENT_GCODE_SD_ABORT));
  379. #endif
  380. TERN_(PASSWORD_AFTER_SD_PRINT_ABORT, password.lock_machine());
  381. }
  382. inline void finishSDPrinting() {
  383. if (queue.enqueue_one_P(PSTR("M1001"))) {
  384. marlin_state = MF_RUNNING;
  385. TERN_(PASSWORD_AFTER_SD_PRINT_END, password.lock_machine());
  386. }
  387. }
  388. #endif // SDSUPPORT
  389. /**
  390. * Minimal management of Marlin's core activities:
  391. * - Keep the command buffer full
  392. * - Check for maximum inactive time between commands
  393. * - Check for maximum inactive time between stepper commands
  394. * - Check if CHDK_PIN needs to go LOW
  395. * - Check for KILL button held down
  396. * - Check for HOME button held down
  397. * - Check if cooling fan needs to be switched on
  398. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  399. * - Pulse FET_SAFETY_PIN if it exists
  400. */
  401. inline void manage_inactivity(const bool ignore_stepper_queue=false) {
  402. if (queue.length < BUFSIZE) queue.get_available_commands();
  403. const millis_t ms = millis();
  404. // Prevent steppers timing-out in the middle of M600
  405. // unless PAUSE_PARK_NO_STEPPER_TIMEOUT is disabled
  406. const bool parked_or_ignoring = ignore_stepper_queue ||
  407. (BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT) && did_pause_print);
  408. // Reset both the M18/M84 activity timeout and the M85 max 'kill' timeout
  409. if (parked_or_ignoring) gcode.reset_stepper_timeout(ms);
  410. if (gcode.stepper_max_timed_out(ms)) {
  411. SERIAL_ERROR_START();
  412. SERIAL_ECHOLNPAIR(STR_KILL_INACTIVE_TIME, parser.command_ptr);
  413. kill();
  414. }
  415. // M18 / M94 : Handle steppers inactive time timeout
  416. if (gcode.stepper_inactive_time) {
  417. static bool already_shutdown_steppers; // = false
  418. // Any moves in the planner? Resets both the M18/M84
  419. // activity timeout and the M85 max 'kill' timeout
  420. if (planner.has_blocks_queued())
  421. gcode.reset_stepper_timeout(ms);
  422. else if (!parked_or_ignoring && gcode.stepper_inactive_timeout()) {
  423. if (!already_shutdown_steppers) {
  424. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  425. // Individual axes will be disabled if configured
  426. if (ENABLED(DISABLE_INACTIVE_X)) DISABLE_AXIS_X();
  427. if (ENABLED(DISABLE_INACTIVE_Y)) DISABLE_AXIS_Y();
  428. if (ENABLED(DISABLE_INACTIVE_Z)) DISABLE_AXIS_Z();
  429. if (ENABLED(DISABLE_INACTIVE_E)) disable_e_steppers();
  430. TERN_(AUTO_BED_LEVELING_UBL, ubl.steppers_were_disabled());
  431. }
  432. }
  433. else
  434. already_shutdown_steppers = false;
  435. }
  436. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  437. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  438. chdk_timeout = 0;
  439. WRITE(CHDK_PIN, LOW);
  440. }
  441. #endif
  442. #if HAS_KILL
  443. // Check if the kill button was pressed and wait just in case it was an accidental
  444. // key kill key press
  445. // -------------------------------------------------------------------------------
  446. static int killCount = 0; // make the inactivity button a bit less responsive
  447. const int KILL_DELAY = 750;
  448. if (kill_state())
  449. killCount++;
  450. else if (killCount > 0)
  451. killCount--;
  452. // Exceeded threshold and we can confirm that it was not accidental
  453. // KILL the machine
  454. // ----------------------------------------------------------------
  455. if (killCount >= KILL_DELAY) {
  456. SERIAL_ERROR_MSG(STR_KILL_BUTTON);
  457. kill();
  458. }
  459. #endif
  460. #if HAS_HOME
  461. // Handle a standalone HOME button
  462. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  463. static millis_t next_home_key_ms; // = 0
  464. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  465. const millis_t ms = millis();
  466. if (ELAPSED(ms, next_home_key_ms)) {
  467. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  468. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  469. queue.enqueue_now_P(G28_STR);
  470. }
  471. }
  472. #endif
  473. TERN_(USE_CONTROLLER_FAN, controllerFan.update()); // Check if fan should be turned on to cool stepper drivers down
  474. TERN_(AUTO_POWER_CONTROL, powerManager.check());
  475. TERN_(HOTEND_IDLE_TIMEOUT, hotend_idle.check());
  476. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  477. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  478. && ELAPSED(ms, gcode.previous_move_ms + SEC_TO_MS(EXTRUDER_RUNOUT_SECONDS))
  479. && !planner.has_blocks_queued()
  480. ) {
  481. #if ENABLED(SWITCHING_EXTRUDER)
  482. bool oldstatus;
  483. switch (active_extruder) {
  484. default: oldstatus = E0_ENABLE_READ(); ENABLE_AXIS_E0(); break;
  485. #if E_STEPPERS > 1
  486. case 2: case 3: oldstatus = E1_ENABLE_READ(); ENABLE_AXIS_E1(); break;
  487. #if E_STEPPERS > 2
  488. case 4: case 5: oldstatus = E2_ENABLE_READ(); ENABLE_AXIS_E2(); break;
  489. #if E_STEPPERS > 3
  490. case 6: case 7: oldstatus = E3_ENABLE_READ(); ENABLE_AXIS_E3(); break;
  491. #endif // E_STEPPERS > 3
  492. #endif // E_STEPPERS > 2
  493. #endif // E_STEPPERS > 1
  494. }
  495. #else // !SWITCHING_EXTRUDER
  496. bool oldstatus;
  497. switch (active_extruder) {
  498. default:
  499. #define _CASE_EN(N) case N: oldstatus = E##N##_ENABLE_READ(); ENABLE_AXIS_E##N(); break;
  500. REPEAT(E_STEPPERS, _CASE_EN);
  501. }
  502. #endif
  503. const float olde = current_position.e;
  504. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  505. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  506. current_position.e = olde;
  507. planner.set_e_position_mm(olde);
  508. planner.synchronize();
  509. #if ENABLED(SWITCHING_EXTRUDER)
  510. switch (active_extruder) {
  511. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  512. #if E_STEPPERS > 1
  513. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  514. #if E_STEPPERS > 2
  515. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  516. #endif // E_STEPPERS > 2
  517. #endif // E_STEPPERS > 1
  518. }
  519. #else // !SWITCHING_EXTRUDER
  520. switch (active_extruder) {
  521. #define _CASE_RESTORE(N) case N: E##N##_ENABLE_WRITE(oldstatus); break;
  522. REPEAT(E_STEPPERS, _CASE_RESTORE);
  523. }
  524. #endif // !SWITCHING_EXTRUDER
  525. gcode.reset_stepper_timeout(ms);
  526. }
  527. #endif // EXTRUDER_RUNOUT_PREVENT
  528. #if ENABLED(DUAL_X_CARRIAGE)
  529. // handle delayed move timeout
  530. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  531. // travel moves have been received so enact them
  532. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  533. destination = current_position;
  534. prepare_line_to_destination();
  535. }
  536. #endif
  537. TERN_(TEMP_STAT_LEDS, handle_status_leds());
  538. TERN_(MONITOR_DRIVER_STATUS, monitor_tmc_drivers());
  539. TERN_(MONITOR_L6470_DRIVER_STATUS, L64xxManager.monitor_driver());
  540. // Limit check_axes_activity frequency to 10Hz
  541. static millis_t next_check_axes_ms = 0;
  542. if (ELAPSED(ms, next_check_axes_ms)) {
  543. planner.check_axes_activity();
  544. next_check_axes_ms = ms + 100UL;
  545. }
  546. #if PIN_EXISTS(FET_SAFETY)
  547. static millis_t FET_next;
  548. if (ELAPSED(ms, FET_next)) {
  549. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  550. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  551. DELAY_US(2);
  552. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  553. }
  554. #endif
  555. }
  556. /**
  557. * Standard idle routine keeps the machine alive:
  558. * - Core Marlin activities
  559. * - Manage heaters (and Watchdog)
  560. * - Max7219 heartbeat, animation, etc.
  561. *
  562. * Only after setup() is complete:
  563. * - Handle filament runout sensors
  564. * - Run HAL idle tasks
  565. * - Handle Power-Loss Recovery
  566. * - Run StallGuard endstop checks
  567. * - Handle SD Card insert / remove
  568. * - Handle USB Flash Drive insert / remove
  569. * - Announce Host Keepalive state (if any)
  570. * - Update the Print Job Timer state
  571. * - Update the Beeper queue
  572. * - Read Buttons and Update the LCD
  573. * - Run i2c Position Encoders
  574. * - Auto-report Temperatures / SD Status
  575. * - Update the Průša MMU2
  576. * - Handle Joystick jogging
  577. */
  578. void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
  579. // Core Marlin activities
  580. manage_inactivity(TERN_(ADVANCED_PAUSE_FEATURE, no_stepper_sleep));
  581. // Manage Heaters (and Watchdog)
  582. thermalManager.manage_heater();
  583. // Max7219 heartbeat, animation, etc
  584. TERN_(MAX7219_DEBUG, max7219.idle_tasks());
  585. // Return if setup() isn't completed
  586. if (marlin_state == MF_INITIALIZING) return;
  587. // Handle filament runout sensors
  588. TERN_(HAS_FILAMENT_SENSOR, runout.run());
  589. // Run HAL idle tasks
  590. #ifdef HAL_IDLETASK
  591. HAL_idletask();
  592. #endif
  593. // Check network connection
  594. TERN_(HAS_ETHERNET, ethernet.check());
  595. // Handle Power-Loss Recovery
  596. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  597. if (printJobOngoing()) recovery.outage();
  598. #endif
  599. // Run StallGuard endstop checks
  600. #if ENABLED(SPI_ENDSTOPS)
  601. if (endstops.tmc_spi_homing.any
  602. && TERN1(IMPROVE_HOMING_RELIABILITY, ELAPSED(millis(), sg_guard_period))
  603. ) LOOP_L_N(i, 4) // Read SGT 4 times per idle loop
  604. if (endstops.tmc_spi_homing_check()) break;
  605. #endif
  606. // Handle SD Card insert / remove
  607. TERN_(SDSUPPORT, card.manage_media());
  608. // Handle USB Flash Drive insert / remove
  609. TERN_(USB_FLASH_DRIVE_SUPPORT, Sd2Card::idle());
  610. // Announce Host Keepalive state (if any)
  611. TERN_(HOST_KEEPALIVE_FEATURE, gcode.host_keepalive());
  612. // Update the Print Job Timer state
  613. TERN_(PRINTCOUNTER, print_job_timer.tick());
  614. // Update the Beeper queue
  615. TERN_(USE_BEEPER, buzzer.tick());
  616. // Handle UI input / draw events
  617. TERN(DWIN_CREALITY_LCD, DWIN_Update(), ui.update());
  618. // Run i2c Position Encoders
  619. #if ENABLED(I2C_POSITION_ENCODERS)
  620. static millis_t i2cpem_next_update_ms;
  621. if (planner.has_blocks_queued()) {
  622. const millis_t ms = millis();
  623. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  624. I2CPEM.update();
  625. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  626. }
  627. }
  628. #endif
  629. // Auto-report Temperatures / SD Status
  630. #if HAS_AUTO_REPORTING
  631. if (!gcode.autoreport_paused) {
  632. TERN_(AUTO_REPORT_TEMPERATURES, thermalManager.auto_report_temperatures());
  633. TERN_(AUTO_REPORT_SD_STATUS, card.auto_report_sd_status());
  634. }
  635. #endif
  636. // Update the Průša MMU2
  637. TERN_(PRUSA_MMU2, mmu2.mmu_loop());
  638. // Handle Joystick jogging
  639. TERN_(POLL_JOG, joystick.inject_jog_moves());
  640. // Direct Stepping
  641. TERN_(DIRECT_STEPPING, page_manager.write_responses());
  642. #if HAS_TFT_LVGL_UI
  643. LV_TASK_HANDLER();
  644. #endif
  645. }
  646. /**
  647. * Kill all activity and lock the machine.
  648. * After this the machine will need to be reset.
  649. */
  650. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  651. thermalManager.disable_all_heaters();
  652. TERN_(HAS_CUTTER, cutter.kill()); // Full cutter shutdown including ISR control
  653. SERIAL_ERROR_MSG(STR_ERR_KILLED);
  654. #if HAS_DISPLAY
  655. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component ?: NUL_STR);
  656. #else
  657. UNUSED(lcd_error);
  658. UNUSED(lcd_component);
  659. #endif
  660. #if HAS_TFT_LVGL_UI
  661. lv_draw_error_message(lcd_error);
  662. #endif
  663. #ifdef ACTION_ON_KILL
  664. host_action_kill();
  665. #endif
  666. minkill(steppers_off);
  667. }
  668. void minkill(const bool steppers_off/*=false*/) {
  669. // Wait a short time (allows messages to get out before shutting down.
  670. for (int i = 1000; i--;) DELAY_US(600);
  671. cli(); // Stop interrupts
  672. // Wait to ensure all interrupts stopped
  673. for (int i = 1000; i--;) DELAY_US(250);
  674. // Reiterate heaters off
  675. thermalManager.disable_all_heaters();
  676. TERN_(HAS_CUTTER, cutter.kill()); // Reiterate cutter shutdown
  677. // Power off all steppers (for M112) or just the E steppers
  678. steppers_off ? disable_all_steppers() : disable_e_steppers();
  679. TERN_(PSU_CONTROL, PSU_OFF());
  680. TERN_(HAS_SUICIDE, suicide());
  681. #if HAS_KILL
  682. // Wait for kill to be released
  683. while (kill_state()) watchdog_refresh();
  684. // Wait for kill to be pressed
  685. while (!kill_state()) watchdog_refresh();
  686. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  687. resetFunc(); // Jump to address 0
  688. #else
  689. for (;;) watchdog_refresh(); // Wait for reset
  690. #endif
  691. }
  692. /**
  693. * Turn off heaters and stop the print in progress
  694. * After a stop the machine may be resumed with M999
  695. */
  696. void stop() {
  697. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  698. print_job_timer.stop();
  699. #if ENABLED(PROBING_FANS_OFF)
  700. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  701. #endif
  702. if (IsRunning()) {
  703. SERIAL_ERROR_MSG(STR_ERR_STOPPED);
  704. LCD_MESSAGEPGM(MSG_STOPPED);
  705. safe_delay(350); // allow enough time for messages to get out before stopping
  706. marlin_state = MF_STOPPED;
  707. }
  708. }
  709. inline void tmc_standby_setup() {
  710. #if PIN_EXISTS(X_STDBY)
  711. SET_INPUT_PULLDOWN(X_STDBY_PIN);
  712. #endif
  713. #if PIN_EXISTS(X2_STDBY)
  714. SET_INPUT_PULLDOWN(X2_STDBY_PIN);
  715. #endif
  716. #if PIN_EXISTS(Y_STDBY)
  717. SET_INPUT_PULLDOWN(Y_STDBY_PIN);
  718. #endif
  719. #if PIN_EXISTS(Y2_STDBY)
  720. SET_INPUT_PULLDOWN(Y2_STDBY_PIN);
  721. #endif
  722. #if PIN_EXISTS(Z_STDBY)
  723. SET_INPUT_PULLDOWN(Z_STDBY_PIN);
  724. #endif
  725. #if PIN_EXISTS(Z2_STDBY)
  726. SET_INPUT_PULLDOWN(Z2_STDBY_PIN);
  727. #endif
  728. #if PIN_EXISTS(Z3_STDBY)
  729. SET_INPUT_PULLDOWN(Z3_STDBY_PIN);
  730. #endif
  731. #if PIN_EXISTS(Z4_STDBY)
  732. SET_INPUT_PULLDOWN(Z4_STDBY_PIN);
  733. #endif
  734. #if PIN_EXISTS(E0_STDBY)
  735. SET_INPUT_PULLDOWN(E0_STDBY_PIN);
  736. #endif
  737. #if PIN_EXISTS(E1_STDBY)
  738. SET_INPUT_PULLDOWN(E1_STDBY_PIN);
  739. #endif
  740. #if PIN_EXISTS(E2_STDBY)
  741. SET_INPUT_PULLDOWN(E2_STDBY_PIN);
  742. #endif
  743. #if PIN_EXISTS(E3_STDBY)
  744. SET_INPUT_PULLDOWN(E3_STDBY_PIN);
  745. #endif
  746. #if PIN_EXISTS(E4_STDBY)
  747. SET_INPUT_PULLDOWN(E4_STDBY_PIN);
  748. #endif
  749. #if PIN_EXISTS(E5_STDBY)
  750. SET_INPUT_PULLDOWN(E5_STDBY_PIN);
  751. #endif
  752. #if PIN_EXISTS(E6_STDBY)
  753. SET_INPUT_PULLDOWN(E6_STDBY_PIN);
  754. #endif
  755. #if PIN_EXISTS(E7_STDBY)
  756. SET_INPUT_PULLDOWN(E7_STDBY_PIN);
  757. #endif
  758. }
  759. /**
  760. * Marlin entry-point: Set up before the program loop
  761. * - Set up the kill pin, filament runout, power hold
  762. * - Start the serial port
  763. * - Print startup messages and diagnostics
  764. * - Get EEPROM or default settings
  765. * - Initialize managers for:
  766. * • temperature
  767. * • planner
  768. * • watchdog
  769. * • stepper
  770. * • photo pin
  771. * • servos
  772. * • LCD controller
  773. * • Digipot I2C
  774. * • Z probe sled
  775. * • status LEDs
  776. * • Max7219
  777. */
  778. void setup() {
  779. tmc_standby_setup(); // TMC Low Power Standby pins must be set early or they're not usable
  780. #if ENABLED(MARLIN_DEV_MODE)
  781. auto log_current_ms = [&](PGM_P const msg) {
  782. SERIAL_ECHO_START();
  783. SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHOPGM("] ");
  784. serialprintPGM(msg);
  785. SERIAL_EOL();
  786. };
  787. #define SETUP_LOG(M) log_current_ms(PSTR(M))
  788. #else
  789. #define SETUP_LOG(...) NOOP
  790. #endif
  791. #define SETUP_RUN(C) do{ SETUP_LOG(STRINGIFY(C)); C; }while(0)
  792. #if EITHER(DISABLE_DEBUG, DISABLE_JTAG)
  793. // Disable any hardware debug to free up pins for IO
  794. #if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
  795. JTAGSWD_DISABLE();
  796. #elif defined(JTAG_DISABLE)
  797. JTAG_DISABLE();
  798. #else
  799. #error "DISABLE_(DEBUG|JTAG) is not supported for the selected MCU/Board."
  800. #endif
  801. #endif
  802. MYSERIAL0.begin(BAUDRATE);
  803. uint32_t serial_connect_timeout = millis() + 1000UL;
  804. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  805. #if HAS_MULTI_SERIAL && !HAS_ETHERNET
  806. MYSERIAL1.begin(BAUDRATE);
  807. serial_connect_timeout = millis() + 1000UL;
  808. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  809. #endif
  810. SERIAL_ECHO_MSG("start");
  811. #if BOTH(HAS_TFT_LVGL_UI, USE_WIFI_FUNCTION)
  812. mks_esp_wifi_init();
  813. WIFISERIAL.begin(WIFI_BAUDRATE);
  814. serial_connect_timeout = millis() + 1000UL;
  815. while (/*!WIFISERIAL && */PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  816. #endif
  817. SETUP_RUN(HAL_init());
  818. #if HAS_L64XX
  819. SETUP_RUN(L64xxManager.init()); // Set up SPI, init drivers
  820. #endif
  821. #if ENABLED(DUET_SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  822. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  823. #endif
  824. #if HAS_FILAMENT_SENSOR
  825. SETUP_RUN(runout.setup());
  826. #endif
  827. #if ENABLED(POWER_LOSS_RECOVERY)
  828. SETUP_RUN(recovery.setup());
  829. #endif
  830. SETUP_RUN(setup_killpin());
  831. #if HAS_TMC220x
  832. SETUP_RUN(tmc_serial_begin());
  833. #endif
  834. SETUP_RUN(setup_powerhold());
  835. #if HAS_STEPPER_RESET
  836. SETUP_RUN(disableStepperDrivers());
  837. #endif
  838. #if HAS_TMC_SPI
  839. #if DISABLED(TMC_USE_SW_SPI)
  840. SETUP_RUN(SPI.begin());
  841. #endif
  842. SETUP_RUN(tmc_init_cs_pins());
  843. #endif
  844. #ifdef BOARD_INIT
  845. SETUP_LOG("BOARD_INIT");
  846. BOARD_INIT();
  847. #endif
  848. SETUP_RUN(esp_wifi_init());
  849. // Check startup - does nothing if bootloader sets MCUSR to 0
  850. const byte mcu = HAL_get_reset_source();
  851. if (mcu & RST_POWER_ON) SERIAL_ECHOLNPGM(STR_POWERUP);
  852. if (mcu & RST_EXTERNAL) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  853. if (mcu & RST_BROWN_OUT) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  854. if (mcu & RST_WATCHDOG) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  855. if (mcu & RST_SOFTWARE) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  856. HAL_clear_reset_source();
  857. serialprintPGM(GET_TEXT(MSG_MARLIN));
  858. SERIAL_CHAR(' ');
  859. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  860. SERIAL_EOL();
  861. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  862. SERIAL_ECHO_MSG(
  863. " Last Updated: " STRING_DISTRIBUTION_DATE
  864. " | Author: " STRING_CONFIG_H_AUTHOR
  865. );
  866. #endif
  867. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  868. SERIAL_ECHO_MSG(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  869. // Init buzzer pin(s)
  870. #if USE_BEEPER
  871. SETUP_RUN(buzzer.init());
  872. #endif
  873. // Set up LEDs early
  874. #if HAS_COLOR_LEDS
  875. SETUP_RUN(leds.setup());
  876. #endif
  877. #if ENABLED(NEOPIXEL2_SEPARATE)
  878. SETUP_RUN(leds2.setup());
  879. #endif
  880. #if ENABLED(USE_CONTROLLER_FAN) // Set up fan controller to initialize also the default configurations.
  881. SETUP_RUN(controllerFan.setup());
  882. #endif
  883. // UI must be initialized before EEPROM
  884. // (because EEPROM code calls the UI).
  885. #if ENABLED(DWIN_CREALITY_LCD)
  886. delay(800); // Required delay (since boot?)
  887. SERIAL_ECHOPGM("\nDWIN handshake ");
  888. if (DWIN_Handshake()) SERIAL_ECHOLNPGM("ok."); else SERIAL_ECHOLNPGM("error.");
  889. DWIN_Frame_SetDir(1); // Orientation 90°
  890. DWIN_UpdateLCD(); // Show bootscreen (first image)
  891. #else
  892. SETUP_RUN(ui.init());
  893. #if HAS_WIRED_LCD && ENABLED(SHOW_BOOTSCREEN)
  894. SETUP_RUN(ui.show_bootscreen());
  895. #endif
  896. SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)
  897. #endif
  898. #if BOTH(SDSUPPORT, SDCARD_EEPROM_EMULATION)
  899. SETUP_RUN(card.mount()); // Mount media with settings before first_load
  900. #endif
  901. SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
  902. // This also updates variables in the planner, elsewhere
  903. #if HAS_ETHERNET
  904. SETUP_RUN(ethernet.init());
  905. #endif
  906. #if HAS_TOUCH_XPT2046
  907. SETUP_RUN(touch.init());
  908. #endif
  909. TERN_(HAS_M206_COMMAND, current_position += home_offset); // Init current position based on home_offset
  910. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  911. SETUP_RUN(thermalManager.init()); // Initialize temperature loop
  912. SETUP_RUN(print_job_timer.init()); // Initial setup of print job timer
  913. SETUP_RUN(endstops.init()); // Init endstops and pullups
  914. SETUP_RUN(stepper.init()); // Init stepper. This enables interrupts!
  915. #if HAS_SERVOS
  916. SETUP_RUN(servo_init());
  917. #endif
  918. #if HAS_Z_SERVO_PROBE
  919. SETUP_RUN(probe.servo_probe_init());
  920. #endif
  921. #if HAS_PHOTOGRAPH
  922. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  923. #endif
  924. #if HAS_CUTTER
  925. SETUP_RUN(cutter.init());
  926. #endif
  927. #if ENABLED(COOLANT_MIST)
  928. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  929. #endif
  930. #if ENABLED(COOLANT_FLOOD)
  931. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  932. #endif
  933. #if HAS_BED_PROBE
  934. SETUP_RUN(endstops.enable_z_probe(false));
  935. #endif
  936. #if HAS_STEPPER_RESET
  937. SETUP_RUN(enableStepperDrivers());
  938. #endif
  939. #if HAS_MOTOR_CURRENT_I2C
  940. SETUP_RUN(digipot_i2c.init());
  941. #endif
  942. #if ENABLED(HAS_MOTOR_CURRENT_DAC)
  943. SETUP_RUN(stepper_dac.init());
  944. #endif
  945. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  946. OUT_WRITE(SOL1_PIN, LOW); // OFF
  947. #endif
  948. #if HAS_HOME
  949. SET_INPUT_PULLUP(HOME_PIN);
  950. #endif
  951. #if PIN_EXISTS(STAT_LED_RED)
  952. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  953. #endif
  954. #if PIN_EXISTS(STAT_LED_BLUE)
  955. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  956. #endif
  957. #if ENABLED(CASE_LIGHT_ENABLE)
  958. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  959. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  960. #endif
  961. SETUP_RUN(caselight.update_brightness());
  962. #endif
  963. #if ENABLED(MK2_MULTIPLEXER)
  964. SETUP_LOG("MK2_MULTIPLEXER");
  965. SET_OUTPUT(E_MUX0_PIN);
  966. SET_OUTPUT(E_MUX1_PIN);
  967. SET_OUTPUT(E_MUX2_PIN);
  968. #endif
  969. #if HAS_FANMUX
  970. SETUP_RUN(fanmux_init());
  971. #endif
  972. #if ENABLED(MIXING_EXTRUDER)
  973. SETUP_RUN(mixer.init());
  974. #endif
  975. #if ENABLED(BLTOUCH)
  976. SETUP_RUN(bltouch.init(/*set_voltage=*/true));
  977. #endif
  978. #if ENABLED(I2C_POSITION_ENCODERS)
  979. SETUP_RUN(I2CPEM.init());
  980. #endif
  981. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  982. SETUP_LOG("i2c...");
  983. i2c.onReceive(i2c_on_receive);
  984. i2c.onRequest(i2c_on_request);
  985. #endif
  986. #if DO_SWITCH_EXTRUDER
  987. SETUP_RUN(move_extruder_servo(0)); // Initialize extruder servo
  988. #endif
  989. #if ENABLED(SWITCHING_NOZZLE)
  990. SETUP_LOG("SWITCHING_NOZZLE");
  991. // Initialize nozzle servo(s)
  992. #if SWITCHING_NOZZLE_TWO_SERVOS
  993. lower_nozzle(0);
  994. raise_nozzle(1);
  995. #else
  996. move_nozzle_servo(0);
  997. #endif
  998. #endif
  999. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  1000. SETUP_RUN(mpe_settings_init());
  1001. #endif
  1002. #if ENABLED(PARKING_EXTRUDER)
  1003. SETUP_RUN(pe_solenoid_init());
  1004. #endif
  1005. #if ENABLED(SWITCHING_TOOLHEAD)
  1006. SETUP_RUN(swt_init());
  1007. #endif
  1008. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  1009. SETUP_RUN(est_init());
  1010. #endif
  1011. #if ENABLED(USE_WATCHDOG)
  1012. SETUP_RUN(watchdog_init()); // Reinit watchdog after HAL_get_reset_source call
  1013. #endif
  1014. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  1015. SETUP_RUN(closedloop.init());
  1016. #endif
  1017. #ifdef STARTUP_COMMANDS
  1018. SETUP_LOG("STARTUP_COMMANDS");
  1019. queue.inject_P(PSTR(STARTUP_COMMANDS));
  1020. #endif
  1021. #if ENABLED(HOST_PROMPT_SUPPORT)
  1022. SETUP_RUN(host_action_prompt_end());
  1023. #endif
  1024. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  1025. SETUP_RUN(test_tmc_connection(true, true, true, true));
  1026. #endif
  1027. #if ENABLED(PRUSA_MMU2)
  1028. SETUP_RUN(mmu2.init());
  1029. #endif
  1030. #if ENABLED(IIC_BL24CXX_EEPROM)
  1031. BL24CXX::init();
  1032. const uint8_t err = BL24CXX::check();
  1033. SERIAL_ECHO_TERNARY(err, "BL24CXX Check ", "failed", "succeeded", "!\n");
  1034. #endif
  1035. #if ENABLED(DWIN_CREALITY_LCD)
  1036. Encoder_Configuration();
  1037. HMI_Init();
  1038. HMI_StartFrame(true);
  1039. #endif
  1040. #if HAS_SERVICE_INTERVALS && DISABLED(DWIN_CREALITY_LCD)
  1041. ui.reset_status(true); // Show service messages or keep current status
  1042. #endif
  1043. #if ENABLED(MAX7219_DEBUG)
  1044. SETUP_RUN(max7219.init());
  1045. #endif
  1046. #if ENABLED(DIRECT_STEPPING)
  1047. SETUP_RUN(page_manager.init());
  1048. #endif
  1049. #if HAS_TFT_LVGL_UI
  1050. #if ENABLED(SDSUPPORT)
  1051. if (!card.isMounted()) SETUP_RUN(card.mount()); // Mount SD to load graphics and fonts
  1052. #endif
  1053. SETUP_RUN(tft_lvgl_init());
  1054. #endif
  1055. #if ENABLED(PASSWORD_ON_STARTUP)
  1056. SETUP_RUN(password.lock_machine()); // Will not proceed until correct password provided
  1057. #endif
  1058. marlin_state = MF_RUNNING;
  1059. SETUP_LOG("setup() completed.");
  1060. }
  1061. /**
  1062. * The main Marlin program loop
  1063. *
  1064. * - Call idle() to handle all tasks between G-code commands
  1065. * Note that no G-codes from the queue can be executed during idle()
  1066. * but many G-codes can be called directly anytime like macros.
  1067. * - Check whether SD card auto-start is needed now.
  1068. * - Check whether SD print finishing is needed now.
  1069. * - Run one G-code command from the immediate or main command queue
  1070. * and open up one space. Commands in the main queue may come from sd
  1071. * card, host, or by direct injection. The queue will continue to fill
  1072. * as long as idle() or manage_inactivity() are being called.
  1073. */
  1074. void loop() {
  1075. do {
  1076. idle();
  1077. #if ENABLED(SDSUPPORT)
  1078. card.checkautostart();
  1079. if (card.flag.abort_sd_printing) abortSDPrinting();
  1080. if (marlin_state == MF_SD_COMPLETE) finishSDPrinting();
  1081. #endif
  1082. queue.advance();
  1083. endstops.event_handler();
  1084. TERN_(HAS_TFT_LVGL_UI, printer_state_polling());
  1085. } while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
  1086. }