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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016, 2017 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. #ifdef ARDUINO
  41. #include <pins_arduino.h>
  42. #endif
  43. #include <math.h>
  44. #include "libs/nozzle.h"
  45. #include "gcode/gcode.h"
  46. #include "gcode/parser.h"
  47. #include "gcode/queue.h"
  48. #if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
  49. #include "libs/buzzer.h"
  50. #endif
  51. #if ENABLED(DIGIPOT_I2C)
  52. #include "feature/digipot/digipot.h"
  53. #endif
  54. #if ENABLED(MIXING_EXTRUDER)
  55. #include "feature/mixing.h"
  56. #endif
  57. #if ENABLED(BEZIER_CURVE_SUPPORT)
  58. #include "module/planner_bezier.h"
  59. #endif
  60. #if ENABLED(MAX7219_DEBUG)
  61. #include "feature/Max7219_Debug_LEDs.h"
  62. #endif
  63. #if HAS_COLOR_LEDS
  64. #include "feature/leds/leds.h"
  65. #endif
  66. #if HAS_SERVOS
  67. #include "module/servo.h"
  68. #endif
  69. #if HAS_DIGIPOTSS
  70. #include <SPI.h>
  71. #endif
  72. #if ENABLED(DAC_STEPPER_CURRENT)
  73. #include "feature/dac/stepper_dac.h"
  74. #endif
  75. #if ENABLED(EXPERIMENTAL_I2CBUS)
  76. #include "feature/twibus.h"
  77. TWIBus i2c;
  78. #endif
  79. #if ENABLED(I2C_POSITION_ENCODERS)
  80. #include "feature/I2CPositionEncoder.h"
  81. #endif
  82. #if HAS_TRINAMIC
  83. #include "feature/tmc_util.h"
  84. #endif
  85. #if ENABLED(SDSUPPORT)
  86. CardReader card;
  87. #endif
  88. #if ENABLED(G38_PROBE_TARGET)
  89. bool G38_move = false,
  90. G38_endstop_hit = false;
  91. #endif
  92. #if ENABLED(DELTA)
  93. #include "module/delta.h"
  94. #elif IS_SCARA
  95. #include "module/scara.h"
  96. #endif
  97. #if HAS_LEVELING
  98. #include "feature/bedlevel/bedlevel.h"
  99. #endif
  100. #if ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT)
  101. #include "feature/pause.h"
  102. #endif
  103. #if ENABLED(POWER_LOSS_RECOVERY)
  104. #include "feature/power_loss_recovery.h"
  105. #endif
  106. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  107. #include "feature/runout.h"
  108. #endif
  109. #if ENABLED(TEMP_STAT_LEDS)
  110. #include "feature/leds/tempstat.h"
  111. #endif
  112. #if HAS_CASE_LIGHT
  113. #include "feature/caselight.h"
  114. #endif
  115. #if HAS_FANMUX
  116. #include "feature/fanmux.h"
  117. #endif
  118. #if DO_SWITCH_EXTRUDER || ENABLED(SWITCHING_NOZZLE) || ENABLED(PARKING_EXTRUDER)
  119. #include "module/tool_change.h"
  120. #endif
  121. #if ENABLED(USE_CONTROLLER_FAN)
  122. #include "feature/controllerfan.h"
  123. #endif
  124. bool Running = true;
  125. /**
  126. * axis_homed
  127. * Flags that each linear axis was homed.
  128. * XYZ on cartesian, ABC on delta, ABZ on SCARA.
  129. *
  130. * axis_known_position
  131. * Flags that the position is known in each linear axis. Set when homed.
  132. * Cleared whenever a stepper powers off, potentially losing its position.
  133. */
  134. bool axis_homed[XYZ] = { false }, axis_known_position[XYZ] = { false };
  135. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  136. TempUnit input_temp_units = TEMPUNIT_C;
  137. #endif
  138. #if FAN_COUNT > 0
  139. int16_t fanSpeeds[FAN_COUNT] = { 0 };
  140. #if ENABLED(EXTRA_FAN_SPEED)
  141. int16_t old_fanSpeeds[FAN_COUNT],
  142. new_fanSpeeds[FAN_COUNT];
  143. #endif
  144. #if ENABLED(PROBING_FANS_OFF)
  145. bool fans_paused; // = false;
  146. int16_t paused_fanSpeeds[FAN_COUNT] = { 0 };
  147. #endif
  148. #endif
  149. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  150. volatile bool wait_for_heatup = true;
  151. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  152. #if HAS_RESUME_CONTINUE
  153. volatile bool wait_for_user; // = false;
  154. #endif
  155. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  156. bool suspend_auto_report; // = false
  157. #endif
  158. // Inactivity shutdown
  159. millis_t max_inactive_time, // = 0
  160. stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
  161. #ifdef CHDK
  162. millis_t chdkHigh; // = 0;
  163. bool chdkActive; // = false;
  164. #endif
  165. #if ENABLED(I2C_POSITION_ENCODERS)
  166. I2CPositionEncodersMgr I2CPEM;
  167. #endif
  168. /**
  169. * ***************************************************************************
  170. * ******************************** FUNCTIONS ********************************
  171. * ***************************************************************************
  172. */
  173. void setup_killpin() {
  174. #if HAS_KILL
  175. SET_INPUT_PULLUP(KILL_PIN);
  176. #endif
  177. }
  178. void setup_powerhold() {
  179. #if HAS_SUICIDE
  180. OUT_WRITE(SUICIDE_PIN, HIGH);
  181. #endif
  182. #if HAS_POWER_SWITCH
  183. #if ENABLED(PS_DEFAULT_OFF)
  184. PSU_OFF();
  185. #else
  186. PSU_ON();
  187. #endif
  188. #endif
  189. }
  190. /**
  191. * Stepper Reset (RigidBoard, et.al.)
  192. */
  193. #if HAS_STEPPER_RESET
  194. void disableStepperDrivers() {
  195. OUT_WRITE(STEPPER_RESET_PIN, LOW); // drive it down to hold in reset motor driver chips
  196. }
  197. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // set to input, which allows it to be pulled high by pullups
  198. #endif
  199. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  200. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  201. i2c.receive(bytes);
  202. }
  203. void i2c_on_request() { // just send dummy data for now
  204. i2c.reply("Hello World!\n");
  205. }
  206. #endif
  207. /**
  208. * Sensitive pin test for M42, M226
  209. */
  210. bool pin_is_protected(const pin_t pin) {
  211. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  212. for (uint8_t i = 0; i < COUNT(sensitive_pins); i++) {
  213. pin_t sensitive_pin;
  214. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  215. if (pin == sensitive_pin) return true;
  216. }
  217. return false;
  218. }
  219. void quickstop_stepper() {
  220. planner.quick_stop();
  221. planner.synchronize();
  222. set_current_from_steppers_for_axis(ALL_AXES);
  223. SYNC_PLAN_POSITION_KINEMATIC();
  224. }
  225. void enable_all_steppers() {
  226. #if ENABLED(AUTO_POWER_CONTROL)
  227. powerManager.power_on();
  228. #endif
  229. enable_X();
  230. enable_Y();
  231. enable_Z();
  232. enable_E0();
  233. enable_E1();
  234. enable_E2();
  235. enable_E3();
  236. enable_E4();
  237. }
  238. void disable_e_steppers() {
  239. disable_E0();
  240. disable_E1();
  241. disable_E2();
  242. disable_E3();
  243. disable_E4();
  244. }
  245. void disable_e_stepper(const uint8_t e) {
  246. switch (e) {
  247. case 0: disable_E0(); break;
  248. case 1: disable_E1(); break;
  249. case 2: disable_E2(); break;
  250. case 3: disable_E3(); break;
  251. case 4: disable_E4(); break;
  252. }
  253. }
  254. void disable_all_steppers() {
  255. disable_X();
  256. disable_Y();
  257. disable_Z();
  258. disable_e_steppers();
  259. }
  260. /**
  261. * Manage several activities:
  262. * - Check for Filament Runout
  263. * - Keep the command buffer full
  264. * - Check for maximum inactive time between commands
  265. * - Check for maximum inactive time between stepper commands
  266. * - Check if pin CHDK needs to go LOW
  267. * - Check for KILL button held down
  268. * - Check for HOME button held down
  269. * - Check if cooling fan needs to be switched on
  270. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  271. */
  272. void manage_inactivity(const bool ignore_stepper_queue/*=false*/) {
  273. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  274. runout.run();
  275. #endif
  276. if (commands_in_queue < BUFSIZE) get_available_commands();
  277. const millis_t ms = millis();
  278. if (max_inactive_time && ELAPSED(ms, gcode.previous_move_ms + max_inactive_time)) {
  279. SERIAL_ERROR_START();
  280. SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
  281. kill(PSTR(MSG_KILLED));
  282. }
  283. // Prevent steppers timing-out in the middle of M600
  284. #if ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT)
  285. #define MOVE_AWAY_TEST !did_pause_print
  286. #else
  287. #define MOVE_AWAY_TEST true
  288. #endif
  289. if (stepper_inactive_time) {
  290. if (planner.has_blocks_queued())
  291. gcode.previous_move_ms = ms; // reset_stepper_timeout to keep steppers powered
  292. else if (MOVE_AWAY_TEST && !ignore_stepper_queue && ELAPSED(ms, gcode.previous_move_ms + stepper_inactive_time)) {
  293. #if ENABLED(DISABLE_INACTIVE_X)
  294. disable_X();
  295. #endif
  296. #if ENABLED(DISABLE_INACTIVE_Y)
  297. disable_Y();
  298. #endif
  299. #if ENABLED(DISABLE_INACTIVE_Z)
  300. disable_Z();
  301. #endif
  302. #if ENABLED(DISABLE_INACTIVE_E)
  303. disable_e_steppers();
  304. #endif
  305. #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(ULTIPANEL) // Only needed with an LCD
  306. if (ubl.lcd_map_control) ubl.lcd_map_control = defer_return_to_status = false;
  307. #endif
  308. }
  309. }
  310. #ifdef CHDK // Check if pin should be set to LOW after M240 set it to HIGH
  311. if (chdkActive && ELAPSED(ms, chdkHigh + CHDK_DELAY)) {
  312. chdkActive = false;
  313. WRITE(CHDK, LOW);
  314. }
  315. #endif
  316. #if HAS_KILL
  317. // Check if the kill button was pressed and wait just in case it was an accidental
  318. // key kill key press
  319. // -------------------------------------------------------------------------------
  320. static int killCount = 0; // make the inactivity button a bit less responsive
  321. const int KILL_DELAY = 750;
  322. if (!READ(KILL_PIN))
  323. killCount++;
  324. else if (killCount > 0)
  325. killCount--;
  326. // Exceeded threshold and we can confirm that it was not accidental
  327. // KILL the machine
  328. // ----------------------------------------------------------------
  329. if (killCount >= KILL_DELAY) {
  330. SERIAL_ERROR_START();
  331. SERIAL_ERRORLNPGM(MSG_KILL_BUTTON);
  332. kill(PSTR(MSG_KILLED));
  333. }
  334. #endif
  335. #if HAS_HOME
  336. // Check to see if we have to home, use poor man's debouncer
  337. // ---------------------------------------------------------
  338. static int homeDebounceCount = 0; // poor man's debouncing count
  339. const int HOME_DEBOUNCE_DELAY = 2500;
  340. if (!IS_SD_PRINTING && !READ(HOME_PIN)) {
  341. if (!homeDebounceCount) {
  342. enqueue_and_echo_commands_P(PSTR("G28"));
  343. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  344. }
  345. if (homeDebounceCount < HOME_DEBOUNCE_DELAY)
  346. homeDebounceCount++;
  347. else
  348. homeDebounceCount = 0;
  349. }
  350. #endif
  351. #if ENABLED(USE_CONTROLLER_FAN)
  352. controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
  353. #endif
  354. #if ENABLED(AUTO_POWER_CONTROL)
  355. powerManager.check();
  356. #endif
  357. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  358. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  359. && ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  360. && !planner.has_blocks_queued()
  361. ) {
  362. #if ENABLED(SWITCHING_EXTRUDER)
  363. const bool oldstatus = E0_ENABLE_READ;
  364. enable_E0();
  365. #else // !SWITCHING_EXTRUDER
  366. bool oldstatus;
  367. switch (active_extruder) {
  368. default: oldstatus = E0_ENABLE_READ; enable_E0(); break;
  369. #if E_STEPPERS > 1
  370. case 1: oldstatus = E1_ENABLE_READ; enable_E1(); break;
  371. #if E_STEPPERS > 2
  372. case 2: oldstatus = E2_ENABLE_READ; enable_E2(); break;
  373. #if E_STEPPERS > 3
  374. case 3: oldstatus = E3_ENABLE_READ; enable_E3(); break;
  375. #if E_STEPPERS > 4
  376. case 4: oldstatus = E4_ENABLE_READ; enable_E4(); break;
  377. #endif // E_STEPPERS > 4
  378. #endif // E_STEPPERS > 3
  379. #endif // E_STEPPERS > 2
  380. #endif // E_STEPPERS > 1
  381. }
  382. #endif // !SWITCHING_EXTRUDER
  383. const float olde = current_position[E_AXIS];
  384. current_position[E_AXIS] += EXTRUDER_RUNOUT_EXTRUDE;
  385. planner.buffer_line_kinematic(current_position, MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED), active_extruder);
  386. current_position[E_AXIS] = olde;
  387. planner.set_e_position_mm(olde);
  388. planner.synchronize();
  389. #if ENABLED(SWITCHING_EXTRUDER)
  390. E0_ENABLE_WRITE(oldstatus);
  391. #else
  392. switch (active_extruder) {
  393. case 0: E0_ENABLE_WRITE(oldstatus); break;
  394. #if E_STEPPERS > 1
  395. case 1: E1_ENABLE_WRITE(oldstatus); break;
  396. #if E_STEPPERS > 2
  397. case 2: E2_ENABLE_WRITE(oldstatus); break;
  398. #if E_STEPPERS > 3
  399. case 3: E3_ENABLE_WRITE(oldstatus); break;
  400. #if E_STEPPERS > 4
  401. case 4: E4_ENABLE_WRITE(oldstatus); break;
  402. #endif // E_STEPPERS > 4
  403. #endif // E_STEPPERS > 3
  404. #endif // E_STEPPERS > 2
  405. #endif // E_STEPPERS > 1
  406. }
  407. #endif // !SWITCHING_EXTRUDER
  408. gcode.previous_move_ms = ms; // reset_stepper_timeout to keep steppers powered
  409. }
  410. #endif // EXTRUDER_RUNOUT_PREVENT
  411. #if ENABLED(DUAL_X_CARRIAGE)
  412. // handle delayed move timeout
  413. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  414. // travel moves have been received so enact them
  415. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  416. set_destination_from_current();
  417. prepare_move_to_destination();
  418. }
  419. #endif
  420. #if ENABLED(TEMP_STAT_LEDS)
  421. handle_status_leds();
  422. #endif
  423. #if ENABLED(MONITOR_DRIVER_STATUS)
  424. monitor_tmc_driver();
  425. #endif
  426. // Limit check_axes_activity frequency to 10Hz
  427. static millis_t next_check_axes_ms = 0;
  428. if (ELAPSED(ms, next_check_axes_ms)) {
  429. planner.check_axes_activity();
  430. next_check_axes_ms = ms + 100UL;
  431. }
  432. }
  433. /**
  434. * Standard idle routine keeps the machine alive
  435. */
  436. void idle(
  437. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  438. bool no_stepper_sleep/*=false*/
  439. #endif
  440. ) {
  441. #if ENABLED(MAX7219_DEBUG)
  442. Max7219_idle_tasks();
  443. #endif // MAX7219_DEBUG
  444. lcd_update();
  445. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  446. gcode.host_keepalive();
  447. #endif
  448. manage_inactivity(
  449. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  450. no_stepper_sleep
  451. #endif
  452. );
  453. thermalManager.manage_heater();
  454. #if ENABLED(PRINTCOUNTER)
  455. print_job_timer.tick();
  456. #endif
  457. #if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
  458. buzzer.tick();
  459. #endif
  460. #if ENABLED(I2C_POSITION_ENCODERS)
  461. static millis_t i2cpem_next_update_ms;
  462. if (planner.has_blocks_queued() && ELAPSED(millis(), i2cpem_next_update_ms)) {
  463. I2CPEM.update();
  464. i2cpem_next_update_ms = millis() + I2CPE_MIN_UPD_TIME_MS;
  465. }
  466. #endif
  467. #ifdef HAL_IDLETASK
  468. HAL_idletask();
  469. #endif
  470. #if HAS_AUTO_REPORTING
  471. if (!suspend_auto_report) {
  472. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  473. thermalManager.auto_report_temperatures();
  474. #endif
  475. #if ENABLED(AUTO_REPORT_SD_STATUS)
  476. card.auto_report_sd_status();
  477. #endif
  478. }
  479. #endif
  480. }
  481. /**
  482. * Kill all activity and lock the machine.
  483. * After this the machine will need to be reset.
  484. */
  485. void kill(const char* lcd_msg) {
  486. SERIAL_ERROR_START();
  487. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  488. thermalManager.disable_all_heaters();
  489. disable_all_steppers();
  490. #if ENABLED(ULTRA_LCD)
  491. kill_screen(lcd_msg);
  492. #else
  493. UNUSED(lcd_msg);
  494. #endif
  495. _delay_ms(600); // Wait a short time (allows messages to get out before shutting down.
  496. cli(); // Stop interrupts
  497. _delay_ms(250); //Wait to ensure all interrupts routines stopped
  498. thermalManager.disable_all_heaters(); //turn off heaters again
  499. #ifdef ACTION_ON_KILL
  500. SERIAL_ECHOLNPGM("//action:" ACTION_ON_KILL);
  501. #endif
  502. #if HAS_POWER_SWITCH
  503. PSU_OFF();
  504. #endif
  505. #if HAS_SUICIDE
  506. suicide();
  507. #endif
  508. while (1) {
  509. #if ENABLED(USE_WATCHDOG)
  510. watchdog_reset();
  511. #endif
  512. } // Wait for reset
  513. }
  514. /**
  515. * Turn off heaters and stop the print in progress
  516. * After a stop the machine may be resumed with M999
  517. */
  518. void stop() {
  519. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  520. #if ENABLED(PROBING_FANS_OFF)
  521. if (fans_paused) fans_pause(false); // put things back the way they were
  522. #endif
  523. if (IsRunning()) {
  524. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  525. SERIAL_ERROR_START();
  526. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  527. LCD_MESSAGEPGM(MSG_STOPPED);
  528. safe_delay(350); // allow enough time for messages to get out before stopping
  529. Running = false;
  530. }
  531. }
  532. /**
  533. * Marlin entry-point: Set up before the program loop
  534. * - Set up the kill pin, filament runout, power hold
  535. * - Start the serial port
  536. * - Print startup messages and diagnostics
  537. * - Get EEPROM or default settings
  538. * - Initialize managers for:
  539. * • temperature
  540. * • planner
  541. * • watchdog
  542. * • stepper
  543. * • photo pin
  544. * • servos
  545. * • LCD controller
  546. * • Digipot I2C
  547. * • Z probe sled
  548. * • status LEDs
  549. */
  550. void setup() {
  551. #ifdef HAL_INIT
  552. HAL_init();
  553. #endif
  554. #if ENABLED(MAX7219_DEBUG)
  555. Max7219_init();
  556. #endif
  557. #if ENABLED(DISABLE_JTAG)
  558. // Disable JTAG on AT90USB chips to free up pins for IO
  559. MCUCR = 0x80;
  560. MCUCR = 0x80;
  561. #endif
  562. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  563. runout.setup();
  564. #endif
  565. setup_killpin();
  566. setup_powerhold();
  567. #if HAS_STEPPER_RESET
  568. disableStepperDrivers();
  569. #endif
  570. #if NUM_SERIAL > 0
  571. MYSERIAL0.begin(BAUDRATE);
  572. #if NUM_SERIAL > 1
  573. MYSERIAL1.begin(BAUDRATE);
  574. #endif
  575. #endif
  576. #if NUM_SERIAL > 0
  577. uint32_t serial_connect_timeout = millis() + 1000UL;
  578. while(!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  579. #if NUM_SERIAL > 1
  580. serial_connect_timeout = millis() + 1000UL;
  581. while(!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  582. #endif
  583. #endif
  584. SERIAL_PROTOCOLLNPGM("start");
  585. SERIAL_ECHO_START();
  586. #if ENABLED(HAVE_TMC2130)
  587. tmc_init_cs_pins();
  588. #endif
  589. #if ENABLED(HAVE_TMC2208)
  590. tmc2208_serial_begin();
  591. #endif
  592. // Check startup - does nothing if bootloader sets MCUSR to 0
  593. byte mcu = HAL_get_reset_source();
  594. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  595. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  596. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  597. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  598. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  599. HAL_clear_reset_source();
  600. SERIAL_ECHOPGM(MSG_MARLIN);
  601. SERIAL_CHAR(' ');
  602. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  603. SERIAL_EOL();
  604. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  605. SERIAL_ECHO_START();
  606. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  607. SERIAL_ECHOPGM(STRING_DISTRIBUTION_DATE);
  608. SERIAL_ECHOLNPGM(MSG_AUTHOR STRING_CONFIG_H_AUTHOR);
  609. SERIAL_ECHO_START();
  610. SERIAL_ECHOLNPGM("Compiled: " __DATE__);
  611. #endif
  612. SERIAL_ECHO_START();
  613. SERIAL_ECHOPAIR(MSG_FREE_MEMORY, freeMemory());
  614. SERIAL_ECHOLNPAIR(MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  615. queue_setup();
  616. // Load data from EEPROM if available (or use defaults)
  617. // This also updates variables in the planner, elsewhere
  618. (void)settings.load();
  619. #if HAS_M206_COMMAND
  620. // Initialize current position based on home_offset
  621. COPY(current_position, home_offset);
  622. #else
  623. ZERO(current_position);
  624. #endif
  625. // Vital to init stepper/planner equivalent for current_position
  626. SYNC_PLAN_POSITION_KINEMATIC();
  627. thermalManager.init(); // Initialize temperature loop
  628. print_job_timer.init(); // Initial setup of print job timer
  629. endstops.init(); // Init endstops and pullups
  630. stepper.init(); // Init stepper. This enables interrupts!
  631. #if HAS_SERVOS
  632. servo_init();
  633. #endif
  634. #if HAS_Z_SERVO_PROBE
  635. servo_probe_init();
  636. #endif
  637. #if HAS_PHOTOGRAPH
  638. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  639. #endif
  640. #if HAS_CASE_LIGHT
  641. update_case_light();
  642. #endif
  643. #if ENABLED(SPINDLE_LASER_ENABLE)
  644. OUT_WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); // init spindle to off
  645. #if SPINDLE_DIR_CHANGE
  646. OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // init rotation to clockwise (M3)
  647. #endif
  648. #if ENABLED(SPINDLE_LASER_PWM) && defined(SPINDLE_LASER_PWM_PIN) && SPINDLE_LASER_PWM_PIN >= 0
  649. SET_OUTPUT(SPINDLE_LASER_PWM_PIN);
  650. analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // set to lowest speed
  651. #endif
  652. #endif
  653. #if HAS_BED_PROBE
  654. endstops.enable_z_probe(false);
  655. #endif
  656. #if ENABLED(USE_CONTROLLER_FAN)
  657. SET_OUTPUT(CONTROLLER_FAN_PIN); //Set pin used for driver cooling fan
  658. #endif
  659. #if HAS_STEPPER_RESET
  660. enableStepperDrivers();
  661. #endif
  662. #if ENABLED(DIGIPOT_I2C)
  663. digipot_i2c_init();
  664. #endif
  665. #if ENABLED(DAC_STEPPER_CURRENT)
  666. dac_init();
  667. #endif
  668. #if (ENABLED(Z_PROBE_SLED) || ENABLED(SOLENOID_PROBE)) && HAS_SOLENOID_1
  669. OUT_WRITE(SOL1_PIN, LOW); // turn it off
  670. #endif
  671. #if HAS_HOME
  672. SET_INPUT_PULLUP(HOME_PIN);
  673. #endif
  674. #if PIN_EXISTS(STAT_LED_RED)
  675. OUT_WRITE(STAT_LED_RED_PIN, LOW); // turn it off
  676. #endif
  677. #if PIN_EXISTS(STAT_LED_BLUE)
  678. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // turn it off
  679. #endif
  680. #if HAS_COLOR_LEDS
  681. leds.setup();
  682. #endif
  683. #if ENABLED(RGB_LED) || ENABLED(RGBW_LED)
  684. SET_OUTPUT(RGB_LED_R_PIN);
  685. SET_OUTPUT(RGB_LED_G_PIN);
  686. SET_OUTPUT(RGB_LED_B_PIN);
  687. #if ENABLED(RGBW_LED)
  688. SET_OUTPUT(RGB_LED_W_PIN);
  689. #endif
  690. #endif
  691. #if ENABLED(MK2_MULTIPLEXER)
  692. SET_OUTPUT(E_MUX0_PIN);
  693. SET_OUTPUT(E_MUX1_PIN);
  694. SET_OUTPUT(E_MUX2_PIN);
  695. #endif
  696. #if HAS_FANMUX
  697. fanmux_init();
  698. #endif
  699. lcd_init();
  700. lcd_reset_status();
  701. #if ENABLED(SHOW_BOOTSCREEN)
  702. lcd_bootscreen();
  703. #endif
  704. #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1
  705. mixing_tools_init();
  706. #endif
  707. #if ENABLED(BLTOUCH)
  708. bltouch_init();
  709. #endif
  710. #if ENABLED(I2C_POSITION_ENCODERS)
  711. I2CPEM.init();
  712. #endif
  713. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  714. i2c.onReceive(i2c_on_receive);
  715. i2c.onRequest(i2c_on_request);
  716. #endif
  717. #if DO_SWITCH_EXTRUDER
  718. move_extruder_servo(0); // Initialize extruder servo
  719. #endif
  720. #if ENABLED(SWITCHING_NOZZLE)
  721. move_nozzle_servo(0); // Initialize nozzle servo
  722. #endif
  723. #if ENABLED(PARKING_EXTRUDER)
  724. pe_magnet_init();
  725. #endif
  726. #if ENABLED(POWER_LOSS_RECOVERY)
  727. do_print_job_recovery();
  728. #endif
  729. #if ENABLED(USE_WATCHDOG) // Reinit watchdog after HAL_get_reset_source call
  730. watchdog_init();
  731. #endif
  732. }
  733. /**
  734. * The main Marlin program loop
  735. *
  736. * - Save or log commands to SD
  737. * - Process available commands (if not saving)
  738. * - Call endstop manager
  739. * - Call inactivity manager
  740. */
  741. void loop() {
  742. for (;;) {
  743. #if ENABLED(SDSUPPORT)
  744. card.checkautostart();
  745. #endif
  746. #if ENABLED(SDSUPPORT) && ENABLED(ULTIPANEL)
  747. if (abort_sd_printing) {
  748. abort_sd_printing = false;
  749. card.stopSDPrint(
  750. #if SD_RESORT
  751. true
  752. #endif
  753. );
  754. clear_command_queue();
  755. quickstop_stepper();
  756. print_job_timer.stop();
  757. thermalManager.disable_all_heaters();
  758. #if FAN_COUNT > 0
  759. for (uint8_t i = 0; i < FAN_COUNT; i++) fanSpeeds[i] = 0;
  760. #endif
  761. wait_for_heatup = false;
  762. }
  763. #endif // SDSUPPORT && ULTIPANEL
  764. if (commands_in_queue < BUFSIZE) get_available_commands();
  765. advance_command_queue();
  766. endstops.report_state();
  767. idle();
  768. }
  769. }