stepper.cpp 50 KB

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