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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016, 2017 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/simen/grbl
  28. */
  29. #include "Marlin.h"
  30. #include "lcd/ultralcd.h"
  31. #include "module/motion.h"
  32. #include "module/planner.h"
  33. #include "module/stepper.h"
  34. #include "module/endstops.h"
  35. #include "module/probe.h"
  36. #include "module/temperature.h"
  37. #include "sd/cardreader.h"
  38. #include "module/configuration_store.h"
  39. #include "module/printcounter.h" // PrintCounter or Stopwatch
  40. #ifdef ARDUINO
  41. #include <pins_arduino.h>
  42. #endif
  43. #include <math.h>
  44. #include "libs/nozzle.h"
  45. #include "gcode/gcode.h"
  46. #include "gcode/parser.h"
  47. #include "gcode/queue.h"
  48. #if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
  49. #include "libs/buzzer.h"
  50. #endif
  51. #if (ENABLED(SWITCHING_EXTRUDER) && !DONT_SWITCH) || ENABLED(SWITCHING_NOZZLE)
  52. #include "module/tool_change.h"
  53. #endif
  54. #if ENABLED(BEZIER_CURVE_SUPPORT)
  55. #include "module/planner_bezier.h"
  56. #endif
  57. #if ENABLED(MAX7219_DEBUG)
  58. #include "feature/Max7219_Debug_LEDs.h"
  59. #endif
  60. #if HAS_COLOR_LEDS
  61. #include "feature/leds/leds.h"
  62. #endif
  63. #if HAS_SERVOS
  64. #include "HAL/servo.h"
  65. #endif
  66. #if HAS_DIGIPOTSS
  67. #include <SPI.h>
  68. #endif
  69. #if ENABLED(DAC_STEPPER_CURRENT)
  70. #include "feature/dac/stepper_dac.h"
  71. #endif
  72. #if ENABLED(EXPERIMENTAL_I2CBUS)
  73. #include "feature/twibus.h"
  74. #endif
  75. #if ENABLED(I2C_POSITION_ENCODERS)
  76. #include "feature/I2CPositionEncoder.h"
  77. #endif
  78. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  79. #include "HAL/HAL_endstop_interrupts.h"
  80. #endif
  81. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  82. void M100_dump_routine(const char * const title, const char *start, const char *end);
  83. #endif
  84. #if ENABLED(SDSUPPORT)
  85. CardReader card;
  86. #endif
  87. #if ENABLED(EXPERIMENTAL_I2CBUS)
  88. TWIBus i2c;
  89. #endif
  90. #if ENABLED(G38_PROBE_TARGET)
  91. bool G38_move = false,
  92. G38_endstop_hit = false;
  93. #endif
  94. #if ENABLED(DELTA)
  95. #include "module/delta.h"
  96. #elif IS_SCARA
  97. #include "module/scara.h"
  98. #endif
  99. #if HAS_LEVELING
  100. #include "feature/bedlevel/bedlevel.h"
  101. #endif
  102. #if ENABLED(SENSORLESS_HOMING)
  103. #include "feature/tmc2130.h"
  104. #endif
  105. #if ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT)
  106. #include "feature/pause.h"
  107. #endif
  108. #if ENABLED(TEMP_STAT_LEDS)
  109. #include "feature/leds/tempstat.h"
  110. #endif
  111. bool Running = true;
  112. /**
  113. * axis_homed
  114. * Flags that each linear axis was homed.
  115. * XYZ on cartesian, ABC on delta, ABZ on SCARA.
  116. *
  117. * axis_known_position
  118. * Flags that the position is known in each linear axis. Set when homed.
  119. * Cleared whenever a stepper powers off, potentially losing its position.
  120. */
  121. bool axis_homed[XYZ] = { false }, axis_known_position[XYZ] = { false };
  122. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  123. TempUnit input_temp_units = TEMPUNIT_C;
  124. #endif
  125. // Initialized by settings.load()
  126. float filament_size[EXTRUDERS], volumetric_multiplier[EXTRUDERS];
  127. #if FAN_COUNT > 0
  128. int16_t fanSpeeds[FAN_COUNT] = { 0 };
  129. #if ENABLED(PROBING_FANS_OFF)
  130. bool fans_paused = false;
  131. int16_t paused_fanSpeeds[FAN_COUNT] = { 0 };
  132. #endif
  133. #endif
  134. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  135. volatile bool wait_for_heatup = true;
  136. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  137. #if HAS_RESUME_CONTINUE
  138. volatile bool wait_for_user = false;
  139. #endif
  140. // Inactivity shutdown
  141. static millis_t max_inactive_time = 0;
  142. static millis_t stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
  143. #if ENABLED(Z_DUAL_ENDSTOPS)
  144. float z_endstop_adj;
  145. #endif
  146. #if ENABLED(BARICUDA)
  147. uint8_t baricuda_valve_pressure = 0,
  148. baricuda_e_to_p_pressure = 0;
  149. #endif
  150. #if HAS_POWER_SWITCH
  151. bool powersupply_on =
  152. #if ENABLED(PS_DEFAULT_OFF)
  153. false
  154. #else
  155. true
  156. #endif
  157. ;
  158. #endif
  159. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  160. static bool filament_ran_out = false;
  161. #endif
  162. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  163. AdvancedPauseMenuResponse advanced_pause_menu_response;
  164. #endif
  165. #if ENABLED(MIXING_EXTRUDER)
  166. float mixing_factor[MIXING_STEPPERS]; // Reciprocal of mix proportion. 0.0 = off, otherwise >= 1.0.
  167. #if MIXING_VIRTUAL_TOOLS > 1
  168. float mixing_virtual_tool_mix[MIXING_VIRTUAL_TOOLS][MIXING_STEPPERS];
  169. #endif
  170. #endif
  171. #ifdef CHDK
  172. millis_t chdkHigh = 0;
  173. bool chdkActive = false;
  174. #endif
  175. #if ENABLED(PID_EXTRUSION_SCALING)
  176. int lpq_len = 20;
  177. #endif
  178. #if ENABLED(I2C_POSITION_ENCODERS)
  179. I2CPositionEncodersMgr I2CPEM;
  180. uint8_t blockBufferIndexRef = 0;
  181. millis_t lastUpdateMillis;
  182. #endif
  183. /**
  184. * ***************************************************************************
  185. * ******************************** FUNCTIONS ********************************
  186. * ***************************************************************************
  187. */
  188. #if ENABLED(DIGIPOT_I2C)
  189. extern void digipot_i2c_set_current(uint8_t channel, float current);
  190. extern void digipot_i2c_init();
  191. #endif
  192. void setup_killpin() {
  193. #if HAS_KILL
  194. SET_INPUT_PULLUP(KILL_PIN);
  195. #endif
  196. }
  197. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  198. void setup_filrunoutpin() {
  199. #if ENABLED(ENDSTOPPULLUP_FIL_RUNOUT)
  200. SET_INPUT_PULLUP(FIL_RUNOUT_PIN);
  201. #else
  202. SET_INPUT(FIL_RUNOUT_PIN);
  203. #endif
  204. }
  205. #endif
  206. void setup_powerhold() {
  207. #if HAS_SUICIDE
  208. OUT_WRITE(SUICIDE_PIN, HIGH);
  209. #endif
  210. #if HAS_POWER_SWITCH
  211. #if ENABLED(PS_DEFAULT_OFF)
  212. OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  213. #else
  214. OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE);
  215. #endif
  216. #endif
  217. }
  218. void suicide() {
  219. #if HAS_SUICIDE
  220. OUT_WRITE(SUICIDE_PIN, LOW);
  221. #endif
  222. }
  223. #if HAS_SERVOS
  224. HAL_SERVO_LIB servo[NUM_SERVOS];
  225. void servo_init() {
  226. #if NUM_SERVOS >= 1 && HAS_SERVO_0
  227. servo[0].attach(SERVO0_PIN);
  228. servo[0].detach(); // Just set up the pin. We don't have a position yet. Don't move to a random position.
  229. #endif
  230. #if NUM_SERVOS >= 2 && HAS_SERVO_1
  231. servo[1].attach(SERVO1_PIN);
  232. servo[1].detach();
  233. #endif
  234. #if NUM_SERVOS >= 3 && HAS_SERVO_2
  235. servo[2].attach(SERVO2_PIN);
  236. servo[2].detach();
  237. #endif
  238. #if NUM_SERVOS >= 4 && HAS_SERVO_3
  239. servo[3].attach(SERVO3_PIN);
  240. servo[3].detach();
  241. #endif
  242. #if HAS_Z_SERVO_ENDSTOP
  243. servo_probe_init();
  244. #endif
  245. }
  246. #endif // HAS_SERVOS
  247. /**
  248. * Stepper Reset (RigidBoard, et.al.)
  249. */
  250. #if HAS_STEPPER_RESET
  251. void disableStepperDrivers() {
  252. OUT_WRITE(STEPPER_RESET_PIN, LOW); // drive it down to hold in reset motor driver chips
  253. }
  254. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // set to input, which allows it to be pulled high by pullups
  255. #endif
  256. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  257. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  258. i2c.receive(bytes);
  259. }
  260. void i2c_on_request() { // just send dummy data for now
  261. i2c.reply("Hello World!\n");
  262. }
  263. #endif
  264. #if ENABLED(MIXING_EXTRUDER)
  265. void normalize_mix() {
  266. float mix_total = 0.0;
  267. for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mix_total += RECIPROCAL(mixing_factor[i]);
  268. // Scale all values if they don't add up to ~1.0
  269. if (!NEAR(mix_total, 1.0)) {
  270. SERIAL_PROTOCOLLNPGM("Warning: Mix factors must add up to 1.0. Scaling.");
  271. for (uint8_t i = 0; i < MIXING_STEPPERS; i++) mixing_factor[i] *= mix_total;
  272. }
  273. }
  274. #if ENABLED(DIRECT_MIXING_IN_G1)
  275. // Get mixing parameters from the GCode
  276. // The total "must" be 1.0 (but it will be normalized)
  277. // If no mix factors are given, the old mix is preserved
  278. void gcode_get_mix() {
  279. const char* mixing_codes = "ABCDHI";
  280. byte mix_bits = 0;
  281. for (uint8_t i = 0; i < MIXING_STEPPERS; i++) {
  282. if (parser.seenval(mixing_codes[i])) {
  283. SBI(mix_bits, i);
  284. float v = parser.value_float();
  285. NOLESS(v, 0.0);
  286. mixing_factor[i] = RECIPROCAL(v);
  287. }
  288. }
  289. // If any mixing factors were included, clear the rest
  290. // If none were included, preserve the last mix
  291. if (mix_bits) {
  292. for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
  293. if (!TEST(mix_bits, i)) mixing_factor[i] = 0.0;
  294. normalize_mix();
  295. }
  296. }
  297. #endif
  298. #endif
  299. /**************************************************
  300. ***************** GCode Handlers *****************
  301. **************************************************/
  302. /**
  303. * Sensitive pin test for M42, M226
  304. */
  305. bool pin_is_protected(const int8_t pin) {
  306. static const int8_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  307. for (uint8_t i = 0; i < COUNT(sensitive_pins); i++)
  308. if (pin == (int8_t)pgm_read_byte(&sensitive_pins[i])) return true;
  309. return false;
  310. }
  311. #if ENABLED(AUTO_REPORT_TEMPERATURES) && (HAS_TEMP_HOTEND || HAS_TEMP_BED)
  312. static uint8_t auto_report_temp_interval;
  313. static millis_t next_temp_report_ms;
  314. inline void auto_report_temperatures() {
  315. if (auto_report_temp_interval && ELAPSED(millis(), next_temp_report_ms)) {
  316. next_temp_report_ms = millis() + 1000UL * auto_report_temp_interval;
  317. thermalManager.print_heaterstates();
  318. SERIAL_EOL();
  319. }
  320. }
  321. #include "gcode/temperature/M155.h"
  322. #endif // AUTO_REPORT_TEMPERATURES && (HAS_TEMP_HOTEND || HAS_TEMP_BED)
  323. #if FAN_COUNT > 0
  324. #include "gcode/temperature/M106.h"
  325. #include "gcode/temperature/M107.h"
  326. #endif
  327. #if DISABLED(EMERGENCY_PARSER)
  328. #include "gcode/control/M108.h"
  329. #include "gcode/control/M112.h"
  330. #include "gcode/control/M410.h"
  331. #endif
  332. #if HAS_TEMP_BED
  333. #include "gcode/temperature/M190.h"
  334. #endif
  335. #include "gcode/host/M110.h"
  336. #include "gcode/control/M111.h"
  337. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  338. #include "gcode/host/M113.h"
  339. #endif
  340. #if ENABLED(BARICUDA)
  341. #if HAS_HEATER_1
  342. #include "gcode/feature/baricuda/M126.h"
  343. #include "gcode/feature/baricuda/M127.h"
  344. #endif
  345. #if HAS_HEATER_2
  346. #include "gcode/feature/baricuda/M128.h"
  347. #include "gcode/feature/baricuda/M129.h"
  348. #endif
  349. #endif
  350. #include "gcode/temperature/M140.h"
  351. #if ENABLED(ULTIPANEL)
  352. #include "gcode/lcd/M145.h"
  353. #endif
  354. #if ENABLED(TEMPERATURE_UNITS_SUPPORT)
  355. #include "gcode/units/M149.h"
  356. #endif
  357. #if HAS_POWER_SWITCH
  358. #include "gcode/control/M80.h"
  359. #endif
  360. #include "gcode/control/M81.h"
  361. #include "gcode/units/M82_M83.h"
  362. #include "gcode/control/M18_M84.h"
  363. #include "gcode/control/M85.h"
  364. #include "gcode/config/M92.h"
  365. #if ENABLED(M100_FREE_MEMORY_WATCHER)
  366. #include "gcode/calibrate/M100.h"
  367. #endif
  368. #include "gcode/host/M114.h"
  369. #include "gcode/host/M115.h"
  370. #include "gcode/lcd/M117.h"
  371. #include "gcode/host/M118.h"
  372. #include "gcode/host/M119.h"
  373. #include "gcode/control/M120_M121.h"
  374. #if HAS_COLOR_LEDS
  375. #include "gcode/feature/leds/M150.h"
  376. #endif
  377. #include "gcode/config/M201.h"
  378. #if 0 // Not used for Sprinter/grbl gen6
  379. #include "gcode/config/M202.h"
  380. #endif
  381. #include "gcode/config/M203.h"
  382. #include "gcode/config/M204.h"
  383. #include "gcode/config/M205.h"
  384. #if HAS_M206_COMMAND
  385. #include "gcode/geometry/M206.h"
  386. #endif
  387. #if IS_KINEMATIC
  388. #include "gcode/calibrate/M665.h"
  389. #endif
  390. #if ENABLED(DELTA) || ENABLED(Z_DUAL_ENDSTOPS)
  391. #include "gcode/calibrate/M666.h"
  392. #endif
  393. #include "gcode/control/M211.h"
  394. #include "gcode/config/M220.h"
  395. #include "gcode/control/M226.h"
  396. #if ENABLED(EXPERIMENTAL_I2CBUS)
  397. #include "gcode/feature/i2c/M260_M261.h"
  398. #endif
  399. #if HAS_SERVOS
  400. #include "gcode/control/M280.h"
  401. #endif
  402. #if HAS_BUZZER
  403. #include "gcode/lcd/M300.h"
  404. #endif
  405. #if ENABLED(PIDTEMP)
  406. #include "gcode/config/M301.h"
  407. #endif
  408. #if ENABLED(PIDTEMPBED)
  409. #include "gcode/config/M304.h"
  410. #endif
  411. #if defined(CHDK) || HAS_PHOTOGRAPH
  412. #include "gcode/feature/camera/M240.h"
  413. #endif
  414. #if HAS_LCD_CONTRAST
  415. #include "gcode/lcd/M250.h"
  416. #endif
  417. #if ENABLED(PREVENT_COLD_EXTRUSION)
  418. #include "gcode/config/M302.h"
  419. #endif
  420. #if ENABLED(MORGAN_SCARA)
  421. #include "gcode/scara/M360-M364.h"
  422. #endif
  423. #if ENABLED(EXT_SOLENOID)
  424. #include "gcode/control/M380_M381.h"
  425. #endif
  426. #include "gcode/control/M400.h"
  427. #if HAS_BED_PROBE
  428. #include "gcode/probe/M401_M402.h"
  429. #endif
  430. void quickstop_stepper() {
  431. stepper.quick_stop();
  432. stepper.synchronize();
  433. set_current_from_steppers_for_axis(ALL_AXES);
  434. SYNC_PLAN_POSITION_KINEMATIC();
  435. }
  436. #if HAS_M206_COMMAND
  437. #include "gcode/geometry/M428.h"
  438. #endif
  439. #include "gcode/eeprom/M500.h"
  440. #include "gcode/eeprom/M501.h"
  441. #include "gcode/eeprom/M502.h"
  442. #if DISABLED(DISABLE_M503)
  443. #include "gcode/eeprom/M503.h"
  444. #endif
  445. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  446. #include "gcode/config/M540.h"
  447. #endif
  448. #if ENABLED(MK2_MULTIPLEXER)
  449. #include "gcode/feature/snmm/M702.h"
  450. #endif
  451. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  452. #include "gcode/control/M605.h"
  453. #endif
  454. #if ENABLED(LIN_ADVANCE)
  455. #include "gcode/feature/advance/M900.h"
  456. #endif
  457. #if ENABLED(HAVE_TMC2130)
  458. #include "feature/tmc2130.h"
  459. #include "gcode/feature/trinamic/M906.h"
  460. #include "gcode/feature/trinamic/M911.h"
  461. #include "gcode/feature/trinamic/M912.h"
  462. #if ENABLED(HYBRID_THRESHOLD)
  463. #include "gcode/feature/trinamic/M913.h"
  464. #endif
  465. #if ENABLED(SENSORLESS_HOMING)
  466. #include "gcode/feature/trinamic/M914.h"
  467. #endif
  468. #endif
  469. #include "gcode/feature/digipot/M907.h"
  470. #if HAS_DIGIPOTSS || ENABLED(DAC_STEPPER_CURRENT)
  471. #include "gcode/feature/digipot/M908.h"
  472. #if ENABLED(DAC_STEPPER_CURRENT) // As with Printrbot RevF
  473. #include "gcode/feature/digipot/M909.h"
  474. #include "gcode/feature/digipot/M910.h"
  475. #endif
  476. #endif
  477. #if HAS_MICROSTEPS
  478. #include "gcode/control/M350.h"
  479. #include "gcode/control/M351.h"
  480. #endif
  481. #include "gcode/feature/caselight/M355.h"
  482. #if ENABLED(MIXING_EXTRUDER)
  483. #include "gcode/feature/mixing/M163.h"
  484. #if MIXING_VIRTUAL_TOOLS > 1
  485. #include "gcode/feature/mixing/M164.h"
  486. #endif
  487. #if ENABLED(DIRECT_MIXING_IN_G1)
  488. #include "gcode/feature/mixing/M165.h"
  489. #endif
  490. #endif
  491. #include "gcode/control/M999.h"
  492. #include "gcode/control/T.h"
  493. #if ENABLED(USE_CONTROLLER_FAN)
  494. void controllerFan() {
  495. static millis_t lastMotorOn = 0, // Last time a motor was turned on
  496. nextMotorCheck = 0; // Last time the state was checked
  497. const millis_t ms = millis();
  498. if (ELAPSED(ms, nextMotorCheck)) {
  499. nextMotorCheck = ms + 2500UL; // Not a time critical function, so only check every 2.5s
  500. if (X_ENABLE_READ == X_ENABLE_ON || Y_ENABLE_READ == Y_ENABLE_ON || Z_ENABLE_READ == Z_ENABLE_ON || thermalManager.soft_pwm_amount_bed > 0
  501. || E0_ENABLE_READ == E_ENABLE_ON // If any of the drivers are enabled...
  502. #if E_STEPPERS > 1
  503. || E1_ENABLE_READ == E_ENABLE_ON
  504. #if HAS_X2_ENABLE
  505. || X2_ENABLE_READ == X_ENABLE_ON
  506. #endif
  507. #if E_STEPPERS > 2
  508. || E2_ENABLE_READ == E_ENABLE_ON
  509. #if E_STEPPERS > 3
  510. || E3_ENABLE_READ == E_ENABLE_ON
  511. #if E_STEPPERS > 4
  512. || E4_ENABLE_READ == E_ENABLE_ON
  513. #endif // E_STEPPERS > 4
  514. #endif // E_STEPPERS > 3
  515. #endif // E_STEPPERS > 2
  516. #endif // E_STEPPERS > 1
  517. ) {
  518. lastMotorOn = ms; //... set time to NOW so the fan will turn on
  519. }
  520. // Fan off if no steppers have been enabled for CONTROLLERFAN_SECS seconds
  521. uint8_t speed = (!lastMotorOn || ELAPSED(ms, lastMotorOn + (CONTROLLERFAN_SECS) * 1000UL)) ? 0 : CONTROLLERFAN_SPEED;
  522. // allows digital or PWM fan output to be used (see M42 handling)
  523. WRITE(CONTROLLER_FAN_PIN, speed);
  524. analogWrite(CONTROLLER_FAN_PIN, speed);
  525. }
  526. }
  527. #endif // USE_CONTROLLER_FAN
  528. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  529. void handle_filament_runout() {
  530. if (!filament_ran_out) {
  531. filament_ran_out = true;
  532. enqueue_and_echo_commands_P(PSTR(FILAMENT_RUNOUT_SCRIPT));
  533. stepper.synchronize();
  534. }
  535. }
  536. #endif // FILAMENT_RUNOUT_SENSOR
  537. float calculate_volumetric_multiplier(const float diameter) {
  538. if (!parser.volumetric_enabled || diameter == 0) return 1.0;
  539. return 1.0 / (M_PI * sq(diameter * 0.5));
  540. }
  541. void calculate_volumetric_multipliers() {
  542. for (uint8_t i = 0; i < COUNT(filament_size); i++)
  543. volumetric_multiplier[i] = calculate_volumetric_multiplier(filament_size[i]);
  544. }
  545. void enable_all_steppers() {
  546. enable_X();
  547. enable_Y();
  548. enable_Z();
  549. enable_E0();
  550. enable_E1();
  551. enable_E2();
  552. enable_E3();
  553. enable_E4();
  554. }
  555. void disable_e_steppers() {
  556. disable_E0();
  557. disable_E1();
  558. disable_E2();
  559. disable_E3();
  560. disable_E4();
  561. }
  562. void disable_all_steppers() {
  563. disable_X();
  564. disable_Y();
  565. disable_Z();
  566. disable_e_steppers();
  567. }
  568. /**
  569. * Manage several activities:
  570. * - Check for Filament Runout
  571. * - Keep the command buffer full
  572. * - Check for maximum inactive time between commands
  573. * - Check for maximum inactive time between stepper commands
  574. * - Check if pin CHDK needs to go LOW
  575. * - Check for KILL button held down
  576. * - Check for HOME button held down
  577. * - Check if cooling fan needs to be switched on
  578. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  579. */
  580. void manage_inactivity(bool ignore_stepper_queue/*=false*/) {
  581. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  582. if ((IS_SD_PRINTING || print_job_timer.isRunning()) && (READ(FIL_RUNOUT_PIN) == FIL_RUNOUT_INVERTING))
  583. handle_filament_runout();
  584. #endif
  585. if (commands_in_queue < BUFSIZE) get_available_commands();
  586. const millis_t ms = millis();
  587. if (max_inactive_time && ELAPSED(ms, gcode.previous_cmd_ms + max_inactive_time)) {
  588. SERIAL_ERROR_START();
  589. SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
  590. kill(PSTR(MSG_KILLED));
  591. }
  592. // Prevent steppers timing-out in the middle of M600
  593. #if ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT)
  594. #define MOVE_AWAY_TEST !move_away_flag
  595. #else
  596. #define MOVE_AWAY_TEST true
  597. #endif
  598. if (MOVE_AWAY_TEST && stepper_inactive_time && ELAPSED(ms, gcode.previous_cmd_ms + stepper_inactive_time)
  599. && !ignore_stepper_queue && !planner.blocks_queued()) {
  600. #if ENABLED(DISABLE_INACTIVE_X)
  601. disable_X();
  602. #endif
  603. #if ENABLED(DISABLE_INACTIVE_Y)
  604. disable_Y();
  605. #endif
  606. #if ENABLED(DISABLE_INACTIVE_Z)
  607. disable_Z();
  608. #endif
  609. #if ENABLED(DISABLE_INACTIVE_E)
  610. disable_e_steppers();
  611. #endif
  612. #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(ULTRA_LCD) // Only needed with an LCD
  613. ubl.lcd_map_control = defer_return_to_status = false;
  614. #endif
  615. }
  616. #ifdef CHDK // Check if pin should be set to LOW after M240 set it to HIGH
  617. if (chdkActive && ELAPSED(ms, chdkHigh + CHDK_DELAY)) {
  618. chdkActive = false;
  619. WRITE(CHDK, LOW);
  620. }
  621. #endif
  622. #if HAS_KILL
  623. // Check if the kill button was pressed and wait just in case it was an accidental
  624. // key kill key press
  625. // -------------------------------------------------------------------------------
  626. static int killCount = 0; // make the inactivity button a bit less responsive
  627. const int KILL_DELAY = 750;
  628. if (!READ(KILL_PIN))
  629. killCount++;
  630. else if (killCount > 0)
  631. killCount--;
  632. // Exceeded threshold and we can confirm that it was not accidental
  633. // KILL the machine
  634. // ----------------------------------------------------------------
  635. if (killCount >= KILL_DELAY) {
  636. SERIAL_ERROR_START();
  637. SERIAL_ERRORLNPGM(MSG_KILL_BUTTON);
  638. kill(PSTR(MSG_KILLED));
  639. }
  640. #endif
  641. #if HAS_HOME
  642. // Check to see if we have to home, use poor man's debouncer
  643. // ---------------------------------------------------------
  644. static int homeDebounceCount = 0; // poor man's debouncing count
  645. const int HOME_DEBOUNCE_DELAY = 2500;
  646. if (!IS_SD_PRINTING && !READ(HOME_PIN)) {
  647. if (!homeDebounceCount) {
  648. enqueue_and_echo_commands_P(PSTR("G28"));
  649. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  650. }
  651. if (homeDebounceCount < HOME_DEBOUNCE_DELAY)
  652. homeDebounceCount++;
  653. else
  654. homeDebounceCount = 0;
  655. }
  656. #endif
  657. #if ENABLED(USE_CONTROLLER_FAN)
  658. controllerFan(); // Check if fan should be turned on to cool stepper drivers down
  659. #endif
  660. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  661. if (ELAPSED(ms, gcode.previous_cmd_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  662. && thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP) {
  663. #if ENABLED(SWITCHING_EXTRUDER)
  664. const bool oldstatus = E0_ENABLE_READ;
  665. enable_E0();
  666. #else // !SWITCHING_EXTRUDER
  667. bool oldstatus;
  668. switch (active_extruder) {
  669. default: oldstatus = E0_ENABLE_READ; enable_E0(); break;
  670. #if E_STEPPERS > 1
  671. case 1: oldstatus = E1_ENABLE_READ; enable_E1(); break;
  672. #if E_STEPPERS > 2
  673. case 2: oldstatus = E2_ENABLE_READ; enable_E2(); break;
  674. #if E_STEPPERS > 3
  675. case 3: oldstatus = E3_ENABLE_READ; enable_E3(); break;
  676. #if E_STEPPERS > 4
  677. case 4: oldstatus = E4_ENABLE_READ; enable_E4(); break;
  678. #endif // E_STEPPERS > 4
  679. #endif // E_STEPPERS > 3
  680. #endif // E_STEPPERS > 2
  681. #endif // E_STEPPERS > 1
  682. }
  683. #endif // !SWITCHING_EXTRUDER
  684. gcode.refresh_cmd_timeout()
  685. const float olde = current_position[E_AXIS];
  686. current_position[E_AXIS] += EXTRUDER_RUNOUT_EXTRUDE;
  687. planner.buffer_line_kinematic(current_position, MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED), active_extruder);
  688. current_position[E_AXIS] = olde;
  689. planner.set_e_position_mm(olde);
  690. stepper.synchronize();
  691. #if ENABLED(SWITCHING_EXTRUDER)
  692. E0_ENABLE_WRITE(oldstatus);
  693. #else
  694. switch (active_extruder) {
  695. case 0: E0_ENABLE_WRITE(oldstatus); break;
  696. #if E_STEPPERS > 1
  697. case 1: E1_ENABLE_WRITE(oldstatus); break;
  698. #if E_STEPPERS > 2
  699. case 2: E2_ENABLE_WRITE(oldstatus); break;
  700. #if E_STEPPERS > 3
  701. case 3: E3_ENABLE_WRITE(oldstatus); break;
  702. #if E_STEPPERS > 4
  703. case 4: E4_ENABLE_WRITE(oldstatus); break;
  704. #endif // E_STEPPERS > 4
  705. #endif // E_STEPPERS > 3
  706. #endif // E_STEPPERS > 2
  707. #endif // E_STEPPERS > 1
  708. }
  709. #endif // !SWITCHING_EXTRUDER
  710. }
  711. #endif // EXTRUDER_RUNOUT_PREVENT
  712. #if ENABLED(DUAL_X_CARRIAGE)
  713. // handle delayed move timeout
  714. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  715. // travel moves have been received so enact them
  716. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  717. set_destination_to_current();
  718. prepare_move_to_destination();
  719. }
  720. #endif
  721. #if ENABLED(TEMP_STAT_LEDS)
  722. handle_status_leds();
  723. #endif
  724. #if ENABLED(HAVE_TMC2130)
  725. tmc2130_checkOverTemp();
  726. #endif
  727. planner.check_axes_activity();
  728. }
  729. /**
  730. * Standard idle routine keeps the machine alive
  731. */
  732. void idle(
  733. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  734. bool no_stepper_sleep/*=false*/
  735. #endif
  736. ) {
  737. #if ENABLED(MAX7219_DEBUG)
  738. Max7219_idle_tasks();
  739. #endif // MAX7219_DEBUG
  740. lcd_update();
  741. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  742. gcode.host_keepalive();
  743. #endif
  744. #if ENABLED(AUTO_REPORT_TEMPERATURES) && (HAS_TEMP_HOTEND || HAS_TEMP_BED)
  745. auto_report_temperatures();
  746. #endif
  747. manage_inactivity(
  748. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  749. no_stepper_sleep
  750. #endif
  751. );
  752. thermalManager.manage_heater();
  753. #if ENABLED(PRINTCOUNTER)
  754. print_job_timer.tick();
  755. #endif
  756. #if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
  757. buzzer.tick();
  758. #endif
  759. #if ENABLED(I2C_POSITION_ENCODERS)
  760. if (planner.blocks_queued() &&
  761. ( (blockBufferIndexRef != planner.block_buffer_head) ||
  762. ((lastUpdateMillis + I2CPE_MIN_UPD_TIME_MS) < millis())) ) {
  763. blockBufferIndexRef = planner.block_buffer_head;
  764. I2CPEM.update();
  765. lastUpdateMillis = millis();
  766. }
  767. #endif
  768. }
  769. /**
  770. * Kill all activity and lock the machine.
  771. * After this the machine will need to be reset.
  772. */
  773. void kill(const char* lcd_msg) {
  774. SERIAL_ERROR_START();
  775. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  776. thermalManager.disable_all_heaters();
  777. disable_all_steppers();
  778. #if ENABLED(ULTRA_LCD)
  779. kill_screen(lcd_msg);
  780. #else
  781. UNUSED(lcd_msg);
  782. #endif
  783. _delay_ms(600); // Wait a short time (allows messages to get out before shutting down.
  784. cli(); // Stop interrupts
  785. _delay_ms(250); //Wait to ensure all interrupts routines stopped
  786. thermalManager.disable_all_heaters(); //turn off heaters again
  787. #ifdef ACTION_ON_KILL
  788. SERIAL_ECHOLNPGM("//action:" ACTION_ON_KILL);
  789. #endif
  790. #if HAS_POWER_SWITCH
  791. SET_INPUT(PS_ON_PIN);
  792. #endif
  793. suicide();
  794. while (1) {
  795. #if ENABLED(USE_WATCHDOG)
  796. watchdog_reset();
  797. #endif
  798. } // Wait for reset
  799. }
  800. /**
  801. * Turn off heaters and stop the print in progress
  802. * After a stop the machine may be resumed with M999
  803. */
  804. void stop() {
  805. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  806. #if ENABLED(PROBING_FANS_OFF)
  807. if (fans_paused) fans_pause(false); // put things back the way they were
  808. #endif
  809. if (IsRunning()) {
  810. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  811. SERIAL_ERROR_START();
  812. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  813. LCD_MESSAGEPGM(MSG_STOPPED);
  814. safe_delay(350); // allow enough time for messages to get out before stopping
  815. Running = false;
  816. }
  817. }
  818. /**
  819. * Marlin entry-point: Set up before the program loop
  820. * - Set up the kill pin, filament runout, power hold
  821. * - Start the serial port
  822. * - Print startup messages and diagnostics
  823. * - Get EEPROM or default settings
  824. * - Initialize managers for:
  825. * • temperature
  826. * • planner
  827. * • watchdog
  828. * • stepper
  829. * • photo pin
  830. * • servos
  831. * • LCD controller
  832. * • Digipot I2C
  833. * • Z probe sled
  834. * • status LEDs
  835. */
  836. void setup() {
  837. #if ENABLED(MAX7219_DEBUG)
  838. Max7219_init();
  839. #endif
  840. #ifdef DISABLE_JTAG
  841. // Disable JTAG on AT90USB chips to free up pins for IO
  842. MCUCR = 0x80;
  843. MCUCR = 0x80;
  844. #endif
  845. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  846. setup_filrunoutpin();
  847. #endif
  848. setup_killpin();
  849. setup_powerhold();
  850. #if HAS_STEPPER_RESET
  851. disableStepperDrivers();
  852. #endif
  853. MYSERIAL.begin(BAUDRATE);
  854. while(!MYSERIAL);
  855. SERIAL_PROTOCOLLNPGM("start");
  856. SERIAL_ECHO_START();
  857. // Check startup - does nothing if bootloader sets MCUSR to 0
  858. byte mcu = HAL_get_reset_source();
  859. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  860. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  861. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  862. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  863. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  864. HAL_clear_reset_source();
  865. #if ENABLED(USE_WATCHDOG) //reinit watchdog after HAL_get_reset_source call
  866. watchdog_init();
  867. #endif
  868. SERIAL_ECHOPGM(MSG_MARLIN);
  869. SERIAL_CHAR(' ');
  870. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  871. SERIAL_EOL();
  872. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  873. SERIAL_ECHO_START();
  874. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  875. SERIAL_ECHOPGM(STRING_DISTRIBUTION_DATE);
  876. SERIAL_ECHOLNPGM(MSG_AUTHOR STRING_CONFIG_H_AUTHOR);
  877. SERIAL_ECHO_START();
  878. SERIAL_ECHOLNPGM("Compiled: " __DATE__);
  879. #endif
  880. SERIAL_ECHO_START();
  881. SERIAL_ECHOPAIR(MSG_FREE_MEMORY, freeMemory());
  882. SERIAL_ECHOLNPAIR(MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  883. queue_setup();
  884. // Load data from EEPROM if available (or use defaults)
  885. // This also updates variables in the planner, elsewhere
  886. (void)settings.load();
  887. #if HAS_M206_COMMAND
  888. // Initialize current position based on home_offset
  889. COPY(current_position, home_offset);
  890. #else
  891. ZERO(current_position);
  892. #endif
  893. // Vital to init stepper/planner equivalent for current_position
  894. SYNC_PLAN_POSITION_KINEMATIC();
  895. thermalManager.init(); // Initialize temperature loop
  896. stepper.init(); // Initialize stepper, this enables interrupts!
  897. #if HAS_SERVOS
  898. servo_init();
  899. #endif
  900. #if HAS_PHOTOGRAPH
  901. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  902. #endif
  903. #if HAS_CASE_LIGHT
  904. case_light_on = CASE_LIGHT_DEFAULT_ON;
  905. case_light_brightness = CASE_LIGHT_DEFAULT_BRIGHTNESS;
  906. update_case_light();
  907. #endif
  908. #if ENABLED(SPINDLE_LASER_ENABLE)
  909. OUT_WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); // init spindle to off
  910. #if SPINDLE_DIR_CHANGE
  911. OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // init rotation to clockwise (M3)
  912. #endif
  913. #if ENABLED(SPINDLE_LASER_PWM) && defined(SPINDLE_LASER_PWM_PIN) && SPINDLE_LASER_PWM_PIN >= 0
  914. SET_OUTPUT(SPINDLE_LASER_PWM_PIN);
  915. analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // set to lowest speed
  916. #endif
  917. #endif
  918. #if HAS_BED_PROBE
  919. endstops.enable_z_probe(false);
  920. #endif
  921. #if ENABLED(USE_CONTROLLER_FAN)
  922. SET_OUTPUT(CONTROLLER_FAN_PIN); //Set pin used for driver cooling fan
  923. #endif
  924. #if HAS_STEPPER_RESET
  925. enableStepperDrivers();
  926. #endif
  927. #if ENABLED(DIGIPOT_I2C)
  928. digipot_i2c_init();
  929. #endif
  930. #if ENABLED(DAC_STEPPER_CURRENT)
  931. dac_init();
  932. #endif
  933. #if (ENABLED(Z_PROBE_SLED) || ENABLED(SOLENOID_PROBE)) && HAS_SOLENOID_1
  934. OUT_WRITE(SOL1_PIN, LOW); // turn it off
  935. #endif
  936. #if HAS_HOME
  937. SET_INPUT_PULLUP(HOME_PIN);
  938. #endif
  939. #if PIN_EXISTS(STAT_LED_RED)
  940. OUT_WRITE(STAT_LED_RED_PIN, LOW); // turn it off
  941. #endif
  942. #if PIN_EXISTS(STAT_LED_BLUE)
  943. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // turn it off
  944. #endif
  945. #if ENABLED(NEOPIXEL_RGBW_LED)
  946. SET_OUTPUT(NEOPIXEL_PIN);
  947. setup_neopixel();
  948. #endif
  949. #if ENABLED(RGB_LED) || ENABLED(RGBW_LED)
  950. SET_OUTPUT(RGB_LED_R_PIN);
  951. SET_OUTPUT(RGB_LED_G_PIN);
  952. SET_OUTPUT(RGB_LED_B_PIN);
  953. #if ENABLED(RGBW_LED)
  954. SET_OUTPUT(RGB_LED_W_PIN);
  955. #endif
  956. #endif
  957. #if ENABLED(MK2_MULTIPLEXER)
  958. SET_OUTPUT(E_MUX0_PIN);
  959. SET_OUTPUT(E_MUX1_PIN);
  960. SET_OUTPUT(E_MUX2_PIN);
  961. #endif
  962. #if HAS_FANMUX
  963. fanmux_init();
  964. #endif
  965. lcd_init();
  966. #ifndef CUSTOM_BOOTSCREEN_TIMEOUT
  967. #define CUSTOM_BOOTSCREEN_TIMEOUT 2500
  968. #endif
  969. #if ENABLED(SHOW_BOOTSCREEN)
  970. #if ENABLED(DOGLCD) // On DOGM the first bootscreen is already drawn
  971. #if ENABLED(SHOW_CUSTOM_BOOTSCREEN)
  972. safe_delay(CUSTOM_BOOTSCREEN_TIMEOUT); // Custom boot screen pause
  973. lcd_bootscreen(); // Show Marlin boot screen
  974. #endif
  975. safe_delay(BOOTSCREEN_TIMEOUT); // Pause
  976. #elif ENABLED(ULTRA_LCD)
  977. lcd_bootscreen();
  978. #if DISABLED(SDSUPPORT)
  979. lcd_init();
  980. #endif
  981. #endif
  982. #endif
  983. #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1
  984. // Initialize mixing to 100% color 1
  985. for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
  986. mixing_factor[i] = (i == 0) ? 1.0 : 0.0;
  987. for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS; t++)
  988. for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
  989. mixing_virtual_tool_mix[t][i] = mixing_factor[i];
  990. #endif
  991. #if ENABLED(BLTOUCH)
  992. bltouch_init();
  993. #endif
  994. #if ENABLED(I2C_POSITION_ENCODERS)
  995. I2CPEM.init();
  996. #endif
  997. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  998. i2c.onReceive(i2c_on_receive);
  999. i2c.onRequest(i2c_on_request);
  1000. #endif
  1001. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  1002. setup_endstop_interrupts();
  1003. #endif
  1004. #if ENABLED(SWITCHING_EXTRUDER) && !DONT_SWITCH
  1005. move_extruder_servo(0); // Initialize extruder servo
  1006. #endif
  1007. #if ENABLED(SWITCHING_NOZZLE)
  1008. move_nozzle_servo(0); // Initialize nozzle servo
  1009. #endif
  1010. #if ENABLED(PARKING_EXTRUDER)
  1011. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  1012. pe_activate_magnet(0);
  1013. pe_activate_magnet(1);
  1014. #else
  1015. pe_deactivate_magnet(0);
  1016. pe_deactivate_magnet(1);
  1017. #endif
  1018. #endif
  1019. }
  1020. /**
  1021. * The main Marlin program loop
  1022. *
  1023. * - Save or log commands to SD
  1024. * - Process available commands (if not saving)
  1025. * - Call heater manager
  1026. * - Call inactivity manager
  1027. * - Call endstop manager
  1028. * - Call LCD update
  1029. */
  1030. void loop() {
  1031. if (commands_in_queue < BUFSIZE) get_available_commands();
  1032. #if ENABLED(SDSUPPORT)
  1033. card.checkautostart(false);
  1034. #endif
  1035. advance_command_queue();
  1036. endstops.report_state();
  1037. idle();
  1038. }