stepper.cpp 51 KB

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