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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 "core/utility.h"
  31. #include "lcd/ultralcd.h"
  32. #include "module/motion.h"
  33. #include "module/planner.h"
  34. #include "module/stepper.h"
  35. #include "module/endstops.h"
  36. #include "module/probe.h"
  37. #include "module/temperature.h"
  38. #include "sd/cardreader.h"
  39. #include "module/configuration_store.h"
  40. #include "module/printcounter.h" // PrintCounter or Stopwatch
  41. #include "feature/closedloop.h"
  42. #include "HAL/shared/Delay.h"
  43. #include "module/stepper/indirection.h"
  44. #ifdef ARDUINO
  45. #include <pins_arduino.h>
  46. #endif
  47. #include <math.h>
  48. #include "libs/nozzle.h"
  49. #include "gcode/gcode.h"
  50. #include "gcode/parser.h"
  51. #include "gcode/queue.h"
  52. #if ENABLED(TOUCH_BUTTONS)
  53. #include "feature/touch/xpt2046.h"
  54. #endif
  55. #if ENABLED(HOST_ACTION_COMMANDS)
  56. #include "feature/host_actions.h"
  57. #endif
  58. #if USE_BEEPER
  59. #include "libs/buzzer.h"
  60. #endif
  61. #if ENABLED(DIGIPOT_I2C)
  62. #include "feature/digipot/digipot.h"
  63. #endif
  64. #if ENABLED(MIXING_EXTRUDER)
  65. #include "feature/mixing.h"
  66. #endif
  67. #if ENABLED(MAX7219_DEBUG)
  68. #include "feature/Max7219_Debug_LEDs.h"
  69. #endif
  70. #if HAS_COLOR_LEDS
  71. #include "feature/leds/leds.h"
  72. #endif
  73. #if ENABLED(BLTOUCH)
  74. #include "feature/bltouch.h"
  75. #endif
  76. #if ENABLED(POLL_JOG)
  77. #include "feature/joystick.h"
  78. #endif
  79. #if HAS_SERVOS
  80. #include "module/servo.h"
  81. #endif
  82. #if ENABLED(DAC_STEPPER_CURRENT)
  83. #include "feature/dac/stepper_dac.h"
  84. #endif
  85. #if ENABLED(EXPERIMENTAL_I2CBUS)
  86. #include "feature/twibus.h"
  87. TWIBus i2c;
  88. #endif
  89. #if ENABLED(I2C_POSITION_ENCODERS)
  90. #include "feature/I2CPositionEncoder.h"
  91. #endif
  92. #if HAS_TRINAMIC && DISABLED(PS_DEFAULT_OFF)
  93. #include "feature/tmc_util.h"
  94. #endif
  95. #if HAS_CUTTER
  96. #include "feature/spindle_laser.h"
  97. #endif
  98. #if ENABLED(SDSUPPORT)
  99. CardReader card;
  100. #endif
  101. #if ENABLED(G38_PROBE_TARGET)
  102. uint8_t G38_move; // = 0
  103. bool G38_did_trigger; // = false
  104. #endif
  105. #if ENABLED(DELTA)
  106. #include "module/delta.h"
  107. #elif IS_SCARA
  108. #include "module/scara.h"
  109. #endif
  110. #if HAS_LEVELING
  111. #include "feature/bedlevel/bedlevel.h"
  112. #endif
  113. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  114. #include "feature/pause.h"
  115. #endif
  116. #if ENABLED(POWER_LOSS_RECOVERY)
  117. #include "feature/power_loss_recovery.h"
  118. #endif
  119. #if ENABLED(CANCEL_OBJECTS)
  120. #include "feature/cancel_object.h"
  121. #endif
  122. #if HAS_FILAMENT_SENSOR
  123. #include "feature/runout.h"
  124. #endif
  125. #if ENABLED(TEMP_STAT_LEDS)
  126. #include "feature/leds/tempstat.h"
  127. #endif
  128. #if HAS_CASE_LIGHT
  129. #include "feature/caselight.h"
  130. #endif
  131. #if HAS_FANMUX
  132. #include "feature/fanmux.h"
  133. #endif
  134. #if DO_SWITCH_EXTRUDER || ANY(SWITCHING_NOZZLE, PARKING_EXTRUDER, MAGNETIC_PARKING_EXTRUDER, ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  135. #include "module/tool_change.h"
  136. #endif
  137. #if ENABLED(USE_CONTROLLER_FAN)
  138. #include "feature/controllerfan.h"
  139. #endif
  140. #if ENABLED(PRUSA_MMU2)
  141. #include "feature/prusa_MMU2/mmu2.h"
  142. #endif
  143. #if HAS_DRIVER(L6470)
  144. #include "libs/L6470/L6470_Marlin.h"
  145. #endif
  146. bool Running = true;
  147. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  148. bool wait_for_heatup = true;
  149. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  150. #if HAS_RESUME_CONTINUE
  151. bool wait_for_user; // = false;
  152. #endif
  153. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  154. bool suspend_auto_report; // = false
  155. #endif
  156. // Inactivity shutdown
  157. millis_t max_inactive_time, // = 0
  158. stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
  159. #if PIN_EXISTS(CHDK)
  160. extern millis_t chdk_timeout;
  161. #endif
  162. #if ENABLED(I2C_POSITION_ENCODERS)
  163. I2CPositionEncodersMgr I2CPEM;
  164. #endif
  165. /**
  166. * ***************************************************************************
  167. * ******************************** FUNCTIONS ********************************
  168. * ***************************************************************************
  169. */
  170. void setup_killpin() {
  171. #if HAS_KILL
  172. SET_INPUT_PULLUP(KILL_PIN);
  173. #endif
  174. }
  175. void setup_powerhold() {
  176. #if HAS_SUICIDE
  177. OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
  178. #endif
  179. #if ENABLED(PSU_CONTROL)
  180. #if ENABLED(PS_DEFAULT_OFF)
  181. powersupply_on = true; PSU_OFF();
  182. #else
  183. powersupply_on = false; PSU_ON();
  184. #endif
  185. #endif
  186. }
  187. /**
  188. * Stepper Reset (RigidBoard, et.al.)
  189. */
  190. #if HAS_STEPPER_RESET
  191. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  192. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  193. #endif
  194. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  195. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  196. i2c.receive(bytes);
  197. }
  198. void i2c_on_request() { // just send dummy data for now
  199. i2c.reply("Hello World!\n");
  200. }
  201. #endif
  202. /**
  203. * Sensitive pin test for M42, M226
  204. */
  205. #include "pins/sensitive_pins.h"
  206. bool pin_is_protected(const pin_t pin) {
  207. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  208. for (uint8_t i = 0; i < COUNT(sensitive_pins); i++) {
  209. pin_t sensitive_pin;
  210. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  211. if (pin == sensitive_pin) return true;
  212. }
  213. return false;
  214. }
  215. void protected_pin_err() {
  216. SERIAL_ERROR_MSG(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_e_steppers() {
  225. enable_E0(); enable_E1(); enable_E2(); enable_E3(); enable_E4(); enable_E5();
  226. }
  227. void enable_all_steppers() {
  228. #if ENABLED(AUTO_POWER_CONTROL)
  229. powerManager.power_on();
  230. #endif
  231. enable_X();
  232. enable_Y();
  233. enable_Z();
  234. enable_e_steppers();
  235. }
  236. void disable_e_steppers() {
  237. disable_E0(); disable_E1(); disable_E2(); disable_E3(); disable_E4(); 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. #if ENABLED(G29_RETRY_AND_RECOVER)
  256. void event_probe_failure() {
  257. #ifdef ACTION_ON_G29_FAILURE
  258. host_action(PSTR(ACTION_ON_G29_FAILURE));
  259. #endif
  260. #ifdef G29_FAILURE_COMMANDS
  261. gcode.process_subcommands_now_P(PSTR(G29_FAILURE_COMMANDS));
  262. #endif
  263. #if ENABLED(G29_HALT_ON_FAILURE)
  264. #ifdef ACTION_ON_CANCEL
  265. host_action_cancel();
  266. #endif
  267. kill(GET_TEXT(MSG_LCD_PROBING_FAILED));
  268. #endif
  269. }
  270. void event_probe_recover() {
  271. #if ENABLED(HOST_PROMPT_SUPPORT)
  272. host_prompt_do(PROMPT_INFO, PSTR("G29 Retrying"), PSTR("Dismiss"));
  273. #endif
  274. #ifdef ACTION_ON_G29_RECOVER
  275. host_action(PSTR(ACTION_ON_G29_RECOVER));
  276. #endif
  277. #ifdef G29_RECOVER_COMMANDS
  278. gcode.process_subcommands_now_P(PSTR(G29_RECOVER_COMMANDS));
  279. #endif
  280. }
  281. #endif
  282. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  283. #include "feature/pause.h"
  284. #else
  285. constexpr bool did_pause_print = false;
  286. #endif
  287. /**
  288. * Printing is active when the print job timer is running
  289. */
  290. bool printingIsActive() {
  291. return !did_pause_print && (print_job_timer.isRunning() || IS_SD_PRINTING());
  292. }
  293. /**
  294. * Printing is paused according to SD or host indicators
  295. */
  296. bool printingIsPaused() {
  297. return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
  298. }
  299. void startOrResumeJob() {
  300. if (!printingIsPaused()) {
  301. #if ENABLED(CANCEL_OBJECTS)
  302. cancelable.reset();
  303. #endif
  304. #if ENABLED(LCD_SHOW_E_TOTAL)
  305. e_move_accumulator = 0;
  306. #endif
  307. }
  308. print_job_timer.start();
  309. }
  310. /**
  311. * Manage several activities:
  312. * - Check for Filament Runout
  313. * - Keep the command buffer full
  314. * - Check for maximum inactive time between commands
  315. * - Check for maximum inactive time between stepper commands
  316. * - Check if CHDK_PIN needs to go LOW
  317. * - Check for KILL button held down
  318. * - Check for HOME button held down
  319. * - Check if cooling fan needs to be switched on
  320. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  321. * - Pulse FET_SAFETY_PIN if it exists
  322. */
  323. void manage_inactivity(const bool ignore_stepper_queue/*=false*/) {
  324. #if HAS_FILAMENT_SENSOR
  325. runout.run();
  326. #endif
  327. if (queue.length < BUFSIZE) queue.get_available_commands();
  328. const millis_t ms = millis();
  329. if (max_inactive_time && ELAPSED(ms, gcode.previous_move_ms + max_inactive_time)) {
  330. SERIAL_ERROR_START();
  331. SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
  332. kill();
  333. }
  334. // Prevent steppers timing-out in the middle of M600
  335. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  336. #define MOVE_AWAY_TEST !did_pause_print
  337. #else
  338. #define MOVE_AWAY_TEST true
  339. #endif
  340. if (stepper_inactive_time) {
  341. static bool already_shutdown_steppers; // = false
  342. if (planner.has_blocks_queued())
  343. gcode.reset_stepper_timeout();
  344. else if (MOVE_AWAY_TEST && !ignore_stepper_queue && ELAPSED(ms, gcode.previous_move_ms + stepper_inactive_time)) {
  345. if (!already_shutdown_steppers) {
  346. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  347. #if ENABLED(DISABLE_INACTIVE_X)
  348. disable_X();
  349. #endif
  350. #if ENABLED(DISABLE_INACTIVE_Y)
  351. disable_Y();
  352. #endif
  353. #if ENABLED(DISABLE_INACTIVE_Z)
  354. disable_Z();
  355. #endif
  356. #if ENABLED(DISABLE_INACTIVE_E)
  357. disable_e_steppers();
  358. #endif
  359. #if HAS_LCD_MENU && ENABLED(AUTO_BED_LEVELING_UBL)
  360. if (ubl.lcd_map_control) {
  361. ubl.lcd_map_control = false;
  362. ui.defer_status_screen(false);
  363. }
  364. #endif
  365. }
  366. }
  367. else
  368. already_shutdown_steppers = false;
  369. }
  370. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  371. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  372. chdk_timeout = 0;
  373. WRITE(CHDK_PIN, LOW);
  374. }
  375. #endif
  376. #if HAS_KILL
  377. // Check if the kill button was pressed and wait just in case it was an accidental
  378. // key kill key press
  379. // -------------------------------------------------------------------------------
  380. static int killCount = 0; // make the inactivity button a bit less responsive
  381. const int KILL_DELAY = 750;
  382. if (!READ(KILL_PIN))
  383. killCount++;
  384. else if (killCount > 0)
  385. killCount--;
  386. // Exceeded threshold and we can confirm that it was not accidental
  387. // KILL the machine
  388. // ----------------------------------------------------------------
  389. if (killCount >= KILL_DELAY) {
  390. SERIAL_ERROR_MSG(MSG_KILL_BUTTON);
  391. kill();
  392. }
  393. #endif
  394. #if HAS_HOME
  395. // Handle a standalone HOME button
  396. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  397. static millis_t next_home_key_ms; // = 0
  398. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  399. const millis_t ms = millis();
  400. if (ELAPSED(ms, next_home_key_ms)) {
  401. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  402. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  403. queue.enqueue_now_P(PSTR("G28"));
  404. }
  405. }
  406. #endif
  407. #if ENABLED(USE_CONTROLLER_FAN)
  408. controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
  409. #endif
  410. #if ENABLED(AUTO_POWER_CONTROL)
  411. powerManager.check();
  412. #endif
  413. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  414. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  415. && ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  416. && !planner.has_blocks_queued()
  417. ) {
  418. #if ENABLED(SWITCHING_EXTRUDER)
  419. bool oldstatus;
  420. switch (active_extruder) {
  421. default: oldstatus = E0_ENABLE_READ(); enable_E0(); break;
  422. #if E_STEPPERS > 1
  423. case 2: case 3: oldstatus = E1_ENABLE_READ(); enable_E1(); break;
  424. #if E_STEPPERS > 2
  425. case 4: case 5: oldstatus = E2_ENABLE_READ(); enable_E2(); break;
  426. #endif // E_STEPPERS > 2
  427. #endif // E_STEPPERS > 1
  428. }
  429. #else // !SWITCHING_EXTRUDER
  430. bool oldstatus;
  431. switch (active_extruder) {
  432. default: oldstatus = E0_ENABLE_READ(); enable_E0(); break;
  433. #if E_STEPPERS > 1
  434. case 1: oldstatus = E1_ENABLE_READ(); enable_E1(); break;
  435. #if E_STEPPERS > 2
  436. case 2: oldstatus = E2_ENABLE_READ(); enable_E2(); break;
  437. #if E_STEPPERS > 3
  438. case 3: oldstatus = E3_ENABLE_READ(); enable_E3(); break;
  439. #if E_STEPPERS > 4
  440. case 4: oldstatus = E4_ENABLE_READ(); enable_E4(); break;
  441. #if E_STEPPERS > 5
  442. case 5: oldstatus = E5_ENABLE_READ(); enable_E5(); break;
  443. #endif // E_STEPPERS > 5
  444. #endif // E_STEPPERS > 4
  445. #endif // E_STEPPERS > 3
  446. #endif // E_STEPPERS > 2
  447. #endif // E_STEPPERS > 1
  448. }
  449. #endif // !SWITCHING_EXTRUDER
  450. const float olde = current_position.e;
  451. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  452. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  453. current_position.e = olde;
  454. planner.set_e_position_mm(olde);
  455. planner.synchronize();
  456. #if ENABLED(SWITCHING_EXTRUDER)
  457. switch (active_extruder) {
  458. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  459. #if E_STEPPERS > 1
  460. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  461. #if E_STEPPERS > 2
  462. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  463. #endif // E_STEPPERS > 2
  464. #endif // E_STEPPERS > 1
  465. }
  466. #else // !SWITCHING_EXTRUDER
  467. switch (active_extruder) {
  468. case 0: E0_ENABLE_WRITE(oldstatus); break;
  469. #if E_STEPPERS > 1
  470. case 1: E1_ENABLE_WRITE(oldstatus); break;
  471. #if E_STEPPERS > 2
  472. case 2: E2_ENABLE_WRITE(oldstatus); break;
  473. #if E_STEPPERS > 3
  474. case 3: E3_ENABLE_WRITE(oldstatus); break;
  475. #if E_STEPPERS > 4
  476. case 4: E4_ENABLE_WRITE(oldstatus); break;
  477. #if E_STEPPERS > 5
  478. case 5: E5_ENABLE_WRITE(oldstatus); break;
  479. #endif // E_STEPPERS > 5
  480. #endif // E_STEPPERS > 4
  481. #endif // E_STEPPERS > 3
  482. #endif // E_STEPPERS > 2
  483. #endif // E_STEPPERS > 1
  484. }
  485. #endif // !SWITCHING_EXTRUDER
  486. gcode.reset_stepper_timeout();
  487. }
  488. #endif // EXTRUDER_RUNOUT_PREVENT
  489. #if ENABLED(DUAL_X_CARRIAGE)
  490. // handle delayed move timeout
  491. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  492. // travel moves have been received so enact them
  493. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  494. destination = current_position;
  495. prepare_move_to_destination();
  496. }
  497. #endif
  498. #if ENABLED(TEMP_STAT_LEDS)
  499. handle_status_leds();
  500. #endif
  501. #if ENABLED(MONITOR_DRIVER_STATUS)
  502. monitor_tmc_driver();
  503. #endif
  504. #if ENABLED(MONITOR_L6470_DRIVER_STATUS)
  505. L6470.monitor_driver();
  506. #endif
  507. // Limit check_axes_activity frequency to 10Hz
  508. static millis_t next_check_axes_ms = 0;
  509. if (ELAPSED(ms, next_check_axes_ms)) {
  510. planner.check_axes_activity();
  511. next_check_axes_ms = ms + 100UL;
  512. }
  513. #if PIN_EXISTS(FET_SAFETY)
  514. static millis_t FET_next;
  515. if (ELAPSED(ms, FET_next)) {
  516. FET_next = ms + FET_SAFETY_DELAY; // 2uS pulse every FET_SAFETY_DELAY mS
  517. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  518. DELAY_US(2);
  519. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  520. }
  521. #endif
  522. }
  523. /**
  524. * Standard idle routine keeps the machine alive
  525. */
  526. void idle(
  527. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  528. bool no_stepper_sleep/*=false*/
  529. #endif
  530. ) {
  531. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  532. recovery.outage();
  533. #endif
  534. #if ENABLED(SPI_ENDSTOPS)
  535. if (endstops.tmc_spi_homing.any
  536. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  537. && ELAPSED(millis(), sg_guard_period)
  538. #endif
  539. ) {
  540. for (uint8_t i = 4; i--;) // Read SGT 4 times per idle loop
  541. if (endstops.tmc_spi_homing_check()) break;
  542. }
  543. #endif
  544. #if ENABLED(MAX7219_DEBUG)
  545. max7219.idle_tasks();
  546. #endif
  547. ui.update();
  548. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  549. gcode.host_keepalive();
  550. #endif
  551. manage_inactivity(
  552. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  553. no_stepper_sleep
  554. #endif
  555. );
  556. thermalManager.manage_heater();
  557. #if ENABLED(PRINTCOUNTER)
  558. print_job_timer.tick();
  559. #endif
  560. #if USE_BEEPER
  561. buzzer.tick();
  562. #endif
  563. #if ENABLED(I2C_POSITION_ENCODERS)
  564. static millis_t i2cpem_next_update_ms;
  565. if (planner.has_blocks_queued()) {
  566. const millis_t ms = millis();
  567. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  568. I2CPEM.update();
  569. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  570. }
  571. }
  572. #endif
  573. #ifdef HAL_IDLETASK
  574. HAL_idletask();
  575. #endif
  576. #if HAS_AUTO_REPORTING
  577. if (!suspend_auto_report) {
  578. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  579. thermalManager.auto_report_temperatures();
  580. #endif
  581. #if ENABLED(AUTO_REPORT_SD_STATUS)
  582. card.auto_report_sd_status();
  583. #endif
  584. }
  585. #endif
  586. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  587. Sd2Card::idle();
  588. #endif
  589. #if ENABLED(PRUSA_MMU2)
  590. mmu2.mmu_loop();
  591. #endif
  592. #if ENABLED(POLL_JOG)
  593. joystick.inject_jog_moves();
  594. #endif
  595. }
  596. /**
  597. * Kill all activity and lock the machine.
  598. * After this the machine will need to be reset.
  599. */
  600. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  601. thermalManager.disable_all_heaters();
  602. SERIAL_ERROR_MSG(MSG_ERR_KILLED);
  603. #if HAS_DISPLAY
  604. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component);
  605. #else
  606. UNUSED(lcd_error);
  607. UNUSED(lcd_component);
  608. #endif
  609. #ifdef ACTION_ON_KILL
  610. host_action_kill();
  611. #endif
  612. minkill(steppers_off);
  613. }
  614. void minkill(const bool steppers_off/*=false*/) {
  615. // Wait a short time (allows messages to get out before shutting down.
  616. for (int i = 1000; i--;) DELAY_US(600);
  617. cli(); // Stop interrupts
  618. // Wait to ensure all interrupts stopped
  619. for (int i = 1000; i--;) DELAY_US(250);
  620. // Reiterate heaters off
  621. thermalManager.disable_all_heaters();
  622. // Power off all steppers (for M112) or just the E steppers
  623. steppers_off ? disable_all_steppers() : disable_e_steppers();
  624. #if ENABLED(PSU_CONTROL)
  625. PSU_OFF();
  626. #endif
  627. #if HAS_SUICIDE
  628. suicide();
  629. #endif
  630. #if HAS_KILL
  631. // Wait for kill to be released
  632. while (!READ(KILL_PIN)) watchdog_refresh();
  633. // Wait for kill to be pressed
  634. while (READ(KILL_PIN)) watchdog_refresh();
  635. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  636. resetFunc(); // Jump to address 0
  637. #else // !HAS_KILL
  638. for (;;) watchdog_refresh(); // Wait for reset
  639. #endif // !HAS_KILL
  640. }
  641. /**
  642. * Turn off heaters and stop the print in progress
  643. * After a stop the machine may be resumed with M999
  644. */
  645. void stop() {
  646. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  647. print_job_timer.stop();
  648. #if ENABLED(PROBING_FANS_OFF)
  649. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  650. #endif
  651. if (IsRunning()) {
  652. queue.stop();
  653. SERIAL_ERROR_MSG(MSG_ERR_STOPPED);
  654. LCD_MESSAGEPGM(MSG_STOPPED);
  655. safe_delay(350); // allow enough time for messages to get out before stopping
  656. Running = false;
  657. }
  658. }
  659. /**
  660. * Marlin entry-point: Set up before the program loop
  661. * - Set up the kill pin, filament runout, power hold
  662. * - Start the serial port
  663. * - Print startup messages and diagnostics
  664. * - Get EEPROM or default settings
  665. * - Initialize managers for:
  666. * • temperature
  667. * • planner
  668. * • watchdog
  669. * • stepper
  670. * • photo pin
  671. * • servos
  672. * • LCD controller
  673. * • Digipot I2C
  674. * • Z probe sled
  675. * • status LEDs
  676. */
  677. void setup() {
  678. HAL_init();
  679. #if HAS_DRIVER(L6470)
  680. L6470.init(); // setup SPI and then init chips
  681. #endif
  682. #if ENABLED(MAX7219_DEBUG)
  683. max7219.init();
  684. #endif
  685. #if ENABLED(DISABLE_DEBUG)
  686. // Disable any hardware debug to free up pins for IO
  687. #ifdef JTAGSWD_DISABLE
  688. JTAGSWD_DISABLE();
  689. #elif defined(JTAG_DISABLE)
  690. JTAG_DISABLE();
  691. #else
  692. #error "DISABLE_DEBUG is not supported for the selected MCU/Board"
  693. #endif
  694. #elif ENABLED(DISABLE_JTAG)
  695. // Disable JTAG to free up pins for IO
  696. #ifdef JTAG_DISABLE
  697. JTAG_DISABLE();
  698. #else
  699. #error "DISABLE_JTAG is not supported for the selected MCU/Board"
  700. #endif
  701. #endif
  702. #if HAS_FILAMENT_SENSOR
  703. runout.setup();
  704. #endif
  705. #if ENABLED(POWER_LOSS_RECOVERY)
  706. recovery.setup();
  707. #endif
  708. setup_killpin();
  709. #if HAS_TMC220x
  710. tmc_serial_begin();
  711. #endif
  712. setup_powerhold();
  713. #if HAS_STEPPER_RESET
  714. disableStepperDrivers();
  715. #endif
  716. #if NUM_SERIAL > 0
  717. MYSERIAL0.begin(BAUDRATE);
  718. #if NUM_SERIAL > 1
  719. MYSERIAL1.begin(BAUDRATE);
  720. #endif
  721. #endif
  722. #if NUM_SERIAL > 0
  723. uint32_t serial_connect_timeout = millis() + 1000UL;
  724. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  725. #if NUM_SERIAL > 1
  726. serial_connect_timeout = millis() + 1000UL;
  727. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  728. #endif
  729. #endif
  730. SERIAL_ECHOLNPGM("start");
  731. SERIAL_ECHO_START();
  732. #if TMC_HAS_SPI
  733. #if DISABLED(TMC_USE_SW_SPI)
  734. SPI.begin();
  735. #endif
  736. tmc_init_cs_pins();
  737. #endif
  738. #ifdef BOARD_INIT
  739. BOARD_INIT();
  740. #endif
  741. // Check startup - does nothing if bootloader sets MCUSR to 0
  742. byte mcu = HAL_get_reset_source();
  743. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  744. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  745. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  746. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  747. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  748. HAL_clear_reset_source();
  749. SERIAL_ECHOPGM(MSG_MARLIN);
  750. SERIAL_CHAR(' ');
  751. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  752. SERIAL_EOL();
  753. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  754. SERIAL_ECHO_MSG(
  755. MSG_CONFIGURATION_VER
  756. STRING_DISTRIBUTION_DATE
  757. MSG_AUTHOR STRING_CONFIG_H_AUTHOR
  758. );
  759. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  760. #endif
  761. SERIAL_ECHO_START();
  762. SERIAL_ECHOLNPAIR(MSG_FREE_MEMORY, freeMemory(), MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  763. // UI must be initialized before EEPROM
  764. // (because EEPROM code calls the UI).
  765. // Set up LEDs early
  766. #if HAS_COLOR_LEDS
  767. leds.setup();
  768. #endif
  769. ui.init();
  770. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  771. ui.show_bootscreen();
  772. #endif
  773. #if ENABLED(SDSUPPORT)
  774. card.mount(); // Mount the SD card before settings.first_load
  775. #endif
  776. // Load data from EEPROM if available (or use defaults)
  777. // This also updates variables in the planner, elsewhere
  778. settings.first_load();
  779. #if ENABLED(TOUCH_BUTTONS)
  780. touch.init();
  781. #endif
  782. #if HAS_M206_COMMAND
  783. // Initialize current position based on home_offset
  784. current_position += home_offset;
  785. #endif
  786. // Vital to init stepper/planner equivalent for current_position
  787. sync_plan_position();
  788. thermalManager.init(); // Initialize temperature loop
  789. print_job_timer.init(); // Initial setup of print job timer
  790. ui.reset_status(); // Print startup message after print statistics are loaded
  791. endstops.init(); // Init endstops and pullups
  792. stepper.init(); // Init stepper. This enables interrupts!
  793. #if HAS_SERVOS
  794. servo_init();
  795. #endif
  796. #if HAS_Z_SERVO_PROBE
  797. servo_probe_init();
  798. #endif
  799. #if HAS_PHOTOGRAPH
  800. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  801. #endif
  802. #if HAS_CUTTER
  803. cutter.init();
  804. #endif
  805. #if ENABLED(COOLANT_MIST)
  806. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  807. #endif
  808. #if ENABLED(COOLANT_FLOOD)
  809. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  810. #endif
  811. #if HAS_BED_PROBE
  812. endstops.enable_z_probe(false);
  813. #endif
  814. #if ENABLED(USE_CONTROLLER_FAN)
  815. SET_OUTPUT(CONTROLLER_FAN_PIN);
  816. #endif
  817. #if HAS_STEPPER_RESET
  818. enableStepperDrivers();
  819. #endif
  820. #if ENABLED(DIGIPOT_I2C)
  821. digipot_i2c_init();
  822. #endif
  823. #if ENABLED(DAC_STEPPER_CURRENT)
  824. dac_init();
  825. #endif
  826. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  827. OUT_WRITE(SOL1_PIN, LOW); // OFF
  828. #endif
  829. #if HAS_HOME
  830. SET_INPUT_PULLUP(HOME_PIN);
  831. #endif
  832. #if PIN_EXISTS(STAT_LED_RED)
  833. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  834. #endif
  835. #if PIN_EXISTS(STAT_LED_BLUE)
  836. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  837. #endif
  838. #if HAS_CASE_LIGHT
  839. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  840. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  841. #endif
  842. update_case_light();
  843. #endif
  844. #if ENABLED(MK2_MULTIPLEXER)
  845. SET_OUTPUT(E_MUX0_PIN);
  846. SET_OUTPUT(E_MUX1_PIN);
  847. SET_OUTPUT(E_MUX2_PIN);
  848. #endif
  849. #if HAS_FANMUX
  850. fanmux_init();
  851. #endif
  852. #if ENABLED(MIXING_EXTRUDER)
  853. mixer.init();
  854. #endif
  855. #if ENABLED(BLTOUCH)
  856. bltouch.init(/*set_voltage=*/true);
  857. #endif
  858. #if ENABLED(I2C_POSITION_ENCODERS)
  859. I2CPEM.init();
  860. #endif
  861. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  862. i2c.onReceive(i2c_on_receive);
  863. i2c.onRequest(i2c_on_request);
  864. #endif
  865. #if DO_SWITCH_EXTRUDER
  866. move_extruder_servo(0); // Initialize extruder servo
  867. #endif
  868. #if ENABLED(SWITCHING_NOZZLE)
  869. // Initialize nozzle servo(s)
  870. #if SWITCHING_NOZZLE_TWO_SERVOS
  871. lower_nozzle(0);
  872. raise_nozzle(1);
  873. #else
  874. move_nozzle_servo(0);
  875. #endif
  876. #endif
  877. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  878. mpe_settings_init();
  879. #endif
  880. #if ENABLED(PARKING_EXTRUDER)
  881. pe_solenoid_init();
  882. #endif
  883. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  884. est_init();
  885. #endif
  886. #if ENABLED(POWER_LOSS_RECOVERY)
  887. recovery.check();
  888. #endif
  889. #if ENABLED(USE_WATCHDOG)
  890. watchdog_init(); // Reinit watchdog after HAL_get_reset_source call
  891. #endif
  892. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  893. init_closedloop();
  894. #endif
  895. #ifdef STARTUP_COMMANDS
  896. queue.inject_P(PSTR(STARTUP_COMMANDS));
  897. #endif
  898. #if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
  899. card.beginautostart();
  900. #endif
  901. #if ENABLED(HOST_PROMPT_SUPPORT)
  902. host_action_prompt_end();
  903. #endif
  904. #if HAS_TRINAMIC && DISABLED(PS_DEFAULT_OFF)
  905. test_tmc_connection(true, true, true, true);
  906. #endif
  907. #if ENABLED(PRUSA_MMU2)
  908. mmu2.init();
  909. #endif
  910. }
  911. /**
  912. * The main Marlin program loop
  913. *
  914. * - Save or log commands to SD
  915. * - Process available commands (if not saving)
  916. * - Call endstop manager
  917. * - Call inactivity manager
  918. */
  919. void loop() {
  920. for (;;) {
  921. idle(); // Do an idle first so boot is slightly faster
  922. #if ENABLED(SDSUPPORT)
  923. card.checkautostart();
  924. if (card.flag.abort_sd_printing) {
  925. card.stopSDPrint(
  926. #if SD_RESORT
  927. true
  928. #endif
  929. );
  930. queue.clear();
  931. quickstop_stepper();
  932. print_job_timer.stop();
  933. #if DISABLED(SD_ABORT_NO_COOLDOWN)
  934. thermalManager.disable_all_heaters();
  935. #endif
  936. thermalManager.zero_fan_speeds();
  937. wait_for_heatup = false;
  938. #if ENABLED(POWER_LOSS_RECOVERY)
  939. card.removeJobRecoveryFile();
  940. #endif
  941. #ifdef EVENT_GCODE_SD_STOP
  942. queue.inject_P(PSTR(EVENT_GCODE_SD_STOP));
  943. #endif
  944. }
  945. #endif // SDSUPPORT
  946. queue.advance();
  947. endstops.event_handler();
  948. }
  949. }