My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

stepper.cpp 24KB

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