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

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