stepper.cpp 52 KB

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