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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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 (ENABLED(SWITCHING_EXTRUDER) && !DONT_SWITCH) || ENABLED(SWITCHING_NOZZLE) || ENABLED(PARKING_EXTRUDER)
  116. #include "module/tool_change.h"
  117. #endif
  118. #if ENABLED(USE_CONTROLLER_FAN)
  119. #include "feature/controllerfan.h"
  120. #endif
  121. bool Running = true;
  122. /**
  123. * axis_homed
  124. * Flags that each linear axis was homed.
  125. * XYZ on cartesian, ABC on delta, ABZ on SCARA.
  126. *
  127. * axis_known_position
  128. * Flags that the position is known in each linear axis. Set when homed.
  129. * Cleared whenever a stepper powers off, potentially losing its position.
  130. */
  131. bool axis_homed[XYZ] = { false }, axis_known_position[XYZ] = { false };
  132. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  133. TempUnit input_temp_units = TEMPUNIT_C;
  134. #endif
  135. #if FAN_COUNT > 0
  136. int16_t fanSpeeds[FAN_COUNT] = { 0 };
  137. #if ENABLED(EXTRA_FAN_SPEEDS)
  138. int16_t old_fanSpeeds[FAN_COUNT],
  139. new_fanSpeeds[FAN_COUNT];
  140. #endif
  141. #if ENABLED(PROBING_FANS_OFF)
  142. bool fans_paused = false;
  143. int16_t paused_fanSpeeds[FAN_COUNT] = { 0 };
  144. #endif
  145. #endif
  146. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  147. volatile bool wait_for_heatup = true;
  148. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  149. #if HAS_RESUME_CONTINUE
  150. volatile bool wait_for_user = false;
  151. #endif
  152. // Inactivity shutdown
  153. millis_t max_inactive_time = 0,
  154. stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
  155. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  156. AdvancedPauseMenuResponse advanced_pause_menu_response;
  157. #endif
  158. #ifdef CHDK
  159. millis_t chdkHigh = 0;
  160. bool chdkActive = false;
  161. #endif
  162. #if ENABLED(PID_EXTRUSION_SCALING)
  163. int lpq_len = 20;
  164. #endif
  165. #if ENABLED(I2C_POSITION_ENCODERS)
  166. I2CPositionEncodersMgr I2CPEM;
  167. uint8_t blockBufferIndexRef = 0;
  168. millis_t lastUpdateMillis;
  169. #endif
  170. /**
  171. * ***************************************************************************
  172. * ******************************** FUNCTIONS ********************************
  173. * ***************************************************************************
  174. */
  175. void setup_killpin() {
  176. #if HAS_KILL
  177. SET_INPUT_PULLUP(KILL_PIN);
  178. #endif
  179. }
  180. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  181. void setup_filrunoutpin() {
  182. #if ENABLED(ENDSTOPPULLUP_FIL_RUNOUT)
  183. SET_INPUT_PULLUP(FIL_RUNOUT_PIN);
  184. #else
  185. SET_INPUT(FIL_RUNOUT_PIN);
  186. #endif
  187. }
  188. #endif
  189. void setup_powerhold() {
  190. #if HAS_SUICIDE
  191. OUT_WRITE(SUICIDE_PIN, HIGH);
  192. #endif
  193. #if HAS_POWER_SWITCH
  194. #if ENABLED(PS_DEFAULT_OFF)
  195. OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  196. #else
  197. OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE);
  198. #endif
  199. #endif
  200. }
  201. #if HAS_SERVOS
  202. HAL_SERVO_LIB servo[NUM_SERVOS];
  203. void servo_init() {
  204. #if NUM_SERVOS >= 1 && HAS_SERVO_0
  205. servo[0].attach(SERVO0_PIN);
  206. servo[0].detach(); // Just set up the pin. We don't have a position yet. Don't move to a random position.
  207. #endif
  208. #if NUM_SERVOS >= 2 && HAS_SERVO_1
  209. servo[1].attach(SERVO1_PIN);
  210. servo[1].detach();
  211. #endif
  212. #if NUM_SERVOS >= 3 && HAS_SERVO_2
  213. servo[2].attach(SERVO2_PIN);
  214. servo[2].detach();
  215. #endif
  216. #if NUM_SERVOS >= 4 && HAS_SERVO_3
  217. servo[3].attach(SERVO3_PIN);
  218. servo[3].detach();
  219. #endif
  220. #if HAS_Z_SERVO_ENDSTOP
  221. servo_probe_init();
  222. #endif
  223. }
  224. #endif // HAS_SERVOS
  225. /**
  226. * Stepper Reset (RigidBoard, et.al.)
  227. */
  228. #if HAS_STEPPER_RESET
  229. void disableStepperDrivers() {
  230. OUT_WRITE(STEPPER_RESET_PIN, LOW); // drive it down to hold in reset motor driver chips
  231. }
  232. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // set to input, which allows it to be pulled high by pullups
  233. #endif
  234. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  235. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  236. i2c.receive(bytes);
  237. }
  238. void i2c_on_request() { // just send dummy data for now
  239. i2c.reply("Hello World!\n");
  240. }
  241. #endif
  242. /**
  243. * Sensitive pin test for M42, M226
  244. */
  245. bool pin_is_protected(const int8_t pin) {
  246. static const int8_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  247. for (uint8_t i = 0; i < COUNT(sensitive_pins); i++)
  248. if (pin == (int8_t)pgm_read_byte(&sensitive_pins[i])) return true;
  249. return false;
  250. }
  251. void quickstop_stepper() {
  252. stepper.quick_stop();
  253. stepper.synchronize();
  254. set_current_from_steppers_for_axis(ALL_AXES);
  255. SYNC_PLAN_POSITION_KINEMATIC();
  256. }
  257. void enable_all_steppers() {
  258. enable_X();
  259. enable_Y();
  260. enable_Z();
  261. enable_E0();
  262. enable_E1();
  263. enable_E2();
  264. enable_E3();
  265. enable_E4();
  266. }
  267. void disable_e_steppers() {
  268. disable_E0();
  269. disable_E1();
  270. disable_E2();
  271. disable_E3();
  272. disable_E4();
  273. }
  274. void disable_all_steppers() {
  275. disable_X();
  276. disable_Y();
  277. disable_Z();
  278. disable_e_steppers();
  279. }
  280. /**
  281. * Manage several activities:
  282. * - Check for Filament Runout
  283. * - Keep the command buffer full
  284. * - Check for maximum inactive time between commands
  285. * - Check for maximum inactive time between stepper commands
  286. * - Check if pin CHDK needs to go LOW
  287. * - Check for KILL button held down
  288. * - Check for HOME button held down
  289. * - Check if cooling fan needs to be switched on
  290. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  291. */
  292. void manage_inactivity(bool ignore_stepper_queue/*=false*/) {
  293. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  294. if ((IS_SD_PRINTING || print_job_timer.isRunning()) && (READ(FIL_RUNOUT_PIN) == FIL_RUNOUT_INVERTING))
  295. handle_filament_runout();
  296. #endif
  297. if (commands_in_queue < BUFSIZE) get_available_commands();
  298. const millis_t ms = millis();
  299. if (max_inactive_time && ELAPSED(ms, gcode.previous_cmd_ms + max_inactive_time)) {
  300. SERIAL_ERROR_START();
  301. SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
  302. kill(PSTR(MSG_KILLED));
  303. }
  304. // Prevent steppers timing-out in the middle of M600
  305. #if ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT)
  306. #define MOVE_AWAY_TEST !move_away_flag
  307. #else
  308. #define MOVE_AWAY_TEST true
  309. #endif
  310. if (MOVE_AWAY_TEST && stepper_inactive_time && ELAPSED(ms, gcode.previous_cmd_ms + stepper_inactive_time)
  311. && !ignore_stepper_queue && !planner.blocks_queued()) {
  312. #if ENABLED(DISABLE_INACTIVE_X)
  313. disable_X();
  314. #endif
  315. #if ENABLED(DISABLE_INACTIVE_Y)
  316. disable_Y();
  317. #endif
  318. #if ENABLED(DISABLE_INACTIVE_Z)
  319. disable_Z();
  320. #endif
  321. #if ENABLED(DISABLE_INACTIVE_E)
  322. disable_e_steppers();
  323. #endif
  324. #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(ULTRA_LCD) // Only needed with an LCD
  325. ubl.lcd_map_control = defer_return_to_status = false;
  326. #endif
  327. }
  328. #ifdef CHDK // Check if pin should be set to LOW after M240 set it to HIGH
  329. if (chdkActive && ELAPSED(ms, chdkHigh + CHDK_DELAY)) {
  330. chdkActive = false;
  331. WRITE(CHDK, LOW);
  332. }
  333. #endif
  334. #if HAS_KILL
  335. // Check if the kill button was pressed and wait just in case it was an accidental
  336. // key kill key press
  337. // -------------------------------------------------------------------------------
  338. static int killCount = 0; // make the inactivity button a bit less responsive
  339. const int KILL_DELAY = 750;
  340. if (!READ(KILL_PIN))
  341. killCount++;
  342. else if (killCount > 0)
  343. killCount--;
  344. // Exceeded threshold and we can confirm that it was not accidental
  345. // KILL the machine
  346. // ----------------------------------------------------------------
  347. if (killCount >= KILL_DELAY) {
  348. SERIAL_ERROR_START();
  349. SERIAL_ERRORLNPGM(MSG_KILL_BUTTON);
  350. kill(PSTR(MSG_KILLED));
  351. }
  352. #endif
  353. #if HAS_HOME
  354. // Check to see if we have to home, use poor man's debouncer
  355. // ---------------------------------------------------------
  356. static int homeDebounceCount = 0; // poor man's debouncing count
  357. const int HOME_DEBOUNCE_DELAY = 2500;
  358. if (!IS_SD_PRINTING && !READ(HOME_PIN)) {
  359. if (!homeDebounceCount) {
  360. enqueue_and_echo_commands_P(PSTR("G28"));
  361. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  362. }
  363. if (homeDebounceCount < HOME_DEBOUNCE_DELAY)
  364. homeDebounceCount++;
  365. else
  366. homeDebounceCount = 0;
  367. }
  368. #endif
  369. #if ENABLED(USE_CONTROLLER_FAN)
  370. controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
  371. #endif
  372. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  373. if (ELAPSED(ms, gcode.previous_cmd_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  374. && thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP) {
  375. #if ENABLED(SWITCHING_EXTRUDER)
  376. const bool oldstatus = E0_ENABLE_READ;
  377. enable_E0();
  378. #else // !SWITCHING_EXTRUDER
  379. bool oldstatus;
  380. switch (active_extruder) {
  381. default: oldstatus = E0_ENABLE_READ; enable_E0(); break;
  382. #if E_STEPPERS > 1
  383. case 1: oldstatus = E1_ENABLE_READ; enable_E1(); break;
  384. #if E_STEPPERS > 2
  385. case 2: oldstatus = E2_ENABLE_READ; enable_E2(); break;
  386. #if E_STEPPERS > 3
  387. case 3: oldstatus = E3_ENABLE_READ; enable_E3(); break;
  388. #if E_STEPPERS > 4
  389. case 4: oldstatus = E4_ENABLE_READ; enable_E4(); break;
  390. #endif // E_STEPPERS > 4
  391. #endif // E_STEPPERS > 3
  392. #endif // E_STEPPERS > 2
  393. #endif // E_STEPPERS > 1
  394. }
  395. #endif // !SWITCHING_EXTRUDER
  396. gcode.refresh_cmd_timeout()
  397. const float olde = current_position[E_AXIS];
  398. current_position[E_AXIS] += EXTRUDER_RUNOUT_EXTRUDE;
  399. planner.buffer_line_kinematic(current_position, MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED), active_extruder);
  400. current_position[E_AXIS] = olde;
  401. planner.set_e_position_mm(olde);
  402. stepper.synchronize();
  403. #if ENABLED(SWITCHING_EXTRUDER)
  404. E0_ENABLE_WRITE(oldstatus);
  405. #else
  406. switch (active_extruder) {
  407. case 0: E0_ENABLE_WRITE(oldstatus); break;
  408. #if E_STEPPERS > 1
  409. case 1: E1_ENABLE_WRITE(oldstatus); break;
  410. #if E_STEPPERS > 2
  411. case 2: E2_ENABLE_WRITE(oldstatus); break;
  412. #if E_STEPPERS > 3
  413. case 3: E3_ENABLE_WRITE(oldstatus); break;
  414. #if E_STEPPERS > 4
  415. case 4: E4_ENABLE_WRITE(oldstatus); break;
  416. #endif // E_STEPPERS > 4
  417. #endif // E_STEPPERS > 3
  418. #endif // E_STEPPERS > 2
  419. #endif // E_STEPPERS > 1
  420. }
  421. #endif // !SWITCHING_EXTRUDER
  422. }
  423. #endif // EXTRUDER_RUNOUT_PREVENT
  424. #if ENABLED(DUAL_X_CARRIAGE)
  425. // handle delayed move timeout
  426. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  427. // travel moves have been received so enact them
  428. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  429. set_destination_from_current();
  430. prepare_move_to_destination();
  431. }
  432. #endif
  433. #if ENABLED(TEMP_STAT_LEDS)
  434. handle_status_leds();
  435. #endif
  436. #if ENABLED(HAVE_TMC2130)
  437. tmc2130_checkOverTemp();
  438. #endif
  439. planner.check_axes_activity();
  440. }
  441. /**
  442. * Standard idle routine keeps the machine alive
  443. */
  444. void idle(
  445. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  446. bool no_stepper_sleep/*=false*/
  447. #endif
  448. ) {
  449. #if ENABLED(MAX7219_DEBUG)
  450. Max7219_idle_tasks();
  451. #endif // MAX7219_DEBUG
  452. lcd_update();
  453. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  454. gcode.host_keepalive();
  455. #endif
  456. #if ENABLED(AUTO_REPORT_TEMPERATURES) && (HAS_TEMP_HOTEND || HAS_TEMP_BED)
  457. thermalManager.auto_report_temperatures();
  458. #endif
  459. manage_inactivity(
  460. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  461. no_stepper_sleep
  462. #endif
  463. );
  464. thermalManager.manage_heater();
  465. #if ENABLED(PRINTCOUNTER)
  466. print_job_timer.tick();
  467. #endif
  468. #if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
  469. buzzer.tick();
  470. #endif
  471. #if ENABLED(I2C_POSITION_ENCODERS)
  472. if (planner.blocks_queued() &&
  473. ( (blockBufferIndexRef != planner.block_buffer_head) ||
  474. ((lastUpdateMillis + I2CPE_MIN_UPD_TIME_MS) < millis())) ) {
  475. blockBufferIndexRef = planner.block_buffer_head;
  476. I2CPEM.update();
  477. lastUpdateMillis = millis();
  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. SET_INPUT(PS_ON_PIN);
  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. #if ENABLED(MAX7219_DEBUG)
  552. Max7219_init();
  553. #endif
  554. #ifdef DISABLE_JTAG
  555. // Disable JTAG on AT90USB chips to free up pins for IO
  556. MCUCR = 0x80;
  557. MCUCR = 0x80;
  558. #endif
  559. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  560. setup_filrunoutpin();
  561. #endif
  562. setup_killpin();
  563. setup_powerhold();
  564. #if HAS_STEPPER_RESET
  565. disableStepperDrivers();
  566. #endif
  567. MYSERIAL.begin(BAUDRATE);
  568. uint32_t serial_connect_timeout = millis() + 1000;
  569. while(!MYSERIAL && PENDING(millis(), serial_connect_timeout));
  570. SERIAL_PROTOCOLLNPGM("start");
  571. SERIAL_ECHO_START();
  572. // Check startup - does nothing if bootloader sets MCUSR to 0
  573. byte mcu = HAL_get_reset_source();
  574. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  575. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  576. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  577. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  578. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  579. HAL_clear_reset_source();
  580. #if ENABLED(USE_WATCHDOG) //reinit watchdog after HAL_get_reset_source call
  581. watchdog_init();
  582. #endif
  583. SERIAL_ECHOPGM(MSG_MARLIN);
  584. SERIAL_CHAR(' ');
  585. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  586. SERIAL_EOL();
  587. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  588. SERIAL_ECHO_START();
  589. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  590. SERIAL_ECHOPGM(STRING_DISTRIBUTION_DATE);
  591. SERIAL_ECHOLNPGM(MSG_AUTHOR STRING_CONFIG_H_AUTHOR);
  592. SERIAL_ECHO_START();
  593. SERIAL_ECHOLNPGM("Compiled: " __DATE__);
  594. #endif
  595. SERIAL_ECHO_START();
  596. SERIAL_ECHOPAIR(MSG_FREE_MEMORY, freeMemory());
  597. SERIAL_ECHOLNPAIR(MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  598. queue_setup();
  599. // Load data from EEPROM if available (or use defaults)
  600. // This also updates variables in the planner, elsewhere
  601. (void)settings.load();
  602. #if HAS_M206_COMMAND
  603. // Initialize current position based on home_offset
  604. COPY(current_position, home_offset);
  605. #else
  606. ZERO(current_position);
  607. #endif
  608. // Vital to init stepper/planner equivalent for current_position
  609. SYNC_PLAN_POSITION_KINEMATIC();
  610. thermalManager.init(); // Initialize temperature loop
  611. stepper.init(); // Initialize stepper, this enables interrupts!
  612. #if HAS_SERVOS
  613. servo_init();
  614. #endif
  615. #if HAS_PHOTOGRAPH
  616. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  617. #endif
  618. #if HAS_CASE_LIGHT
  619. update_case_light();
  620. #endif
  621. #if ENABLED(SPINDLE_LASER_ENABLE)
  622. OUT_WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); // init spindle to off
  623. #if SPINDLE_DIR_CHANGE
  624. OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // init rotation to clockwise (M3)
  625. #endif
  626. #if ENABLED(SPINDLE_LASER_PWM) && defined(SPINDLE_LASER_PWM_PIN) && SPINDLE_LASER_PWM_PIN >= 0
  627. SET_OUTPUT(SPINDLE_LASER_PWM_PIN);
  628. analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // set to lowest speed
  629. #endif
  630. #endif
  631. #if HAS_BED_PROBE
  632. endstops.enable_z_probe(false);
  633. #endif
  634. #if ENABLED(USE_CONTROLLER_FAN)
  635. SET_OUTPUT(CONTROLLER_FAN_PIN); //Set pin used for driver cooling fan
  636. #endif
  637. #if HAS_STEPPER_RESET
  638. enableStepperDrivers();
  639. #endif
  640. #if ENABLED(DIGIPOT_I2C)
  641. digipot_i2c_init();
  642. #endif
  643. #if ENABLED(DAC_STEPPER_CURRENT)
  644. dac_init();
  645. #endif
  646. #if (ENABLED(Z_PROBE_SLED) || ENABLED(SOLENOID_PROBE)) && HAS_SOLENOID_1
  647. OUT_WRITE(SOL1_PIN, LOW); // turn it off
  648. #endif
  649. #if HAS_HOME
  650. SET_INPUT_PULLUP(HOME_PIN);
  651. #endif
  652. #if PIN_EXISTS(STAT_LED_RED)
  653. OUT_WRITE(STAT_LED_RED_PIN, LOW); // turn it off
  654. #endif
  655. #if PIN_EXISTS(STAT_LED_BLUE)
  656. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // turn it off
  657. #endif
  658. #if ENABLED(NEOPIXEL_LED)
  659. setup_neopixel();
  660. #endif
  661. #if ENABLED(RGB_LED) || ENABLED(RGBW_LED)
  662. SET_OUTPUT(RGB_LED_R_PIN);
  663. SET_OUTPUT(RGB_LED_G_PIN);
  664. SET_OUTPUT(RGB_LED_B_PIN);
  665. #if ENABLED(RGBW_LED)
  666. SET_OUTPUT(RGB_LED_W_PIN);
  667. #endif
  668. #endif
  669. #if ENABLED(MK2_MULTIPLEXER)
  670. SET_OUTPUT(E_MUX0_PIN);
  671. SET_OUTPUT(E_MUX1_PIN);
  672. SET_OUTPUT(E_MUX2_PIN);
  673. #endif
  674. #if HAS_FANMUX
  675. fanmux_init();
  676. #endif
  677. lcd_init();
  678. #ifndef CUSTOM_BOOTSCREEN_TIMEOUT
  679. #define CUSTOM_BOOTSCREEN_TIMEOUT 2500
  680. #endif
  681. #if ENABLED(SHOW_BOOTSCREEN)
  682. #if ENABLED(DOGLCD) // On DOGM the first bootscreen is already drawn
  683. #if ENABLED(SHOW_CUSTOM_BOOTSCREEN)
  684. safe_delay(CUSTOM_BOOTSCREEN_TIMEOUT); // Custom boot screen pause
  685. lcd_bootscreen(); // Show Marlin boot screen
  686. #endif
  687. safe_delay(BOOTSCREEN_TIMEOUT); // Pause
  688. #elif ENABLED(ULTRA_LCD)
  689. lcd_bootscreen();
  690. #if DISABLED(SDSUPPORT)
  691. lcd_init();
  692. #endif
  693. #endif
  694. #endif
  695. #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1
  696. mixing_tools_init();
  697. #endif
  698. #if ENABLED(BLTOUCH)
  699. bltouch_init();
  700. #endif
  701. #if ENABLED(I2C_POSITION_ENCODERS)
  702. I2CPEM.init();
  703. #endif
  704. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  705. i2c.onReceive(i2c_on_receive);
  706. i2c.onRequest(i2c_on_request);
  707. #endif
  708. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  709. setup_endstop_interrupts();
  710. #endif
  711. #if ENABLED(SWITCHING_EXTRUDER) && !DONT_SWITCH
  712. move_extruder_servo(0); // Initialize extruder servo
  713. #endif
  714. #if ENABLED(SWITCHING_NOZZLE)
  715. move_nozzle_servo(0); // Initialize nozzle servo
  716. #endif
  717. #if ENABLED(PARKING_EXTRUDER)
  718. pe_magnet_init();
  719. #endif
  720. }
  721. /**
  722. * The main Marlin program loop
  723. *
  724. * - Save or log commands to SD
  725. * - Process available commands (if not saving)
  726. * - Call heater manager
  727. * - Call inactivity manager
  728. * - Call endstop manager
  729. * - Call LCD update
  730. */
  731. void loop() {
  732. if (commands_in_queue < BUFSIZE) get_available_commands();
  733. #if ENABLED(SDSUPPORT)
  734. card.checkautostart(false);
  735. #endif
  736. advance_command_queue();
  737. endstops.report_state();
  738. idle();
  739. }