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

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