My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MarlinCore.cpp 34KB

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