stepper.cpp 51 KB

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