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 27KB

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