stepper.cpp 49 KB

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