My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

stepper.cpp 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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 "Marlin.h"
  19. #include "stepper.h"
  20. #include "planner.h"
  21. #include "temperature.h"
  22. #include "ultralcd.h"
  23. #include "language.h"
  24. #include "speed_lookuptable.h"
  25. //===========================================================================
  26. //=============================public variables ============================
  27. //===========================================================================
  28. block_t *current_block; // A pointer to the block currently being traced
  29. //===========================================================================
  30. //=============================private variables ============================
  31. //===========================================================================
  32. //static makes it inpossible to be called from outside of this file by extern.!
  33. // Variables used by The Stepper Driver Interrupt
  34. static unsigned char out_bits; // The next stepping-bits to be output
  35. static long counter_x, // Counter variables for the bresenham line tracer
  36. counter_y,
  37. counter_z,
  38. counter_e;
  39. volatile static unsigned long step_events_completed; // The number of step events executed in the current block
  40. #ifdef ADVANCE
  41. static long advance_rate, advance, final_advance = 0;
  42. static long old_advance = 0;
  43. #endif
  44. static long e_steps[3];
  45. static unsigned char busy = false; // TRUE when SIG_OUTPUT_COMPARE1A is being serviced. Used to avoid retriggering that handler.
  46. static long acceleration_time, deceleration_time;
  47. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  48. static unsigned short acc_step_rate; // needed for deccelaration start point
  49. static char step_loops;
  50. static unsigned short OCR1A_nominal;
  51. volatile long endstops_trigsteps[3]={0,0,0};
  52. volatile long endstops_stepsTotal,endstops_stepsDone;
  53. static volatile bool endstop_x_hit=false;
  54. static volatile bool endstop_y_hit=false;
  55. static volatile bool endstop_z_hit=false;
  56. static bool old_x_min_endstop=false;
  57. static bool old_x_max_endstop=false;
  58. static bool old_y_min_endstop=false;
  59. static bool old_y_max_endstop=false;
  60. static bool old_z_min_endstop=false;
  61. static bool old_z_max_endstop=false;
  62. static bool check_endstops = true;
  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. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  69. #define CHECK_ENDSTOPS if(check_endstops)
  70. #else
  71. #define CHECK_ENDSTOPS
  72. #endif
  73. // intRes = intIn1 * intIn2 >> 16
  74. // uses:
  75. // r26 to store 0
  76. // r27 to store the byte 1 of the 24 bit result
  77. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  78. asm volatile ( \
  79. "clr r26 \n\t" \
  80. "mul %A1, %B2 \n\t" \
  81. "movw %A0, r0 \n\t" \
  82. "mul %A1, %A2 \n\t" \
  83. "add %A0, r1 \n\t" \
  84. "adc %B0, r26 \n\t" \
  85. "lsr r0 \n\t" \
  86. "adc %A0, r26 \n\t" \
  87. "adc %B0, r26 \n\t" \
  88. "clr r1 \n\t" \
  89. : \
  90. "=&r" (intRes) \
  91. : \
  92. "d" (charIn1), \
  93. "d" (intIn2) \
  94. : \
  95. "r26" \
  96. )
  97. // intRes = longIn1 * longIn2 >> 24
  98. // uses:
  99. // r26 to store 0
  100. // r27 to store the byte 1 of the 48bit result
  101. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  102. asm volatile ( \
  103. "clr r26 \n\t" \
  104. "mul %A1, %B2 \n\t" \
  105. "mov r27, r1 \n\t" \
  106. "mul %B1, %C2 \n\t" \
  107. "movw %A0, r0 \n\t" \
  108. "mul %C1, %C2 \n\t" \
  109. "add %B0, r0 \n\t" \
  110. "mul %C1, %B2 \n\t" \
  111. "add %A0, r0 \n\t" \
  112. "adc %B0, r1 \n\t" \
  113. "mul %A1, %C2 \n\t" \
  114. "add r27, r0 \n\t" \
  115. "adc %A0, r1 \n\t" \
  116. "adc %B0, r26 \n\t" \
  117. "mul %B1, %B2 \n\t" \
  118. "add r27, r0 \n\t" \
  119. "adc %A0, r1 \n\t" \
  120. "adc %B0, r26 \n\t" \
  121. "mul %C1, %A2 \n\t" \
  122. "add r27, r0 \n\t" \
  123. "adc %A0, r1 \n\t" \
  124. "adc %B0, r26 \n\t" \
  125. "mul %B1, %A2 \n\t" \
  126. "add r27, r1 \n\t" \
  127. "adc %A0, r26 \n\t" \
  128. "adc %B0, r26 \n\t" \
  129. "lsr r27 \n\t" \
  130. "adc %A0, r26 \n\t" \
  131. "adc %B0, r26 \n\t" \
  132. "clr r1 \n\t" \
  133. : \
  134. "=&r" (intRes) \
  135. : \
  136. "d" (longIn1), \
  137. "d" (longIn2) \
  138. : \
  139. "r26" , "r27" \
  140. )
  141. // Some useful constants
  142. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
  143. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
  144. void checkHitEndstops()
  145. {
  146. if( endstop_x_hit || endstop_y_hit || endstop_z_hit) {
  147. SERIAL_ECHO_START;
  148. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  149. if(endstop_x_hit) {
  150. SERIAL_ECHOPAIR(" X:",(float)endstops_trigsteps[X_AXIS]/axis_steps_per_unit[X_AXIS]);
  151. }
  152. if(endstop_y_hit) {
  153. SERIAL_ECHOPAIR(" Y:",(float)endstops_trigsteps[Y_AXIS]/axis_steps_per_unit[Y_AXIS]);
  154. }
  155. if(endstop_z_hit) {
  156. SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
  157. }
  158. SERIAL_ECHOLN("");
  159. endstop_x_hit=false;
  160. endstop_y_hit=false;
  161. endstop_z_hit=false;
  162. }
  163. }
  164. void endstops_hit_on_purpose()
  165. {
  166. endstop_x_hit=false;
  167. endstop_y_hit=false;
  168. endstop_z_hit=false;
  169. }
  170. void enable_endstops(bool check)
  171. {
  172. check_endstops = check;
  173. }
  174. // __________________________
  175. // /| |\ _________________ ^
  176. // / | | \ /| |\ |
  177. // / | | \ / | | \ s
  178. // / | | | | | \ p
  179. // / | | | | | \ e
  180. // +-----+------------------------+---+--+---------------+----+ e
  181. // | BLOCK 1 | BLOCK 2 | d
  182. //
  183. // time ----->
  184. //
  185. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  186. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  187. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  188. // The slope of acceleration is calculated with the leib ramp alghorithm.
  189. void st_wake_up() {
  190. // TCNT1 = 0;
  191. if(busy == false)
  192. ENABLE_STEPPER_DRIVER_INTERRUPT();
  193. }
  194. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  195. unsigned short timer;
  196. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  197. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  198. step_rate = (step_rate >> 2)&0x3fff;
  199. step_loops = 4;
  200. }
  201. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  202. step_rate = (step_rate >> 1)&0x7fff;
  203. step_loops = 2;
  204. }
  205. else {
  206. step_loops = 1;
  207. }
  208. if(step_rate < 32) step_rate = 32;
  209. step_rate -= 32; // Correct for minimal speed
  210. if(step_rate >= (8*256)){ // higher step rate
  211. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  212. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  213. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  214. MultiU16X8toH16(timer, tmp_step_rate, gain);
  215. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  216. }
  217. else { // lower step rates
  218. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  219. table_address += ((step_rate)>>1) & 0xfffc;
  220. timer = (unsigned short)pgm_read_word_near(table_address);
  221. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  222. }
  223. if(timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
  224. return timer;
  225. }
  226. // Initializes the trapezoid generator from the current block. Called whenever a new
  227. // block begins.
  228. FORCE_INLINE void trapezoid_generator_reset() {
  229. #ifdef ADVANCE
  230. advance = current_block->initial_advance;
  231. final_advance = current_block->final_advance;
  232. // Do E steps + advance steps
  233. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  234. old_advance = advance >>8;
  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. // SERIAL_ECHO_START;
  243. // SERIAL_ECHOPGM("advance :");
  244. // SERIAL_ECHO(current_block->advance/256.0);
  245. // SERIAL_ECHOPGM("advance rate :");
  246. // SERIAL_ECHO(current_block->advance_rate/256.0);
  247. // SERIAL_ECHOPGM("initial advance :");
  248. // SERIAL_ECHO(current_block->initial_advance/256.0);
  249. // SERIAL_ECHOPGM("final advance :");
  250. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  251. }
  252. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  253. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  254. ISR(TIMER1_COMPA_vect)
  255. {
  256. // If there is no current block, attempt to pop one from the buffer
  257. if (current_block == NULL) {
  258. // Anything in the buffer?
  259. current_block = plan_get_current_block();
  260. if (current_block != NULL) {
  261. trapezoid_generator_reset();
  262. counter_x = -(current_block->step_event_count >> 1);
  263. counter_y = counter_x;
  264. counter_z = counter_x;
  265. counter_e = counter_x;
  266. step_events_completed = 0;
  267. #ifdef Z_LATE_ENABLE
  268. if(current_block->steps_z > 0) {
  269. enable_z();
  270. OCR1A = 2000; //1ms wait
  271. return;
  272. }
  273. #endif
  274. // #ifdef ADVANCE
  275. // e_steps[current_block->active_extruder] = 0;
  276. // #endif
  277. }
  278. else {
  279. OCR1A=2000; // 1kHz.
  280. }
  281. }
  282. if (current_block != NULL) {
  283. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  284. out_bits = current_block->direction_bits;
  285. // Set direction en check limit switches
  286. if ((out_bits & (1<<X_AXIS)) != 0) { // -direction
  287. WRITE(X_DIR_PIN, INVERT_X_DIR);
  288. count_direction[X_AXIS]=-1;
  289. CHECK_ENDSTOPS
  290. {
  291. #if X_MIN_PIN > -1
  292. bool x_min_endstop=(READ(X_MIN_PIN) != X_ENDSTOPS_INVERTING);
  293. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
  294. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  295. endstop_x_hit=true;
  296. step_events_completed = current_block->step_event_count;
  297. }
  298. old_x_min_endstop = x_min_endstop;
  299. #endif
  300. }
  301. }
  302. else { // +direction
  303. WRITE(X_DIR_PIN,!INVERT_X_DIR);
  304. count_direction[X_AXIS]=1;
  305. CHECK_ENDSTOPS
  306. {
  307. #if X_MAX_PIN > -1
  308. bool x_max_endstop=(READ(X_MAX_PIN) != X_ENDSTOPS_INVERTING);
  309. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
  310. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  311. endstop_x_hit=true;
  312. step_events_completed = current_block->step_event_count;
  313. }
  314. old_x_max_endstop = x_max_endstop;
  315. #endif
  316. }
  317. }
  318. if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
  319. WRITE(Y_DIR_PIN,INVERT_Y_DIR);
  320. count_direction[Y_AXIS]=-1;
  321. CHECK_ENDSTOPS
  322. {
  323. #if Y_MIN_PIN > -1
  324. bool y_min_endstop=(READ(Y_MIN_PIN) != Y_ENDSTOPS_INVERTING);
  325. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
  326. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  327. endstop_y_hit=true;
  328. step_events_completed = current_block->step_event_count;
  329. }
  330. old_y_min_endstop = y_min_endstop;
  331. #endif
  332. }
  333. }
  334. else { // +direction
  335. WRITE(Y_DIR_PIN,!INVERT_Y_DIR);
  336. count_direction[Y_AXIS]=1;
  337. CHECK_ENDSTOPS
  338. {
  339. #if Y_MAX_PIN > -1
  340. bool y_max_endstop=(READ(Y_MAX_PIN) != Y_ENDSTOPS_INVERTING);
  341. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
  342. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  343. endstop_y_hit=true;
  344. step_events_completed = current_block->step_event_count;
  345. }
  346. old_y_max_endstop = y_max_endstop;
  347. #endif
  348. }
  349. }
  350. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  351. WRITE(Z_DIR_PIN,INVERT_Z_DIR);
  352. count_direction[Z_AXIS]=-1;
  353. CHECK_ENDSTOPS
  354. {
  355. #if Z_MIN_PIN > -1
  356. bool z_min_endstop=(READ(Z_MIN_PIN) != Z_ENDSTOPS_INVERTING);
  357. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
  358. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  359. endstop_z_hit=true;
  360. step_events_completed = current_block->step_event_count;
  361. }
  362. old_z_min_endstop = z_min_endstop;
  363. #endif
  364. }
  365. }
  366. else { // +direction
  367. WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
  368. count_direction[Z_AXIS]=1;
  369. CHECK_ENDSTOPS
  370. {
  371. #if Z_MAX_PIN > -1
  372. bool z_max_endstop=(READ(Z_MAX_PIN) != Z_ENDSTOPS_INVERTING);
  373. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
  374. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  375. endstop_z_hit=true;
  376. step_events_completed = current_block->step_event_count;
  377. }
  378. old_z_max_endstop = z_max_endstop;
  379. #endif
  380. }
  381. }
  382. #ifndef ADVANCE
  383. if ((out_bits & (1<<E_AXIS)) != 0) { // -direction
  384. REV_E_DIR();
  385. count_direction[E_AXIS]=-1;
  386. }
  387. else { // +direction
  388. NORM_E_DIR();
  389. count_direction[E_AXIS]=1;
  390. }
  391. #endif //!ADVANCE
  392. for(int8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
  393. #if MOTHERBOARD != 8 // !teensylu
  394. MSerial.checkRx(); // Check for serial chars.
  395. #endif
  396. #ifdef ADVANCE
  397. counter_e += current_block->steps_e;
  398. if (counter_e > 0) {
  399. counter_e -= current_block->step_event_count;
  400. if ((out_bits & (1<<E_AXIS)) != 0) { // - direction
  401. e_steps[current_block->active_extruder]--;
  402. }
  403. else {
  404. e_steps[current_block->active_extruder]++;
  405. }
  406. }
  407. #endif //ADVANCE
  408. counter_x += current_block->steps_x;
  409. if (counter_x > 0) {
  410. WRITE(X_STEP_PIN, HIGH);
  411. counter_x -= current_block->step_event_count;
  412. WRITE(X_STEP_PIN, LOW);
  413. count_position[X_AXIS]+=count_direction[X_AXIS];
  414. }
  415. counter_y += current_block->steps_y;
  416. if (counter_y > 0) {
  417. WRITE(Y_STEP_PIN, HIGH);
  418. counter_y -= current_block->step_event_count;
  419. WRITE(Y_STEP_PIN, LOW);
  420. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  421. }
  422. counter_z += current_block->steps_z;
  423. if (counter_z > 0) {
  424. WRITE(Z_STEP_PIN, HIGH);
  425. counter_z -= current_block->step_event_count;
  426. WRITE(Z_STEP_PIN, LOW);
  427. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  428. }
  429. #ifndef ADVANCE
  430. counter_e += current_block->steps_e;
  431. if (counter_e > 0) {
  432. WRITE_E_STEP(HIGH);
  433. counter_e -= current_block->step_event_count;
  434. WRITE_E_STEP(LOW);
  435. count_position[E_AXIS]+=count_direction[E_AXIS];
  436. }
  437. #endif //!ADVANCE
  438. step_events_completed += 1;
  439. if(step_events_completed >= current_block->step_event_count) break;
  440. }
  441. // Calculare new timer value
  442. unsigned short timer;
  443. unsigned short step_rate;
  444. if (step_events_completed <= (unsigned long int)current_block->accelerate_until) {
  445. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  446. acc_step_rate += current_block->initial_rate;
  447. // upper limit
  448. if(acc_step_rate > current_block->nominal_rate)
  449. acc_step_rate = current_block->nominal_rate;
  450. // step_rate to timer interval
  451. timer = calc_timer(acc_step_rate);
  452. OCR1A = timer;
  453. acceleration_time += timer;
  454. #ifdef ADVANCE
  455. for(int8_t i=0; i < step_loops; i++) {
  456. advance += advance_rate;
  457. }
  458. //if(advance > current_block->advance) advance = current_block->advance;
  459. // Do E steps + advance steps
  460. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  461. old_advance = advance >>8;
  462. #endif
  463. }
  464. else if (step_events_completed > (unsigned long int)current_block->decelerate_after) {
  465. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  466. if(step_rate > acc_step_rate) { // Check step_rate stays positive
  467. step_rate = current_block->final_rate;
  468. }
  469. else {
  470. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  471. }
  472. // lower limit
  473. if(step_rate < current_block->final_rate)
  474. step_rate = current_block->final_rate;
  475. // step_rate to timer interval
  476. timer = calc_timer(step_rate);
  477. OCR1A = timer;
  478. deceleration_time += timer;
  479. #ifdef ADVANCE
  480. for(int8_t i=0; i < step_loops; i++) {
  481. advance -= advance_rate;
  482. }
  483. if(advance < final_advance) advance = final_advance;
  484. // Do E steps + advance steps
  485. e_steps[current_block->active_extruder] += ((advance >>8) - old_advance);
  486. old_advance = advance >>8;
  487. #endif //ADVANCE
  488. }
  489. else {
  490. OCR1A = OCR1A_nominal;
  491. }
  492. // If current block is finished, reset pointer
  493. if (step_events_completed >= current_block->step_event_count) {
  494. current_block = NULL;
  495. plan_discard_current_block();
  496. }
  497. }
  498. }
  499. #ifdef ADVANCE
  500. unsigned char old_OCR0A;
  501. // Timer interrupt for E. e_steps is set in the main routine;
  502. // Timer 0 is shared with millies
  503. ISR(TIMER0_COMPA_vect)
  504. {
  505. old_OCR0A += 52; // ~10kHz interrupt (250000 / 26 = 9615kHz)
  506. OCR0A = old_OCR0A;
  507. // Set E direction (Depends on E direction + advance)
  508. for(unsigned char i=0; i<4;i++) {
  509. if (e_steps[0] != 0) {
  510. WRITE(E0_STEP_PIN, LOW);
  511. if (e_steps[0] < 0) {
  512. WRITE(E0_DIR_PIN, INVERT_E0_DIR);
  513. e_steps[0]++;
  514. WRITE(E0_STEP_PIN, HIGH);
  515. }
  516. else if (e_steps[0] > 0) {
  517. WRITE(E0_DIR_PIN, !INVERT_E0_DIR);
  518. e_steps[0]--;
  519. WRITE(E0_STEP_PIN, HIGH);
  520. }
  521. }
  522. #if EXTRUDERS > 1
  523. if (e_steps[1] != 0) {
  524. WRITE(E1_STEP_PIN, LOW);
  525. if (e_steps[1] < 0) {
  526. WRITE(E1_DIR_PIN, INVERT_E1_DIR);
  527. e_steps[1]++;
  528. WRITE(E1_STEP_PIN, HIGH);
  529. }
  530. else if (e_steps[1] > 0) {
  531. WRITE(E1_DIR_PIN, !INVERT_E1_DIR);
  532. e_steps[1]--;
  533. WRITE(E1_STEP_PIN, HIGH);
  534. }
  535. }
  536. #endif
  537. #if EXTRUDERS > 2
  538. if (e_steps[2] != 0) {
  539. WRITE(E2_STEP_PIN, LOW);
  540. if (e_steps[2] < 0) {
  541. WRITE(E2_DIR_PIN, INVERT_E2_DIR);
  542. e_steps[2]++;
  543. WRITE(E2_STEP_PIN, HIGH);
  544. }
  545. else if (e_steps[2] > 0) {
  546. WRITE(E2_DIR_PIN, !INVERT_E2_DIR);
  547. e_steps[2]--;
  548. WRITE(E2_STEP_PIN, HIGH);
  549. }
  550. }
  551. #endif
  552. }
  553. }
  554. #endif // ADVANCE
  555. void st_init()
  556. {
  557. //Initialize Dir Pins
  558. #if X_DIR_PIN > -1
  559. SET_OUTPUT(X_DIR_PIN);
  560. #endif
  561. #if Y_DIR_PIN > -1
  562. SET_OUTPUT(Y_DIR_PIN);
  563. #endif
  564. #if Z_DIR_PIN > -1
  565. SET_OUTPUT(Z_DIR_PIN);
  566. #endif
  567. #if E0_DIR_PIN > -1
  568. SET_OUTPUT(E0_DIR_PIN);
  569. #endif
  570. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  571. SET_OUTPUT(E1_DIR_PIN);
  572. #endif
  573. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  574. SET_OUTPUT(E2_DIR_PIN);
  575. #endif
  576. //Initialize Enable Pins - steppers default to disabled.
  577. #if (X_ENABLE_PIN > -1)
  578. SET_OUTPUT(X_ENABLE_PIN);
  579. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  580. #endif
  581. #if (Y_ENABLE_PIN > -1)
  582. SET_OUTPUT(Y_ENABLE_PIN);
  583. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  584. #endif
  585. #if (Z_ENABLE_PIN > -1)
  586. SET_OUTPUT(Z_ENABLE_PIN);
  587. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  588. #endif
  589. #if (E0_ENABLE_PIN > -1)
  590. SET_OUTPUT(E0_ENABLE_PIN);
  591. if(!E_ENABLE_ON) WRITE(E0_ENABLE_PIN,HIGH);
  592. #endif
  593. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  594. SET_OUTPUT(E1_ENABLE_PIN);
  595. if(!E_ENABLE_ON) WRITE(E1_ENABLE_PIN,HIGH);
  596. #endif
  597. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  598. SET_OUTPUT(E2_ENABLE_PIN);
  599. if(!E_ENABLE_ON) WRITE(E2_ENABLE_PIN,HIGH);
  600. #endif
  601. //endstops and pullups
  602. #ifdef ENDSTOPPULLUPS
  603. #if X_MIN_PIN > -1
  604. SET_INPUT(X_MIN_PIN);
  605. WRITE(X_MIN_PIN,HIGH);
  606. #endif
  607. #if X_MAX_PIN > -1
  608. SET_INPUT(X_MAX_PIN);
  609. WRITE(X_MAX_PIN,HIGH);
  610. #endif
  611. #if Y_MIN_PIN > -1
  612. SET_INPUT(Y_MIN_PIN);
  613. WRITE(Y_MIN_PIN,HIGH);
  614. #endif
  615. #if Y_MAX_PIN > -1
  616. SET_INPUT(Y_MAX_PIN);
  617. WRITE(Y_MAX_PIN,HIGH);
  618. #endif
  619. #if Z_MIN_PIN > -1
  620. SET_INPUT(Z_MIN_PIN);
  621. WRITE(Z_MIN_PIN,HIGH);
  622. #endif
  623. #if Z_MAX_PIN > -1
  624. SET_INPUT(Z_MAX_PIN);
  625. WRITE(Z_MAX_PIN,HIGH);
  626. #endif
  627. #else //ENDSTOPPULLUPS
  628. #if X_MIN_PIN > -1
  629. SET_INPUT(X_MIN_PIN);
  630. #endif
  631. #if X_MAX_PIN > -1
  632. SET_INPUT(X_MAX_PIN);
  633. #endif
  634. #if Y_MIN_PIN > -1
  635. SET_INPUT(Y_MIN_PIN);
  636. #endif
  637. #if Y_MAX_PIN > -1
  638. SET_INPUT(Y_MAX_PIN);
  639. #endif
  640. #if Z_MIN_PIN > -1
  641. SET_INPUT(Z_MIN_PIN);
  642. #endif
  643. #if Z_MAX_PIN > -1
  644. SET_INPUT(Z_MAX_PIN);
  645. #endif
  646. #endif //ENDSTOPPULLUPS
  647. //Initialize Step Pins
  648. #if (X_STEP_PIN > -1)
  649. SET_OUTPUT(X_STEP_PIN);
  650. #endif
  651. #if (Y_STEP_PIN > -1)
  652. SET_OUTPUT(Y_STEP_PIN);
  653. #endif
  654. #if (Z_STEP_PIN > -1)
  655. SET_OUTPUT(Z_STEP_PIN);
  656. #endif
  657. #if (E0_STEP_PIN > -1)
  658. SET_OUTPUT(E0_STEP_PIN);
  659. #endif
  660. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  661. SET_OUTPUT(E1_STEP_PIN);
  662. #endif
  663. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  664. SET_OUTPUT(E2_STEP_PIN);
  665. #endif
  666. // waveform generation = 0100 = CTC
  667. TCCR1B &= ~(1<<WGM13);
  668. TCCR1B |= (1<<WGM12);
  669. TCCR1A &= ~(1<<WGM11);
  670. TCCR1A &= ~(1<<WGM10);
  671. // output mode = 00 (disconnected)
  672. TCCR1A &= ~(3<<COM1A0);
  673. TCCR1A &= ~(3<<COM1B0);
  674. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10); // 2MHz timer
  675. OCR1A = 0x4000;
  676. TCNT1 = 0;
  677. ENABLE_STEPPER_DRIVER_INTERRUPT();
  678. #ifdef ADVANCE
  679. #if defined(TCCR0A) && defined(WGM01)
  680. TCCR0A &= ~(1<<WGM01);
  681. TCCR0A &= ~(1<<WGM00);
  682. #endif
  683. e_steps[0] = 0;
  684. e_steps[1] = 0;
  685. e_steps[2] = 0;
  686. TIMSK0 |= (1<<OCIE0A);
  687. #endif //ADVANCE
  688. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  689. enable_endstops(false);
  690. #else
  691. enable_endstops(true);
  692. #endif
  693. sei();
  694. }
  695. // Block until all buffered steps are executed
  696. void st_synchronize()
  697. {
  698. while( blocks_queued()) {
  699. manage_heater();
  700. manage_inactivity(1);
  701. LCD_STATUS;
  702. }
  703. }
  704. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  705. {
  706. CRITICAL_SECTION_START;
  707. count_position[X_AXIS] = x;
  708. count_position[Y_AXIS] = y;
  709. count_position[Z_AXIS] = z;
  710. count_position[E_AXIS] = e;
  711. CRITICAL_SECTION_END;
  712. }
  713. void st_set_e_position(const long &e)
  714. {
  715. CRITICAL_SECTION_START;
  716. count_position[E_AXIS] = e;
  717. CRITICAL_SECTION_END;
  718. }
  719. long st_get_position(uint8_t axis)
  720. {
  721. long count_pos;
  722. CRITICAL_SECTION_START;
  723. count_pos = count_position[axis];
  724. CRITICAL_SECTION_END;
  725. return count_pos;
  726. }
  727. void finishAndDisableSteppers()
  728. {
  729. st_synchronize();
  730. LCD_MESSAGEPGM(MSG_STEPPER_RELEASED);
  731. disable_x();
  732. disable_y();
  733. disable_z();
  734. disable_e0();
  735. disable_e1();
  736. disable_e2();
  737. }
  738. void quickStop()
  739. {
  740. DISABLE_STEPPER_DRIVER_INTERRUPT();
  741. while(blocks_queued())
  742. plan_discard_current_block();
  743. current_block = NULL;
  744. ENABLE_STEPPER_DRIVER_INTERRUPT();
  745. }