stepper.cpp 50 KB

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