stepper.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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. #ifdef TMC2130
  30. #include "tmc2130.h"
  31. #endif //TMC2130
  32. #ifdef FILAMENT_SENSOR
  33. #include "fsensor.h"
  34. int fsensor_counter = 0; //counter for e-steps
  35. #endif //FILAMENT_SENSOR
  36. #include "mmu.h"
  37. #include "ConfigurationStore.h"
  38. #ifdef DEBUG_STACK_MONITOR
  39. uint16_t SP_min = 0x21FF;
  40. #endif //DEBUG_STACK_MONITOR
  41. //===========================================================================
  42. //=============================public variables ============================
  43. //===========================================================================
  44. block_t *current_block; // A pointer to the block currently being traced
  45. bool x_min_endstop = false;
  46. bool x_max_endstop = false;
  47. bool y_min_endstop = false;
  48. bool y_max_endstop = false;
  49. bool z_min_endstop = false;
  50. bool z_max_endstop = false;
  51. //===========================================================================
  52. //=============================private variables ============================
  53. //===========================================================================
  54. //static makes it inpossible to be called from outside of this file by extern.!
  55. // Variables used by The Stepper Driver Interrupt
  56. static unsigned char out_bits; // The next stepping-bits to be output
  57. static dda_isteps_t
  58. counter_x, // Counter variables for the bresenham line tracer
  59. counter_y,
  60. counter_z,
  61. counter_e;
  62. volatile dda_usteps_t step_events_completed; // The number of step events executed in the current block
  63. static int32_t acceleration_time, deceleration_time;
  64. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  65. static uint16_t acc_step_rate; // needed for deccelaration start point
  66. static uint8_t step_loops;
  67. static uint16_t OCR1A_nominal;
  68. static uint8_t step_loops_nominal;
  69. volatile long endstops_trigsteps[3]={0,0,0};
  70. volatile long endstops_stepsTotal,endstops_stepsDone;
  71. static volatile bool endstop_x_hit=false;
  72. static volatile bool endstop_y_hit=false;
  73. static volatile bool endstop_z_hit=false;
  74. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  75. bool abort_on_endstop_hit = false;
  76. #endif
  77. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  78. int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
  79. int motor_current_setting_silent[3] = DEFAULT_PWM_MOTOR_CURRENT;
  80. int motor_current_setting_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  81. #endif
  82. #if ( (defined(X_MAX_PIN) && (X_MAX_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_XMAXLIMIT)
  83. static bool old_x_max_endstop=false;
  84. #endif
  85. #if ( (defined(Y_MAX_PIN) && (Y_MAX_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_YMAXLIMIT)
  86. static bool old_y_max_endstop=false;
  87. #endif
  88. static bool old_x_min_endstop=false;
  89. static bool old_y_min_endstop=false;
  90. static bool old_z_min_endstop=false;
  91. static bool old_z_max_endstop=false;
  92. static bool check_endstops = true;
  93. static bool check_z_endstop = false;
  94. static bool z_endstop_invert = false;
  95. volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};
  96. volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1};
  97. #ifdef LIN_ADVANCE
  98. void advance_isr_scheduler();
  99. void advance_isr();
  100. static const uint16_t ADV_NEVER = 0xFFFF;
  101. static uint16_t nextMainISR = 0;
  102. static uint16_t nextAdvanceISR = ADV_NEVER;
  103. static uint16_t eISR_Rate = ADV_NEVER;
  104. static bool use_advance_lead;
  105. static uint16_t current_adv_steps;
  106. static uint16_t final_adv_steps;
  107. static uint16_t max_adv_steps;
  108. static uint32_t LA_decelerate_after;
  109. static volatile int8_t e_steps;
  110. #define _NEXT_ISR(T) nextMainISR = T
  111. #else
  112. #define _NEXT_ISR(T) OCR1A = T
  113. #endif
  114. #ifdef DEBUG_STEPPER_TIMER_MISSED
  115. extern bool stepper_timer_overflow_state;
  116. extern uint16_t stepper_timer_overflow_last;
  117. #endif /* DEBUG_STEPPER_TIMER_MISSED */
  118. //===========================================================================
  119. //=============================functions ============================
  120. //===========================================================================
  121. void checkHitEndstops()
  122. {
  123. if( endstop_x_hit || endstop_y_hit || endstop_z_hit) {
  124. SERIAL_ECHO_START;
  125. SERIAL_ECHORPGM(MSG_ENDSTOPS_HIT);
  126. if(endstop_x_hit) {
  127. SERIAL_ECHOPAIR(" X:",(float)endstops_trigsteps[X_AXIS]/cs.axis_steps_per_unit[X_AXIS]);
  128. // LCD_MESSAGERPGM(CAT2((MSG_ENDSTOPS_HIT), PSTR("X")));
  129. }
  130. if(endstop_y_hit) {
  131. SERIAL_ECHOPAIR(" Y:",(float)endstops_trigsteps[Y_AXIS]/cs.axis_steps_per_unit[Y_AXIS]);
  132. // LCD_MESSAGERPGM(CAT2((MSG_ENDSTOPS_HIT), PSTR("Y")));
  133. }
  134. if(endstop_z_hit) {
  135. SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/cs.axis_steps_per_unit[Z_AXIS]);
  136. // LCD_MESSAGERPGM(CAT2((MSG_ENDSTOPS_HIT),PSTR("Z")));
  137. }
  138. SERIAL_ECHOLN("");
  139. endstop_x_hit=false;
  140. endstop_y_hit=false;
  141. endstop_z_hit=false;
  142. #if defined(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && defined(SDSUPPORT)
  143. if (abort_on_endstop_hit)
  144. {
  145. card.sdprinting = false;
  146. card.closefile();
  147. quickStop();
  148. setTargetHotend0(0);
  149. setTargetHotend1(0);
  150. setTargetHotend2(0);
  151. }
  152. #endif
  153. }
  154. }
  155. bool endstops_hit_on_purpose()
  156. {
  157. bool hit = endstop_x_hit || endstop_y_hit || endstop_z_hit;
  158. endstop_x_hit=false;
  159. endstop_y_hit=false;
  160. endstop_z_hit=false;
  161. return hit;
  162. }
  163. bool endstop_z_hit_on_purpose()
  164. {
  165. bool hit = endstop_z_hit;
  166. endstop_z_hit=false;
  167. return hit;
  168. }
  169. bool enable_endstops(bool check)
  170. {
  171. bool old = check_endstops;
  172. check_endstops = check;
  173. return old;
  174. }
  175. bool enable_z_endstop(bool check)
  176. {
  177. bool old = check_z_endstop;
  178. check_z_endstop = check;
  179. endstop_z_hit = false;
  180. return old;
  181. }
  182. void invert_z_endstop(bool endstop_invert)
  183. {
  184. z_endstop_invert = endstop_invert;
  185. }
  186. // __________________________
  187. // /| |\ _________________ ^
  188. // / | | \ /| |\ |
  189. // / | | \ / | | \ s
  190. // / | | | | | \ p
  191. // / | | | | | \ e
  192. // +-----+------------------------+---+--+---------------+----+ e
  193. // | BLOCK 1 | BLOCK 2 | d
  194. //
  195. // time ----->
  196. //
  197. // The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  198. // first block->accelerate_until step_events_completed, then keeps going at constant speed until
  199. // step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  200. // The slope of acceleration is calculated with the leib ramp alghorithm.
  201. FORCE_INLINE unsigned short calc_timer(uint16_t step_rate) {
  202. unsigned short timer;
  203. if(step_rate > MAX_STEP_FREQUENCY) step_rate = MAX_STEP_FREQUENCY;
  204. if(step_rate > 20000) { // If steprate > 20kHz >> step 4 times
  205. step_rate = (step_rate >> 2)&0x3fff;
  206. step_loops = 4;
  207. }
  208. else if(step_rate > 10000) { // If steprate > 10kHz >> step 2 times
  209. step_rate = (step_rate >> 1)&0x7fff;
  210. step_loops = 2;
  211. }
  212. else {
  213. step_loops = 1;
  214. }
  215. // step_loops = 1;
  216. if(step_rate < (F_CPU/500000)) step_rate = (F_CPU/500000);
  217. step_rate -= (F_CPU/500000); // Correct for minimal speed
  218. if(step_rate >= (8*256)){ // higher step rate
  219. unsigned short table_address = (unsigned short)&speed_lookuptable_fast[(unsigned char)(step_rate>>8)][0];
  220. unsigned char tmp_step_rate = (step_rate & 0x00ff);
  221. unsigned short gain = (unsigned short)pgm_read_word_near(table_address+2);
  222. MultiU16X8toH16(timer, tmp_step_rate, gain);
  223. timer = (unsigned short)pgm_read_word_near(table_address) - timer;
  224. }
  225. else { // lower step rates
  226. unsigned short table_address = (unsigned short)&speed_lookuptable_slow[0][0];
  227. table_address += ((step_rate)>>1) & 0xfffc;
  228. timer = (unsigned short)pgm_read_word_near(table_address);
  229. timer -= (((unsigned short)pgm_read_word_near(table_address+2) * (unsigned char)(step_rate & 0x0007))>>3);
  230. }
  231. if(timer < 100) { timer = 100; MYSERIAL.print(_N("Steprate too high: ")); MYSERIAL.println(step_rate); }//(20kHz this should never happen)////MSG_STEPPER_TOO_HIGH
  232. return timer;
  233. }
  234. // "The Stepper Driver Interrupt" - This timer interrupt is the workhorse.
  235. // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately.
  236. ISR(TIMER1_COMPA_vect) {
  237. #ifdef DEBUG_STACK_MONITOR
  238. uint16_t sp = SPL + 256 * SPH;
  239. if (sp < SP_min) SP_min = sp;
  240. #endif //DEBUG_STACK_MONITOR
  241. #ifdef LIN_ADVANCE
  242. advance_isr_scheduler();
  243. #else
  244. isr();
  245. #endif
  246. // Don't run the ISR faster than possible
  247. // Is there a 8us time left before the next interrupt triggers?
  248. if (OCR1A < TCNT1 + 16) {
  249. #ifdef DEBUG_STEPPER_TIMER_MISSED
  250. // Verify whether the next planned timer interrupt has not been missed already.
  251. // This debugging test takes < 1.125us
  252. // This skews the profiling slightly as the fastest stepper timer
  253. // interrupt repeats at a 100us rate (10kHz).
  254. if (OCR1A + 40 < TCNT1) {
  255. // The interrupt was delayed by more than 20us (which is 1/5th of the 10kHz ISR repeat rate).
  256. // Give a warning.
  257. stepper_timer_overflow_state = true;
  258. stepper_timer_overflow_last = TCNT1 - OCR1A;
  259. // Beep, the beeper will be cleared at the stepper_timer_overflow() called from the main thread.
  260. WRITE(BEEPER, HIGH);
  261. }
  262. #endif
  263. // Fix the next interrupt to be executed after 8us from now.
  264. OCR1A = TCNT1 + 16;
  265. }
  266. }
  267. uint8_t last_dir_bits = 0;
  268. #ifdef BACKLASH_X
  269. uint8_t st_backlash_x = 0;
  270. #endif //BACKLASH_X
  271. #ifdef BACKLASH_Y
  272. uint8_t st_backlash_y = 0;
  273. #endif //BACKLASH_Y
  274. FORCE_INLINE void stepper_next_block()
  275. {
  276. // Anything in the buffer?
  277. //WRITE_NC(LOGIC_ANALYZER_CH2, true);
  278. current_block = plan_get_current_block();
  279. if (current_block != NULL) {
  280. #ifdef BACKLASH_X
  281. if (current_block->steps_x.wide)
  282. { //X-axis movement
  283. if ((current_block->direction_bits ^ last_dir_bits) & 1)
  284. {
  285. printf_P(PSTR("BL %d\n"), (current_block->direction_bits & 1)?st_backlash_x:-st_backlash_x);
  286. if (current_block->direction_bits & 1)
  287. WRITE_NC(X_DIR_PIN, INVERT_X_DIR);
  288. else
  289. WRITE_NC(X_DIR_PIN, !INVERT_X_DIR);
  290. _delay_us(100);
  291. for (uint8_t i = 0; i < st_backlash_x; i++)
  292. {
  293. WRITE_NC(X_STEP_PIN, !INVERT_X_STEP_PIN);
  294. _delay_us(100);
  295. WRITE_NC(X_STEP_PIN, INVERT_X_STEP_PIN);
  296. _delay_us(900);
  297. }
  298. }
  299. last_dir_bits &= ~1;
  300. last_dir_bits |= current_block->direction_bits & 1;
  301. }
  302. #endif
  303. #ifdef BACKLASH_Y
  304. if (current_block->steps_y.wide)
  305. { //Y-axis movement
  306. if ((current_block->direction_bits ^ last_dir_bits) & 2)
  307. {
  308. printf_P(PSTR("BL %d\n"), (current_block->direction_bits & 2)?st_backlash_y:-st_backlash_y);
  309. if (current_block->direction_bits & 2)
  310. WRITE_NC(Y_DIR_PIN, INVERT_Y_DIR);
  311. else
  312. WRITE_NC(Y_DIR_PIN, !INVERT_Y_DIR);
  313. _delay_us(100);
  314. for (uint8_t i = 0; i < st_backlash_y; i++)
  315. {
  316. WRITE_NC(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  317. _delay_us(100);
  318. WRITE_NC(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  319. _delay_us(900);
  320. }
  321. }
  322. last_dir_bits &= ~2;
  323. last_dir_bits |= current_block->direction_bits & 2;
  324. }
  325. #endif
  326. #ifdef FILAMENT_SENSOR
  327. fsensor_counter = 0;
  328. fsensor_st_block_begin(current_block);
  329. #endif //FILAMENT_SENSOR
  330. // The busy flag is set by the plan_get_current_block() call.
  331. // current_block->busy = true;
  332. // Initializes the trapezoid generator from the current block. Called whenever a new
  333. // block begins.
  334. deceleration_time = 0;
  335. // Set the nominal step loops to zero to indicate, that the timer value is not known yet.
  336. // That means, delay the initialization of nominal step rate and step loops until the steady
  337. // state is reached.
  338. step_loops_nominal = 0;
  339. acc_step_rate = uint16_t(current_block->initial_rate);
  340. acceleration_time = calc_timer(acc_step_rate);
  341. #ifdef LIN_ADVANCE
  342. if ((use_advance_lead = current_block->use_advance_lead)) {
  343. LA_decelerate_after = current_block->decelerate_after;
  344. final_adv_steps = current_block->final_adv_steps;
  345. max_adv_steps = current_block->max_adv_steps;
  346. }
  347. #endif
  348. if (current_block->flag & BLOCK_FLAG_DDA_LOWRES) {
  349. counter_x.lo = -(current_block->step_event_count.lo >> 1);
  350. counter_y.lo = counter_x.lo;
  351. counter_z.lo = counter_x.lo;
  352. counter_e.lo = counter_x.lo;
  353. } else {
  354. counter_x.wide = -(current_block->step_event_count.wide >> 1);
  355. counter_y.wide = counter_x.wide;
  356. counter_z.wide = counter_x.wide;
  357. counter_e.wide = counter_x.wide;
  358. }
  359. step_events_completed.wide = 0;
  360. // Set directions.
  361. out_bits = current_block->direction_bits;
  362. // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY)
  363. if((out_bits & (1<<X_AXIS))!=0){
  364. WRITE_NC(X_DIR_PIN, INVERT_X_DIR);
  365. count_direction[X_AXIS]=-1;
  366. } else {
  367. WRITE_NC(X_DIR_PIN, !INVERT_X_DIR);
  368. count_direction[X_AXIS]=1;
  369. }
  370. if((out_bits & (1<<Y_AXIS))!=0){
  371. WRITE_NC(Y_DIR_PIN, INVERT_Y_DIR);
  372. count_direction[Y_AXIS]=-1;
  373. } else {
  374. WRITE_NC(Y_DIR_PIN, !INVERT_Y_DIR);
  375. count_direction[Y_AXIS]=1;
  376. }
  377. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  378. WRITE_NC(Z_DIR_PIN,INVERT_Z_DIR);
  379. count_direction[Z_AXIS]=-1;
  380. } else { // +direction
  381. WRITE_NC(Z_DIR_PIN,!INVERT_Z_DIR);
  382. count_direction[Z_AXIS]=1;
  383. }
  384. if ((out_bits & (1 << E_AXIS)) != 0) { // -direction
  385. #ifndef LIN_ADVANCE
  386. WRITE(E0_DIR_PIN,
  387. #ifdef SNMM
  388. (mmu_extruder == 0 || mmu_extruder == 2) ? !INVERT_E0_DIR :
  389. #endif // SNMM
  390. INVERT_E0_DIR);
  391. #endif /* LIN_ADVANCE */
  392. count_direction[E_AXIS] = -1;
  393. } else { // +direction
  394. #ifndef LIN_ADVANCE
  395. WRITE(E0_DIR_PIN,
  396. #ifdef SNMM
  397. (mmu_extruder == 0 || mmu_extruder == 2) ? INVERT_E0_DIR :
  398. #endif // SNMM
  399. !INVERT_E0_DIR);
  400. #endif /* LIN_ADVANCE */
  401. count_direction[E_AXIS] = 1;
  402. }
  403. }
  404. else {
  405. _NEXT_ISR(2000); // 1kHz.
  406. }
  407. //WRITE_NC(LOGIC_ANALYZER_CH2, false);
  408. }
  409. // Check limit switches.
  410. FORCE_INLINE void stepper_check_endstops()
  411. {
  412. if(check_endstops)
  413. {
  414. #ifndef COREXY
  415. if ((out_bits & (1<<X_AXIS)) != 0) // stepping along -X axis
  416. #else
  417. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) != 0)) //-X occurs for -A and -B
  418. #endif
  419. {
  420. #if ( (defined(X_MIN_PIN) && (X_MIN_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_XMINLIMIT)
  421. #ifdef TMC2130_SG_HOMING
  422. // Stall guard homing turned on
  423. x_min_endstop = (READ(X_TMC2130_DIAG) != 0);
  424. #else
  425. // Normal homing
  426. x_min_endstop = (READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
  427. #endif
  428. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x.wide > 0)) {
  429. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  430. endstop_x_hit=true;
  431. step_events_completed.wide = current_block->step_event_count.wide;
  432. }
  433. old_x_min_endstop = x_min_endstop;
  434. #endif
  435. } else { // +direction
  436. #if ( (defined(X_MAX_PIN) && (X_MAX_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_XMAXLIMIT)
  437. #ifdef TMC2130_SG_HOMING
  438. // Stall guard homing turned on
  439. x_max_endstop = (READ(X_TMC2130_DIAG) != 0);
  440. #else
  441. // Normal homing
  442. x_max_endstop = (READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
  443. #endif
  444. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x.wide > 0)){
  445. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  446. endstop_x_hit=true;
  447. step_events_completed.wide = current_block->step_event_count.wide;
  448. }
  449. old_x_max_endstop = x_max_endstop;
  450. #endif
  451. }
  452. #ifndef COREXY
  453. if ((out_bits & (1<<Y_AXIS)) != 0) // -direction
  454. #else
  455. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) == 0)) // -Y occurs for -A and +B
  456. #endif
  457. {
  458. #if ( (defined(Y_MIN_PIN) && (Y_MIN_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_YMINLIMIT)
  459. #ifdef TMC2130_SG_HOMING
  460. // Stall guard homing turned on
  461. y_min_endstop = (READ(Y_TMC2130_DIAG) != 0);
  462. #else
  463. // Normal homing
  464. y_min_endstop = (READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
  465. #endif
  466. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y.wide > 0)) {
  467. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  468. endstop_y_hit=true;
  469. step_events_completed.wide = current_block->step_event_count.wide;
  470. }
  471. old_y_min_endstop = y_min_endstop;
  472. #endif
  473. } else { // +direction
  474. #if ( (defined(Y_MAX_PIN) && (Y_MAX_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_YMAXLIMIT)
  475. #ifdef TMC2130_SG_HOMING
  476. // Stall guard homing turned on
  477. y_max_endstop = (READ(Y_TMC2130_DIAG) != 0);
  478. #else
  479. // Normal homing
  480. y_max_endstop = (READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
  481. #endif
  482. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y.wide > 0)){
  483. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  484. endstop_y_hit=true;
  485. step_events_completed.wide = current_block->step_event_count.wide;
  486. }
  487. old_y_max_endstop = y_max_endstop;
  488. #endif
  489. }
  490. if ((out_bits & (1<<Z_AXIS)) != 0) // -direction
  491. {
  492. #if defined(Z_MIN_PIN) && (Z_MIN_PIN > -1) && !defined(DEBUG_DISABLE_ZMINLIMIT)
  493. if (! check_z_endstop) {
  494. #ifdef TMC2130_SG_HOMING
  495. // Stall guard homing turned on
  496. #ifdef TMC2130_STEALTH_Z
  497. if ((tmc2130_mode == TMC2130_MODE_SILENT) && !(tmc2130_sg_homing_axes_mask & 0x04))
  498. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  499. else
  500. #endif //TMC2130_STEALTH_Z
  501. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING) || (READ(Z_TMC2130_DIAG) != 0);
  502. #else
  503. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  504. #endif //TMC2130_SG_HOMING
  505. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z.wide > 0)) {
  506. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  507. endstop_z_hit=true;
  508. step_events_completed.wide = current_block->step_event_count.wide;
  509. }
  510. old_z_min_endstop = z_min_endstop;
  511. }
  512. #endif
  513. } else { // +direction
  514. #if defined(Z_MAX_PIN) && (Z_MAX_PIN > -1) && !defined(DEBUG_DISABLE_ZMAXLIMIT)
  515. #ifdef TMC2130_SG_HOMING
  516. // Stall guard homing turned on
  517. #ifdef TMC2130_STEALTH_Z
  518. if ((tmc2130_mode == TMC2130_MODE_SILENT) && !(tmc2130_sg_homing_axes_mask & 0x04))
  519. z_max_endstop = false;
  520. else
  521. #endif //TMC2130_STEALTH_Z
  522. z_max_endstop = (READ(Z_TMC2130_DIAG) != 0);
  523. #else
  524. z_max_endstop = (READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING);
  525. #endif //TMC2130_SG_HOMING
  526. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z.wide > 0)) {
  527. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  528. endstop_z_hit=true;
  529. step_events_completed.wide = current_block->step_event_count.wide;
  530. }
  531. old_z_max_endstop = z_max_endstop;
  532. #endif
  533. }
  534. }
  535. // Supporting stopping on a trigger of the Z-stop induction sensor, not only for the Z-minus movements.
  536. #if defined(Z_MIN_PIN) && (Z_MIN_PIN > -1) && !defined(DEBUG_DISABLE_ZMINLIMIT)
  537. if (check_z_endstop) {
  538. // Check the Z min end-stop no matter what.
  539. // Good for searching for the center of an induction target.
  540. #ifdef TMC2130_SG_HOMING
  541. // Stall guard homing turned on
  542. #ifdef TMC2130_STEALTH_Z
  543. if ((tmc2130_mode == TMC2130_MODE_SILENT) && !(tmc2130_sg_homing_axes_mask & 0x04))
  544. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  545. else
  546. #endif //TMC2130_STEALTH_Z
  547. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING) || (READ(Z_TMC2130_DIAG) != 0);
  548. #else
  549. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  550. #endif //TMC2130_SG_HOMING
  551. if(z_min_endstop && old_z_min_endstop) {
  552. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  553. endstop_z_hit=true;
  554. step_events_completed.wide = current_block->step_event_count.wide;
  555. }
  556. old_z_min_endstop = z_min_endstop;
  557. }
  558. #endif
  559. }
  560. FORCE_INLINE void stepper_tick_lowres()
  561. {
  562. for (uint8_t i=0; i < step_loops; ++ i) { // Take multiple steps per interrupt (For high speed moves)
  563. MSerial.checkRx(); // Check for serial chars.
  564. // Step in X axis
  565. counter_x.lo += current_block->steps_x.lo;
  566. if (counter_x.lo > 0) {
  567. WRITE_NC(X_STEP_PIN, !INVERT_X_STEP_PIN);
  568. #ifdef DEBUG_XSTEP_DUP_PIN
  569. WRITE_NC(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  570. #endif //DEBUG_XSTEP_DUP_PIN
  571. counter_x.lo -= current_block->step_event_count.lo;
  572. count_position[X_AXIS]+=count_direction[X_AXIS];
  573. WRITE_NC(X_STEP_PIN, INVERT_X_STEP_PIN);
  574. #ifdef DEBUG_XSTEP_DUP_PIN
  575. WRITE_NC(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  576. #endif //DEBUG_XSTEP_DUP_PIN
  577. }
  578. // Step in Y axis
  579. counter_y.lo += current_block->steps_y.lo;
  580. if (counter_y.lo > 0) {
  581. WRITE_NC(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  582. #ifdef DEBUG_YSTEP_DUP_PIN
  583. WRITE_NC(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  584. #endif //DEBUG_YSTEP_DUP_PIN
  585. counter_y.lo -= current_block->step_event_count.lo;
  586. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  587. WRITE_NC(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  588. #ifdef DEBUG_YSTEP_DUP_PIN
  589. WRITE_NC(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  590. #endif //DEBUG_YSTEP_DUP_PIN
  591. }
  592. // Step in Z axis
  593. counter_z.lo += current_block->steps_z.lo;
  594. if (counter_z.lo > 0) {
  595. WRITE_NC(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  596. counter_z.lo -= current_block->step_event_count.lo;
  597. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  598. WRITE_NC(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  599. }
  600. // Step in E axis
  601. counter_e.lo += current_block->steps_e.lo;
  602. if (counter_e.lo > 0) {
  603. #ifndef LIN_ADVANCE
  604. WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  605. #endif /* LIN_ADVANCE */
  606. counter_e.lo -= current_block->step_event_count.lo;
  607. count_position[E_AXIS] += count_direction[E_AXIS];
  608. #ifdef LIN_ADVANCE
  609. e_steps += count_direction[E_AXIS];
  610. #else
  611. #ifdef FILAMENT_SENSOR
  612. ++ fsensor_counter;
  613. #endif //FILAMENT_SENSOR
  614. WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN);
  615. #endif
  616. }
  617. if(++ step_events_completed.lo >= current_block->step_event_count.lo)
  618. break;
  619. }
  620. }
  621. FORCE_INLINE void stepper_tick_highres()
  622. {
  623. for (uint8_t i=0; i < step_loops; ++ i) { // Take multiple steps per interrupt (For high speed moves)
  624. MSerial.checkRx(); // Check for serial chars.
  625. // Step in X axis
  626. counter_x.wide += current_block->steps_x.wide;
  627. if (counter_x.wide > 0) {
  628. WRITE_NC(X_STEP_PIN, !INVERT_X_STEP_PIN);
  629. #ifdef DEBUG_XSTEP_DUP_PIN
  630. WRITE_NC(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  631. #endif //DEBUG_XSTEP_DUP_PIN
  632. counter_x.wide -= current_block->step_event_count.wide;
  633. count_position[X_AXIS]+=count_direction[X_AXIS];
  634. WRITE_NC(X_STEP_PIN, INVERT_X_STEP_PIN);
  635. #ifdef DEBUG_XSTEP_DUP_PIN
  636. WRITE_NC(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  637. #endif //DEBUG_XSTEP_DUP_PIN
  638. }
  639. // Step in Y axis
  640. counter_y.wide += current_block->steps_y.wide;
  641. if (counter_y.wide > 0) {
  642. WRITE_NC(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  643. #ifdef DEBUG_YSTEP_DUP_PIN
  644. WRITE_NC(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  645. #endif //DEBUG_YSTEP_DUP_PIN
  646. counter_y.wide -= current_block->step_event_count.wide;
  647. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  648. WRITE_NC(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  649. #ifdef DEBUG_YSTEP_DUP_PIN
  650. WRITE_NC(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  651. #endif //DEBUG_YSTEP_DUP_PIN
  652. }
  653. // Step in Z axis
  654. counter_z.wide += current_block->steps_z.wide;
  655. if (counter_z.wide > 0) {
  656. WRITE_NC(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  657. counter_z.wide -= current_block->step_event_count.wide;
  658. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  659. WRITE_NC(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  660. }
  661. // Step in E axis
  662. counter_e.wide += current_block->steps_e.wide;
  663. if (counter_e.wide > 0) {
  664. #ifndef LIN_ADVANCE
  665. WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  666. #endif /* LIN_ADVANCE */
  667. counter_e.wide -= current_block->step_event_count.wide;
  668. count_position[E_AXIS]+=count_direction[E_AXIS];
  669. #ifdef LIN_ADVANCE
  670. e_steps += count_direction[E_AXIS];
  671. #else
  672. #ifdef FILAMENT_SENSOR
  673. ++ fsensor_counter;
  674. #endif //FILAMENT_SENSOR
  675. WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN);
  676. #endif
  677. }
  678. if(++ step_events_completed.wide >= current_block->step_event_count.wide)
  679. break;
  680. }
  681. }
  682. FORCE_INLINE void isr() {
  683. //WRITE_NC(LOGIC_ANALYZER_CH0, true);
  684. //if (UVLO) uvlo();
  685. // If there is no current block, attempt to pop one from the buffer
  686. if (current_block == NULL)
  687. stepper_next_block();
  688. if (current_block != NULL)
  689. {
  690. stepper_check_endstops();
  691. if (current_block->flag & BLOCK_FLAG_DDA_LOWRES)
  692. stepper_tick_lowres();
  693. else
  694. stepper_tick_highres();
  695. // Calculare new timer value
  696. // 13.38-14.63us for steady state,
  697. // 25.12us for acceleration / deceleration.
  698. {
  699. //WRITE_NC(LOGIC_ANALYZER_CH1, true);
  700. if (step_events_completed.wide <= (unsigned long int)current_block->accelerate_until) {
  701. // v = t * a -> acc_step_rate = acceleration_time * current_block->acceleration_rate
  702. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  703. acc_step_rate += uint16_t(current_block->initial_rate);
  704. // upper limit
  705. if(acc_step_rate > uint16_t(current_block->nominal_rate))
  706. acc_step_rate = current_block->nominal_rate;
  707. // step_rate to timer interval
  708. uint16_t timer = calc_timer(acc_step_rate);
  709. _NEXT_ISR(timer);
  710. acceleration_time += timer;
  711. #ifdef LIN_ADVANCE
  712. if (current_block->use_advance_lead) {
  713. if (step_events_completed.wide == (unsigned long int)step_loops || (e_steps && eISR_Rate != current_block->advance_speed)) {
  714. nextAdvanceISR = 0; // Wake up eISR on first acceleration loop and fire ISR if final adv_rate is reached
  715. eISR_Rate = current_block->advance_speed;
  716. }
  717. }
  718. else {
  719. eISR_Rate = ADV_NEVER;
  720. if (e_steps) nextAdvanceISR = 0;
  721. }
  722. #endif
  723. }
  724. else if (step_events_completed.wide > (unsigned long int)current_block->decelerate_after) {
  725. uint16_t step_rate;
  726. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  727. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  728. if ((step_rate & 0x8000) || step_rate < uint16_t(current_block->final_rate)) {
  729. // Result is negative or too small.
  730. step_rate = uint16_t(current_block->final_rate);
  731. }
  732. // Step_rate to timer interval.
  733. uint16_t timer = calc_timer(step_rate);
  734. _NEXT_ISR(timer);
  735. deceleration_time += timer;
  736. #ifdef LIN_ADVANCE
  737. if (current_block->use_advance_lead) {
  738. if (step_events_completed.wide <= (unsigned long int)current_block->decelerate_after + step_loops || (e_steps && eISR_Rate != current_block->advance_speed)) {
  739. nextAdvanceISR = 0; // Wake up eISR on first deceleration loop
  740. eISR_Rate = current_block->advance_speed;
  741. }
  742. }
  743. else {
  744. eISR_Rate = ADV_NEVER;
  745. if (e_steps) nextAdvanceISR = 0;
  746. }
  747. #endif
  748. }
  749. else {
  750. #ifdef LIN_ADVANCE
  751. // If we have esteps to execute, fire the next advance_isr "now"
  752. if (e_steps && eISR_Rate != current_block->advance_speed) nextAdvanceISR = 0;
  753. #endif
  754. if (! step_loops_nominal) {
  755. // Calculation of the steady state timer rate has been delayed to the 1st tick of the steady state to lower
  756. // the initial interrupt blocking.
  757. OCR1A_nominal = calc_timer(uint16_t(current_block->nominal_rate));
  758. step_loops_nominal = step_loops;
  759. }
  760. _NEXT_ISR(OCR1A_nominal);
  761. }
  762. //WRITE_NC(LOGIC_ANALYZER_CH1, false);
  763. }
  764. // If current block is finished, reset pointer
  765. if (step_events_completed.wide >= current_block->step_event_count.wide) {
  766. #ifdef FILAMENT_SENSOR
  767. fsensor_st_block_chunk(current_block, fsensor_counter);
  768. fsensor_counter = 0;
  769. #endif //FILAMENT_SENSOR
  770. current_block = NULL;
  771. plan_discard_current_block();
  772. }
  773. #ifdef FILAMENT_SENSOR
  774. else if ((fsensor_counter >= fsensor_chunk_len))
  775. {
  776. fsensor_st_block_chunk(current_block, fsensor_counter);
  777. fsensor_counter = 0;
  778. }
  779. #endif //FILAMENT_SENSOR
  780. }
  781. #ifdef TMC2130
  782. tmc2130_st_isr();
  783. #endif //TMC2130
  784. //WRITE_NC(LOGIC_ANALYZER_CH0, false);
  785. }
  786. #ifdef LIN_ADVANCE
  787. // Timer interrupt for E. e_steps is set in the main routine.
  788. FORCE_INLINE void advance_isr() {
  789. if (use_advance_lead) {
  790. if (step_events_completed.wide > LA_decelerate_after && current_adv_steps > final_adv_steps) {
  791. e_steps--;
  792. current_adv_steps--;
  793. nextAdvanceISR = eISR_Rate;
  794. }
  795. else if (step_events_completed.wide < LA_decelerate_after && current_adv_steps < max_adv_steps) {
  796. e_steps++;
  797. current_adv_steps++;
  798. nextAdvanceISR = eISR_Rate;
  799. }
  800. else {
  801. nextAdvanceISR = ADV_NEVER;
  802. eISR_Rate = ADV_NEVER;
  803. }
  804. }
  805. else
  806. nextAdvanceISR = ADV_NEVER;
  807. if (e_steps) {
  808. MSerial.checkRx(); // Check for serial chars.
  809. bool dir =
  810. #ifdef SNMM
  811. ((e_steps < 0) == (snmm_extruder & 1))
  812. #else
  813. (e_steps < 0)
  814. #endif
  815. ? INVERT_E0_DIR : !INVERT_E0_DIR; //If we have SNMM, reverse every second extruder.
  816. WRITE(E0_DIR_PIN, dir);
  817. if(e_steps < 0) e_steps = -e_steps;
  818. fsensor_counter += e_steps;
  819. while (e_steps) {
  820. WRITE_NC(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  821. --e_steps;
  822. WRITE_NC(E0_STEP_PIN, INVERT_E_STEP_PIN);
  823. }
  824. }
  825. }
  826. FORCE_INLINE void advance_isr_scheduler() {
  827. // Run main stepping ISR if flagged
  828. if (!nextMainISR) isr();
  829. // Run Advance stepping ISR if flagged
  830. if (!nextAdvanceISR) advance_isr();
  831. // Is the next advance ISR scheduled before the next main ISR?
  832. if (nextAdvanceISR <= nextMainISR) {
  833. // Set up the next interrupt
  834. OCR1A = nextAdvanceISR;
  835. // New interval for the next main ISR
  836. if (nextMainISR) nextMainISR -= nextAdvanceISR;
  837. // Will call Stepper::advance_isr on the next interrupt
  838. nextAdvanceISR = 0;
  839. }
  840. else {
  841. // The next main ISR comes first
  842. OCR1A = nextMainISR;
  843. // New interval for the next advance ISR, if any
  844. if (nextAdvanceISR && nextAdvanceISR != ADV_NEVER)
  845. nextAdvanceISR -= nextMainISR;
  846. // Will call Stepper::isr on the next interrupt
  847. nextMainISR = 0;
  848. }
  849. }
  850. void clear_current_adv_vars() {
  851. e_steps = 0;
  852. current_adv_steps = 0;
  853. }
  854. #endif // LIN_ADVANCE
  855. void st_init()
  856. {
  857. #ifdef TMC2130
  858. tmc2130_init();
  859. #endif //TMC2130
  860. st_current_init(); //Initialize Digipot Motor Current
  861. microstep_init(); //Initialize Microstepping Pins
  862. //Initialize Dir Pins
  863. #if defined(X_DIR_PIN) && X_DIR_PIN > -1
  864. SET_OUTPUT(X_DIR_PIN);
  865. #endif
  866. #if defined(X2_DIR_PIN) && X2_DIR_PIN > -1
  867. SET_OUTPUT(X2_DIR_PIN);
  868. #endif
  869. #if defined(Y_DIR_PIN) && Y_DIR_PIN > -1
  870. SET_OUTPUT(Y_DIR_PIN);
  871. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_DIR_PIN) && (Y2_DIR_PIN > -1)
  872. SET_OUTPUT(Y2_DIR_PIN);
  873. #endif
  874. #endif
  875. #if defined(Z_DIR_PIN) && Z_DIR_PIN > -1
  876. SET_OUTPUT(Z_DIR_PIN);
  877. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_DIR_PIN) && (Z2_DIR_PIN > -1)
  878. SET_OUTPUT(Z2_DIR_PIN);
  879. #endif
  880. #endif
  881. #if defined(E0_DIR_PIN) && E0_DIR_PIN > -1
  882. SET_OUTPUT(E0_DIR_PIN);
  883. #endif
  884. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  885. SET_OUTPUT(E1_DIR_PIN);
  886. #endif
  887. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  888. SET_OUTPUT(E2_DIR_PIN);
  889. #endif
  890. //Initialize Enable Pins - steppers default to disabled.
  891. #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
  892. SET_OUTPUT(X_ENABLE_PIN);
  893. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  894. #endif
  895. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  896. SET_OUTPUT(X2_ENABLE_PIN);
  897. if(!X_ENABLE_ON) WRITE(X2_ENABLE_PIN,HIGH);
  898. #endif
  899. #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
  900. SET_OUTPUT(Y_ENABLE_PIN);
  901. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  902. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_ENABLE_PIN) && (Y2_ENABLE_PIN > -1)
  903. SET_OUTPUT(Y2_ENABLE_PIN);
  904. if(!Y_ENABLE_ON) WRITE(Y2_ENABLE_PIN,HIGH);
  905. #endif
  906. #endif
  907. #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  908. SET_OUTPUT(Z_ENABLE_PIN);
  909. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  910. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_ENABLE_PIN) && (Z2_ENABLE_PIN > -1)
  911. SET_OUTPUT(Z2_ENABLE_PIN);
  912. if(!Z_ENABLE_ON) WRITE(Z2_ENABLE_PIN,HIGH);
  913. #endif
  914. #endif
  915. #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
  916. SET_OUTPUT(E0_ENABLE_PIN);
  917. if(!E_ENABLE_ON) WRITE(E0_ENABLE_PIN,HIGH);
  918. #endif
  919. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  920. SET_OUTPUT(E1_ENABLE_PIN);
  921. if(!E_ENABLE_ON) WRITE(E1_ENABLE_PIN,HIGH);
  922. #endif
  923. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  924. SET_OUTPUT(E2_ENABLE_PIN);
  925. if(!E_ENABLE_ON) WRITE(E2_ENABLE_PIN,HIGH);
  926. #endif
  927. //endstops and pullups
  928. #ifdef TMC2130_SG_HOMING
  929. SET_INPUT(X_TMC2130_DIAG);
  930. WRITE(X_TMC2130_DIAG,HIGH);
  931. SET_INPUT(Y_TMC2130_DIAG);
  932. WRITE(Y_TMC2130_DIAG,HIGH);
  933. SET_INPUT(Z_TMC2130_DIAG);
  934. WRITE(Z_TMC2130_DIAG,HIGH);
  935. SET_INPUT(E0_TMC2130_DIAG);
  936. WRITE(E0_TMC2130_DIAG,HIGH);
  937. #endif
  938. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  939. SET_INPUT(X_MIN_PIN);
  940. #ifdef ENDSTOPPULLUP_XMIN
  941. WRITE(X_MIN_PIN,HIGH);
  942. #endif
  943. #endif
  944. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  945. SET_INPUT(Y_MIN_PIN);
  946. #ifdef ENDSTOPPULLUP_YMIN
  947. WRITE(Y_MIN_PIN,HIGH);
  948. #endif
  949. #endif
  950. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  951. SET_INPUT(Z_MIN_PIN);
  952. #ifdef ENDSTOPPULLUP_ZMIN
  953. WRITE(Z_MIN_PIN,HIGH);
  954. #endif
  955. #endif
  956. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  957. SET_INPUT(X_MAX_PIN);
  958. #ifdef ENDSTOPPULLUP_XMAX
  959. WRITE(X_MAX_PIN,HIGH);
  960. #endif
  961. #endif
  962. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  963. SET_INPUT(Y_MAX_PIN);
  964. #ifdef ENDSTOPPULLUP_YMAX
  965. WRITE(Y_MAX_PIN,HIGH);
  966. #endif
  967. #endif
  968. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  969. SET_INPUT(Z_MAX_PIN);
  970. #ifdef ENDSTOPPULLUP_ZMAX
  971. WRITE(Z_MAX_PIN,HIGH);
  972. #endif
  973. #endif
  974. #if (defined(FANCHECK) && defined(TACH_0) && (TACH_0 > -1))
  975. SET_INPUT(TACH_0);
  976. #ifdef TACH0PULLUP
  977. WRITE(TACH_0, HIGH);
  978. #endif
  979. #endif
  980. //Initialize Step Pins
  981. #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
  982. SET_OUTPUT(X_STEP_PIN);
  983. WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
  984. #ifdef DEBUG_XSTEP_DUP_PIN
  985. SET_OUTPUT(DEBUG_XSTEP_DUP_PIN);
  986. WRITE(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  987. #endif //DEBUG_XSTEP_DUP_PIN
  988. disable_x();
  989. #endif
  990. #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
  991. SET_OUTPUT(X2_STEP_PIN);
  992. WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
  993. disable_x();
  994. #endif
  995. #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
  996. SET_OUTPUT(Y_STEP_PIN);
  997. WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
  998. #ifdef DEBUG_YSTEP_DUP_PIN
  999. SET_OUTPUT(DEBUG_YSTEP_DUP_PIN);
  1000. WRITE(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  1001. #endif //DEBUG_YSTEP_DUP_PIN
  1002. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
  1003. SET_OUTPUT(Y2_STEP_PIN);
  1004. WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
  1005. #endif
  1006. disable_y();
  1007. #endif
  1008. #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
  1009. SET_OUTPUT(Z_STEP_PIN);
  1010. WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
  1011. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
  1012. SET_OUTPUT(Z2_STEP_PIN);
  1013. WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
  1014. #endif
  1015. disable_z();
  1016. #endif
  1017. #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
  1018. SET_OUTPUT(E0_STEP_PIN);
  1019. WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
  1020. disable_e0();
  1021. #endif
  1022. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  1023. SET_OUTPUT(E1_STEP_PIN);
  1024. WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
  1025. disable_e1();
  1026. #endif
  1027. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  1028. SET_OUTPUT(E2_STEP_PIN);
  1029. WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
  1030. disable_e2();
  1031. #endif
  1032. // waveform generation = 0100 = CTC
  1033. TCCR1B &= ~(1<<WGM13);
  1034. TCCR1B |= (1<<WGM12);
  1035. TCCR1A &= ~(1<<WGM11);
  1036. TCCR1A &= ~(1<<WGM10);
  1037. // output mode = 00 (disconnected)
  1038. TCCR1A &= ~(3<<COM1A0);
  1039. TCCR1A &= ~(3<<COM1B0);
  1040. // Set the timer pre-scaler
  1041. // Generally we use a divider of 8, resulting in a 2MHz timer
  1042. // frequency on a 16MHz MCU. If you are going to change this, be
  1043. // sure to regenerate speed_lookuptable.h with
  1044. // create_speed_lookuptable.py
  1045. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  1046. #ifdef LIN_ADVANCE
  1047. // Reset the state for the next advance scheduler as well
  1048. nextMainISR = 0;
  1049. nextAdvanceISR = ADV_NEVER;
  1050. eISR_Rate = ADV_NEVER;
  1051. clear_current_adv_vars();
  1052. #endif
  1053. st_reset_timer();
  1054. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  1055. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1056. sei();
  1057. }
  1058. void st_reset_timer()
  1059. {
  1060. // Clear a possible pending interrupt on OCR1A overflow.
  1061. TIFR1 |= 1 << OCF1A;
  1062. // Reset the counter.
  1063. TCNT1 = 0;
  1064. // Wake up after 1ms from now.
  1065. OCR1A = 2000;
  1066. #ifdef LIN_ADVANCE
  1067. // If an eISR was pending, reschedule too
  1068. if (nextAdvanceISR <= nextMainISR)
  1069. nextAdvanceISR = 0;
  1070. nextMainISR = 0;
  1071. #endif
  1072. }
  1073. // Block until all buffered steps are executed
  1074. void st_synchronize()
  1075. {
  1076. while(blocks_queued())
  1077. {
  1078. #ifdef TMC2130
  1079. manage_heater();
  1080. // Vojtech: Don't disable motors inside the planner!
  1081. if (!tmc2130_update_sg())
  1082. {
  1083. manage_inactivity(true);
  1084. lcd_update(0);
  1085. }
  1086. #else //TMC2130
  1087. manage_heater();
  1088. // Vojtech: Don't disable motors inside the planner!
  1089. manage_inactivity(true);
  1090. lcd_update(0);
  1091. #endif //TMC2130
  1092. }
  1093. }
  1094. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  1095. {
  1096. CRITICAL_SECTION_START;
  1097. // Copy 4x4B.
  1098. // This block locks the interrupts globally for 4.56 us,
  1099. // which corresponds to a maximum repeat frequency of 219.18 kHz.
  1100. // This blocking is safe in the context of a 10kHz stepper driver interrupt
  1101. // or a 115200 Bd serial line receive interrupt, which will not trigger faster than 12kHz.
  1102. count_position[X_AXIS] = x;
  1103. count_position[Y_AXIS] = y;
  1104. count_position[Z_AXIS] = z;
  1105. count_position[E_AXIS] = e;
  1106. CRITICAL_SECTION_END;
  1107. }
  1108. void st_set_e_position(const long &e)
  1109. {
  1110. CRITICAL_SECTION_START;
  1111. count_position[E_AXIS] = e;
  1112. CRITICAL_SECTION_END;
  1113. }
  1114. long st_get_position(uint8_t axis)
  1115. {
  1116. long count_pos;
  1117. CRITICAL_SECTION_START;
  1118. count_pos = count_position[axis];
  1119. CRITICAL_SECTION_END;
  1120. return count_pos;
  1121. }
  1122. void st_get_position_xy(long &x, long &y)
  1123. {
  1124. CRITICAL_SECTION_START;
  1125. x = count_position[X_AXIS];
  1126. y = count_position[Y_AXIS];
  1127. CRITICAL_SECTION_END;
  1128. }
  1129. float st_get_position_mm(uint8_t axis)
  1130. {
  1131. float steper_position_in_steps = st_get_position(axis);
  1132. return steper_position_in_steps / cs.axis_steps_per_unit[axis];
  1133. }
  1134. void finishAndDisableSteppers()
  1135. {
  1136. st_synchronize();
  1137. disable_x();
  1138. disable_y();
  1139. disable_z();
  1140. disable_e0();
  1141. disable_e1();
  1142. disable_e2();
  1143. }
  1144. void quickStop()
  1145. {
  1146. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1147. while (blocks_queued()) plan_discard_current_block();
  1148. current_block = NULL;
  1149. st_reset_timer();
  1150. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1151. }
  1152. #ifdef BABYSTEPPING
  1153. void babystep(const uint8_t axis,const bool direction)
  1154. {
  1155. //MUST ONLY BE CALLED BY A ISR, it depends on that no other ISR interrupts this
  1156. //store initial pin states
  1157. switch(axis)
  1158. {
  1159. case X_AXIS:
  1160. {
  1161. enable_x();
  1162. uint8_t old_x_dir_pin= READ(X_DIR_PIN); //if dualzstepper, both point to same direction.
  1163. //setup new step
  1164. WRITE(X_DIR_PIN,(INVERT_X_DIR)^direction);
  1165. //perform step
  1166. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  1167. #ifdef DEBUG_XSTEP_DUP_PIN
  1168. WRITE(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  1169. #endif //DEBUG_XSTEP_DUP_PIN
  1170. delayMicroseconds(1);
  1171. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  1172. #ifdef DEBUG_XSTEP_DUP_PIN
  1173. WRITE(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  1174. #endif //DEBUG_XSTEP_DUP_PIN
  1175. //get old pin state back.
  1176. WRITE(X_DIR_PIN,old_x_dir_pin);
  1177. }
  1178. break;
  1179. case Y_AXIS:
  1180. {
  1181. enable_y();
  1182. uint8_t old_y_dir_pin= READ(Y_DIR_PIN); //if dualzstepper, both point to same direction.
  1183. //setup new step
  1184. WRITE(Y_DIR_PIN,(INVERT_Y_DIR)^direction);
  1185. //perform step
  1186. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  1187. #ifdef DEBUG_YSTEP_DUP_PIN
  1188. WRITE(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  1189. #endif //DEBUG_YSTEP_DUP_PIN
  1190. delayMicroseconds(1);
  1191. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  1192. #ifdef DEBUG_YSTEP_DUP_PIN
  1193. WRITE(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  1194. #endif //DEBUG_YSTEP_DUP_PIN
  1195. //get old pin state back.
  1196. WRITE(Y_DIR_PIN,old_y_dir_pin);
  1197. }
  1198. break;
  1199. case Z_AXIS:
  1200. {
  1201. enable_z();
  1202. uint8_t old_z_dir_pin= READ(Z_DIR_PIN); //if dualzstepper, both point to same direction.
  1203. //setup new step
  1204. WRITE(Z_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1205. #ifdef Z_DUAL_STEPPER_DRIVERS
  1206. WRITE(Z2_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1207. #endif
  1208. //perform step
  1209. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  1210. #ifdef Z_DUAL_STEPPER_DRIVERS
  1211. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  1212. #endif
  1213. delayMicroseconds(1);
  1214. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  1215. #ifdef Z_DUAL_STEPPER_DRIVERS
  1216. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  1217. #endif
  1218. //get old pin state back.
  1219. WRITE(Z_DIR_PIN,old_z_dir_pin);
  1220. #ifdef Z_DUAL_STEPPER_DRIVERS
  1221. WRITE(Z2_DIR_PIN,old_z_dir_pin);
  1222. #endif
  1223. }
  1224. break;
  1225. default: break;
  1226. }
  1227. }
  1228. #endif //BABYSTEPPING
  1229. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1230. void digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  1231. {
  1232. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  1233. SPI.transfer(address); // send in the address and value via SPI:
  1234. SPI.transfer(value);
  1235. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  1236. //_delay(10);
  1237. }
  1238. #endif
  1239. void EEPROM_read_st(int pos, uint8_t* value, uint8_t size)
  1240. {
  1241. do
  1242. {
  1243. *value = eeprom_read_byte((unsigned char*)pos);
  1244. pos++;
  1245. value++;
  1246. }while(--size);
  1247. }
  1248. void st_current_init() //Initialize Digipot Motor Current
  1249. {
  1250. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  1251. SilentModeMenu = SilentMode;
  1252. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1253. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  1254. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  1255. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  1256. if((SilentMode == SILENT_MODE_OFF) || (farm_mode) ){
  1257. motor_current_setting[0] = motor_current_setting_loud[0];
  1258. motor_current_setting[1] = motor_current_setting_loud[1];
  1259. motor_current_setting[2] = motor_current_setting_loud[2];
  1260. }else{
  1261. motor_current_setting[0] = motor_current_setting_silent[0];
  1262. motor_current_setting[1] = motor_current_setting_silent[1];
  1263. motor_current_setting[2] = motor_current_setting_silent[2];
  1264. }
  1265. st_current_set(0, motor_current_setting[0]);
  1266. st_current_set(1, motor_current_setting[1]);
  1267. st_current_set(2, motor_current_setting[2]);
  1268. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1269. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1270. #endif
  1271. }
  1272. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1273. void st_current_set(uint8_t driver, int current)
  1274. {
  1275. if (driver == 0) analogWrite(MOTOR_CURRENT_PWM_XY_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1276. if (driver == 1) analogWrite(MOTOR_CURRENT_PWM_Z_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1277. if (driver == 2) analogWrite(MOTOR_CURRENT_PWM_E_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1278. }
  1279. #else //MOTOR_CURRENT_PWM_XY_PIN
  1280. void st_current_set(uint8_t, int ){}
  1281. #endif //MOTOR_CURRENT_PWM_XY_PIN
  1282. void microstep_init()
  1283. {
  1284. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1285. pinMode(E1_MS1_PIN,OUTPUT);
  1286. pinMode(E1_MS2_PIN,OUTPUT);
  1287. #endif
  1288. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  1289. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1290. pinMode(X_MS1_PIN,OUTPUT);
  1291. pinMode(X_MS2_PIN,OUTPUT);
  1292. pinMode(Y_MS1_PIN,OUTPUT);
  1293. pinMode(Y_MS2_PIN,OUTPUT);
  1294. pinMode(Z_MS1_PIN,OUTPUT);
  1295. pinMode(Z_MS2_PIN,OUTPUT);
  1296. pinMode(E0_MS1_PIN,OUTPUT);
  1297. pinMode(E0_MS2_PIN,OUTPUT);
  1298. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  1299. #endif
  1300. }
  1301. #ifndef TMC2130
  1302. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  1303. {
  1304. if(ms1 > -1) switch(driver)
  1305. {
  1306. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  1307. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  1308. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  1309. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  1310. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1311. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  1312. #endif
  1313. }
  1314. if(ms2 > -1) switch(driver)
  1315. {
  1316. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  1317. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  1318. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  1319. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  1320. #if defined(E1_MS2_PIN) && E1_MS2_PIN > -1
  1321. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  1322. #endif
  1323. }
  1324. }
  1325. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  1326. {
  1327. switch(stepping_mode)
  1328. {
  1329. case 1: microstep_ms(driver,MICROSTEP1); break;
  1330. case 2: microstep_ms(driver,MICROSTEP2); break;
  1331. case 4: microstep_ms(driver,MICROSTEP4); break;
  1332. case 8: microstep_ms(driver,MICROSTEP8); break;
  1333. case 16: microstep_ms(driver,MICROSTEP16); break;
  1334. }
  1335. }
  1336. void microstep_readings()
  1337. {
  1338. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1339. SERIAL_PROTOCOLPGM("X: ");
  1340. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  1341. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  1342. SERIAL_PROTOCOLPGM("Y: ");
  1343. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  1344. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  1345. SERIAL_PROTOCOLPGM("Z: ");
  1346. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  1347. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  1348. SERIAL_PROTOCOLPGM("E0: ");
  1349. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  1350. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  1351. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1352. SERIAL_PROTOCOLPGM("E1: ");
  1353. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  1354. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  1355. #endif
  1356. }
  1357. #endif //TMC2130