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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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 "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. G28_STR[] PROGMEM = "G28",
  148. M21_STR[] PROGMEM = "M21",
  149. M23_STR[] PROGMEM = "M23 %s",
  150. M24_STR[] PROGMEM = "M24",
  151. SP_X_STR[] PROGMEM = " X",
  152. SP_Y_STR[] PROGMEM = " Y",
  153. SP_Z_STR[] PROGMEM = " Z",
  154. SP_E_STR[] PROGMEM = " E";
  155. bool Running = true;
  156. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  157. bool wait_for_heatup = true;
  158. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  159. #if HAS_RESUME_CONTINUE
  160. bool wait_for_user; // = false;
  161. #endif
  162. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  163. bool suspend_auto_report; // = 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_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_X();
  242. enable_Y();
  243. enable_Z();
  244. enable_e_steppers();
  245. }
  246. void disable_e_steppers() {
  247. #define _DIS_E(N) disable_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_E##N(); break;
  252. switch (e) {
  253. REPEAT(EXTRUDERS, _CASE_DIS_E)
  254. }
  255. }
  256. void disable_all_steppers() {
  257. disable_X();
  258. disable_Y();
  259. disable_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"), PSTR("Dismiss"));
  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. card.removeJobRecoveryFile();
  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. 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_X();
  382. #endif
  383. #if ENABLED(DISABLE_INACTIVE_Y)
  384. disable_Y();
  385. #endif
  386. #if ENABLED(DISABLE_INACTIVE_Z)
  387. disable_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_E0(); break;
  455. #if E_STEPPERS > 1
  456. case 2: case 3: oldstatus = E1_ENABLE_READ(); enable_E1(); break;
  457. #if E_STEPPERS > 2
  458. case 4: case 5: oldstatus = E2_ENABLE_READ(); enable_E2(); break;
  459. #endif // E_STEPPERS > 2
  460. #endif // E_STEPPERS > 1
  461. }
  462. #else // !SWITCHING_EXTRUDER
  463. bool oldstatus;
  464. switch (active_extruder) {
  465. default:
  466. #define _CASE_EN(N) case N: oldstatus = E##N##_ENABLE_READ(); enable_E##N(); break;
  467. REPEAT(E_STEPPERS, _CASE_EN);
  468. }
  469. #endif
  470. const float olde = current_position.e;
  471. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  472. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  473. current_position.e = olde;
  474. planner.set_e_position_mm(olde);
  475. planner.synchronize();
  476. #if ENABLED(SWITCHING_EXTRUDER)
  477. switch (active_extruder) {
  478. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  479. #if E_STEPPERS > 1
  480. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  481. #if E_STEPPERS > 2
  482. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  483. #endif // E_STEPPERS > 2
  484. #endif // E_STEPPERS > 1
  485. }
  486. #else // !SWITCHING_EXTRUDER
  487. switch (active_extruder) {
  488. #define _CASE_RESTORE(N) case N: E##N##_ENABLE_WRITE(oldstatus); break;
  489. REPEAT(E_STEPPERS, _CASE_RESTORE);
  490. }
  491. #endif // !SWITCHING_EXTRUDER
  492. gcode.reset_stepper_timeout();
  493. }
  494. #endif // EXTRUDER_RUNOUT_PREVENT
  495. #if ENABLED(DUAL_X_CARRIAGE)
  496. // handle delayed move timeout
  497. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  498. // travel moves have been received so enact them
  499. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  500. destination = current_position;
  501. prepare_move_to_destination();
  502. }
  503. #endif
  504. #if ENABLED(TEMP_STAT_LEDS)
  505. handle_status_leds();
  506. #endif
  507. #if ENABLED(MONITOR_DRIVER_STATUS)
  508. monitor_tmc_drivers();
  509. #endif
  510. #if ENABLED(MONITOR_L6470_DRIVER_STATUS)
  511. L64xxManager.monitor_driver();
  512. #endif
  513. // Limit check_axes_activity frequency to 10Hz
  514. static millis_t next_check_axes_ms = 0;
  515. if (ELAPSED(ms, next_check_axes_ms)) {
  516. planner.check_axes_activity();
  517. next_check_axes_ms = ms + 100UL;
  518. }
  519. #if PIN_EXISTS(FET_SAFETY)
  520. static millis_t FET_next;
  521. if (ELAPSED(ms, FET_next)) {
  522. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  523. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  524. DELAY_US(2);
  525. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  526. }
  527. #endif
  528. }
  529. /**
  530. * Standard idle routine keeps the machine alive
  531. */
  532. void idle(
  533. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  534. bool no_stepper_sleep/*=false*/
  535. #endif
  536. ) {
  537. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  538. recovery.outage();
  539. #endif
  540. #if ENABLED(SPI_ENDSTOPS)
  541. if (endstops.tmc_spi_homing.any
  542. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  543. && ELAPSED(millis(), sg_guard_period)
  544. #endif
  545. ) {
  546. for (uint8_t i = 4; i--;) // Read SGT 4 times per idle loop
  547. if (endstops.tmc_spi_homing_check()) break;
  548. }
  549. #endif
  550. #if ENABLED(MAX7219_DEBUG)
  551. max7219.idle_tasks();
  552. #endif
  553. ui.update();
  554. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  555. gcode.host_keepalive();
  556. #endif
  557. manage_inactivity(
  558. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  559. no_stepper_sleep
  560. #endif
  561. );
  562. thermalManager.manage_heater();
  563. #if ENABLED(PRINTCOUNTER)
  564. print_job_timer.tick();
  565. #endif
  566. #if USE_BEEPER
  567. buzzer.tick();
  568. #endif
  569. #if ENABLED(I2C_POSITION_ENCODERS)
  570. static millis_t i2cpem_next_update_ms;
  571. if (planner.has_blocks_queued()) {
  572. const millis_t ms = millis();
  573. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  574. I2CPEM.update();
  575. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  576. }
  577. }
  578. #endif
  579. #ifdef HAL_IDLETASK
  580. HAL_idletask();
  581. #endif
  582. #if HAS_AUTO_REPORTING
  583. if (!suspend_auto_report) {
  584. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  585. thermalManager.auto_report_temperatures();
  586. #endif
  587. #if ENABLED(AUTO_REPORT_SD_STATUS)
  588. card.auto_report_sd_status();
  589. #endif
  590. }
  591. #endif
  592. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  593. Sd2Card::idle();
  594. #endif
  595. #if ENABLED(PRUSA_MMU2)
  596. mmu2.mmu_loop();
  597. #endif
  598. #if ENABLED(POLL_JOG)
  599. joystick.inject_jog_moves();
  600. #endif
  601. }
  602. /**
  603. * Kill all activity and lock the machine.
  604. * After this the machine will need to be reset.
  605. */
  606. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  607. thermalManager.disable_all_heaters();
  608. SERIAL_ERROR_MSG(MSG_ERR_KILLED);
  609. #if HAS_DISPLAY
  610. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component ?: NUL_STR);
  611. #else
  612. UNUSED(lcd_error);
  613. UNUSED(lcd_component);
  614. #endif
  615. #ifdef ACTION_ON_KILL
  616. host_action_kill();
  617. #endif
  618. minkill(steppers_off);
  619. }
  620. void minkill(const bool steppers_off/*=false*/) {
  621. // Wait a short time (allows messages to get out before shutting down.
  622. for (int i = 1000; i--;) DELAY_US(600);
  623. cli(); // Stop interrupts
  624. // Wait to ensure all interrupts stopped
  625. for (int i = 1000; i--;) DELAY_US(250);
  626. // Reiterate heaters off
  627. thermalManager.disable_all_heaters();
  628. // Power off all steppers (for M112) or just the E steppers
  629. steppers_off ? disable_all_steppers() : disable_e_steppers();
  630. #if ENABLED(PSU_CONTROL)
  631. PSU_OFF();
  632. #endif
  633. #if HAS_SUICIDE
  634. suicide();
  635. #endif
  636. #if HAS_KILL
  637. // Wait for kill to be released
  638. while (!READ(KILL_PIN)) watchdog_refresh();
  639. // Wait for kill to be pressed
  640. while (READ(KILL_PIN)) watchdog_refresh();
  641. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  642. resetFunc(); // Jump to address 0
  643. #else // !HAS_KILL
  644. for (;;) watchdog_refresh(); // Wait for reset
  645. #endif // !HAS_KILL
  646. }
  647. /**
  648. * Turn off heaters and stop the print in progress
  649. * After a stop the machine may be resumed with M999
  650. */
  651. void stop() {
  652. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  653. print_job_timer.stop();
  654. #if ENABLED(PROBING_FANS_OFF)
  655. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  656. #endif
  657. if (IsRunning()) {
  658. queue.stop();
  659. SERIAL_ERROR_MSG(MSG_ERR_STOPPED);
  660. LCD_MESSAGEPGM(MSG_STOPPED);
  661. safe_delay(350); // allow enough time for messages to get out before stopping
  662. Running = false;
  663. }
  664. }
  665. /**
  666. * Marlin entry-point: Set up before the program loop
  667. * - Set up the kill pin, filament runout, power hold
  668. * - Start the serial port
  669. * - Print startup messages and diagnostics
  670. * - Get EEPROM or default settings
  671. * - Initialize managers for:
  672. * • temperature
  673. * • planner
  674. * • watchdog
  675. * • stepper
  676. * • photo pin
  677. * • servos
  678. * • LCD controller
  679. * • Digipot I2C
  680. * • Z probe sled
  681. * • status LEDs
  682. */
  683. void setup() {
  684. HAL_init();
  685. #if HAS_L64XX
  686. L64xxManager.init(); // Set up SPI, init drivers
  687. #endif
  688. #if ENABLED(MAX7219_DEBUG)
  689. max7219.init();
  690. #endif
  691. #if ENABLED(DISABLE_DEBUG)
  692. // Disable any hardware debug to free up pins for IO
  693. #ifdef JTAGSWD_DISABLE
  694. JTAGSWD_DISABLE();
  695. #elif defined(JTAG_DISABLE)
  696. JTAG_DISABLE();
  697. #else
  698. #error "DISABLE_DEBUG is not supported for the selected MCU/Board"
  699. #endif
  700. #elif ENABLED(DISABLE_JTAG)
  701. // Disable JTAG to free up pins for IO
  702. #ifdef JTAG_DISABLE
  703. JTAG_DISABLE();
  704. #else
  705. #error "DISABLE_JTAG is not supported for the selected MCU/Board"
  706. #endif
  707. #endif
  708. #if HAS_FILAMENT_SENSOR
  709. runout.setup();
  710. #endif
  711. #if ENABLED(POWER_LOSS_RECOVERY)
  712. recovery.setup();
  713. #endif
  714. setup_killpin();
  715. #if HAS_TMC220x
  716. tmc_serial_begin();
  717. #endif
  718. setup_powerhold();
  719. #if HAS_STEPPER_RESET
  720. disableStepperDrivers();
  721. #endif
  722. #if NUM_SERIAL > 0
  723. MYSERIAL0.begin(BAUDRATE);
  724. #if NUM_SERIAL > 1
  725. MYSERIAL1.begin(BAUDRATE);
  726. #endif
  727. #endif
  728. #if NUM_SERIAL > 0
  729. uint32_t serial_connect_timeout = millis() + 1000UL;
  730. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  731. #if NUM_SERIAL > 1
  732. serial_connect_timeout = millis() + 1000UL;
  733. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  734. #endif
  735. #endif
  736. SERIAL_ECHOLNPGM("start");
  737. SERIAL_ECHO_START();
  738. #if TMC_HAS_SPI
  739. #if DISABLED(TMC_USE_SW_SPI)
  740. SPI.begin();
  741. #endif
  742. tmc_init_cs_pins();
  743. #endif
  744. #ifdef BOARD_INIT
  745. BOARD_INIT();
  746. #endif
  747. // Check startup - does nothing if bootloader sets MCUSR to 0
  748. byte mcu = HAL_get_reset_source();
  749. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  750. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  751. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  752. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  753. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  754. HAL_clear_reset_source();
  755. SERIAL_ECHOPGM(MSG_MARLIN);
  756. SERIAL_CHAR(' ');
  757. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  758. SERIAL_EOL();
  759. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  760. SERIAL_ECHO_MSG(
  761. MSG_CONFIGURATION_VER
  762. STRING_DISTRIBUTION_DATE
  763. MSG_AUTHOR STRING_CONFIG_H_AUTHOR
  764. );
  765. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  766. #endif
  767. SERIAL_ECHO_START();
  768. SERIAL_ECHOLNPAIR(MSG_FREE_MEMORY, freeMemory(), MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  769. // UI must be initialized before EEPROM
  770. // (because EEPROM code calls the UI).
  771. // Set up LEDs early
  772. #if HAS_COLOR_LEDS
  773. leds.setup();
  774. #endif
  775. ui.init();
  776. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  777. ui.show_bootscreen();
  778. #endif
  779. #if ENABLED(SDSUPPORT)
  780. card.mount(); // Mount the SD card before settings.first_load
  781. #endif
  782. // Load data from EEPROM if available (or use defaults)
  783. // This also updates variables in the planner, elsewhere
  784. settings.first_load();
  785. #if ENABLED(TOUCH_BUTTONS)
  786. touch.init();
  787. #endif
  788. #if HAS_M206_COMMAND
  789. // Initialize current position based on home_offset
  790. current_position += home_offset;
  791. #endif
  792. // Vital to init stepper/planner equivalent for current_position
  793. sync_plan_position();
  794. thermalManager.init(); // Initialize temperature loop
  795. print_job_timer.init(); // Initial setup of print job timer
  796. ui.reset_status(); // Print startup message after print statistics are loaded
  797. endstops.init(); // Init endstops and pullups
  798. stepper.init(); // Init stepper. This enables interrupts!
  799. #if HAS_SERVOS
  800. servo_init();
  801. #endif
  802. #if HAS_Z_SERVO_PROBE
  803. servo_probe_init();
  804. #endif
  805. #if HAS_PHOTOGRAPH
  806. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  807. #endif
  808. #if HAS_CUTTER
  809. cutter.init();
  810. #endif
  811. #if ENABLED(COOLANT_MIST)
  812. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  813. #endif
  814. #if ENABLED(COOLANT_FLOOD)
  815. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  816. #endif
  817. #if HAS_BED_PROBE
  818. endstops.enable_z_probe(false);
  819. #endif
  820. #if ENABLED(USE_CONTROLLER_FAN)
  821. SET_OUTPUT(CONTROLLER_FAN_PIN);
  822. #endif
  823. #if HAS_STEPPER_RESET
  824. enableStepperDrivers();
  825. #endif
  826. #if ENABLED(DIGIPOT_I2C)
  827. digipot_i2c_init();
  828. #endif
  829. #if ENABLED(DAC_STEPPER_CURRENT)
  830. dac_init();
  831. #endif
  832. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  833. OUT_WRITE(SOL1_PIN, LOW); // OFF
  834. #endif
  835. #if HAS_HOME
  836. SET_INPUT_PULLUP(HOME_PIN);
  837. #endif
  838. #if PIN_EXISTS(STAT_LED_RED)
  839. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  840. #endif
  841. #if PIN_EXISTS(STAT_LED_BLUE)
  842. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  843. #endif
  844. #if HAS_CASE_LIGHT
  845. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  846. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  847. #endif
  848. update_case_light();
  849. #endif
  850. #if ENABLED(MK2_MULTIPLEXER)
  851. SET_OUTPUT(E_MUX0_PIN);
  852. SET_OUTPUT(E_MUX1_PIN);
  853. SET_OUTPUT(E_MUX2_PIN);
  854. #endif
  855. #if HAS_FANMUX
  856. fanmux_init();
  857. #endif
  858. #if ENABLED(MIXING_EXTRUDER)
  859. mixer.init();
  860. #endif
  861. #if ENABLED(BLTOUCH)
  862. bltouch.init(/*set_voltage=*/true);
  863. #endif
  864. #if ENABLED(I2C_POSITION_ENCODERS)
  865. I2CPEM.init();
  866. #endif
  867. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  868. i2c.onReceive(i2c_on_receive);
  869. i2c.onRequest(i2c_on_request);
  870. #endif
  871. #if DO_SWITCH_EXTRUDER
  872. move_extruder_servo(0); // Initialize extruder servo
  873. #endif
  874. #if ENABLED(SWITCHING_NOZZLE)
  875. // Initialize nozzle servo(s)
  876. #if SWITCHING_NOZZLE_TWO_SERVOS
  877. lower_nozzle(0);
  878. raise_nozzle(1);
  879. #else
  880. move_nozzle_servo(0);
  881. #endif
  882. #endif
  883. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  884. mpe_settings_init();
  885. #endif
  886. #if ENABLED(PARKING_EXTRUDER)
  887. pe_solenoid_init();
  888. #endif
  889. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  890. est_init();
  891. #endif
  892. #if ENABLED(POWER_LOSS_RECOVERY)
  893. recovery.check();
  894. #endif
  895. #if ENABLED(USE_WATCHDOG)
  896. watchdog_init(); // Reinit watchdog after HAL_get_reset_source call
  897. #endif
  898. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  899. init_closedloop();
  900. #endif
  901. #ifdef STARTUP_COMMANDS
  902. queue.inject_P(PSTR(STARTUP_COMMANDS));
  903. #endif
  904. #if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
  905. card.beginautostart();
  906. #endif
  907. #if ENABLED(HOST_PROMPT_SUPPORT)
  908. host_action_prompt_end();
  909. #endif
  910. #if HAS_TRINAMIC && DISABLED(PSU_DEFAULT_OFF)
  911. test_tmc_connection(true, true, true, true);
  912. #endif
  913. #if ENABLED(PRUSA_MMU2)
  914. mmu2.init();
  915. #endif
  916. }
  917. /**
  918. * The main Marlin program loop
  919. *
  920. * - Save or log commands to SD
  921. * - Process available commands (if not saving)
  922. * - Call endstop manager
  923. * - Call inactivity manager
  924. */
  925. void loop() {
  926. do {
  927. idle();
  928. #if ENABLED(SDSUPPORT)
  929. card.checkautostart();
  930. if (card.flag.abort_sd_printing) abortSDPrinting();
  931. #endif
  932. queue.advance();
  933. endstops.event_handler();
  934. } while (false // Return to caller for best compatibility
  935. #ifdef __AVR__
  936. || true // Loop forever on slower (AVR) boards
  937. #endif
  938. );
  939. }