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

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