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

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