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

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