My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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