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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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(STR_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. inline void abortSDPrinting() {
  322. card.endFilePrint(
  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. inline void finishSDPrinting() {
  343. bool did_state = true;
  344. switch (card.sdprinting_done_state) {
  345. #if HAS_RESUME_CONTINUE // Display "Click to Continue..."
  346. case 1:
  347. did_state = queue.enqueue_P(PSTR("M0Q1S"
  348. #if HAS_LCD_MENU
  349. "1800" // ...for 30 minutes with LCD
  350. #else
  351. "60" // ...for 1 minute with no LCD
  352. #endif
  353. ));
  354. break;
  355. #endif
  356. case 2: print_job_timer.stop(); break;
  357. case 3:
  358. did_state = print_job_timer.duration() < 60 || queue.enqueue_P(PSTR("M31"));
  359. break;
  360. case 4:
  361. #if ENABLED(POWER_LOSS_RECOVERY)
  362. recovery.purge();
  363. #endif
  364. #if ENABLED(SD_FINISHED_STEPPERRELEASE) && defined(SD_FINISHED_RELEASECOMMAND)
  365. planner.finish_and_disable();
  366. #endif
  367. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  368. ui.set_progress_done();
  369. #endif
  370. #if ENABLED(SD_REPRINT_LAST_SELECTED_FILE)
  371. ui.reselect_last_file();
  372. #endif
  373. SERIAL_ECHOLNPGM(STR_FILE_PRINTED);
  374. default:
  375. did_state = false;
  376. card.sdprinting_done_state = 0;
  377. }
  378. if (did_state) ++card.sdprinting_done_state;
  379. }
  380. #endif // SDSUPPORT
  381. /**
  382. * Minimal management of Marlin's core activities:
  383. * - Check for Filament Runout
  384. * - Keep the command buffer full
  385. * - Check for maximum inactive time between commands
  386. * - Check for maximum inactive time between stepper commands
  387. * - Check if CHDK_PIN needs to go LOW
  388. * - Check for KILL button held down
  389. * - Check for HOME button held down
  390. * - Check if cooling fan needs to be switched on
  391. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  392. * - Pulse FET_SAFETY_PIN if it exists
  393. */
  394. inline void manage_inactivity(const bool ignore_stepper_queue=false) {
  395. #if HAS_FILAMENT_SENSOR
  396. runout.run();
  397. #endif
  398. if (queue.length < BUFSIZE) queue.get_available_commands();
  399. const millis_t ms = millis();
  400. if (max_inactive_time && ELAPSED(ms, gcode.previous_move_ms + max_inactive_time)) {
  401. SERIAL_ERROR_START();
  402. SERIAL_ECHOLNPAIR(STR_KILL_INACTIVE_TIME, parser.command_ptr);
  403. kill();
  404. }
  405. // Prevent steppers timing-out in the middle of M600
  406. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  407. #define MOVE_AWAY_TEST !did_pause_print
  408. #else
  409. #define MOVE_AWAY_TEST true
  410. #endif
  411. if (stepper_inactive_time) {
  412. static bool already_shutdown_steppers; // = false
  413. if (planner.has_blocks_queued())
  414. gcode.reset_stepper_timeout();
  415. else if (MOVE_AWAY_TEST && !ignore_stepper_queue && ELAPSED(ms, gcode.previous_move_ms + stepper_inactive_time)) {
  416. if (!already_shutdown_steppers) {
  417. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  418. #if ENABLED(DISABLE_INACTIVE_X)
  419. DISABLE_AXIS_X();
  420. #endif
  421. #if ENABLED(DISABLE_INACTIVE_Y)
  422. DISABLE_AXIS_Y();
  423. #endif
  424. #if ENABLED(DISABLE_INACTIVE_Z)
  425. DISABLE_AXIS_Z();
  426. #endif
  427. #if ENABLED(DISABLE_INACTIVE_E)
  428. disable_e_steppers();
  429. #endif
  430. #if HAS_LCD_MENU && ENABLED(AUTO_BED_LEVELING_UBL)
  431. if (ubl.lcd_map_control) {
  432. ubl.lcd_map_control = false;
  433. ui.defer_status_screen(false);
  434. }
  435. #endif
  436. }
  437. }
  438. else
  439. already_shutdown_steppers = false;
  440. }
  441. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  442. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  443. chdk_timeout = 0;
  444. WRITE(CHDK_PIN, LOW);
  445. }
  446. #endif
  447. #if HAS_KILL
  448. // Check if the kill button was pressed and wait just in case it was an accidental
  449. // key kill key press
  450. // -------------------------------------------------------------------------------
  451. static int killCount = 0; // make the inactivity button a bit less responsive
  452. const int KILL_DELAY = 750;
  453. if (!READ(KILL_PIN))
  454. killCount++;
  455. else if (killCount > 0)
  456. killCount--;
  457. // Exceeded threshold and we can confirm that it was not accidental
  458. // KILL the machine
  459. // ----------------------------------------------------------------
  460. if (killCount >= KILL_DELAY) {
  461. SERIAL_ERROR_MSG(STR_KILL_BUTTON);
  462. kill();
  463. }
  464. #endif
  465. #if HAS_HOME
  466. // Handle a standalone HOME button
  467. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  468. static millis_t next_home_key_ms; // = 0
  469. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  470. const millis_t ms = millis();
  471. if (ELAPSED(ms, next_home_key_ms)) {
  472. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  473. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  474. queue.enqueue_now_P(G28_STR);
  475. }
  476. }
  477. #endif
  478. #if ENABLED(USE_CONTROLLER_FAN)
  479. controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
  480. #endif
  481. #if ENABLED(AUTO_POWER_CONTROL)
  482. powerManager.check();
  483. #endif
  484. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  485. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  486. && ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  487. && !planner.has_blocks_queued()
  488. ) {
  489. #if ENABLED(SWITCHING_EXTRUDER)
  490. bool oldstatus;
  491. switch (active_extruder) {
  492. default: oldstatus = E0_ENABLE_READ(); ENABLE_AXIS_E0(); break;
  493. #if E_STEPPERS > 1
  494. case 2: case 3: oldstatus = E1_ENABLE_READ(); ENABLE_AXIS_E1(); break;
  495. #if E_STEPPERS > 2
  496. case 4: case 5: oldstatus = E2_ENABLE_READ(); ENABLE_AXIS_E2(); break;
  497. #if E_STEPPERS > 3
  498. case 6: case 7: oldstatus = E3_ENABLE_READ(); ENABLE_AXIS_E3(); break;
  499. #endif // E_STEPPERS > 3
  500. #endif // E_STEPPERS > 2
  501. #endif // E_STEPPERS > 1
  502. }
  503. #else // !SWITCHING_EXTRUDER
  504. bool oldstatus;
  505. switch (active_extruder) {
  506. default:
  507. #define _CASE_EN(N) case N: oldstatus = E##N##_ENABLE_READ(); ENABLE_AXIS_E##N(); break;
  508. REPEAT(E_STEPPERS, _CASE_EN);
  509. }
  510. #endif
  511. const float olde = current_position.e;
  512. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  513. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  514. current_position.e = olde;
  515. planner.set_e_position_mm(olde);
  516. planner.synchronize();
  517. #if ENABLED(SWITCHING_EXTRUDER)
  518. switch (active_extruder) {
  519. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  520. #if E_STEPPERS > 1
  521. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  522. #if E_STEPPERS > 2
  523. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  524. #endif // E_STEPPERS > 2
  525. #endif // E_STEPPERS > 1
  526. }
  527. #else // !SWITCHING_EXTRUDER
  528. switch (active_extruder) {
  529. #define _CASE_RESTORE(N) case N: E##N##_ENABLE_WRITE(oldstatus); break;
  530. REPEAT(E_STEPPERS, _CASE_RESTORE);
  531. }
  532. #endif // !SWITCHING_EXTRUDER
  533. gcode.reset_stepper_timeout();
  534. }
  535. #endif // EXTRUDER_RUNOUT_PREVENT
  536. #if ENABLED(DUAL_X_CARRIAGE)
  537. // handle delayed move timeout
  538. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  539. // travel moves have been received so enact them
  540. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  541. destination = current_position;
  542. prepare_move_to_destination();
  543. }
  544. #endif
  545. #if ENABLED(TEMP_STAT_LEDS)
  546. handle_status_leds();
  547. #endif
  548. #if ENABLED(MONITOR_DRIVER_STATUS)
  549. monitor_tmc_drivers();
  550. #endif
  551. #if ENABLED(MONITOR_L6470_DRIVER_STATUS)
  552. L64xxManager.monitor_driver();
  553. #endif
  554. // Limit check_axes_activity frequency to 10Hz
  555. static millis_t next_check_axes_ms = 0;
  556. if (ELAPSED(ms, next_check_axes_ms)) {
  557. planner.check_axes_activity();
  558. next_check_axes_ms = ms + 100UL;
  559. }
  560. #if PIN_EXISTS(FET_SAFETY)
  561. static millis_t FET_next;
  562. if (ELAPSED(ms, FET_next)) {
  563. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  564. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  565. DELAY_US(2);
  566. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  567. }
  568. #endif
  569. }
  570. /**
  571. * Standard idle routine keeps the machine alive
  572. */
  573. void idle(
  574. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  575. bool no_stepper_sleep/*=false*/
  576. #endif
  577. ) {
  578. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  579. recovery.outage();
  580. #endif
  581. #if ENABLED(SPI_ENDSTOPS)
  582. if (endstops.tmc_spi_homing.any
  583. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  584. && ELAPSED(millis(), sg_guard_period)
  585. #endif
  586. ) {
  587. for (uint8_t i = 4; i--;) // Read SGT 4 times per idle loop
  588. if (endstops.tmc_spi_homing_check()) break;
  589. }
  590. #endif
  591. #if ENABLED(MAX7219_DEBUG)
  592. max7219.idle_tasks();
  593. #endif
  594. ui.update();
  595. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  596. gcode.host_keepalive();
  597. #endif
  598. manage_inactivity(
  599. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  600. no_stepper_sleep
  601. #endif
  602. );
  603. thermalManager.manage_heater();
  604. #if ENABLED(PRINTCOUNTER)
  605. print_job_timer.tick();
  606. #endif
  607. #if USE_BEEPER
  608. buzzer.tick();
  609. #endif
  610. #if ENABLED(I2C_POSITION_ENCODERS)
  611. static millis_t i2cpem_next_update_ms;
  612. if (planner.has_blocks_queued()) {
  613. const millis_t ms = millis();
  614. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  615. I2CPEM.update();
  616. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  617. }
  618. }
  619. #endif
  620. #ifdef HAL_IDLETASK
  621. HAL_idletask();
  622. #endif
  623. #if HAS_AUTO_REPORTING
  624. if (!gcode.autoreport_paused) {
  625. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  626. thermalManager.auto_report_temperatures();
  627. #endif
  628. #if ENABLED(AUTO_REPORT_SD_STATUS)
  629. card.auto_report_sd_status();
  630. #endif
  631. }
  632. #endif
  633. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  634. Sd2Card::idle();
  635. #endif
  636. #if ENABLED(PRUSA_MMU2)
  637. mmu2.mmu_loop();
  638. #endif
  639. #if ENABLED(POLL_JOG)
  640. joystick.inject_jog_moves();
  641. #endif
  642. }
  643. /**
  644. * Kill all activity and lock the machine.
  645. * After this the machine will need to be reset.
  646. */
  647. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  648. thermalManager.disable_all_heaters();
  649. SERIAL_ERROR_MSG(STR_ERR_KILLED);
  650. #if HAS_DISPLAY
  651. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component ?: NUL_STR);
  652. #else
  653. UNUSED(lcd_error);
  654. UNUSED(lcd_component);
  655. #endif
  656. #ifdef ACTION_ON_KILL
  657. host_action_kill();
  658. #endif
  659. minkill(steppers_off);
  660. }
  661. void minkill(const bool steppers_off/*=false*/) {
  662. // Wait a short time (allows messages to get out before shutting down.
  663. for (int i = 1000; i--;) DELAY_US(600);
  664. cli(); // Stop interrupts
  665. // Wait to ensure all interrupts stopped
  666. for (int i = 1000; i--;) DELAY_US(250);
  667. // Reiterate heaters off
  668. thermalManager.disable_all_heaters();
  669. // Power off all steppers (for M112) or just the E steppers
  670. steppers_off ? disable_all_steppers() : disable_e_steppers();
  671. #if ENABLED(PSU_CONTROL)
  672. PSU_OFF();
  673. #endif
  674. #if HAS_SUICIDE
  675. suicide();
  676. #endif
  677. #if HAS_KILL
  678. // Wait for kill to be released
  679. while (!READ(KILL_PIN)) watchdog_refresh();
  680. // Wait for kill to be pressed
  681. while (READ(KILL_PIN)) watchdog_refresh();
  682. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  683. resetFunc(); // Jump to address 0
  684. #else // !HAS_KILL
  685. for (;;) watchdog_refresh(); // Wait for reset
  686. #endif // !HAS_KILL
  687. }
  688. /**
  689. * Turn off heaters and stop the print in progress
  690. * After a stop the machine may be resumed with M999
  691. */
  692. void stop() {
  693. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  694. print_job_timer.stop();
  695. #if ENABLED(PROBING_FANS_OFF)
  696. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  697. #endif
  698. if (IsRunning()) {
  699. SERIAL_ERROR_MSG(STR_ERR_STOPPED);
  700. LCD_MESSAGEPGM(MSG_STOPPED);
  701. safe_delay(350); // allow enough time for messages to get out before stopping
  702. Running = false;
  703. }
  704. }
  705. /**
  706. * Marlin entry-point: Set up before the program loop
  707. * - Set up the kill pin, filament runout, power hold
  708. * - Start the serial port
  709. * - Print startup messages and diagnostics
  710. * - Get EEPROM or default settings
  711. * - Initialize managers for:
  712. * • temperature
  713. * • planner
  714. * • watchdog
  715. * • stepper
  716. * • photo pin
  717. * • servos
  718. * • LCD controller
  719. * • Digipot I2C
  720. * • Z probe sled
  721. * • status LEDs
  722. */
  723. void setup() {
  724. HAL_init();
  725. #if HAS_L64XX
  726. L64xxManager.init(); // Set up SPI, init drivers
  727. #endif
  728. #if ENABLED(SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  729. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  730. #endif
  731. #if ENABLED(MAX7219_DEBUG)
  732. max7219.init();
  733. #endif
  734. #if ENABLED(DISABLE_DEBUG)
  735. // Disable any hardware debug to free up pins for IO
  736. #ifdef JTAGSWD_DISABLE
  737. JTAGSWD_DISABLE();
  738. #elif defined(JTAG_DISABLE)
  739. JTAG_DISABLE();
  740. #else
  741. #error "DISABLE_DEBUG is not supported for the selected MCU/Board"
  742. #endif
  743. #elif ENABLED(DISABLE_JTAG)
  744. // Disable JTAG to free up pins for IO
  745. #ifdef JTAG_DISABLE
  746. JTAG_DISABLE();
  747. #else
  748. #error "DISABLE_JTAG is not supported for the selected MCU/Board"
  749. #endif
  750. #endif
  751. #if HAS_FILAMENT_SENSOR
  752. runout.setup();
  753. #endif
  754. #if ENABLED(POWER_LOSS_RECOVERY)
  755. recovery.setup();
  756. #endif
  757. setup_killpin();
  758. #if HAS_TMC220x
  759. tmc_serial_begin();
  760. #endif
  761. setup_powerhold();
  762. #if HAS_STEPPER_RESET
  763. disableStepperDrivers();
  764. #endif
  765. #if NUM_SERIAL > 0
  766. MYSERIAL0.begin(BAUDRATE);
  767. uint32_t serial_connect_timeout = millis() + 1000UL;
  768. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  769. #if NUM_SERIAL > 1
  770. MYSERIAL1.begin(BAUDRATE);
  771. serial_connect_timeout = millis() + 1000UL;
  772. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  773. #endif
  774. #endif
  775. SERIAL_ECHOLNPGM("start");
  776. SERIAL_ECHO_START();
  777. #if HAS_TMC_SPI
  778. #if DISABLED(TMC_USE_SW_SPI)
  779. SPI.begin();
  780. #endif
  781. tmc_init_cs_pins();
  782. #endif
  783. #ifdef BOARD_INIT
  784. BOARD_INIT();
  785. #endif
  786. // Check startup - does nothing if bootloader sets MCUSR to 0
  787. byte mcu = HAL_get_reset_source();
  788. if (mcu & 1) SERIAL_ECHOLNPGM(STR_POWERUP);
  789. if (mcu & 2) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  790. if (mcu & 4) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  791. if (mcu & 8) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  792. if (mcu & 32) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  793. HAL_clear_reset_source();
  794. serialprintPGM(GET_TEXT(MSG_MARLIN));
  795. SERIAL_CHAR(' ');
  796. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  797. SERIAL_EOL();
  798. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  799. SERIAL_ECHO_MSG(
  800. STR_CONFIGURATION_VER
  801. STRING_DISTRIBUTION_DATE
  802. STR_AUTHOR STRING_CONFIG_H_AUTHOR
  803. );
  804. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  805. #endif
  806. SERIAL_ECHO_START();
  807. SERIAL_ECHOLNPAIR(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  808. // UI must be initialized before EEPROM
  809. // (because EEPROM code calls the UI).
  810. // Set up LEDs early
  811. #if HAS_COLOR_LEDS
  812. leds.setup();
  813. #endif
  814. ui.init();
  815. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  816. ui.show_bootscreen();
  817. #endif
  818. ui.reset_status(); // Load welcome message early. (Retained if no errors exist.)
  819. #if ENABLED(SDSUPPORT)
  820. card.mount(); // Mount the SD card before settings.first_load
  821. #endif
  822. // Load data from EEPROM if available (or use defaults)
  823. settings.first_load(); // This also updates variables in the planner, elsewhere
  824. #if ENABLED(TOUCH_BUTTONS)
  825. touch.init();
  826. #endif
  827. #if HAS_M206_COMMAND // Initialize current position based on home_offset
  828. current_position += home_offset;
  829. #endif
  830. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  831. thermalManager.init(); // Initialize temperature loop
  832. print_job_timer.init(); // Initial setup of print job timer
  833. endstops.init(); // Init endstops and pullups
  834. stepper.init(); // Init stepper. This enables interrupts!
  835. #if HAS_SERVOS
  836. servo_init();
  837. #endif
  838. #if HAS_Z_SERVO_PROBE
  839. probe.servo_probe_init();
  840. #endif
  841. #if HAS_PHOTOGRAPH
  842. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  843. #endif
  844. #if HAS_CUTTER
  845. cutter.init();
  846. #endif
  847. #if ENABLED(COOLANT_MIST)
  848. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  849. #endif
  850. #if ENABLED(COOLANT_FLOOD)
  851. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  852. #endif
  853. #if HAS_BED_PROBE
  854. endstops.enable_z_probe(false);
  855. #endif
  856. #if ENABLED(USE_CONTROLLER_FAN)
  857. SET_OUTPUT(CONTROLLER_FAN_PIN);
  858. #endif
  859. #if HAS_STEPPER_RESET
  860. enableStepperDrivers();
  861. #endif
  862. #if ENABLED(DIGIPOT_I2C)
  863. digipot_i2c_init();
  864. #endif
  865. #if ENABLED(DAC_STEPPER_CURRENT)
  866. dac_init();
  867. #endif
  868. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  869. OUT_WRITE(SOL1_PIN, LOW); // OFF
  870. #endif
  871. #if HAS_HOME
  872. SET_INPUT_PULLUP(HOME_PIN);
  873. #endif
  874. #if PIN_EXISTS(STAT_LED_RED)
  875. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  876. #endif
  877. #if PIN_EXISTS(STAT_LED_BLUE)
  878. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  879. #endif
  880. #if HAS_CASE_LIGHT
  881. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  882. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  883. #endif
  884. update_case_light();
  885. #endif
  886. #if ENABLED(MK2_MULTIPLEXER)
  887. SET_OUTPUT(E_MUX0_PIN);
  888. SET_OUTPUT(E_MUX1_PIN);
  889. SET_OUTPUT(E_MUX2_PIN);
  890. #endif
  891. #if HAS_FANMUX
  892. fanmux_init();
  893. #endif
  894. #if ENABLED(MIXING_EXTRUDER)
  895. mixer.init();
  896. #endif
  897. #if ENABLED(BLTOUCH)
  898. bltouch.init(/*set_voltage=*/true);
  899. #endif
  900. #if ENABLED(I2C_POSITION_ENCODERS)
  901. I2CPEM.init();
  902. #endif
  903. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  904. i2c.onReceive(i2c_on_receive);
  905. i2c.onRequest(i2c_on_request);
  906. #endif
  907. #if DO_SWITCH_EXTRUDER
  908. move_extruder_servo(0); // Initialize extruder servo
  909. #endif
  910. #if ENABLED(SWITCHING_NOZZLE)
  911. // Initialize nozzle servo(s)
  912. #if SWITCHING_NOZZLE_TWO_SERVOS
  913. lower_nozzle(0);
  914. raise_nozzle(1);
  915. #else
  916. move_nozzle_servo(0);
  917. #endif
  918. #endif
  919. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  920. mpe_settings_init();
  921. #endif
  922. #if ENABLED(PARKING_EXTRUDER)
  923. pe_solenoid_init();
  924. #endif
  925. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  926. est_init();
  927. #endif
  928. #if ENABLED(POWER_LOSS_RECOVERY)
  929. recovery.check();
  930. #endif
  931. #if ENABLED(USE_WATCHDOG)
  932. watchdog_init(); // Reinit watchdog after HAL_get_reset_source call
  933. #endif
  934. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  935. init_closedloop();
  936. #endif
  937. #ifdef STARTUP_COMMANDS
  938. queue.inject_P(PSTR(STARTUP_COMMANDS));
  939. #endif
  940. #if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
  941. card.beginautostart();
  942. #endif
  943. #if ENABLED(HOST_PROMPT_SUPPORT)
  944. host_action_prompt_end();
  945. #endif
  946. #if HAS_TRINAMIC && DISABLED(PSU_DEFAULT_OFF)
  947. test_tmc_connection(true, true, true, true);
  948. #endif
  949. #if ENABLED(PRUSA_MMU2)
  950. mmu2.init();
  951. #endif
  952. #if HAS_SERVICE_INTERVALS
  953. ui.reset_status(true); // Show service messages or keep current status
  954. #endif
  955. }
  956. /**
  957. * The main Marlin program loop
  958. *
  959. * - Call idle() to handle all tasks between G-code commands
  960. * Note that no G-codes from the queue can be executed during idle()
  961. * but many G-codes can be called directly anytime like macros.
  962. * - Check whether SD card auto-start is needed now.
  963. * - Check whether SD print finishing is needed now.
  964. * - Run one G-code command from the immediate or main command queue
  965. * and open up one space. Commands in the main queue may come from sd
  966. * card, host, or by direct injection. The queue will continue to fill
  967. * as long as idle() or manage_inactivity() are being called.
  968. */
  969. void loop() {
  970. do {
  971. idle();
  972. #if ENABLED(SDSUPPORT)
  973. card.checkautostart();
  974. if (card.flag.abort_sd_printing) abortSDPrinting();
  975. if (card.sdprinting_done_state) finishSDPrinting();
  976. #endif
  977. queue.advance();
  978. endstops.event_handler();
  979. } while (false // Return to caller for best compatibility
  980. #ifdef __AVR__
  981. || true // Loop forever on slower (AVR) boards
  982. #endif
  983. );
  984. }