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

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