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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. stepper.c - stepper motor driver: executes motion plans using stepper motors
  3. Part of Grbl
  4. Copyright (c) 2009-2011 Simen Svale Skogsrud
  5. Grbl is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Grbl is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Grbl. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
  17. and Philipp Tiefenbacher. */
  18. #include "stepper.h"
  19. #include "Configuration.h"
  20. #include "Marlin.h"
  21. #include "planner.h"
  22. #include "pins.h"
  23. #include "fastio.h"
  24. #include "temperature.h"
  25. #include "ultralcd.h"
  26. #include "speed_lookuptable.h"
  27. //===========================================================================
  28. //=============================public variables ============================
  29. //===========================================================================
  30. block_t *current_block; // A pointer to the block currently being traced
  31. //===========================================================================
  32. //=============================private variables ============================
  33. //===========================================================================
  34. //static makes it inpossible to be called from outside of this file by extern.!
  35. // Variables used by The Stepper Driver Interrupt
  36. static unsigned char out_bits; // The next stepping-bits to be output
  37. static long counter_x, // Counter variables for the bresenham line tracer
  38. counter_y,
  39. counter_z,
  40. counter_e;
  41. static unsigned long step_events_completed; // The number of step events executed in the current block
  42. #ifdef ADVANCE
  43. static long advance_rate, advance, final_advance = 0;
  44. static short old_advance = 0;
  45. static short e_steps;
  46. #endif
  47. static unsigned char busy = false; // TRUE when SIG_OUTPUT_COMPARE1A is being serviced. Used to avoid retriggering that handler.
  48. static long acceleration_time, deceleration_time;
  49. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  50. static unsigned short acc_step_rate; // needed for deccelaration start point
  51. static char step_loops;
  52. volatile long endstops_trigsteps[3]={0,0,0};
  53. volatile long endstops_stepsTotal,endstops_stepsDone;
  54. static volatile bool endstops_hit=false;
  55. // if DEBUG_STEPS is enabled, M114 can be used to compare two methods of determining the X,Y,Z position of the printer.
  56. // for debugging purposes only, should be disabled by default
  57. #ifdef DEBUG_STEPS
  58. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  59. volatile int count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  60. #endif
  61. //===========================================================================
  62. //=============================functions ============================
  63. //===========================================================================
  64. // intRes = intIn1 * intIn2 >> 16
  65. // uses:
  66. // r26 to store 0
  67. // r27 to store the byte 1 of the 24 bit result
  68. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  69. asm volatile ( \
  70. "clr r26 \n\t" \
  71. "mul %A1, %B2 \n\t" \
  72. "movw %A0, r0 \n\t" \
  73. "mul %A1, %A2 \n\t" \
  74. "add %A0, r1 \n\t" \
  75. "adc %B0, r26 \n\t" \
  76. "lsr r0 \n\t" \
  77. "adc %A0, r26 \n\t" \
  78. "adc %B0, r26 \n\t" \
  79. "clr r1 \n\t" \
  80. : \
  81. "=&r" (intRes) \
  82. : \
  83. "d" (charIn1), \
  84. "d" (intIn2) \
  85. : \
  86. "r26" \
  87. )
  88. // intRes = longIn1 * longIn2 >> 24
  89. // uses:
  90. // r26 to store 0
  91. // r27 to store the byte 1 of the 48bit result
  92. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  93. asm volatile ( \
  94. "clr r26 \n\t" \
  95. "mul %A1, %B2 \n\t" \
  96. "mov r27, r1 \n\t" \
  97. "mul %B1, %C2 \n\t" \
  98. "movw %A0, r0 \n\t" \
  99. "mul %C1, %C2 \n\t" \
  100. "add %B0, r0 \n\t" \
  101. "mul %C1, %B2 \n\t" \
  102. "add %A0, r0 \n\t" \
  103. "adc %B0, r1 \n\t" \
  104. "mul %A1, %C2 \n\t" \
  105. "add r27, r0 \n\t" \
  106. "adc %A0, r1 \n\t" \
  107. "adc %B0, r26 \n\t" \
  108. "mul %B1, %B2 \n\t" \
  109. "add r27, r0 \n\t" \
  110. "adc %A0, r1 \n\t" \
  111. "adc %B0, r26 \n\t" \
  112. "mul %C1, %A2 \n\t" \
  113. "add r27, r0 \n\t" \
  114. "adc %A0, r1 \n\t" \
  115. "adc %B0, r26 \n\t" \
  116. "mul %B1, %A2 \n\t" \
  117. "add r27, r1 \n\t" \
  118. "adc %A0, r26 \n\t" \
  119. "adc %B0, r26 \n\t" \
  120. "lsr r27 \n\t" \
  121. "adc %A0, r26 \n\t" \
  122. "adc %B0, r26 \n\t" \
  123. "clr r1 \n\t" \
  124. : \
  125. "=&r" (intRes) \
  126. : \
  127. "d" (longIn1), \
  128. "d" (longIn2) \
  129. : \
  130. "r26" , "r27" \
  131. )
  132. // Some useful constants
  133. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
  134. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
  135. void endstops_triggered(const unsigned long &stepstaken)
  136. {
  137. //this will only work if there is no bufferig
  138. //however, if you perform a move at which the endstops should be triggered, and wait for it to complete, i.e. by blocking command, it should work
  139. //yes, it uses floats, but: if endstops are triggered, thats hopefully not critical anymore anyways.
  140. //endstops_triggerpos;
  141. if(endstops_hit) //hitting a second time while the first hit is not reported
  142. return;
  143. if(current_block == NULL)
  144. return;
  145. endstops_stepsTotal=current_block->step_event_count;
  146. endstops_stepsDone=stepstaken;
  147. endstops_trigsteps[0]=current_block->steps_x;
  148. endstops_trigsteps[1]=current_block->steps_y;
  149. endstops_trigsteps[2]=current_block->steps_z;
  150. endstops_hit=true;
  151. }
  152. void checkHitEndstops()
  153. {
  154. if( !endstops_hit)
  155. return;
  156. float endstops_triggerpos[3]={0,0,0};
  157. float ratiodone=endstops_stepsDone/float(endstops_stepsTotal); //ratio of current_block thas was performed
  158. endstops_triggerpos[0]=current_position[0]-(endstops_trigsteps[0]*ratiodone)/float(axis_steps_per_unit[0]);
  159. endstops_triggerpos[1]=current_position[1]-(endstops_trigsteps[1]*ratiodone)/float(axis_steps_per_unit[1]);
  160. endstops_triggerpos[2]=current_position[2]-(endstops_trigsteps[2]*ratiodone)/float(axis_steps_per_unit[2]);
  161. SERIAL_ECHO_START;
  162. SERIAL_ECHOPGM("endstops hit: ");
  163. SERIAL_ECHOPAIR(" X:",endstops_triggerpos[0]);
  164. SERIAL_ECHOPAIR(" Y:",endstops_triggerpos[1]);
  165. SERIAL_ECHOPAIR(" Z:",endstops_triggerpos[2]);
  166. SERIAL_ECHOLN("");
  167. endstops_hit=false;
  168. }
  169. void endstops_hit_on_purpose()
  170. {
  171. endstops_hit=false;
  172. }
  173. // __________________________
  174. // /| |\ _________________ ^
  175. // / | | \ /| |\ |
  176. // / | | \ / | | \ s
  177. // / | | | | | \ p
  178. // / | | | | | \ e
  179. // +-----+------------------------+---+--+---------------+----+ e
  180. // | BLOCK 1 | BLOCK 2 | d
  181. //
  182. // time ----->
  183. //
  184. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  185. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  186. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  187. // The slope of acceleration is calculated with the leib ramp alghorithm.
  188. void st_wake_up() {
  189. // TCNT1 = 0;
  190. if(busy == false)
  191. ENABLE_STEPPER_DRIVER_INTERRUPT();
  192. }
  193. inline unsigned short calc_timer(unsigned short step_rate) {
  194. unsigned short timer;
  195. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  196. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  197. step_rate = (step_rate >> 2)&0x3fff;
  198. step_loops = 4;
  199. }
  200. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  201. step_rate = (step_rate >> 1)&0x7fff;
  202. step_loops = 2;
  203. }
  204. else {
  205. step_loops = 1;
  206. }
  207. if(step_rate < 32) step_rate = 32;
  208. step_rate -= 32; // Correct for minimal speed
  209. if(step_rate >= (8*256)){ // higher step rate
  210. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  211. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  212. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  213. MultiU16X8toH16(timer, tmp_step_rate, gain);
  214. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  215. }
  216. else { // lower step rates
  217. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  218. table_address += ((step_rate)>>1) & 0xfffc;
  219. timer = (unsigned short)pgm_read_word_near(table_address);
  220. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  221. }
  222. if(timer < 100) { timer = 100; Serial.print("Steprate to high : "); Serial.println(step_rate); }//(20kHz this should never happen)
  223. return timer;
  224. }
  225. // Initializes the trapezoid generator from the current block. Called whenever a new
  226. // block begins.
  227. inline void trapezoid_generator_reset() {
  228. #ifdef ADVANCE
  229. advance = current_block->initial_advance;
  230. final_advance = current_block->final_advance;
  231. #endif
  232. deceleration_time = 0;
  233. // step_rate to timer interval
  234. acc_step_rate = current_block->initial_rate;
  235. acceleration_time = calc_timer(acc_step_rate);
  236. OCR1A = acceleration_time;
  237. }
  238. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  239. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  240. ISR(TIMER1_COMPA_vect)
  241. {
  242. if(busy){
  243. SERIAL_ERROR_START
  244. SERIAL_ERROR(*(unsigned short *)OCR1A);
  245. SERIAL_ERRORLNPGM(" ISR overtaking itself.");
  246. return;
  247. } // The busy-flag is used to avoid reentering this interrupt
  248. busy = true;
  249. sei(); // Re enable interrupts (normally disabled while inside an interrupt handler)
  250. // If there is no current block, attempt to pop one from the buffer
  251. if (current_block == NULL) {
  252. // Anything in the buffer?
  253. current_block = plan_get_current_block();
  254. if (current_block != NULL) {
  255. trapezoid_generator_reset();
  256. counter_x = -(current_block->step_event_count >> 1);
  257. counter_y = counter_x;
  258. counter_z = counter_x;
  259. counter_e = counter_x;
  260. step_events_completed = 0;
  261. #ifdef ADVANCE
  262. e_steps = 0;
  263. #endif
  264. }
  265. else {
  266. // DISABLE_STEPPER_DRIVER_INTERRUPT();
  267. }
  268. }
  269. if (current_block != NULL) {
  270. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  271. out_bits = current_block->direction_bits;
  272. #ifdef ADVANCE
  273. // Calculate E early.
  274. counter_e += current_block->steps_e;
  275. if (counter_e > 0) {
  276. counter_e -= current_block->step_event_count;
  277. if ((out_bits & (1<<E_AXIS)) != 0) { // - direction
  278. CRITICAL_SECTION_START;
  279. e_steps--;
  280. CRITICAL_SECTION_END;
  281. }
  282. else {
  283. CRITICAL_SECTION_START;
  284. e_steps++;
  285. CRITICAL_SECTION_END;
  286. }
  287. }
  288. // Do E steps + advance steps
  289. CRITICAL_SECTION_START;
  290. e_steps += ((advance >> 16) - old_advance);
  291. CRITICAL_SECTION_END;
  292. old_advance = advance >> 16;
  293. #endif //ADVANCE
  294. // Set direction en check limit switches
  295. if ((out_bits & (1<<X_AXIS)) != 0) { // -direction
  296. WRITE(X_DIR_PIN, INVERT_X_DIR);
  297. #ifdef DEBUG_STEPS
  298. count_direction[X_AXIS]=-1;
  299. #endif
  300. #if X_MIN_PIN > -1
  301. if(READ(X_MIN_PIN) != ENDSTOPS_INVERTING) {
  302. // endstops_triggered(step_events_completed);
  303. step_events_completed = current_block->step_event_count;
  304. }
  305. #endif
  306. }
  307. else { // +direction
  308. WRITE(X_DIR_PIN,!INVERT_X_DIR);
  309. #ifdef DEBUG_STEPS
  310. count_direction[X_AXIS]=1;
  311. #endif
  312. #if X_MAX_PIN > -1
  313. if((READ(X_MAX_PIN) != ENDSTOPS_INVERTING) && (current_block->steps_x >0)){
  314. // endstops_triggered(step_events_completed);
  315. step_events_completed = current_block->step_event_count;
  316. }
  317. #endif
  318. }
  319. if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
  320. WRITE(Y_DIR_PIN,INVERT_Y_DIR);
  321. #ifdef DEBUG_STEPS
  322. count_direction[Y_AXIS]=-1;
  323. #endif
  324. #if Y_MIN_PIN > -1
  325. if(READ(Y_MIN_PIN) != ENDSTOPS_INVERTING) {
  326. // endstops_triggered(step_events_completed);
  327. step_events_completed = current_block->step_event_count;
  328. }
  329. #endif
  330. }
  331. else { // +direction
  332. WRITE(Y_DIR_PIN,!INVERT_Y_DIR);
  333. #ifdef DEBUG_STEPS
  334. count_direction[Y_AXIS]=1;
  335. #endif
  336. #if Y_MAX_PIN > -1
  337. if((READ(Y_MAX_PIN) != ENDSTOPS_INVERTING) && (current_block->steps_y >0)){
  338. // endstops_triggered(step_events_completed);
  339. step_events_completed = current_block->step_event_count;
  340. }
  341. #endif
  342. }
  343. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  344. WRITE(Z_DIR_PIN,INVERT_Z_DIR);
  345. #ifdef DEBUG_STEPS
  346. count_direction[Z_AXIS]=-1;
  347. #endif
  348. #if Z_MIN_PIN > -1
  349. if(READ(Z_MIN_PIN) != ENDSTOPS_INVERTING) {
  350. endstops_triggered(step_events_completed);
  351. step_events_completed = current_block->step_event_count;
  352. }
  353. #endif
  354. }
  355. else { // +direction
  356. WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
  357. #ifdef DEBUG_STEPS
  358. count_direction[Z_AXIS]=1;
  359. #endif
  360. #if Z_MAX_PIN > -1
  361. if((READ(Z_MAX_PIN) != ENDSTOPS_INVERTING) && (current_block->steps_z >0)){
  362. // endstops_triggered(step_events_completed);
  363. step_events_completed = current_block->step_event_count;
  364. }
  365. #endif
  366. }
  367. #ifndef ADVANCE
  368. if ((out_bits & (1<<E_AXIS)) != 0) // -direction
  369. WRITE(E_DIR_PIN,INVERT_E_DIR);
  370. else // +direction
  371. WRITE(E_DIR_PIN,!INVERT_E_DIR);
  372. #endif //!ADVANCE
  373. for(int8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
  374. counter_x += current_block->steps_x;
  375. if (counter_x > 0) {
  376. WRITE(X_STEP_PIN, HIGH);
  377. counter_x -= current_block->step_event_count;
  378. WRITE(X_STEP_PIN, LOW);
  379. #ifdef DEBUG_STEPS
  380. count_position[X_AXIS]+=count_direction[X_AXIS];
  381. #endif
  382. }
  383. counter_y += current_block->steps_y;
  384. if (counter_y > 0) {
  385. WRITE(Y_STEP_PIN, HIGH);
  386. counter_y -= current_block->step_event_count;
  387. WRITE(Y_STEP_PIN, LOW);
  388. #ifdef DEBUG_STEPS
  389. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  390. #endif
  391. }
  392. counter_z += current_block->steps_z;
  393. if (counter_z > 0) {
  394. WRITE(Z_STEP_PIN, HIGH);
  395. counter_z -= current_block->step_event_count;
  396. WRITE(Z_STEP_PIN, LOW);
  397. #ifdef DEBUG_STEPS
  398. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  399. #endif
  400. }
  401. #ifndef ADVANCE
  402. counter_e += current_block->steps_e;
  403. if (counter_e > 0) {
  404. WRITE(E_STEP_PIN, HIGH);
  405. counter_e -= current_block->step_event_count;
  406. WRITE(E_STEP_PIN, LOW);
  407. }
  408. #endif //!ADVANCE
  409. step_events_completed += 1;
  410. if(step_events_completed >= current_block->step_event_count) break;
  411. }
  412. // Calculare new timer value
  413. unsigned short timer;
  414. unsigned short step_rate;
  415. if (step_events_completed <= current_block->accelerate_until) {
  416. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  417. acc_step_rate += current_block->initial_rate;
  418. // upper limit
  419. if(acc_step_rate > current_block->nominal_rate)
  420. acc_step_rate = current_block->nominal_rate;
  421. // step_rate to timer interval
  422. timer = calc_timer(acc_step_rate);
  423. #ifdef ADVANCE
  424. advance += advance_rate;
  425. #endif
  426. acceleration_time += timer;
  427. OCR1A = timer;
  428. }
  429. else if (step_events_completed > current_block->decelerate_after) {
  430. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  431. if(step_rate > acc_step_rate) { // Check step_rate stays positive
  432. step_rate = current_block->final_rate;
  433. }
  434. else {
  435. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  436. }
  437. // lower limit
  438. if(step_rate < current_block->final_rate)
  439. step_rate = current_block->final_rate;
  440. // step_rate to timer interval
  441. timer = calc_timer(step_rate);
  442. #ifdef ADVANCE
  443. advance -= advance_rate;
  444. if(advance < final_advance)
  445. advance = final_advance;
  446. #endif //ADVANCE
  447. deceleration_time += timer;
  448. OCR1A = timer;
  449. }
  450. else {
  451. timer = calc_timer(current_block->nominal_rate);
  452. OCR1A = timer;
  453. }
  454. // If current block is finished, reset pointer
  455. if (step_events_completed >= current_block->step_event_count) {
  456. current_block = NULL;
  457. plan_discard_current_block();
  458. }
  459. }
  460. cli(); // disable interrupts
  461. busy=false;
  462. }
  463. #ifdef ADVANCE
  464. unsigned char old_OCR0A;
  465. // Timer interrupt for E. e_steps is set in the main routine;
  466. // Timer 0 is shared with millies
  467. ISR(TIMER0_COMPA_vect)
  468. {
  469. // Critical section needed because Timer 1 interrupt has higher priority.
  470. // The pin set functions are placed on trategic position to comply with the stepper driver timing.
  471. WRITE(E_STEP_PIN, LOW);
  472. // Set E direction (Depends on E direction + advance)
  473. if (e_steps < 0) {
  474. WRITE(E_DIR_PIN,INVERT_E_DIR);
  475. e_steps++;
  476. WRITE(E_STEP_PIN, HIGH);
  477. }
  478. if (e_steps > 0) {
  479. WRITE(E_DIR_PIN,!INVERT_E_DIR);
  480. e_steps--;
  481. WRITE(E_STEP_PIN, HIGH);
  482. }
  483. old_OCR0A += 25; // 10kHz interrupt
  484. OCR0A = old_OCR0A;
  485. }
  486. #endif // ADVANCE
  487. void st_init()
  488. {
  489. //Initialize Dir Pins
  490. #if X_DIR_PIN > -1
  491. SET_OUTPUT(X_DIR_PIN);
  492. #endif
  493. #if Y_DIR_PIN > -1
  494. SET_OUTPUT(Y_DIR_PIN);
  495. #endif
  496. #if Z_DIR_PIN > -1
  497. SET_OUTPUT(Z_DIR_PIN);
  498. #endif
  499. #if E_DIR_PIN > -1
  500. SET_OUTPUT(E_DIR_PIN);
  501. #endif
  502. //Initialize Enable Pins - steppers default to disabled.
  503. #if (X_ENABLE_PIN > -1)
  504. SET_OUTPUT(X_ENABLE_PIN);
  505. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  506. #endif
  507. #if (Y_ENABLE_PIN > -1)
  508. SET_OUTPUT(Y_ENABLE_PIN);
  509. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  510. #endif
  511. #if (Z_ENABLE_PIN > -1)
  512. SET_OUTPUT(Z_ENABLE_PIN);
  513. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  514. #endif
  515. #if (E_ENABLE_PIN > -1)
  516. SET_OUTPUT(E_ENABLE_PIN);
  517. if(!E_ENABLE_ON) WRITE(E_ENABLE_PIN,HIGH);
  518. #endif
  519. //endstops and pullups
  520. #ifdef ENDSTOPPULLUPS
  521. #if X_MIN_PIN > -1
  522. SET_INPUT(X_MIN_PIN);
  523. WRITE(X_MIN_PIN,HIGH);
  524. #endif
  525. #if X_MAX_PIN > -1
  526. SET_INPUT(X_MAX_PIN);
  527. WRITE(X_MAX_PIN,HIGH);
  528. #endif
  529. #if Y_MIN_PIN > -1
  530. SET_INPUT(Y_MIN_PIN);
  531. WRITE(Y_MIN_PIN,HIGH);
  532. #endif
  533. #if Y_MAX_PIN > -1
  534. SET_INPUT(Y_MAX_PIN);
  535. WRITE(Y_MAX_PIN,HIGH);
  536. #endif
  537. #if Z_MIN_PIN > -1
  538. SET_INPUT(Z_MIN_PIN);
  539. WRITE(Z_MIN_PIN,HIGH);
  540. #endif
  541. #if Z_MAX_PIN > -1
  542. SET_INPUT(Z_MAX_PIN);
  543. WRITE(Z_MAX_PIN,HIGH);
  544. #endif
  545. #else //ENDSTOPPULLUPS
  546. #if X_MIN_PIN > -1
  547. SET_INPUT(X_MIN_PIN);
  548. #endif
  549. #if X_MAX_PIN > -1
  550. SET_INPUT(X_MAX_PIN);
  551. #endif
  552. #if Y_MIN_PIN > -1
  553. SET_INPUT(Y_MIN_PIN);
  554. #endif
  555. #if Y_MAX_PIN > -1
  556. SET_INPUT(Y_MAX_PIN);
  557. #endif
  558. #if Z_MIN_PIN > -1
  559. SET_INPUT(Z_MIN_PIN);
  560. #endif
  561. #if Z_MAX_PIN > -1
  562. SET_INPUT(Z_MAX_PIN);
  563. #endif
  564. #endif //ENDSTOPPULLUPS
  565. //Initialize Step Pins
  566. #if (X_STEP_PIN > -1)
  567. SET_OUTPUT(X_STEP_PIN);
  568. #endif
  569. #if (Y_STEP_PIN > -1)
  570. SET_OUTPUT(Y_STEP_PIN);
  571. #endif
  572. #if (Z_STEP_PIN > -1)
  573. SET_OUTPUT(Z_STEP_PIN);
  574. #endif
  575. #if (E_STEP_PIN > -1)
  576. SET_OUTPUT(E_STEP_PIN);
  577. #endif
  578. // waveform generation = 0100 = CTC
  579. TCCR1B &= ~(1<<WGM13);
  580. TCCR1B |= (1<<WGM12);
  581. TCCR1A &= ~(1<<WGM11);
  582. TCCR1A &= ~(1<<WGM10);
  583. // output mode = 00 (disconnected)
  584. TCCR1A &= ~(3<<COM1A0);
  585. TCCR1A &= ~(3<<COM1B0);
  586. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10); // 2MHz timer
  587. OCR1A = 0x4000;
  588. DISABLE_STEPPER_DRIVER_INTERRUPT();
  589. #ifdef ADVANCE
  590. e_steps = 0;
  591. TIMSK0 |= (1<<OCIE0A);
  592. #endif //ADVANCE
  593. sei();
  594. }
  595. // Block until all buffered steps are executed
  596. void st_synchronize()
  597. {
  598. while(plan_get_current_block()) {
  599. manage_heater();
  600. manage_inactivity(1);
  601. LCD_STATUS;
  602. }
  603. }