My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MarlinCore.cpp 28KB

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