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

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