stepper.cpp 48 KB

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