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

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