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

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