forked from prusa3d/Prusa-Firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarlin_main.cpp
executable file
·11653 lines (10087 loc) · 390 KB
/
Marlin_main.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
/* -*- c++ -*- */
/**
* @file
*/
/**
* @mainpage Reprap 3D printer firmware based on Sprinter and grbl.
*
* @section intro_sec Introduction
*
* This firmware is a mashup between Sprinter and grbl.
* https://github.com/kliment/Sprinter
* https://github.com/simen/grbl/tree
*
* It has preliminary support for Matthew Roberts advance algorithm
* http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
*
* Prusa Research s.r.o. https://www.prusa3d.cz
*
* @section copyright_sec Copyright
*
* 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 <http://www.gnu.org/licenses/>.
*
* @section notes_sec Notes
*
* * Do not create static objects in global functions.
* Otherwise constructor guard against concurrent calls is generated costing
* about 8B RAM and 14B flash.
*
*
*/
//-//
#include "Configuration.h"
#include "Marlin.h"
#ifdef ENABLE_AUTO_BED_LEVELING
#include "vector_3.h"
#ifdef AUTO_BED_LEVELING_GRID
#include "qr_solve.h"
#endif
#endif // ENABLE_AUTO_BED_LEVELING
#ifdef MESH_BED_LEVELING
#include "mesh_bed_leveling.h"
#include "mesh_bed_calibration.h"
#endif
#include "printers.h"
#include "menu.h"
#include "ultralcd.h"
#include "backlight.h"
#include "planner.h"
#include "stepper.h"
#include "temperature.h"
#include "motion_control.h"
#include "cardreader.h"
#include "ConfigurationStore.h"
#include "language.h"
#include "pins_arduino.h"
#include "math.h"
#include "util.h"
#include "Timer.h"
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include "Dcodes.h"
#include "AutoDeplete.h"
#ifndef LA_NOCOMPAT
#include "la10compat.h"
#endif
#ifdef SWSPI
#include "swspi.h"
#endif //SWSPI
#include "spi.h"
#ifdef SWI2C
#include "swi2c.h"
#endif //SWI2C
#ifdef FILAMENT_SENSOR
#include "fsensor.h"
#endif //FILAMENT_SENSOR
#ifdef TMC2130
#include "tmc2130.h"
#endif //TMC2130
#ifdef W25X20CL
#include "w25x20cl.h"
#include "optiboot_w25x20cl.h"
#endif //W25X20CL
#ifdef BLINKM
#include "BlinkM.h"
#include "Wire.h"
#endif
#ifdef ULTRALCD
#include "ultralcd.h"
#endif
#if NUM_SERVOS > 0
#include "Servo.h"
#endif
#if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
#include <SPI.h>
#endif
#include "mmu.h"
#define VERSION_STRING "1.0.2"
#include "ultralcd.h"
#include "sound.h"
#include "cmdqueue.h"
#include "io_atmega2560.h"
// Macros for bit masks
#define BIT(b) (1<<(b))
#define TEST(n,b) (((n)&BIT(b))!=0)
#define SET_BIT(n,b,value) (n) ^= ((-value)^(n)) & (BIT(b))
//Macro for print fan speed
#define FAN_PULSE_WIDTH_LIMIT ((fanSpeed > 100) ? 3 : 4) //time in ms
//filament types
#define FILAMENT_DEFAULT 0
#define FILAMENT_FLEX 1
#define FILAMENT_PVA 2
#define FILAMENT_UNDEFINED 255
//Stepper Movement Variables
//===========================================================================
//=============================imported variables============================
//===========================================================================
//===========================================================================
//=============================public variables=============================
//===========================================================================
#ifdef SDSUPPORT
CardReader card;
#endif
unsigned long PingTime = _millis();
unsigned long NcTime;
uint8_t mbl_z_probe_nr = 3; //numer of Z measurements for each point in mesh bed leveling calibration
//used for PINDA temp calibration and pause print
#define DEFAULT_RETRACTION 1
#define DEFAULT_RETRACTION_MM 4 //MM
float default_retraction = DEFAULT_RETRACTION;
float homing_feedrate[] = HOMING_FEEDRATE;
// Currently only the extruder axis may be switched to a relative mode.
// Other axes are always absolute or relative based on the common relative_mode flag.
bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
int feedmultiply=100; //100->1 200->2
int extrudemultiply=100; //100->1 200->2
int extruder_multiply[EXTRUDERS] = {100
#if EXTRUDERS > 1
, 100
#if EXTRUDERS > 2
, 100
#endif
#endif
};
int bowden_length[4] = {385, 385, 385, 385};
bool is_usb_printing = false;
bool homing_flag = false;
bool temp_cal_active = false;
unsigned long kicktime = _millis()+100000;
unsigned int usb_printing_counter;
int8_t lcd_change_fil_state = 0;
unsigned long pause_time = 0;
unsigned long start_pause_print = _millis();
unsigned long t_fan_rising_edge = _millis();
LongTimer safetyTimer;
static LongTimer crashDetTimer;
//unsigned long load_filament_time;
bool mesh_bed_leveling_flag = false;
bool mesh_bed_run_from_menu = false;
bool prusa_sd_card_upload = false;
unsigned int status_number = 0;
unsigned long total_filament_used;
unsigned int heating_status;
unsigned int heating_status_counter;
bool loading_flag = false;
char snmm_filaments_used = 0;
bool fan_state[2];
int fan_edge_counter[2];
int fan_speed[2];
char dir_names[3][9];
bool sortAlpha = false;
float extruder_multiplier[EXTRUDERS] = {1.0
#if EXTRUDERS > 1
, 1.0
#if EXTRUDERS > 2
, 1.0
#endif
#endif
};
float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
//shortcuts for more readable code
#define _x current_position[X_AXIS]
#define _y current_position[Y_AXIS]
#define _z current_position[Z_AXIS]
#define _e current_position[E_AXIS]
float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
bool axis_known_position[3] = {false, false, false};
// Extruder offset
#if EXTRUDERS > 1
#define NUM_EXTRUDER_OFFSETS 2 // only in XY plane
float extruder_offset[NUM_EXTRUDER_OFFSETS][EXTRUDERS] = {
#if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
#endif
};
#endif
uint8_t active_extruder = 0;
int fanSpeed=0;
#ifdef FWRETRACT
bool retracted[EXTRUDERS]={false
#if EXTRUDERS > 1
, false
#if EXTRUDERS > 2
, false
#endif
#endif
};
bool retracted_swap[EXTRUDERS]={false
#if EXTRUDERS > 1
, false
#if EXTRUDERS > 2
, false
#endif
#endif
};
float retract_length_swap = RETRACT_LENGTH_SWAP;
float retract_recover_length_swap = RETRACT_RECOVER_LENGTH_SWAP;
#endif
#ifdef PS_DEFAULT_OFF
bool powersupply = false;
#else
bool powersupply = true;
#endif
bool cancel_heatup = false ;
int8_t busy_state = NOT_BUSY;
static long prev_busy_signal_ms = -1;
uint8_t host_keepalive_interval = HOST_KEEPALIVE_INTERVAL;
const char errormagic[] PROGMEM = "Error:";
const char echomagic[] PROGMEM = "echo:";
bool no_response = false;
uint8_t important_status;
uint8_t saved_filament_type;
#define SAVED_TARGET_UNSET (X_MIN_POS-1)
float saved_target[NUM_AXIS] = {SAVED_TARGET_UNSET, 0, 0, 0};
// save/restore printing in case that mmu was not responding
bool mmu_print_saved = false;
// storing estimated time to end of print counted by slicer
uint8_t print_percent_done_normal = PRINT_PERCENT_DONE_INIT;
uint16_t print_time_remaining_normal = PRINT_TIME_REMAINING_INIT; //estimated remaining print time in minutes
uint8_t print_percent_done_silent = PRINT_PERCENT_DONE_INIT;
uint16_t print_time_remaining_silent = PRINT_TIME_REMAINING_INIT; //estimated remaining print time in minutes
//===========================================================================
//=============================Private Variables=============================
//===========================================================================
#define MSG_BED_LEVELING_FAILED_TIMEOUT 30
const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
// For tracing an arc
static float offset[3] = {0.0, 0.0, 0.0};
// Current feedrate
float feedrate = 1500.0;
// Feedrate for the next move
static float next_feedrate;
// Original feedrate saved during homing moves
static float saved_feedrate;
const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
//static float tt = 0;
//static float bt = 0;
//Inactivity shutdown variables
static unsigned long previous_millis_cmd = 0;
unsigned long max_inactive_time = 0;
static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
static unsigned long safetytimer_inactive_time = DEFAULT_SAFETYTIMER_TIME_MINS*60*1000ul;
unsigned long starttime=0;
unsigned long stoptime=0;
unsigned long _usb_timer = 0;
bool Stopped=false;
#if NUM_SERVOS > 0
Servo servos[NUM_SERVOS];
#endif
bool target_direction;
//Insert variables if CHDK is defined
#ifdef CHDK
unsigned long chdkHigh = 0;
boolean chdkActive = false;
#endif
//! @name RAM save/restore printing
//! @{
bool saved_printing = false; //!< Print is paused and saved in RAM
static uint32_t saved_sdpos = 0; //!< SD card position, or line number in case of USB printing
uint8_t saved_printing_type = PRINTING_TYPE_SD;
static float saved_pos[4] = { 0, 0, 0, 0 };
static uint16_t saved_feedrate2 = 0; //!< Default feedrate (truncated from float)
static int saved_feedmultiply2 = 0;
static uint8_t saved_active_extruder = 0;
static float saved_extruder_temperature = 0.0; //!< Active extruder temperature
static bool saved_extruder_relative_mode = false;
static int saved_fanSpeed = 0; //!< Print fan speed
//! @}
static int saved_feedmultiply_mm = 100;
//===========================================================================
//=============================Routines======================================
//===========================================================================
static void get_arc_coordinates();
static bool setTargetedHotend(int code, uint8_t &extruder);
static void print_time_remaining_init();
static void wait_for_heater(long codenum, uint8_t extruder);
static void gcode_G28(bool home_x_axis, bool home_y_axis, bool home_z_axis);
static void temp_compensation_start();
static void temp_compensation_apply();
uint16_t gcode_in_progress = 0;
uint16_t mcode_in_progress = 0;
void serial_echopair_P(const char *s_P, float v)
{ serialprintPGM(s_P); SERIAL_ECHO(v); }
void serial_echopair_P(const char *s_P, double v)
{ serialprintPGM(s_P); SERIAL_ECHO(v); }
void serial_echopair_P(const char *s_P, unsigned long v)
{ serialprintPGM(s_P); SERIAL_ECHO(v); }
/*FORCE_INLINE*/ void serialprintPGM(const char *str)
{
#if 0
char ch=pgm_read_byte(str);
while(ch)
{
MYSERIAL.write(ch);
ch=pgm_read_byte(++str);
}
#else
// hmm, same size as the above version, the compiler did a good job optimizing the above
while( uint8_t ch = pgm_read_byte(str) ){
MYSERIAL.write((char)ch);
++str;
}
#endif
}
#ifdef SDSUPPORT
#include "SdFatUtil.h"
int freeMemory() { return SdFatUtil::FreeRam(); }
#else
extern "C" {
extern unsigned int __bss_end;
extern unsigned int __heap_start;
extern void *__brkval;
int freeMemory() {
int free_memory;
if ((int)__brkval == 0)
free_memory = ((int)&free_memory) - ((int)&__bss_end);
else
free_memory = ((int)&free_memory) - ((int)__brkval);
return free_memory;
}
}
#endif //!SDSUPPORT
void setup_killpin()
{
#if defined(KILL_PIN) && KILL_PIN > -1
SET_INPUT(KILL_PIN);
WRITE(KILL_PIN,HIGH);
#endif
}
// Set home pin
void setup_homepin(void)
{
#if defined(HOME_PIN) && HOME_PIN > -1
SET_INPUT(HOME_PIN);
WRITE(HOME_PIN,HIGH);
#endif
}
void setup_photpin()
{
#if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
SET_OUTPUT(PHOTOGRAPH_PIN);
WRITE(PHOTOGRAPH_PIN, LOW);
#endif
}
void setup_powerhold()
{
#if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
SET_OUTPUT(SUICIDE_PIN);
WRITE(SUICIDE_PIN, HIGH);
#endif
#if defined(PS_ON_PIN) && PS_ON_PIN > -1
SET_OUTPUT(PS_ON_PIN);
#if defined(PS_DEFAULT_OFF)
WRITE(PS_ON_PIN, PS_ON_ASLEEP);
#else
WRITE(PS_ON_PIN, PS_ON_AWAKE);
#endif
#endif
}
void suicide()
{
#if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
SET_OUTPUT(SUICIDE_PIN);
WRITE(SUICIDE_PIN, LOW);
#endif
}
void servo_init()
{
#if (NUM_SERVOS >= 1) && defined(SERVO0_PIN) && (SERVO0_PIN > -1)
servos[0].attach(SERVO0_PIN);
#endif
#if (NUM_SERVOS >= 2) && defined(SERVO1_PIN) && (SERVO1_PIN > -1)
servos[1].attach(SERVO1_PIN);
#endif
#if (NUM_SERVOS >= 3) && defined(SERVO2_PIN) && (SERVO2_PIN > -1)
servos[2].attach(SERVO2_PIN);
#endif
#if (NUM_SERVOS >= 4) && defined(SERVO3_PIN) && (SERVO3_PIN > -1)
servos[3].attach(SERVO3_PIN);
#endif
#if (NUM_SERVOS >= 5)
#error "TODO: enter initalisation code for more servos"
#endif
}
bool fans_check_enabled = true;
#ifdef TMC2130
void crashdet_stop_and_save_print()
{
stop_and_save_print_to_ram(10, -default_retraction); //XY - no change, Z 10mm up, E -1mm retract
}
void crashdet_restore_print_and_continue()
{
restore_print_from_ram_and_continue(default_retraction); //XYZ = orig, E +1mm unretract
// babystep_apply();
}
void crashdet_stop_and_save_print2()
{
cli();
planner_abort_hard(); //abort printing
cmdqueue_reset(); //empty cmdqueue
card.sdprinting = false;
card.closefile();
// Reset and re-enable the stepper timer just before the global interrupts are enabled.
st_reset_timer();
sei();
}
void crashdet_detected(uint8_t mask)
{
st_synchronize();
static uint8_t crashDet_counter = 0;
bool automatic_recovery_after_crash = true;
if (crashDet_counter++ == 0) {
crashDetTimer.start();
}
else if (crashDetTimer.expired(CRASHDET_TIMER * 1000ul)){
crashDetTimer.stop();
crashDet_counter = 0;
}
else if(crashDet_counter == CRASHDET_COUNTER_MAX){
automatic_recovery_after_crash = false;
crashDetTimer.stop();
crashDet_counter = 0;
}
else {
crashDetTimer.start();
}
lcd_update_enable(true);
lcd_clear();
lcd_update(2);
if (mask & X_AXIS_MASK)
{
eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT_X, eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_X) + 1);
eeprom_update_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT) + 1);
}
if (mask & Y_AXIS_MASK)
{
eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_Y) + 1);
eeprom_update_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT) + 1);
}
lcd_update_enable(true);
lcd_update(2);
lcd_setstatuspgm(_T(MSG_CRASH_DETECTED));
gcode_G28(true, true, false); //home X and Y
st_synchronize();
if (automatic_recovery_after_crash) {
enquecommand_P(PSTR("CRASH_RECOVER"));
}else{
setTargetHotend(0, active_extruder);
bool yesno = lcd_show_fullscreen_message_yes_no_and_wait_P(_i("Crash detected. Resume print?"), false);
lcd_update_enable(true);
if (yesno)
{
enquecommand_P(PSTR("CRASH_RECOVER"));
}
else
{
enquecommand_P(PSTR("CRASH_CANCEL"));
}
}
}
void crashdet_recover()
{
crashdet_restore_print_and_continue();
if (lcd_crash_detect_enabled()) tmc2130_sg_stop_on_crash = true;
}
void crashdet_cancel()
{
saved_printing = false;
tmc2130_sg_stop_on_crash = true;
if (saved_printing_type == PRINTING_TYPE_SD) {
lcd_print_stop();
}else if(saved_printing_type == PRINTING_TYPE_USB){
SERIAL_ECHOLNRPGM(MSG_OCTOPRINT_CANCEL); //for Octoprint: works the same as clicking "Abort" button in Octoprint GUI
SERIAL_PROTOCOLLNRPGM(MSG_OK);
}
}
#endif //TMC2130
void failstats_reset_print()
{
eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_X, 0);
eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_Y, 0);
eeprom_update_byte((uint8_t *)EEPROM_FERROR_COUNT, 0);
eeprom_update_byte((uint8_t *)EEPROM_POWER_COUNT, 0);
eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0);
eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0);
#if defined(FILAMENT_SENSOR) && defined(PAT9125)
fsensor_softfail = 0;
#endif
}
#ifdef MESH_BED_LEVELING
enum MeshLevelingState { MeshReport, MeshStart, MeshNext, MeshSet };
#endif
// Factory reset function
// This function is used to erase parts or whole EEPROM memory which is used for storing calibration and and so on.
// Level input parameter sets depth of reset
int er_progress = 0;
static void factory_reset(char level)
{
lcd_clear();
switch (level) {
// Level 0: Language reset
case 0:
Sound_MakeCustom(100,0,false);
lang_reset();
break;
//Level 1: Reset statistics
case 1:
Sound_MakeCustom(100,0,false);
eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_X, 0);
eeprom_update_byte((uint8_t *)EEPROM_CRASH_COUNT_Y, 0);
eeprom_update_byte((uint8_t *)EEPROM_FERROR_COUNT, 0);
eeprom_update_byte((uint8_t *)EEPROM_POWER_COUNT, 0);
eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_X_TOT, 0);
eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_Y_TOT, 0);
eeprom_update_word((uint16_t *)EEPROM_FERROR_COUNT_TOT, 0);
eeprom_update_word((uint16_t *)EEPROM_POWER_COUNT_TOT, 0);
eeprom_update_word((uint16_t *)EEPROM_MMU_FAIL_TOT, 0);
eeprom_update_word((uint16_t *)EEPROM_MMU_LOAD_FAIL_TOT, 0);
eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0);
eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0);
lcd_menu_statistics();
break;
// Level 2: Prepare for shipping
case 2:
//lcd_puts_P(PSTR("Factory RESET"));
//lcd_puts_at_P(1,2,PSTR("Shipping prep"));
// Force language selection at the next boot up.
lang_reset();
// Force the "Follow calibration flow" message at the next boot up.
calibration_status_store(CALIBRATION_STATUS_Z_CALIBRATION);
eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 1); //run wizard
farm_no = 0;
farm_mode = false;
eeprom_update_byte((uint8_t*)EEPROM_FARM_MODE, farm_mode);
EEPROM_save_B(EEPROM_FARM_NUMBER, &farm_no);
eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_X_TOT, 0);
eeprom_update_word((uint16_t *)EEPROM_CRASH_COUNT_Y_TOT, 0);
eeprom_update_word((uint16_t *)EEPROM_FERROR_COUNT_TOT, 0);
eeprom_update_word((uint16_t *)EEPROM_POWER_COUNT_TOT, 0);
eeprom_update_word((uint16_t *)EEPROM_MMU_FAIL_TOT, 0);
eeprom_update_word((uint16_t *)EEPROM_MMU_LOAD_FAIL_TOT, 0);
eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0);
eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0);
#ifdef FILAMENT_SENSOR
fsensor_enable();
fsensor_autoload_set(true);
#endif //FILAMENT_SENSOR
Sound_MakeCustom(100,0,false);
//_delay_ms(2000);
break;
// Level 3: erase everything, whole EEPROM will be set to 0xFF
case 3:
lcd_puts_P(PSTR("Factory RESET"));
lcd_puts_at_P(1, 2, PSTR("ERASING all data"));
Sound_MakeCustom(100,0,false);
er_progress = 0;
lcd_puts_at_P(3, 3, PSTR(" "));
lcd_set_cursor(3, 3);
lcd_print(er_progress);
// Erase EEPROM
for (int i = 0; i < 4096; i++) {
eeprom_update_byte((uint8_t*)i, 0xFF);
if (i % 41 == 0) {
er_progress++;
lcd_puts_at_P(3, 3, PSTR(" "));
lcd_set_cursor(3, 3);
lcd_print(er_progress);
lcd_puts_P(PSTR("%"));
}
}
break;
case 4:
bowden_menu();
break;
default:
break;
}
}
extern "C" {
FILE _uartout; //= {0}; Global variable is always zero initialized. No need to explicitly state this.
}
int uart_putchar(char c, FILE *)
{
MYSERIAL.write(c);
return 0;
}
void lcd_splash()
{
lcd_clear(); // clears display and homes screen
lcd_puts_P(PSTR("\n Original Prusa i3\n Prusa Research"));
}
void factory_reset()
{
KEEPALIVE_STATE(PAUSED_FOR_USER);
if (!READ(BTN_ENC))
{
_delay_ms(1000);
if (!READ(BTN_ENC))
{
lcd_clear();
lcd_puts_P(PSTR("Factory RESET"));
SET_OUTPUT(BEEPER);
if(eSoundMode!=e_SOUND_MODE_SILENT)
WRITE(BEEPER, HIGH);
while (!READ(BTN_ENC));
WRITE(BEEPER, LOW);
_delay_ms(2000);
char level = reset_menu();
factory_reset(level);
switch (level) {
case 0: _delay_ms(0); break;
case 1: _delay_ms(0); break;
case 2: _delay_ms(0); break;
case 3: _delay_ms(0); break;
}
}
}
KEEPALIVE_STATE(IN_HANDLER);
}
void show_fw_version_warnings() {
if (FW_DEV_VERSION == FW_VERSION_GOLD || FW_DEV_VERSION == FW_VERSION_RC) return;
switch (FW_DEV_VERSION) {
case(FW_VERSION_ALPHA): lcd_show_fullscreen_message_and_wait_P(_i("You are using firmware alpha version. This is development version. Using this version is not recommended and may cause printer damage.")); break;////MSG_FW_VERSION_ALPHA c=20 r=8
case(FW_VERSION_BETA): lcd_show_fullscreen_message_and_wait_P(_i("You are using firmware beta version. This is development version. Using this version is not recommended and may cause printer damage.")); break;////MSG_FW_VERSION_BETA c=20 r=8
case(FW_VERSION_DEVEL):
case(FW_VERSION_DEBUG):
lcd_update_enable(false);
lcd_clear();
#if FW_DEV_VERSION == FW_VERSION_DEVEL
lcd_puts_at_P(0, 0, PSTR("Development build !!"));
#else
lcd_puts_at_P(0, 0, PSTR("Debbugging build !!!"));
#endif
lcd_puts_at_P(0, 1, PSTR("May destroy printer!"));
lcd_puts_at_P(0, 2, PSTR("ver ")); lcd_puts_P(PSTR(FW_VERSION_FULL));
lcd_puts_at_P(0, 3, PSTR(FW_REPOSITORY));
lcd_wait_for_click();
break;
// default: lcd_show_fullscreen_message_and_wait_P(_i("WARNING: This is an unofficial, unsupported build. Use at your own risk!")); break;////MSG_FW_VERSION_UNKNOWN c=20 r=8
}
lcd_update_enable(true);
}
//! @brief try to check if firmware is on right type of printer
static void check_if_fw_is_on_right_printer(){
#ifdef FILAMENT_SENSOR
if((PRINTER_TYPE == PRINTER_MK3) || (PRINTER_TYPE == PRINTER_MK3S)){
#ifdef IR_SENSOR
swi2c_init();
const uint8_t pat9125_detected = swi2c_readByte_A8(PAT9125_I2C_ADDR,0x00,NULL);
if (pat9125_detected){
lcd_show_fullscreen_message_and_wait_P(_i("MK3S firmware detected on MK3 printer"));}
#endif //IR_SENSOR
#ifdef PAT9125
//will return 1 only if IR can detect filament in bondtech extruder so this may fail even when we have IR sensor
const uint8_t ir_detected = !(PIN_GET(IR_SENSOR_PIN));
if (ir_detected){
lcd_show_fullscreen_message_and_wait_P(_i("MK3 firmware detected on MK3S printer"));}
#endif //PAT9125
}
#endif //FILAMENT_SENSOR
}
uint8_t check_printer_version()
{
uint8_t version_changed = 0;
uint16_t printer_type = eeprom_read_word((uint16_t*)EEPROM_PRINTER_TYPE);
uint16_t motherboard = eeprom_read_word((uint16_t*)EEPROM_BOARD_TYPE);
if (printer_type != PRINTER_TYPE) {
if (printer_type == 0xffff) eeprom_write_word((uint16_t*)EEPROM_PRINTER_TYPE, PRINTER_TYPE);
else version_changed |= 0b10;
}
if (motherboard != MOTHERBOARD) {
if(motherboard == 0xffff) eeprom_write_word((uint16_t*)EEPROM_BOARD_TYPE, MOTHERBOARD);
else version_changed |= 0b01;
}
return version_changed;
}
#ifdef BOOTAPP
#include "bootapp.h" //bootloader support
#endif //BOOTAPP
#if (LANG_MODE != 0) //secondary language support
#ifdef W25X20CL
// language update from external flash
#define LANGBOOT_BLOCKSIZE 0x1000u
#define LANGBOOT_RAMBUFFER 0x0800
void update_sec_lang_from_external_flash()
{
if ((boot_app_magic == BOOT_APP_MAGIC) && (boot_app_flags & BOOT_APP_FLG_USER0))
{
uint8_t lang = boot_reserved >> 4;
uint8_t state = boot_reserved & 0xf;
lang_table_header_t header;
uint32_t src_addr;
if (lang_get_header(lang, &header, &src_addr))
{
lcd_puts_at_P(1,3,PSTR("Language update."));
for (uint8_t i = 0; i < state; i++) fputc('.', lcdout);
_delay(100);
boot_reserved = (state + 1) | (lang << 4);
if ((state * LANGBOOT_BLOCKSIZE) < header.size)
{
cli();
uint16_t size = header.size - state * LANGBOOT_BLOCKSIZE;
if (size > LANGBOOT_BLOCKSIZE) size = LANGBOOT_BLOCKSIZE;
w25x20cl_rd_data(src_addr + state * LANGBOOT_BLOCKSIZE, (uint8_t*)LANGBOOT_RAMBUFFER, size);
if (state == 0)
{
//TODO - check header integrity
}
bootapp_ram2flash(LANGBOOT_RAMBUFFER, _SEC_LANG_TABLE + state * LANGBOOT_BLOCKSIZE, size);
}
else
{
//TODO - check sec lang data integrity
eeprom_update_byte((unsigned char *)EEPROM_LANG, LANG_ID_SEC);
}
}
}
boot_app_flags &= ~BOOT_APP_FLG_USER0;
}
#ifdef DEBUG_W25X20CL
uint8_t lang_xflash_enum_codes(uint16_t* codes)
{
lang_table_header_t header;
uint8_t count = 0;
uint32_t addr = 0x00000;
while (1)
{
printf_P(_n("LANGTABLE%d:"), count);
w25x20cl_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t));
if (header.magic != LANG_MAGIC)
{
printf_P(_n("NG!\n"));
break;
}
printf_P(_n("OK\n"));
printf_P(_n(" _lt_magic = 0x%08lx %S\n"), header.magic, (header.magic==LANG_MAGIC)?_n("OK"):_n("NA"));
printf_P(_n(" _lt_size = 0x%04x (%d)\n"), header.size, header.size);
printf_P(_n(" _lt_count = 0x%04x (%d)\n"), header.count, header.count);
printf_P(_n(" _lt_chsum = 0x%04x\n"), header.checksum);
printf_P(_n(" _lt_code = 0x%04x (%c%c)\n"), header.code, header.code >> 8, header.code & 0xff);
printf_P(_n(" _lt_sign = 0x%08lx\n"), header.signature);
addr += header.size;
codes[count] = header.code;
count ++;
}
return count;
}
void list_sec_lang_from_external_flash()
{
uint16_t codes[8];
uint8_t count = lang_xflash_enum_codes(codes);
printf_P(_n("XFlash lang count = %hhd\n"), count);
}
#endif //DEBUG_W25X20CL
#endif //W25X20CL
#endif //(LANG_MODE != 0)
static void w25x20cl_err_msg()
{
lcd_clear();
lcd_puts_P(_n("External SPI flash\nW25X20CL is not res-\nponding. Language\nswitch unavailable."));
}
// "Setup" function is called by the Arduino framework on startup.
// Before startup, the Timers-functions (PWM)/Analog RW and HardwareSerial provided by the Arduino-code
// are initialized by the main() routine provided by the Arduino framework.
void setup()
{
mmu_init();
ultralcd_init();