-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
Copy pathMarlinCore.cpp
1706 lines (1420 loc) · 47.7 KB
/
MarlinCore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/**
* About Marlin
*
* This firmware is a mashup between Sprinter and grbl.
* - https://github.com/kliment/Sprinter
* - https://github.com/grbl/grbl
*/
#include "MarlinCore.h"
#include "HAL/shared/Delay.h"
#include "HAL/shared/esp_wifi.h"
#include "HAL/shared/cpu_exception/exception_hook.h"
#if ENABLED(WIFISUPPORT)
#include "HAL/shared/esp_wifi.h"
#endif
#ifdef ARDUINO
#include <pins_arduino.h>
#endif
#include <math.h>
#include "module/endstops.h"
#include "module/motion.h"
#include "module/planner.h"
#include "module/printcounter.h" // PrintCounter or Stopwatch
#include "module/settings.h"
#include "module/stepper.h"
#include "module/temperature.h"
#if ENABLED(FT_MOTION)
#include "module/ft_motion.h"
#endif
#include "gcode/gcode.h"
#include "gcode/parser.h"
#include "gcode/queue.h"
#include "feature/pause.h"
#include "sd/cardreader.h"
#include "lcd/marlinui.h"
#if HAS_TOUCH_BUTTONS
#include "lcd/touch/touch_buttons.h"
#endif
#if HAS_TFT_LVGL_UI
#include "lcd/extui/mks_ui/tft_lvgl_configuration.h"
#include "lcd/extui/mks_ui/draw_ui.h"
#include "lcd/extui/mks_ui/mks_hardware.h"
#include <lvgl.h>
#endif
#if HAS_DWIN_E3V2
#include "lcd/e3v2/common/encoder.h"
#if ENABLED(DWIN_CREALITY_LCD)
#include "lcd/e3v2/creality/dwin.h"
#elif ENABLED(DWIN_CREALITY_LCD_JYERSUI)
#include "lcd/e3v2/jyersui/dwin.h"
#endif
#endif
#if HAS_ETHERNET
#include "feature/ethernet.h"
#endif
#if ENABLED(IIC_BL24CXX_EEPROM)
#include "libs/BL24CXX.h"
#endif
#if ENABLED(DIRECT_STEPPING)
#include "feature/direct_stepping.h"
#endif
#if ENABLED(HOST_ACTION_COMMANDS)
#include "feature/host_actions.h"
#endif
#if HAS_BEEPER
#include "libs/buzzer.h"
#endif
#if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
#include "feature/closedloop.h"
#endif
#if HAS_MOTOR_CURRENT_I2C
#include "feature/digipot/digipot.h"
#endif
#if ENABLED(MIXING_EXTRUDER)
#include "feature/mixing.h"
#endif
#if ENABLED(MAX7219_DEBUG)
#include "feature/max7219.h"
#endif
#if HAS_COLOR_LEDS
#include "feature/leds/leds.h"
#endif
#if ENABLED(BLTOUCH)
#include "feature/bltouch.h"
#endif
#if ENABLED(BD_SENSOR)
#include "feature/bedlevel/bdl/bdl.h"
#endif
#if ENABLED(POLL_JOG)
#include "feature/joystick.h"
#endif
#if HAS_SERVOS
#include "module/servo.h"
#endif
#if HAS_MOTOR_CURRENT_DAC
#include "feature/dac/stepper_dac.h"
#endif
#if ENABLED(EXPERIMENTAL_I2CBUS)
#include "feature/twibus.h"
#endif
#if ENABLED(I2C_POSITION_ENCODERS)
#include "feature/encoder_i2c.h"
#endif
#if (HAS_TRINAMIC_CONFIG || HAS_TMC_SPI) && DISABLED(PSU_DEFAULT_OFF)
#include "feature/tmc_util.h"
#endif
#if HAS_CUTTER
#include "feature/spindle_laser.h"
#endif
#if HAS_MEDIA
CardReader card;
#endif
#if ENABLED(G38_PROBE_TARGET)
uint8_t G38_move; // = 0
bool G38_did_trigger; // = false
#endif
#if ENABLED(DELTA)
#include "module/delta.h"
#elif ENABLED(POLARGRAPH)
#include "module/polargraph.h"
#elif IS_SCARA
#include "module/scara.h"
#elif ENABLED(POLAR)
#include "module/polar.h"
#endif
#if HAS_LEVELING
#include "feature/bedlevel/bedlevel.h"
#endif
#if ENABLED(GCODE_REPEAT_MARKERS)
#include "feature/repeat.h"
#endif
#if ENABLED(POWER_LOSS_RECOVERY)
#include "feature/powerloss.h"
#endif
#if ENABLED(CANCEL_OBJECTS)
#include "feature/cancel_object.h"
#endif
#if HAS_FILAMENT_SENSOR
#include "feature/runout.h"
#endif
#if ANY(PROBE_TARE, HAS_Z_SERVO_PROBE)
#include "module/probe.h"
#endif
#if ENABLED(HOTEND_IDLE_TIMEOUT)
#include "feature/hotend_idle.h"
#endif
#if ENABLED(TEMP_STAT_LEDS)
#include "feature/leds/tempstat.h"
#endif
#if ENABLED(CASE_LIGHT_ENABLE)
#include "feature/caselight.h"
#endif
#if HAS_FANMUX
#include "feature/fanmux.h"
#endif
#if HAS_TOOLCHANGE
#include "module/tool_change.h"
#endif
#if HAS_FANCHECK
#include "feature/fancheck.h"
#endif
#if ENABLED(USE_CONTROLLER_FAN)
#include "feature/controllerfan.h"
#endif
#if HAS_PRUSA_MMU1
#include "feature/mmu/mmu.h"
#endif
#if HAS_PRUSA_MMU2
#include "feature/mmu/mmu2.h"
#endif
#if ENABLED(PASSWORD_FEATURE)
#include "feature/password/password.h"
#endif
#if DGUS_LCD_UI_MKS
#include "lcd/extui/dgus/DGUSScreenHandler.h"
#endif
#if HAS_DRIVER_SAFE_POWER_PROTECT
#include "feature/stepper_driver_safety.h"
#endif
#if ENABLED(PSU_CONTROL)
#include "feature/power.h"
#endif
#if ENABLED(EASYTHREED_UI)
#include "feature/easythreed_ui.h"
#endif
#if ENABLED(MARLIN_TEST_BUILD)
#include "tests/marlin_tests.h"
#endif
#if HAS_RS485_SERIAL
#include "feature/rs485.h"
#endif
PGMSTR(M112_KILL_STR, "M112 Shutdown");
MarlinState marlin_state = MarlinState::MF_INITIALIZING;
// For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
bool wait_for_heatup = false;
// For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
#if HAS_RESUME_CONTINUE
bool wait_for_user; // = false;
void wait_for_user_response(millis_t ms/*=0*/, const bool no_sleep/*=false*/) {
UNUSED(no_sleep);
KEEPALIVE_STATE(PAUSED_FOR_USER);
wait_for_user = true;
if (ms) ms += millis(); // expire time
while (wait_for_user && !(ms && ELAPSED(millis(), ms)))
idle(TERN_(ADVANCED_PAUSE_FEATURE, no_sleep));
wait_for_user = false;
while (ui.button_pressed()) safe_delay(50);
}
#endif
/**
* ***************************************************************************
* ******************************** FUNCTIONS ********************************
* ***************************************************************************
*/
/**
* Stepper Reset (RigidBoard, et.al.)
*/
#if HAS_STEPPER_RESET
void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
#endif
/**
* Sensitive pin test for M42, M226
*/
#include "pins/sensitive_pins.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnarrowing"
#pragma GCC diagnostic ignored "-Wsign-compare"
bool pin_is_protected(const pin_t pin) {
#define pgm_read_pin(P) (sizeof(pin_t) == 2 ? (pin_t)pgm_read_word(P) : (pin_t)pgm_read_byte(P))
for (uint8_t i = 0; i < COUNT(sensitive_dio); ++i)
if (pin == pgm_read_pin(&sensitive_dio[i])) return true;
for (uint8_t i = 0; i < COUNT(sensitive_aio); ++i)
if (pin == analogInputToDigitalPin(pgm_read_pin(&sensitive_aio[i]))) return true;
return false;
}
#pragma GCC diagnostic pop
bool printer_busy() {
return planner.movesplanned() || printingIsActive();
}
/**
* A Print Job exists when the timer is running or SD is printing
*/
bool printJobOngoing() { return print_job_timer.isRunning() || IS_SD_PRINTING(); }
/**
* Printing is active when a job is underway but not paused
*/
bool printingIsActive() { return !did_pause_print && printJobOngoing(); }
/**
* Printing is paused according to SD or host indicators
*/
bool printingIsPaused() {
return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
}
void startOrResumeJob() {
if (!printingIsPaused()) {
TERN_(GCODE_REPEAT_MARKERS, repeat.reset());
TERN_(CANCEL_OBJECTS, cancelable.reset());
TERN_(LCD_SHOW_E_TOTAL, e_move_accumulator = 0);
TERN_(SET_REMAINING_TIME, ui.reset_remaining_time());
}
print_job_timer.start();
}
#if HAS_MEDIA
inline void abortSDPrinting() {
IF_DISABLED(NO_SD_AUTOSTART, card.autofile_cancel());
card.abortFilePrintNow(TERN_(SD_RESORT, true));
queue.clear();
quickstop_stepper();
print_job_timer.abort();
IF_DISABLED(SD_ABORT_NO_COOLDOWN, thermalManager.disable_all_heaters());
TERN(HAS_CUTTER, cutter.kill(), thermalManager.zero_fan_speeds()); // Full cutter shutdown including ISR control
wait_for_heatup = false;
TERN_(POWER_LOSS_RECOVERY, recovery.purge());
#ifdef EVENT_GCODE_SD_ABORT
queue.inject(F(EVENT_GCODE_SD_ABORT));
#endif
TERN_(PASSWORD_AFTER_SD_PRINT_ABORT, password.lock_machine());
}
inline void finishSDPrinting() {
if (queue.enqueue_one(F("M1001"))) { // Keep trying until it gets queued
marlin_state = MarlinState::MF_RUNNING; // Signal to stop trying
TERN_(PASSWORD_AFTER_SD_PRINT_END, password.lock_machine());
TERN_(DGUS_LCD_UI_MKS, screen.sdPrintingFinished());
}
}
#endif // HAS_MEDIA
/**
* Minimal management of Marlin's core activities:
* - Keep the command buffer full
* - Check for maximum inactive time between commands
* - Check for maximum inactive time between stepper commands
* - Check if CHDK_PIN needs to go LOW
* - Check for KILL button held down
* - Check for HOME button held down
* - Check for CUSTOM USER button held down
* - Check if cooling fan needs to be switched on
* - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
* - Pulse FET_SAFETY_PIN if it exists
*/
inline void manage_inactivity(const bool no_stepper_sleep=false) {
queue.get_available_commands();
const millis_t ms = millis();
// Prevent steppers timing-out
const bool do_reset_timeout = no_stepper_sleep
|| TERN0(PAUSE_PARK_NO_STEPPER_TIMEOUT, did_pause_print);
// Reset both the M18/M84 activity timeout and the M85 max 'kill' timeout
if (do_reset_timeout) gcode.reset_stepper_timeout(ms);
if (gcode.stepper_max_timed_out(ms)) {
SERIAL_ERROR_START();
SERIAL_ECHOPGM(STR_KILL_PRE);
SERIAL_ECHOLNPGM(STR_KILL_INACTIVE_TIME, parser.command_ptr);
kill();
}
const bool has_blocks = planner.has_blocks_queued(); // Any moves in the planner?
if (has_blocks) gcode.reset_stepper_timeout(ms); // Reset timeout for M18/M84, M85 max 'kill', and laser.
// M18 / M84 : Handle steppers inactive time timeout
#if HAS_DISABLE_IDLE_AXES
if (gcode.stepper_inactive_time) {
static bool already_shutdown_steppers; // = false
if (!has_blocks && !do_reset_timeout && gcode.stepper_inactive_timeout()) {
if (!already_shutdown_steppers) {
already_shutdown_steppers = true;
// Individual axes will be disabled if configured
TERN_(DISABLE_IDLE_X, stepper.disable_axis(X_AXIS));
TERN_(DISABLE_IDLE_Y, stepper.disable_axis(Y_AXIS));
TERN_(DISABLE_IDLE_Z, stepper.disable_axis(Z_AXIS));
TERN_(DISABLE_IDLE_I, stepper.disable_axis(I_AXIS));
TERN_(DISABLE_IDLE_J, stepper.disable_axis(J_AXIS));
TERN_(DISABLE_IDLE_K, stepper.disable_axis(K_AXIS));
TERN_(DISABLE_IDLE_U, stepper.disable_axis(U_AXIS));
TERN_(DISABLE_IDLE_V, stepper.disable_axis(V_AXIS));
TERN_(DISABLE_IDLE_W, stepper.disable_axis(W_AXIS));
TERN_(DISABLE_IDLE_E, stepper.disable_e_steppers());
TERN_(AUTO_BED_LEVELING_UBL, bedlevel.steppers_were_disabled());
}
}
else
already_shutdown_steppers = false;
}
#endif
#if ENABLED(PHOTO_GCODE) && PIN_EXISTS(CHDK)
// Check if CHDK should be set to LOW (after M240 set it HIGH)
extern millis_t chdk_timeout;
if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
chdk_timeout = 0;
WRITE(CHDK_PIN, LOW);
}
#endif
#if HAS_KILL
// Check if the kill button was pressed and wait to ensure the signal is not noise
// typically caused by poor insulation and grounding on LCD cables.
// Lower numbers here will increase response time and therefore safety rating.
// It is recommended to set this as low as possibe without false triggers.
// -------------------------------------------------------------------------------
#ifndef KILL_DELAY
#define KILL_DELAY 250
#endif
static int killCount = 0; // make the inactivity button a bit less responsive
if (kill_state())
killCount++;
else if (killCount > 0)
killCount--;
// Exceeded threshold and we can confirm that it was not accidental
// KILL the machine
// ----------------------------------------------------------------
if (killCount >= KILL_DELAY) {
SERIAL_ERROR_START();
SERIAL_ECHOPGM(STR_KILL_PRE);
SERIAL_ECHOLNPGM(STR_KILL_BUTTON);
kill();
}
#endif
#if ENABLED(FREEZE_FEATURE)
stepper.frozen = READ(FREEZE_PIN) == FREEZE_STATE;
#endif
#if HAS_HOME
// Handle a standalone HOME button
constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
static millis_t next_home_key_ms; // = 0
if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
if (ELAPSED(ms, next_home_key_ms)) {
next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
LCD_MESSAGE(MSG_AUTO_HOME);
queue.inject_P(G28_STR);
}
}
#endif
#if ENABLED(CUSTOM_USER_BUTTONS)
// Handle a custom user button if defined
const bool printer_not_busy = !printingIsActive();
#define HAS_CUSTOM_USER_BUTTON(N) (PIN_EXISTS(BUTTON##N) && defined(BUTTON##N##_HIT_STATE) && defined(BUTTON##N##_GCODE))
#define HAS_BETTER_USER_BUTTON(N) HAS_CUSTOM_USER_BUTTON(N) && defined(BUTTON##N##_DESC)
#define _CHECK_CUSTOM_USER_BUTTON(N, CODE) do{ \
constexpr millis_t CUB_DEBOUNCE_DELAY_##N = 250UL; \
static millis_t next_cub_ms_##N; \
if (BUTTON##N##_HIT_STATE == READ(BUTTON##N##_PIN) \
&& (ENABLED(BUTTON##N##_WHEN_PRINTING) || printer_not_busy)) { \
if (ELAPSED(ms, next_cub_ms_##N)) { \
next_cub_ms_##N = ms + CUB_DEBOUNCE_DELAY_##N; \
CODE; \
queue.inject(F(BUTTON##N##_GCODE)); \
TERN_(HAS_MARLINUI_MENU, ui.quick_feedback()); \
} \
} \
}while(0)
#define CHECK_CUSTOM_USER_BUTTON(N) _CHECK_CUSTOM_USER_BUTTON(N, NOOP)
#define CHECK_BETTER_USER_BUTTON(N) _CHECK_CUSTOM_USER_BUTTON(N, if (strlen(BUTTON##N##_DESC)) LCD_MESSAGE_F(BUTTON##N##_DESC))
#if HAS_BETTER_USER_BUTTON(1)
CHECK_BETTER_USER_BUTTON(1);
#elif HAS_CUSTOM_USER_BUTTON(1)
CHECK_CUSTOM_USER_BUTTON(1);
#endif
#if HAS_BETTER_USER_BUTTON(2)
CHECK_BETTER_USER_BUTTON(2);
#elif HAS_CUSTOM_USER_BUTTON(2)
CHECK_CUSTOM_USER_BUTTON(2);
#endif
#if HAS_BETTER_USER_BUTTON(3)
CHECK_BETTER_USER_BUTTON(3);
#elif HAS_CUSTOM_USER_BUTTON(3)
CHECK_CUSTOM_USER_BUTTON(3);
#endif
#if HAS_BETTER_USER_BUTTON(4)
CHECK_BETTER_USER_BUTTON(4);
#elif HAS_CUSTOM_USER_BUTTON(4)
CHECK_CUSTOM_USER_BUTTON(4);
#endif
#if HAS_BETTER_USER_BUTTON(5)
CHECK_BETTER_USER_BUTTON(5);
#elif HAS_CUSTOM_USER_BUTTON(5)
CHECK_CUSTOM_USER_BUTTON(5);
#endif
#if HAS_BETTER_USER_BUTTON(6)
CHECK_BETTER_USER_BUTTON(6);
#elif HAS_CUSTOM_USER_BUTTON(6)
CHECK_CUSTOM_USER_BUTTON(6);
#endif
#if HAS_BETTER_USER_BUTTON(7)
CHECK_BETTER_USER_BUTTON(7);
#elif HAS_CUSTOM_USER_BUTTON(7)
CHECK_CUSTOM_USER_BUTTON(7);
#endif
#if HAS_BETTER_USER_BUTTON(8)
CHECK_BETTER_USER_BUTTON(8);
#elif HAS_CUSTOM_USER_BUTTON(8)
CHECK_CUSTOM_USER_BUTTON(8);
#endif
#if HAS_BETTER_USER_BUTTON(9)
CHECK_BETTER_USER_BUTTON(9);
#elif HAS_CUSTOM_USER_BUTTON(9)
CHECK_CUSTOM_USER_BUTTON(9);
#endif
#if HAS_BETTER_USER_BUTTON(10)
CHECK_BETTER_USER_BUTTON(10);
#elif HAS_CUSTOM_USER_BUTTON(10)
CHECK_CUSTOM_USER_BUTTON(10);
#endif
#if HAS_BETTER_USER_BUTTON(11)
CHECK_BETTER_USER_BUTTON(11);
#elif HAS_CUSTOM_USER_BUTTON(11)
CHECK_CUSTOM_USER_BUTTON(11);
#endif
#if HAS_BETTER_USER_BUTTON(12)
CHECK_BETTER_USER_BUTTON(12);
#elif HAS_CUSTOM_USER_BUTTON(12)
CHECK_CUSTOM_USER_BUTTON(12);
#endif
#if HAS_BETTER_USER_BUTTON(13)
CHECK_BETTER_USER_BUTTON(13);
#elif HAS_CUSTOM_USER_BUTTON(13)
CHECK_CUSTOM_USER_BUTTON(13);
#endif
#if HAS_BETTER_USER_BUTTON(14)
CHECK_BETTER_USER_BUTTON(14);
#elif HAS_CUSTOM_USER_BUTTON(14)
CHECK_CUSTOM_USER_BUTTON(14);
#endif
#if HAS_BETTER_USER_BUTTON(15)
CHECK_BETTER_USER_BUTTON(15);
#elif HAS_CUSTOM_USER_BUTTON(15)
CHECK_CUSTOM_USER_BUTTON(15);
#endif
#if HAS_BETTER_USER_BUTTON(16)
CHECK_BETTER_USER_BUTTON(16);
#elif HAS_CUSTOM_USER_BUTTON(16)
CHECK_CUSTOM_USER_BUTTON(16);
#endif
#if HAS_BETTER_USER_BUTTON(17)
CHECK_BETTER_USER_BUTTON(17);
#elif HAS_CUSTOM_USER_BUTTON(17)
CHECK_CUSTOM_USER_BUTTON(17);
#endif
#if HAS_BETTER_USER_BUTTON(18)
CHECK_BETTER_USER_BUTTON(18);
#elif HAS_CUSTOM_USER_BUTTON(18)
CHECK_CUSTOM_USER_BUTTON(18);
#endif
#if HAS_BETTER_USER_BUTTON(19)
CHECK_BETTER_USER_BUTTON(19);
#elif HAS_CUSTOM_USER_BUTTON(19)
CHECK_CUSTOM_USER_BUTTON(19);
#endif
#if HAS_BETTER_USER_BUTTON(20)
CHECK_BETTER_USER_BUTTON(20);
#elif HAS_CUSTOM_USER_BUTTON(20)
CHECK_CUSTOM_USER_BUTTON(20);
#endif
#if HAS_BETTER_USER_BUTTON(21)
CHECK_BETTER_USER_BUTTON(21);
#elif HAS_CUSTOM_USER_BUTTON(21)
CHECK_CUSTOM_USER_BUTTON(21);
#endif
#if HAS_BETTER_USER_BUTTON(22)
CHECK_BETTER_USER_BUTTON(22);
#elif HAS_CUSTOM_USER_BUTTON(22)
CHECK_CUSTOM_USER_BUTTON(22);
#endif
#if HAS_BETTER_USER_BUTTON(23)
CHECK_BETTER_USER_BUTTON(23);
#elif HAS_CUSTOM_USER_BUTTON(23)
CHECK_CUSTOM_USER_BUTTON(23);
#endif
#if HAS_BETTER_USER_BUTTON(24)
CHECK_BETTER_USER_BUTTON(24);
#elif HAS_CUSTOM_USER_BUTTON(24)
CHECK_CUSTOM_USER_BUTTON(24);
#endif
#if HAS_BETTER_USER_BUTTON(25)
CHECK_BETTER_USER_BUTTON(25);
#elif HAS_CUSTOM_USER_BUTTON(25)
CHECK_CUSTOM_USER_BUTTON(25);
#endif
#endif
TERN_(EASYTHREED_UI, easythreed_ui.run());
TERN_(USE_CONTROLLER_FAN, controllerFan.update()); // Check if fan should be turned on to cool stepper drivers down
TERN_(AUTO_POWER_CONTROL, powerManager.check(!ui.on_status_screen() || printJobOngoing() || printingIsPaused()));
TERN_(HOTEND_IDLE_TIMEOUT, hotend_idle.check());
#if ANY(PSU_CONTROL, AUTO_POWER_CONTROL) && PIN_EXISTS(PS_ON_EDM)
if ( ELAPSED(ms, powerManager.last_state_change_ms + PS_EDM_RESPONSE)
&& (READ(PS_ON_PIN) != READ(PS_ON_EDM_PIN) || TERN0(PSU_OFF_REDUNDANT, extDigitalRead(PS_ON1_PIN) != extDigitalRead(PS_ON1_EDM_PIN)))
) kill(GET_TEXT_F(MSG_POWER_EDM_FAULT));
#endif
#if ENABLED(EXTRUDER_RUNOUT_PREVENT)
if (thermalManager.degHotend(active_extruder) > (EXTRUDER_RUNOUT_MINTEMP)
&& ELAPSED(ms, gcode.previous_move_ms + SEC_TO_MS(EXTRUDER_RUNOUT_SECONDS))
&& !planner.has_blocks_queued()
) {
const int8_t e_stepper = TERN(HAS_SWITCHING_EXTRUDER, active_extruder >> 1, active_extruder);
const bool e_off = !stepper.AXIS_IS_ENABLED(E_AXIS, e_stepper);
if (e_off) stepper.ENABLE_EXTRUDER(e_stepper);
const float olde = current_position.e;
current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
current_position.e = olde;
planner.set_e_position_mm(olde);
planner.synchronize();
if (e_off) stepper.DISABLE_EXTRUDER(e_stepper);
gcode.reset_stepper_timeout(ms);
}
#endif // EXTRUDER_RUNOUT_PREVENT
#if ENABLED(DUAL_X_CARRIAGE)
// handle delayed move timeout
if (delayed_move_time && ELAPSED(ms, delayed_move_time) && IsRunning()) {
// travel moves have been received so enact them
delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
destination = current_position;
prepare_line_to_destination();
planner.synchronize();
}
#endif
TERN_(TEMP_STAT_LEDS, handle_status_leds());
TERN_(MONITOR_DRIVER_STATUS, monitor_tmc_drivers());
// Limit check_axes_activity frequency to 10Hz
static millis_t next_check_axes_ms = 0;
if (ELAPSED(ms, next_check_axes_ms)) {
planner.check_axes_activity();
next_check_axes_ms = ms + 100UL;
}
#if PIN_EXISTS(FET_SAFETY)
static millis_t FET_next;
if (ELAPSED(ms, FET_next)) {
FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
DELAY_US(2);
WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
}
#endif
}
#if ALL(EP_BABYSTEPPING, EMERGENCY_PARSER)
#include "feature/babystep.h"
#endif
/**
* Standard idle routine keeps the machine alive:
* - Core Marlin activities
* - Manage heaters (and Watchdog)
* - Max7219 heartbeat, animation, etc.
*
* Only after setup() is complete:
* - Handle filament runout sensors
* - Run HAL idle tasks
* - Handle Power-Loss Recovery
* - Run StallGuard endstop checks
* - Handle SD Card insert / remove
* - Handle USB Flash Drive insert / remove
* - Announce Host Keepalive state (if any)
* - Update the Print Job Timer state
* - Update the Beeper queue
* - Read Buttons and Update the LCD
* - Run i2c Position Encoders
* - Auto-report Temperatures / SD Status
* - Update the Průša MMU2
* - Handle Joystick jogging
*/
void idle(const bool no_stepper_sleep/*=false*/) {
#ifdef MAX7219_DEBUG_PROFILE
CodeProfiler idle_profiler;
#endif
#if ENABLED(MARLIN_DEV_MODE)
static uint16_t idle_depth = 0;
if (++idle_depth > 5) SERIAL_ECHOLNPGM("idle() call depth: ", idle_depth);
#endif
// Bed Distance Sensor task
TERN_(BD_SENSOR, bdl.process());
// Core Marlin activities
manage_inactivity(no_stepper_sleep);
// Manage Heaters (and Watchdog)
thermalManager.task();
// Max7219 heartbeat, animation, etc
TERN_(MAX7219_DEBUG, max7219.idle_tasks());
// Return if setup() isn't completed
if (marlin_state == MarlinState::MF_INITIALIZING) goto IDLE_DONE;
// TODO: Still causing errors
TERN_(TOOL_SENSOR, (void)check_tool_sensor_stats(active_extruder, true));
// Handle filament runout sensors
#if HAS_FILAMENT_SENSOR
if (TERN1(HAS_PRUSA_MMU2, !mmu2.enabled()))
runout.run();
#endif
// Run HAL idle tasks
hal.idletask();
// Check network connection
TERN_(HAS_ETHERNET, ethernet.check());
// Handle Power-Loss Recovery
#if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
if (IS_SD_PRINTING()) recovery.outage();
#endif
// Run StallGuard endstop checks
#if ENABLED(SPI_ENDSTOPS)
if (endstops.tmc_spi_homing.any && TERN1(IMPROVE_HOMING_RELIABILITY, ELAPSED(millis(), sg_guard_period)))
for (uint8_t i = 0; i < 4; ++i) if (endstops.tmc_spi_homing_check()) break; // Read SGT 4 times per idle loop
#endif
// Handle SD Card insert / remove
TERN_(HAS_MEDIA, card.manage_media());
// Handle USB Flash Drive insert / remove
TERN_(USB_FLASH_DRIVE_SUPPORT, card.diskIODriver()->idle());
// Announce Host Keepalive state (if any)
TERN_(HOST_KEEPALIVE_FEATURE, gcode.host_keepalive());
// Update the Print Job Timer state
TERN_(PRINTCOUNTER, print_job_timer.tick());
// Update the Beeper queue
TERN_(HAS_BEEPER, buzzer.tick());
// Handle UI input / draw events
ui.update();
// Run i2c Position Encoders
#if ENABLED(I2C_POSITION_ENCODERS)
{
static millis_t i2cpem_next_update_ms;
if (planner.has_blocks_queued()) {
const millis_t ms = millis();
if (ELAPSED(ms, i2cpem_next_update_ms)) {
I2CPEM.update();
i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
}
}
}
#endif
// Auto-report Temperatures / SD Status
#if HAS_AUTO_REPORTING
if (!gcode.autoreport_paused) {
TERN_(AUTO_REPORT_TEMPERATURES, thermalManager.auto_reporter.tick());
TERN_(AUTO_REPORT_FANS, fan_check.auto_reporter.tick());
TERN_(AUTO_REPORT_SD_STATUS, card.auto_reporter.tick());
TERN_(AUTO_REPORT_POSITION, position_auto_reporter.tick());
TERN_(BUFFER_MONITORING, queue.auto_report_buffer_statistics());
}
#endif
// Update the Průša MMU2
TERN_(HAS_PRUSA_MMU2, mmu2.mmu_loop());
// Handle Joystick jogging
TERN_(POLL_JOG, joystick.inject_jog_moves());
// Async Babystepping via the Emergency Parser
#if ALL(EP_BABYSTEPPING, EMERGENCY_PARSER)
babystep.do_ep_steps();
#endif
// Direct Stepping
TERN_(DIRECT_STEPPING, page_manager.write_responses());
// Update the LVGL interface
TERN_(HAS_TFT_LVGL_UI, LV_TASK_HANDLER());
// Manage Fixed-time Motion Control
TERN_(FT_MOTION, ftMotion.loop());
IDLE_DONE:
TERN_(MARLIN_DEV_MODE, idle_depth--);
return;
}
/**
* Kill all activity and lock the machine.
* After this the machine will need to be reset.
*/
void kill(FSTR_P const lcd_error/*=nullptr*/, FSTR_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
thermalManager.disable_all_heaters();
TERN_(HAS_CUTTER, cutter.kill()); // Full cutter shutdown including ISR control
// Echo the LCD message to serial for extra context
if (lcd_error) { SERIAL_ECHO_START(); SERIAL_ECHOLN(lcd_error); }
#if HAS_DISPLAY
ui.kill_screen(lcd_error ?: GET_TEXT_F(MSG_KILLED), lcd_component ?: FPSTR(NUL_STR));
#else
UNUSED(lcd_error); UNUSED(lcd_component);
#endif
TERN_(HAS_TFT_LVGL_UI, lv_draw_error_message(lcd_error));
// "Error:Printer halted. kill() called!"
SERIAL_ERROR_MSG(STR_ERR_KILLED);
#ifdef ACTION_ON_KILL
hostui.kill();
#endif
minkill(steppers_off);
}
void minkill(const bool steppers_off/*=false*/) {
// Wait a short time (allows messages to get out before shutting down.
for (int i = 1000; i--;) DELAY_US(600);
cli(); // Stop interrupts
// Wait to ensure all interrupts stopped
for (int i = 1000; i--;) DELAY_US(250);
// Reiterate heaters off
thermalManager.disable_all_heaters();
TERN_(HAS_CUTTER, cutter.kill()); // Reiterate cutter shutdown
// Power off all steppers (for M112) or just the E steppers
steppers_off ? stepper.disable_all_steppers() : stepper.disable_e_steppers();
TERN_(PSU_CONTROL, powerManager.power_off());
TERN_(HAS_SUICIDE, suicide());
#if ANY(HAS_KILL, SOFT_RESET_ON_KILL)
// Wait for both KILL and ENC to be released
while (TERN0(HAS_KILL, kill_state()) || TERN0(SOFT_RESET_ON_KILL, ui.button_pressed()))
hal.watchdog_refresh();
// Wait for either KILL or ENC to be pressed again
while (TERN1(HAS_KILL, !kill_state()) && TERN1(SOFT_RESET_ON_KILL, !ui.button_pressed()))
hal.watchdog_refresh();
// Reboot the board
hal.reboot();
#else
for (;;) hal.watchdog_refresh(); // Wait for RESET button or power-cycle
#endif
}
/**
* Turn off heaters and stop the print in progress
* After a stop the machine may be resumed with M999
*/
void stop() {
thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
print_job_timer.stop();
#if ANY(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
thermalManager.set_fans_paused(false); // Un-pause fans for safety
#endif
if (!IsStopped()) {
SERIAL_ERROR_MSG(STR_ERR_STOPPED);
LCD_MESSAGE(MSG_STOPPED);
safe_delay(350); // allow enough time for messages to get out before stopping
marlin_state = MarlinState::MF_STOPPED;
}
}
inline void tmc_standby_setup() {
#if PIN_EXISTS(X_STDBY)
SET_INPUT_PULLDOWN(X_STDBY_PIN);
#endif
#if PIN_EXISTS(X2_STDBY)
SET_INPUT_PULLDOWN(X2_STDBY_PIN);
#endif
#if PIN_EXISTS(Y_STDBY)
SET_INPUT_PULLDOWN(Y_STDBY_PIN);
#endif
#if PIN_EXISTS(Y2_STDBY)
SET_INPUT_PULLDOWN(Y2_STDBY_PIN);
#endif
#if PIN_EXISTS(Z_STDBY)
SET_INPUT_PULLDOWN(Z_STDBY_PIN);
#endif
#if PIN_EXISTS(Z2_STDBY)
SET_INPUT_PULLDOWN(Z2_STDBY_PIN);
#endif
#if PIN_EXISTS(Z3_STDBY)
SET_INPUT_PULLDOWN(Z3_STDBY_PIN);
#endif
#if PIN_EXISTS(Z4_STDBY)
SET_INPUT_PULLDOWN(Z4_STDBY_PIN);
#endif
#if PIN_EXISTS(I_STDBY)
SET_INPUT_PULLDOWN(I_STDBY_PIN);
#endif
#if PIN_EXISTS(J_STDBY)
SET_INPUT_PULLDOWN(J_STDBY_PIN);