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.

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