Prusa3d Marlin fork
Marlin.h
1 // Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware.
2 // License: GPL
3 
4 #ifndef MARLIN_H
5 #define MARLIN_H
6 
7 #include "macros.h"
8 
9 #include <math.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <inttypes.h>
14 
15 #include <util/delay.h>
16 #include <avr/pgmspace.h>
17 #include <avr/eeprom.h>
18 #include <avr/interrupt.h>
19 #include "system_timer.h"
20 #include "fastio.h"
21 #include "Configuration.h"
22 #include "pins.h"
23 #include "Timer.h"
24 #include "mmu2.h"
25 #include "printer_state.h"
26 
27 #ifndef AT90USB
28 #define HardwareSerial_h // trick to disable the standard HWserial
29 #endif
30 
31 #if (ARDUINO >= 100)
32 # include "Arduino.h"
33 #else
34 # include "WProgram.h"
35 #endif
36 
37 // Arduino < 1.0.0 does not define this, so we need to do it ourselves
38 #ifndef analogInputToDigitalPin
39 # define analogInputToDigitalPin(p) ((p) + A0)
40 #endif
41 
42 #ifdef AT90USB
43 #include "HardwareSerial.h"
44 #endif
45 
46 #include "MarlinSerial.h"
47 
48 #ifndef cbi
49 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
50 #endif
51 #ifndef sbi
52 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
53 #endif
54 
55 //#include "WString.h"
56 
57 #ifdef AT90USB
58  #ifdef BTENABLED
59  #define MYSERIAL bt
60  #else
61  #define MYSERIAL Serial
62  #endif // BTENABLED
63 #else
64  #define MYSERIAL MSerial
65 #endif
66 
67 #include "lcd.h"
68 
69 #define SERIAL_PROTOCOL(x) (MYSERIAL.print(x))
70 #define SERIAL_PROTOCOL_F(x,y) (MYSERIAL.print(x,y))
71 #define SERIAL_PROTOCOLPGM(x) (serialprintPGM(PSTR(x)))
72 #define SERIAL_PROTOCOLRPGM(x) (serialprintPGM((x)))
73 #define SERIAL_PROTOCOLLN(x) (MYSERIAL.println(x))
74 #define SERIAL_PROTOCOLLNPGM(x) (serialprintlnPGM(PSTR(x)))
75 #define SERIAL_PROTOCOLLNRPGM(x) (serialprintlnPGM((x)))
76 
77 
78 extern const char errormagic[] PROGMEM;
79 extern const char echomagic[] PROGMEM;
80 
81 #define SERIAL_ERROR_START (serialprintPGM(errormagic))
82 #define SERIAL_ERROR(x) SERIAL_PROTOCOL(x)
83 #define SERIAL_ERRORPGM(x) SERIAL_PROTOCOLPGM(x)
84 #define SERIAL_ERRORRPGM(x) SERIAL_PROTOCOLRPGM(x)
85 #define SERIAL_ERRORLN(x) SERIAL_PROTOCOLLN(x)
86 #define SERIAL_ERRORLNPGM(x) SERIAL_PROTOCOLLNPGM(x)
87 #define SERIAL_ERRORLNRPGM(x) SERIAL_PROTOCOLLNRPGM(x)
88 
89 #define SERIAL_ECHO_START (serialprintPGM(echomagic))
90 #define SERIAL_ECHO(x) SERIAL_PROTOCOL(x)
91 #define SERIAL_ECHOPGM(x) SERIAL_PROTOCOLPGM(x)
92 #define SERIAL_ECHORPGM(x) SERIAL_PROTOCOLRPGM(x)
93 #define SERIAL_ECHOLN(x) SERIAL_PROTOCOLLN(x)
94 #define SERIAL_ECHOLNPGM(x) SERIAL_PROTOCOLLNPGM(x)
95 #define SERIAL_ECHOLNRPGM(x) SERIAL_PROTOCOLLNRPGM(x)
96 
97 #define SERIAL_ECHOPAIR(name,value) (serial_echopair_P(PSTR(name),(value)))
98 
99 void serial_echopair_P(const char *s_P, float v);
100 void serial_echopair_P(const char *s_P, double v);
101 void serial_echopair_P(const char *s_P, unsigned long v);
102 
103 
104 //Things to write to serial from Program memory. Saves 400 to 2k of RAM.
105 // Making this FORCE_INLINE is not a good idea when running out of FLASH
106 // I'd rather skip a few CPU ticks than 5.5KB (!!) of FLASH
107 void serialprintPGM(const char *str);
108 
109 //The "ln" variant of the function above.
110 void serialprintlnPGM(const char *str);
111 
112 bool is_buffer_empty();
113 void process_commands();
114 void ramming();
115 
116 void manage_inactivity(bool ignore_stepper_queue=false);
117 
118 #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
119  #define enable_x() WRITE(X_ENABLE_PIN, X_ENABLE_ON)
120  #define disable_x() { WRITE(X_ENABLE_PIN,!X_ENABLE_ON); axis_known_position[X_AXIS] = false; }
121 #else
122  #define enable_x() ;
123  #define disable_x() ;
124 #endif
125 
126 #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
127  #ifdef Y_DUAL_STEPPER_DRIVERS
128  #define enable_y() { WRITE(Y_ENABLE_PIN, Y_ENABLE_ON); WRITE(Y2_ENABLE_PIN, Y_ENABLE_ON); }
129  #define disable_y() { WRITE(Y_ENABLE_PIN,!Y_ENABLE_ON); WRITE(Y2_ENABLE_PIN, !Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; }
130  #else
131  #define enable_y() WRITE(Y_ENABLE_PIN, Y_ENABLE_ON)
132  #define disable_y() { WRITE(Y_ENABLE_PIN,!Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; }
133  #endif
134 #else
135  #define enable_y() ;
136  #define disable_y() ;
137 #endif
138 
139 #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
140  #if defined(Z_AXIS_ALWAYS_ON)
141  #ifdef Z_DUAL_STEPPER_DRIVERS
142  #define poweron_z() { WRITE(Z_ENABLE_PIN, Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN, Z_ENABLE_ON); }
143  #define poweroff_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }
144  #else
145  #define poweron_z() WRITE(Z_ENABLE_PIN, Z_ENABLE_ON)
146  #define poweroff_z() {}
147  #endif
148  #else
149  #ifdef Z_DUAL_STEPPER_DRIVERS
150  #define poweron_z() { WRITE(Z_ENABLE_PIN, Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN, Z_ENABLE_ON); }
151  #define poweroff_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }
152  #else
153  #define poweron_z() WRITE(Z_ENABLE_PIN, Z_ENABLE_ON)
154  #define poweroff_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }
155  #endif
156  #endif
157 #else
158  #define poweron_z() {}
159  #define poweroff_z() {}
160 #endif
161 
162 #ifndef PSU_Delta
163  #define enable_z() poweron_z()
164  #define disable_z() poweroff_z()
165 #else
166  extern bool bEnableForce_z; // Used by ultralcd stealth toggle
167  void init_force_z();
168  void check_force_z();
169  void enable_force_z();
170  void disable_force_z();
171  #define enable_z() enable_force_z()
172  #define disable_z() disable_force_z()
173 #endif // PSU_Delta
174 
175 #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
176  #define enable_e0() WRITE(E0_ENABLE_PIN, E_ENABLE_ON)
177  #define disable_e0() WRITE(E0_ENABLE_PIN,!E_ENABLE_ON)
178 #else
179  #define enable_e0() /* nothing */
180  #define disable_e0() /* nothing */
181 #endif
182 
183 enum AxisEnum {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, E_AXIS=3, X_HEAD=4, Y_HEAD=5};
184 #define X_AXIS_MASK 1
185 #define Y_AXIS_MASK 2
186 #define Z_AXIS_MASK 4
187 #define E_AXIS_MASK 8
188 #define X_HEAD_MASK 16
189 #define Y_HEAD_MASK 32
190 
191 
192 void FlushSerialRequestResend();
193 void ClearToSend();
194 void update_currents();
195 
196 void kill(const char *full_screen_message = NULL);
197 void finishAndDisableSteppers();
198 
199 void UnconditionalStop(); // Stop heaters, motion and clear current print status
200 void ConditionalStop(); // Similar to UnconditionalStop, but doesn't disable heaters
201 void ThermalStop(bool allow_pause = false); // Emergency stop used by overtemp functions which allows
202  // recovery (with pause=true)
203 bool IsStopped(); // Returns true if the print has been stopped
204 
205 //put an ASCII command at the end of the current buffer, read from flash
206 #define enquecommand_P(cmd) enquecommand(cmd, true)
207 
208 //put an ASCII command at the begin of the current buffer, read from flash
209 #define enquecommand_front_P(cmd) enquecommand_front(cmd, true)
210 
211 void clamp_to_software_endstops(float target[3]);
212 void refresh_cmd_timeout(void);
213 
214 #ifdef FAST_PWM_FAN
215 void setPwmFrequency(uint8_t pin, int val);
216 #endif
217 
218 enum class HeatingStatus : uint8_t
219 {
220  NO_HEATING = 0,
221  EXTRUDER_HEATING = 1,
222  EXTRUDER_HEATING_COMPLETE = 2,
223  BED_HEATING = 3,
224  BED_HEATING_COMPLETE = 4,
225 };
226 
227 extern HeatingStatus heating_status;
228 
229 extern bool fans_check_enabled;
230 constexpr float homing_feedrate[] = HOMING_FEEDRATE;
231 extern uint8_t axis_relative_modes;
232 extern float feedrate;
233 extern int feedmultiply;
234 extern int extrudemultiply; // Sets extrude multiply factor (in percent) for all extruders
235 extern float extruder_multiplier[EXTRUDERS]; // reciprocal of cross-sectional area of filament (in square millimeters), stored this way to reduce computational burden in planner
236 extern float current_position[NUM_AXIS] ;
237 extern float destination[NUM_AXIS] ;
238 extern float min_pos[3];
239 extern float max_pos[3];
240 extern bool axis_known_position[3];
241 extern uint8_t fanSpeed;
242 extern uint8_t newFanSpeed;
243 extern float default_retraction;
244 
245 void get_coordinates();
246 void prepare_move(uint16_t start_segment_idx = 0);
247 void prepare_arc_move(bool isclockwise, uint16_t start_segment_idx = 0);
248 uint16_t restore_interrupted_gcode();
249 
253 float __attribute__((noinline)) get_feedrate_mm_s(const float feedrate_mm_min);
254 
255 #ifdef TMC2130
256 void check_Z_crash(void);
257 void homeaxis(uint8_t axis, uint8_t cnt = 1, uint8_t* pstep = 0);
258 #else
259 void homeaxis(uint8_t axis, uint8_t cnt = 1);
260 #endif //TMC2130
261 
262 #ifdef FWRETRACT
263 extern bool retracted[EXTRUDERS];
264 extern float retract_length_swap;
265 extern float retract_recover_length_swap;
266 #endif
267 
268 extern ShortTimer usb_timer;
269 extern bool processing_tcode;
270 extern bool homing_flag;
271 extern uint32_t total_filament_used; // mm/100 or 10um
272 
274 void save_statistics();
275 
276 extern int fan_edge_counter[2];
277 extern int fan_speed[2];
278 
279 // Active extruder becomes a #define to make the whole firmware compilable.
280 // We may even remove the references to it wherever possible in the future
281 #define active_extruder 0
282 
283 extern bool mesh_bed_leveling_flag;
284 extern bool did_pause_print;
285 
286 // save/restore printing
287 extern bool saved_printing;
288 extern uint32_t saved_sdpos;
289 extern uint8_t saved_printing_type;
290 
291 extern uint16_t saved_extruder_temperature;
292 extern uint8_t saved_bed_temperature;
293 extern bool saved_extruder_relative_mode;
294 extern uint8_t saved_fan_speed;
295 extern float saved_pos[NUM_AXIS];
296 extern uint16_t saved_feedrate2;
297 
298 //estimated time to end of the print
299 extern uint8_t print_percent_done_normal;
300 extern uint8_t print_percent_done_silent;
301 extern uint16_t print_time_remaining_normal;
302 extern uint16_t print_time_remaining_silent;
303 extern uint16_t print_time_to_change_normal;
304 extern uint16_t print_time_to_change_silent;
305 
306 #define PRINT_TIME_REMAINING_INIT 0xffff
307 
308 extern LongTimer safetyTimer;
309 
310 #define PRINT_PERCENT_DONE_INIT 0xff
311 
312 // Returns true if there is a print running. It does not matter if
313 // the print is paused, that still counts as a "running" print.
314 bool printJobOngoing();
315 
316 // Make debug_printer_states available everywhere
317 #ifdef DEBUG_PRINTER_STATES
318 void debug_printer_states();
319 #endif //DEBUG_PRINTER_STATES
320 
321 // Printing is paused according to SD or host indicators
322 bool printingIsPaused();
323 
324 bool printer_active();
325 
326 bool printer_recovering();
327 
335 bool check_fsensor();
336 
345 bool babystep_allowed();
346 
350 bool babystep_allowed_strict();
351 
352 extern void calculate_extruder_multipliers();
353 
354 // Similar to the default Arduino delay function,
355 // but it keeps the background tasks running.
356 extern void delay_keep_alive(unsigned int ms);
357 
358 extern void check_babystep();
359 
360 extern void long_pause();
361 extern void crashdet_stop_and_save_print();
362 
363 #ifdef HEATBED_ANALYSIS
364 void d_setup();
365 float d_ReadData();
366 void bed_analysis(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y);
367 void bed_check(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y);
368 #endif //HEATBED_ANALYSIS
369 float temp_comp_interpolation(float temperature);
370 #if 0
371 void show_fw_version_warnings();
372 #endif
373 uint8_t check_printer_version();
374 
375 #ifdef PINDA_THERMISTOR
376 float temp_compensation_pinda_thermistor_offset(float temperature_pinda);
377 #endif //PINDA_THERMISTOR
378 
379 void serialecho_temperatures();
380 bool check_commands();
381 
382 extern void print_world_coordinates();
383 extern void print_physical_coordinates();
384 extern void print_mesh_bed_leveling_table();
385 
386 void save_print_file_state();
387 void restore_print_file_state();
388 void save_planner_global_state();
389 void refresh_print_state_in_ram();
390 
393 void refresh_saved_feedrate_multiplier_in_ram();
394 void clear_print_state_in_ram();
395 extern void stop_and_save_print_to_ram(float z_move, float e_move);
396 void restore_file_from_sd();
397 void restore_extruder_temperature_from_ram();
398 extern void restore_print_from_ram_and_continue(float e_move);
399 extern void cancel_saved_printing();
400 
401 // Define some coordinates outside the clamp limits (making them invalid past the parsing stage) so
402 // that they can be used later for various logical checks
403 #define X_COORD_INVALID (X_MIN_POS-1)
404 #define SAVED_START_POSITION_UNSET X_COORD_INVALID
405 extern float saved_start_position[NUM_AXIS];
406 extern uint16_t saved_segment_idx;
407 extern bool isPartialBackupAvailable;
408 
409 
410 //estimated time to end of the print
411 extern uint8_t calc_percent_done();
412 
413 
414 
415 // States for managing Marlin and host communication
416 // Marlin sends messages if blocked or busy
417 /*enum MarlinBusyState {
418  NOT_BUSY, // Not in a handler
419  IN_HANDLER, // Processing a GCode
420  IN_PROCESS, // Known to be blocking command input (as in G29)
421  PAUSED_FOR_USER, // Blocking pending any input
422  PAUSED_FOR_INPUT // Blocking pending text input (concept)
423 };*/
424 
425 #define NOT_BUSY 1
426 #define IN_HANDLER 2
427 #define IN_PROCESS 3
428 #define PAUSED_FOR_USER 4
429 #define PAUSED_FOR_INPUT 5
430 
431 #define KEEPALIVE_STATE(n) do { busy_state = n;} while (0)
432 extern void host_keepalive();
433 extern void host_autoreport();
434 //extern MarlinBusyState busy_state;
435 extern int8_t busy_state;
436 
437 
438 #ifdef TMC2130
439 
440 #define FORCE_HIGH_POWER_START force_high_power_mode(true)
441 #define FORCE_HIGH_POWER_END force_high_power_mode(false)
442 
443 void force_high_power_mode(bool start_high_power_section);
444 void change_power_mode_live(uint8_t mode);
445 
446 #endif //TMC2130
447 
448 // G-codes
449 
450 bool gcode_M45(bool onlyZ, int8_t verbosity_level);
451 void gcode_M114();
452 #if (defined(FANCHECK) && (((defined(TACH_0) && (TACH_0 >-1)) || (defined(TACH_1) && (TACH_1 > -1)))))
453 void gcode_M123();
454 #endif //FANCHECK and TACH_0 and TACH_1
455 void gcode_M701(float fastLoadLength, uint8_t mmuSlotIndex);
456 
457 #define UVLO !(PINE & (1<<4))
458 
459 
460 void M600_load_filament();
461 void M600_load_filament_movements();
462 void M600_wait_for_user();
463 bool M600_check_state_and_repeat();
464 void load_filament_final_feed();
465 void marlin_wait_for_click();
466 float raise_z(float delta);
467 void raise_z_above(float target);
468 
469 extern "C" void softReset();
470 void stack_error();
471 
472 extern uint32_t IP_address;
473 #endif
uint32_t saved_sdpos
SD card position, or line number in case of USB printing.
Definition: Marlin_main.cpp:314
uint16_t saved_extruder_temperature
Active extruder temperature.
Definition: Marlin_main.cpp:319
void debug_printer_states()
debug printer states
Definition: Marlin_main.cpp:538
uint8_t saved_bed_temperature
Bed temperature.
Definition: Marlin_main.cpp:320
float raise_z(float delta)
Safely move Z-axis by distance delta (mm)
Definition: Marlin_main.cpp:2146
bool check_fsensor()
Definition: Marlin_main.cpp:586
void host_keepalive()
Definition: Marlin_main.cpp:1746
bool saved_printing
Print is paused and saved in RAM.
Definition: Marlin_main.cpp:313
void host_autoreport()
Definition: Marlin_main.cpp:1721
uint8_t fanSpeed
Print fan speed, ranges from 0 to 255.
Definition: Marlin_main.cpp:207
uint16_t saved_feedrate2
Default feedrate (truncated from float)
Definition: Marlin_main.cpp:317
uint8_t saved_fan_speed
Print fan speed, ranges from 0 to 255.
Definition: Marlin_main.cpp:322
static constexpr float __attribute__((noinline)) count_e(float layer_height
Count extrude length.
Printer States.