My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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/grbl/grbl
  28. */
  29. #include "Marlin.h"
  30. #include "core/utility.h"
  31. #include "lcd/ultralcd.h"
  32. #include "module/motion.h"
  33. #include "module/planner.h"
  34. #include "module/stepper.h"
  35. #include "module/endstops.h"
  36. #include "module/probe.h"
  37. #include "module/temperature.h"
  38. #include "sd/cardreader.h"
  39. #include "module/configuration_store.h"
  40. #include "module/printcounter.h" // PrintCounter or Stopwatch
  41. #include "feature/closedloop.h"
  42. #include "HAL/shared/Delay.h"
  43. #include "module/stepper/indirection.h"
  44. #ifdef ARDUINO
  45. #include <pins_arduino.h>
  46. #endif
  47. #include <math.h>
  48. #include "libs/nozzle.h"
  49. #include "gcode/gcode.h"
  50. #include "gcode/parser.h"
  51. #include "gcode/queue.h"
  52. #if ENABLED(TOUCH_BUTTONS)
  53. #include "feature/touch/xpt2046.h"
  54. #endif
  55. #if ENABLED(HOST_ACTION_COMMANDS)
  56. #include "feature/host_actions.h"
  57. #endif
  58. #if USE_BEEPER
  59. #include "libs/buzzer.h"
  60. #endif
  61. #if ENABLED(DIGIPOT_I2C)
  62. #include "feature/digipot/digipot.h"
  63. #endif
  64. #if ENABLED(MIXING_EXTRUDER)
  65. #include "feature/mixing.h"
  66. #endif
  67. #if ENABLED(MAX7219_DEBUG)
  68. #include "feature/Max7219_Debug_LEDs.h"
  69. #endif
  70. #if HAS_COLOR_LEDS
  71. #include "feature/leds/leds.h"
  72. #endif
  73. #if ENABLED(BLTOUCH)
  74. #include "feature/bltouch.h"
  75. #endif
  76. #if ENABLED(POLL_JOG)
  77. #include "feature/joystick.h"
  78. #endif
  79. #if HAS_SERVOS
  80. #include "module/servo.h"
  81. #endif
  82. #if ENABLED(DAC_STEPPER_CURRENT)
  83. #include "feature/dac/stepper_dac.h"
  84. #endif
  85. #if ENABLED(EXPERIMENTAL_I2CBUS)
  86. #include "feature/twibus.h"
  87. TWIBus i2c;
  88. #endif
  89. #if ENABLED(I2C_POSITION_ENCODERS)
  90. #include "feature/I2CPositionEncoder.h"
  91. #endif
  92. #if HAS_TRINAMIC && DISABLED(PS_DEFAULT_OFF)
  93. #include "feature/tmc_util.h"
  94. #endif
  95. #if HAS_CUTTER
  96. #include "feature/spindle_laser.h"
  97. #endif
  98. #if ENABLED(SDSUPPORT)
  99. CardReader card;
  100. #endif
  101. #if ENABLED(G38_PROBE_TARGET)
  102. uint8_t G38_move; // = 0
  103. bool G38_did_trigger; // = false
  104. #endif
  105. #if ENABLED(DELTA)
  106. #include "module/delta.h"
  107. #elif IS_SCARA
  108. #include "module/scara.h"
  109. #endif
  110. #if HAS_LEVELING
  111. #include "feature/bedlevel/bedlevel.h"
  112. #endif
  113. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  114. #include "feature/pause.h"
  115. #endif
  116. #if ENABLED(POWER_LOSS_RECOVERY)
  117. #include "feature/power_loss_recovery.h"
  118. #endif
  119. #if HAS_FILAMENT_SENSOR
  120. #include "feature/runout.h"
  121. #endif
  122. #if ENABLED(TEMP_STAT_LEDS)
  123. #include "feature/leds/tempstat.h"
  124. #endif
  125. #if HAS_CASE_LIGHT
  126. #include "feature/caselight.h"
  127. #endif
  128. #if HAS_FANMUX
  129. #include "feature/fanmux.h"
  130. #endif
  131. #if DO_SWITCH_EXTRUDER || ANY(SWITCHING_NOZZLE, PARKING_EXTRUDER, MAGNETIC_PARKING_EXTRUDER, ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  132. #include "module/tool_change.h"
  133. #endif
  134. #if ENABLED(USE_CONTROLLER_FAN)
  135. #include "feature/controllerfan.h"
  136. #endif
  137. #if ENABLED(PRUSA_MMU2)
  138. #include "feature/prusa_MMU2/mmu2.h"
  139. #endif
  140. #if HAS_DRIVER(L6470)
  141. #include "libs/L6470/L6470_Marlin.h"
  142. #endif
  143. bool Running = true;
  144. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  145. bool wait_for_heatup = true;
  146. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  147. #if HAS_RESUME_CONTINUE
  148. bool wait_for_user; // = false;
  149. #endif
  150. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  151. bool suspend_auto_report; // = false
  152. #endif
  153. // Inactivity shutdown
  154. millis_t max_inactive_time, // = 0
  155. stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
  156. #if PIN_EXISTS(CHDK)
  157. extern millis_t chdk_timeout;
  158. #endif
  159. #if ENABLED(I2C_POSITION_ENCODERS)
  160. I2CPositionEncodersMgr I2CPEM;
  161. #endif
  162. /**
  163. * ***************************************************************************
  164. * ******************************** FUNCTIONS ********************************
  165. * ***************************************************************************
  166. */
  167. void setup_killpin() {
  168. #if HAS_KILL
  169. SET_INPUT_PULLUP(KILL_PIN);
  170. #endif
  171. }
  172. void setup_powerhold() {
  173. #if HAS_SUICIDE
  174. OUT_WRITE(SUICIDE_PIN, HIGH);
  175. #endif
  176. #if HAS_POWER_SWITCH
  177. #if ENABLED(PS_DEFAULT_OFF)
  178. powersupply_on = true; PSU_OFF();
  179. #else
  180. powersupply_on = false; PSU_ON();
  181. #endif
  182. #endif
  183. }
  184. /**
  185. * Stepper Reset (RigidBoard, et.al.)
  186. */
  187. #if HAS_STEPPER_RESET
  188. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  189. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  190. #endif
  191. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  192. void i2c_on_receive(int bytes) { // just echo all bytes received to serial
  193. i2c.receive(bytes);
  194. }
  195. void i2c_on_request() { // just send dummy data for now
  196. i2c.reply("Hello World!\n");
  197. }
  198. #endif
  199. /**
  200. * Sensitive pin test for M42, M226
  201. */
  202. #include "pins/sensitive_pins.h"
  203. bool pin_is_protected(const pin_t pin) {
  204. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  205. for (uint8_t i = 0; i < COUNT(sensitive_pins); i++) {
  206. pin_t sensitive_pin;
  207. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  208. if (pin == sensitive_pin) return true;
  209. }
  210. return false;
  211. }
  212. void protected_pin_err() {
  213. SERIAL_ERROR_MSG(MSG_ERR_PROTECTED_PIN);
  214. }
  215. void quickstop_stepper() {
  216. planner.quick_stop();
  217. planner.synchronize();
  218. set_current_from_steppers_for_axis(ALL_AXES);
  219. sync_plan_position();
  220. }
  221. void enable_e_steppers() {
  222. enable_E0(); enable_E1(); enable_E2(); enable_E3(); enable_E4(); enable_E5();
  223. }
  224. void enable_all_steppers() {
  225. #if ENABLED(AUTO_POWER_CONTROL)
  226. powerManager.power_on();
  227. #endif
  228. enable_X();
  229. enable_Y();
  230. enable_Z();
  231. enable_e_steppers();
  232. }
  233. void disable_e_steppers() {
  234. disable_E0(); disable_E1(); disable_E2(); disable_E3(); disable_E4(); disable_E5();
  235. }
  236. void disable_e_stepper(const uint8_t e) {
  237. switch (e) {
  238. case 0: disable_E0(); break;
  239. case 1: disable_E1(); break;
  240. case 2: disable_E2(); break;
  241. case 3: disable_E3(); break;
  242. case 4: disable_E4(); break;
  243. case 5: disable_E5(); break;
  244. }
  245. }
  246. void disable_all_steppers() {
  247. disable_X();
  248. disable_Y();
  249. disable_Z();
  250. disable_e_steppers();
  251. }
  252. #if ENABLED(G29_RETRY_AND_RECOVER)
  253. void event_probe_failure() {
  254. #ifdef ACTION_ON_G29_FAILURE
  255. host_action(PSTR(ACTION_ON_G29_FAILURE));
  256. #endif
  257. #ifdef G29_FAILURE_COMMANDS
  258. gcode.process_subcommands_now_P(PSTR(G29_FAILURE_COMMANDS));
  259. #endif
  260. #if ENABLED(G29_HALT_ON_FAILURE)
  261. #ifdef ACTION_ON_CANCEL
  262. host_action_cancel();
  263. #endif
  264. kill(GET_TEXT(MSG_LCD_PROBING_FAILED));
  265. #endif
  266. }
  267. void event_probe_recover() {
  268. #if ENABLED(HOST_PROMPT_SUPPORT)
  269. host_prompt_do(PROMPT_INFO, PSTR("G29 Retrying"), PSTR("Dismiss"));
  270. #endif
  271. #ifdef ACTION_ON_G29_RECOVER
  272. host_action(PSTR(ACTION_ON_G29_RECOVER));
  273. #endif
  274. #ifdef G29_RECOVER_COMMANDS
  275. gcode.process_subcommands_now_P(PSTR(G29_RECOVER_COMMANDS));
  276. #endif
  277. }
  278. #endif
  279. /**
  280. * Printing is active when the print job timer is running
  281. */
  282. bool printingIsActive() {
  283. return print_job_timer.isRunning() || IS_SD_PRINTING();
  284. }
  285. /**
  286. * Printing is paused according to SD or host indicators
  287. */
  288. bool printingIsPaused() {
  289. return print_job_timer.isPaused() || IS_SD_PAUSED();
  290. }
  291. /**
  292. * Manage several activities:
  293. * - Check for Filament Runout
  294. * - Keep the command buffer full
  295. * - Check for maximum inactive time between commands
  296. * - Check for maximum inactive time between stepper commands
  297. * - Check if CHDK_PIN needs to go LOW
  298. * - Check for KILL button held down
  299. * - Check for HOME button held down
  300. * - Check if cooling fan needs to be switched on
  301. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  302. * - Pulse FET_SAFETY_PIN if it exists
  303. */
  304. void manage_inactivity(const bool ignore_stepper_queue/*=false*/) {
  305. #if HAS_FILAMENT_SENSOR
  306. runout.run();
  307. #endif
  308. if (queue.length < BUFSIZE) queue.get_available_commands();
  309. const millis_t ms = millis();
  310. if (max_inactive_time && ELAPSED(ms, gcode.previous_move_ms + max_inactive_time)) {
  311. SERIAL_ERROR_START();
  312. SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
  313. kill();
  314. }
  315. // Prevent steppers timing-out in the middle of M600
  316. #if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
  317. #define MOVE_AWAY_TEST !did_pause_print
  318. #else
  319. #define MOVE_AWAY_TEST true
  320. #endif
  321. if (stepper_inactive_time) {
  322. static bool already_shutdown_steppers; // = false
  323. if (planner.has_blocks_queued())
  324. gcode.reset_stepper_timeout();
  325. else if (MOVE_AWAY_TEST && !ignore_stepper_queue && ELAPSED(ms, gcode.previous_move_ms + stepper_inactive_time)) {
  326. if (!already_shutdown_steppers) {
  327. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  328. #if ENABLED(DISABLE_INACTIVE_X)
  329. disable_X();
  330. #endif
  331. #if ENABLED(DISABLE_INACTIVE_Y)
  332. disable_Y();
  333. #endif
  334. #if ENABLED(DISABLE_INACTIVE_Z)
  335. disable_Z();
  336. #endif
  337. #if ENABLED(DISABLE_INACTIVE_E)
  338. disable_e_steppers();
  339. #endif
  340. #if HAS_LCD_MENU && ENABLED(AUTO_BED_LEVELING_UBL)
  341. if (ubl.lcd_map_control) {
  342. ubl.lcd_map_control = false;
  343. ui.defer_status_screen(false);
  344. }
  345. #endif
  346. }
  347. }
  348. else
  349. already_shutdown_steppers = false;
  350. }
  351. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  352. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  353. chdk_timeout = 0;
  354. WRITE(CHDK_PIN, LOW);
  355. }
  356. #endif
  357. #if HAS_KILL
  358. // Check if the kill button was pressed and wait just in case it was an accidental
  359. // key kill key press
  360. // -------------------------------------------------------------------------------
  361. static int killCount = 0; // make the inactivity button a bit less responsive
  362. const int KILL_DELAY = 750;
  363. if (!READ(KILL_PIN))
  364. killCount++;
  365. else if (killCount > 0)
  366. killCount--;
  367. // Exceeded threshold and we can confirm that it was not accidental
  368. // KILL the machine
  369. // ----------------------------------------------------------------
  370. if (killCount >= KILL_DELAY) {
  371. SERIAL_ERROR_MSG(MSG_KILL_BUTTON);
  372. kill();
  373. }
  374. #endif
  375. #if HAS_HOME
  376. // Handle a standalone HOME button
  377. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  378. static millis_t next_home_key_ms; // = 0
  379. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  380. const millis_t ms = millis();
  381. if (ELAPSED(ms, next_home_key_ms)) {
  382. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  383. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  384. queue.enqueue_now_P(PSTR("G28"));
  385. }
  386. }
  387. #endif
  388. #if ENABLED(USE_CONTROLLER_FAN)
  389. controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
  390. #endif
  391. #if ENABLED(AUTO_POWER_CONTROL)
  392. powerManager.check();
  393. #endif
  394. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  395. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  396. && ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
  397. && !planner.has_blocks_queued()
  398. ) {
  399. #if ENABLED(SWITCHING_EXTRUDER)
  400. bool oldstatus;
  401. switch (active_extruder) {
  402. default: oldstatus = E0_ENABLE_READ(); enable_E0(); break;
  403. #if E_STEPPERS > 1
  404. case 2: case 3: oldstatus = E1_ENABLE_READ(); enable_E1(); break;
  405. #if E_STEPPERS > 2
  406. case 4: case 5: oldstatus = E2_ENABLE_READ(); enable_E2(); break;
  407. #endif // E_STEPPERS > 2
  408. #endif // E_STEPPERS > 1
  409. }
  410. #else // !SWITCHING_EXTRUDER
  411. bool oldstatus;
  412. switch (active_extruder) {
  413. default: oldstatus = E0_ENABLE_READ(); enable_E0(); break;
  414. #if E_STEPPERS > 1
  415. case 1: oldstatus = E1_ENABLE_READ(); enable_E1(); break;
  416. #if E_STEPPERS > 2
  417. case 2: oldstatus = E2_ENABLE_READ(); enable_E2(); break;
  418. #if E_STEPPERS > 3
  419. case 3: oldstatus = E3_ENABLE_READ(); enable_E3(); break;
  420. #if E_STEPPERS > 4
  421. case 4: oldstatus = E4_ENABLE_READ(); enable_E4(); break;
  422. #if E_STEPPERS > 5
  423. case 5: oldstatus = E5_ENABLE_READ(); enable_E5(); break;
  424. #endif // E_STEPPERS > 5
  425. #endif // E_STEPPERS > 4
  426. #endif // E_STEPPERS > 3
  427. #endif // E_STEPPERS > 2
  428. #endif // E_STEPPERS > 1
  429. }
  430. #endif // !SWITCHING_EXTRUDER
  431. const float olde = current_position.e;
  432. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  433. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  434. current_position.e = olde;
  435. planner.set_e_position_mm(olde);
  436. planner.synchronize();
  437. #if ENABLED(SWITCHING_EXTRUDER)
  438. switch (active_extruder) {
  439. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  440. #if E_STEPPERS > 1
  441. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  442. #if E_STEPPERS > 2
  443. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  444. #endif // E_STEPPERS > 2
  445. #endif // E_STEPPERS > 1
  446. }
  447. #else // !SWITCHING_EXTRUDER
  448. switch (active_extruder) {
  449. case 0: E0_ENABLE_WRITE(oldstatus); break;
  450. #if E_STEPPERS > 1
  451. case 1: E1_ENABLE_WRITE(oldstatus); break;
  452. #if E_STEPPERS > 2
  453. case 2: E2_ENABLE_WRITE(oldstatus); break;
  454. #if E_STEPPERS > 3
  455. case 3: E3_ENABLE_WRITE(oldstatus); break;
  456. #if E_STEPPERS > 4
  457. case 4: E4_ENABLE_WRITE(oldstatus); break;
  458. #if E_STEPPERS > 5
  459. case 5: E5_ENABLE_WRITE(oldstatus); break;
  460. #endif // E_STEPPERS > 5
  461. #endif // E_STEPPERS > 4
  462. #endif // E_STEPPERS > 3
  463. #endif // E_STEPPERS > 2
  464. #endif // E_STEPPERS > 1
  465. }
  466. #endif // !SWITCHING_EXTRUDER
  467. gcode.reset_stepper_timeout();
  468. }
  469. #endif // EXTRUDER_RUNOUT_PREVENT
  470. #if ENABLED(DUAL_X_CARRIAGE)
  471. // handle delayed move timeout
  472. if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
  473. // travel moves have been received so enact them
  474. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  475. destination = current_position;
  476. prepare_move_to_destination();
  477. }
  478. #endif
  479. #if ENABLED(TEMP_STAT_LEDS)
  480. handle_status_leds();
  481. #endif
  482. #if ENABLED(MONITOR_DRIVER_STATUS)
  483. monitor_tmc_driver();
  484. #endif
  485. #if ENABLED(MONITOR_L6470_DRIVER_STATUS)
  486. L6470.monitor_driver();
  487. #endif
  488. // Limit check_axes_activity frequency to 10Hz
  489. static millis_t next_check_axes_ms = 0;
  490. if (ELAPSED(ms, next_check_axes_ms)) {
  491. planner.check_axes_activity();
  492. next_check_axes_ms = ms + 100UL;
  493. }
  494. #if PIN_EXISTS(FET_SAFETY)
  495. static millis_t FET_next;
  496. if (ELAPSED(ms, FET_next)) {
  497. FET_next = ms + FET_SAFETY_DELAY; // 2uS pulse every FET_SAFETY_DELAY mS
  498. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  499. DELAY_US(2);
  500. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  501. }
  502. #endif
  503. }
  504. /**
  505. * Standard idle routine keeps the machine alive
  506. */
  507. void idle(
  508. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  509. bool no_stepper_sleep/*=false*/
  510. #endif
  511. ) {
  512. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  513. recovery.outage();
  514. #endif
  515. #if ENABLED(SPI_ENDSTOPS)
  516. if (endstops.tmc_spi_homing.any
  517. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  518. && ELAPSED(millis(), sg_guard_period)
  519. #endif
  520. ) {
  521. for (uint8_t i = 4; i--;) // Read SGT 4 times per idle loop
  522. if (endstops.tmc_spi_homing_check()) break;
  523. }
  524. #endif
  525. #if ENABLED(MAX7219_DEBUG)
  526. max7219.idle_tasks();
  527. #endif
  528. ui.update();
  529. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  530. gcode.host_keepalive();
  531. #endif
  532. manage_inactivity(
  533. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  534. no_stepper_sleep
  535. #endif
  536. );
  537. thermalManager.manage_heater();
  538. #if ENABLED(PRINTCOUNTER)
  539. print_job_timer.tick();
  540. #endif
  541. #if USE_BEEPER
  542. buzzer.tick();
  543. #endif
  544. #if ENABLED(I2C_POSITION_ENCODERS)
  545. static millis_t i2cpem_next_update_ms;
  546. if (planner.has_blocks_queued()) {
  547. const millis_t ms = millis();
  548. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  549. I2CPEM.update();
  550. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  551. }
  552. }
  553. #endif
  554. #ifdef HAL_IDLETASK
  555. HAL_idletask();
  556. #endif
  557. #if HAS_AUTO_REPORTING
  558. if (!suspend_auto_report) {
  559. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  560. thermalManager.auto_report_temperatures();
  561. #endif
  562. #if ENABLED(AUTO_REPORT_SD_STATUS)
  563. card.auto_report_sd_status();
  564. #endif
  565. }
  566. #endif
  567. #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
  568. Sd2Card::idle();
  569. #endif
  570. #if ENABLED(PRUSA_MMU2)
  571. mmu2.mmu_loop();
  572. #endif
  573. #if ENABLED(POLL_JOG)
  574. joystick.inject_jog_moves();
  575. #endif
  576. }
  577. /**
  578. * Kill all activity and lock the machine.
  579. * After this the machine will need to be reset.
  580. */
  581. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  582. thermalManager.disable_all_heaters();
  583. SERIAL_ERROR_MSG(MSG_ERR_KILLED);
  584. #if HAS_DISPLAY
  585. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component);
  586. #else
  587. UNUSED(lcd_error);
  588. UNUSED(lcd_component);
  589. #endif
  590. #ifdef ACTION_ON_KILL
  591. host_action_kill();
  592. #endif
  593. minkill(steppers_off);
  594. }
  595. void minkill(const bool steppers_off/*=false*/) {
  596. // Wait a short time (allows messages to get out before shutting down.
  597. for (int i = 1000; i--;) DELAY_US(600);
  598. cli(); // Stop interrupts
  599. // Wait to ensure all interrupts stopped
  600. for (int i = 1000; i--;) DELAY_US(250);
  601. // Reiterate heaters off
  602. thermalManager.disable_all_heaters();
  603. // Power off all steppers (for M112) or just the E steppers
  604. steppers_off ? disable_all_steppers() : disable_e_steppers();
  605. #if HAS_POWER_SWITCH
  606. PSU_OFF();
  607. #endif
  608. #if HAS_SUICIDE
  609. suicide();
  610. #endif
  611. #if HAS_KILL
  612. // Wait for kill to be released
  613. while (!READ(KILL_PIN)) watchdog_refresh();
  614. // Wait for kill to be pressed
  615. while (READ(KILL_PIN)) watchdog_refresh();
  616. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  617. resetFunc(); // Jump to address 0
  618. #else // !HAS_KILL
  619. for (;;) watchdog_refresh(); // Wait for reset
  620. #endif // !HAS_KILL
  621. }
  622. /**
  623. * Turn off heaters and stop the print in progress
  624. * After a stop the machine may be resumed with M999
  625. */
  626. void stop() {
  627. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  628. print_job_timer.stop();
  629. #if ENABLED(PROBING_FANS_OFF)
  630. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  631. #endif
  632. if (IsRunning()) {
  633. queue.stop();
  634. SERIAL_ERROR_MSG(MSG_ERR_STOPPED);
  635. LCD_MESSAGEPGM(MSG_STOPPED);
  636. safe_delay(350); // allow enough time for messages to get out before stopping
  637. Running = false;
  638. }
  639. }
  640. /**
  641. * Marlin entry-point: Set up before the program loop
  642. * - Set up the kill pin, filament runout, power hold
  643. * - Start the serial port
  644. * - Print startup messages and diagnostics
  645. * - Get EEPROM or default settings
  646. * - Initialize managers for:
  647. * • temperature
  648. * • planner
  649. * • watchdog
  650. * • stepper
  651. * • photo pin
  652. * • servos
  653. * • LCD controller
  654. * • Digipot I2C
  655. * • Z probe sled
  656. * • status LEDs
  657. */
  658. void setup() {
  659. HAL_init();
  660. #if HAS_DRIVER(L6470)
  661. L6470.init(); // setup SPI and then init chips
  662. #endif
  663. #if ENABLED(MAX7219_DEBUG)
  664. max7219.init();
  665. #endif
  666. #if ENABLED(DISABLE_DEBUG)
  667. // Disable any hardware debug to free up pins for IO
  668. #ifdef JTAGSWD_DISABLE
  669. JTAGSWD_DISABLE();
  670. #elif defined(JTAG_DISABLE)
  671. JTAG_DISABLE();
  672. #else
  673. #error "DISABLE_DEBUG is not supported for the selected MCU/Board"
  674. #endif
  675. #elif ENABLED(DISABLE_JTAG)
  676. // Disable JTAG to free up pins for IO
  677. #ifdef JTAG_DISABLE
  678. JTAG_DISABLE();
  679. #else
  680. #error "DISABLE_JTAG is not supported for the selected MCU/Board"
  681. #endif
  682. #endif
  683. #if HAS_FILAMENT_SENSOR
  684. runout.setup();
  685. #endif
  686. #if ENABLED(POWER_LOSS_RECOVERY)
  687. recovery.setup();
  688. #endif
  689. setup_killpin();
  690. #if HAS_TMC220x
  691. tmc_serial_begin();
  692. #endif
  693. setup_powerhold();
  694. #if HAS_STEPPER_RESET
  695. disableStepperDrivers();
  696. #endif
  697. #if NUM_SERIAL > 0
  698. MYSERIAL0.begin(BAUDRATE);
  699. #if NUM_SERIAL > 1
  700. MYSERIAL1.begin(BAUDRATE);
  701. #endif
  702. #endif
  703. #if NUM_SERIAL > 0
  704. uint32_t serial_connect_timeout = millis() + 1000UL;
  705. while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  706. #if NUM_SERIAL > 1
  707. serial_connect_timeout = millis() + 1000UL;
  708. while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  709. #endif
  710. #endif
  711. SERIAL_ECHOLNPGM("start");
  712. SERIAL_ECHO_START();
  713. #if TMC_HAS_SPI
  714. #if DISABLED(TMC_USE_SW_SPI)
  715. SPI.begin();
  716. #endif
  717. tmc_init_cs_pins();
  718. #endif
  719. #ifdef BOARD_INIT
  720. BOARD_INIT();
  721. #endif
  722. // Check startup - does nothing if bootloader sets MCUSR to 0
  723. byte mcu = HAL_get_reset_source();
  724. if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  725. if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  726. if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  727. if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  728. if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  729. HAL_clear_reset_source();
  730. SERIAL_ECHOPGM(MSG_MARLIN);
  731. SERIAL_CHAR(' ');
  732. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  733. SERIAL_EOL();
  734. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  735. SERIAL_ECHO_MSG(
  736. MSG_CONFIGURATION_VER
  737. STRING_DISTRIBUTION_DATE
  738. MSG_AUTHOR STRING_CONFIG_H_AUTHOR
  739. );
  740. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  741. #endif
  742. SERIAL_ECHO_START();
  743. SERIAL_ECHOLNPAIR(MSG_FREE_MEMORY, freeMemory(), MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  744. // UI must be initialized before EEPROM
  745. // (because EEPROM code calls the UI).
  746. // Set up LEDs early
  747. #if HAS_COLOR_LEDS
  748. leds.setup();
  749. #endif
  750. ui.init();
  751. ui.reset_status();
  752. #if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
  753. ui.show_bootscreen();
  754. #endif
  755. #if ENABLED(SDSUPPORT)
  756. card.mount(); // Mount the SD card before settings.first_load
  757. #endif
  758. // Load data from EEPROM if available (or use defaults)
  759. // This also updates variables in the planner, elsewhere
  760. settings.first_load();
  761. #if ENABLED(TOUCH_BUTTONS)
  762. touch.init();
  763. #endif
  764. #if HAS_M206_COMMAND
  765. // Initialize current position based on home_offset
  766. current_position += home_offset;
  767. #endif
  768. // Vital to init stepper/planner equivalent for current_position
  769. sync_plan_position();
  770. thermalManager.init(); // Initialize temperature loop
  771. print_job_timer.init(); // Initial setup of print job timer
  772. endstops.init(); // Init endstops and pullups
  773. stepper.init(); // Init stepper. This enables interrupts!
  774. #if HAS_SERVOS
  775. servo_init();
  776. #endif
  777. #if HAS_Z_SERVO_PROBE
  778. servo_probe_init();
  779. #endif
  780. #if HAS_PHOTOGRAPH
  781. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  782. #endif
  783. #if HAS_CUTTER
  784. cutter.init();
  785. #endif
  786. #if ENABLED(COOLANT_MIST)
  787. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  788. #endif
  789. #if ENABLED(COOLANT_FLOOD)
  790. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  791. #endif
  792. #if HAS_BED_PROBE
  793. endstops.enable_z_probe(false);
  794. #endif
  795. #if ENABLED(USE_CONTROLLER_FAN)
  796. SET_OUTPUT(CONTROLLER_FAN_PIN);
  797. #endif
  798. #if HAS_STEPPER_RESET
  799. enableStepperDrivers();
  800. #endif
  801. #if ENABLED(DIGIPOT_I2C)
  802. digipot_i2c_init();
  803. #endif
  804. #if ENABLED(DAC_STEPPER_CURRENT)
  805. dac_init();
  806. #endif
  807. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  808. OUT_WRITE(SOL1_PIN, LOW); // OFF
  809. #endif
  810. #if HAS_HOME
  811. SET_INPUT_PULLUP(HOME_PIN);
  812. #endif
  813. #if PIN_EXISTS(STAT_LED_RED)
  814. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  815. #endif
  816. #if PIN_EXISTS(STAT_LED_BLUE)
  817. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  818. #endif
  819. #if HAS_CASE_LIGHT
  820. #if DISABLED(CASE_LIGHT_USE_NEOPIXEL)
  821. if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN);
  822. #endif
  823. update_case_light();
  824. #endif
  825. #if ENABLED(MK2_MULTIPLEXER)
  826. SET_OUTPUT(E_MUX0_PIN);
  827. SET_OUTPUT(E_MUX1_PIN);
  828. SET_OUTPUT(E_MUX2_PIN);
  829. #endif
  830. #if HAS_FANMUX
  831. fanmux_init();
  832. #endif
  833. #if ENABLED(MIXING_EXTRUDER)
  834. mixer.init();
  835. #endif
  836. #if ENABLED(BLTOUCH)
  837. bltouch.init(/*set_voltage=*/true);
  838. #endif
  839. #if ENABLED(I2C_POSITION_ENCODERS)
  840. I2CPEM.init();
  841. #endif
  842. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  843. i2c.onReceive(i2c_on_receive);
  844. i2c.onRequest(i2c_on_request);
  845. #endif
  846. #if DO_SWITCH_EXTRUDER
  847. move_extruder_servo(0); // Initialize extruder servo
  848. #endif
  849. #if ENABLED(SWITCHING_NOZZLE)
  850. // Initialize nozzle servo(s)
  851. #if SWITCHING_NOZZLE_TWO_SERVOS
  852. lower_nozzle(0);
  853. raise_nozzle(1);
  854. #else
  855. move_nozzle_servo(0);
  856. #endif
  857. #endif
  858. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  859. mpe_settings_init();
  860. #endif
  861. #if ENABLED(PARKING_EXTRUDER)
  862. pe_solenoid_init();
  863. #endif
  864. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  865. est_init();
  866. #endif
  867. #if ENABLED(POWER_LOSS_RECOVERY)
  868. recovery.check();
  869. #endif
  870. #if ENABLED(USE_WATCHDOG)
  871. watchdog_init(); // Reinit watchdog after HAL_get_reset_source call
  872. #endif
  873. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  874. init_closedloop();
  875. #endif
  876. #ifdef STARTUP_COMMANDS
  877. queue.inject_P(PSTR(STARTUP_COMMANDS));
  878. #endif
  879. #if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
  880. card.beginautostart();
  881. #endif
  882. #if ENABLED(HOST_PROMPT_SUPPORT)
  883. host_action_prompt_end();
  884. #endif
  885. #if HAS_TRINAMIC && DISABLED(PS_DEFAULT_OFF)
  886. test_tmc_connection(true, true, true, true);
  887. #endif
  888. #if ENABLED(PRUSA_MMU2)
  889. mmu2.init();
  890. #endif
  891. }
  892. /**
  893. * The main Marlin program loop
  894. *
  895. * - Save or log commands to SD
  896. * - Process available commands (if not saving)
  897. * - Call endstop manager
  898. * - Call inactivity manager
  899. */
  900. void loop() {
  901. for (;;) {
  902. idle(); // Do an idle first so boot is slightly faster
  903. #if ENABLED(SDSUPPORT)
  904. card.checkautostart();
  905. if (card.flag.abort_sd_printing) {
  906. card.stopSDPrint(
  907. #if SD_RESORT
  908. true
  909. #endif
  910. );
  911. queue.clear();
  912. quickstop_stepper();
  913. print_job_timer.stop();
  914. #if DISABLED(SD_ABORT_NO_COOLDOWN)
  915. thermalManager.disable_all_heaters();
  916. #endif
  917. thermalManager.zero_fan_speeds();
  918. wait_for_heatup = false;
  919. #if ENABLED(POWER_LOSS_RECOVERY)
  920. card.removeJobRecoveryFile();
  921. #endif
  922. #ifdef EVENT_GCODE_SD_STOP
  923. queue.inject_P(PSTR(EVENT_GCODE_SD_STOP));
  924. #endif
  925. }
  926. #endif // SDSUPPORT
  927. queue.advance();
  928. endstops.event_handler();
  929. }
  930. }