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

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