stepper.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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 "cardreader.h"
  25. #include "speed_lookuptable.h"
  26. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  27. #include <SPI.h>
  28. #endif
  29. //===========================================================================
  30. //=============================public variables ============================
  31. //===========================================================================
  32. block_t *current_block; // A pointer to the block currently being traced
  33. //===========================================================================
  34. //=============================private variables ============================
  35. //===========================================================================
  36. //static makes it inpossible to be called from outside of this file by extern.!
  37. // Variables used by The Stepper Driver Interrupt
  38. static unsigned char out_bits; // The next stepping-bits to be output
  39. static int32_t counter_x, // Counter variables for the bresenham line tracer
  40. counter_y,
  41. counter_z,
  42. counter_e;
  43. volatile static uint32_t step_events_completed; // The number of step events executed in the current block
  44. static int32_t acceleration_time, deceleration_time;
  45. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  46. static uint16_t acc_step_rate; // needed for deccelaration start point
  47. static uint8_t step_loops;
  48. static uint16_t OCR1A_nominal;
  49. static uint8_t step_loops_nominal;
  50. volatile long endstops_trigsteps[3]={0,0,0};
  51. volatile long endstops_stepsTotal,endstops_stepsDone;
  52. static volatile bool endstop_x_hit=false;
  53. static volatile bool endstop_y_hit=false;
  54. static volatile bool endstop_z_hit=false;
  55. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  56. bool abort_on_endstop_hit = false;
  57. #endif
  58. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  59. int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
  60. int motor_current_setting_silent[3] = DEFAULT_PWM_MOTOR_CURRENT;
  61. int motor_current_setting_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  62. #endif
  63. static bool old_x_min_endstop=false;
  64. static bool old_x_max_endstop=false;
  65. static bool old_y_min_endstop=false;
  66. static bool old_y_max_endstop=false;
  67. static bool old_z_min_endstop=false;
  68. static bool old_z_max_endstop=false;
  69. static bool check_endstops = true;
  70. static bool check_z_endstop = false;
  71. int8_t SilentMode;
  72. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  73. volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  74. //===========================================================================
  75. //=============================functions ============================
  76. //===========================================================================
  77. #define CHECK_ENDSTOPS if(check_endstops)
  78. // intRes = intIn1 * intIn2 >> 16
  79. // uses:
  80. // r26 to store 0
  81. // r27 to store the byte 1 of the 24 bit result
  82. #define MultiU16X8toH16(intRes, charIn1, intIn2) \
  83. asm volatile ( \
  84. "clr r26 \n\t" \
  85. "mul %A1, %B2 \n\t" \
  86. "movw %A0, r0 \n\t" \
  87. "mul %A1, %A2 \n\t" \
  88. "add %A0, r1 \n\t" \
  89. "adc %B0, r26 \n\t" \
  90. "lsr r0 \n\t" \
  91. "adc %A0, r26 \n\t" \
  92. "adc %B0, r26 \n\t" \
  93. "clr r1 \n\t" \
  94. : \
  95. "=&r" (intRes) \
  96. : \
  97. "d" (charIn1), \
  98. "d" (intIn2) \
  99. : \
  100. "r26" \
  101. )
  102. // intRes = longIn1 * longIn2 >> 24
  103. // uses:
  104. // r26 to store 0
  105. // r27 to store the byte 1 of the 48bit result
  106. #define MultiU24X24toH16(intRes, longIn1, longIn2) \
  107. asm volatile ( \
  108. "clr r26 \n\t" \
  109. "mul %A1, %B2 \n\t" \
  110. "mov r27, r1 \n\t" \
  111. "mul %B1, %C2 \n\t" \
  112. "movw %A0, r0 \n\t" \
  113. "mul %C1, %C2 \n\t" \
  114. "add %B0, r0 \n\t" \
  115. "mul %C1, %B2 \n\t" \
  116. "add %A0, r0 \n\t" \
  117. "adc %B0, r1 \n\t" \
  118. "mul %A1, %C2 \n\t" \
  119. "add r27, r0 \n\t" \
  120. "adc %A0, r1 \n\t" \
  121. "adc %B0, r26 \n\t" \
  122. "mul %B1, %B2 \n\t" \
  123. "add r27, r0 \n\t" \
  124. "adc %A0, r1 \n\t" \
  125. "adc %B0, r26 \n\t" \
  126. "mul %C1, %A2 \n\t" \
  127. "add r27, r0 \n\t" \
  128. "adc %A0, r1 \n\t" \
  129. "adc %B0, r26 \n\t" \
  130. "mul %B1, %A2 \n\t" \
  131. "add r27, r1 \n\t" \
  132. "adc %A0, r26 \n\t" \
  133. "adc %B0, r26 \n\t" \
  134. "lsr r27 \n\t" \
  135. "adc %A0, r26 \n\t" \
  136. "adc %B0, r26 \n\t" \
  137. "clr r1 \n\t" \
  138. : \
  139. "=&r" (intRes) \
  140. : \
  141. "d" (longIn1), \
  142. "d" (longIn2) \
  143. : \
  144. "r26" , "r27" \
  145. )
  146. // Some useful constants
  147. #define ENABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 |= (1<<OCIE1A)
  148. #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~(1<<OCIE1A)
  149. void checkHitEndstops()
  150. {
  151. if( endstop_x_hit || endstop_y_hit || endstop_z_hit) {
  152. SERIAL_ECHO_START;
  153. SERIAL_ECHORPGM(MSG_ENDSTOPS_HIT);
  154. if(endstop_x_hit) {
  155. SERIAL_ECHOPAIR(" X:",(float)endstops_trigsteps[X_AXIS]/axis_steps_per_unit[X_AXIS]);
  156. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT, PSTR("X")));
  157. }
  158. if(endstop_y_hit) {
  159. SERIAL_ECHOPAIR(" Y:",(float)endstops_trigsteps[Y_AXIS]/axis_steps_per_unit[Y_AXIS]);
  160. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT, PSTR("Y")));
  161. }
  162. if(endstop_z_hit) {
  163. SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
  164. LCD_MESSAGERPGM(CAT2(MSG_ENDSTOPS_HIT,PSTR("Z")));
  165. }
  166. SERIAL_ECHOLN("");
  167. endstop_x_hit=false;
  168. endstop_y_hit=false;
  169. endstop_z_hit=false;
  170. #if defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && defined(SDSUPPORT)
  171. if (abort_on_endstop_hit)
  172. {
  173. card.sdprinting = false;
  174. card.closefile();
  175. quickStop();
  176. setTargetHotend0(0);
  177. setTargetHotend1(0);
  178. setTargetHotend2(0);
  179. }
  180. #endif
  181. }
  182. }
  183. bool endstops_hit_on_purpose()
  184. {
  185. bool hit = endstop_x_hit || endstop_y_hit || endstop_z_hit;
  186. endstop_x_hit=false;
  187. endstop_y_hit=false;
  188. endstop_z_hit=false;
  189. return hit;
  190. }
  191. bool endstop_z_hit_on_purpose()
  192. {
  193. bool hit = endstop_z_hit;
  194. endstop_z_hit=false;
  195. return hit;
  196. }
  197. bool enable_endstops(bool check)
  198. {
  199. bool old = check_endstops;
  200. check_endstops = check;
  201. return old;
  202. }
  203. bool enable_z_endstop(bool check)
  204. {
  205. bool old = check_z_endstop;
  206. check_z_endstop = check;
  207. endstop_z_hit=false;
  208. return old;
  209. }
  210. // __________________________
  211. // /| |\ _________________ ^
  212. // / | | \ /| |\ |
  213. // / | | \ / | | \ s
  214. // / | | | | | \ p
  215. // / | | | | | \ e
  216. // +-----+------------------------+---+--+---------------+----+ e
  217. // | BLOCK 1 | BLOCK 2 | d
  218. //
  219. // time ----->
  220. //
  221. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  222. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  223. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  224. // The slope of acceleration is calculated with the leib ramp alghorithm.
  225. void st_wake_up() {
  226. // TCNT1 = 0;
  227. ENABLE_STEPPER_DRIVER_INTERRUPT();
  228. }
  229. void step_wait(){
  230. for(int8_t i=0; i < 6; i++){
  231. }
  232. }
  233. FORCE_INLINE unsigned short calc_timer(unsigned short step_rate) {
  234. unsigned short timer;
  235. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  236. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  237. step_rate = (step_rate >> 2)&0x3fff;
  238. step_loops = 4;
  239. }
  240. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  241. step_rate = (step_rate >> 1)&0x7fff;
  242. step_loops = 2;
  243. }
  244. else {
  245. step_loops = 1;
  246. }
  247. if(step_rate < (F_CPU/500000)) step_rate = (F_CPU/500000);
  248. step_rate -= (F_CPU/500000); // Correct for minimal speed
  249. if(step_rate >= (8*256)){ // higher step rate
  250. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  251. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  252. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  253. MultiU16X8toH16(timer, tmp_step_rate, gain);
  254. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  255. }
  256. else { // lower step rates
  257. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  258. table_address += ((step_rate)>>1) & 0xfffc;
  259. timer = (unsigned short)pgm_read_word_near(table_address);
  260. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  261. }
  262. if(timer < 100) { timer = 100; MYSERIAL.print(MSG_STEPPER_TOO_HIGH); MYSERIAL.println(step_rate); }//(20kHz this should never happen)
  263. return timer;
  264. }
  265. // Initializes the trapezoid generator from the current block. Called whenever a new
  266. // block begins.
  267. FORCE_INLINE void trapezoid_generator_reset() {
  268. deceleration_time = 0;
  269. // step_rate to timer interval
  270. OCR1A_nominal = calc_timer(current_block->nominal_rate);
  271. // make a note of the number of step loops required at nominal speed
  272. step_loops_nominal = step_loops;
  273. acc_step_rate = current_block->initial_rate;
  274. acceleration_time = calc_timer(acc_step_rate);
  275. OCR1A = acceleration_time;
  276. // SERIAL_ECHO_START;
  277. // SERIAL_ECHOPGM("advance :");
  278. // SERIAL_ECHO(current_block->advance/256.0);
  279. // SERIAL_ECHOPGM("advance rate :");
  280. // SERIAL_ECHO(current_block->advance_rate/256.0);
  281. // SERIAL_ECHOPGM("initial advance :");
  282. // SERIAL_ECHO(current_block->initial_advance/256.0);
  283. // SERIAL_ECHOPGM("final advance :");
  284. // SERIAL_ECHOLN(current_block->final_advance/256.0);
  285. }
  286. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  287. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  288. ISR(TIMER1_COMPA_vect)
  289. {
  290. // If there is no current block, attempt to pop one from the buffer
  291. if (current_block == NULL) {
  292. // Anything in the buffer?
  293. current_block = plan_get_current_block();
  294. if (current_block != NULL) {
  295. // The busy flag is set by the plan_get_current_block() call.
  296. // current_block->busy = true;
  297. trapezoid_generator_reset();
  298. counter_x = -(current_block->step_event_count >> 1);
  299. counter_y = counter_x;
  300. counter_z = counter_x;
  301. counter_e = counter_x;
  302. step_events_completed = 0;
  303. #ifdef Z_LATE_ENABLE
  304. if(current_block->steps_z > 0) {
  305. enable_z();
  306. OCR1A = 2000; //1ms wait
  307. return;
  308. }
  309. #endif
  310. }
  311. else {
  312. OCR1A=2000; // 1kHz.
  313. }
  314. }
  315. if (current_block != NULL) {
  316. // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt
  317. out_bits = current_block->direction_bits;
  318. // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY)
  319. if((out_bits & (1<<X_AXIS))!=0){
  320. WRITE(X_DIR_PIN, INVERT_X_DIR);
  321. count_direction[X_AXIS]=-1;
  322. }
  323. else{
  324. WRITE(X_DIR_PIN, !INVERT_X_DIR);
  325. count_direction[X_AXIS]=1;
  326. }
  327. if((out_bits & (1<<Y_AXIS))!=0){
  328. WRITE(Y_DIR_PIN, INVERT_Y_DIR);
  329. #ifdef Y_DUAL_STEPPER_DRIVERS
  330. WRITE(Y2_DIR_PIN, !(INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  331. #endif
  332. count_direction[Y_AXIS]=-1;
  333. }
  334. else{
  335. WRITE(Y_DIR_PIN, !INVERT_Y_DIR);
  336. #ifdef Y_DUAL_STEPPER_DRIVERS
  337. WRITE(Y2_DIR_PIN, (INVERT_Y_DIR == INVERT_Y2_VS_Y_DIR));
  338. #endif
  339. count_direction[Y_AXIS]=1;
  340. }
  341. // Set direction en check limit switches
  342. #ifndef COREXY
  343. if ((out_bits & (1<<X_AXIS)) != 0) { // stepping along -X axis
  344. #else
  345. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) != 0)) { //-X occurs for -A and -B
  346. #endif
  347. CHECK_ENDSTOPS
  348. {
  349. {
  350. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  351. bool x_min_endstop=(READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
  352. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
  353. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  354. endstop_x_hit=true;
  355. step_events_completed = current_block->step_event_count;
  356. }
  357. old_x_min_endstop = x_min_endstop;
  358. #endif
  359. }
  360. }
  361. }
  362. else { // +direction
  363. CHECK_ENDSTOPS
  364. {
  365. {
  366. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  367. bool x_max_endstop=(READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
  368. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
  369. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  370. endstop_x_hit=true;
  371. step_events_completed = current_block->step_event_count;
  372. }
  373. old_x_max_endstop = x_max_endstop;
  374. #endif
  375. }
  376. }
  377. }
  378. #ifndef COREXY
  379. if ((out_bits & (1<<Y_AXIS)) != 0) { // -direction
  380. #else
  381. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) == 0)) { // -Y occurs for -A and +B
  382. #endif
  383. CHECK_ENDSTOPS
  384. {
  385. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  386. bool y_min_endstop=(READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
  387. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
  388. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  389. endstop_y_hit=true;
  390. step_events_completed = current_block->step_event_count;
  391. }
  392. old_y_min_endstop = y_min_endstop;
  393. #endif
  394. }
  395. }
  396. else { // +direction
  397. CHECK_ENDSTOPS
  398. {
  399. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  400. bool y_max_endstop=(READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
  401. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
  402. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  403. endstop_y_hit=true;
  404. step_events_completed = current_block->step_event_count;
  405. }
  406. old_y_max_endstop = y_max_endstop;
  407. #endif
  408. }
  409. }
  410. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  411. WRITE(Z_DIR_PIN,INVERT_Z_DIR);
  412. #ifdef Z_DUAL_STEPPER_DRIVERS
  413. WRITE(Z2_DIR_PIN,INVERT_Z_DIR);
  414. #endif
  415. count_direction[Z_AXIS]=-1;
  416. if(check_endstops && ! check_z_endstop)
  417. {
  418. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  419. bool z_min_endstop=(READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  420. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z > 0)) {
  421. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  422. endstop_z_hit=true;
  423. step_events_completed = current_block->step_event_count;
  424. }
  425. old_z_min_endstop = z_min_endstop;
  426. #endif
  427. }
  428. }
  429. else { // +direction
  430. WRITE(Z_DIR_PIN,!INVERT_Z_DIR);
  431. #ifdef Z_DUAL_STEPPER_DRIVERS
  432. WRITE(Z2_DIR_PIN,!INVERT_Z_DIR);
  433. #endif
  434. count_direction[Z_AXIS]=1;
  435. CHECK_ENDSTOPS
  436. {
  437. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  438. bool z_max_endstop=(READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING);
  439. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z > 0)) {
  440. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  441. endstop_z_hit=true;
  442. step_events_completed = current_block->step_event_count;
  443. }
  444. old_z_max_endstop = z_max_endstop;
  445. #endif
  446. }
  447. }
  448. // Supporting stopping on a trigger of the Z-stop induction sensor, not only for the Z-minus movements.
  449. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  450. if(check_z_endstop) {
  451. // Check the Z min end-stop no matter what.
  452. // Good for searching for the center of an induction target.
  453. bool z_min_endstop=(READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  454. if(z_min_endstop && old_z_min_endstop) {
  455. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  456. endstop_z_hit=true;
  457. step_events_completed = current_block->step_event_count;
  458. }
  459. old_z_min_endstop = z_min_endstop;
  460. }
  461. #endif
  462. if ((out_bits & (1 << E_AXIS)) != 0)
  463. { // -direction
  464. //AKU
  465. #ifdef SNMM
  466. if (snmm_extruder == 0 || snmm_extruder == 2)
  467. {
  468. NORM_E_DIR();
  469. }
  470. else
  471. {
  472. REV_E_DIR();
  473. }
  474. #else
  475. REV_E_DIR();
  476. #endif // SNMM
  477. count_direction[E_AXIS] = -1;
  478. }
  479. else
  480. { // +direction
  481. #ifdef SNMM
  482. if (snmm_extruder == 0 || snmm_extruder == 2)
  483. {
  484. REV_E_DIR();
  485. }
  486. else
  487. {
  488. NORM_E_DIR();
  489. }
  490. #else
  491. NORM_E_DIR();
  492. #endif // SNMM
  493. count_direction[E_AXIS] = 1;
  494. }
  495. for(uint8_t i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves)
  496. #ifndef AT90USB
  497. MSerial.checkRx(); // Check for serial chars.
  498. #endif
  499. counter_x += current_block->steps_x;
  500. if (counter_x > 0) {
  501. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  502. counter_x -= current_block->step_event_count;
  503. count_position[X_AXIS]+=count_direction[X_AXIS];
  504. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  505. }
  506. counter_y += current_block->steps_y;
  507. if (counter_y > 0) {
  508. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  509. #ifdef Y_DUAL_STEPPER_DRIVERS
  510. WRITE(Y2_STEP_PIN, !INVERT_Y_STEP_PIN);
  511. #endif
  512. counter_y -= current_block->step_event_count;
  513. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  514. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  515. #ifdef Y_DUAL_STEPPER_DRIVERS
  516. WRITE(Y2_STEP_PIN, INVERT_Y_STEP_PIN);
  517. #endif
  518. }
  519. counter_z += current_block->steps_z;
  520. if (counter_z > 0) {
  521. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  522. #ifdef Z_DUAL_STEPPER_DRIVERS
  523. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  524. #endif
  525. counter_z -= current_block->step_event_count;
  526. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  527. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  528. #ifdef Z_DUAL_STEPPER_DRIVERS
  529. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  530. #endif
  531. }
  532. counter_e += current_block->steps_e;
  533. if (counter_e > 0) {
  534. WRITE_E_STEP(!INVERT_E_STEP_PIN);
  535. counter_e -= current_block->step_event_count;
  536. count_position[E_AXIS]+=count_direction[E_AXIS];
  537. WRITE_E_STEP(INVERT_E_STEP_PIN);
  538. }
  539. step_events_completed += 1;
  540. if(step_events_completed >= current_block->step_event_count) break;
  541. }
  542. // Calculare new timer value
  543. unsigned short timer;
  544. unsigned short step_rate;
  545. if (step_events_completed <= (unsigned long int)current_block->accelerate_until) {
  546. // v = t * a -> acc_step_rate = acceleration_time * current_block->acceleration_rate
  547. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  548. acc_step_rate += current_block->initial_rate;
  549. // upper limit
  550. if(acc_step_rate > current_block->nominal_rate)
  551. acc_step_rate = current_block->nominal_rate;
  552. // step_rate to timer interval
  553. timer = calc_timer(acc_step_rate);
  554. OCR1A = timer;
  555. acceleration_time += timer;
  556. }
  557. else if (step_events_completed > (unsigned long int)current_block->decelerate_after) {
  558. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  559. if(step_rate > acc_step_rate) { // Check step_rate stays positive
  560. step_rate = current_block->final_rate;
  561. }
  562. else {
  563. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  564. }
  565. // lower limit
  566. if(step_rate < current_block->final_rate)
  567. step_rate = current_block->final_rate;
  568. // step_rate to timer interval
  569. timer = calc_timer(step_rate);
  570. OCR1A = timer;
  571. deceleration_time += timer;
  572. }
  573. else {
  574. OCR1A = OCR1A_nominal;
  575. // ensure we're running at the correct step rate, even if we just came off an acceleration
  576. step_loops = step_loops_nominal;
  577. }
  578. // If current block is finished, reset pointer
  579. if (step_events_completed >= current_block->step_event_count) {
  580. current_block = NULL;
  581. plan_discard_current_block();
  582. }
  583. }
  584. }
  585. void st_init()
  586. {
  587. digipot_init(); //Initialize Digipot Motor Current
  588. microstep_init(); //Initialize Microstepping Pins
  589. //Initialize Dir Pins
  590. #if defined(X_DIR_PIN) && X_DIR_PIN > -1
  591. SET_OUTPUT(X_DIR_PIN);
  592. #endif
  593. #if defined(X2_DIR_PIN) && X2_DIR_PIN > -1
  594. SET_OUTPUT(X2_DIR_PIN);
  595. #endif
  596. #if defined(Y_DIR_PIN) && Y_DIR_PIN > -1
  597. SET_OUTPUT(Y_DIR_PIN);
  598. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_DIR_PIN) && (Y2_DIR_PIN > -1)
  599. SET_OUTPUT(Y2_DIR_PIN);
  600. #endif
  601. #endif
  602. #if defined(Z_DIR_PIN) && Z_DIR_PIN > -1
  603. SET_OUTPUT(Z_DIR_PIN);
  604. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_DIR_PIN) && (Z2_DIR_PIN > -1)
  605. SET_OUTPUT(Z2_DIR_PIN);
  606. #endif
  607. #endif
  608. #if defined(E0_DIR_PIN) && E0_DIR_PIN > -1
  609. SET_OUTPUT(E0_DIR_PIN);
  610. #endif
  611. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  612. SET_OUTPUT(E1_DIR_PIN);
  613. #endif
  614. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  615. SET_OUTPUT(E2_DIR_PIN);
  616. #endif
  617. //Initialize Enable Pins - steppers default to disabled.
  618. #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
  619. SET_OUTPUT(X_ENABLE_PIN);
  620. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  621. #endif
  622. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  623. SET_OUTPUT(X2_ENABLE_PIN);
  624. if(!X_ENABLE_ON) WRITE(X2_ENABLE_PIN,HIGH);
  625. #endif
  626. #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
  627. SET_OUTPUT(Y_ENABLE_PIN);
  628. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  629. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_ENABLE_PIN) && (Y2_ENABLE_PIN > -1)
  630. SET_OUTPUT(Y2_ENABLE_PIN);
  631. if(!Y_ENABLE_ON) WRITE(Y2_ENABLE_PIN,HIGH);
  632. #endif
  633. #endif
  634. #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  635. SET_OUTPUT(Z_ENABLE_PIN);
  636. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  637. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_ENABLE_PIN) && (Z2_ENABLE_PIN > -1)
  638. SET_OUTPUT(Z2_ENABLE_PIN);
  639. if(!Z_ENABLE_ON) WRITE(Z2_ENABLE_PIN,HIGH);
  640. #endif
  641. #endif
  642. #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
  643. SET_OUTPUT(E0_ENABLE_PIN);
  644. if(!E_ENABLE_ON) WRITE(E0_ENABLE_PIN,HIGH);
  645. #endif
  646. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  647. SET_OUTPUT(E1_ENABLE_PIN);
  648. if(!E_ENABLE_ON) WRITE(E1_ENABLE_PIN,HIGH);
  649. #endif
  650. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  651. SET_OUTPUT(E2_ENABLE_PIN);
  652. if(!E_ENABLE_ON) WRITE(E2_ENABLE_PIN,HIGH);
  653. #endif
  654. //endstops and pullups
  655. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  656. SET_INPUT(X_MIN_PIN);
  657. #ifdef ENDSTOPPULLUP_XMIN
  658. WRITE(X_MIN_PIN,HIGH);
  659. #endif
  660. #endif
  661. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  662. SET_INPUT(Y_MIN_PIN);
  663. #ifdef ENDSTOPPULLUP_YMIN
  664. WRITE(Y_MIN_PIN,HIGH);
  665. #endif
  666. #endif
  667. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  668. SET_INPUT(Z_MIN_PIN);
  669. #ifdef ENDSTOPPULLUP_ZMIN
  670. WRITE(Z_MIN_PIN,HIGH);
  671. #endif
  672. #endif
  673. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  674. SET_INPUT(X_MAX_PIN);
  675. #ifdef ENDSTOPPULLUP_XMAX
  676. WRITE(X_MAX_PIN,HIGH);
  677. #endif
  678. #endif
  679. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  680. SET_INPUT(Y_MAX_PIN);
  681. #ifdef ENDSTOPPULLUP_YMAX
  682. WRITE(Y_MAX_PIN,HIGH);
  683. #endif
  684. #endif
  685. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  686. SET_INPUT(Z_MAX_PIN);
  687. #ifdef ENDSTOPPULLUP_ZMAX
  688. WRITE(Z_MAX_PIN,HIGH);
  689. #endif
  690. #endif
  691. //Initialize Step Pins
  692. #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
  693. SET_OUTPUT(X_STEP_PIN);
  694. WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
  695. disable_x();
  696. #endif
  697. #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
  698. SET_OUTPUT(X2_STEP_PIN);
  699. WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
  700. disable_x();
  701. #endif
  702. #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
  703. SET_OUTPUT(Y_STEP_PIN);
  704. WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
  705. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
  706. SET_OUTPUT(Y2_STEP_PIN);
  707. WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
  708. #endif
  709. disable_y();
  710. #endif
  711. #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
  712. SET_OUTPUT(Z_STEP_PIN);
  713. WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
  714. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
  715. SET_OUTPUT(Z2_STEP_PIN);
  716. WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
  717. #endif
  718. disable_z();
  719. #endif
  720. #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
  721. SET_OUTPUT(E0_STEP_PIN);
  722. WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
  723. disable_e0();
  724. #endif
  725. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  726. SET_OUTPUT(E1_STEP_PIN);
  727. WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
  728. disable_e1();
  729. #endif
  730. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  731. SET_OUTPUT(E2_STEP_PIN);
  732. WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
  733. disable_e2();
  734. #endif
  735. // waveform generation = 0100 = CTC
  736. TCCR1B &= ~(1<<WGM13);
  737. TCCR1B |= (1<<WGM12);
  738. TCCR1A &= ~(1<<WGM11);
  739. TCCR1A &= ~(1<<WGM10);
  740. // output mode = 00 (disconnected)
  741. TCCR1A &= ~(3<<COM1A0);
  742. TCCR1A &= ~(3<<COM1B0);
  743. // Set the timer pre-scaler
  744. // Generally we use a divider of 8, resulting in a 2MHz timer
  745. // frequency on a 16MHz MCU. If you are going to change this, be
  746. // sure to regenerate speed_lookuptable.h with
  747. // create_speed_lookuptable.py
  748. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  749. OCR1A = 0x4000;
  750. TCNT1 = 0;
  751. ENABLE_STEPPER_DRIVER_INTERRUPT();
  752. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  753. sei();
  754. }
  755. // Block until all buffered steps are executed
  756. void st_synchronize()
  757. {
  758. while( blocks_queued()) {
  759. manage_heater();
  760. // Vojtech: Don't disable motors inside the planner!
  761. manage_inactivity(true);
  762. lcd_update();
  763. }
  764. }
  765. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  766. {
  767. CRITICAL_SECTION_START;
  768. count_position[X_AXIS] = x;
  769. count_position[Y_AXIS] = y;
  770. count_position[Z_AXIS] = z;
  771. count_position[E_AXIS] = e;
  772. CRITICAL_SECTION_END;
  773. }
  774. void st_set_e_position(const long &e)
  775. {
  776. CRITICAL_SECTION_START;
  777. count_position[E_AXIS] = e;
  778. CRITICAL_SECTION_END;
  779. }
  780. long st_get_position(uint8_t axis)
  781. {
  782. long count_pos;
  783. CRITICAL_SECTION_START;
  784. count_pos = count_position[axis];
  785. CRITICAL_SECTION_END;
  786. return count_pos;
  787. }
  788. float st_get_position_mm(uint8_t axis)
  789. {
  790. float steper_position_in_steps = st_get_position(axis);
  791. return steper_position_in_steps / axis_steps_per_unit[axis];
  792. }
  793. void finishAndDisableSteppers()
  794. {
  795. st_synchronize();
  796. disable_x();
  797. disable_y();
  798. disable_z();
  799. disable_e0();
  800. disable_e1();
  801. disable_e2();
  802. }
  803. void quickStop()
  804. {
  805. DISABLE_STEPPER_DRIVER_INTERRUPT();
  806. while (blocks_queued()) plan_discard_current_block();
  807. current_block = NULL;
  808. ENABLE_STEPPER_DRIVER_INTERRUPT();
  809. }
  810. #ifdef BABYSTEPPING
  811. void babystep(const uint8_t axis,const bool direction)
  812. {
  813. //MUST ONLY BE CALLED BY A ISR, it depends on that no other ISR interrupts this
  814. //store initial pin states
  815. switch(axis)
  816. {
  817. case X_AXIS:
  818. {
  819. enable_x();
  820. uint8_t old_x_dir_pin= READ(X_DIR_PIN); //if dualzstepper, both point to same direction.
  821. //setup new step
  822. WRITE(X_DIR_PIN,(INVERT_X_DIR)^direction);
  823. //perform step
  824. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  825. {
  826. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  827. }
  828. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  829. //get old pin state back.
  830. WRITE(X_DIR_PIN,old_x_dir_pin);
  831. }
  832. break;
  833. case Y_AXIS:
  834. {
  835. enable_y();
  836. uint8_t old_y_dir_pin= READ(Y_DIR_PIN); //if dualzstepper, both point to same direction.
  837. //setup new step
  838. WRITE(Y_DIR_PIN,(INVERT_Y_DIR)^direction);
  839. //perform step
  840. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  841. {
  842. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  843. }
  844. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  845. //get old pin state back.
  846. WRITE(Y_DIR_PIN,old_y_dir_pin);
  847. }
  848. break;
  849. case Z_AXIS:
  850. {
  851. enable_z();
  852. uint8_t old_z_dir_pin= READ(Z_DIR_PIN); //if dualzstepper, both point to same direction.
  853. //setup new step
  854. WRITE(Z_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  855. #ifdef Z_DUAL_STEPPER_DRIVERS
  856. WRITE(Z2_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  857. #endif
  858. //perform step
  859. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  860. #ifdef Z_DUAL_STEPPER_DRIVERS
  861. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  862. #endif
  863. //wait a tiny bit
  864. {
  865. volatile float x=1./float(axis+1); //absolutely useless
  866. }
  867. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  868. #ifdef Z_DUAL_STEPPER_DRIVERS
  869. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  870. #endif
  871. //get old pin state back.
  872. WRITE(Z_DIR_PIN,old_z_dir_pin);
  873. #ifdef Z_DUAL_STEPPER_DRIVERS
  874. WRITE(Z2_DIR_PIN,old_z_dir_pin);
  875. #endif
  876. }
  877. break;
  878. default: break;
  879. }
  880. }
  881. #endif //BABYSTEPPING
  882. void digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  883. {
  884. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  885. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  886. SPI.transfer(address); // send in the address and value via SPI:
  887. SPI.transfer(value);
  888. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  889. //delay(10);
  890. #endif
  891. }
  892. void EEPROM_read_st(int pos, uint8_t* value, uint8_t size)
  893. {
  894. do
  895. {
  896. *value = eeprom_read_byte((unsigned char*)pos);
  897. pos++;
  898. value++;
  899. }while(--size);
  900. }
  901. void digipot_init() //Initialize Digipot Motor Current
  902. {
  903. EEPROM_read_st(EEPROM_SILENT,(uint8_t*)&SilentMode,sizeof(SilentMode));
  904. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  905. if(SilentMode == 0){
  906. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT_LOUD;
  907. }else{
  908. const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  909. }
  910. SPI.begin();
  911. pinMode(DIGIPOTSS_PIN, OUTPUT);
  912. for(int i=0;i<=4;i++)
  913. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  914. digipot_current(i,digipot_motor_current[i]);
  915. #endif
  916. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  917. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  918. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  919. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  920. if((SilentMode == 0) || (farm_mode) ){
  921. motor_current_setting[0] = motor_current_setting_loud[0];
  922. motor_current_setting[1] = motor_current_setting_loud[1];
  923. motor_current_setting[2] = motor_current_setting_loud[2];
  924. }else{
  925. motor_current_setting[0] = motor_current_setting_silent[0];
  926. motor_current_setting[1] = motor_current_setting_silent[1];
  927. motor_current_setting[2] = motor_current_setting_silent[2];
  928. }
  929. digipot_current(0, motor_current_setting[0]);
  930. digipot_current(1, motor_current_setting[1]);
  931. digipot_current(2, motor_current_setting[2]);
  932. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  933. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  934. #endif
  935. }
  936. void digipot_current(uint8_t driver, int current)
  937. {
  938. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  939. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  940. digitalPotWrite(digipot_ch[driver], current);
  941. #endif
  942. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  943. if (driver == 0) analogWrite(MOTOR_CURRENT_PWM_XY_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  944. if (driver == 1) analogWrite(MOTOR_CURRENT_PWM_Z_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  945. if (driver == 2) analogWrite(MOTOR_CURRENT_PWM_E_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  946. #endif
  947. }
  948. void microstep_init()
  949. {
  950. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  951. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  952. pinMode(E1_MS1_PIN,OUTPUT);
  953. pinMode(E1_MS2_PIN,OUTPUT);
  954. #endif
  955. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  956. pinMode(X_MS1_PIN,OUTPUT);
  957. pinMode(X_MS2_PIN,OUTPUT);
  958. pinMode(Y_MS1_PIN,OUTPUT);
  959. pinMode(Y_MS2_PIN,OUTPUT);
  960. pinMode(Z_MS1_PIN,OUTPUT);
  961. pinMode(Z_MS2_PIN,OUTPUT);
  962. pinMode(E0_MS1_PIN,OUTPUT);
  963. pinMode(E0_MS2_PIN,OUTPUT);
  964. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  965. #endif
  966. }
  967. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  968. {
  969. if(ms1 > -1) switch(driver)
  970. {
  971. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  972. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  973. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  974. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  975. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  976. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  977. #endif
  978. }
  979. if(ms2 > -1) switch(driver)
  980. {
  981. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  982. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  983. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  984. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  985. #if defined(E1_MS2_PIN) && E1_MS2_PIN > -1
  986. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  987. #endif
  988. }
  989. }
  990. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  991. {
  992. switch(stepping_mode)
  993. {
  994. case 1: microstep_ms(driver,MICROSTEP1); break;
  995. case 2: microstep_ms(driver,MICROSTEP2); break;
  996. case 4: microstep_ms(driver,MICROSTEP4); break;
  997. case 8: microstep_ms(driver,MICROSTEP8); break;
  998. case 16: microstep_ms(driver,MICROSTEP16); break;
  999. }
  1000. }
  1001. void microstep_readings()
  1002. {
  1003. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1004. SERIAL_PROTOCOLPGM("X: ");
  1005. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  1006. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  1007. SERIAL_PROTOCOLPGM("Y: ");
  1008. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  1009. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  1010. SERIAL_PROTOCOLPGM("Z: ");
  1011. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  1012. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  1013. SERIAL_PROTOCOLPGM("E0: ");
  1014. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  1015. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  1016. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1017. SERIAL_PROTOCOLPGM("E1: ");
  1018. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  1019. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  1020. #endif
  1021. }