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

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