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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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. void quickstop_stepper() {
  294. stepper.quick_stop();
  295. stepper.synchronize();
  296. set_current_from_steppers_for_axis(ALL_AXES);
  297. SYNC_PLAN_POSITION_KINEMATIC();
  298. }
  299. #if ENABLED(HAVE_TMC2130)
  300. #include "feature/tmc2130.h"
  301. #include "gcode/feature/trinamic/M906.h"
  302. #include "gcode/feature/trinamic/M911.h"
  303. #include "gcode/feature/trinamic/M912.h"
  304. #if ENABLED(HYBRID_THRESHOLD)
  305. #include "gcode/feature/trinamic/M913.h"
  306. #endif
  307. #if ENABLED(SENSORLESS_HOMING)
  308. #include "gcode/feature/trinamic/M914.h"
  309. #endif
  310. #endif
  311. #include "gcode/feature/digipot/M907.h"
  312. #if HAS_DIGIPOTSS || ENABLED(DAC_STEPPER_CURRENT)
  313. #include "gcode/feature/digipot/M908.h"
  314. #if ENABLED(DAC_STEPPER_CURRENT) // As with Printrbot RevF
  315. #include "gcode/feature/digipot/M909.h"
  316. #include "gcode/feature/digipot/M910.h"
  317. #endif
  318. #endif
  319. #if HAS_MICROSTEPS
  320. #include "gcode/control/M350.h"
  321. #include "gcode/control/M351.h"
  322. #endif
  323. #include "gcode/feature/caselight/M355.h"
  324. #if ENABLED(MIXING_EXTRUDER)
  325. #include "gcode/feature/mixing/M163.h"
  326. #if MIXING_VIRTUAL_TOOLS > 1
  327. #include "gcode/feature/mixing/M164.h"
  328. #endif
  329. #if ENABLED(DIRECT_MIXING_IN_G1)
  330. #include "gcode/feature/mixing/M165.h"
  331. #endif
  332. #endif
  333. #include "gcode/control/M999.h"
  334. #include "gcode/control/T.h"
  335. #if ENABLED(USE_CONTROLLER_FAN)
  336. void controllerFan() {
  337. static millis_t lastMotorOn = 0, // Last time a motor was turned on
  338. nextMotorCheck = 0; // Last time the state was checked
  339. const millis_t ms = millis();
  340. if (ELAPSED(ms, nextMotorCheck)) {
  341. nextMotorCheck = ms + 2500UL; // Not a time critical function, so only check every 2.5s
  342. 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
  343. || E0_ENABLE_READ == E_ENABLE_ON // If any of the drivers are enabled...
  344. #if E_STEPPERS > 1
  345. || E1_ENABLE_READ == E_ENABLE_ON
  346. #if HAS_X2_ENABLE
  347. || X2_ENABLE_READ == X_ENABLE_ON
  348. #endif
  349. #if E_STEPPERS > 2
  350. || E2_ENABLE_READ == E_ENABLE_ON
  351. #if E_STEPPERS > 3
  352. || E3_ENABLE_READ == E_ENABLE_ON
  353. #if E_STEPPERS > 4
  354. || E4_ENABLE_READ == E_ENABLE_ON
  355. #endif // E_STEPPERS > 4
  356. #endif // E_STEPPERS > 3
  357. #endif // E_STEPPERS > 2
  358. #endif // E_STEPPERS > 1
  359. ) {
  360. lastMotorOn = ms; //... set time to NOW so the fan will turn on
  361. }
  362. // Fan off if no steppers have been enabled for CONTROLLERFAN_SECS seconds
  363. uint8_t speed = (!lastMotorOn || ELAPSED(ms, lastMotorOn + (CONTROLLERFAN_SECS) * 1000UL)) ? 0 : CONTROLLERFAN_SPEED;
  364. // allows digital or PWM fan output to be used (see M42 handling)
  365. WRITE(CONTROLLER_FAN_PIN, speed);
  366. analogWrite(CONTROLLER_FAN_PIN, speed);
  367. }
  368. }
  369. #endif // USE_CONTROLLER_FAN
  370. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  371. void handle_filament_runout() {
  372. if (!filament_ran_out) {
  373. filament_ran_out = true;
  374. enqueue_and_echo_commands_P(PSTR(FILAMENT_RUNOUT_SCRIPT));
  375. stepper.synchronize();
  376. }
  377. }
  378. #endif // FILAMENT_RUNOUT_SENSOR
  379. float calculate_volumetric_multiplier(const float diameter) {
  380. if (!parser.volumetric_enabled || diameter == 0) return 1.0;
  381. return 1.0 / (M_PI * sq(diameter * 0.5));
  382. }
  383. void calculate_volumetric_multipliers() {
  384. for (uint8_t i = 0; i < COUNT(filament_size); i++)
  385. volumetric_multiplier[i] = calculate_volumetric_multiplier(filament_size[i]);
  386. }
  387. void enable_all_steppers() {
  388. enable_X();
  389. enable_Y();
  390. enable_Z();
  391. enable_E0();
  392. enable_E1();
  393. enable_E2();
  394. enable_E3();
  395. enable_E4();
  396. }
  397. void disable_e_steppers() {
  398. disable_E0();
  399. disable_E1();
  400. disable_E2();
  401. disable_E3();
  402. disable_E4();
  403. }
  404. void disable_all_steppers() {
  405. disable_X();
  406. disable_Y();
  407. disable_Z();
  408. disable_e_steppers();
  409. }
  410. /**
  411. * Manage several activities:
  412. * - Check for Filament Runout
  413. * - Keep the command buffer full
  414. * - Check for maximum inactive time between commands
  415. * - Check for maximum inactive time between stepper commands
  416. * - Check if pin CHDK needs to go LOW
  417. * - Check for KILL button held down
  418. * - Check for HOME button held down
  419. * - Check if cooling fan needs to be switched on
  420. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  421. */
  422. void manage_inactivity(bool ignore_stepper_queue/*=false*/) {
  423. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  424. if ((IS_SD_PRINTING || print_job_timer.isRunning()) && (READ(FIL_RUNOUT_PIN) == FIL_RUNOUT_INVERTING))
  425. handle_filament_runout();
  426. #endif
  427. if (commands_in_queue < BUFSIZE) get_available_commands();
  428. const millis_t ms = millis();
  429. if (max_inactive_time && ELAPSED(ms, gcode.previous_cmd_ms + max_inactive_time)) {
  430. SERIAL_ERROR_START();
  431. SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
  432. kill(PSTR(MSG_KILLED));
  433. }
  434. // Prevent steppers timing-out in the middle of M600
  435. #if ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT)
  436. #define MOVE_AWAY_TEST !move_away_flag
  437. #else
  438. #define MOVE_AWAY_TEST true
  439. #endif
  440. if (MOVE_AWAY_TEST && stepper_inactive_time && ELAPSED(ms, gcode.previous_cmd_ms + stepper_inactive_time)
  441. && !ignore_stepper_queue && !planner.blocks_queued()) {
  442. #if ENABLED(DISABLE_INACTIVE_X)
  443. disable_X();
  444. #endif
  445. #if ENABLED(DISABLE_INACTIVE_Y)
  446. disable_Y();
  447. #endif
  448. #if ENABLED(DISABLE_INACTIVE_Z)
  449. disable_Z();
  450. #endif
  451. #if ENABLED(DISABLE_INACTIVE_E)
  452. disable_e_steppers();
  453. #endif
  454. #if ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(ULTRA_LCD) // Only needed with an LCD
  455. ubl.lcd_map_control = defer_return_to_status = false;
  456. #endif
  457. }
  458. #ifdef CHDK // Check if pin should be set to LOW after M240 set it to HIGH
  459. if (chdkActive && ELAPSED(ms, chdkHigh + CHDK_DELAY)) {
  460. chdkActive = false;
  461. WRITE(CHDK, LOW);
  462. }
  463. #endif
  464. #if HAS_KILL
  465. // Check if the kill button was pressed and wait just in case it was an accidental
  466. // key kill key press
  467. // -------------------------------------------------------------------------------
  468. static int killCount = 0; // make the inactivity button a bit less responsive
  469. const int KILL_DELAY = 750;
  470. if (!READ(KILL_PIN))
  471. killCount++;
  472. else if (killCount > 0)
  473. killCount--;
  474. // Exceeded threshold and we can confirm that it was not accidental
  475. // KILL the machine
  476. // ----------------------------------------------------------------
  477. if (killCount >= KILL_DELAY) {
  478. SERIAL_ERROR_START();
  479. SERIAL_ERRORLNPGM(MSG_KILL_BUTTON);
  480. kill(PSTR(MSG_KILLED));
  481. }
  482. #endif
  483. #if HAS_HOME
  484. // Check to see if we have to home, use poor man's debouncer
  485. // ---------------------------------------------------------
  486. static int homeDebounceCount = 0; // poor man's debouncing count
  487. const int HOME_DEBOUNCE_DELAY = 2500;
  488. if (!IS_SD_PRINTING && !READ(HOME_PIN)) {
  489. if (!homeDebounceCount) {
  490. enqueue_and_echo_commands_P(PSTR("G28"));
  491. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  492. }
  493. if (homeDebounceCount < HOME_DEBOUNCE_DELAY)
  494. homeDebounceCount++;
  495. else
  496. homeDebounceCount = 0;
  497. }
  498. #endif
  499. #if ENABLED(USE_CONTROLLER_FAN)
  500. controllerFan(); // Check if fan should be turned on to cool stepper drivers down
  501. #endif
  502. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  503. if (ELAPSED(ms, gcode.previous_cmd_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  504. && thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP) {
  505. #if ENABLED(SWITCHING_EXTRUDER)
  506. const bool oldstatus = E0_ENABLE_READ;
  507. enable_E0();
  508. #else // !SWITCHING_EXTRUDER
  509. bool oldstatus;
  510. switch (active_extruder) {
  511. default: oldstatus = E0_ENABLE_READ; enable_E0(); break;
  512. #if E_STEPPERS > 1
  513. case 1: oldstatus = E1_ENABLE_READ; enable_E1(); break;
  514. #if E_STEPPERS > 2
  515. case 2: oldstatus = E2_ENABLE_READ; enable_E2(); break;
  516. #if E_STEPPERS > 3
  517. case 3: oldstatus = E3_ENABLE_READ; enable_E3(); break;
  518. #if E_STEPPERS > 4
  519. case 4: oldstatus = E4_ENABLE_READ; enable_E4(); break;
  520. #endif // E_STEPPERS > 4
  521. #endif // E_STEPPERS > 3
  522. #endif // E_STEPPERS > 2
  523. #endif // E_STEPPERS > 1
  524. }
  525. #endif // !SWITCHING_EXTRUDER
  526. gcode.refresh_cmd_timeout()
  527. const float olde = current_position[E_AXIS];
  528. current_position[E_AXIS] += EXTRUDER_RUNOUT_EXTRUDE;
  529. planner.buffer_line_kinematic(current_position, MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED), active_extruder);
  530. current_position[E_AXIS] = olde;
  531. planner.set_e_position_mm(olde);
  532. stepper.synchronize();
  533. #if ENABLED(SWITCHING_EXTRUDER)
  534. E0_ENABLE_WRITE(oldstatus);
  535. #else
  536. switch (active_extruder) {
  537. case 0: E0_ENABLE_WRITE(oldstatus); break;
  538. #if E_STEPPERS > 1
  539. case 1: E1_ENABLE_WRITE(oldstatus); break;
  540. #if E_STEPPERS > 2
  541. case 2: E2_ENABLE_WRITE(oldstatus); break;
  542. #if E_STEPPERS > 3
  543. case 3: E3_ENABLE_WRITE(oldstatus); break;
  544. #if E_STEPPERS > 4
  545. case 4: E4_ENABLE_WRITE(oldstatus); break;
  546. #endif // E_STEPPERS > 4
  547. #endif // E_STEPPERS > 3
  548. #endif // E_STEPPERS > 2
  549. #endif // E_STEPPERS > 1
  550. }
  551. #endif // !SWITCHING_EXTRUDER
  552. }
  553. #endif // EXTRUDER_RUNOUT_PREVENT
  554. #if ENABLED(DUAL_X_CARRIAGE)
  555. // handle delayed move timeout
  556. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  557. // travel moves have been received so enact them
  558. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  559. set_destination_to_current();
  560. prepare_move_to_destination();
  561. }
  562. #endif
  563. #if ENABLED(TEMP_STAT_LEDS)
  564. handle_status_leds();
  565. #endif
  566. #if ENABLED(HAVE_TMC2130)
  567. tmc2130_checkOverTemp();
  568. #endif
  569. planner.check_axes_activity();
  570. }
  571. /**
  572. * Standard idle routine keeps the machine alive
  573. */
  574. void idle(
  575. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  576. bool no_stepper_sleep/*=false*/
  577. #endif
  578. ) {
  579. #if ENABLED(MAX7219_DEBUG)
  580. Max7219_idle_tasks();
  581. #endif // MAX7219_DEBUG
  582. lcd_update();
  583. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  584. gcode.host_keepalive();
  585. #endif
  586. #if ENABLED(AUTO_REPORT_TEMPERATURES) && (HAS_TEMP_HOTEND || HAS_TEMP_BED)
  587. thermalManager.auto_report_temperatures();
  588. #endif
  589. manage_inactivity(
  590. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  591. no_stepper_sleep
  592. #endif
  593. );
  594. thermalManager.manage_heater();
  595. #if ENABLED(PRINTCOUNTER)
  596. print_job_timer.tick();
  597. #endif
  598. #if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
  599. buzzer.tick();
  600. #endif
  601. #if ENABLED(I2C_POSITION_ENCODERS)
  602. if (planner.blocks_queued() &&
  603. ( (blockBufferIndexRef != planner.block_buffer_head) ||
  604. ((lastUpdateMillis + I2CPE_MIN_UPD_TIME_MS) < millis())) ) {
  605. blockBufferIndexRef = planner.block_buffer_head;
  606. I2CPEM.update();
  607. lastUpdateMillis = millis();
  608. }
  609. #endif
  610. }
  611. /**
  612. * Kill all activity and lock the machine.
  613. * After this the machine will need to be reset.
  614. */
  615. void kill(const char* lcd_msg) {
  616. SERIAL_ERROR_START();
  617. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  618. thermalManager.disable_all_heaters();
  619. disable_all_steppers();
  620. #if ENABLED(ULTRA_LCD)
  621. kill_screen(lcd_msg);
  622. #else
  623. UNUSED(lcd_msg);
  624. #endif
  625. _delay_ms(600); // Wait a short time (allows messages to get out before shutting down.
  626. cli(); // Stop interrupts
  627. _delay_ms(250); //Wait to ensure all interrupts routines stopped
  628. thermalManager.disable_all_heaters(); //turn off heaters again
  629. #ifdef ACTION_ON_KILL
  630. SERIAL_ECHOLNPGM("//action:" ACTION_ON_KILL);
  631. #endif
  632. #if HAS_POWER_SWITCH
  633. SET_INPUT(PS_ON_PIN);
  634. #endif
  635. suicide();
  636. while (1) {
  637. #if ENABLED(USE_WATCHDOG)
  638. watchdog_reset();
  639. #endif
  640. } // Wait for reset
  641. }
  642. /**
  643. * Turn off heaters and stop the print in progress
  644. * After a stop the machine may be resumed with M999
  645. */
  646. void stop() {
  647. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  648. #if ENABLED(PROBING_FANS_OFF)
  649. if (fans_paused) fans_pause(false); // put things back the way they were
  650. #endif
  651. if (IsRunning()) {
  652. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  653. SERIAL_ERROR_START();
  654. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  655. LCD_MESSAGEPGM(MSG_STOPPED);
  656. safe_delay(350); // allow enough time for messages to get out before stopping
  657. Running = false;
  658. }
  659. }
  660. /**
  661. * Marlin entry-point: Set up before the program loop
  662. * - Set up the kill pin, filament runout, power hold
  663. * - Start the serial port
  664. * - Print startup messages and diagnostics
  665. * - Get EEPROM or default settings
  666. * - Initialize managers for:
  667. * • temperature
  668. * • planner
  669. * • watchdog
  670. * • stepper
  671. * • photo pin
  672. * • servos
  673. * • LCD controller
  674. * • Digipot I2C
  675. * • Z probe sled
  676. * • status LEDs
  677. */
  678. void setup() {
  679. #if ENABLED(MAX7219_DEBUG)
  680. Max7219_init();
  681. #endif
  682. #ifdef DISABLE_JTAG
  683. // Disable JTAG on AT90USB chips to free up pins for IO
  684. MCUCR = 0x80;
  685. MCUCR = 0x80;
  686. #endif
  687. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  688. setup_filrunoutpin();
  689. #endif
  690. setup_killpin();
  691. setup_powerhold();
  692. #if HAS_STEPPER_RESET
  693. disableStepperDrivers();
  694. #endif
  695. MYSERIAL.begin(BAUDRATE);
  696. while(!MYSERIAL);
  697. SERIAL_PROTOCOLLNPGM("start");
  698. SERIAL_ECHO_START();
  699. // Check startup - does nothing if bootloader sets MCUSR to 0
  700. byte mcu = HAL_get_reset_source();
  701. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  702. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  703. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  704. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  705. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  706. HAL_clear_reset_source();
  707. #if ENABLED(USE_WATCHDOG) //reinit watchdog after HAL_get_reset_source call
  708. watchdog_init();
  709. #endif
  710. SERIAL_ECHOPGM(MSG_MARLIN);
  711. SERIAL_CHAR(' ');
  712. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  713. SERIAL_EOL();
  714. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  715. SERIAL_ECHO_START();
  716. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  717. SERIAL_ECHOPGM(STRING_DISTRIBUTION_DATE);
  718. SERIAL_ECHOLNPGM(MSG_AUTHOR STRING_CONFIG_H_AUTHOR);
  719. SERIAL_ECHO_START();
  720. SERIAL_ECHOLNPGM("Compiled: " __DATE__);
  721. #endif
  722. SERIAL_ECHO_START();
  723. SERIAL_ECHOPAIR(MSG_FREE_MEMORY, freeMemory());
  724. SERIAL_ECHOLNPAIR(MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  725. queue_setup();
  726. // Load data from EEPROM if available (or use defaults)
  727. // This also updates variables in the planner, elsewhere
  728. (void)settings.load();
  729. #if HAS_M206_COMMAND
  730. // Initialize current position based on home_offset
  731. COPY(current_position, home_offset);
  732. #else
  733. ZERO(current_position);
  734. #endif
  735. // Vital to init stepper/planner equivalent for current_position
  736. SYNC_PLAN_POSITION_KINEMATIC();
  737. thermalManager.init(); // Initialize temperature loop
  738. stepper.init(); // Initialize stepper, this enables interrupts!
  739. #if HAS_SERVOS
  740. servo_init();
  741. #endif
  742. #if HAS_PHOTOGRAPH
  743. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  744. #endif
  745. #if HAS_CASE_LIGHT
  746. case_light_on = CASE_LIGHT_DEFAULT_ON;
  747. case_light_brightness = CASE_LIGHT_DEFAULT_BRIGHTNESS;
  748. update_case_light();
  749. #endif
  750. #if ENABLED(SPINDLE_LASER_ENABLE)
  751. OUT_WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); // init spindle to off
  752. #if SPINDLE_DIR_CHANGE
  753. OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // init rotation to clockwise (M3)
  754. #endif
  755. #if ENABLED(SPINDLE_LASER_PWM) && defined(SPINDLE_LASER_PWM_PIN) && SPINDLE_LASER_PWM_PIN >= 0
  756. SET_OUTPUT(SPINDLE_LASER_PWM_PIN);
  757. analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // set to lowest speed
  758. #endif
  759. #endif
  760. #if HAS_BED_PROBE
  761. endstops.enable_z_probe(false);
  762. #endif
  763. #if ENABLED(USE_CONTROLLER_FAN)
  764. SET_OUTPUT(CONTROLLER_FAN_PIN); //Set pin used for driver cooling fan
  765. #endif
  766. #if HAS_STEPPER_RESET
  767. enableStepperDrivers();
  768. #endif
  769. #if ENABLED(DIGIPOT_I2C)
  770. digipot_i2c_init();
  771. #endif
  772. #if ENABLED(DAC_STEPPER_CURRENT)
  773. dac_init();
  774. #endif
  775. #if (ENABLED(Z_PROBE_SLED) || ENABLED(SOLENOID_PROBE)) && HAS_SOLENOID_1
  776. OUT_WRITE(SOL1_PIN, LOW); // turn it off
  777. #endif
  778. #if HAS_HOME
  779. SET_INPUT_PULLUP(HOME_PIN);
  780. #endif
  781. #if PIN_EXISTS(STAT_LED_RED)
  782. OUT_WRITE(STAT_LED_RED_PIN, LOW); // turn it off
  783. #endif
  784. #if PIN_EXISTS(STAT_LED_BLUE)
  785. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // turn it off
  786. #endif
  787. #if ENABLED(NEOPIXEL_RGBW_LED)
  788. SET_OUTPUT(NEOPIXEL_PIN);
  789. setup_neopixel();
  790. #endif
  791. #if ENABLED(RGB_LED) || ENABLED(RGBW_LED)
  792. SET_OUTPUT(RGB_LED_R_PIN);
  793. SET_OUTPUT(RGB_LED_G_PIN);
  794. SET_OUTPUT(RGB_LED_B_PIN);
  795. #if ENABLED(RGBW_LED)
  796. SET_OUTPUT(RGB_LED_W_PIN);
  797. #endif
  798. #endif
  799. #if ENABLED(MK2_MULTIPLEXER)
  800. SET_OUTPUT(E_MUX0_PIN);
  801. SET_OUTPUT(E_MUX1_PIN);
  802. SET_OUTPUT(E_MUX2_PIN);
  803. #endif
  804. #if HAS_FANMUX
  805. fanmux_init();
  806. #endif
  807. lcd_init();
  808. #ifndef CUSTOM_BOOTSCREEN_TIMEOUT
  809. #define CUSTOM_BOOTSCREEN_TIMEOUT 2500
  810. #endif
  811. #if ENABLED(SHOW_BOOTSCREEN)
  812. #if ENABLED(DOGLCD) // On DOGM the first bootscreen is already drawn
  813. #if ENABLED(SHOW_CUSTOM_BOOTSCREEN)
  814. safe_delay(CUSTOM_BOOTSCREEN_TIMEOUT); // Custom boot screen pause
  815. lcd_bootscreen(); // Show Marlin boot screen
  816. #endif
  817. safe_delay(BOOTSCREEN_TIMEOUT); // Pause
  818. #elif ENABLED(ULTRA_LCD)
  819. lcd_bootscreen();
  820. #if DISABLED(SDSUPPORT)
  821. lcd_init();
  822. #endif
  823. #endif
  824. #endif
  825. #if ENABLED(MIXING_EXTRUDER) && MIXING_VIRTUAL_TOOLS > 1
  826. // Initialize mixing to 100% color 1
  827. for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
  828. mixing_factor[i] = (i == 0) ? 1.0 : 0.0;
  829. for (uint8_t t = 0; t < MIXING_VIRTUAL_TOOLS; t++)
  830. for (uint8_t i = 0; i < MIXING_STEPPERS; i++)
  831. mixing_virtual_tool_mix[t][i] = mixing_factor[i];
  832. #endif
  833. #if ENABLED(BLTOUCH)
  834. bltouch_init();
  835. #endif
  836. #if ENABLED(I2C_POSITION_ENCODERS)
  837. I2CPEM.init();
  838. #endif
  839. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  840. i2c.onReceive(i2c_on_receive);
  841. i2c.onRequest(i2c_on_request);
  842. #endif
  843. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  844. setup_endstop_interrupts();
  845. #endif
  846. #if ENABLED(SWITCHING_EXTRUDER) && !DONT_SWITCH
  847. move_extruder_servo(0); // Initialize extruder servo
  848. #endif
  849. #if ENABLED(SWITCHING_NOZZLE)
  850. move_nozzle_servo(0); // Initialize nozzle servo
  851. #endif
  852. #if ENABLED(PARKING_EXTRUDER)
  853. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  854. pe_activate_magnet(0);
  855. pe_activate_magnet(1);
  856. #else
  857. pe_deactivate_magnet(0);
  858. pe_deactivate_magnet(1);
  859. #endif
  860. #endif
  861. }
  862. /**
  863. * The main Marlin program loop
  864. *
  865. * - Save or log commands to SD
  866. * - Process available commands (if not saving)
  867. * - Call heater manager
  868. * - Call inactivity manager
  869. * - Call endstop manager
  870. * - Call LCD update
  871. */
  872. void loop() {
  873. if (commands_in_queue < BUFSIZE) get_available_commands();
  874. #if ENABLED(SDSUPPORT)
  875. card.checkautostart(false);
  876. #endif
  877. advance_command_queue();
  878. endstops.report_state();
  879. idle();
  880. }