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

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