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

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