Marlin.h 14 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. #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. #ifdef SYSTEM_TIMER_2
  222. extern volatile unsigned long timer2_millis;
  223. #else //SYSTEM_TIMER_2
  224. extern volatile unsigned long timer0_millis;
  225. #endif //SYSTEM_TIMER_2
  226. // An unsynchronized equivalent to a standard Arduino _millis() function.
  227. // To be used inside an interrupt routine.
  228. FORCE_INLINE unsigned long millis_nc() {
  229. #ifdef SYSTEM_TIMER_2
  230. return timer2_millis;
  231. #else //SYSTEM_TIMER_2
  232. return timer0_millis;
  233. #endif //SYSTEM_TIMER_2
  234. }
  235. #ifdef FAST_PWM_FAN
  236. void setPwmFrequency(uint8_t pin, int val);
  237. #endif
  238. #ifndef CRITICAL_SECTION_START
  239. #define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli();
  240. #define CRITICAL_SECTION_END SREG = _sreg;
  241. #endif //CRITICAL_SECTION_START
  242. extern float homing_feedrate[];
  243. extern bool axis_relative_modes[];
  244. extern int feedmultiply;
  245. extern int extrudemultiply; // Sets extrude multiply factor (in percent) for all extruders
  246. extern int extruder_multiply[EXTRUDERS]; // sets extrude multiply factor (in percent) for each extruder individually
  247. extern float volumetric_multiplier[EXTRUDERS]; // reciprocal of cross-sectional area of filament (in square millimeters), stored this way to reduce computational burden in planner
  248. extern float current_position[NUM_AXIS] ;
  249. extern float destination[NUM_AXIS] ;
  250. extern float min_pos[3];
  251. extern float max_pos[3];
  252. extern bool axis_known_position[3];
  253. extern int fanSpeed;
  254. extern void homeaxis(int axis, uint8_t cnt = 1, uint8_t* pstep = 0);
  255. extern int8_t lcd_change_fil_state;
  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. #ifdef HOST_KEEPALIVE_FEATURE
  265. extern uint8_t host_keepalive_interval;
  266. #endif
  267. extern unsigned long starttime;
  268. extern unsigned long stoptime;
  269. extern int bowden_length[4];
  270. extern bool is_usb_printing;
  271. extern bool homing_flag;
  272. extern bool temp_cal_active;
  273. extern bool loading_flag;
  274. extern unsigned int usb_printing_counter;
  275. extern unsigned long kicktime;
  276. extern unsigned long total_filament_used;
  277. void save_statistics(unsigned long _total_filament_used, unsigned long _total_print_time);
  278. extern unsigned int heating_status;
  279. extern unsigned int status_number;
  280. extern unsigned int heating_status_counter;
  281. extern char snmm_filaments_used;
  282. extern unsigned long PingTime;
  283. extern unsigned long NcTime;
  284. extern bool no_response;
  285. extern uint8_t important_status;
  286. extern uint8_t saved_filament_type;
  287. extern bool fan_state[2];
  288. extern int fan_edge_counter[2];
  289. extern int fan_speed[2];
  290. // Handling multiple extruders pins
  291. extern uint8_t active_extruder;
  292. #endif
  293. //Long pause
  294. extern unsigned long pause_time;
  295. extern unsigned long start_pause_print;
  296. extern unsigned long t_fan_rising_edge;
  297. extern bool mesh_bed_leveling_flag;
  298. extern bool mesh_bed_run_from_menu;
  299. extern bool sortAlpha;
  300. extern char dir_names[3][9];
  301. extern int8_t lcd_change_fil_state;
  302. // save/restore printing
  303. extern bool saved_printing;
  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 bool wizard_active; //autoload temporarily disabled during wizard
  315. extern LongTimer safetyTimer;
  316. #define PRINT_PERCENT_DONE_INIT 0xff
  317. #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)
  318. extern void calculate_extruder_multipliers();
  319. // Similar to the default Arduino delay function,
  320. // but it keeps the background tasks running.
  321. extern void delay_keep_alive(unsigned int ms);
  322. extern void check_babystep();
  323. extern void long_pause();
  324. extern void crashdet_stop_and_save_print();
  325. #ifdef DIS
  326. void d_setup();
  327. float d_ReadData();
  328. void bed_analysis(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y);
  329. #endif
  330. float temp_comp_interpolation(float temperature);
  331. void temp_compensation_apply();
  332. void temp_compensation_start();
  333. void show_fw_version_warnings();
  334. uint8_t check_printer_version();
  335. #ifdef PINDA_THERMISTOR
  336. float temp_compensation_pinda_thermistor_offset(float temperature_pinda);
  337. #endif //PINDA_THERMISTOR
  338. void serialecho_temperatures();
  339. bool check_commands();
  340. void uvlo_();
  341. void uvlo_tiny();
  342. void recover_print(uint8_t automatic);
  343. void setup_uvlo_interrupt();
  344. #if defined(TACH_1) && TACH_1 >-1
  345. void setup_fan_interrupt();
  346. #endif
  347. //extern void recover_machine_state_after_power_panic();
  348. extern void recover_machine_state_after_power_panic(bool bTiny);
  349. extern void restore_print_from_eeprom();
  350. extern void position_menu();
  351. extern void print_world_coordinates();
  352. extern void print_physical_coordinates();
  353. extern void print_mesh_bed_leveling_table();
  354. extern void stop_and_save_print_to_ram(float z_move, float e_move);
  355. extern void restore_print_from_ram_and_continue(float e_move);
  356. //estimated time to end of the print
  357. extern uint16_t print_time_remaining();
  358. extern uint8_t calc_percent_done();
  359. #ifdef HOST_KEEPALIVE_FEATURE
  360. // States for managing Marlin and host communication
  361. // Marlin sends messages if blocked or busy
  362. /*enum MarlinBusyState {
  363. NOT_BUSY, // Not in a handler
  364. IN_HANDLER, // Processing a GCode
  365. IN_PROCESS, // Known to be blocking command input (as in G29)
  366. PAUSED_FOR_USER, // Blocking pending any input
  367. PAUSED_FOR_INPUT // Blocking pending text input (concept)
  368. };*/
  369. #define NOT_BUSY 1
  370. #define IN_HANDLER 2
  371. #define IN_PROCESS 3
  372. #define PAUSED_FOR_USER 4
  373. #define PAUSED_FOR_INPUT 5
  374. #define KEEPALIVE_STATE(n) do { busy_state = n;} while (0)
  375. extern void host_keepalive();
  376. //extern MarlinBusyState busy_state;
  377. extern int busy_state;
  378. #endif //HOST_KEEPALIVE_FEATURE
  379. #ifdef TMC2130
  380. #define FORCE_HIGH_POWER_START force_high_power_mode(true)
  381. #define FORCE_HIGH_POWER_END force_high_power_mode(false)
  382. void force_high_power_mode(bool start_high_power_section);
  383. #endif //TMC2130
  384. // G-codes
  385. 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);
  386. void gcode_G28(bool home_x_axis, bool home_y_axis, bool home_z_axis);
  387. bool gcode_M45(bool onlyZ, int8_t verbosity_level);
  388. void gcode_M114();
  389. void gcode_M701();
  390. #define UVLO !(PINE & (1<<4))
  391. void proc_commands();
  392. void M600_load_filament();
  393. void M600_load_filament_movements();
  394. void M600_wait_for_user(float HotendTempBckp);
  395. void M600_check_state();
  396. void load_filament_final_feed();