stepper.cpp 50 KB

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