My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

stepper.cpp 21KB

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