Marlin.h 14 KB

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