My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin.cpp 29KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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 "lcd/ultralcd.h"
  31. #include "module/motion.h"
  32. #include "module/planner.h"
  33. #include "module/stepper.h"
  34. #include "module/endstops.h"
  35. #include "module/probe.h"
  36. #include "module/temperature.h"
  37. #include "sd/cardreader.h"
  38. #include "module/configuration_store.h"
  39. #include "module/printcounter.h" // PrintCounter or Stopwatch
  40. #include "feature/closedloop.h"
  41. #include "HAL/shared/Delay.h"
  42. #include "module/stepper_indirection.h"
  43. #ifdef ARDUINO
  44. #include <pins_arduino.h>
  45. #endif
  46. #include <math.h>
  47. #include "libs/nozzle.h"
  48. #include "gcode/gcode.h"
  49. #include "gcode/parser.h"
  50. #include "gcode/queue.h"
  51. #if ENABLED(HOST_ACTION_COMMANDS)
  52. #include "feature/host_actions.h"
  53. #endif
  54. #if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
  55. #include "libs/buzzer.h"
  56. #endif
  57. #if ENABLED(DIGIPOT_I2C)
  58. #include "feature/digipot/digipot.h"
  59. #endif
  60. #if ENABLED(MIXING_EXTRUDER)
  61. #include "feature/mixing.h"
  62. #endif
  63. #if ENABLED(BEZIER_CURVE_SUPPORT)
  64. #include "module/planner_bezier.h"
  65. #endif
  66. #if ENABLED(MAX7219_DEBUG)
  67. #include "feature/Max7219_Debug_LEDs.h"
  68. #endif
  69. #if HAS_COLOR_LEDS
  70. #include "feature/leds/leds.h"
  71. #endif
  72. #if ENABLED(BLTOUCH)
  73. #include "feature/bltouch.h"
  74. #endif
  75. #if HAS_SERVOS
  76. #include "module/servo.h"
  77. #endif
  78. #if HAS_DIGIPOTSS
  79. #include <SPI.h>
  80. #endif
  81. #if ENABLED(DAC_STEPPER_CURRENT)
  82. #include "feature/dac/stepper_dac.h"
  83. #endif
  84. #if ENABLED(EXPERIMENTAL_I2CBUS)
  85. #include "feature/twibus.h"
  86. TWIBus i2c;
  87. #endif
  88. #if ENABLED(I2C_POSITION_ENCODERS)
  89. #include "feature/I2CPositionEncoder.h"
  90. #endif
  91. #if HAS_TRINAMIC
  92. #include "feature/tmc_util.h"
  93. #endif
  94. #if ENABLED(SDSUPPORT)
  95. CardReader card;
  96. #endif
  97. #if ENABLED(G38_PROBE_TARGET)
  98. uint8_t G38_move; // = 0
  99. bool G38_did_trigger; // = false
  100. #endif
  101. #if ENABLED(DELTA)
  102. #include "module/delta.h"
  103. #elif IS_SCARA
  104. #include "module/scara.h"
  105. #endif
  106. #if HAS_LEVELING
  107. #include "feature/bedlevel/bedlevel.h"
  108. #endif
  109. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  110. #include "feature/pause.h"
  111. #endif
  112. #if ENABLED(POWER_LOSS_RECOVERY)
  113. #include "feature/power_loss_recovery.h"
  114. #endif
  115. #if HAS_FILAMENT_SENSOR
  116. #include "feature/runout.h"
  117. #endif
  118. #if ENABLED(TEMP_STAT_LEDS)
  119. #include "feature/leds/tempstat.h"
  120. #endif
  121. #if HAS_CASE_LIGHT
  122. #include "feature/caselight.h"
  123. #endif
  124. #if HAS_FANMUX
  125. #include "feature/fanmux.h"
  126. #endif
  127. #if DO_SWITCH_EXTRUDER || ANY(SWITCHING_NOZZLE, PARKING_EXTRUDER, MAGNETIC_PARKING_EXTRUDER)
  128. #include "module/tool_change.h"
  129. #endif
  130. #if ENABLED(USE_CONTROLLER_FAN)
  131. #include "feature/controllerfan.h"
  132. #endif
  133. #if ENABLED(PRUSA_MMU2)
  134. #include "feature/prusa_MMU2/mmu2.h"
  135. #endif
  136. #if ENABLED(EXTENSIBLE_UI)
  137. #include "lcd/extensible_ui/ui_api.h"
  138. #endif
  139. #if HAS_DRIVER(L6470)
  140. #include "libs/L6470/L6470_Marlin.h"
  141. #endif
  142. bool Running = true;
  143. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  144. TempUnit input_temp_units = TEMPUNIT_C;
  145. #endif
  146. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  147. bool wait_for_heatup = true;
  148. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  149. #if HAS_RESUME_CONTINUE
  150. bool wait_for_user; // = false;
  151. #endif
  152. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  153. bool suspend_auto_report; // = false
  154. #endif
  155. // Inactivity shutdown
  156. millis_t max_inactive_time, // = 0
  157. stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
  158. #if PIN_EXISTS(CHDK)
  159. extern millis_t chdk_timeout;
  160. #endif
  161. #if ENABLED(I2C_POSITION_ENCODERS)
  162. I2CPositionEncodersMgr I2CPEM;
  163. #endif
  164. /**
  165. * ***************************************************************************
  166. * ******************************** FUNCTIONS ********************************
  167. * ***************************************************************************
  168. */
  169. void setup_killpin() {
  170. #if HAS_KILL
  171. SET_INPUT_PULLUP(KILL_PIN);
  172. #endif
  173. }
  174. void setup_powerhold() {
  175. #if HAS_SUICIDE
  176. OUT_WRITE(SUICIDE_PIN, HIGH);
  177. #endif
  178. #if HAS_POWER_SWITCH
  179. #if ENABLED(PS_DEFAULT_OFF)
  180. powersupply_on = true; PSU_OFF();
  181. #else
  182. powersupply_on = false; PSU_ON();
  183. #endif
  184. #endif
  185. }
  186. /**
  187. * Stepper Reset (RigidBoard, et.al.)
  188. */
  189. #if HAS_STEPPER_RESET
  190. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  191. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  192. #endif
  193. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  194. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  195. i2c.receive(bytes);
  196. }
  197. void i2c_on_request() { // just send dummy data for now
  198. i2c.reply("Hello World!\n");
  199. }
  200. #endif
  201. /**
  202. * Sensitive pin test for M42, M226
  203. */
  204. #include "pins/sensitive_pins.h"
  205. bool pin_is_protected(const pin_t pin) {
  206. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  207. for (uint8_t i = 0; i < COUNT(sensitive_pins); i++) {
  208. pin_t sensitive_pin;
  209. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  210. if (pin == sensitive_pin) return true;
  211. }
  212. return false;
  213. }
  214. void protected_pin_err() {
  215. SERIAL_ERROR_MSG(MSG_ERR_PROTECTED_PIN);
  216. }
  217. void quickstop_stepper() {
  218. planner.quick_stop();
  219. planner.synchronize();
  220. set_current_from_steppers_for_axis(ALL_AXES);
  221. sync_plan_position();
  222. }
  223. void enable_all_steppers() {
  224. #if ENABLED(AUTO_POWER_CONTROL)
  225. powerManager.power_on();
  226. #endif
  227. enable_X();
  228. enable_Y();
  229. enable_Z();
  230. enable_E0();
  231. enable_E1();
  232. enable_E2();
  233. enable_E3();
  234. enable_E4();
  235. enable_E5();
  236. }
  237. void disable_e_steppers() {
  238. disable_E0();
  239. disable_E1();
  240. disable_E2();
  241. disable_E3();
  242. disable_E4();
  243. disable_E5();
  244. }
  245. void disable_e_stepper(const uint8_t e) {
  246. switch (e) {
  247. case 0: disable_E0(); break;
  248. case 1: disable_E1(); break;
  249. case 2: disable_E2(); break;
  250. case 3: disable_E3(); break;
  251. case 4: disable_E4(); break;
  252. case 5: disable_E5(); break;
  253. }
  254. }
  255. void disable_all_steppers() {
  256. disable_X();
  257. disable_Y();
  258. disable_Z();
  259. disable_e_steppers();
  260. }
  261. #if HAS_FILAMENT_SENSOR
  262. void event_filament_runout() {
  263. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  264. if (did_pause_print) return; // Action already in progress. Purge triggered repeated runout.
  265. #endif
  266. #if ENABLED(EXTENSIBLE_UI)
  267. ExtUI::onFilamentRunout(ExtUI::getActiveTool());
  268. #endif
  269. #if EITHER(HOST_PROMPT_SUPPORT, HOST_ACTION_COMMANDS)
  270. const char tool = '0'
  271. #if NUM_RUNOUT_SENSORS > 1
  272. + active_extruder
  273. #endif
  274. ;
  275. #endif
  276. //action:out_of_filament
  277. #if ENABLED(HOST_PROMPT_SUPPORT)
  278. host_prompt_reason = PROMPT_FILAMENT_RUNOUT;
  279. host_action_prompt_end();
  280. host_action_prompt_begin(PSTR("FilamentRunout T"), false);
  281. SERIAL_CHAR(tool);
  282. SERIAL_EOL();
  283. host_action_prompt_show();
  284. #endif
  285. const bool run_runout_script = !runout.host_handling;
  286. #if ENABLED(HOST_ACTION_COMMANDS)
  287. if (run_runout_script
  288. && ( strstr(FILAMENT_RUNOUT_SCRIPT, "M600")
  289. || strstr(FILAMENT_RUNOUT_SCRIPT, "M125")
  290. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  291. || strstr(FILAMENT_RUNOUT_SCRIPT, "M25")
  292. #endif
  293. )
  294. ) {
  295. host_action_paused(false);
  296. }
  297. else {
  298. // Legacy Repetier command for use until newer version supports standard dialog
  299. // To be removed later when pause command also triggers dialog
  300. #ifdef ACTION_ON_FILAMENT_RUNOUT
  301. host_action(PSTR(ACTION_ON_FILAMENT_RUNOUT " T"), false);
  302. SERIAL_CHAR(tool);
  303. SERIAL_EOL();
  304. #endif
  305. host_action_pause(false);
  306. }
  307. SERIAL_ECHOPGM(" " ACTION_REASON_ON_FILAMENT_RUNOUT " ");
  308. SERIAL_CHAR(tool);
  309. SERIAL_EOL();
  310. #endif // HOST_ACTION_COMMANDS
  311. if (run_runout_script)
  312. enqueue_and_echo_commands_P(PSTR(FILAMENT_RUNOUT_SCRIPT));
  313. }
  314. #endif // HAS_FILAMENT_SENSOR
  315. #if ENABLED(G29_RETRY_AND_RECOVER)
  316. void event_probe_failure() {
  317. #ifdef ACTION_ON_G29_FAILURE
  318. host_action(PSTR(ACTION_ON_G29_FAILURE));
  319. #endif
  320. #ifdef G29_FAILURE_COMMANDS
  321. gcode.process_subcommands_now_P(PSTR(G29_FAILURE_COMMANDS));
  322. #endif
  323. #if ENABLED(G29_HALT_ON_FAILURE)
  324. #ifdef ACTION_ON_CANCEL
  325. host_action_cancel();
  326. #endif
  327. kill(PSTR(MSG_ERR_PROBING_FAILED));
  328. #endif
  329. }
  330. void event_probe_recover() {
  331. #if ENABLED(HOST_PROMPT_SUPPORT)
  332. host_prompt_do(PROMPT_INFO, PSTR("G29 Retrying"));
  333. #endif
  334. #ifdef ACTION_ON_G29_RECOVER
  335. host_action(PSTR(ACTION_ON_G29_RECOVER));
  336. #endif
  337. #ifdef G29_RECOVER_COMMANDS
  338. gcode.process_subcommands_now_P(PSTR(G29_RECOVER_COMMANDS));
  339. #endif
  340. }
  341. #endif
  342. /**
  343. * Manage several activities:
  344. * - Check for Filament Runout
  345. * - Keep the command buffer full
  346. * - Check for maximum inactive time between commands
  347. * - Check for maximum inactive time between stepper commands
  348. * - Check if CHDK_PIN needs to go LOW
  349. * - Check for KILL button held down
  350. * - Check for HOME button held down
  351. * - Check if cooling fan needs to be switched on
  352. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  353. * - Pulse FET_SAFETY_PIN if it exists
  354. */
  355. void manage_inactivity(const bool ignore_stepper_queue/*=false*/) {
  356. #if HAS_FILAMENT_SENSOR
  357. runout.run();
  358. #endif
  359. if (commands_in_queue < BUFSIZE) get_available_commands();
  360. const millis_t ms = millis();
  361. if (max_inactive_time && ELAPSED(ms, gcode.previous_move_ms + max_inactive_time)) {
  362. SERIAL_ERROR_START();
  363. SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
  364. kill();
  365. }
  366. // Prevent steppers timing-out in the middle of M600
  367. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  368. #define MOVE_AWAY_TEST !did_pause_print
  369. #else
  370. #define MOVE_AWAY_TEST true
  371. #endif
  372. if (stepper_inactive_time) {
  373. static bool already_shutdown_steppers; // = false
  374. if (planner.has_blocks_queued())
  375. gcode.previous_move_ms = ms; // reset_stepper_timeout to keep steppers powered
  376. else if (MOVE_AWAY_TEST && !ignore_stepper_queue && ELAPSED(ms, gcode.previous_move_ms + stepper_inactive_time)) {
  377. if (!already_shutdown_steppers) {
  378. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  379. #if ENABLED(DISABLE_INACTIVE_X)
  380. disable_X();
  381. #endif
  382. #if ENABLED(DISABLE_INACTIVE_Y)
  383. disable_Y();
  384. #endif
  385. #if ENABLED(DISABLE_INACTIVE_Z)
  386. disable_Z();
  387. #endif
  388. #if ENABLED(DISABLE_INACTIVE_E)
  389. disable_e_steppers();
  390. #endif
  391. #if HAS_LCD_MENU
  392. ui.status_screen();
  393. #if ENABLED(AUTO_BED_LEVELING_UBL)
  394. if (ubl.lcd_map_control) {
  395. ubl.lcd_map_control = false;
  396. ui.defer_status_screen(false);
  397. }
  398. #endif
  399. #endif
  400. }
  401. }
  402. else
  403. already_shutdown_steppers = false;
  404. }
  405. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  406. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  407. chdk_timeout = 0;
  408. WRITE(CHDK_PIN, LOW);
  409. }
  410. #endif
  411. #if HAS_KILL
  412. // Check if the kill button was pressed and wait just in case it was an accidental
  413. // key kill key press
  414. // -------------------------------------------------------------------------------
  415. static int killCount = 0; // make the inactivity button a bit less responsive
  416. const int KILL_DELAY = 750;
  417. if (!READ(KILL_PIN))
  418. killCount++;
  419. else if (killCount > 0)
  420. killCount--;
  421. // Exceeded threshold and we can confirm that it was not accidental
  422. // KILL the machine
  423. // ----------------------------------------------------------------
  424. if (killCount >= KILL_DELAY) {
  425. SERIAL_ERROR_MSG(MSG_KILL_BUTTON);
  426. kill();
  427. }
  428. #endif
  429. #if HAS_HOME
  430. // Check to see if we have to home, use poor man's debouncer
  431. // ---------------------------------------------------------
  432. static int homeDebounceCount = 0; // poor man's debouncing count
  433. const int HOME_DEBOUNCE_DELAY = 2500;
  434. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) {
  435. if (!homeDebounceCount) {
  436. enqueue_and_echo_commands_P(PSTR("G28"));
  437. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  438. }
  439. if (homeDebounceCount < HOME_DEBOUNCE_DELAY)
  440. homeDebounceCount++;
  441. else
  442. homeDebounceCount = 0;
  443. }
  444. #endif
  445. #if ENABLED(USE_CONTROLLER_FAN)
  446. controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
  447. #endif
  448. #if ENABLED(AUTO_POWER_CONTROL)
  449. powerManager.check();
  450. #endif
  451. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  452. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  453. && ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  454. && !planner.has_blocks_queued()
  455. ) {
  456. #if ENABLED(SWITCHING_EXTRUDER)
  457. bool oldstatus;
  458. switch (active_extruder) {
  459. default: oldstatus = E0_ENABLE_READ; enable_E0(); break;
  460. #if E_STEPPERS > 1
  461. case 2: case 3: oldstatus = E1_ENABLE_READ; enable_E1(); break;
  462. #if E_STEPPERS > 2
  463. case 4: case 5: oldstatus = E2_ENABLE_READ; enable_E2(); break;
  464. #endif // E_STEPPERS > 2
  465. #endif // E_STEPPERS > 1
  466. }
  467. #else // !SWITCHING_EXTRUDER
  468. bool oldstatus;
  469. switch (active_extruder) {
  470. default: oldstatus = E0_ENABLE_READ; enable_E0(); break;
  471. #if E_STEPPERS > 1
  472. case 1: oldstatus = E1_ENABLE_READ; enable_E1(); break;
  473. #if E_STEPPERS > 2
  474. case 2: oldstatus = E2_ENABLE_READ; enable_E2(); break;
  475. #if E_STEPPERS > 3
  476. case 3: oldstatus = E3_ENABLE_READ; enable_E3(); break;
  477. #if E_STEPPERS > 4
  478. case 4: oldstatus = E4_ENABLE_READ; enable_E4(); break;
  479. #if E_STEPPERS > 5
  480. case 5: oldstatus = E5_ENABLE_READ; enable_E5(); break;
  481. #endif // E_STEPPERS > 5
  482. #endif // E_STEPPERS > 4
  483. #endif // E_STEPPERS > 3
  484. #endif // E_STEPPERS > 2
  485. #endif // E_STEPPERS > 1
  486. }
  487. #endif // !SWITCHING_EXTRUDER
  488. const float olde = current_position[E_AXIS];
  489. current_position[E_AXIS] += EXTRUDER_RUNOUT_EXTRUDE;
  490. planner.buffer_line(current_position, MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED), active_extruder);
  491. current_position[E_AXIS] = olde;
  492. planner.set_e_position_mm(olde);
  493. planner.synchronize();
  494. #if ENABLED(SWITCHING_EXTRUDER)
  495. switch (active_extruder) {
  496. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  497. #if E_STEPPERS > 1
  498. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  499. #if E_STEPPERS > 2
  500. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  501. #endif // E_STEPPERS > 2
  502. #endif // E_STEPPERS > 1
  503. }
  504. #else // !SWITCHING_EXTRUDER
  505. switch (active_extruder) {
  506. case 0: E0_ENABLE_WRITE(oldstatus); break;
  507. #if E_STEPPERS > 1
  508. case 1: E1_ENABLE_WRITE(oldstatus); break;
  509. #if E_STEPPERS > 2
  510. case 2: E2_ENABLE_WRITE(oldstatus); break;
  511. #if E_STEPPERS > 3
  512. case 3: E3_ENABLE_WRITE(oldstatus); break;
  513. #if E_STEPPERS > 4
  514. case 4: E4_ENABLE_WRITE(oldstatus); break;
  515. #if E_STEPPERS > 5
  516. case 5: E5_ENABLE_WRITE(oldstatus); break;
  517. #endif // E_STEPPERS > 5
  518. #endif // E_STEPPERS > 4
  519. #endif // E_STEPPERS > 3
  520. #endif // E_STEPPERS > 2
  521. #endif // E_STEPPERS > 1
  522. }
  523. #endif // !SWITCHING_EXTRUDER
  524. gcode.previous_move_ms = ms; // reset_stepper_timeout to keep steppers powered
  525. }
  526. #endif // EXTRUDER_RUNOUT_PREVENT
  527. #if ENABLED(DUAL_X_CARRIAGE)
  528. // handle delayed move timeout
  529. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  530. // travel moves have been received so enact them
  531. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  532. set_destination_from_current();
  533. prepare_move_to_destination();
  534. }
  535. #endif
  536. #if ENABLED(TEMP_STAT_LEDS)
  537. handle_status_leds();
  538. #endif
  539. #if ENABLED(MONITOR_DRIVER_STATUS)
  540. monitor_tmc_driver();
  541. #endif
  542. #if ENABLED(MONITOR_L6470_DRIVER_STATUS)
  543. L6470.monitor_driver();
  544. #endif
  545. // Limit check_axes_activity frequency to 10Hz
  546. static millis_t next_check_axes_ms = 0;
  547. if (ELAPSED(ms, next_check_axes_ms)) {
  548. planner.check_axes_activity();
  549. next_check_axes_ms = ms + 100UL;
  550. }
  551. #if PIN_EXISTS(FET_SAFETY)
  552. static millis_t FET_next;
  553. if (ELAPSED(ms, FET_next)) {
  554. FET_next = ms + FET_SAFETY_DELAY; // 2uS pulse every FET_SAFETY_DELAY mS
  555. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  556. DELAY_US(2);
  557. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  558. }
  559. #endif
  560. }
  561. /**
  562. * Standard idle routine keeps the machine alive
  563. */
  564. void idle(
  565. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  566. bool no_stepper_sleep/*=false*/
  567. #endif
  568. ) {
  569. #if ENABLED(MAX7219_DEBUG)
  570. max7219.idle_tasks();
  571. #endif
  572. ui.update();
  573. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  574. gcode.host_keepalive();
  575. #endif
  576. manage_inactivity(
  577. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  578. no_stepper_sleep
  579. #endif
  580. );
  581. thermalManager.manage_heater();
  582. #if ENABLED(PRINTCOUNTER)
  583. print_job_timer.tick();
  584. #endif
  585. #if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
  586. buzzer.tick();
  587. #endif
  588. #if ENABLED(I2C_POSITION_ENCODERS)
  589. static millis_t i2cpem_next_update_ms;
  590. if (planner.has_blocks_queued() && ELAPSED(millis(), i2cpem_next_update_ms)) {
  591. I2CPEM.update();
  592. i2cpem_next_update_ms = millis() + I2CPE_MIN_UPD_TIME_MS;
  593. }
  594. #endif
  595. #ifdef HAL_IDLETASK
  596. HAL_idletask();
  597. #endif
  598. #if HAS_AUTO_REPORTING
  599. if (!suspend_auto_report) {
  600. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  601. thermalManager.auto_report_temperatures();
  602. #endif
  603. #if ENABLED(AUTO_REPORT_SD_STATUS)
  604. card.auto_report_sd_status();
  605. #endif
  606. }
  607. #endif
  608. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  609. Sd2Card::idle();
  610. #endif
  611. #if ENABLED(PRUSA_MMU2)
  612. mmu2.mmuLoop();
  613. #endif
  614. }
  615. /**
  616. * Kill all activity and lock the machine.
  617. * After this the machine will need to be reset.
  618. */
  619. void kill(PGM_P const lcd_msg/*=NULL*/) {
  620. thermalManager.disable_all_heaters();
  621. SERIAL_ERROR_MSG(MSG_ERR_KILLED);
  622. #if HAS_SPI_LCD || ENABLED(EXTENSIBLE_UI)
  623. ui.kill_screen(lcd_msg ? lcd_msg : PSTR(MSG_KILLED));
  624. #else
  625. UNUSED(lcd_msg);
  626. #endif
  627. #ifdef ACTION_ON_KILL
  628. host_action_kill();
  629. #endif
  630. minkill();
  631. }
  632. void minkill() {
  633. // Wait a short time (allows messages to get out before shutting down.
  634. for (int i = 1000; i--;) DELAY_US(600);
  635. cli(); // Stop interrupts
  636. // Wait to ensure all interrupts stopped
  637. for (int i = 1000; i--;) DELAY_US(250);
  638. thermalManager.disable_all_heaters(); // turn off heaters again
  639. #if HAS_POWER_SWITCH
  640. PSU_OFF();
  641. #endif
  642. #if HAS_SUICIDE
  643. suicide();
  644. #endif
  645. #if HAS_KILL
  646. // Wait for kill to be released
  647. while (!READ(KILL_PIN)) {
  648. #if ENABLED(USE_WATCHDOG)
  649. watchdog_reset();
  650. #endif
  651. }
  652. // Wait for kill to be pressed
  653. while (READ(KILL_PIN)) {
  654. #if ENABLED(USE_WATCHDOG)
  655. watchdog_reset();
  656. #endif
  657. }
  658. void(*resetFunc)(void) = 0; // Declare resetFunc() at address 0
  659. resetFunc(); // Jump to address 0
  660. #else // !HAS_KILL
  661. for (;;) {
  662. #if ENABLED(USE_WATCHDOG)
  663. watchdog_reset();
  664. #endif
  665. } // Wait for reset
  666. #endif // !HAS_KILL
  667. }
  668. /**
  669. * Turn off heaters and stop the print in progress
  670. * After a stop the machine may be resumed with M999
  671. */
  672. void stop() {
  673. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  674. print_job_timer.stop();
  675. #if ENABLED(PROBING_FANS_OFF)
  676. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  677. #endif
  678. if (IsRunning()) {
  679. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  680. SERIAL_ERROR_MSG(MSG_ERR_STOPPED);
  681. LCD_MESSAGEPGM(MSG_STOPPED);
  682. safe_delay(350); // allow enough time for messages to get out before stopping
  683. Running = false;
  684. }
  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. */
  704. void setup() {
  705. #ifdef HAL_INIT
  706. HAL_init();
  707. #endif
  708. #if HAS_DRIVER(L6470)
  709. L6470.init(); // setup SPI and then init chips
  710. #endif
  711. #if ENABLED(MAX7219_DEBUG)
  712. max7219.init();
  713. #endif
  714. #if ENABLED(DISABLE_DEBUG)
  715. // Disable any hardware debug to free up pins for IO
  716. #ifdef JTAGSWD_DISABLE
  717. JTAGSWD_DISABLE();
  718. #elif defined(JTAG_DISABLE)
  719. JTAG_DISABLE();
  720. #else
  721. #error "DISABLE_DEBUG is not supported for the selected MCU/Board"
  722. #endif
  723. #elif ENABLED(DISABLE_JTAG)
  724. // Disable JTAG to free up pins for IO
  725. #ifdef JTAG_DISABLE
  726. JTAG_DISABLE();
  727. #else
  728. #error "DISABLE_JTAG is not supported for the selected MCU/Board"
  729. #endif
  730. #endif
  731. #if HAS_FILAMENT_SENSOR
  732. runout.setup();
  733. #endif
  734. setup_killpin();
  735. setup_powerhold();
  736. #if HAS_STEPPER_RESET
  737. disableStepperDrivers();
  738. #endif
  739. #if NUM_SERIAL > 0
  740. MYSERIAL0.begin(BAUDRATE);
  741. #if NUM_SERIAL > 1
  742. MYSERIAL1.begin(BAUDRATE);
  743. #endif
  744. #endif
  745. #if NUM_SERIAL > 0
  746. uint32_t serial_connect_timeout = millis() + 1000UL;
  747. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  748. #if NUM_SERIAL > 1
  749. serial_connect_timeout = millis() + 1000UL;
  750. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  751. #endif
  752. #endif
  753. SERIAL_ECHOLNPGM("start");
  754. SERIAL_ECHO_START();
  755. #if TMC_HAS_SPI
  756. #if DISABLED(TMC_USE_SW_SPI)
  757. SPI.begin();
  758. #endif
  759. tmc_init_cs_pins();
  760. #endif
  761. #if HAS_DRIVER(TMC2208)
  762. tmc2208_serial_begin();
  763. #endif
  764. #ifdef BOARD_INIT
  765. BOARD_INIT();
  766. #endif
  767. // Check startup - does nothing if bootloader sets MCUSR to 0
  768. byte mcu = HAL_get_reset_source();
  769. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  770. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  771. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  772. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  773. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  774. HAL_clear_reset_source();
  775. SERIAL_ECHOPGM(MSG_MARLIN);
  776. SERIAL_CHAR(' ');
  777. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  778. SERIAL_EOL();
  779. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  780. SERIAL_ECHO_START();
  781. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  782. SERIAL_ECHOPGM(STRING_DISTRIBUTION_DATE);
  783. SERIAL_ECHOLNPGM(MSG_AUTHOR STRING_CONFIG_H_AUTHOR);
  784. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  785. #endif
  786. SERIAL_ECHO_START();
  787. SERIAL_ECHOLNPAIR(MSG_FREE_MEMORY, freeMemory(), MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  788. queue_setup();
  789. // Load data from EEPROM if available (or use defaults)
  790. // This also updates variables in the planner, elsewhere
  791. (void)settings.load();
  792. #if HAS_M206_COMMAND
  793. // Initialize current position based on home_offset
  794. LOOP_XYZ(a) current_position[a] += home_offset[a];
  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. endstops.init(); // Init endstops and pullups
  801. stepper.init(); // Init stepper. This enables interrupts!
  802. #if HAS_SERVOS
  803. servo_init();
  804. #endif
  805. #if HAS_Z_SERVO_PROBE
  806. servo_probe_init();
  807. #endif
  808. #if HAS_PHOTOGRAPH
  809. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  810. #endif
  811. #if ENABLED(SPINDLE_LASER_ENABLE)
  812. OUT_WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); // init spindle to off
  813. #if SPINDLE_DIR_CHANGE
  814. OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // init rotation to clockwise (M3)
  815. #endif
  816. #if ENABLED(SPINDLE_LASER_PWM) && defined(SPINDLE_LASER_PWM_PIN) && SPINDLE_LASER_PWM_PIN >= 0
  817. SET_PWM(SPINDLE_LASER_PWM_PIN);
  818. analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // set to lowest speed
  819. #endif
  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_COLOR_LEDS
  849. leds.setup();
  850. #endif
  851. #if HAS_CASE_LIGHT
  852. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  853. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  854. #endif
  855. update_case_light();
  856. #endif
  857. #if ENABLED(MK2_MULTIPLEXER)
  858. SET_OUTPUT(E_MUX0_PIN);
  859. SET_OUTPUT(E_MUX1_PIN);
  860. SET_OUTPUT(E_MUX2_PIN);
  861. #endif
  862. #if HAS_FANMUX
  863. fanmux_init();
  864. #endif
  865. ui.init();
  866. ui.reset_status();
  867. #if ENABLED(SHOW_BOOTSCREEN)
  868. ui.show_bootscreen();
  869. #endif
  870. #if ENABLED(MIXING_EXTRUDER)
  871. mixer.init();
  872. #endif
  873. #if ENABLED(BLTOUCH)
  874. bltouch.init();
  875. #endif
  876. #if ENABLED(I2C_POSITION_ENCODERS)
  877. I2CPEM.init();
  878. #endif
  879. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  880. i2c.onReceive(i2c_on_receive);
  881. i2c.onRequest(i2c_on_request);
  882. #endif
  883. #if DO_SWITCH_EXTRUDER
  884. move_extruder_servo(0); // Initialize extruder servo
  885. #endif
  886. #if ENABLED(SWITCHING_NOZZLE)
  887. // Initialize nozzle servo(s)
  888. #if SWITCHING_NOZZLE_TWO_SERVOS
  889. lower_nozzle(0);
  890. raise_nozzle(1);
  891. #else
  892. move_nozzle_servo(0);
  893. #endif
  894. #endif
  895. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  896. mpe_settings_init();
  897. #endif
  898. #if ENABLED(PARKING_EXTRUDER)
  899. pe_solenoid_init();
  900. #endif
  901. #if ENABLED(POWER_LOSS_RECOVERY)
  902. recovery.check();
  903. #endif
  904. #if ENABLED(USE_WATCHDOG)
  905. watchdog_init(); // Reinit watchdog after HAL_get_reset_source call
  906. #endif
  907. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  908. init_closedloop();
  909. #endif
  910. #if ENABLED(SDSUPPORT) && DISABLED(ULTRA_LCD)
  911. card.beginautostart();
  912. #endif
  913. #if HAS_TRINAMIC && DISABLED(PS_DEFAULT_OFF)
  914. test_tmc_connection(true, true, true, true);
  915. #endif
  916. #if ENABLED(PRUSA_MMU2)
  917. mmu2.init();
  918. #endif
  919. }
  920. /**
  921. * The main Marlin program loop
  922. *
  923. * - Save or log commands to SD
  924. * - Process available commands (if not saving)
  925. * - Call endstop manager
  926. * - Call inactivity manager
  927. */
  928. void loop() {
  929. for (;;) {
  930. #if ENABLED(SDSUPPORT)
  931. card.checkautostart();
  932. if (card.flag.abort_sd_printing) {
  933. card.stopSDPrint(
  934. #if SD_RESORT
  935. true
  936. #endif
  937. );
  938. clear_command_queue();
  939. quickstop_stepper();
  940. print_job_timer.stop();
  941. thermalManager.disable_all_heaters();
  942. thermalManager.zero_fan_speeds();
  943. wait_for_heatup = false;
  944. #if ENABLED(POWER_LOSS_RECOVERY)
  945. card.removeJobRecoveryFile();
  946. #endif
  947. }
  948. #endif // SDSUPPORT
  949. if (commands_in_queue < BUFSIZE) get_available_commands();
  950. advance_command_queue();
  951. endstops.event_handler();
  952. idle();
  953. }
  954. }