Marlin.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. // Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware.
  2. // License: GPL
  3. #ifndef MARLIN_H
  4. #define MARLIN_H
  5. #include "macros.h"
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <inttypes.h>
  11. #include <util/delay.h>
  12. #include <avr/pgmspace.h>
  13. #include <avr/eeprom.h>
  14. #include <avr/interrupt.h>
  15. #include "system_timer.h"
  16. #include "fastio.h"
  17. #include "Configuration.h"
  18. #include "pins.h"
  19. #include "Timer.h"
  20. extern uint8_t mbl_z_probe_nr;
  21. #ifndef AT90USB
  22. #define HardwareSerial_h // trick to disable the standard HWserial
  23. #endif
  24. #if (ARDUINO >= 100)
  25. # include "Arduino.h"
  26. #else
  27. # include "WProgram.h"
  28. #endif
  29. // Arduino < 1.0.0 does not define this, so we need to do it ourselves
  30. #ifndef analogInputToDigitalPin
  31. # define analogInputToDigitalPin(p) ((p) + A0)
  32. #endif
  33. #ifdef AT90USB
  34. #include "HardwareSerial.h"
  35. #endif
  36. #include "MarlinSerial.h"
  37. #ifndef cbi
  38. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  39. #endif
  40. #ifndef sbi
  41. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  42. #endif
  43. //#include "WString.h"
  44. #ifdef AT90USB
  45. #ifdef BTENABLED
  46. #define MYSERIAL bt
  47. #else
  48. #define MYSERIAL Serial
  49. #endif // BTENABLED
  50. #else
  51. #define MYSERIAL MSerial
  52. #endif
  53. #include "lcd.h"
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. extern FILE _uartout;
  58. #ifdef __cplusplus
  59. }
  60. #endif
  61. #define uartout (&_uartout)
  62. #define SERIAL_PROTOCOL(x) (MYSERIAL.print(x))
  63. #define SERIAL_PROTOCOL_F(x,y) (MYSERIAL.print(x,y))
  64. #define SERIAL_PROTOCOLPGM(x) (serialprintPGM(PSTR(x)))
  65. #define SERIAL_PROTOCOLRPGM(x) (serialprintPGM((x)))
  66. #define SERIAL_PROTOCOLLN(x) (MYSERIAL.println(x)/*,MYSERIAL.write('\n')*/)
  67. #define SERIAL_PROTOCOLLNPGM(x) (serialprintPGM(PSTR(x)),MYSERIAL.println()/*write('\n')*/)
  68. #define SERIAL_PROTOCOLLNRPGM(x) (serialprintPGM((x)),MYSERIAL.println()/*write('\n')*/)
  69. extern const char errormagic[] PROGMEM;
  70. extern const char echomagic[] PROGMEM;
  71. #define SERIAL_ERROR_START (serialprintPGM(errormagic))
  72. #define SERIAL_ERROR(x) SERIAL_PROTOCOL(x)
  73. #define SERIAL_ERRORPGM(x) SERIAL_PROTOCOLPGM(x)
  74. #define SERIAL_ERRORRPGM(x) SERIAL_PROTOCOLRPGM(x)
  75. #define SERIAL_ERRORLN(x) SERIAL_PROTOCOLLN(x)
  76. #define SERIAL_ERRORLNPGM(x) SERIAL_PROTOCOLLNPGM(x)
  77. #define SERIAL_ERRORLNRPGM(x) SERIAL_PROTOCOLLNRPGM(x)
  78. #define SERIAL_ECHO_START (serialprintPGM(echomagic))
  79. #define SERIAL_ECHO(x) SERIAL_PROTOCOL(x)
  80. #define SERIAL_ECHOPGM(x) SERIAL_PROTOCOLPGM(x)
  81. #define SERIAL_ECHORPGM(x) SERIAL_PROTOCOLRPGM(x)
  82. #define SERIAL_ECHOLN(x) SERIAL_PROTOCOLLN(x)
  83. #define SERIAL_ECHOLNPGM(x) SERIAL_PROTOCOLLNPGM(x)
  84. #define SERIAL_ECHOLNRPGM(x) SERIAL_PROTOCOLLNRPGM(x)
  85. #define SERIAL_ECHOPAIR(name,value) (serial_echopair_P(PSTR(name),(value)))
  86. void serial_echopair_P(const char *s_P, float v);
  87. void serial_echopair_P(const char *s_P, double v);
  88. void serial_echopair_P(const char *s_P, unsigned long v);
  89. //Things to write to serial from Program memory. Saves 400 to 2k of RAM.
  90. // Making this FORCE_INLINE is not a good idea when running out of FLASH
  91. // I'd rather skip a few CPU ticks than 5.5KB (!!) of FLASH
  92. void serialprintPGM(const char *str);
  93. bool is_buffer_empty();
  94. void get_command();
  95. void process_commands();
  96. void ramming();
  97. void manage_inactivity(bool ignore_stepper_queue=false);
  98. #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
  99. #define enable_x() WRITE(X_ENABLE_PIN, X_ENABLE_ON)
  100. #define disable_x() { WRITE(X_ENABLE_PIN,!X_ENABLE_ON); axis_known_position[X_AXIS] = false; }
  101. #else
  102. #define enable_x() ;
  103. #define disable_x() ;
  104. #endif
  105. #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
  106. #ifdef Y_DUAL_STEPPER_DRIVERS
  107. #define enable_y() { WRITE(Y_ENABLE_PIN, Y_ENABLE_ON); WRITE(Y2_ENABLE_PIN, Y_ENABLE_ON); }
  108. #define disable_y() { WRITE(Y_ENABLE_PIN,!Y_ENABLE_ON); WRITE(Y2_ENABLE_PIN, !Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; }
  109. #else
  110. #define enable_y() WRITE(Y_ENABLE_PIN, Y_ENABLE_ON)
  111. #define disable_y() { WRITE(Y_ENABLE_PIN,!Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; }
  112. #endif
  113. #else
  114. #define enable_y() ;
  115. #define disable_y() ;
  116. #endif
  117. #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  118. #if defined(Z_AXIS_ALWAYS_ON)
  119. #ifdef Z_DUAL_STEPPER_DRIVERS
  120. #define poweron_z() { WRITE(Z_ENABLE_PIN, Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN, Z_ENABLE_ON); }
  121. #define poweroff_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }
  122. #else
  123. #define poweron_z() WRITE(Z_ENABLE_PIN, Z_ENABLE_ON)
  124. #define poweroff_z() {}
  125. #endif
  126. #else
  127. #ifdef Z_DUAL_STEPPER_DRIVERS
  128. #define poweron_z() { WRITE(Z_ENABLE_PIN, Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN, Z_ENABLE_ON); }
  129. #define poweroff_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }
  130. #else
  131. #define poweron_z() WRITE(Z_ENABLE_PIN, Z_ENABLE_ON)
  132. #define poweroff_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }
  133. #endif
  134. #endif
  135. #else
  136. #define poweron_z() {}
  137. #define poweroff_z() {}
  138. #endif
  139. #ifndef PSU_Delta
  140. #define enable_z() poweron_z()
  141. #define disable_z() poweroff_z()
  142. #else
  143. void init_force_z();
  144. void check_force_z();
  145. void enable_force_z();
  146. void disable_force_z();
  147. #define enable_z() enable_force_z()
  148. #define disable_z() disable_force_z()
  149. #endif // PSU_Delta
  150. //#if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  151. //#ifdef Z_DUAL_STEPPER_DRIVERS
  152. //#define enable_z() { WRITE(Z_ENABLE_PIN, Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN, Z_ENABLE_ON); }
  153. //#define disable_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); WRITE(Z2_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }
  154. //#else
  155. //#define enable_z() WRITE(Z_ENABLE_PIN, Z_ENABLE_ON)
  156. //#define disable_z() { WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }
  157. //#endif
  158. //#else
  159. //#define enable_z() ;
  160. //#define disable_z() ;
  161. //#endif
  162. #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
  163. #define enable_e0() WRITE(E0_ENABLE_PIN, E_ENABLE_ON)
  164. #define disable_e0() WRITE(E0_ENABLE_PIN,!E_ENABLE_ON)
  165. #else
  166. #define enable_e0() /* nothing */
  167. #define disable_e0() /* nothing */
  168. #endif
  169. #if (EXTRUDERS > 1) && defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  170. #define enable_e1() WRITE(E1_ENABLE_PIN, E_ENABLE_ON)
  171. #define disable_e1() WRITE(E1_ENABLE_PIN,!E_ENABLE_ON)
  172. #else
  173. #define enable_e1() /* nothing */
  174. #define disable_e1() /* nothing */
  175. #endif
  176. #if (EXTRUDERS > 2) && defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  177. #define enable_e2() WRITE(E2_ENABLE_PIN, E_ENABLE_ON)
  178. #define disable_e2() WRITE(E2_ENABLE_PIN,!E_ENABLE_ON)
  179. #else
  180. #define enable_e2() /* nothing */
  181. #define disable_e2() /* nothing */
  182. #endif
  183. #define FARM_FILAMENT_COLOR_NONE 99;
  184. enum AxisEnum {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, E_AXIS=3, X_HEAD=4, Y_HEAD=5};
  185. #define X_AXIS_MASK 1
  186. #define Y_AXIS_MASK 2
  187. #define Z_AXIS_MASK 4
  188. #define E_AXIS_MASK 8
  189. #define X_HEAD_MASK 16
  190. #define Y_HEAD_MASK 32
  191. void FlushSerialRequestResend();
  192. void ClearToSend();
  193. void update_currents();
  194. void get_coordinates();
  195. void prepare_move();
  196. void kill(const char *full_screen_message = NULL, unsigned char id = 0);
  197. void Stop();
  198. bool IsStopped();
  199. void finishAndDisableSteppers();
  200. //put an ASCII command at the end of the current buffer.
  201. void enquecommand(const char *cmd, bool from_progmem = false);
  202. //put an ASCII command at the end of the current buffer, read from flash
  203. #define enquecommand_P(cmd) enquecommand(cmd, true)
  204. //put an ASCII command at the begin of the current buffer
  205. void enquecommand_front(const char *cmd, bool from_progmem = false);
  206. //put an ASCII command at the begin of the current buffer, read from flash
  207. #define enquecommand_front_P(cmd) enquecommand_front(cmd, true)
  208. void repeatcommand_front();
  209. // Remove all lines from the command queue.
  210. void cmdqueue_reset();
  211. void prepare_arc_move(char isclockwise);
  212. void clamp_to_software_endstops(float target[3]);
  213. void refresh_cmd_timeout(void);
  214. // Timer counter, incremented by the 1ms Arduino timer.
  215. // The standard Arduino timer() function returns this value atomically
  216. // by disabling / enabling interrupts. This is costly, if the interrupts are known
  217. // to be disabled.
  218. #ifdef SYSTEM_TIMER_2
  219. extern volatile unsigned long timer2_millis;
  220. #else //SYSTEM_TIMER_2
  221. extern volatile unsigned long timer0_millis;
  222. #endif //SYSTEM_TIMER_2
  223. // An unsynchronized equivalent to a standard Arduino _millis() function.
  224. // To be used inside an interrupt routine.
  225. FORCE_INLINE unsigned long millis_nc() {
  226. #ifdef SYSTEM_TIMER_2
  227. return timer2_millis;
  228. #else //SYSTEM_TIMER_2
  229. return timer0_millis;
  230. #endif //SYSTEM_TIMER_2
  231. }
  232. #ifdef FAST_PWM_FAN
  233. void setPwmFrequency(uint8_t pin, int val);
  234. #endif
  235. extern bool fans_check_enabled;
  236. extern float homing_feedrate[];
  237. extern uint8_t axis_relative_modes;
  238. extern float feedrate;
  239. extern int feedmultiply;
  240. extern int extrudemultiply; // Sets extrude multiply factor (in percent) for all extruders
  241. extern int extruder_multiply[EXTRUDERS]; // sets extrude multiply factor (in percent) for each extruder individually
  242. extern float extruder_multiplier[EXTRUDERS]; // reciprocal of cross-sectional area of filament (in square millimeters), stored this way to reduce computational burden in planner
  243. extern float current_position[NUM_AXIS] ;
  244. extern float destination[NUM_AXIS] ;
  245. extern float min_pos[3];
  246. extern float max_pos[3];
  247. extern bool axis_known_position[3];
  248. extern int fanSpeed;
  249. extern int8_t lcd_change_fil_state;
  250. extern float default_retraction;
  251. #ifdef TMC2130
  252. void homeaxis(int axis, uint8_t cnt = 1, uint8_t* pstep = 0);
  253. #else
  254. void homeaxis(int axis, uint8_t cnt = 1);
  255. #endif //TMC2130
  256. #ifdef FAN_SOFT_PWM
  257. extern unsigned char fanSpeedSoftPwm;
  258. #endif
  259. #ifdef FWRETRACT
  260. extern bool retracted[EXTRUDERS];
  261. extern float retract_length_swap;
  262. extern float retract_recover_length_swap;
  263. #endif
  264. extern uint8_t host_keepalive_interval;
  265. extern unsigned long starttime;
  266. extern unsigned long stoptime;
  267. extern int bowden_length[4];
  268. extern bool is_usb_printing;
  269. extern bool homing_flag;
  270. extern bool loading_flag;
  271. extern unsigned int usb_printing_counter;
  272. extern unsigned long kicktime;
  273. extern unsigned long total_filament_used;
  274. void save_statistics(unsigned long _total_filament_used, unsigned long _total_print_time);
  275. extern unsigned int heating_status;
  276. extern unsigned int status_number;
  277. extern unsigned int heating_status_counter;
  278. extern char snmm_filaments_used;
  279. extern unsigned long PingTime;
  280. extern unsigned long NcTime;
  281. extern bool no_response;
  282. extern uint8_t important_status;
  283. extern uint8_t saved_filament_type;
  284. extern bool fan_state[2];
  285. extern int fan_edge_counter[2];
  286. extern int fan_speed[2];
  287. // Handling multiple extruders pins
  288. extern uint8_t active_extruder;
  289. //Long pause
  290. extern unsigned long pause_time;
  291. extern unsigned long start_pause_print;
  292. extern unsigned long t_fan_rising_edge;
  293. extern bool mesh_bed_leveling_flag;
  294. extern bool mesh_bed_run_from_menu;
  295. extern bool sortAlpha;
  296. extern char dir_names[3][9];
  297. extern int8_t lcd_change_fil_state;
  298. // save/restore printing
  299. extern bool saved_printing;
  300. extern uint8_t saved_printing_type;
  301. #define PRINTING_TYPE_SD 0
  302. #define PRINTING_TYPE_USB 1
  303. #define PRINTING_TYPE_NONE 2
  304. //save/restore printing in case that mmu is not responding
  305. extern bool mmu_print_saved;
  306. //estimated time to end of the print
  307. extern uint8_t print_percent_done_normal;
  308. extern uint16_t print_time_remaining_normal;
  309. extern uint8_t print_percent_done_silent;
  310. extern uint16_t print_time_remaining_silent;
  311. #define PRINT_TIME_REMAINING_INIT 0xffff
  312. extern uint16_t mcode_in_progress;
  313. extern uint16_t gcode_in_progress;
  314. extern LongTimer safetyTimer;
  315. #define PRINT_PERCENT_DONE_INIT 0xff
  316. #define PRINTER_ACTIVE (IS_SD_PRINTING || is_usb_printing || isPrintPaused || (custom_message_type == CustomMsg::TempCal) || saved_printing || (lcd_commands_type == LcdCommands::Layer1Cal) || mmu_print_saved)
  317. //! Beware - mcode_in_progress is set as soon as the command gets really processed,
  318. //! which is not the same as posting the M600 command into the command queue
  319. //! There can be a considerable lag between posting M600 and its real processing which might result
  320. //! in posting multiple M600's into the command queue
  321. //! Instead, the fsensor uses another state variable :( , which is set to true, when the M600 command is enqued
  322. //! and is reset to false when the fsensor returns into its filament runout finished handler
  323. //! I'd normally change this macro, but who knows what would happen in the MMU :)
  324. #define CHECK_FSENSOR ((IS_SD_PRINTING || is_usb_printing) && (mcode_in_progress != 600) && !saved_printing && e_active())
  325. extern void calculate_extruder_multipliers();
  326. // Similar to the default Arduino delay function,
  327. // but it keeps the background tasks running.
  328. extern void delay_keep_alive(unsigned int ms);
  329. extern void check_babystep();
  330. extern void long_pause();
  331. extern void crashdet_stop_and_save_print();
  332. #ifdef HEATBED_ANALYSIS
  333. void d_setup();
  334. float d_ReadData();
  335. void bed_analysis(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y);
  336. void bed_check(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y);
  337. #endif //HEATBED_ANALYSIS
  338. float temp_comp_interpolation(float temperature);
  339. void show_fw_version_warnings();
  340. uint8_t check_printer_version();
  341. #ifdef PINDA_THERMISTOR
  342. float temp_compensation_pinda_thermistor_offset(float temperature_pinda);
  343. #endif //PINDA_THERMISTOR
  344. void serialecho_temperatures();
  345. bool check_commands();
  346. void uvlo_();
  347. void uvlo_tiny();
  348. void recover_print(uint8_t automatic);
  349. void setup_uvlo_interrupt();
  350. #if defined(TACH_1) && TACH_1 >-1
  351. void setup_fan_interrupt();
  352. #endif
  353. extern bool recover_machine_state_after_power_panic();
  354. extern void restore_print_from_eeprom(bool mbl_was_active);
  355. extern void position_menu();
  356. extern void print_world_coordinates();
  357. extern void print_physical_coordinates();
  358. extern void print_mesh_bed_leveling_table();
  359. extern void stop_and_save_print_to_ram(float z_move, float e_move);
  360. extern void restore_print_from_ram_and_continue(float e_move);
  361. extern void cancel_saved_printing();
  362. //estimated time to end of the print
  363. extern uint16_t print_time_remaining();
  364. extern uint8_t calc_percent_done();
  365. // States for managing Marlin and host communication
  366. // Marlin sends messages if blocked or busy
  367. /*enum MarlinBusyState {
  368. NOT_BUSY, // Not in a handler
  369. IN_HANDLER, // Processing a GCode
  370. IN_PROCESS, // Known to be blocking command input (as in G29)
  371. PAUSED_FOR_USER, // Blocking pending any input
  372. PAUSED_FOR_INPUT // Blocking pending text input (concept)
  373. };*/
  374. #define NOT_BUSY 1
  375. #define IN_HANDLER 2
  376. #define IN_PROCESS 3
  377. #define PAUSED_FOR_USER 4
  378. #define PAUSED_FOR_INPUT 5
  379. #define KEEPALIVE_STATE(n) do { busy_state = n;} while (0)
  380. extern void host_keepalive();
  381. //extern MarlinBusyState busy_state;
  382. extern int8_t busy_state;
  383. #ifdef TMC2130
  384. #define FORCE_HIGH_POWER_START force_high_power_mode(true)
  385. #define FORCE_HIGH_POWER_END force_high_power_mode(false)
  386. void force_high_power_mode(bool start_high_power_section);
  387. #endif //TMC2130
  388. // G-codes
  389. bool gcode_M45(bool onlyZ, int8_t verbosity_level);
  390. void gcode_M114();
  391. void gcode_M701();
  392. #define UVLO !(PINE & (1<<4))
  393. void proc_commands();
  394. void M600_load_filament();
  395. void M600_load_filament_movements();
  396. void M600_wait_for_user(float HotendTempBckp);
  397. void M600_check_state(float nozzle_temp);
  398. void load_filament_final_feed();
  399. void marlin_wait_for_click();
  400. void raise_z_above(float target, bool plan=true);
  401. extern "C" void softReset();
  402. #endif