My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MarlinCore.cpp 28KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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 <http://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. #include "core/utility.h"
  31. #include "lcd/ultralcd.h"
  32. #include "module/motion.h"
  33. #include "module/planner.h"
  34. #include "module/stepper.h"
  35. #include "module/endstops.h"
  36. #include "module/probe.h"
  37. #include "module/temperature.h"
  38. #include "sd/cardreader.h"
  39. #include "module/configuration_store.h"
  40. #include "module/printcounter.h" // PrintCounter or Stopwatch
  41. #include "feature/closedloop.h"
  42. #include "HAL/shared/Delay.h"
  43. #include "module/stepper/indirection.h"
  44. #ifdef ARDUINO
  45. #include <pins_arduino.h>
  46. #endif
  47. #include <math.h>
  48. #include "libs/nozzle.h"
  49. #include "gcode/gcode.h"
  50. #include "gcode/parser.h"
  51. #include "gcode/queue.h"
  52. #if ENABLED(TOUCH_BUTTONS)
  53. #include "feature/touch/xpt2046.h"
  54. #endif
  55. #if ENABLED(HOST_ACTION_COMMANDS)
  56. #include "feature/host_actions.h"
  57. #endif
  58. #if USE_BEEPER
  59. #include "libs/buzzer.h"
  60. #endif
  61. #if ENABLED(DIGIPOT_I2C)
  62. #include "feature/digipot/digipot.h"
  63. #endif
  64. #if ENABLED(MIXING_EXTRUDER)
  65. #include "feature/mixing.h"
  66. #endif
  67. #if ENABLED(MAX7219_DEBUG)
  68. #include "feature/Max7219_Debug_LEDs.h"
  69. #endif
  70. #if HAS_COLOR_LEDS
  71. #include "feature/leds/leds.h"
  72. #endif
  73. #if ENABLED(BLTOUCH)
  74. #include "feature/bltouch.h"
  75. #endif
  76. #if ENABLED(POLL_JOG)
  77. #include "feature/joystick.h"
  78. #endif
  79. #if HAS_SERVOS
  80. #include "module/servo.h"
  81. #endif
  82. #if ENABLED(DAC_STEPPER_CURRENT)
  83. #include "feature/dac/stepper_dac.h"
  84. #endif
  85. #if ENABLED(EXPERIMENTAL_I2CBUS)
  86. #include "feature/twibus.h"
  87. TWIBus i2c;
  88. #endif
  89. #if ENABLED(I2C_POSITION_ENCODERS)
  90. #include "feature/I2CPositionEncoder.h"
  91. #endif
  92. #if HAS_TRINAMIC && DISABLED(PSU_DEFAULT_OFF)
  93. #include "feature/tmc_util.h"
  94. #endif
  95. #if HAS_CUTTER
  96. #include "feature/spindle_laser.h"
  97. #endif
  98. #if ENABLED(SDSUPPORT)
  99. CardReader card;
  100. #endif
  101. #if ENABLED(G38_PROBE_TARGET)
  102. uint8_t G38_move; // = 0
  103. bool G38_did_trigger; // = false
  104. #endif
  105. #if ENABLED(DELTA)
  106. #include "module/delta.h"
  107. #elif IS_SCARA
  108. #include "module/scara.h"
  109. #endif
  110. #if HAS_LEVELING
  111. #include "feature/bedlevel/bedlevel.h"
  112. #endif
  113. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  114. #include "feature/pause.h"
  115. #endif
  116. #if ENABLED(POWER_LOSS_RECOVERY)
  117. #include "feature/power_loss_recovery.h"
  118. #endif
  119. #if ENABLED(CANCEL_OBJECTS)
  120. #include "feature/cancel_object.h"
  121. #endif
  122. #if HAS_FILAMENT_SENSOR
  123. #include "feature/runout.h"
  124. #endif
  125. #if ENABLED(TEMP_STAT_LEDS)
  126. #include "feature/leds/tempstat.h"
  127. #endif
  128. #if HAS_CASE_LIGHT
  129. #include "feature/caselight.h"
  130. #endif
  131. #if HAS_FANMUX
  132. #include "feature/fanmux.h"
  133. #endif
  134. #if DO_SWITCH_EXTRUDER || ANY(SWITCHING_NOZZLE, PARKING_EXTRUDER, MAGNETIC_PARKING_EXTRUDER, ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  135. #include "module/tool_change.h"
  136. #endif
  137. #if ENABLED(USE_CONTROLLER_FAN)
  138. #include "feature/controllerfan.h"
  139. #endif
  140. #if ENABLED(PRUSA_MMU2)
  141. #include "feature/prusa_MMU2/mmu2.h"
  142. #endif
  143. #if HAS_L64XX
  144. #include "libs/L64XX/L64XX_Marlin.h"
  145. #endif
  146. const char NUL_STR[] PROGMEM = "",
  147. M112_KILL_STR[] PROGMEM = "M112 Shutdown",
  148. G28_STR[] PROGMEM = "G28",
  149. M21_STR[] PROGMEM = "M21",
  150. M23_STR[] PROGMEM = "M23 %s",
  151. M24_STR[] PROGMEM = "M24",
  152. SP_P_STR[] PROGMEM = " P",
  153. SP_T_STR[] PROGMEM = " T",
  154. SP_X_STR[] PROGMEM = " X",
  155. SP_Y_STR[] PROGMEM = " Y",
  156. SP_Z_STR[] PROGMEM = " Z",
  157. SP_E_STR[] PROGMEM = " E";
  158. bool Running = true;
  159. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  160. bool wait_for_heatup = true;
  161. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  162. #if HAS_RESUME_CONTINUE
  163. bool wait_for_user; // = false;
  164. #endif
  165. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  166. bool suspend_auto_report; // = false
  167. #endif
  168. // Inactivity shutdown
  169. millis_t max_inactive_time, // = 0
  170. stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
  171. #if PIN_EXISTS(CHDK)
  172. extern millis_t chdk_timeout;
  173. #endif
  174. #if ENABLED(I2C_POSITION_ENCODERS)
  175. I2CPositionEncodersMgr I2CPEM;
  176. #endif
  177. /**
  178. * ***************************************************************************
  179. * ******************************** FUNCTIONS ********************************
  180. * ***************************************************************************
  181. */
  182. void setup_killpin() {
  183. #if HAS_KILL
  184. SET_INPUT_PULLUP(KILL_PIN);
  185. #endif
  186. }
  187. void setup_powerhold() {
  188. #if HAS_SUICIDE
  189. OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
  190. #endif
  191. #if ENABLED(PSU_CONTROL)
  192. #if ENABLED(PSU_DEFAULT_OFF)
  193. powersupply_on = true; PSU_OFF();
  194. #else
  195. powersupply_on = false; PSU_ON();
  196. #endif
  197. #endif
  198. }
  199. /**
  200. * Stepper Reset (RigidBoard, et.al.)
  201. */
  202. #if HAS_STEPPER_RESET
  203. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  204. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  205. #endif
  206. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  207. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  208. i2c.receive(bytes);
  209. }
  210. void i2c_on_request() { // just send dummy data for now
  211. i2c.reply("Hello World!\n");
  212. }
  213. #endif
  214. /**
  215. * Sensitive pin test for M42, M226
  216. */
  217. #include "pins/sensitive_pins.h"
  218. bool pin_is_protected(const pin_t pin) {
  219. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  220. for (uint8_t i = 0; i < COUNT(sensitive_pins); i++) {
  221. pin_t sensitive_pin;
  222. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  223. if (pin == sensitive_pin) return true;
  224. }
  225. return false;
  226. }
  227. void protected_pin_err() {
  228. SERIAL_ERROR_MSG(MSG_ERR_PROTECTED_PIN);
  229. }
  230. void quickstop_stepper() {
  231. planner.quick_stop();
  232. planner.synchronize();
  233. set_current_from_steppers_for_axis(ALL_AXES);
  234. sync_plan_position();
  235. }
  236. void enable_e_steppers() {
  237. #define _ENA_E(N) ENABLE_AXIS_E##N();
  238. REPEAT(E_STEPPERS, _ENA_E)
  239. }
  240. void enable_all_steppers() {
  241. #if ENABLED(AUTO_POWER_CONTROL)
  242. powerManager.power_on();
  243. #endif
  244. ENABLE_AXIS_X();
  245. ENABLE_AXIS_Y();
  246. ENABLE_AXIS_Z();
  247. enable_e_steppers();
  248. }
  249. void disable_e_steppers() {
  250. #define _DIS_E(N) DISABLE_AXIS_E##N();
  251. REPEAT(E_STEPPERS, _DIS_E)
  252. }
  253. void disable_e_stepper(const uint8_t e) {
  254. #define _CASE_DIS_E(N) case N: DISABLE_AXIS_E##N(); break;
  255. switch (e) {
  256. REPEAT(EXTRUDERS, _CASE_DIS_E)
  257. }
  258. }
  259. void disable_all_steppers() {
  260. DISABLE_AXIS_X();
  261. DISABLE_AXIS_Y();
  262. DISABLE_AXIS_Z();
  263. disable_e_steppers();
  264. }
  265. #if ENABLED(G29_RETRY_AND_RECOVER)
  266. void event_probe_failure() {
  267. #ifdef ACTION_ON_G29_FAILURE
  268. host_action(PSTR(ACTION_ON_G29_FAILURE));
  269. #endif
  270. #ifdef G29_FAILURE_COMMANDS
  271. gcode.process_subcommands_now_P(PSTR(G29_FAILURE_COMMANDS));
  272. #endif
  273. #if ENABLED(G29_HALT_ON_FAILURE)
  274. #ifdef ACTION_ON_CANCEL
  275. host_action_cancel();
  276. #endif
  277. kill(GET_TEXT(MSG_LCD_PROBING_FAILED));
  278. #endif
  279. }
  280. void event_probe_recover() {
  281. #if ENABLED(HOST_PROMPT_SUPPORT)
  282. host_prompt_do(PROMPT_INFO, PSTR("G29 Retrying"), DISMISS_STR);
  283. #endif
  284. #ifdef ACTION_ON_G29_RECOVER
  285. host_action(PSTR(ACTION_ON_G29_RECOVER));
  286. #endif
  287. #ifdef G29_RECOVER_COMMANDS
  288. gcode.process_subcommands_now_P(PSTR(G29_RECOVER_COMMANDS));
  289. #endif
  290. }
  291. #endif
  292. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  293. #include "feature/pause.h"
  294. #else
  295. constexpr bool did_pause_print = false;
  296. #endif
  297. /**
  298. * Printing is active when the print job timer is running
  299. */
  300. bool printingIsActive() {
  301. return !did_pause_print && (print_job_timer.isRunning() || IS_SD_PRINTING());
  302. }
  303. /**
  304. * Printing is paused according to SD or host indicators
  305. */
  306. bool printingIsPaused() {
  307. return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
  308. }
  309. void startOrResumeJob() {
  310. if (!printingIsPaused()) {
  311. #if ENABLED(CANCEL_OBJECTS)
  312. cancelable.reset();
  313. #endif
  314. #if ENABLED(LCD_SHOW_E_TOTAL)
  315. e_move_accumulator = 0;
  316. #endif
  317. #if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
  318. ui.reset_remaining_time();
  319. #endif
  320. }
  321. print_job_timer.start();
  322. }
  323. #if ENABLED(SDSUPPORT)
  324. void abortSDPrinting() {
  325. card.stopSDPrint(
  326. #if SD_RESORT
  327. true
  328. #endif
  329. );
  330. queue.clear();
  331. quickstop_stepper();
  332. print_job_timer.stop();
  333. #if DISABLED(SD_ABORT_NO_COOLDOWN)
  334. thermalManager.disable_all_heaters();
  335. #endif
  336. thermalManager.zero_fan_speeds();
  337. wait_for_heatup = false;
  338. #if ENABLED(POWER_LOSS_RECOVERY)
  339. recovery.purge();
  340. #endif
  341. #ifdef EVENT_GCODE_SD_STOP
  342. queue.inject_P(PSTR(EVENT_GCODE_SD_STOP));
  343. #endif
  344. }
  345. #endif
  346. /**
  347. * Manage several activities:
  348. * - Check for Filament Runout
  349. * - Keep the command buffer full
  350. * - Check for maximum inactive time between commands
  351. * - Check for maximum inactive time between stepper commands
  352. * - Check if CHDK_PIN needs to go LOW
  353. * - Check for KILL button held down
  354. * - Check for HOME button held down
  355. * - Check if cooling fan needs to be switched on
  356. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  357. * - Pulse FET_SAFETY_PIN if it exists
  358. */
  359. void manage_inactivity(const bool ignore_stepper_queue/*=false*/) {
  360. #if HAS_FILAMENT_SENSOR
  361. runout.run();
  362. #endif
  363. if (queue.length < BUFSIZE) queue.get_available_commands();
  364. const millis_t ms = millis();
  365. if (max_inactive_time && ELAPSED(ms, gcode.previous_move_ms + max_inactive_time)) {
  366. SERIAL_ERROR_START();
  367. SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
  368. kill();
  369. }
  370. // Prevent steppers timing-out in the middle of M600
  371. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  372. #define MOVE_AWAY_TEST !did_pause_print
  373. #else
  374. #define MOVE_AWAY_TEST true
  375. #endif
  376. if (stepper_inactive_time) {
  377. static bool already_shutdown_steppers; // = false
  378. if (planner.has_blocks_queued())
  379. gcode.reset_stepper_timeout();
  380. else if (MOVE_AWAY_TEST && !ignore_stepper_queue && ELAPSED(ms, gcode.previous_move_ms + stepper_inactive_time)) {
  381. if (!already_shutdown_steppers) {
  382. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  383. #if ENABLED(DISABLE_INACTIVE_X)
  384. DISABLE_AXIS_X();
  385. #endif
  386. #if ENABLED(DISABLE_INACTIVE_Y)
  387. DISABLE_AXIS_Y();
  388. #endif
  389. #if ENABLED(DISABLE_INACTIVE_Z)
  390. DISABLE_AXIS_Z();
  391. #endif
  392. #if ENABLED(DISABLE_INACTIVE_E)
  393. disable_e_steppers();
  394. #endif
  395. #if HAS_LCD_MENU && ENABLED(AUTO_BED_LEVELING_UBL)
  396. if (ubl.lcd_map_control) {
  397. ubl.lcd_map_control = false;
  398. ui.defer_status_screen(false);
  399. }
  400. #endif
  401. }
  402. }
  403. else
  404. already_shutdown_steppers = false;
  405. }
  406. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  407. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  408. chdk_timeout = 0;
  409. WRITE(CHDK_PIN, LOW);
  410. }
  411. #endif
  412. #if HAS_KILL
  413. // Check if the kill button was pressed and wait just in case it was an accidental
  414. // key kill key press
  415. // -------------------------------------------------------------------------------
  416. static int killCount = 0; // make the inactivity button a bit less responsive
  417. const int KILL_DELAY = 750;
  418. if (!READ(KILL_PIN))
  419. killCount++;
  420. else if (killCount > 0)
  421. killCount--;
  422. // Exceeded threshold and we can confirm that it was not accidental
  423. // KILL the machine
  424. // ----------------------------------------------------------------
  425. if (killCount >= KILL_DELAY) {
  426. SERIAL_ERROR_MSG(MSG_KILL_BUTTON);
  427. kill();
  428. }
  429. #endif
  430. #if HAS_HOME
  431. // Handle a standalone HOME button
  432. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  433. static millis_t next_home_key_ms; // = 0
  434. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  435. const millis_t ms = millis();
  436. if (ELAPSED(ms, next_home_key_ms)) {
  437. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  438. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  439. queue.enqueue_now_P(G28_STR);
  440. }
  441. }
  442. #endif
  443. #if ENABLED(USE_CONTROLLER_FAN)
  444. controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
  445. #endif
  446. #if ENABLED(AUTO_POWER_CONTROL)
  447. powerManager.check();
  448. #endif
  449. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  450. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  451. && ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  452. && !planner.has_blocks_queued()
  453. ) {
  454. #if ENABLED(SWITCHING_EXTRUDER)
  455. bool oldstatus;
  456. switch (active_extruder) {
  457. default: oldstatus = E0_ENABLE_READ(); ENABLE_AXIS_E0(); break;
  458. #if E_STEPPERS > 1
  459. case 2: case 3: oldstatus = E1_ENABLE_READ(); ENABLE_AXIS_E1(); break;
  460. #if E_STEPPERS > 2
  461. case 4: case 5: oldstatus = E2_ENABLE_READ(); ENABLE_AXIS_E2(); break;
  462. #if E_STEPPERS > 3
  463. case 6: case 7: oldstatus = E3_ENABLE_READ(); ENABLE_AXIS_E3(); break;
  464. #endif // E_STEPPERS > 3
  465. #endif // E_STEPPERS > 2
  466. #endif // E_STEPPERS > 1
  467. }
  468. #else // !SWITCHING_EXTRUDER
  469. bool oldstatus;
  470. switch (active_extruder) {
  471. default:
  472. #define _CASE_EN(N) case N: oldstatus = E##N##_ENABLE_READ(); ENABLE_AXIS_E##N(); break;
  473. REPEAT(E_STEPPERS, _CASE_EN);
  474. }
  475. #endif
  476. const float olde = current_position.e;
  477. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  478. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  479. current_position.e = olde;
  480. planner.set_e_position_mm(olde);
  481. planner.synchronize();
  482. #if ENABLED(SWITCHING_EXTRUDER)
  483. switch (active_extruder) {
  484. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  485. #if E_STEPPERS > 1
  486. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  487. #if E_STEPPERS > 2
  488. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  489. #endif // E_STEPPERS > 2
  490. #endif // E_STEPPERS > 1
  491. }
  492. #else // !SWITCHING_EXTRUDER
  493. switch (active_extruder) {
  494. #define _CASE_RESTORE(N) case N: E##N##_ENABLE_WRITE(oldstatus); break;
  495. REPEAT(E_STEPPERS, _CASE_RESTORE);
  496. }
  497. #endif // !SWITCHING_EXTRUDER
  498. gcode.reset_stepper_timeout();
  499. }
  500. #endif // EXTRUDER_RUNOUT_PREVENT
  501. #if ENABLED(DUAL_X_CARRIAGE)
  502. // handle delayed move timeout
  503. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  504. // travel moves have been received so enact them
  505. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  506. destination = current_position;
  507. prepare_move_to_destination();
  508. }
  509. #endif
  510. #if ENABLED(TEMP_STAT_LEDS)
  511. handle_status_leds();
  512. #endif
  513. #if ENABLED(MONITOR_DRIVER_STATUS)
  514. monitor_tmc_drivers();
  515. #endif
  516. #if ENABLED(MONITOR_L6470_DRIVER_STATUS)
  517. L64xxManager.monitor_driver();
  518. #endif
  519. // Limit check_axes_activity frequency to 10Hz
  520. static millis_t next_check_axes_ms = 0;
  521. if (ELAPSED(ms, next_check_axes_ms)) {
  522. planner.check_axes_activity();
  523. next_check_axes_ms = ms + 100UL;
  524. }
  525. #if PIN_EXISTS(FET_SAFETY)
  526. static millis_t FET_next;
  527. if (ELAPSED(ms, FET_next)) {
  528. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  529. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  530. DELAY_US(2);
  531. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  532. }
  533. #endif
  534. }
  535. /**
  536. * Standard idle routine keeps the machine alive
  537. */
  538. void idle(
  539. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  540. bool no_stepper_sleep/*=false*/
  541. #endif
  542. ) {
  543. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  544. recovery.outage();
  545. #endif
  546. #if ENABLED(SPI_ENDSTOPS)
  547. if (endstops.tmc_spi_homing.any
  548. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  549. && ELAPSED(millis(), sg_guard_period)
  550. #endif
  551. ) {
  552. for (uint8_t i = 4; i--;) // Read SGT 4 times per idle loop
  553. if (endstops.tmc_spi_homing_check()) break;
  554. }
  555. #endif
  556. #if ENABLED(MAX7219_DEBUG)
  557. max7219.idle_tasks();
  558. #endif
  559. ui.update();
  560. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  561. gcode.host_keepalive();
  562. #endif
  563. manage_inactivity(
  564. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  565. no_stepper_sleep
  566. #endif
  567. );
  568. thermalManager.manage_heater();
  569. #if ENABLED(PRINTCOUNTER)
  570. print_job_timer.tick();
  571. #endif
  572. #if USE_BEEPER
  573. buzzer.tick();
  574. #endif
  575. #if ENABLED(I2C_POSITION_ENCODERS)
  576. static millis_t i2cpem_next_update_ms;
  577. if (planner.has_blocks_queued()) {
  578. const millis_t ms = millis();
  579. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  580. I2CPEM.update();
  581. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  582. }
  583. }
  584. #endif
  585. #ifdef HAL_IDLETASK
  586. HAL_idletask();
  587. #endif
  588. #if HAS_AUTO_REPORTING
  589. if (!suspend_auto_report) {
  590. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  591. thermalManager.auto_report_temperatures();
  592. #endif
  593. #if ENABLED(AUTO_REPORT_SD_STATUS)
  594. card.auto_report_sd_status();
  595. #endif
  596. }
  597. #endif
  598. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  599. Sd2Card::idle();
  600. #endif
  601. #if ENABLED(PRUSA_MMU2)
  602. mmu2.mmu_loop();
  603. #endif
  604. #if ENABLED(POLL_JOG)
  605. joystick.inject_jog_moves();
  606. #endif
  607. }
  608. /**
  609. * Kill all activity and lock the machine.
  610. * After this the machine will need to be reset.
  611. */
  612. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  613. thermalManager.disable_all_heaters();
  614. SERIAL_ERROR_MSG(MSG_ERR_KILLED);
  615. #if HAS_DISPLAY
  616. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component ?: NUL_STR);
  617. #else
  618. UNUSED(lcd_error);
  619. UNUSED(lcd_component);
  620. #endif
  621. #ifdef ACTION_ON_KILL
  622. host_action_kill();
  623. #endif
  624. minkill(steppers_off);
  625. }
  626. void minkill(const bool steppers_off/*=false*/) {
  627. // Wait a short time (allows messages to get out before shutting down.
  628. for (int i = 1000; i--;) DELAY_US(600);
  629. cli(); // Stop interrupts
  630. // Wait to ensure all interrupts stopped
  631. for (int i = 1000; i--;) DELAY_US(250);
  632. // Reiterate heaters off
  633. thermalManager.disable_all_heaters();
  634. // Power off all steppers (for M112) or just the E steppers
  635. steppers_off ? disable_all_steppers() : disable_e_steppers();
  636. #if ENABLED(PSU_CONTROL)
  637. PSU_OFF();
  638. #endif
  639. #if HAS_SUICIDE
  640. suicide();
  641. #endif
  642. #if HAS_KILL
  643. // Wait for kill to be released
  644. while (!READ(KILL_PIN)) watchdog_refresh();
  645. // Wait for kill to be pressed
  646. while (READ(KILL_PIN)) watchdog_refresh();
  647. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  648. resetFunc(); // Jump to address 0
  649. #else // !HAS_KILL
  650. for (;;) watchdog_refresh(); // Wait for reset
  651. #endif // !HAS_KILL
  652. }
  653. /**
  654. * Turn off heaters and stop the print in progress
  655. * After a stop the machine may be resumed with M999
  656. */
  657. void stop() {
  658. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  659. print_job_timer.stop();
  660. #if ENABLED(PROBING_FANS_OFF)
  661. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  662. #endif
  663. if (IsRunning()) {
  664. SERIAL_ERROR_MSG(MSG_ERR_STOPPED);
  665. LCD_MESSAGEPGM(MSG_STOPPED);
  666. safe_delay(350); // allow enough time for messages to get out before stopping
  667. Running = false;
  668. }
  669. }
  670. /**
  671. * Marlin entry-point: Set up before the program loop
  672. * - Set up the kill pin, filament runout, power hold
  673. * - Start the serial port
  674. * - Print startup messages and diagnostics
  675. * - Get EEPROM or default settings
  676. * - Initialize managers for:
  677. * • temperature
  678. * • planner
  679. * • watchdog
  680. * • stepper
  681. * • photo pin
  682. * • servos
  683. * • LCD controller
  684. * • Digipot I2C
  685. * • Z probe sled
  686. * • status LEDs
  687. */
  688. void setup() {
  689. HAL_init();
  690. #if HAS_L64XX
  691. L64xxManager.init(); // Set up SPI, init drivers
  692. #endif
  693. #if ENABLED(SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  694. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  695. #endif
  696. #if ENABLED(MAX7219_DEBUG)
  697. max7219.init();
  698. #endif
  699. #if ENABLED(DISABLE_DEBUG)
  700. // Disable any hardware debug to free up pins for IO
  701. #ifdef JTAGSWD_DISABLE
  702. JTAGSWD_DISABLE();
  703. #elif defined(JTAG_DISABLE)
  704. JTAG_DISABLE();
  705. #else
  706. #error "DISABLE_DEBUG is not supported for the selected MCU/Board"
  707. #endif
  708. #elif ENABLED(DISABLE_JTAG)
  709. // Disable JTAG to free up pins for IO
  710. #ifdef JTAG_DISABLE
  711. JTAG_DISABLE();
  712. #else
  713. #error "DISABLE_JTAG is not supported for the selected MCU/Board"
  714. #endif
  715. #endif
  716. #if HAS_FILAMENT_SENSOR
  717. runout.setup();
  718. #endif
  719. #if ENABLED(POWER_LOSS_RECOVERY)
  720. recovery.setup();
  721. #endif
  722. setup_killpin();
  723. #if HAS_TMC220x
  724. tmc_serial_begin();
  725. #endif
  726. setup_powerhold();
  727. #if HAS_STEPPER_RESET
  728. disableStepperDrivers();
  729. #endif
  730. #if NUM_SERIAL > 0
  731. MYSERIAL0.begin(BAUDRATE);
  732. uint32_t serial_connect_timeout = millis() + 1000UL;
  733. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  734. #if NUM_SERIAL > 1
  735. MYSERIAL1.begin(BAUDRATE);
  736. serial_connect_timeout = millis() + 1000UL;
  737. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  738. #endif
  739. #endif
  740. SERIAL_ECHOLNPGM("start");
  741. SERIAL_ECHO_START();
  742. #if HAS_TMC_SPI
  743. #if DISABLED(TMC_USE_SW_SPI)
  744. SPI.begin();
  745. #endif
  746. tmc_init_cs_pins();
  747. #endif
  748. #ifdef BOARD_INIT
  749. BOARD_INIT();
  750. #endif
  751. // Check startup - does nothing if bootloader sets MCUSR to 0
  752. byte mcu = HAL_get_reset_source();
  753. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  754. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  755. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  756. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  757. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  758. HAL_clear_reset_source();
  759. serialprintPGM(GET_TEXT(MSG_MARLIN));
  760. SERIAL_CHAR(' ');
  761. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  762. SERIAL_EOL();
  763. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  764. SERIAL_ECHO_MSG(
  765. MSG_CONFIGURATION_VER
  766. STRING_DISTRIBUTION_DATE
  767. MSG_AUTHOR STRING_CONFIG_H_AUTHOR
  768. );
  769. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  770. #endif
  771. SERIAL_ECHO_START();
  772. SERIAL_ECHOLNPAIR(MSG_FREE_MEMORY, freeMemory(), MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  773. // UI must be initialized before EEPROM
  774. // (because EEPROM code calls the UI).
  775. // Set up LEDs early
  776. #if HAS_COLOR_LEDS
  777. leds.setup();
  778. #endif
  779. ui.init();
  780. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  781. ui.show_bootscreen();
  782. #endif
  783. #if ENABLED(SDSUPPORT)
  784. card.mount(); // Mount the SD card before settings.first_load
  785. #endif
  786. // Load data from EEPROM if available (or use defaults)
  787. // This also updates variables in the planner, elsewhere
  788. settings.first_load();
  789. #if ENABLED(TOUCH_BUTTONS)
  790. touch.init();
  791. #endif
  792. #if HAS_M206_COMMAND
  793. // Initialize current position based on home_offset
  794. current_position += home_offset;
  795. #endif
  796. // Vital to init stepper/planner equivalent for current_position
  797. sync_plan_position();
  798. thermalManager.init(); // Initialize temperature loop
  799. print_job_timer.init(); // Initial setup of print job timer
  800. ui.reset_status(); // Print startup message after print statistics are loaded
  801. endstops.init(); // Init endstops and pullups
  802. stepper.init(); // Init stepper. This enables interrupts!
  803. #if HAS_SERVOS
  804. servo_init();
  805. #endif
  806. #if HAS_Z_SERVO_PROBE
  807. probe.servo_probe_init();
  808. #endif
  809. #if HAS_PHOTOGRAPH
  810. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  811. #endif
  812. #if HAS_CUTTER
  813. cutter.init();
  814. #endif
  815. #if ENABLED(COOLANT_MIST)
  816. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  817. #endif
  818. #if ENABLED(COOLANT_FLOOD)
  819. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  820. #endif
  821. #if HAS_BED_PROBE
  822. endstops.enable_z_probe(false);
  823. #endif
  824. #if ENABLED(USE_CONTROLLER_FAN)
  825. SET_OUTPUT(CONTROLLER_FAN_PIN);
  826. #endif
  827. #if HAS_STEPPER_RESET
  828. enableStepperDrivers();
  829. #endif
  830. #if ENABLED(DIGIPOT_I2C)
  831. digipot_i2c_init();
  832. #endif
  833. #if ENABLED(DAC_STEPPER_CURRENT)
  834. dac_init();
  835. #endif
  836. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  837. OUT_WRITE(SOL1_PIN, LOW); // OFF
  838. #endif
  839. #if HAS_HOME
  840. SET_INPUT_PULLUP(HOME_PIN);
  841. #endif
  842. #if PIN_EXISTS(STAT_LED_RED)
  843. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  844. #endif
  845. #if PIN_EXISTS(STAT_LED_BLUE)
  846. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  847. #endif
  848. #if HAS_CASE_LIGHT
  849. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  850. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  851. #endif
  852. update_case_light();
  853. #endif
  854. #if ENABLED(MK2_MULTIPLEXER)
  855. SET_OUTPUT(E_MUX0_PIN);
  856. SET_OUTPUT(E_MUX1_PIN);
  857. SET_OUTPUT(E_MUX2_PIN);
  858. #endif
  859. #if HAS_FANMUX
  860. fanmux_init();
  861. #endif
  862. #if ENABLED(MIXING_EXTRUDER)
  863. mixer.init();
  864. #endif
  865. #if ENABLED(BLTOUCH)
  866. bltouch.init(/*set_voltage=*/true);
  867. #endif
  868. #if ENABLED(I2C_POSITION_ENCODERS)
  869. I2CPEM.init();
  870. #endif
  871. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  872. i2c.onReceive(i2c_on_receive);
  873. i2c.onRequest(i2c_on_request);
  874. #endif
  875. #if DO_SWITCH_EXTRUDER
  876. move_extruder_servo(0); // Initialize extruder servo
  877. #endif
  878. #if ENABLED(SWITCHING_NOZZLE)
  879. // Initialize nozzle servo(s)
  880. #if SWITCHING_NOZZLE_TWO_SERVOS
  881. lower_nozzle(0);
  882. raise_nozzle(1);
  883. #else
  884. move_nozzle_servo(0);
  885. #endif
  886. #endif
  887. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  888. mpe_settings_init();
  889. #endif
  890. #if ENABLED(PARKING_EXTRUDER)
  891. pe_solenoid_init();
  892. #endif
  893. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  894. est_init();
  895. #endif
  896. #if ENABLED(POWER_LOSS_RECOVERY)
  897. recovery.check();
  898. #endif
  899. #if ENABLED(USE_WATCHDOG)
  900. watchdog_init(); // Reinit watchdog after HAL_get_reset_source call
  901. #endif
  902. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  903. init_closedloop();
  904. #endif
  905. #ifdef STARTUP_COMMANDS
  906. queue.inject_P(PSTR(STARTUP_COMMANDS));
  907. #endif
  908. #if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
  909. card.beginautostart();
  910. #endif
  911. #if ENABLED(HOST_PROMPT_SUPPORT)
  912. host_action_prompt_end();
  913. #endif
  914. #if HAS_TRINAMIC && DISABLED(PSU_DEFAULT_OFF)
  915. test_tmc_connection(true, true, true, true);
  916. #endif
  917. #if ENABLED(PRUSA_MMU2)
  918. mmu2.init();
  919. #endif
  920. }
  921. /**
  922. * The main Marlin program loop
  923. *
  924. * - Save or log commands to SD
  925. * - Process available commands (if not saving)
  926. * - Call endstop manager
  927. * - Call inactivity manager
  928. */
  929. void loop() {
  930. do {
  931. idle();
  932. #if ENABLED(SDSUPPORT)
  933. card.checkautostart();
  934. if (card.flag.abort_sd_printing) abortSDPrinting();
  935. #endif
  936. queue.advance();
  937. endstops.event_handler();
  938. } while (false // Return to caller for best compatibility
  939. #ifdef __AVR__
  940. || true // Loop forever on slower (AVR) boards
  941. #endif
  942. );
  943. }