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. void abortSDPrinting() {
  311. card.stopSDPrint(
  312. #if SD_RESORT
  313. true
  314. #endif
  315. );
  316. queue.clear();
  317. quickstop_stepper();
  318. print_job_timer.stop();
  319. #if DISABLED(SD_ABORT_NO_COOLDOWN)
  320. thermalManager.disable_all_heaters();
  321. #endif
  322. thermalManager.zero_fan_speeds();
  323. wait_for_heatup = false;
  324. #if ENABLED(POWER_LOSS_RECOVERY)
  325. card.removeJobRecoveryFile();
  326. #endif
  327. #ifdef EVENT_GCODE_SD_STOP
  328. queue.inject_P(PSTR(EVENT_GCODE_SD_STOP));
  329. #endif
  330. }
  331. /**
  332. * Manage several activities:
  333. * - Check for Filament Runout
  334. * - Keep the command buffer full
  335. * - Check for maximum inactive time between commands
  336. * - Check for maximum inactive time between stepper commands
  337. * - Check if CHDK_PIN needs to go LOW
  338. * - Check for KILL button held down
  339. * - Check for HOME button held down
  340. * - Check if cooling fan needs to be switched on
  341. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  342. * - Pulse FET_SAFETY_PIN if it exists
  343. */
  344. void manage_inactivity(const bool ignore_stepper_queue/*=false*/) {
  345. #if HAS_FILAMENT_SENSOR
  346. runout.run();
  347. #endif
  348. if (queue.length < BUFSIZE) queue.get_available_commands();
  349. const millis_t ms = millis();
  350. if (max_inactive_time && ELAPSED(ms, gcode.previous_move_ms + max_inactive_time)) {
  351. SERIAL_ERROR_START();
  352. SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
  353. kill();
  354. }
  355. // Prevent steppers timing-out in the middle of M600
  356. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  357. #define MOVE_AWAY_TEST !did_pause_print
  358. #else
  359. #define MOVE_AWAY_TEST true
  360. #endif
  361. if (stepper_inactive_time) {
  362. static bool already_shutdown_steppers; // = false
  363. if (planner.has_blocks_queued())
  364. gcode.reset_stepper_timeout();
  365. else if (MOVE_AWAY_TEST && !ignore_stepper_queue && ELAPSED(ms, gcode.previous_move_ms + stepper_inactive_time)) {
  366. if (!already_shutdown_steppers) {
  367. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  368. #if ENABLED(DISABLE_INACTIVE_X)
  369. disable_X();
  370. #endif
  371. #if ENABLED(DISABLE_INACTIVE_Y)
  372. disable_Y();
  373. #endif
  374. #if ENABLED(DISABLE_INACTIVE_Z)
  375. disable_Z();
  376. #endif
  377. #if ENABLED(DISABLE_INACTIVE_E)
  378. disable_e_steppers();
  379. #endif
  380. #if HAS_LCD_MENU && ENABLED(AUTO_BED_LEVELING_UBL)
  381. if (ubl.lcd_map_control) {
  382. ubl.lcd_map_control = false;
  383. ui.defer_status_screen(false);
  384. }
  385. #endif
  386. }
  387. }
  388. else
  389. already_shutdown_steppers = false;
  390. }
  391. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  392. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  393. chdk_timeout = 0;
  394. WRITE(CHDK_PIN, LOW);
  395. }
  396. #endif
  397. #if HAS_KILL
  398. // Check if the kill button was pressed and wait just in case it was an accidental
  399. // key kill key press
  400. // -------------------------------------------------------------------------------
  401. static int killCount = 0; // make the inactivity button a bit less responsive
  402. const int KILL_DELAY = 750;
  403. if (!READ(KILL_PIN))
  404. killCount++;
  405. else if (killCount > 0)
  406. killCount--;
  407. // Exceeded threshold and we can confirm that it was not accidental
  408. // KILL the machine
  409. // ----------------------------------------------------------------
  410. if (killCount >= KILL_DELAY) {
  411. SERIAL_ERROR_MSG(MSG_KILL_BUTTON);
  412. kill();
  413. }
  414. #endif
  415. #if HAS_HOME
  416. // Handle a standalone HOME button
  417. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  418. static millis_t next_home_key_ms; // = 0
  419. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  420. const millis_t ms = millis();
  421. if (ELAPSED(ms, next_home_key_ms)) {
  422. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  423. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  424. queue.enqueue_now_P(PSTR("G28"));
  425. }
  426. }
  427. #endif
  428. #if ENABLED(USE_CONTROLLER_FAN)
  429. controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
  430. #endif
  431. #if ENABLED(AUTO_POWER_CONTROL)
  432. powerManager.check();
  433. #endif
  434. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  435. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  436. && ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  437. && !planner.has_blocks_queued()
  438. ) {
  439. #if ENABLED(SWITCHING_EXTRUDER)
  440. bool oldstatus;
  441. switch (active_extruder) {
  442. default: oldstatus = E0_ENABLE_READ(); enable_E0(); break;
  443. #if E_STEPPERS > 1
  444. case 2: case 3: oldstatus = E1_ENABLE_READ(); enable_E1(); break;
  445. #if E_STEPPERS > 2
  446. case 4: case 5: oldstatus = E2_ENABLE_READ(); enable_E2(); break;
  447. #endif // E_STEPPERS > 2
  448. #endif // E_STEPPERS > 1
  449. }
  450. #else // !SWITCHING_EXTRUDER
  451. bool oldstatus;
  452. switch (active_extruder) {
  453. default: oldstatus = E0_ENABLE_READ(); enable_E0(); break;
  454. #if E_STEPPERS > 1
  455. case 1: oldstatus = E1_ENABLE_READ(); enable_E1(); break;
  456. #if E_STEPPERS > 2
  457. case 2: oldstatus = E2_ENABLE_READ(); enable_E2(); break;
  458. #if E_STEPPERS > 3
  459. case 3: oldstatus = E3_ENABLE_READ(); enable_E3(); break;
  460. #if E_STEPPERS > 4
  461. case 4: oldstatus = E4_ENABLE_READ(); enable_E4(); break;
  462. #if E_STEPPERS > 5
  463. case 5: oldstatus = E5_ENABLE_READ(); enable_E5(); break;
  464. #endif // E_STEPPERS > 5
  465. #endif // E_STEPPERS > 4
  466. #endif // E_STEPPERS > 3
  467. #endif // E_STEPPERS > 2
  468. #endif // E_STEPPERS > 1
  469. }
  470. #endif // !SWITCHING_EXTRUDER
  471. const float olde = current_position.e;
  472. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  473. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  474. current_position.e = olde;
  475. planner.set_e_position_mm(olde);
  476. planner.synchronize();
  477. #if ENABLED(SWITCHING_EXTRUDER)
  478. switch (active_extruder) {
  479. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  480. #if E_STEPPERS > 1
  481. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  482. #if E_STEPPERS > 2
  483. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  484. #endif // E_STEPPERS > 2
  485. #endif // E_STEPPERS > 1
  486. }
  487. #else // !SWITCHING_EXTRUDER
  488. switch (active_extruder) {
  489. case 0: E0_ENABLE_WRITE(oldstatus); break;
  490. #if E_STEPPERS > 1
  491. case 1: E1_ENABLE_WRITE(oldstatus); break;
  492. #if E_STEPPERS > 2
  493. case 2: E2_ENABLE_WRITE(oldstatus); break;
  494. #if E_STEPPERS > 3
  495. case 3: E3_ENABLE_WRITE(oldstatus); break;
  496. #if E_STEPPERS > 4
  497. case 4: E4_ENABLE_WRITE(oldstatus); break;
  498. #if E_STEPPERS > 5
  499. case 5: E5_ENABLE_WRITE(oldstatus); break;
  500. #endif // E_STEPPERS > 5
  501. #endif // E_STEPPERS > 4
  502. #endif // E_STEPPERS > 3
  503. #endif // E_STEPPERS > 2
  504. #endif // E_STEPPERS > 1
  505. }
  506. #endif // !SWITCHING_EXTRUDER
  507. gcode.reset_stepper_timeout();
  508. }
  509. #endif // EXTRUDER_RUNOUT_PREVENT
  510. #if ENABLED(DUAL_X_CARRIAGE)
  511. // handle delayed move timeout
  512. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  513. // travel moves have been received so enact them
  514. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  515. destination = current_position;
  516. prepare_move_to_destination();
  517. }
  518. #endif
  519. #if ENABLED(TEMP_STAT_LEDS)
  520. handle_status_leds();
  521. #endif
  522. #if ENABLED(MONITOR_DRIVER_STATUS)
  523. monitor_tmc_driver();
  524. #endif
  525. #if ENABLED(MONITOR_L6470_DRIVER_STATUS)
  526. L6470.monitor_driver();
  527. #endif
  528. // Limit check_axes_activity frequency to 10Hz
  529. static millis_t next_check_axes_ms = 0;
  530. if (ELAPSED(ms, next_check_axes_ms)) {
  531. planner.check_axes_activity();
  532. next_check_axes_ms = ms + 100UL;
  533. }
  534. #if PIN_EXISTS(FET_SAFETY)
  535. static millis_t FET_next;
  536. if (ELAPSED(ms, FET_next)) {
  537. FET_next = ms + FET_SAFETY_DELAY; // 2uS pulse every FET_SAFETY_DELAY mS
  538. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  539. DELAY_US(2);
  540. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  541. }
  542. #endif
  543. }
  544. /**
  545. * Standard idle routine keeps the machine alive
  546. */
  547. void idle(
  548. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  549. bool no_stepper_sleep/*=false*/
  550. #endif
  551. ) {
  552. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  553. recovery.outage();
  554. #endif
  555. #if ENABLED(SPI_ENDSTOPS)
  556. if (endstops.tmc_spi_homing.any
  557. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  558. && ELAPSED(millis(), sg_guard_period)
  559. #endif
  560. ) {
  561. for (uint8_t i = 4; i--;) // Read SGT 4 times per idle loop
  562. if (endstops.tmc_spi_homing_check()) break;
  563. }
  564. #endif
  565. #if ENABLED(MAX7219_DEBUG)
  566. max7219.idle_tasks();
  567. #endif
  568. ui.update();
  569. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  570. gcode.host_keepalive();
  571. #endif
  572. manage_inactivity(
  573. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  574. no_stepper_sleep
  575. #endif
  576. );
  577. thermalManager.manage_heater();
  578. #if ENABLED(PRINTCOUNTER)
  579. print_job_timer.tick();
  580. #endif
  581. #if USE_BEEPER
  582. buzzer.tick();
  583. #endif
  584. #if ENABLED(I2C_POSITION_ENCODERS)
  585. static millis_t i2cpem_next_update_ms;
  586. if (planner.has_blocks_queued()) {
  587. const millis_t ms = millis();
  588. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  589. I2CPEM.update();
  590. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  591. }
  592. }
  593. #endif
  594. #ifdef HAL_IDLETASK
  595. HAL_idletask();
  596. #endif
  597. #if HAS_AUTO_REPORTING
  598. if (!suspend_auto_report) {
  599. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  600. thermalManager.auto_report_temperatures();
  601. #endif
  602. #if ENABLED(AUTO_REPORT_SD_STATUS)
  603. card.auto_report_sd_status();
  604. #endif
  605. }
  606. #endif
  607. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  608. Sd2Card::idle();
  609. #endif
  610. #if ENABLED(PRUSA_MMU2)
  611. mmu2.mmu_loop();
  612. #endif
  613. #if ENABLED(POLL_JOG)
  614. joystick.inject_jog_moves();
  615. #endif
  616. }
  617. /**
  618. * Kill all activity and lock the machine.
  619. * After this the machine will need to be reset.
  620. */
  621. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  622. thermalManager.disable_all_heaters();
  623. SERIAL_ERROR_MSG(MSG_ERR_KILLED);
  624. #if HAS_DISPLAY
  625. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component);
  626. #else
  627. UNUSED(lcd_error);
  628. UNUSED(lcd_component);
  629. #endif
  630. #ifdef ACTION_ON_KILL
  631. host_action_kill();
  632. #endif
  633. minkill(steppers_off);
  634. }
  635. void minkill(const bool steppers_off/*=false*/) {
  636. // Wait a short time (allows messages to get out before shutting down.
  637. for (int i = 1000; i--;) DELAY_US(600);
  638. cli(); // Stop interrupts
  639. // Wait to ensure all interrupts stopped
  640. for (int i = 1000; i--;) DELAY_US(250);
  641. // Reiterate heaters off
  642. thermalManager.disable_all_heaters();
  643. // Power off all steppers (for M112) or just the E steppers
  644. steppers_off ? disable_all_steppers() : disable_e_steppers();
  645. #if ENABLED(PSU_CONTROL)
  646. PSU_OFF();
  647. #endif
  648. #if HAS_SUICIDE
  649. suicide();
  650. #endif
  651. #if HAS_KILL
  652. // Wait for kill to be released
  653. while (!READ(KILL_PIN)) watchdog_refresh();
  654. // Wait for kill to be pressed
  655. while (READ(KILL_PIN)) watchdog_refresh();
  656. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  657. resetFunc(); // Jump to address 0
  658. #else // !HAS_KILL
  659. for (;;) watchdog_refresh(); // Wait for reset
  660. #endif // !HAS_KILL
  661. }
  662. /**
  663. * Turn off heaters and stop the print in progress
  664. * After a stop the machine may be resumed with M999
  665. */
  666. void stop() {
  667. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  668. print_job_timer.stop();
  669. #if ENABLED(PROBING_FANS_OFF)
  670. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  671. #endif
  672. if (IsRunning()) {
  673. queue.stop();
  674. SERIAL_ERROR_MSG(MSG_ERR_STOPPED);
  675. LCD_MESSAGEPGM(MSG_STOPPED);
  676. safe_delay(350); // allow enough time for messages to get out before stopping
  677. Running = false;
  678. }
  679. }
  680. /**
  681. * Marlin entry-point: Set up before the program loop
  682. * - Set up the kill pin, filament runout, power hold
  683. * - Start the serial port
  684. * - Print startup messages and diagnostics
  685. * - Get EEPROM or default settings
  686. * - Initialize managers for:
  687. * • temperature
  688. * • planner
  689. * • watchdog
  690. * • stepper
  691. * • photo pin
  692. * • servos
  693. * • LCD controller
  694. * • Digipot I2C
  695. * • Z probe sled
  696. * • status LEDs
  697. */
  698. void setup() {
  699. HAL_init();
  700. #if HAS_DRIVER(L6470)
  701. L6470.init(); // setup SPI and then init chips
  702. #endif
  703. #if ENABLED(MAX7219_DEBUG)
  704. max7219.init();
  705. #endif
  706. #if ENABLED(DISABLE_DEBUG)
  707. // Disable any hardware debug to free up pins for IO
  708. #ifdef JTAGSWD_DISABLE
  709. JTAGSWD_DISABLE();
  710. #elif defined(JTAG_DISABLE)
  711. JTAG_DISABLE();
  712. #else
  713. #error "DISABLE_DEBUG is not supported for the selected MCU/Board"
  714. #endif
  715. #elif ENABLED(DISABLE_JTAG)
  716. // Disable JTAG to free up pins for IO
  717. #ifdef JTAG_DISABLE
  718. JTAG_DISABLE();
  719. #else
  720. #error "DISABLE_JTAG is not supported for the selected MCU/Board"
  721. #endif
  722. #endif
  723. #if HAS_FILAMENT_SENSOR
  724. runout.setup();
  725. #endif
  726. #if ENABLED(POWER_LOSS_RECOVERY)
  727. recovery.setup();
  728. #endif
  729. setup_killpin();
  730. #if HAS_TMC220x
  731. tmc_serial_begin();
  732. #endif
  733. setup_powerhold();
  734. #if HAS_STEPPER_RESET
  735. disableStepperDrivers();
  736. #endif
  737. #if NUM_SERIAL > 0
  738. MYSERIAL0.begin(BAUDRATE);
  739. #if NUM_SERIAL > 1
  740. MYSERIAL1.begin(BAUDRATE);
  741. #endif
  742. #endif
  743. #if NUM_SERIAL > 0
  744. uint32_t serial_connect_timeout = millis() + 1000UL;
  745. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  746. #if NUM_SERIAL > 1
  747. serial_connect_timeout = millis() + 1000UL;
  748. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  749. #endif
  750. #endif
  751. SERIAL_ECHOLNPGM("start");
  752. SERIAL_ECHO_START();
  753. #if TMC_HAS_SPI
  754. #if DISABLED(TMC_USE_SW_SPI)
  755. SPI.begin();
  756. #endif
  757. tmc_init_cs_pins();
  758. #endif
  759. #ifdef BOARD_INIT
  760. BOARD_INIT();
  761. #endif
  762. // Check startup - does nothing if bootloader sets MCUSR to 0
  763. byte mcu = HAL_get_reset_source();
  764. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  765. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  766. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  767. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  768. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  769. HAL_clear_reset_source();
  770. SERIAL_ECHOPGM(MSG_MARLIN);
  771. SERIAL_CHAR(' ');
  772. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  773. SERIAL_EOL();
  774. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  775. SERIAL_ECHO_MSG(
  776. MSG_CONFIGURATION_VER
  777. STRING_DISTRIBUTION_DATE
  778. MSG_AUTHOR STRING_CONFIG_H_AUTHOR
  779. );
  780. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  781. #endif
  782. SERIAL_ECHO_START();
  783. SERIAL_ECHOLNPAIR(MSG_FREE_MEMORY, freeMemory(), MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  784. // UI must be initialized before EEPROM
  785. // (because EEPROM code calls the UI).
  786. // Set up LEDs early
  787. #if HAS_COLOR_LEDS
  788. leds.setup();
  789. #endif
  790. ui.init();
  791. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  792. ui.show_bootscreen();
  793. #endif
  794. #if ENABLED(SDSUPPORT)
  795. card.mount(); // Mount the SD card before settings.first_load
  796. #endif
  797. // Load data from EEPROM if available (or use defaults)
  798. // This also updates variables in the planner, elsewhere
  799. settings.first_load();
  800. #if ENABLED(TOUCH_BUTTONS)
  801. touch.init();
  802. #endif
  803. #if HAS_M206_COMMAND
  804. // Initialize current position based on home_offset
  805. current_position += home_offset;
  806. #endif
  807. // Vital to init stepper/planner equivalent for current_position
  808. sync_plan_position();
  809. thermalManager.init(); // Initialize temperature loop
  810. print_job_timer.init(); // Initial setup of print job timer
  811. ui.reset_status(); // Print startup message after print statistics are loaded
  812. endstops.init(); // Init endstops and pullups
  813. stepper.init(); // Init stepper. This enables interrupts!
  814. #if HAS_SERVOS
  815. servo_init();
  816. #endif
  817. #if HAS_Z_SERVO_PROBE
  818. servo_probe_init();
  819. #endif
  820. #if HAS_PHOTOGRAPH
  821. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  822. #endif
  823. #if HAS_CUTTER
  824. cutter.init();
  825. #endif
  826. #if ENABLED(COOLANT_MIST)
  827. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  828. #endif
  829. #if ENABLED(COOLANT_FLOOD)
  830. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  831. #endif
  832. #if HAS_BED_PROBE
  833. endstops.enable_z_probe(false);
  834. #endif
  835. #if ENABLED(USE_CONTROLLER_FAN)
  836. SET_OUTPUT(CONTROLLER_FAN_PIN);
  837. #endif
  838. #if HAS_STEPPER_RESET
  839. enableStepperDrivers();
  840. #endif
  841. #if ENABLED(DIGIPOT_I2C)
  842. digipot_i2c_init();
  843. #endif
  844. #if ENABLED(DAC_STEPPER_CURRENT)
  845. dac_init();
  846. #endif
  847. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  848. OUT_WRITE(SOL1_PIN, LOW); // OFF
  849. #endif
  850. #if HAS_HOME
  851. SET_INPUT_PULLUP(HOME_PIN);
  852. #endif
  853. #if PIN_EXISTS(STAT_LED_RED)
  854. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  855. #endif
  856. #if PIN_EXISTS(STAT_LED_BLUE)
  857. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  858. #endif
  859. #if HAS_CASE_LIGHT
  860. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  861. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  862. #endif
  863. update_case_light();
  864. #endif
  865. #if ENABLED(MK2_MULTIPLEXER)
  866. SET_OUTPUT(E_MUX0_PIN);
  867. SET_OUTPUT(E_MUX1_PIN);
  868. SET_OUTPUT(E_MUX2_PIN);
  869. #endif
  870. #if HAS_FANMUX
  871. fanmux_init();
  872. #endif
  873. #if ENABLED(MIXING_EXTRUDER)
  874. mixer.init();
  875. #endif
  876. #if ENABLED(BLTOUCH)
  877. bltouch.init(/*set_voltage=*/true);
  878. #endif
  879. #if ENABLED(I2C_POSITION_ENCODERS)
  880. I2CPEM.init();
  881. #endif
  882. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  883. i2c.onReceive(i2c_on_receive);
  884. i2c.onRequest(i2c_on_request);
  885. #endif
  886. #if DO_SWITCH_EXTRUDER
  887. move_extruder_servo(0); // Initialize extruder servo
  888. #endif
  889. #if ENABLED(SWITCHING_NOZZLE)
  890. // Initialize nozzle servo(s)
  891. #if SWITCHING_NOZZLE_TWO_SERVOS
  892. lower_nozzle(0);
  893. raise_nozzle(1);
  894. #else
  895. move_nozzle_servo(0);
  896. #endif
  897. #endif
  898. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  899. mpe_settings_init();
  900. #endif
  901. #if ENABLED(PARKING_EXTRUDER)
  902. pe_solenoid_init();
  903. #endif
  904. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  905. est_init();
  906. #endif
  907. #if ENABLED(POWER_LOSS_RECOVERY)
  908. recovery.check();
  909. #endif
  910. #if ENABLED(USE_WATCHDOG)
  911. watchdog_init(); // Reinit watchdog after HAL_get_reset_source call
  912. #endif
  913. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  914. init_closedloop();
  915. #endif
  916. #ifdef STARTUP_COMMANDS
  917. queue.inject_P(PSTR(STARTUP_COMMANDS));
  918. #endif
  919. #if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
  920. card.beginautostart();
  921. #endif
  922. #if ENABLED(HOST_PROMPT_SUPPORT)
  923. host_action_prompt_end();
  924. #endif
  925. #if HAS_TRINAMIC && DISABLED(PS_DEFAULT_OFF)
  926. test_tmc_connection(true, true, true, true);
  927. #endif
  928. #if ENABLED(PRUSA_MMU2)
  929. mmu2.init();
  930. #endif
  931. }
  932. /**
  933. * The main Marlin program loop
  934. *
  935. * - Save or log commands to SD
  936. * - Process available commands (if not saving)
  937. * - Call endstop manager
  938. * - Call inactivity manager
  939. */
  940. void loop() {
  941. for (;;) {
  942. idle(); // Do an idle first so boot is slightly faster
  943. #if ENABLED(SDSUPPORT)
  944. card.checkautostart();
  945. if (card.flag.abort_sd_printing) abortSDPrinting();
  946. #endif
  947. queue.advance();
  948. endstops.event_handler();
  949. }
  950. }