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.

MarlinCore.cpp 28KB

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