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.

stepper.cpp 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. volatile 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 long old_advance = 0;
  45. #endif
  46. static long e_steps;
  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. static unsigned short OCR1A_nominal;
  53. volatile long endstops_trigsteps[3]={0,0,0};
  54. volatile long endstops_stepsTotal,endstops_stepsDone;
  55. static volatile bool endstop_x_hit=false;
  56. static volatile bool endstop_y_hit=false;
  57. static volatile bool endstop_z_hit=false;
  58. static bool old_x_min_endstop=false;
  59. static bool old_x_max_endstop=false;
  60. static bool old_y_min_endstop=false;
  61. static bool old_y_max_endstop=false;
  62. static bool old_z_min_endstop=false;
  63. static bool old_z_max_endstop=false;
  64. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  65. volatile char count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  66. //===========================================================================
  67. //=============================functions ============================
  68. //===========================================================================
  69. // intRes = intIn1 * intIn2 >> 16
  70. // uses:
  71. // r26 to store 0
  72. // r27 to store the byte 1 of the 24 bit result
  73. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  74. asm volatile ( \
  75. "clr r26 \n\t" \
  76. "mul %A1, %B2 \n\t" \
  77. "movw %A0, r0 \n\t" \
  78. "mul %A1, %A2 \n\t" \
  79. "add %A0, r1 \n\t" \
  80. "adc %B0, r26 \n\t" \
  81. "lsr r0 \n\t" \
  82. "adc %A0, r26 \n\t" \
  83. "adc %B0, r26 \n\t" \
  84. "clr r1 \n\t" \
  85. : \
  86. "=&r" (intRes) \
  87. : \
  88. "d" (charIn1), \
  89. "d" (intIn2) \
  90. : \
  91. "r26" \
  92. )
  93. // intRes = longIn1 * longIn2 >> 24
  94. // uses:
  95. // r26 to store 0
  96. // r27 to store the byte 1 of the 48bit result
  97. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  98. asm volatile ( \
  99. "clr r26 \n\t" \
  100. "mul %A1, %B2 \n\t" \
  101. "mov r27, r1 \n\t" \
  102. "mul %B1, %C2 \n\t" \
  103. "movw %A0, r0 \n\t" \
  104. "mul %C1, %C2 \n\t" \
  105. "add %B0, r0 \n\t" \
  106. "mul %C1, %B2 \n\t" \
  107. "add %A0, r0 \n\t" \
  108. "adc %B0, r1 \n\t" \
  109. "mul %A1, %C2 \n\t" \
  110. "add r27, r0 \n\t" \
  111. "adc %A0, r1 \n\t" \
  112. "adc %B0, r26 \n\t" \
  113. "mul %B1, %B2 \n\t" \
  114. "add r27, r0 \n\t" \
  115. "adc %A0, r1 \n\t" \
  116. "adc %B0, r26 \n\t" \
  117. "mul %C1, %A2 \n\t" \
  118. "add r27, r0 \n\t" \
  119. "adc %A0, r1 \n\t" \
  120. "adc %B0, r26 \n\t" \
  121. "mul %B1, %A2 \n\t" \
  122. "add r27, r1 \n\t" \
  123. "adc %A0, r26 \n\t" \
  124. "adc %B0, r26 \n\t" \
  125. "lsr r27 \n\t" \
  126. "adc %A0, r26 \n\t" \
  127. "adc %B0, r26 \n\t" \
  128. "clr r1 \n\t" \
  129. : \
  130. "=&r" (intRes) \
  131. : \
  132. "d" (longIn1), \
  133. "d" (longIn2) \
  134. : \
  135. "r26" , "r27" \
  136. )
  137. // Some useful constants
  138. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
  139. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
  140. void checkHitEndstops()
  141. {
  142. if( endstop_x_hit || endstop_y_hit || endstop_z_hit) {
  143. SERIAL_ECHO_START;
  144. SERIAL_ECHOPGM("endstops hit: ");
  145. if(endstop_x_hit) {
  146. SERIAL_ECHOPAIR(" X:",(float)endstops_trigsteps[X_AXIS]/axis_steps_per_unit[X_AXIS]);
  147. }
  148. if(endstop_y_hit) {
  149. SERIAL_ECHOPAIR(" Y:",(float)endstops_trigsteps[Y_AXIS]/axis_steps_per_unit[Y_AXIS]);
  150. }
  151. if(endstop_z_hit) {
  152. SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
  153. }
  154. SERIAL_ECHOLN("");
  155. endstop_x_hit=false;
  156. endstop_y_hit=false;
  157. endstop_z_hit=false;
  158. }
  159. }
  160. void endstops_hit_on_purpose()
  161. {
  162. endstop_x_hit=false;
  163. endstop_y_hit=false;
  164. endstop_z_hit=false;
  165. }
  166. // __________________________
  167. // /| |\ _________________ ^
  168. // / | | \ /| |\ |
  169. // / | | \ / | | \ s
  170. // / | | | | | \ p
  171. // / | | | | | \ e
  172. // +-----+------------------------+---+--+---------------+----+ e
  173. // | BLOCK 1 | BLOCK 2 | d
  174. //
  175. // time ----->
  176. //
  177. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  178. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  179. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  180. // The slope of acceleration is calculated with the leib ramp alghorithm.
  181. void st_wake_up() {
  182. // TCNT1 = 0;
  183. if(busy == false)
  184. ENABLE_STEPPER_DRIVER_INTERRUPT();
  185. }
  186. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  187. unsigned short timer;
  188. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  189. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  190. step_rate = (step_rate >> 2)&0x3fff;
  191. step_loops = 4;
  192. }
  193. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  194. step_rate = (step_rate >> 1)&0x7fff;
  195. step_loops = 2;
  196. }
  197. else {
  198. step_loops = 1;
  199. }
  200. if(step_rate < 32) step_rate = 32;
  201. step_rate -= 32; // Correct for minimal speed
  202. if(step_rate >= (8*256)){ // higher step rate
  203. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  204. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  205. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  206. MultiU16X8toH16(timer, tmp_step_rate, gain);
  207. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  208. }
  209. else { // lower step rates
  210. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  211. table_address += ((step_rate)>>1) & 0xfffc;
  212. timer = (unsigned short)pgm_read_word_near(table_address);
  213. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  214. }
  215. if(timer < 100) { timer = 100; MSerial.print("Steprate to high : "); MSerial.println(step_rate); }//(20kHz this should never happen)
  216. return timer;
  217. }
  218. // Initializes the trapezoid generator from the current block. Called whenever a new
  219. // block begins.
  220. FORCE_INLINE void trapezoid_generator_reset() {
  221. #ifdef ADVANCE
  222. advance = current_block->initial_advance;
  223. final_advance = current_block->final_advance;
  224. // Do E steps + advance steps
  225. e_steps += ((advance >>8) - old_advance);
  226. old_advance = advance >>8;
  227. #endif
  228. deceleration_time = 0;
  229. // step_rate to timer interval
  230. acc_step_rate = current_block->initial_rate;
  231. acceleration_time = calc_timer(acc_step_rate);
  232. OCR1A = acceleration_time;
  233. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  234. // SERIAL_ECHO_START;
  235. // SERIAL_ECHOPGM("advance :");
  236. // SERIAL_ECHO(current_block->advance/256.0);
  237. // SERIAL_ECHOPGM("advance rate :");
  238. // SERIAL_ECHO(current_block->advance_rate/256.0);
  239. // SERIAL_ECHOPGM("initial advance :");
  240. // SERIAL_ECHO(current_block->initial_advance/256.0);
  241. // SERIAL_ECHOPGM("final advance :");
  242. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  243. }
  244. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  245. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  246. ISR(TIMER1_COMPA_vect)
  247. {
  248. // If there is no current block, attempt to pop one from the buffer
  249. if (current_block == NULL) {
  250. // Anything in the buffer?
  251. current_block = plan_get_current_block();
  252. if (current_block != NULL) {
  253. trapezoid_generator_reset();
  254. counter_x = -(current_block->step_event_count >> 1);
  255. counter_y = counter_x;
  256. counter_z = counter_x;
  257. counter_e = counter_x;
  258. step_events_completed = 0;
  259. // #ifdef ADVANCE
  260. e_steps = 0;
  261. // #endif
  262. }
  263. else {
  264. OCR1A=2000; // 1kHz.
  265. }
  266. }
  267. if (current_block != NULL) {
  268. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  269. out_bits = current_block->direction_bits;
  270. // Set direction en check limit switches
  271. if ((out_bits & (1<<X_AXIS)) != 0) { // -direction
  272. WRITE(X_DIR_PIN, INVERT_X_DIR);
  273. count_direction[X_AXIS]=-1;
  274. #if X_MIN_PIN > -1
  275. bool x_min_endstop=(READ(X_MIN_PIN) != X_ENDSTOPS_INVERTING);
  276. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
  277. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  278. endstop_x_hit=true;
  279. step_events_completed = current_block->step_event_count;
  280. }
  281. old_x_min_endstop = x_min_endstop;
  282. #endif
  283. }
  284. else { // +direction
  285. WRITE(X_DIR_PIN,!INVERT_X_DIR);
  286. count_direction[X_AXIS]=1;
  287. #if X_MAX_PIN > -1
  288. bool x_max_endstop=(READ(X_MAX_PIN) != X_ENDSTOPS_INVERTING);
  289. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
  290. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  291. endstop_x_hit=true;
  292. step_events_completed = current_block->step_event_count;
  293. }
  294. old_x_max_endstop = x_max_endstop;
  295. #endif
  296. }
  297. if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
  298. WRITE(Y_DIR_PIN,INVERT_Y_DIR);
  299. count_direction[Y_AXIS]=-1;
  300. #if Y_MIN_PIN > -1
  301. bool y_min_endstop=(READ(Y_MIN_PIN) != Y_ENDSTOPS_INVERTING);
  302. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
  303. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  304. endstop_y_hit=true;
  305. step_events_completed = current_block->step_event_count;
  306. }
  307. old_y_min_endstop = y_min_endstop;
  308. #endif
  309. }
  310. else { // +direction
  311. WRITE(Y_DIR_PIN,!INVERT_Y_DIR);
  312. count_direction[Y_AXIS]=1;
  313. #if Y_MAX_PIN > -1
  314. bool y_max_endstop=(READ(Y_MAX_PIN) != Y_ENDSTOPS_INVERTING);
  315. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
  316. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  317. endstop_y_hit=true;
  318. step_events_completed = current_block->step_event_count;
  319. }
  320. old_y_max_endstop = y_max_endstop;
  321. #endif
  322. }
  323. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  324. WRITE(Z_DIR_PIN,INVERT_Z_DIR);
  325. count_direction[Z_AXIS]=-1;
  326. #if Z_MIN_PIN > -1
  327. bool z_min_endstop=(READ(Z_MIN_PIN) != Z_ENDSTOPS_INVERTING);
  328. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
  329. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  330. endstop_z_hit=true;
  331. step_events_completed = current_block->step_event_count;
  332. }
  333. old_z_min_endstop = z_min_endstop;
  334. #endif
  335. }
  336. else { // +direction
  337. WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
  338. count_direction[Z_AXIS]=1;
  339. #if Z_MAX_PIN > -1
  340. bool z_max_endstop=(READ(Z_MAX_PIN) != Z_ENDSTOPS_INVERTING);
  341. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
  342. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  343. endstop_z_hit=true;
  344. step_events_completed = current_block->step_event_count;
  345. }
  346. old_z_max_endstop = z_max_endstop;
  347. #endif
  348. }
  349. #ifndef ADVANCE
  350. if ((out_bits & (1<<E_AXIS)) != 0) { // -direction
  351. WRITE(E_DIR_PIN,INVERT_E_DIR);
  352. count_direction[E_AXIS]=-1;
  353. }
  354. else { // +direction
  355. WRITE(E_DIR_PIN,!INVERT_E_DIR);
  356. count_direction[E_AXIS]=-1;
  357. }
  358. #endif //!ADVANCE
  359. for(int8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
  360. MSerial.checkRx(); // Check for serial chars.
  361. #ifdef ADVANCE
  362. counter_e += current_block->steps_e;
  363. if (counter_e > 0) {
  364. counter_e -= current_block->step_event_count;
  365. if ((out_bits & (1<<E_AXIS)) != 0) { // - direction
  366. e_steps--;
  367. }
  368. else {
  369. e_steps++;
  370. }
  371. }
  372. #endif //ADVANCE
  373. counter_x += current_block->steps_x;
  374. if (counter_x > 0) {
  375. WRITE(X_STEP_PIN, HIGH);
  376. counter_x -= current_block->step_event_count;
  377. WRITE(X_STEP_PIN, LOW);
  378. count_position[X_AXIS]+=count_direction[X_AXIS];
  379. }
  380. counter_y += current_block->steps_y;
  381. if (counter_y > 0) {
  382. WRITE(Y_STEP_PIN, HIGH);
  383. counter_y -= current_block->step_event_count;
  384. WRITE(Y_STEP_PIN, LOW);
  385. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  386. }
  387. counter_z += current_block->steps_z;
  388. if (counter_z > 0) {
  389. WRITE(Z_STEP_PIN, HIGH);
  390. counter_z -= current_block->step_event_count;
  391. WRITE(Z_STEP_PIN, LOW);
  392. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  393. }
  394. #ifndef ADVANCE
  395. counter_e += current_block->steps_e;
  396. if (counter_e > 0) {
  397. WRITE(E_STEP_PIN, HIGH);
  398. counter_e -= current_block->step_event_count;
  399. WRITE(E_STEP_PIN, LOW);
  400. count_position[E_AXIS]+=count_direction[E_AXIS];
  401. }
  402. #endif //!ADVANCE
  403. step_events_completed += 1;
  404. if(step_events_completed >= current_block->step_event_count) break;
  405. }
  406. // Calculare new timer value
  407. unsigned short timer;
  408. unsigned short step_rate;
  409. if (step_events_completed <= (unsigned long int)current_block->accelerate_until) {
  410. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  411. acc_step_rate += current_block->initial_rate;
  412. // upper limit
  413. if(acc_step_rate > current_block->nominal_rate)
  414. acc_step_rate = current_block->nominal_rate;
  415. // step_rate to timer interval
  416. timer = calc_timer(acc_step_rate);
  417. OCR1A = timer;
  418. acceleration_time += timer;
  419. #ifdef ADVANCE
  420. for(int8_t i=0; i < step_loops; i++) {
  421. advance += advance_rate;
  422. }
  423. //if(advance > current_block->advance) advance = current_block->advance;
  424. // Do E steps + advance steps
  425. e_steps += ((advance >>8) - old_advance);
  426. old_advance = advance >>8;
  427. #endif
  428. }
  429. else if (step_events_completed > (unsigned long int)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. OCR1A = timer;
  443. deceleration_time += timer;
  444. #ifdef ADVANCE
  445. for(int8_t i=0; i < step_loops; i++) {
  446. advance -= advance_rate;
  447. }
  448. if(advance < final_advance) advance = final_advance;
  449. // Do E steps + advance steps
  450. e_steps += ((advance >>8) - old_advance);
  451. old_advance = advance >>8;
  452. #endif //ADVANCE
  453. }
  454. else {
  455. OCR1A = OCR1A_nominal;
  456. }
  457. // If current block is finished, reset pointer
  458. if (step_events_completed >= current_block->step_event_count) {
  459. current_block = NULL;
  460. plan_discard_current_block();
  461. }
  462. }
  463. }
  464. #ifdef ADVANCE
  465. unsigned char old_OCR0A;
  466. // Timer interrupt for E. e_steps is set in the main routine;
  467. // Timer 0 is shared with millies
  468. ISR(TIMER0_COMPA_vect)
  469. {
  470. old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
  471. OCR0A = old_OCR0A;
  472. // Set E direction (Depends on E direction + advance)
  473. for(unsigned char i=0; i<4;) {
  474. WRITE(E_STEP_PIN, LOW);
  475. if (e_steps == 0) break;
  476. i++;
  477. if (e_steps < 0) {
  478. WRITE(E_DIR_PIN,INVERT_E_DIR);
  479. e_steps++;
  480. WRITE(E_STEP_PIN, HIGH);
  481. }
  482. else if (e_steps > 0) {
  483. WRITE(E_DIR_PIN,!INVERT_E_DIR);
  484. e_steps--;
  485. WRITE(E_STEP_PIN, HIGH);
  486. }
  487. }
  488. }
  489. #endif // ADVANCE
  490. void st_init()
  491. {
  492. //Initialize Dir Pins
  493. #if X_DIR_PIN > -1
  494. SET_OUTPUT(X_DIR_PIN);
  495. #endif
  496. #if Y_DIR_PIN > -1
  497. SET_OUTPUT(Y_DIR_PIN);
  498. #endif
  499. #if Z_DIR_PIN > -1
  500. SET_OUTPUT(Z_DIR_PIN);
  501. #endif
  502. #if E_DIR_PIN > -1
  503. SET_OUTPUT(E_DIR_PIN);
  504. #endif
  505. //Initialize Enable Pins - steppers default to disabled.
  506. #if (X_ENABLE_PIN > -1)
  507. SET_OUTPUT(X_ENABLE_PIN);
  508. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  509. #endif
  510. #if (Y_ENABLE_PIN > -1)
  511. SET_OUTPUT(Y_ENABLE_PIN);
  512. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  513. #endif
  514. #if (Z_ENABLE_PIN > -1)
  515. SET_OUTPUT(Z_ENABLE_PIN);
  516. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  517. #endif
  518. #if (E_ENABLE_PIN > -1)
  519. SET_OUTPUT(E_ENABLE_PIN);
  520. if(!E_ENABLE_ON) WRITE(E_ENABLE_PIN,HIGH);
  521. #endif
  522. //endstops and pullups
  523. #ifdef ENDSTOPPULLUPS
  524. #if X_MIN_PIN > -1
  525. SET_INPUT(X_MIN_PIN);
  526. WRITE(X_MIN_PIN,HIGH);
  527. #endif
  528. #if X_MAX_PIN > -1
  529. SET_INPUT(X_MAX_PIN);
  530. WRITE(X_MAX_PIN,HIGH);
  531. #endif
  532. #if Y_MIN_PIN > -1
  533. SET_INPUT(Y_MIN_PIN);
  534. WRITE(Y_MIN_PIN,HIGH);
  535. #endif
  536. #if Y_MAX_PIN > -1
  537. SET_INPUT(Y_MAX_PIN);
  538. WRITE(Y_MAX_PIN,HIGH);
  539. #endif
  540. #if Z_MIN_PIN > -1
  541. SET_INPUT(Z_MIN_PIN);
  542. WRITE(Z_MIN_PIN,HIGH);
  543. #endif
  544. #if Z_MAX_PIN > -1
  545. SET_INPUT(Z_MAX_PIN);
  546. WRITE(Z_MAX_PIN,HIGH);
  547. #endif
  548. #else //ENDSTOPPULLUPS
  549. #if X_MIN_PIN > -1
  550. SET_INPUT(X_MIN_PIN);
  551. #endif
  552. #if X_MAX_PIN > -1
  553. SET_INPUT(X_MAX_PIN);
  554. #endif
  555. #if Y_MIN_PIN > -1
  556. SET_INPUT(Y_MIN_PIN);
  557. #endif
  558. #if Y_MAX_PIN > -1
  559. SET_INPUT(Y_MAX_PIN);
  560. #endif
  561. #if Z_MIN_PIN > -1
  562. SET_INPUT(Z_MIN_PIN);
  563. #endif
  564. #if Z_MAX_PIN > -1
  565. SET_INPUT(Z_MAX_PIN);
  566. #endif
  567. #endif //ENDSTOPPULLUPS
  568. //Initialize Step Pins
  569. #if (X_STEP_PIN > -1)
  570. SET_OUTPUT(X_STEP_PIN);
  571. #endif
  572. #if (Y_STEP_PIN > -1)
  573. SET_OUTPUT(Y_STEP_PIN);
  574. #endif
  575. #if (Z_STEP_PIN > -1)
  576. SET_OUTPUT(Z_STEP_PIN);
  577. #endif
  578. #if (E_STEP_PIN > -1)
  579. SET_OUTPUT(E_STEP_PIN);
  580. #endif
  581. // waveform generation = 0100 = CTC
  582. TCCR1B &= ~(1<<WGM13);
  583. TCCR1B |= (1<<WGM12);
  584. TCCR1A &= ~(1<<WGM11);
  585. TCCR1A &= ~(1<<WGM10);
  586. // output mode = 00 (disconnected)
  587. TCCR1A &= ~(3<<COM1A0);
  588. TCCR1A &= ~(3<<COM1B0);
  589. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10); // 2MHz timer
  590. OCR1A = 0x4000;
  591. TCNT1 = 0;
  592. ENABLE_STEPPER_DRIVER_INTERRUPT();
  593. #ifdef ADVANCE
  594. #if defined(TCCR0A) && defined(WGM01)
  595. TCCR0A &= ~(1<<WGM01);
  596. TCCR0A &= ~(1<<WGM00);
  597. #endif
  598. e_steps = 0;
  599. TIMSK0 |= (1<<OCIE0A);
  600. #endif //ADVANCE
  601. sei();
  602. }
  603. // Block until all buffered steps are executed
  604. void st_synchronize()
  605. {
  606. while(plan_get_current_block()) {
  607. manage_heater();
  608. manage_inactivity(1);
  609. LCD_STATUS;
  610. }
  611. }
  612. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  613. {
  614. CRITICAL_SECTION_START;
  615. count_position[X_AXIS] = x;
  616. count_position[Y_AXIS] = y;
  617. count_position[Z_AXIS] = z;
  618. count_position[E_AXIS] = e;
  619. CRITICAL_SECTION_END;
  620. }
  621. void st_set_e_position(const long &e)
  622. {
  623. CRITICAL_SECTION_START;
  624. count_position[E_AXIS] = e;
  625. CRITICAL_SECTION_END;
  626. }
  627. long st_get_position(uint8_t axis)
  628. {
  629. long count_pos;
  630. CRITICAL_SECTION_START;
  631. count_pos = count_position[axis];
  632. CRITICAL_SECTION_END;
  633. return count_pos;
  634. }
  635. void finishAndDisableSteppers()
  636. {
  637. st_synchronize();
  638. LCD_MESSAGEPGM("Released.");
  639. disable_x();
  640. disable_y();
  641. disable_z();
  642. disable_e();
  643. }