stepper.cpp 49 KB

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