stepper.cpp 49 KB

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