My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin.pde 38KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /*
  2. Reprap firmware based on Sprinter and grbl.
  3. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. This firmware is a mashup between Sprinter and grbl.
  17. (https://github.com/kliment/Sprinter)
  18. (https://github.com/simen/grbl/tree)
  19. It has preliminary support for Matthew Roberts advance algorithm
  20. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  21. */
  22. #include "EEPROMwrite.h"
  23. #include "fastio.h"
  24. #include "Configuration.h"
  25. #include "pins.h"
  26. #include "Marlin.h"
  27. #include "ultralcd.h"
  28. #include "streaming.h"
  29. #include "planner.h"
  30. #include "stepper.h"
  31. #include "temperature.h"
  32. #ifdef SIMPLE_LCD
  33. #include "Simplelcd.h"
  34. #endif
  35. char version_string[] = "1.0.0 Alpha 1";
  36. #ifdef SDSUPPORT
  37. #include "SdFat.h"
  38. #endif //SDSUPPORT
  39. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  40. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  41. //Implemented Codes
  42. //-------------------
  43. // G0 -> G1
  44. // G1 - Coordinated Movement X Y Z E
  45. // G2 - CW ARC
  46. // G3 - CCW ARC
  47. // G4 - Dwell S<seconds> or P<milliseconds>
  48. // G28 - Home all Axis
  49. // G90 - Use Absolute Coordinates
  50. // G91 - Use Relative Coordinates
  51. // G92 - Set current position to cordinates given
  52. //RepRap M Codes
  53. // M104 - Set extruder target temp
  54. // M105 - Read current temp
  55. // M106 - Fan on
  56. // M107 - Fan off
  57. // M109 - Wait for extruder current temp to reach target temp.
  58. // M114 - Display current position
  59. //Custom M Codes
  60. // M20 - List SD card
  61. // M21 - Init SD card
  62. // M22 - Release SD card
  63. // M23 - Select SD file (M23 filename.g)
  64. // M24 - Start/resume SD print
  65. // M25 - Pause SD print
  66. // M26 - Set SD position in bytes (M26 S12345)
  67. // M27 - Report SD print status
  68. // M28 - Start SD write (M28 filename.g)
  69. // M29 - Stop SD write
  70. // M42 - Change pin status via gcode
  71. // M80 - Turn on Power Supply
  72. // M81 - Turn off Power Supply
  73. // M82 - Set E codes absolute (default)
  74. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  75. // M84 - Disable steppers until next move,
  76. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  77. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  78. // M92 - Set axis_steps_per_unit - same syntax as G92
  79. // M115 - Capabilities string
  80. // M140 - Set bed target temp
  81. // M190 - Wait for bed current temp to reach target temp.
  82. // M200 - Set filament diameter
  83. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  84. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  85. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  86. // M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) im mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer underruns and M20 minimum feedrate
  87. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  88. // M220 - set speed factor override percentage S:factor in percent
  89. // M301 - Set PID parameters P I and D
  90. // M500 - stores paramters in EEPROM
  91. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily). D
  92. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  93. //Stepper Movement Variables
  94. char axis_codes[NUM_AXIS] = {
  95. 'X', 'Y', 'Z', 'E'};
  96. float destination[NUM_AXIS] = {
  97. 0.0, 0.0, 0.0, 0.0};
  98. float current_position[NUM_AXIS] = {
  99. 0.0, 0.0, 0.0, 0.0};
  100. bool home_all_axis = true;
  101. float feedrate = 1500.0, next_feedrate, saved_feedrate;
  102. long gcode_N, gcode_LastN;
  103. float homing_feedrate[] = HOMING_FEEDRATE;
  104. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  105. bool relative_mode = false; //Determines Absolute or Relative Coordinates
  106. bool relative_mode_e = false; //Determines Absolute or Relative E Codes while in Absolute Coordinates mode. E is always relative in Relative Coordinates mode.
  107. uint8_t fanpwm=0;
  108. volatile int feedmultiply=100; //100->1 200->2
  109. int saved_feedmultiply;
  110. volatile bool feedmultiplychanged=false;
  111. // comm variables
  112. #define MAX_CMD_SIZE 96
  113. #define BUFSIZE 4
  114. char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  115. bool fromsd[BUFSIZE];
  116. int bufindr = 0;
  117. int bufindw = 0;
  118. int buflen = 0;
  119. int i = 0;
  120. char serial_char;
  121. int serial_count = 0;
  122. boolean comment_mode = false;
  123. char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  124. extern float HeaterPower;
  125. #include "EEPROM.h"
  126. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  127. float tt = 0, bt = 0;
  128. #ifdef WATCHPERIOD
  129. int watch_raw = -1000;
  130. unsigned long watchmillis = 0;
  131. #endif //WATCHPERIOD
  132. //Inactivity shutdown variables
  133. unsigned long previous_millis_cmd = 0;
  134. unsigned long max_inactive_time = 0;
  135. unsigned long stepper_inactive_time = 0;
  136. unsigned long starttime=0;
  137. unsigned long stoptime=0;
  138. #ifdef SDSUPPORT
  139. Sd2Card card;
  140. SdVolume volume;
  141. SdFile root;
  142. SdFile file;
  143. uint32_t filesize = 0;
  144. uint32_t sdpos = 0;
  145. bool sdmode = false;
  146. bool sdactive = false;
  147. bool savetosd = false;
  148. int16_t n;
  149. unsigned long autostart_atmillis=0;
  150. void initsd(){
  151. sdactive = false;
  152. #if SDSS >- 1
  153. if(root.isOpen())
  154. root.close();
  155. if (!card.init(SPI_FULL_SPEED,SDSS)){
  156. //if (!card.init(SPI_HALF_SPEED,SDSS))
  157. Serial.println("SD init fail");
  158. }
  159. else if (!volume.init(&card))
  160. Serial.println("volume.init failed");
  161. else if (!root.openRoot(&volume))
  162. Serial.println("openRoot failed");
  163. else
  164. {
  165. sdactive = true;
  166. Serial.println("SD card ok");
  167. }
  168. #endif //SDSS
  169. }
  170. void quickinitsd(){
  171. sdactive=false;
  172. autostart_atmillis=millis()+5000;
  173. }
  174. inline void write_command(char *buf){
  175. char* begin = buf;
  176. char* npos = 0;
  177. char* end = buf + strlen(buf) - 1;
  178. file.writeError = false;
  179. if((npos = strchr(buf, 'N')) != NULL){
  180. begin = strchr(npos, ' ') + 1;
  181. end = strchr(npos, '*') - 1;
  182. }
  183. end[1] = '\r';
  184. end[2] = '\n';
  185. end[3] = '\0';
  186. //Serial.println(begin);
  187. file.write(begin);
  188. if (file.writeError){
  189. Serial.println("error writing to file");
  190. }
  191. }
  192. #endif //SDSUPPORT
  193. ///adds an command to the main command buffer
  194. void enquecommand(const char *cmd)
  195. {
  196. if(buflen < BUFSIZE)
  197. {
  198. //this is dangerous if a mixing of serial and this happsens
  199. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  200. Serial.print("en:");Serial.println(cmdbuffer[bufindw]);
  201. bufindw= (bufindw + 1)%BUFSIZE;
  202. buflen += 1;
  203. }
  204. }
  205. void setup()
  206. {
  207. Serial.begin(BAUDRATE);
  208. ECHOLN("Marlin "<<version_string);
  209. Serial.println("start");
  210. #if defined FANCY_LCD || defined SIMPLE_LCD
  211. lcd_init();
  212. #endif
  213. for(int i = 0; i < BUFSIZE; i++){
  214. fromsd[i] = false;
  215. }
  216. RetrieveSettings(); // loads data from EEPROM if available
  217. for(int i=0; i < NUM_AXIS; i++){
  218. axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
  219. }
  220. #ifdef SDSUPPORT
  221. //power to SD reader
  222. #if SDPOWER > -1
  223. SET_OUTPUT(SDPOWER);
  224. WRITE(SDPOWER,HIGH);
  225. #endif //SDPOWER
  226. quickinitsd();
  227. #endif //SDSUPPORT
  228. plan_init(); // Initialize planner;
  229. st_init(); // Initialize stepper;
  230. tp_init(); // Initialize temperature loop
  231. //checkautostart();
  232. }
  233. #ifdef SDSUPPORT
  234. bool autostart_stilltocheck=true;
  235. void checkautostart(bool force)
  236. {
  237. //this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset
  238. if(!force)
  239. {
  240. if(!autostart_stilltocheck)
  241. return;
  242. if(autostart_atmillis<millis())
  243. return;
  244. }
  245. autostart_stilltocheck=false;
  246. if(!sdactive)
  247. {
  248. initsd();
  249. if(!sdactive) //fail
  250. return;
  251. }
  252. static int lastnr=0;
  253. char autoname[30];
  254. sprintf(autoname,"auto%i.g",lastnr);
  255. for(int i=0;i<(int)strlen(autoname);i++)
  256. autoname[i]=tolower(autoname[i]);
  257. dir_t p;
  258. root.rewind();
  259. //char filename[11];
  260. //int cnt=0;
  261. bool found=false;
  262. while (root.readDir(p) > 0)
  263. {
  264. for(int i=0;i<(int)strlen((char*)p.name);i++)
  265. p.name[i]=tolower(p.name[i]);
  266. //Serial.print((char*)p.name);
  267. //Serial.print(" ");
  268. //Serial.println(autoname);
  269. if(p.name[9]!='~') //skip safety copies
  270. if(strncmp((char*)p.name,autoname,5)==0)
  271. {
  272. char cmd[30];
  273. sprintf(cmd,"M23 %s",autoname);
  274. //sprintf(cmd,"M115");
  275. //enquecommand("G92 Z0");
  276. //enquecommand("G1 Z10 F2000");
  277. //enquecommand("G28 X-105 Y-105");
  278. enquecommand(cmd);
  279. enquecommand("M24");
  280. found=true;
  281. }
  282. }
  283. if(!found)
  284. lastnr=-1;
  285. else
  286. lastnr++;
  287. }
  288. #else
  289. inline void checkautostart(bool x)
  290. {
  291. }
  292. #endif
  293. void loop()
  294. {
  295. if(buflen<3)
  296. get_command();
  297. checkautostart(false);
  298. if(buflen)
  299. {
  300. #ifdef SDSUPPORT
  301. if(savetosd){
  302. if(strstr(cmdbuffer[bufindr],"M29") == NULL){
  303. write_command(cmdbuffer[bufindr]);
  304. Serial.println("ok");
  305. }
  306. else{
  307. file.sync();
  308. file.close();
  309. savetosd = false;
  310. Serial.println("Done saving file.");
  311. }
  312. }
  313. else{
  314. process_commands();
  315. }
  316. #else
  317. process_commands();
  318. #endif //SDSUPPORT
  319. buflen = (buflen-1);
  320. bufindr = (bufindr + 1)%BUFSIZE;
  321. }
  322. //check heater every n milliseconds
  323. manage_heater();
  324. manage_inactivity(1);
  325. LCD_STATUS;
  326. }
  327. inline void get_command()
  328. {
  329. while( Serial.available() > 0 && buflen < BUFSIZE) {
  330. serial_char = Serial.read();
  331. if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) )
  332. {
  333. if(!serial_count) return; //if empty line
  334. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  335. if(!comment_mode){
  336. fromsd[bufindw] = false;
  337. if(strstr(cmdbuffer[bufindw], "N") != NULL)
  338. {
  339. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  340. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  341. if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
  342. Serial.print("Serial Error: Line Number is not Last Line Number+1, Last Line:");
  343. Serial.println(gcode_LastN);
  344. //Serial.println(gcode_N);
  345. FlushSerialRequestResend();
  346. serial_count = 0;
  347. return;
  348. }
  349. if(strstr(cmdbuffer[bufindw], "*") != NULL)
  350. {
  351. byte checksum = 0;
  352. byte count = 0;
  353. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  354. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  355. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  356. Serial.print("Error: checksum mismatch, Last Line:");
  357. Serial.println(gcode_LastN);
  358. FlushSerialRequestResend();
  359. serial_count = 0;
  360. return;
  361. }
  362. //if no errors, continue parsing
  363. }
  364. else
  365. {
  366. Serial.print("Error: No Checksum with line number, Last Line:");
  367. Serial.println(gcode_LastN);
  368. FlushSerialRequestResend();
  369. serial_count = 0;
  370. return;
  371. }
  372. gcode_LastN = gcode_N;
  373. //if no errors, continue parsing
  374. }
  375. else // if we don't receive 'N' but still see '*'
  376. {
  377. if((strstr(cmdbuffer[bufindw], "*") != NULL))
  378. {
  379. Serial.print("Error: No Line Number with checksum, Last Line:");
  380. Serial.println(gcode_LastN);
  381. serial_count = 0;
  382. return;
  383. }
  384. }
  385. if((strstr(cmdbuffer[bufindw], "G") != NULL)){
  386. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  387. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  388. case 0:
  389. case 1:
  390. #ifdef SDSUPPORT
  391. if(savetosd)
  392. break;
  393. #endif //SDSUPPORT
  394. Serial.println("ok");
  395. break;
  396. default:
  397. break;
  398. }
  399. }
  400. bufindw = (bufindw + 1)%BUFSIZE;
  401. buflen += 1;
  402. }
  403. comment_mode = false; //for new command
  404. serial_count = 0; //clear buffer
  405. }
  406. else
  407. {
  408. if(serial_char == ';') comment_mode = true;
  409. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  410. }
  411. }
  412. #ifdef SDSUPPORT
  413. if(!sdmode || serial_count!=0){
  414. return;
  415. }
  416. while( filesize > sdpos && buflen < BUFSIZE) {
  417. n = file.read();
  418. serial_char = (char)n;
  419. if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) || n == -1)
  420. {
  421. sdpos = file.curPosition();
  422. if(sdpos >= filesize){
  423. sdmode = false;
  424. Serial.println("Done printing file");
  425. stoptime=millis();
  426. char time[30];
  427. unsigned long t=(stoptime-starttime)/1000;
  428. int sec,min;
  429. min=t/60;
  430. sec=t%60;
  431. sprintf(time,"%i min, %i sec",min,sec);
  432. Serial.println(time);
  433. LCD_MESSAGE(time);
  434. checkautostart(true);
  435. }
  436. if(!serial_count) return; //if empty line
  437. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  438. if(!comment_mode){
  439. fromsd[bufindw] = true;
  440. buflen += 1;
  441. bufindw = (bufindw + 1)%BUFSIZE;
  442. }
  443. comment_mode = false; //for new command
  444. serial_count = 0; //clear buffer
  445. }
  446. else
  447. {
  448. if(serial_char == ';') comment_mode = true;
  449. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  450. }
  451. }
  452. #endif //SDSUPPORT
  453. }
  454. inline float code_value() {
  455. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  456. }
  457. inline long code_value_long() {
  458. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  459. }
  460. inline bool code_seen(char code_string[]) {
  461. return (strstr(cmdbuffer[bufindr], code_string) != NULL);
  462. } //Return True if the string was found
  463. inline bool code_seen(char code)
  464. {
  465. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  466. return (strchr_pointer != NULL); //Return True if a character was found
  467. }
  468. inline void process_commands()
  469. {
  470. unsigned long codenum; //throw away variable
  471. char *starpos = NULL;
  472. if(code_seen('G'))
  473. {
  474. switch((int)code_value())
  475. {
  476. case 0: // G0 -> G1
  477. case 1: // G1
  478. get_coordinates(); // For X Y Z E F
  479. prepare_move();
  480. previous_millis_cmd = millis();
  481. //ClearToSend();
  482. return;
  483. //break;
  484. case 4: // G4 dwell
  485. codenum = 0;
  486. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  487. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  488. codenum += millis(); // keep track of when we started waiting
  489. while(millis() < codenum ){
  490. manage_heater();
  491. }
  492. break;
  493. case 28: //G28 Home all Axis one at a time
  494. saved_feedrate = feedrate;
  495. saved_feedmultiply = feedmultiply;
  496. feedmultiply = 100;
  497. for(int i=0; i < NUM_AXIS; i++) {
  498. destination[i] = current_position[i];
  499. }
  500. feedrate = 0.0;
  501. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  502. if((home_all_axis) || (code_seen(axis_codes[X_AXIS]))) {
  503. if ((X_MIN_PIN > -1 && X_HOME_DIR==-1) || (X_MAX_PIN > -1 && X_HOME_DIR==1)){
  504. // st_synchronize();
  505. current_position[X_AXIS] = 0;
  506. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  507. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;
  508. feedrate = homing_feedrate[X_AXIS];
  509. prepare_move();
  510. // st_synchronize();
  511. current_position[X_AXIS] = 0;
  512. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  513. destination[X_AXIS] = -5 * X_HOME_DIR;
  514. prepare_move();
  515. // st_synchronize();
  516. destination[X_AXIS] = 10 * X_HOME_DIR;
  517. feedrate = homing_feedrate[X_AXIS]/2 ;
  518. prepare_move();
  519. // st_synchronize();
  520. current_position[X_AXIS] = (X_HOME_DIR == -1) ? 0 : X_MAX_LENGTH;
  521. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  522. destination[X_AXIS] = current_position[X_AXIS];
  523. feedrate = 0.0;
  524. }
  525. }
  526. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  527. if ((Y_MIN_PIN > -1 && Y_HOME_DIR==-1) || (Y_MAX_PIN > -1 && Y_HOME_DIR==1)){
  528. current_position[Y_AXIS] = 0;
  529. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  530. destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  531. feedrate = homing_feedrate[Y_AXIS];
  532. prepare_move();
  533. // st_synchronize();
  534. current_position[Y_AXIS] = 0;
  535. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  536. destination[Y_AXIS] = -5 * Y_HOME_DIR;
  537. prepare_move();
  538. // st_synchronize();
  539. destination[Y_AXIS] = 10 * Y_HOME_DIR;
  540. feedrate = homing_feedrate[Y_AXIS]/2;
  541. prepare_move();
  542. // st_synchronize();
  543. current_position[Y_AXIS] = (Y_HOME_DIR == -1) ? 0 : Y_MAX_LENGTH;
  544. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  545. destination[Y_AXIS] = current_position[Y_AXIS];
  546. feedrate = 0.0;
  547. }
  548. }
  549. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  550. if ((Z_MIN_PIN > -1 && Z_HOME_DIR==-1) || (Z_MAX_PIN > -1 && Z_HOME_DIR==1)){
  551. current_position[Z_AXIS] = 0;
  552. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  553. destination[Z_AXIS] = 1.5 * Z_MAX_LENGTH * Z_HOME_DIR;
  554. feedrate = homing_feedrate[Z_AXIS];
  555. prepare_move();
  556. // st_synchronize();
  557. current_position[Z_AXIS] = 0;
  558. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  559. destination[Z_AXIS] = -2 * Z_HOME_DIR;
  560. prepare_move();
  561. // st_synchronize();
  562. destination[Z_AXIS] = 3 * Z_HOME_DIR;
  563. feedrate = homing_feedrate[Z_AXIS]/2;
  564. prepare_move();
  565. // st_synchronize();
  566. current_position[Z_AXIS] = (Z_HOME_DIR == -1) ? 0 : Z_MAX_LENGTH;
  567. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  568. destination[Z_AXIS] = current_position[Z_AXIS];
  569. feedrate = 0.0;
  570. }
  571. }
  572. feedrate = saved_feedrate;
  573. feedmultiply = saved_feedmultiply;
  574. previous_millis_cmd = millis();
  575. break;
  576. case 90: // G90
  577. relative_mode = false;
  578. break;
  579. case 91: // G91
  580. relative_mode = true;
  581. break;
  582. case 92: // G92
  583. if(!code_seen(axis_codes[E_AXIS]))
  584. st_synchronize();
  585. for(int i=0; i < NUM_AXIS; i++) {
  586. if(code_seen(axis_codes[i])) current_position[i] = code_value();
  587. }
  588. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  589. break;
  590. }
  591. }
  592. else if(code_seen('M'))
  593. {
  594. switch( (int)code_value() )
  595. {
  596. #ifdef SDSUPPORT
  597. case 20: // M20 - list SD card
  598. Serial.println("Begin file list");
  599. root.ls();
  600. Serial.println("End file list");
  601. break;
  602. case 21: // M21 - init SD card
  603. sdmode = false;
  604. initsd();
  605. break;
  606. case 22: //M22 - release SD card
  607. sdmode = false;
  608. sdactive = false;
  609. break;
  610. case 23: //M23 - Select file
  611. if(sdactive){
  612. sdmode = false;
  613. file.close();
  614. starpos = (strchr(strchr_pointer + 4,'*'));
  615. if(starpos!=NULL)
  616. *(starpos-1)='\0';
  617. if (file.open(&root, strchr_pointer + 4, O_READ)) {
  618. Serial.print("File opened:");
  619. Serial.print(strchr_pointer + 4);
  620. Serial.print(" Size:");
  621. Serial.println(file.fileSize());
  622. sdpos = 0;
  623. filesize = file.fileSize();
  624. Serial.println("File selected");
  625. }
  626. else{
  627. Serial.println("file.open failed");
  628. }
  629. }
  630. break;
  631. case 24: //M24 - Start SD print
  632. if(sdactive){
  633. sdmode = true;
  634. starttime=millis();
  635. }
  636. break;
  637. case 25: //M25 - Pause SD print
  638. if(sdmode){
  639. sdmode = false;
  640. }
  641. break;
  642. case 26: //M26 - Set SD index
  643. if(sdactive && code_seen('S')){
  644. sdpos = code_value_long();
  645. file.seekSet(sdpos);
  646. }
  647. break;
  648. case 27: //M27 - Get SD status
  649. if(sdactive){
  650. Serial.print("SD printing byte ");
  651. Serial.print(sdpos);
  652. Serial.print("/");
  653. Serial.println(filesize);
  654. }
  655. else{
  656. Serial.println("Not SD printing");
  657. }
  658. break;
  659. case 28: //M28 - Start SD write
  660. if(sdactive){
  661. char* npos = 0;
  662. file.close();
  663. sdmode = false;
  664. starpos = (strchr(strchr_pointer + 4,'*'));
  665. if(starpos != NULL){
  666. npos = strchr(cmdbuffer[bufindr], 'N');
  667. strchr_pointer = strchr(npos,' ') + 1;
  668. *(starpos-1) = '\0';
  669. }
  670. if (!file.open(&root, strchr_pointer+4, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
  671. {
  672. Serial.print("open failed, File: ");
  673. Serial.print(strchr_pointer + 4);
  674. Serial.print(".");
  675. }
  676. else{
  677. savetosd = true;
  678. Serial.print("Writing to file: ");
  679. Serial.println(strchr_pointer + 4);
  680. }
  681. }
  682. break;
  683. case 29: //M29 - Stop SD write
  684. //processed in write to file routine above
  685. //savetosd = false;
  686. break;
  687. case 30:
  688. {
  689. stoptime=millis();
  690. char time[30];
  691. unsigned long t=(stoptime-starttime)/1000;
  692. int sec,min;
  693. min=t/60;
  694. sec=t%60;
  695. sprintf(time,"%i min, %i sec",min,sec);
  696. Serial.println(time);
  697. LCD_MESSAGE(time);
  698. }
  699. break;
  700. #endif //SDSUPPORT
  701. case 42: //M42 -Change pin status via gcode
  702. if (code_seen('S'))
  703. {
  704. int pin_status = code_value();
  705. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  706. {
  707. int pin_number = code_value();
  708. for(int i = 0; i < (int)sizeof(sensitive_pins); i++)
  709. {
  710. if (sensitive_pins[i] == pin_number)
  711. {
  712. pin_number = -1;
  713. break;
  714. }
  715. }
  716. if (pin_number > -1)
  717. {
  718. pinMode(pin_number, OUTPUT);
  719. digitalWrite(pin_number, pin_status);
  720. analogWrite(pin_number, pin_status);
  721. }
  722. }
  723. }
  724. break;
  725. case 104: // M104
  726. if (code_seen('S')) target_raw[TEMPSENSOR_HOTEND_0] = temp2analog(code_value());
  727. #ifdef PIDTEMP
  728. pid_setpoint = code_value();
  729. #endif //PIDTEM
  730. #ifdef WATCHPERIOD
  731. if(target_raw[TEMPSENSOR_HOTEND_0] > current_raw[TEMPSENSOR_HOTEND_0]){
  732. watchmillis = max(1,millis());
  733. watch_raw[TEMPSENSOR_HOTEND_0] = current_raw[TEMPSENSOR_HOTEND_0];
  734. }else{
  735. watchmillis = 0;
  736. }
  737. #endif
  738. break;
  739. case 140: // M140 set bed temp
  740. if (code_seen('S')) target_raw[TEMPSENSOR_BED] = temp2analogBed(code_value());
  741. break;
  742. case 105: // M105
  743. #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
  744. tt = analog2temp(current_raw[TEMPSENSOR_HOTEND_0]);
  745. #endif
  746. #if TEMP_1_PIN > -1
  747. bt = analog2tempBed(current_raw[TEMPSENSOR_BED]);
  748. #endif
  749. #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
  750. Serial.print("ok T:");
  751. Serial.print(tt);
  752. // Serial.print(", raw:");
  753. // Serial.print(current_raw);
  754. #if TEMP_1_PIN > -1
  755. #ifdef PIDTEMP
  756. Serial.print(" B:");
  757. #if TEMP_1_PIN > -1
  758. Serial.println(bt);
  759. #else
  760. Serial.println(HeaterPower);
  761. #endif
  762. #else
  763. Serial.println();
  764. #endif
  765. #else
  766. Serial.println();
  767. #endif
  768. #else
  769. Serial.println("No thermistors - no temp");
  770. #endif
  771. return;
  772. //break;
  773. case 109: {// M109 - Wait for extruder heater to reach target.
  774. LCD_MESSAGE("Heating...");
  775. if (code_seen('S')) target_raw[TEMPSENSOR_HOTEND_0] = temp2analog(code_value());
  776. #ifdef PIDTEMP
  777. pid_setpoint = code_value();
  778. #endif //PIDTEM
  779. #ifdef WATCHPERIOD
  780. if(target_raw[TEMPSENSOR_HOTEND_0]>current_raw[TEMPSENSOR_HOTEND_0]){
  781. watchmillis = max(1,millis());
  782. watch_raw[TEMPSENSOR_HOTEND_0] = current_raw[TEMPSENSOR_HOTEND_0];
  783. } else {
  784. watchmillis = 0;
  785. }
  786. #endif //WATCHPERIOD
  787. codenum = millis();
  788. /* See if we are heating up or cooling down */
  789. bool target_direction = (current_raw[TEMPSENSOR_HOTEND_0] < target_raw[TEMPSENSOR_HOTEND_0]); // true if heating, false if cooling
  790. #ifdef TEMP_RESIDENCY_TIME
  791. long residencyStart;
  792. residencyStart = -1;
  793. /* continue to loop until we have reached the target temp
  794. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  795. while((target_direction ? (current_raw[TEMPSENSOR_HOTEND_0] < target_raw[TEMPSENSOR_HOTEND_0]) : (current_raw[TEMPSENSOR_HOTEND_0] > target_raw[TEMPSENSOR_HOTEND_0])) ||
  796. (residencyStart > -1 && (millis() - residencyStart) < TEMP_RESIDENCY_TIME*1000) ) {
  797. #else
  798. while ( target_direction ? (current_raw[TEMPSENSOR_HOTEND_0] < target_raw[TEMPSENSOR_HOTEND_0]) : (current_raw[TEMPSENSOR_HOTEND_0] > target_raw[TEMPSENSOR_HOTEND_0]) ) {
  799. #endif //TEMP_RESIDENCY_TIME
  800. if( (millis() - codenum) > 1000 ) { //Print Temp Reading every 1 second while heating up/cooling down
  801. Serial.print("T:");
  802. Serial.println( analog2temp(current_raw[TEMPSENSOR_HOTEND_0]) );
  803. codenum = millis();
  804. }
  805. manage_heater();
  806. LCD_STATUS;
  807. #ifdef TEMP_RESIDENCY_TIME
  808. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  809. or when current temp falls outside the hysteresis after target temp was reached */
  810. if ((residencyStart == -1 && target_direction && current_raw[TEMPSENSOR_HOTEND_0] >= target_raw[TEMPSENSOR_HOTEND_0]) ||
  811. (residencyStart == -1 && !target_direction && current_raw[TEMPSENSOR_HOTEND_0] <= target_raw[TEMPSENSOR_HOTEND_0]) ||
  812. (residencyStart > -1 && labs(analog2temp(current_raw[TEMPSENSOR_HOTEND_0]) - analog2temp(target_raw[TEMPSENSOR_HOTEND_0])) > TEMP_HYSTERESIS) ) {
  813. residencyStart = millis();
  814. }
  815. #endif //TEMP_RESIDENCY_TIME
  816. }
  817. LCD_MESSAGE("Marlin ready.");
  818. }
  819. break;
  820. case 190: // M190 - Wait bed for heater to reach target.
  821. #if TEMP_1_PIN > -1
  822. if (code_seen('S')) target_raw[TEMPSENSOR_BED] = temp2analog(code_value());
  823. codenum = millis();
  824. while(current_raw[TEMPSENSOR_BED] < target_raw[TEMPSENSOR_BED])
  825. {
  826. if( (millis()-codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  827. {
  828. float tt=analog2temp(current_raw[TEMPSENSOR_HOTEND_0]);
  829. Serial.print("T:");
  830. Serial.println( tt );
  831. Serial.print("ok T:");
  832. Serial.print( tt );
  833. Serial.print(" B:");
  834. Serial.println( analog2temp(current_raw[TEMPSENSOR_BED]) );
  835. codenum = millis();
  836. }
  837. manage_heater();
  838. }
  839. #endif
  840. break;
  841. #if FAN_PIN > -1
  842. case 106: //M106 Fan On
  843. if (code_seen('S')){
  844. WRITE(FAN_PIN,HIGH);
  845. fanpwm=constrain(code_value(),0,255);
  846. analogWrite(FAN_PIN, fanpwm);
  847. }
  848. else {
  849. WRITE(FAN_PIN,HIGH);
  850. fanpwm=255;
  851. analogWrite(FAN_PIN, fanpwm);
  852. }
  853. break;
  854. case 107: //M107 Fan Off
  855. WRITE(FAN_PIN,LOW);
  856. analogWrite(FAN_PIN, 0);
  857. break;
  858. #endif
  859. #if (PS_ON_PIN > -1)
  860. case 80: // M80 - ATX Power On
  861. SET_OUTPUT(PS_ON_PIN); //GND
  862. break;
  863. case 81: // M81 - ATX Power Off
  864. SET_INPUT(PS_ON_PIN); //Floating
  865. break;
  866. #endif
  867. case 82:
  868. axis_relative_modes[3] = false;
  869. break;
  870. case 83:
  871. axis_relative_modes[3] = true;
  872. break;
  873. case 18:
  874. case 84:
  875. if(code_seen('S')){
  876. stepper_inactive_time = code_value() * 1000;
  877. }
  878. else{
  879. st_synchronize();
  880. disable_x();
  881. disable_y();
  882. disable_z();
  883. disable_e();
  884. }
  885. break;
  886. case 85: // M85
  887. code_seen('S');
  888. max_inactive_time = code_value() * 1000;
  889. break;
  890. case 92: // M92
  891. for(int i=0; i < NUM_AXIS; i++) {
  892. if(code_seen(axis_codes[i])) axis_steps_per_unit[i] = code_value();
  893. }
  894. break;
  895. case 115: // M115
  896. Serial.println("FIRMWARE_NAME:Sprinter/grbl mashup for gen6 FIRMWARE_URL:http://www.mendel-parts.com PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:1");
  897. break;
  898. case 114: // M114
  899. Serial.print("X:");
  900. Serial.print(current_position[X_AXIS]);
  901. Serial.print("Y:");
  902. Serial.print(current_position[Y_AXIS]);
  903. Serial.print("Z:");
  904. Serial.print(current_position[Z_AXIS]);
  905. Serial.print("E:");
  906. Serial.print(current_position[E_AXIS]);
  907. #ifdef DEBUG_STEPS
  908. Serial.print(" Count X:");
  909. Serial.print(float(count_position[X_AXIS])/axis_steps_per_unit[X_AXIS]);
  910. Serial.print("Y:");
  911. Serial.print(float(count_position[Y_AXIS])/axis_steps_per_unit[Y_AXIS]);
  912. Serial.print("Z:");
  913. Serial.println(float(count_position[Z_AXIS])/axis_steps_per_unit[Z_AXIS]);
  914. #endif
  915. Serial.println("");
  916. break;
  917. case 119: // M119
  918. #if (X_MIN_PIN > -1)
  919. Serial.print("x_min:");
  920. Serial.print((READ(X_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  921. #endif
  922. #if (X_MAX_PIN > -1)
  923. Serial.print("x_max:");
  924. Serial.print((READ(X_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  925. #endif
  926. #if (Y_MIN_PIN > -1)
  927. Serial.print("y_min:");
  928. Serial.print((READ(Y_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  929. #endif
  930. #if (Y_MAX_PIN > -1)
  931. Serial.print("y_max:");
  932. Serial.print((READ(Y_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  933. #endif
  934. #if (Z_MIN_PIN > -1)
  935. Serial.print("z_min:");
  936. Serial.print((READ(Z_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  937. #endif
  938. #if (Z_MAX_PIN > -1)
  939. Serial.print("z_max:");
  940. Serial.print((READ(Z_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
  941. #endif
  942. Serial.println("");
  943. break;
  944. //TODO: update for all axis, use for loop
  945. case 201: // M201
  946. for(int i=0; i < NUM_AXIS; i++) {
  947. if(code_seen(axis_codes[i])) axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  948. }
  949. break;
  950. #if 0 // Not used for Sprinter/grbl gen6
  951. case 202: // M202
  952. for(int i=0; i < NUM_AXIS; i++) {
  953. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  954. }
  955. break;
  956. #endif
  957. case 203: // M203 max feedrate mm/sec
  958. for(int i=0; i < NUM_AXIS; i++) {
  959. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value()*60 ;
  960. }
  961. break;
  962. case 204: // M204 acclereration S normal moves T filmanent only moves
  963. {
  964. if(code_seen('S')) acceleration = code_value() ;
  965. if(code_seen('T')) retract_acceleration = code_value() ;
  966. }
  967. break;
  968. case 205: //M205 advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  969. {
  970. if(code_seen('S')) minimumfeedrate = code_value()*60 ;
  971. if(code_seen('T')) mintravelfeedrate = code_value()*60 ;
  972. if(code_seen('B')) minsegmenttime = code_value() ;
  973. if(code_seen('X')) max_xy_jerk = code_value()*60 ;
  974. if(code_seen('Z')) max_z_jerk = code_value()*60 ;
  975. }
  976. break;
  977. case 220: // M220 S<factor in percent>- set speed factor override percentage
  978. {
  979. if(code_seen('S'))
  980. {
  981. feedmultiply = code_value() ;
  982. feedmultiplychanged=true;
  983. }
  984. }
  985. break;
  986. #ifdef PIDTEMP
  987. case 301: // M301
  988. if(code_seen('P')) Kp = code_value();
  989. if(code_seen('I')) Ki = code_value()*PID_dT;
  990. if(code_seen('D')) Kd = code_value()/PID_dT;
  991. // ECHOLN("Kp "<<_FLOAT(Kp,2));
  992. // ECHOLN("Ki "<<_FLOAT(Ki/PID_dT,2));
  993. // ECHOLN("Kd "<<_FLOAT(Kd*PID_dT,2));
  994. // temp_iState_min = 0.0;
  995. // if (Ki!=0) {
  996. // temp_iState_max = PID_INTEGRAL_DRIVE_MAX / (Ki/100.0);
  997. // }
  998. // else temp_iState_max = 1.0e10;
  999. break;
  1000. #endif //PIDTEMP
  1001. case 500: // Store settings in EEPROM
  1002. {
  1003. StoreSettings();
  1004. }
  1005. break;
  1006. case 501: // Read settings from EEPROM
  1007. {
  1008. RetrieveSettings();
  1009. }
  1010. break;
  1011. case 502: // Revert to default settings
  1012. {
  1013. RetrieveSettings(true);
  1014. }
  1015. break;
  1016. }
  1017. }
  1018. else{
  1019. Serial.println("Unknown command:");
  1020. Serial.println(cmdbuffer[bufindr]);
  1021. }
  1022. ClearToSend();
  1023. }
  1024. void FlushSerialRequestResend()
  1025. {
  1026. //char cmdbuffer[bufindr][100]="Resend:";
  1027. Serial.flush();
  1028. Serial.print("Resend:");
  1029. Serial.println(gcode_LastN + 1);
  1030. ClearToSend();
  1031. }
  1032. void ClearToSend()
  1033. {
  1034. previous_millis_cmd = millis();
  1035. #ifdef SDSUPPORT
  1036. if(fromsd[bufindr])
  1037. return;
  1038. #endif //SDSUPPORT
  1039. Serial.println("ok");
  1040. }
  1041. inline void get_coordinates()
  1042. {
  1043. for(int i=0; i < NUM_AXIS; i++) {
  1044. if(code_seen(axis_codes[i])) destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1045. else destination[i] = current_position[i]; //Are these else lines really needed?
  1046. }
  1047. if(code_seen('F')) {
  1048. next_feedrate = code_value();
  1049. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1050. }
  1051. }
  1052. void prepare_move()
  1053. {
  1054. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60.0/100.0);
  1055. for(int i=0; i < NUM_AXIS; i++) {
  1056. current_position[i] = destination[i];
  1057. }
  1058. }
  1059. #ifdef USE_WATCHDOG
  1060. #include <avr/wdt.h>
  1061. #include <avr/interrupt.h>
  1062. volatile uint8_t timeout_seconds=0;
  1063. void(* ctrlaltdelete) (void) = 0;
  1064. ISR(WDT_vect) { //Watchdog timer interrupt, called if main program blocks >1sec
  1065. if(timeout_seconds++ >= WATCHDOG_TIMEOUT)
  1066. {
  1067. kill();
  1068. #ifdef RESET_MANUAL
  1069. LCD_MESSAGE("Please Reset!");
  1070. ECHOLN("echo_: Something is wrong, please turn off the printer.");
  1071. #else
  1072. LCD_MESSAGE("Timeout, resetting!");
  1073. #endif
  1074. //disable watchdog, it will survife reboot.
  1075. WDTCSR |= (1<<WDCE) | (1<<WDE);
  1076. WDTCSR = 0;
  1077. #ifdef RESET_MANUAL
  1078. while(1); //wait for user or serial reset
  1079. #else
  1080. ctrlaltdelete();
  1081. #endif
  1082. }
  1083. }
  1084. /// intialise watch dog with a 1 sec interrupt time
  1085. void wd_init() {
  1086. WDTCSR = (1<<WDCE )|(1<<WDE ); //allow changes
  1087. WDTCSR = (1<<WDIF)|(1<<WDIE)| (1<<WDCE )|(1<<WDE )| (1<<WDP2 )|(1<<WDP1)|(0<<WDP0);
  1088. }
  1089. /// reset watchdog. MUST be called every 1s after init or avr will reset.
  1090. void wd_reset() {
  1091. wdt_reset();
  1092. timeout_seconds=0; //reset counter for resets
  1093. }
  1094. #endif /* USE_WATCHDOG */
  1095. inline void kill()
  1096. {
  1097. #if TEMP_0_PIN > -1
  1098. target_raw[0]=0;
  1099. #if HEATER_0_PIN > -1
  1100. WRITE(HEATER_0_PIN,LOW);
  1101. #endif
  1102. #endif
  1103. #if TEMP_1_PIN > -1
  1104. target_raw[1]=0;
  1105. #if HEATER_1_PIN > -1
  1106. WRITE(HEATER_1_PIN,LOW);
  1107. #endif
  1108. #endif
  1109. #if TEMP_2_PIN > -1
  1110. target_raw[2]=0;
  1111. #if HEATER_2_PIN > -1
  1112. WRITE(HEATER_2_PIN,LOW);
  1113. #endif
  1114. #endif
  1115. disable_x();
  1116. disable_y();
  1117. disable_z();
  1118. disable_e();
  1119. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1120. Serial.println("!! Printer halted. kill() called!!");
  1121. while(1); // Wait for reset
  1122. }
  1123. void manage_inactivity(byte debug) {
  1124. if( (millis()-previous_millis_cmd) > max_inactive_time ) if(max_inactive_time) kill();
  1125. if( (millis()-previous_millis_cmd) > stepper_inactive_time ) if(stepper_inactive_time) {
  1126. disable_x();
  1127. disable_y();
  1128. disable_z();
  1129. disable_e();
  1130. }
  1131. check_axes_activity();
  1132. }