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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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/simen/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 "HAL/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 ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  83. #include "HAL/HAL_endstop_interrupts.h"
  84. #endif
  85. #if ENABLED(HAVE_TMC2130)
  86. #include "feature/tmc2130.h"
  87. #endif
  88. #if ENABLED(SDSUPPORT)
  89. CardReader card;
  90. #endif
  91. #if ENABLED(G38_PROBE_TARGET)
  92. bool G38_move = false,
  93. G38_endstop_hit = false;
  94. #endif
  95. #if ENABLED(DELTA)
  96. #include "module/delta.h"
  97. #elif IS_SCARA
  98. #include "module/scara.h"
  99. #endif
  100. #if HAS_LEVELING
  101. #include "feature/bedlevel/bedlevel.h"
  102. #endif
  103. #if ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT)
  104. #include "feature/pause.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 (ENABLED(SWITCHING_EXTRUDER) && !DONT_SWITCH) || 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. // Inactivity shutdown
  156. millis_t max_inactive_time = 0,
  157. stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
  158. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  159. AdvancedPauseMenuResponse advanced_pause_menu_response;
  160. #endif
  161. #ifdef CHDK
  162. millis_t chdkHigh = 0;
  163. bool chdkActive = false;
  164. #endif
  165. #if ENABLED(PID_EXTRUSION_SCALING)
  166. int lpq_len = 20;
  167. #endif
  168. #if ENABLED(I2C_POSITION_ENCODERS)
  169. I2CPositionEncodersMgr I2CPEM;
  170. uint8_t blockBufferIndexRef = 0;
  171. millis_t lastUpdateMillis;
  172. #endif
  173. /**
  174. * ***************************************************************************
  175. * ******************************** FUNCTIONS ********************************
  176. * ***************************************************************************
  177. */
  178. void setup_killpin() {
  179. #if HAS_KILL
  180. SET_INPUT_PULLUP(KILL_PIN);
  181. #endif
  182. }
  183. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  184. void setup_filrunoutpin() {
  185. #if ENABLED(ENDSTOPPULLUP_FIL_RUNOUT)
  186. SET_INPUT_PULLUP(FIL_RUNOUT_PIN);
  187. #else
  188. SET_INPUT(FIL_RUNOUT_PIN);
  189. #endif
  190. }
  191. #endif
  192. void setup_powerhold() {
  193. #if HAS_SUICIDE
  194. OUT_WRITE(SUICIDE_PIN, HIGH);
  195. #endif
  196. #if HAS_POWER_SWITCH
  197. #if ENABLED(PS_DEFAULT_OFF)
  198. OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  199. #else
  200. OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE);
  201. #endif
  202. #endif
  203. }
  204. #if HAS_SERVOS
  205. HAL_SERVO_LIB servo[NUM_SERVOS];
  206. void servo_init() {
  207. #if NUM_SERVOS >= 1 && HAS_SERVO_0
  208. servo[0].attach(SERVO0_PIN);
  209. servo[0].detach(); // Just set up the pin. We don't have a position yet. Don't move to a random position.
  210. #endif
  211. #if NUM_SERVOS >= 2 && HAS_SERVO_1
  212. servo[1].attach(SERVO1_PIN);
  213. servo[1].detach();
  214. #endif
  215. #if NUM_SERVOS >= 3 && HAS_SERVO_2
  216. servo[2].attach(SERVO2_PIN);
  217. servo[2].detach();
  218. #endif
  219. #if NUM_SERVOS >= 4 && HAS_SERVO_3
  220. servo[3].attach(SERVO3_PIN);
  221. servo[3].detach();
  222. #endif
  223. #if HAS_Z_SERVO_ENDSTOP
  224. servo_probe_init();
  225. #endif
  226. }
  227. #endif // HAS_SERVOS
  228. /**
  229. * Stepper Reset (RigidBoard, et.al.)
  230. */
  231. #if HAS_STEPPER_RESET
  232. void disableStepperDrivers() {
  233. OUT_WRITE(STEPPER_RESET_PIN, LOW); // drive it down to hold in reset motor driver chips
  234. }
  235. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // set to input, which allows it to be pulled high by pullups
  236. #endif
  237. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  238. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  239. i2c.receive(bytes);
  240. }
  241. void i2c_on_request() { // just send dummy data for now
  242. i2c.reply("Hello World!\n");
  243. }
  244. #endif
  245. /**
  246. * Sensitive pin test for M42, M226
  247. */
  248. bool pin_is_protected(const pin_t pin) {
  249. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  250. for (uint8_t i = 0; i < COUNT(sensitive_pins); i++) {
  251. pin_t sensitive_pin;
  252. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  253. if (pin == sensitive_pin) return true;
  254. }
  255. return false;
  256. }
  257. void quickstop_stepper() {
  258. stepper.quick_stop();
  259. stepper.synchronize();
  260. set_current_from_steppers_for_axis(ALL_AXES);
  261. SYNC_PLAN_POSITION_KINEMATIC();
  262. }
  263. void enable_all_steppers() {
  264. enable_X();
  265. enable_Y();
  266. enable_Z();
  267. enable_E0();
  268. enable_E1();
  269. enable_E2();
  270. enable_E3();
  271. enable_E4();
  272. }
  273. void disable_e_steppers() {
  274. disable_E0();
  275. disable_E1();
  276. disable_E2();
  277. disable_E3();
  278. disable_E4();
  279. }
  280. void disable_all_steppers() {
  281. disable_X();
  282. disable_Y();
  283. disable_Z();
  284. disable_e_steppers();
  285. }
  286. /**
  287. * Manage several activities:
  288. * - Check for Filament Runout
  289. * - Keep the command buffer full
  290. * - Check for maximum inactive time between commands
  291. * - Check for maximum inactive time between stepper commands
  292. * - Check if pin CHDK needs to go LOW
  293. * - Check for KILL button held down
  294. * - Check for HOME button held down
  295. * - Check if cooling fan needs to be switched on
  296. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  297. */
  298. void manage_inactivity(bool ignore_stepper_queue/*=false*/) {
  299. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  300. if ((IS_SD_PRINTING || print_job_timer.isRunning()) && (READ(FIL_RUNOUT_PIN) == FIL_RUNOUT_INVERTING))
  301. handle_filament_runout();
  302. #endif
  303. if (commands_in_queue < BUFSIZE) get_available_commands();
  304. const millis_t ms = millis();
  305. if (max_inactive_time && ELAPSED(ms, gcode.previous_cmd_ms + max_inactive_time)) {
  306. SERIAL_ERROR_START();
  307. SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
  308. kill(PSTR(MSG_KILLED));
  309. }
  310. // Prevent steppers timing-out in the middle of M600
  311. #if ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT)
  312. #define MOVE_AWAY_TEST !move_away_flag
  313. #else
  314. #define MOVE_AWAY_TEST true
  315. #endif
  316. if (MOVE_AWAY_TEST && stepper_inactive_time && ELAPSED(ms, gcode.previous_cmd_ms + stepper_inactive_time)
  317. && !ignore_stepper_queue && !planner.blocks_queued()) {
  318. #if ENABLED(DISABLE_INACTIVE_X)
  319. disable_X();
  320. #endif
  321. #if ENABLED(DISABLE_INACTIVE_Y)
  322. disable_Y();
  323. #endif
  324. #if ENABLED(DISABLE_INACTIVE_Z)
  325. disable_Z();
  326. #endif
  327. #if ENABLED(DISABLE_INACTIVE_E)
  328. disable_e_steppers();
  329. #endif
  330. #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(ULTRA_LCD) // Only needed with an LCD
  331. ubl.lcd_map_control = defer_return_to_status = false;
  332. #endif
  333. }
  334. #ifdef CHDK // Check if pin should be set to LOW after M240 set it to HIGH
  335. if (chdkActive && ELAPSED(ms, chdkHigh + CHDK_DELAY)) {
  336. chdkActive = false;
  337. WRITE(CHDK, LOW);
  338. }
  339. #endif
  340. #if HAS_KILL
  341. // Check if the kill button was pressed and wait just in case it was an accidental
  342. // key kill key press
  343. // -------------------------------------------------------------------------------
  344. static int killCount = 0; // make the inactivity button a bit less responsive
  345. const int KILL_DELAY = 750;
  346. if (!READ(KILL_PIN))
  347. killCount++;
  348. else if (killCount > 0)
  349. killCount--;
  350. // Exceeded threshold and we can confirm that it was not accidental
  351. // KILL the machine
  352. // ----------------------------------------------------------------
  353. if (killCount >= KILL_DELAY) {
  354. SERIAL_ERROR_START();
  355. SERIAL_ERRORLNPGM(MSG_KILL_BUTTON);
  356. kill(PSTR(MSG_KILLED));
  357. }
  358. #endif
  359. #if HAS_HOME
  360. // Check to see if we have to home, use poor man's debouncer
  361. // ---------------------------------------------------------
  362. static int homeDebounceCount = 0; // poor man's debouncing count
  363. const int HOME_DEBOUNCE_DELAY = 2500;
  364. if (!IS_SD_PRINTING && !READ(HOME_PIN)) {
  365. if (!homeDebounceCount) {
  366. enqueue_and_echo_commands_P(PSTR("G28"));
  367. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  368. }
  369. if (homeDebounceCount < HOME_DEBOUNCE_DELAY)
  370. homeDebounceCount++;
  371. else
  372. homeDebounceCount = 0;
  373. }
  374. #endif
  375. #if ENABLED(USE_CONTROLLER_FAN)
  376. controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
  377. #endif
  378. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  379. if (ELAPSED(ms, gcode.previous_cmd_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  380. && thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP) {
  381. #if ENABLED(SWITCHING_EXTRUDER)
  382. const bool oldstatus = E0_ENABLE_READ;
  383. enable_E0();
  384. #else // !SWITCHING_EXTRUDER
  385. bool oldstatus;
  386. switch (active_extruder) {
  387. default: oldstatus = E0_ENABLE_READ; enable_E0(); break;
  388. #if E_STEPPERS > 1
  389. case 1: oldstatus = E1_ENABLE_READ; enable_E1(); break;
  390. #if E_STEPPERS > 2
  391. case 2: oldstatus = E2_ENABLE_READ; enable_E2(); break;
  392. #if E_STEPPERS > 3
  393. case 3: oldstatus = E3_ENABLE_READ; enable_E3(); break;
  394. #if E_STEPPERS > 4
  395. case 4: oldstatus = E4_ENABLE_READ; enable_E4(); break;
  396. #endif // E_STEPPERS > 4
  397. #endif // E_STEPPERS > 3
  398. #endif // E_STEPPERS > 2
  399. #endif // E_STEPPERS > 1
  400. }
  401. #endif // !SWITCHING_EXTRUDER
  402. gcode.refresh_cmd_timeout()
  403. const float olde = current_position[E_AXIS];
  404. current_position[E_AXIS] += EXTRUDER_RUNOUT_EXTRUDE;
  405. planner.buffer_line_kinematic(current_position, MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED), active_extruder);
  406. current_position[E_AXIS] = olde;
  407. planner.set_e_position_mm(olde);
  408. stepper.synchronize();
  409. #if ENABLED(SWITCHING_EXTRUDER)
  410. E0_ENABLE_WRITE(oldstatus);
  411. #else
  412. switch (active_extruder) {
  413. case 0: E0_ENABLE_WRITE(oldstatus); break;
  414. #if E_STEPPERS > 1
  415. case 1: E1_ENABLE_WRITE(oldstatus); break;
  416. #if E_STEPPERS > 2
  417. case 2: E2_ENABLE_WRITE(oldstatus); break;
  418. #if E_STEPPERS > 3
  419. case 3: E3_ENABLE_WRITE(oldstatus); break;
  420. #if E_STEPPERS > 4
  421. case 4: E4_ENABLE_WRITE(oldstatus); break;
  422. #endif // E_STEPPERS > 4
  423. #endif // E_STEPPERS > 3
  424. #endif // E_STEPPERS > 2
  425. #endif // E_STEPPERS > 1
  426. }
  427. #endif // !SWITCHING_EXTRUDER
  428. }
  429. #endif // EXTRUDER_RUNOUT_PREVENT
  430. #if ENABLED(DUAL_X_CARRIAGE)
  431. // handle delayed move timeout
  432. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  433. // travel moves have been received so enact them
  434. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  435. set_destination_from_current();
  436. prepare_move_to_destination();
  437. }
  438. #endif
  439. #if ENABLED(TEMP_STAT_LEDS)
  440. handle_status_leds();
  441. #endif
  442. #if ENABLED(HAVE_TMC2130)
  443. tmc2130_checkOverTemp();
  444. #endif
  445. planner.check_axes_activity();
  446. }
  447. /**
  448. * Standard idle routine keeps the machine alive
  449. */
  450. void idle(
  451. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  452. bool no_stepper_sleep/*=false*/
  453. #endif
  454. ) {
  455. #if ENABLED(MAX7219_DEBUG)
  456. Max7219_idle_tasks();
  457. #endif // MAX7219_DEBUG
  458. lcd_update();
  459. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  460. gcode.host_keepalive();
  461. #endif
  462. #if ENABLED(AUTO_REPORT_TEMPERATURES) && (HAS_TEMP_HOTEND || HAS_TEMP_BED)
  463. thermalManager.auto_report_temperatures();
  464. #endif
  465. manage_inactivity(
  466. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  467. no_stepper_sleep
  468. #endif
  469. );
  470. thermalManager.manage_heater();
  471. #if ENABLED(PRINTCOUNTER)
  472. print_job_timer.tick();
  473. #endif
  474. #if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
  475. buzzer.tick();
  476. #endif
  477. #if ENABLED(I2C_POSITION_ENCODERS)
  478. if (planner.blocks_queued() &&
  479. ( (blockBufferIndexRef != planner.block_buffer_head) ||
  480. ((lastUpdateMillis + I2CPE_MIN_UPD_TIME_MS) < millis())) ) {
  481. blockBufferIndexRef = planner.block_buffer_head;
  482. I2CPEM.update();
  483. lastUpdateMillis = millis();
  484. }
  485. #endif
  486. }
  487. /**
  488. * Kill all activity and lock the machine.
  489. * After this the machine will need to be reset.
  490. */
  491. void kill(const char* lcd_msg) {
  492. SERIAL_ERROR_START();
  493. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  494. thermalManager.disable_all_heaters();
  495. disable_all_steppers();
  496. #if ENABLED(ULTRA_LCD)
  497. kill_screen(lcd_msg);
  498. #else
  499. UNUSED(lcd_msg);
  500. #endif
  501. _delay_ms(600); // Wait a short time (allows messages to get out before shutting down.
  502. cli(); // Stop interrupts
  503. _delay_ms(250); //Wait to ensure all interrupts routines stopped
  504. thermalManager.disable_all_heaters(); //turn off heaters again
  505. #ifdef ACTION_ON_KILL
  506. SERIAL_ECHOLNPGM("//action:" ACTION_ON_KILL);
  507. #endif
  508. #if HAS_POWER_SWITCH
  509. SET_INPUT(PS_ON_PIN);
  510. #endif
  511. #if HAS_SUICIDE
  512. suicide();
  513. #endif
  514. while (1) {
  515. #if ENABLED(USE_WATCHDOG)
  516. watchdog_reset();
  517. #endif
  518. } // Wait for reset
  519. }
  520. /**
  521. * Turn off heaters and stop the print in progress
  522. * After a stop the machine may be resumed with M999
  523. */
  524. void stop() {
  525. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  526. #if ENABLED(PROBING_FANS_OFF)
  527. if (fans_paused) fans_pause(false); // put things back the way they were
  528. #endif
  529. if (IsRunning()) {
  530. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  531. SERIAL_ERROR_START();
  532. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  533. LCD_MESSAGEPGM(MSG_STOPPED);
  534. safe_delay(350); // allow enough time for messages to get out before stopping
  535. Running = false;
  536. }
  537. }
  538. /**
  539. * Marlin entry-point: Set up before the program loop
  540. * - Set up the kill pin, filament runout, power hold
  541. * - Start the serial port
  542. * - Print startup messages and diagnostics
  543. * - Get EEPROM or default settings
  544. * - Initialize managers for:
  545. * • temperature
  546. * • planner
  547. * • watchdog
  548. * • stepper
  549. * • photo pin
  550. * • servos
  551. * • LCD controller
  552. * • Digipot I2C
  553. * • Z probe sled
  554. * • status LEDs
  555. */
  556. void setup() {
  557. #if ENABLED(MAX7219_DEBUG)
  558. Max7219_init();
  559. #endif
  560. #ifdef DISABLE_JTAG
  561. // Disable JTAG on AT90USB chips to free up pins for IO
  562. MCUCR = 0x80;
  563. MCUCR = 0x80;
  564. #endif
  565. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  566. setup_filrunoutpin();
  567. #endif
  568. setup_killpin();
  569. setup_powerhold();
  570. #if HAS_STEPPER_RESET
  571. disableStepperDrivers();
  572. #endif
  573. MYSERIAL.begin(BAUDRATE);
  574. uint32_t serial_connect_timeout = millis() + 1000;
  575. while(!MYSERIAL && PENDING(millis(), serial_connect_timeout));
  576. SERIAL_PROTOCOLLNPGM("start");
  577. SERIAL_ECHO_START();
  578. // Check startup - does nothing if bootloader sets MCUSR to 0
  579. byte mcu = HAL_get_reset_source();
  580. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  581. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  582. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  583. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  584. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  585. HAL_clear_reset_source();
  586. #if ENABLED(USE_WATCHDOG) //reinit watchdog after HAL_get_reset_source call
  587. watchdog_init();
  588. #endif
  589. SERIAL_ECHOPGM(MSG_MARLIN);
  590. SERIAL_CHAR(' ');
  591. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  592. SERIAL_EOL();
  593. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  594. SERIAL_ECHO_START();
  595. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  596. SERIAL_ECHOPGM(STRING_DISTRIBUTION_DATE);
  597. SERIAL_ECHOLNPGM(MSG_AUTHOR STRING_CONFIG_H_AUTHOR);
  598. SERIAL_ECHO_START();
  599. SERIAL_ECHOLNPGM("Compiled: " __DATE__);
  600. #endif
  601. SERIAL_ECHO_START();
  602. SERIAL_ECHOPAIR(MSG_FREE_MEMORY, freeMemory());
  603. SERIAL_ECHOLNPAIR(MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  604. queue_setup();
  605. // Load data from EEPROM if available (or use defaults)
  606. // This also updates variables in the planner, elsewhere
  607. (void)settings.load();
  608. #if HAS_M206_COMMAND
  609. // Initialize current position based on home_offset
  610. COPY(current_position, home_offset);
  611. #else
  612. ZERO(current_position);
  613. #endif
  614. // Vital to init stepper/planner equivalent for current_position
  615. SYNC_PLAN_POSITION_KINEMATIC();
  616. thermalManager.init(); // Initialize temperature loop
  617. stepper.init(); // Initialize stepper, this enables interrupts!
  618. #if HAS_SERVOS
  619. servo_init();
  620. #endif
  621. #if HAS_PHOTOGRAPH
  622. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  623. #endif
  624. #if HAS_CASE_LIGHT
  625. update_case_light();
  626. #endif
  627. #if ENABLED(SPINDLE_LASER_ENABLE)
  628. OUT_WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); // init spindle to off
  629. #if SPINDLE_DIR_CHANGE
  630. OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // init rotation to clockwise (M3)
  631. #endif
  632. #if ENABLED(SPINDLE_LASER_PWM) && defined(SPINDLE_LASER_PWM_PIN) && SPINDLE_LASER_PWM_PIN >= 0
  633. SET_OUTPUT(SPINDLE_LASER_PWM_PIN);
  634. analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // set to lowest speed
  635. #endif
  636. #endif
  637. #if HAS_BED_PROBE
  638. endstops.enable_z_probe(false);
  639. #endif
  640. #if ENABLED(USE_CONTROLLER_FAN)
  641. SET_OUTPUT(CONTROLLER_FAN_PIN); //Set pin used for driver cooling fan
  642. #endif
  643. #if HAS_STEPPER_RESET
  644. enableStepperDrivers();
  645. #endif
  646. #if ENABLED(DIGIPOT_I2C)
  647. digipot_i2c_init();
  648. #endif
  649. #if ENABLED(DAC_STEPPER_CURRENT)
  650. dac_init();
  651. #endif
  652. #if (ENABLED(Z_PROBE_SLED) || ENABLED(SOLENOID_PROBE)) && HAS_SOLENOID_1
  653. OUT_WRITE(SOL1_PIN, LOW); // turn it off
  654. #endif
  655. #if HAS_HOME
  656. SET_INPUT_PULLUP(HOME_PIN);
  657. #endif
  658. #if PIN_EXISTS(STAT_LED_RED)
  659. OUT_WRITE(STAT_LED_RED_PIN, LOW); // turn it off
  660. #endif
  661. #if PIN_EXISTS(STAT_LED_BLUE)
  662. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // turn it off
  663. #endif
  664. #if ENABLED(NEOPIXEL_LED)
  665. setup_neopixel();
  666. #endif
  667. #if ENABLED(RGB_LED) || ENABLED(RGBW_LED)
  668. SET_OUTPUT(RGB_LED_R_PIN);
  669. SET_OUTPUT(RGB_LED_G_PIN);
  670. SET_OUTPUT(RGB_LED_B_PIN);
  671. #if ENABLED(RGBW_LED)
  672. SET_OUTPUT(RGB_LED_W_PIN);
  673. #endif
  674. #endif
  675. #if ENABLED(MK2_MULTIPLEXER)
  676. SET_OUTPUT(E_MUX0_PIN);
  677. SET_OUTPUT(E_MUX1_PIN);
  678. SET_OUTPUT(E_MUX2_PIN);
  679. #endif
  680. #if HAS_FANMUX
  681. fanmux_init();
  682. #endif
  683. lcd_init();
  684. #if ENABLED(SHOW_BOOTSCREEN)
  685. lcd_bootscreen();
  686. #if ENABLED(ULTRA_LCD) && DISABLED(SDSUPPORT)
  687. lcd_init();
  688. #endif
  689. #endif
  690. #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1
  691. mixing_tools_init();
  692. #endif
  693. #if ENABLED(BLTOUCH)
  694. bltouch_init();
  695. #endif
  696. #if ENABLED(I2C_POSITION_ENCODERS)
  697. I2CPEM.init();
  698. #endif
  699. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  700. i2c.onReceive(i2c_on_receive);
  701. i2c.onRequest(i2c_on_request);
  702. #endif
  703. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  704. setup_endstop_interrupts();
  705. #endif
  706. #if ENABLED(SWITCHING_EXTRUDER) && !DONT_SWITCH
  707. move_extruder_servo(0); // Initialize extruder servo
  708. #endif
  709. #if ENABLED(SWITCHING_NOZZLE)
  710. move_nozzle_servo(0); // Initialize nozzle servo
  711. #endif
  712. #if ENABLED(PARKING_EXTRUDER)
  713. pe_magnet_init();
  714. #endif
  715. }
  716. /**
  717. * The main Marlin program loop
  718. *
  719. * - Save or log commands to SD
  720. * - Process available commands (if not saving)
  721. * - Call heater manager
  722. * - Call inactivity manager
  723. * - Call endstop manager
  724. * - Call LCD update
  725. */
  726. void loop() {
  727. if (commands_in_queue < BUFSIZE) get_available_commands();
  728. #if ENABLED(SDSUPPORT)
  729. card.checkautostart(false);
  730. #endif
  731. advance_command_queue();
  732. endstops.report_state();
  733. idle();
  734. }