stepper.cpp 47 KB

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