My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MarlinCore.cpp 34KB

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