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

Marlin.cpp 28KB

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