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

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