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.

MarlinCore.cpp 31KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 "MarlinCore.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.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/encoder_i2c.h"
  91. #endif
  92. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_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/powerloss.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, 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/mmu2/mmu2.h"
  142. #endif
  143. #if HAS_L64XX
  144. #include "libs/L64XX/L64XX_Marlin.h"
  145. #endif
  146. const char NUL_STR[] PROGMEM = "",
  147. M112_KILL_STR[] PROGMEM = "M112 Shutdown",
  148. G28_STR[] PROGMEM = "G28",
  149. M21_STR[] PROGMEM = "M21",
  150. M23_STR[] PROGMEM = "M23 %s",
  151. M24_STR[] PROGMEM = "M24",
  152. SP_P_STR[] PROGMEM = " P",
  153. SP_T_STR[] PROGMEM = " T",
  154. SP_X_STR[] PROGMEM = " X",
  155. SP_Y_STR[] PROGMEM = " Y",
  156. SP_Z_STR[] PROGMEM = " Z",
  157. SP_E_STR[] PROGMEM = " E",
  158. X_LBL[] PROGMEM = "X:",
  159. Y_LBL[] PROGMEM = "Y:",
  160. Z_LBL[] PROGMEM = "Z:",
  161. E_LBL[] PROGMEM = "E:",
  162. SP_X_LBL[] PROGMEM = " X:",
  163. SP_Y_LBL[] PROGMEM = " Y:",
  164. SP_Z_LBL[] PROGMEM = " Z:",
  165. SP_E_LBL[] PROGMEM = " E:";
  166. MarlinState marlin_state = MF_INITIALIZING;
  167. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  168. bool wait_for_heatup = true;
  169. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  170. #if HAS_RESUME_CONTINUE
  171. bool wait_for_user; // = false;
  172. #endif
  173. // Inactivity shutdown
  174. millis_t max_inactive_time, // = 0
  175. stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
  176. #if PIN_EXISTS(CHDK)
  177. extern millis_t chdk_timeout;
  178. #endif
  179. #if ENABLED(I2C_POSITION_ENCODERS)
  180. I2CPositionEncodersMgr I2CPEM;
  181. #endif
  182. /**
  183. * ***************************************************************************
  184. * ******************************** FUNCTIONS ********************************
  185. * ***************************************************************************
  186. */
  187. void setup_killpin() {
  188. #if HAS_KILL
  189. SET_INPUT_PULLUP(KILL_PIN);
  190. #endif
  191. }
  192. void setup_powerhold() {
  193. #if HAS_SUICIDE
  194. OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
  195. #endif
  196. #if ENABLED(PSU_CONTROL)
  197. powersupply_on = ENABLED(PSU_DEFAULT_OFF);
  198. if (ENABLED(PSU_DEFAULT_OFF)) PSU_OFF(); else PSU_ON();
  199. #endif
  200. }
  201. /**
  202. * Stepper Reset (RigidBoard, et.al.)
  203. */
  204. #if HAS_STEPPER_RESET
  205. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  206. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  207. #endif
  208. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  209. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  210. i2c.receive(bytes);
  211. }
  212. void i2c_on_request() { // just send dummy data for now
  213. i2c.reply("Hello World!\n");
  214. }
  215. #endif
  216. /**
  217. * Sensitive pin test for M42, M226
  218. */
  219. #include "pins/sensitive_pins.h"
  220. bool pin_is_protected(const pin_t pin) {
  221. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  222. LOOP_L_N(i, COUNT(sensitive_pins)) {
  223. pin_t sensitive_pin;
  224. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  225. if (pin == sensitive_pin) return true;
  226. }
  227. return false;
  228. }
  229. void protected_pin_err() {
  230. SERIAL_ERROR_MSG(STR_ERR_PROTECTED_PIN);
  231. }
  232. void quickstop_stepper() {
  233. planner.quick_stop();
  234. planner.synchronize();
  235. set_current_from_steppers_for_axis(ALL_AXES);
  236. sync_plan_position();
  237. }
  238. void enable_e_steppers() {
  239. #define _ENA_E(N) ENABLE_AXIS_E##N();
  240. REPEAT(E_STEPPERS, _ENA_E)
  241. }
  242. void enable_all_steppers() {
  243. #if ENABLED(AUTO_POWER_CONTROL)
  244. powerManager.power_on();
  245. #endif
  246. ENABLE_AXIS_X();
  247. ENABLE_AXIS_Y();
  248. ENABLE_AXIS_Z();
  249. enable_e_steppers();
  250. }
  251. void disable_e_steppers() {
  252. #define _DIS_E(N) DISABLE_AXIS_E##N();
  253. REPEAT(E_STEPPERS, _DIS_E)
  254. }
  255. void disable_e_stepper(const uint8_t e) {
  256. #define _CASE_DIS_E(N) case N: DISABLE_AXIS_E##N(); break;
  257. switch (e) {
  258. REPEAT(EXTRUDERS, _CASE_DIS_E)
  259. }
  260. }
  261. void disable_all_steppers() {
  262. DISABLE_AXIS_X();
  263. DISABLE_AXIS_Y();
  264. DISABLE_AXIS_Z();
  265. disable_e_steppers();
  266. }
  267. #if ENABLED(G29_RETRY_AND_RECOVER)
  268. void event_probe_failure() {
  269. #ifdef ACTION_ON_G29_FAILURE
  270. host_action(PSTR(ACTION_ON_G29_FAILURE));
  271. #endif
  272. #ifdef G29_FAILURE_COMMANDS
  273. gcode.process_subcommands_now_P(PSTR(G29_FAILURE_COMMANDS));
  274. #endif
  275. #if ENABLED(G29_HALT_ON_FAILURE)
  276. #ifdef ACTION_ON_CANCEL
  277. host_action_cancel();
  278. #endif
  279. kill(GET_TEXT(MSG_LCD_PROBING_FAILED));
  280. #endif
  281. }
  282. void event_probe_recover() {
  283. #if ENABLED(HOST_PROMPT_SUPPORT)
  284. host_prompt_do(PROMPT_INFO, PSTR("G29 Retrying"), DISMISS_STR);
  285. #endif
  286. #ifdef ACTION_ON_G29_RECOVER
  287. host_action(PSTR(ACTION_ON_G29_RECOVER));
  288. #endif
  289. #ifdef G29_RECOVER_COMMANDS
  290. gcode.process_subcommands_now_P(PSTR(G29_RECOVER_COMMANDS));
  291. #endif
  292. }
  293. #endif
  294. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  295. #include "feature/pause.h"
  296. #else
  297. constexpr bool did_pause_print = false;
  298. #endif
  299. /**
  300. * Printing is active when the print job timer is running
  301. */
  302. bool printingIsActive() {
  303. return !did_pause_print && (print_job_timer.isRunning() || IS_SD_PRINTING());
  304. }
  305. /**
  306. * Printing is paused according to SD or host indicators
  307. */
  308. bool printingIsPaused() {
  309. return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
  310. }
  311. void startOrResumeJob() {
  312. if (!printingIsPaused()) {
  313. #if ENABLED(CANCEL_OBJECTS)
  314. cancelable.reset();
  315. #endif
  316. #if ENABLED(LCD_SHOW_E_TOTAL)
  317. e_move_accumulator = 0;
  318. #endif
  319. #if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
  320. ui.reset_remaining_time();
  321. #endif
  322. }
  323. print_job_timer.start();
  324. }
  325. #if ENABLED(SDSUPPORT)
  326. inline void abortSDPrinting() {
  327. card.endFilePrint(
  328. #if SD_RESORT
  329. true
  330. #endif
  331. );
  332. queue.clear();
  333. quickstop_stepper();
  334. print_job_timer.stop();
  335. #if DISABLED(SD_ABORT_NO_COOLDOWN)
  336. thermalManager.disable_all_heaters();
  337. #endif
  338. thermalManager.zero_fan_speeds();
  339. wait_for_heatup = false;
  340. #if ENABLED(POWER_LOSS_RECOVERY)
  341. recovery.purge();
  342. #endif
  343. #ifdef EVENT_GCODE_SD_STOP
  344. queue.inject_P(PSTR(EVENT_GCODE_SD_STOP));
  345. #endif
  346. }
  347. inline void finishSDPrinting() {
  348. bool did_state = true;
  349. switch (card.sdprinting_done_state) {
  350. case 1:
  351. if (print_job_timer.duration() > 60)
  352. did_state = queue.enqueue_one_P(PSTR("M31"));
  353. break;
  354. case 2:
  355. did_state = queue.enqueue_one_P(PSTR("M77"));
  356. break;
  357. case 3:
  358. #if ENABLED(LCD_SET_PROGRESS_MANUALLY)
  359. ui.set_progress_done();
  360. #endif
  361. break;
  362. case 4: // Display "Click to Continue..."
  363. #if HAS_LEDS_OFF_FLAG // 30 min timeout with LCD, 1 min without
  364. did_state = queue.enqueue_one_P(
  365. print_job_timer.duration() < 60 ? PSTR("M0Q1P1") : PSTR("M0Q1S" TERN(HAS_LCD_MENU, "1800", "60"))
  366. );
  367. #endif
  368. break;
  369. case 5:
  370. #if ENABLED(POWER_LOSS_RECOVERY)
  371. recovery.purge();
  372. #endif
  373. #if ENABLED(SD_FINISHED_STEPPERRELEASE) && defined(SD_FINISHED_RELEASECOMMAND)
  374. planner.finish_and_disable();
  375. #endif
  376. #if ENABLED(SD_REPRINT_LAST_SELECTED_FILE)
  377. ui.reselect_last_file();
  378. #endif
  379. SERIAL_ECHOLNPGM(STR_FILE_PRINTED);
  380. default:
  381. did_state = false;
  382. card.sdprinting_done_state = 0;
  383. }
  384. if (did_state) ++card.sdprinting_done_state;
  385. }
  386. #endif // SDSUPPORT
  387. /**
  388. * Minimal management of Marlin's core activities:
  389. * - Check for Filament Runout
  390. * - Keep the command buffer full
  391. * - Check for maximum inactive time between commands
  392. * - Check for maximum inactive time between stepper commands
  393. * - Check if CHDK_PIN needs to go LOW
  394. * - Check for KILL button held down
  395. * - Check for HOME button held down
  396. * - Check if cooling fan needs to be switched on
  397. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  398. * - Pulse FET_SAFETY_PIN if it exists
  399. */
  400. inline void manage_inactivity(const bool ignore_stepper_queue=false) {
  401. #if HAS_FILAMENT_SENSOR
  402. runout.run();
  403. #endif
  404. if (queue.length < BUFSIZE) queue.get_available_commands();
  405. const millis_t ms = millis();
  406. if (max_inactive_time && ELAPSED(ms, gcode.previous_move_ms + max_inactive_time)) {
  407. SERIAL_ERROR_START();
  408. SERIAL_ECHOLNPAIR(STR_KILL_INACTIVE_TIME, parser.command_ptr);
  409. kill();
  410. }
  411. // Prevent steppers timing-out in the middle of M600
  412. #define STAY_TEST (BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT) && did_pause_print)
  413. if (stepper_inactive_time) {
  414. static bool already_shutdown_steppers; // = false
  415. if (planner.has_blocks_queued())
  416. gcode.reset_stepper_timeout();
  417. else if (!STAY_TEST && !ignore_stepper_queue && ELAPSED(ms, gcode.previous_move_ms + stepper_inactive_time)) {
  418. if (!already_shutdown_steppers) {
  419. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  420. if (ENABLED(DISABLE_INACTIVE_X)) DISABLE_AXIS_X();
  421. if (ENABLED(DISABLE_INACTIVE_Y)) DISABLE_AXIS_Y();
  422. if (ENABLED(DISABLE_INACTIVE_Z)) DISABLE_AXIS_Z();
  423. if (ENABLED(DISABLE_INACTIVE_E)) disable_e_steppers();
  424. #if HAS_LCD_MENU && ENABLED(AUTO_BED_LEVELING_UBL)
  425. if (ubl.lcd_map_control) {
  426. ubl.lcd_map_control = false;
  427. ui.defer_status_screen(false);
  428. }
  429. #endif
  430. }
  431. }
  432. else
  433. already_shutdown_steppers = false;
  434. }
  435. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  436. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  437. chdk_timeout = 0;
  438. WRITE(CHDK_PIN, LOW);
  439. }
  440. #endif
  441. #if HAS_KILL
  442. // Check if the kill button was pressed and wait just in case it was an accidental
  443. // key kill key press
  444. // -------------------------------------------------------------------------------
  445. static int killCount = 0; // make the inactivity button a bit less responsive
  446. const int KILL_DELAY = 750;
  447. if (!READ(KILL_PIN))
  448. killCount++;
  449. else if (killCount > 0)
  450. killCount--;
  451. // Exceeded threshold and we can confirm that it was not accidental
  452. // KILL the machine
  453. // ----------------------------------------------------------------
  454. if (killCount >= KILL_DELAY) {
  455. SERIAL_ERROR_MSG(STR_KILL_BUTTON);
  456. kill();
  457. }
  458. #endif
  459. #if HAS_HOME
  460. // Handle a standalone HOME button
  461. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  462. static millis_t next_home_key_ms; // = 0
  463. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  464. const millis_t ms = millis();
  465. if (ELAPSED(ms, next_home_key_ms)) {
  466. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  467. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  468. queue.enqueue_now_P(G28_STR);
  469. }
  470. }
  471. #endif
  472. #if ENABLED(USE_CONTROLLER_FAN)
  473. controllerFan.update(); // Check if fan should be turned on to cool stepper drivers down
  474. #endif
  475. #if ENABLED(AUTO_POWER_CONTROL)
  476. powerManager.check();
  477. #endif
  478. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  479. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  480. && ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  481. && !planner.has_blocks_queued()
  482. ) {
  483. #if ENABLED(SWITCHING_EXTRUDER)
  484. bool oldstatus;
  485. switch (active_extruder) {
  486. default: oldstatus = E0_ENABLE_READ(); ENABLE_AXIS_E0(); break;
  487. #if E_STEPPERS > 1
  488. case 2: case 3: oldstatus = E1_ENABLE_READ(); ENABLE_AXIS_E1(); break;
  489. #if E_STEPPERS > 2
  490. case 4: case 5: oldstatus = E2_ENABLE_READ(); ENABLE_AXIS_E2(); break;
  491. #if E_STEPPERS > 3
  492. case 6: case 7: oldstatus = E3_ENABLE_READ(); ENABLE_AXIS_E3(); break;
  493. #endif // E_STEPPERS > 3
  494. #endif // E_STEPPERS > 2
  495. #endif // E_STEPPERS > 1
  496. }
  497. #else // !SWITCHING_EXTRUDER
  498. bool oldstatus;
  499. switch (active_extruder) {
  500. default:
  501. #define _CASE_EN(N) case N: oldstatus = E##N##_ENABLE_READ(); ENABLE_AXIS_E##N(); break;
  502. REPEAT(E_STEPPERS, _CASE_EN);
  503. }
  504. #endif
  505. const float olde = current_position.e;
  506. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  507. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  508. current_position.e = olde;
  509. planner.set_e_position_mm(olde);
  510. planner.synchronize();
  511. #if ENABLED(SWITCHING_EXTRUDER)
  512. switch (active_extruder) {
  513. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  514. #if E_STEPPERS > 1
  515. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  516. #if E_STEPPERS > 2
  517. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  518. #endif // E_STEPPERS > 2
  519. #endif // E_STEPPERS > 1
  520. }
  521. #else // !SWITCHING_EXTRUDER
  522. switch (active_extruder) {
  523. #define _CASE_RESTORE(N) case N: E##N##_ENABLE_WRITE(oldstatus); break;
  524. REPEAT(E_STEPPERS, _CASE_RESTORE);
  525. }
  526. #endif // !SWITCHING_EXTRUDER
  527. gcode.reset_stepper_timeout();
  528. }
  529. #endif // EXTRUDER_RUNOUT_PREVENT
  530. #if ENABLED(DUAL_X_CARRIAGE)
  531. // handle delayed move timeout
  532. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  533. // travel moves have been received so enact them
  534. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  535. destination = current_position;
  536. prepare_line_to_destination();
  537. }
  538. #endif
  539. #if ENABLED(TEMP_STAT_LEDS)
  540. handle_status_leds();
  541. #endif
  542. #if ENABLED(MONITOR_DRIVER_STATUS)
  543. monitor_tmc_drivers();
  544. #endif
  545. #if ENABLED(MONITOR_L6470_DRIVER_STATUS)
  546. L64xxManager.monitor_driver();
  547. #endif
  548. // Limit check_axes_activity frequency to 10Hz
  549. static millis_t next_check_axes_ms = 0;
  550. if (ELAPSED(ms, next_check_axes_ms)) {
  551. planner.check_axes_activity();
  552. next_check_axes_ms = ms + 100UL;
  553. }
  554. #if PIN_EXISTS(FET_SAFETY)
  555. static millis_t FET_next;
  556. if (ELAPSED(ms, FET_next)) {
  557. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  558. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  559. DELAY_US(2);
  560. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  561. }
  562. #endif
  563. }
  564. /**
  565. * Standard idle routine keeps the machine alive
  566. */
  567. void idle(
  568. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  569. bool no_stepper_sleep/*=false*/
  570. #endif
  571. ) {
  572. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  573. recovery.outage();
  574. #endif
  575. #if ENABLED(SPI_ENDSTOPS)
  576. if (endstops.tmc_spi_homing.any
  577. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  578. && ELAPSED(millis(), sg_guard_period)
  579. #endif
  580. ) {
  581. for (uint8_t i = 4; i--;) // Read SGT 4 times per idle loop
  582. if (endstops.tmc_spi_homing_check()) break;
  583. }
  584. #endif
  585. #if ENABLED(MAX7219_DEBUG)
  586. max7219.idle_tasks();
  587. #endif
  588. ui.update();
  589. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  590. gcode.host_keepalive();
  591. #endif
  592. manage_inactivity(
  593. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  594. no_stepper_sleep
  595. #endif
  596. );
  597. thermalManager.manage_heater();
  598. #if ENABLED(PRINTCOUNTER)
  599. print_job_timer.tick();
  600. #endif
  601. #if USE_BEEPER
  602. buzzer.tick();
  603. #endif
  604. #if ENABLED(I2C_POSITION_ENCODERS)
  605. static millis_t i2cpem_next_update_ms;
  606. if (planner.has_blocks_queued()) {
  607. const millis_t ms = millis();
  608. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  609. I2CPEM.update();
  610. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  611. }
  612. }
  613. #endif
  614. #ifdef HAL_IDLETASK
  615. HAL_idletask();
  616. #endif
  617. #if HAS_AUTO_REPORTING
  618. if (!gcode.autoreport_paused) {
  619. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  620. thermalManager.auto_report_temperatures();
  621. #endif
  622. #if ENABLED(AUTO_REPORT_SD_STATUS)
  623. card.auto_report_sd_status();
  624. #endif
  625. }
  626. #endif
  627. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  628. Sd2Card::idle();
  629. #endif
  630. #if ENABLED(PRUSA_MMU2)
  631. mmu2.mmu_loop();
  632. #endif
  633. #if ENABLED(POLL_JOG)
  634. joystick.inject_jog_moves();
  635. #endif
  636. }
  637. /**
  638. * Kill all activity and lock the machine.
  639. * After this the machine will need to be reset.
  640. */
  641. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  642. thermalManager.disable_all_heaters();
  643. SERIAL_ERROR_MSG(STR_ERR_KILLED);
  644. #if HAS_DISPLAY
  645. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component ?: NUL_STR);
  646. #else
  647. UNUSED(lcd_error);
  648. UNUSED(lcd_component);
  649. #endif
  650. #ifdef ACTION_ON_KILL
  651. host_action_kill();
  652. #endif
  653. minkill(steppers_off);
  654. }
  655. void minkill(const bool steppers_off/*=false*/) {
  656. // Wait a short time (allows messages to get out before shutting down.
  657. for (int i = 1000; i--;) DELAY_US(600);
  658. cli(); // Stop interrupts
  659. // Wait to ensure all interrupts stopped
  660. for (int i = 1000; i--;) DELAY_US(250);
  661. // Reiterate heaters off
  662. thermalManager.disable_all_heaters();
  663. // Power off all steppers (for M112) or just the E steppers
  664. steppers_off ? disable_all_steppers() : disable_e_steppers();
  665. #if ENABLED(PSU_CONTROL)
  666. PSU_OFF();
  667. #endif
  668. #if HAS_SUICIDE
  669. suicide();
  670. #endif
  671. #if HAS_KILL
  672. // Wait for kill to be released
  673. while (!READ(KILL_PIN)) watchdog_refresh();
  674. // Wait for kill to be pressed
  675. while (READ(KILL_PIN)) watchdog_refresh();
  676. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  677. resetFunc(); // Jump to address 0
  678. #else // !HAS_KILL
  679. for (;;) watchdog_refresh(); // Wait for reset
  680. #endif // !HAS_KILL
  681. }
  682. /**
  683. * Turn off heaters and stop the print in progress
  684. * After a stop the machine may be resumed with M999
  685. */
  686. void stop() {
  687. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  688. print_job_timer.stop();
  689. #if ENABLED(PROBING_FANS_OFF)
  690. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  691. #endif
  692. if (IsRunning()) {
  693. SERIAL_ERROR_MSG(STR_ERR_STOPPED);
  694. LCD_MESSAGEPGM(MSG_STOPPED);
  695. safe_delay(350); // allow enough time for messages to get out before stopping
  696. marlin_state = MF_STOPPED;
  697. }
  698. }
  699. /**
  700. * Marlin entry-point: Set up before the program loop
  701. * - Set up the kill pin, filament runout, power hold
  702. * - Start the serial port
  703. * - Print startup messages and diagnostics
  704. * - Get EEPROM or default settings
  705. * - Initialize managers for:
  706. * • temperature
  707. * • planner
  708. * • watchdog
  709. * • stepper
  710. * • photo pin
  711. * • servos
  712. * • LCD controller
  713. * • Digipot I2C
  714. * • Z probe sled
  715. * • status LEDs
  716. * • Max7219
  717. */
  718. void setup() {
  719. HAL_init();
  720. #if HAS_L64XX
  721. L64xxManager.init(); // Set up SPI, init drivers
  722. #endif
  723. #if ENABLED(SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  724. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  725. #endif
  726. #if ENABLED(DISABLE_DEBUG)
  727. // Disable any hardware debug to free up pins for IO
  728. #ifdef JTAGSWD_DISABLE
  729. JTAGSWD_DISABLE();
  730. #elif defined(JTAG_DISABLE)
  731. JTAG_DISABLE();
  732. #else
  733. #error "DISABLE_DEBUG is not supported for the selected MCU/Board"
  734. #endif
  735. #elif ENABLED(DISABLE_JTAG)
  736. // Disable JTAG to free up pins for IO
  737. #ifdef JTAG_DISABLE
  738. JTAG_DISABLE();
  739. #else
  740. #error "DISABLE_JTAG is not supported for the selected MCU/Board"
  741. #endif
  742. #endif
  743. #if HAS_FILAMENT_SENSOR
  744. runout.setup();
  745. #endif
  746. #if ENABLED(POWER_LOSS_RECOVERY)
  747. recovery.setup();
  748. #endif
  749. setup_killpin();
  750. #if HAS_TMC220x
  751. tmc_serial_begin();
  752. #endif
  753. setup_powerhold();
  754. #if HAS_STEPPER_RESET
  755. disableStepperDrivers();
  756. #endif
  757. #if NUM_SERIAL > 0
  758. MYSERIAL0.begin(BAUDRATE);
  759. uint32_t serial_connect_timeout = millis() + 1000UL;
  760. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  761. #if NUM_SERIAL > 1
  762. MYSERIAL1.begin(BAUDRATE);
  763. serial_connect_timeout = millis() + 1000UL;
  764. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  765. #endif
  766. #endif
  767. SERIAL_ECHOLNPGM("start");
  768. SERIAL_ECHO_START();
  769. #if HAS_TMC_SPI
  770. #if DISABLED(TMC_USE_SW_SPI)
  771. SPI.begin();
  772. #endif
  773. tmc_init_cs_pins();
  774. #endif
  775. #ifdef BOARD_INIT
  776. BOARD_INIT();
  777. #endif
  778. // Check startup - does nothing if bootloader sets MCUSR to 0
  779. byte mcu = HAL_get_reset_source();
  780. if (mcu & 1) SERIAL_ECHOLNPGM(STR_POWERUP);
  781. if (mcu & 2) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  782. if (mcu & 4) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  783. if (mcu & 8) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  784. if (mcu & 32) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  785. HAL_clear_reset_source();
  786. serialprintPGM(GET_TEXT(MSG_MARLIN));
  787. SERIAL_CHAR(' ');
  788. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  789. SERIAL_EOL();
  790. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  791. SERIAL_ECHO_MSG(
  792. STR_CONFIGURATION_VER
  793. STRING_DISTRIBUTION_DATE
  794. STR_AUTHOR STRING_CONFIG_H_AUTHOR
  795. );
  796. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  797. #endif
  798. SERIAL_ECHO_START();
  799. SERIAL_ECHOLNPAIR(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  800. // UI must be initialized before EEPROM
  801. // (because EEPROM code calls the UI).
  802. #if ENABLED(MARLIN_DEV_MODE)
  803. auto log_current_ms = [&](PGM_P const msg) {
  804. SERIAL_ECHO_START();
  805. SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHO("] ");
  806. serialprintPGM(msg);
  807. SERIAL_EOL();
  808. };
  809. #define SETUP_LOG(M) log_current_ms(PSTR(M))
  810. #else
  811. #define SETUP_LOG(...) NOOP
  812. #endif
  813. #define SETUP_RUN(C) do{ SETUP_LOG(STRINGIFY(C)); C; }while(0)
  814. // Set up LEDs early
  815. #if HAS_COLOR_LEDS
  816. SETUP_RUN(leds.setup());
  817. #endif
  818. #if ENABLED(USE_CONTROLLER_FAN) // Set up fan controller to initialize also the default configurations.
  819. SETUP_RUN(controllerFan.setup());
  820. #endif
  821. SETUP_RUN(ui.init());
  822. SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)
  823. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  824. SETUP_RUN(ui.show_bootscreen());
  825. #endif
  826. #if ENABLED(SDSUPPORT) && defined(SDCARD_CONNECTION) && !SD_CONNECTION_IS(LCD)
  827. SETUP_RUN(card.mount()); // Mount onboard / custom SD card before settings.first_load
  828. #endif
  829. SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
  830. // This also updates variables in the planner, elsewhere
  831. #if HAS_SERVICE_INTERVALS
  832. SETUP_RUN(ui.reset_status(true)); // Show service messages or keep current status
  833. #endif
  834. #if ENABLED(TOUCH_BUTTONS)
  835. SETUP_RUN(touch.init());
  836. #endif
  837. #if HAS_M206_COMMAND
  838. current_position += home_offset; // Init current position based on home_offset
  839. #endif
  840. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  841. SETUP_RUN(thermalManager.init()); // Initialize temperature loop
  842. SETUP_RUN(print_job_timer.init()); // Initial setup of print job timer
  843. SETUP_RUN(endstops.init()); // Init endstops and pullups
  844. SETUP_RUN(stepper.init()); // Init stepper. This enables interrupts!
  845. #if HAS_SERVOS
  846. SETUP_RUN(servo_init());
  847. #endif
  848. #if HAS_Z_SERVO_PROBE
  849. SETUP_RUN(probe.servo_probe_init());
  850. #endif
  851. #if HAS_PHOTOGRAPH
  852. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  853. #endif
  854. #if HAS_CUTTER
  855. SETUP_RUN(cutter.init());
  856. #endif
  857. #if ENABLED(COOLANT_MIST)
  858. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  859. #endif
  860. #if ENABLED(COOLANT_FLOOD)
  861. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  862. #endif
  863. #if HAS_BED_PROBE
  864. SETUP_RUN(endstops.enable_z_probe(false));
  865. #endif
  866. #if HAS_STEPPER_RESET
  867. SETUP_RUN(enableStepperDrivers());
  868. #endif
  869. #if ENABLED(DIGIPOT_I2C)
  870. SETUP_RUN(digipot_i2c_init());
  871. #endif
  872. #if ENABLED(DAC_STEPPER_CURRENT)
  873. SETUP_RUN(dac_init());
  874. #endif
  875. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  876. OUT_WRITE(SOL1_PIN, LOW); // OFF
  877. #endif
  878. #if HAS_HOME
  879. SET_INPUT_PULLUP(HOME_PIN);
  880. #endif
  881. #if PIN_EXISTS(STAT_LED_RED)
  882. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  883. #endif
  884. #if PIN_EXISTS(STAT_LED_BLUE)
  885. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  886. #endif
  887. #if HAS_CASE_LIGHT
  888. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  889. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  890. #endif
  891. SETUP_RUN(update_case_light());
  892. #endif
  893. #if ENABLED(MK2_MULTIPLEXER)
  894. SETUP_LOG("MK2_MULTIPLEXER");
  895. SET_OUTPUT(E_MUX0_PIN);
  896. SET_OUTPUT(E_MUX1_PIN);
  897. SET_OUTPUT(E_MUX2_PIN);
  898. #endif
  899. #if HAS_FANMUX
  900. SETUP_RUN(fanmux_init());
  901. #endif
  902. #if ENABLED(MIXING_EXTRUDER)
  903. SETUP_RUN(mixer.init());
  904. #endif
  905. #if ENABLED(BLTOUCH)
  906. SETUP_RUN(bltouch.init(/*set_voltage=*/true));
  907. #endif
  908. #if ENABLED(I2C_POSITION_ENCODERS)
  909. SETUP_RUN(I2CPEM.init());
  910. #endif
  911. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  912. SETUP_LOG("i2c...");
  913. i2c.onReceive(i2c_on_receive);
  914. i2c.onRequest(i2c_on_request);
  915. #endif
  916. #if DO_SWITCH_EXTRUDER
  917. SETUP_RUN(move_extruder_servo(0)); // Initialize extruder servo
  918. #endif
  919. #if ENABLED(SWITCHING_NOZZLE)
  920. SETUP_LOG("SWITCHING_NOZZLE");
  921. // Initialize nozzle servo(s)
  922. #if SWITCHING_NOZZLE_TWO_SERVOS
  923. lower_nozzle(0);
  924. raise_nozzle(1);
  925. #else
  926. move_nozzle_servo(0);
  927. #endif
  928. #endif
  929. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  930. SETUP_RUN(mpe_settings_init());
  931. #endif
  932. #if ENABLED(PARKING_EXTRUDER)
  933. SETUP_RUN(pe_solenoid_init());
  934. #endif
  935. #if ENABLED(SWITCHING_TOOLHEAD)
  936. swt_init();
  937. #endif
  938. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  939. SETUP_RUN(est_init());
  940. #endif
  941. #if ENABLED(POWER_LOSS_RECOVERY)
  942. SETUP_RUN(recovery.check());
  943. #endif
  944. #if ENABLED(USE_WATCHDOG)
  945. SETUP_RUN(watchdog_init()); // Reinit watchdog after HAL_get_reset_source call
  946. #endif
  947. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  948. SETUP_RUN(init_closedloop());
  949. #endif
  950. #ifdef STARTUP_COMMANDS
  951. SETUP_LOG("STARTUP_COMMANDS");
  952. queue.inject_P(PSTR(STARTUP_COMMANDS));
  953. #endif
  954. #if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
  955. SETUP_RUN(card.beginautostart());
  956. #endif
  957. #if ENABLED(HOST_PROMPT_SUPPORT)
  958. SETUP_RUN(host_action_prompt_end());
  959. #endif
  960. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  961. SETUP_RUN(test_tmc_connection(true, true, true, true));
  962. #endif
  963. #if ENABLED(PRUSA_MMU2)
  964. SETUP_RUN(mmu2.init());
  965. #endif
  966. #if ENABLED(MAX7219_DEBUG)
  967. SETUP_RUN(max7219.init());
  968. #endif
  969. marlin_state = MF_RUNNING;
  970. SETUP_LOG("setup() completed.");
  971. }
  972. /**
  973. * The main Marlin program loop
  974. *
  975. * - Call idle() to handle all tasks between G-code commands
  976. * Note that no G-codes from the queue can be executed during idle()
  977. * but many G-codes can be called directly anytime like macros.
  978. * - Check whether SD card auto-start is needed now.
  979. * - Check whether SD print finishing is needed now.
  980. * - Run one G-code command from the immediate or main command queue
  981. * and open up one space. Commands in the main queue may come from sd
  982. * card, host, or by direct injection. The queue will continue to fill
  983. * as long as idle() or manage_inactivity() are being called.
  984. */
  985. void loop() {
  986. do {
  987. idle();
  988. #if ENABLED(SDSUPPORT)
  989. card.checkautostart();
  990. if (card.flag.abort_sd_printing) abortSDPrinting();
  991. if (card.sdprinting_done_state) finishSDPrinting();
  992. #endif
  993. queue.advance();
  994. endstops.event_handler();
  995. } while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
  996. }