stepper.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  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 "sound.h"
  24. #include "language.h"
  25. #include "cardreader.h"
  26. #include "speed_lookuptable.h"
  27. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  28. #include <SPI.h>
  29. #endif
  30. #ifdef TMC2130
  31. #include "tmc2130.h"
  32. #endif //TMC2130
  33. #ifdef FILAMENT_SENSOR
  34. #include "fsensor.h"
  35. int fsensor_counter = 0; //counter for e-steps
  36. #endif //FILAMENT_SENSOR
  37. #ifdef DEBUG_STACK_MONITOR
  38. uint16_t SP_min = 0x21FF;
  39. #endif //DEBUG_STACK_MONITOR
  40. //===========================================================================
  41. //=============================public variables ============================
  42. //===========================================================================
  43. block_t *current_block; // A pointer to the block currently being traced
  44. bool x_min_endstop = false;
  45. bool x_max_endstop = false;
  46. bool y_min_endstop = false;
  47. bool y_max_endstop = false;
  48. bool z_min_endstop = false;
  49. bool z_max_endstop = false;
  50. //===========================================================================
  51. //=============================private variables ============================
  52. //===========================================================================
  53. //static makes it inpossible to be called from outside of this file by extern.!
  54. // Variables used by The Stepper Driver Interrupt
  55. static unsigned char out_bits; // The next stepping-bits to be output
  56. static dda_isteps_t
  57. counter_x, // Counter variables for the bresenham line tracer
  58. counter_y,
  59. counter_z,
  60. counter_e;
  61. volatile dda_usteps_t step_events_completed; // The number of step events executed in the current block
  62. static int32_t acceleration_time, deceleration_time;
  63. //static unsigned long accelerate_until, decelerate_after, acceleration_rate, initial_rate, final_rate, nominal_rate;
  64. static uint16_t acc_step_rate; // needed for deccelaration start point
  65. static uint8_t step_loops;
  66. static uint16_t OCR1A_nominal;
  67. static uint8_t step_loops_nominal;
  68. volatile long endstops_trigsteps[3]={0,0,0};
  69. volatile long endstops_stepsTotal,endstops_stepsDone;
  70. static volatile bool endstop_x_hit=false;
  71. static volatile bool endstop_y_hit=false;
  72. static volatile bool endstop_z_hit=false;
  73. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  74. bool abort_on_endstop_hit = false;
  75. #endif
  76. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  77. int motor_current_setting[3] = DEFAULT_PWM_MOTOR_CURRENT;
  78. int motor_current_setting_silent[3] = DEFAULT_PWM_MOTOR_CURRENT;
  79. int motor_current_setting_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
  80. #endif
  81. static bool old_x_min_endstop=false;
  82. static bool old_x_max_endstop=false;
  83. static bool old_y_min_endstop=false;
  84. static bool old_y_max_endstop=false;
  85. static bool old_z_min_endstop=false;
  86. static bool old_z_max_endstop=false;
  87. static bool check_endstops = true;
  88. static bool check_z_endstop = false;
  89. static bool z_endstop_invert = false;
  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(_T(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(_T(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(_T(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(_T(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(_i("Steprate too high: ")); MYSERIAL.println(step_rate); }//(20kHz this should never happen)////MSG_STEPPER_TOO_HIGH c=0 r=0
  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. if((eSoundMode==e_SOUND_MODE_LOUD)||(eSoundMode==e_SOUND_MODE_ONCE))
  360. WRITE(BEEPER, HIGH);
  361. }
  362. #endif
  363. // Fix the next interrupt to be executed after 8us from now.
  364. OCR1A = TCNT1 + 16;
  365. }
  366. }
  367. uint8_t last_dir_bits = 0;
  368. #ifdef BACKLASH_X
  369. uint8_t st_backlash_x = 0;
  370. #endif //BACKLASH_X
  371. #ifdef BACKLASH_Y
  372. uint8_t st_backlash_y = 0;
  373. #endif //BACKLASH_Y
  374. FORCE_INLINE void stepper_next_block()
  375. {
  376. // Anything in the buffer?
  377. //WRITE_NC(LOGIC_ANALYZER_CH2, true);
  378. current_block = plan_get_current_block();
  379. if (current_block != NULL) {
  380. #ifdef BACKLASH_X
  381. if (current_block->steps_x.wide)
  382. { //X-axis movement
  383. if ((current_block->direction_bits ^ last_dir_bits) & 1)
  384. {
  385. printf_P(PSTR("BL %d\n"), (current_block->direction_bits & 1)?st_backlash_x:-st_backlash_x);
  386. if (current_block->direction_bits & 1)
  387. WRITE_NC(X_DIR_PIN, INVERT_X_DIR);
  388. else
  389. WRITE_NC(X_DIR_PIN, !INVERT_X_DIR);
  390. _delay_us(100);
  391. for (uint8_t i = 0; i < st_backlash_x; i++)
  392. {
  393. WRITE_NC(X_STEP_PIN, !INVERT_X_STEP_PIN);
  394. _delay_us(100);
  395. WRITE_NC(X_STEP_PIN, INVERT_X_STEP_PIN);
  396. _delay_us(900);
  397. }
  398. }
  399. last_dir_bits &= ~1;
  400. last_dir_bits |= current_block->direction_bits & 1;
  401. }
  402. #endif
  403. #ifdef BACKLASH_Y
  404. if (current_block->steps_y.wide)
  405. { //Y-axis movement
  406. if ((current_block->direction_bits ^ last_dir_bits) & 2)
  407. {
  408. printf_P(PSTR("BL %d\n"), (current_block->direction_bits & 2)?st_backlash_y:-st_backlash_y);
  409. if (current_block->direction_bits & 2)
  410. WRITE_NC(Y_DIR_PIN, INVERT_Y_DIR);
  411. else
  412. WRITE_NC(Y_DIR_PIN, !INVERT_Y_DIR);
  413. _delay_us(100);
  414. for (uint8_t i = 0; i < st_backlash_y; i++)
  415. {
  416. WRITE_NC(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  417. _delay_us(100);
  418. WRITE_NC(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  419. _delay_us(900);
  420. }
  421. }
  422. last_dir_bits &= ~2;
  423. last_dir_bits |= current_block->direction_bits & 2;
  424. }
  425. #endif
  426. #ifdef FILAMENT_SENSOR
  427. fsensor_counter = 0;
  428. fsensor_st_block_begin(current_block);
  429. #endif //FILAMENT_SENSOR
  430. // The busy flag is set by the plan_get_current_block() call.
  431. // current_block->busy = true;
  432. // Initializes the trapezoid generator from the current block. Called whenever a new
  433. // block begins.
  434. deceleration_time = 0;
  435. // Set the nominal step loops to zero to indicate, that the timer value is not known yet.
  436. // That means, delay the initialization of nominal step rate and step loops until the steady
  437. // state is reached.
  438. step_loops_nominal = 0;
  439. acc_step_rate = uint16_t(current_block->initial_rate);
  440. acceleration_time = calc_timer(acc_step_rate);
  441. #ifdef LIN_ADVANCE
  442. current_estep_rate = ((unsigned long)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  443. #endif /* LIN_ADVANCE */
  444. if (current_block->flag & BLOCK_FLAG_DDA_LOWRES) {
  445. counter_x.lo = -(current_block->step_event_count.lo >> 1);
  446. counter_y.lo = counter_x.lo;
  447. counter_z.lo = counter_x.lo;
  448. counter_e.lo = counter_x.lo;
  449. } else {
  450. counter_x.wide = -(current_block->step_event_count.wide >> 1);
  451. counter_y.wide = counter_x.wide;
  452. counter_z.wide = counter_x.wide;
  453. counter_e.wide = counter_x.wide;
  454. }
  455. step_events_completed.wide = 0;
  456. // Set directions.
  457. out_bits = current_block->direction_bits;
  458. // Set the direction bits (X_AXIS=A_AXIS and Y_AXIS=B_AXIS for COREXY)
  459. if((out_bits & (1<<X_AXIS))!=0){
  460. WRITE_NC(X_DIR_PIN, INVERT_X_DIR);
  461. count_direction[X_AXIS]=-1;
  462. } else {
  463. WRITE_NC(X_DIR_PIN, !INVERT_X_DIR);
  464. count_direction[X_AXIS]=1;
  465. }
  466. if((out_bits & (1<<Y_AXIS))!=0){
  467. WRITE_NC(Y_DIR_PIN, INVERT_Y_DIR);
  468. count_direction[Y_AXIS]=-1;
  469. } else {
  470. WRITE_NC(Y_DIR_PIN, !INVERT_Y_DIR);
  471. count_direction[Y_AXIS]=1;
  472. }
  473. if ((out_bits & (1<<Z_AXIS)) != 0) { // -direction
  474. WRITE_NC(Z_DIR_PIN,INVERT_Z_DIR);
  475. count_direction[Z_AXIS]=-1;
  476. } else { // +direction
  477. WRITE_NC(Z_DIR_PIN,!INVERT_Z_DIR);
  478. count_direction[Z_AXIS]=1;
  479. }
  480. if ((out_bits & (1 << E_AXIS)) != 0) { // -direction
  481. #ifndef LIN_ADVANCE
  482. WRITE(E0_DIR_PIN,
  483. #ifdef SNMM
  484. (snmm_extruder == 0 || snmm_extruder == 2) ? !INVERT_E0_DIR :
  485. #endif // SNMM
  486. INVERT_E0_DIR);
  487. #endif /* LIN_ADVANCE */
  488. count_direction[E_AXIS] = -1;
  489. } else { // +direction
  490. #ifndef LIN_ADVANCE
  491. WRITE(E0_DIR_PIN,
  492. #ifdef SNMM
  493. (snmm_extruder == 0 || snmm_extruder == 2) ? INVERT_E0_DIR :
  494. #endif // SNMM
  495. !INVERT_E0_DIR);
  496. #endif /* LIN_ADVANCE */
  497. count_direction[E_AXIS] = 1;
  498. }
  499. }
  500. else {
  501. OCR1A = 2000; // 1kHz.
  502. }
  503. //WRITE_NC(LOGIC_ANALYZER_CH2, false);
  504. }
  505. // Check limit switches.
  506. FORCE_INLINE void stepper_check_endstops()
  507. {
  508. if(check_endstops)
  509. {
  510. #ifndef COREXY
  511. if ((out_bits & (1<<X_AXIS)) != 0) // stepping along -X axis
  512. #else
  513. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) != 0)) //-X occurs for -A and -B
  514. #endif
  515. {
  516. #if ( (defined(X_MIN_PIN) && (X_MIN_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_XMINLIMIT)
  517. #ifdef TMC2130_SG_HOMING
  518. // Stall guard homing turned on
  519. x_min_endstop = (READ(X_TMC2130_DIAG) != 0);
  520. #else
  521. // Normal homing
  522. x_min_endstop = (READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
  523. #endif
  524. if(x_min_endstop && old_x_min_endstop && (current_block->steps_x.wide > 0)) {
  525. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  526. endstop_x_hit=true;
  527. step_events_completed.wide = current_block->step_event_count.wide;
  528. }
  529. old_x_min_endstop = x_min_endstop;
  530. #endif
  531. } else { // +direction
  532. #if ( (defined(X_MAX_PIN) && (X_MAX_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_XMAXLIMIT)
  533. #ifdef TMC2130_SG_HOMING
  534. // Stall guard homing turned on
  535. x_max_endstop = (READ(X_TMC2130_DIAG) != 0);
  536. #else
  537. // Normal homing
  538. x_max_endstop = (READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
  539. #endif
  540. if(x_max_endstop && old_x_max_endstop && (current_block->steps_x.wide > 0)){
  541. endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
  542. endstop_x_hit=true;
  543. step_events_completed.wide = current_block->step_event_count.wide;
  544. }
  545. old_x_max_endstop = x_max_endstop;
  546. #endif
  547. }
  548. #ifndef COREXY
  549. if ((out_bits & (1<<Y_AXIS)) != 0) // -direction
  550. #else
  551. if ((((out_bits & (1<<X_AXIS)) != 0)&&(out_bits & (1<<Y_AXIS)) == 0)) // -Y occurs for -A and +B
  552. #endif
  553. {
  554. #if ( (defined(Y_MIN_PIN) && (Y_MIN_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_YMINLIMIT)
  555. #ifdef TMC2130_SG_HOMING
  556. // Stall guard homing turned on
  557. y_min_endstop = (READ(Y_TMC2130_DIAG) != 0);
  558. #else
  559. // Normal homing
  560. y_min_endstop = (READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
  561. #endif
  562. if(y_min_endstop && old_y_min_endstop && (current_block->steps_y.wide > 0)) {
  563. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  564. endstop_y_hit=true;
  565. step_events_completed.wide = current_block->step_event_count.wide;
  566. }
  567. old_y_min_endstop = y_min_endstop;
  568. #endif
  569. } else { // +direction
  570. #if ( (defined(Y_MAX_PIN) && (Y_MAX_PIN > -1)) || defined(TMC2130_SG_HOMING) ) && !defined(DEBUG_DISABLE_YMAXLIMIT)
  571. #ifdef TMC2130_SG_HOMING
  572. // Stall guard homing turned on
  573. y_max_endstop = (READ(Y_TMC2130_DIAG) != 0);
  574. #else
  575. // Normal homing
  576. y_max_endstop = (READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
  577. #endif
  578. if(y_max_endstop && old_y_max_endstop && (current_block->steps_y.wide > 0)){
  579. endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
  580. endstop_y_hit=true;
  581. step_events_completed.wide = current_block->step_event_count.wide;
  582. }
  583. old_y_max_endstop = y_max_endstop;
  584. #endif
  585. }
  586. if ((out_bits & (1<<Z_AXIS)) != 0) // -direction
  587. {
  588. #if defined(Z_MIN_PIN) && (Z_MIN_PIN > -1) && !defined(DEBUG_DISABLE_ZMINLIMIT)
  589. if (! check_z_endstop) {
  590. #ifdef TMC2130_SG_HOMING
  591. // Stall guard homing turned on
  592. #ifdef TMC2130_STEALTH_Z
  593. if ((tmc2130_mode == TMC2130_MODE_SILENT) && !(tmc2130_sg_homing_axes_mask & 0x04))
  594. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  595. else
  596. #endif //TMC2130_STEALTH_Z
  597. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING) || (READ(Z_TMC2130_DIAG) != 0);
  598. #else
  599. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  600. #endif //TMC2130_SG_HOMING
  601. if(z_min_endstop && old_z_min_endstop && (current_block->steps_z.wide > 0)) {
  602. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  603. endstop_z_hit=true;
  604. step_events_completed.wide = current_block->step_event_count.wide;
  605. }
  606. old_z_min_endstop = z_min_endstop;
  607. }
  608. #endif
  609. } else { // +direction
  610. #if defined(Z_MAX_PIN) && (Z_MAX_PIN > -1) && !defined(DEBUG_DISABLE_ZMAXLIMIT)
  611. #ifdef TMC2130_SG_HOMING
  612. // Stall guard homing turned on
  613. #ifdef TMC2130_STEALTH_Z
  614. if ((tmc2130_mode == TMC2130_MODE_SILENT) && !(tmc2130_sg_homing_axes_mask & 0x04))
  615. z_max_endstop = false;
  616. else
  617. #endif //TMC2130_STEALTH_Z
  618. z_max_endstop = (READ(Z_TMC2130_DIAG) != 0);
  619. #else
  620. z_max_endstop = (READ(Z_MAX_PIN) != Z_MAX_ENDSTOP_INVERTING);
  621. #endif //TMC2130_SG_HOMING
  622. if(z_max_endstop && old_z_max_endstop && (current_block->steps_z.wide > 0)) {
  623. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  624. endstop_z_hit=true;
  625. step_events_completed.wide = current_block->step_event_count.wide;
  626. }
  627. old_z_max_endstop = z_max_endstop;
  628. #endif
  629. }
  630. }
  631. // Supporting stopping on a trigger of the Z-stop induction sensor, not only for the Z-minus movements.
  632. #if defined(Z_MIN_PIN) && (Z_MIN_PIN > -1) && !defined(DEBUG_DISABLE_ZMINLIMIT)
  633. if (check_z_endstop) {
  634. // Check the Z min end-stop no matter what.
  635. // Good for searching for the center of an induction target.
  636. #ifdef TMC2130_SG_HOMING
  637. // Stall guard homing turned on
  638. #ifdef TMC2130_STEALTH_Z
  639. if ((tmc2130_mode == TMC2130_MODE_SILENT) && !(tmc2130_sg_homing_axes_mask & 0x04))
  640. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  641. else
  642. #endif //TMC2130_STEALTH_Z
  643. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING) || (READ(Z_TMC2130_DIAG) != 0);
  644. #else
  645. z_min_endstop = (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING);
  646. #endif //TMC2130_SG_HOMING
  647. if(z_min_endstop && old_z_min_endstop) {
  648. endstops_trigsteps[Z_AXIS] = count_position[Z_AXIS];
  649. endstop_z_hit=true;
  650. step_events_completed.wide = current_block->step_event_count.wide;
  651. }
  652. old_z_min_endstop = z_min_endstop;
  653. }
  654. #endif
  655. }
  656. FORCE_INLINE void stepper_tick_lowres()
  657. {
  658. for (uint8_t i=0; i < step_loops; ++ i) { // Take multiple steps per interrupt (For high speed moves)
  659. MSerial.checkRx(); // Check for serial chars.
  660. // Step in X axis
  661. counter_x.lo += current_block->steps_x.lo;
  662. if (counter_x.lo > 0) {
  663. WRITE_NC(X_STEP_PIN, !INVERT_X_STEP_PIN);
  664. LastStepMask |= X_AXIS_MASK;
  665. #ifdef DEBUG_XSTEP_DUP_PIN
  666. WRITE_NC(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  667. #endif //DEBUG_XSTEP_DUP_PIN
  668. counter_x.lo -= current_block->step_event_count.lo;
  669. count_position[X_AXIS]+=count_direction[X_AXIS];
  670. WRITE_NC(X_STEP_PIN, INVERT_X_STEP_PIN);
  671. #ifdef DEBUG_XSTEP_DUP_PIN
  672. WRITE_NC(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  673. #endif //DEBUG_XSTEP_DUP_PIN
  674. }
  675. // Step in Y axis
  676. counter_y.lo += current_block->steps_y.lo;
  677. if (counter_y.lo > 0) {
  678. WRITE_NC(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  679. LastStepMask |= Y_AXIS_MASK;
  680. #ifdef DEBUG_YSTEP_DUP_PIN
  681. WRITE_NC(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  682. #endif //DEBUG_YSTEP_DUP_PIN
  683. counter_y.lo -= current_block->step_event_count.lo;
  684. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  685. WRITE_NC(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  686. #ifdef DEBUG_YSTEP_DUP_PIN
  687. WRITE_NC(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  688. #endif //DEBUG_YSTEP_DUP_PIN
  689. }
  690. // Step in Z axis
  691. counter_z.lo += current_block->steps_z.lo;
  692. if (counter_z.lo > 0) {
  693. WRITE_NC(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  694. LastStepMask |= Z_AXIS_MASK;
  695. counter_z.lo -= current_block->step_event_count.lo;
  696. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  697. WRITE_NC(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  698. }
  699. // Step in E axis
  700. counter_e.lo += current_block->steps_e.lo;
  701. if (counter_e.lo > 0) {
  702. #ifndef LIN_ADVANCE
  703. WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  704. #endif /* LIN_ADVANCE */
  705. counter_e.lo -= current_block->step_event_count.lo;
  706. count_position[E_AXIS] += count_direction[E_AXIS];
  707. #ifdef LIN_ADVANCE
  708. ++ e_steps;
  709. #else
  710. #ifdef FILAMENT_SENSOR
  711. ++ fsensor_counter;
  712. #endif //FILAMENT_SENSOR
  713. WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN);
  714. #endif
  715. }
  716. if(++ step_events_completed.lo >= current_block->step_event_count.lo)
  717. break;
  718. }
  719. }
  720. FORCE_INLINE void stepper_tick_highres()
  721. {
  722. for (uint8_t i=0; i < step_loops; ++ i) { // Take multiple steps per interrupt (For high speed moves)
  723. MSerial.checkRx(); // Check for serial chars.
  724. // Step in X axis
  725. counter_x.wide += current_block->steps_x.wide;
  726. if (counter_x.wide > 0) {
  727. WRITE_NC(X_STEP_PIN, !INVERT_X_STEP_PIN);
  728. LastStepMask |= X_AXIS_MASK;
  729. #ifdef DEBUG_XSTEP_DUP_PIN
  730. WRITE_NC(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  731. #endif //DEBUG_XSTEP_DUP_PIN
  732. counter_x.wide -= current_block->step_event_count.wide;
  733. count_position[X_AXIS]+=count_direction[X_AXIS];
  734. WRITE_NC(X_STEP_PIN, INVERT_X_STEP_PIN);
  735. #ifdef DEBUG_XSTEP_DUP_PIN
  736. WRITE_NC(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  737. #endif //DEBUG_XSTEP_DUP_PIN
  738. }
  739. // Step in Y axis
  740. counter_y.wide += current_block->steps_y.wide;
  741. if (counter_y.wide > 0) {
  742. WRITE_NC(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  743. LastStepMask |= Y_AXIS_MASK;
  744. #ifdef DEBUG_YSTEP_DUP_PIN
  745. WRITE_NC(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  746. #endif //DEBUG_YSTEP_DUP_PIN
  747. counter_y.wide -= current_block->step_event_count.wide;
  748. count_position[Y_AXIS]+=count_direction[Y_AXIS];
  749. WRITE_NC(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  750. #ifdef DEBUG_YSTEP_DUP_PIN
  751. WRITE_NC(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  752. #endif //DEBUG_YSTEP_DUP_PIN
  753. }
  754. // Step in Z axis
  755. counter_z.wide += current_block->steps_z.wide;
  756. if (counter_z.wide > 0) {
  757. WRITE_NC(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  758. LastStepMask |= Z_AXIS_MASK;
  759. counter_z.wide -= current_block->step_event_count.wide;
  760. count_position[Z_AXIS]+=count_direction[Z_AXIS];
  761. WRITE_NC(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  762. }
  763. // Step in E axis
  764. counter_e.wide += current_block->steps_e.wide;
  765. if (counter_e.wide > 0) {
  766. #ifndef LIN_ADVANCE
  767. WRITE(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  768. #endif /* LIN_ADVANCE */
  769. counter_e.wide -= current_block->step_event_count.wide;
  770. count_position[E_AXIS]+=count_direction[E_AXIS];
  771. #ifdef LIN_ADVANCE
  772. ++ e_steps;
  773. #else
  774. #ifdef FILAMENT_SENSOR
  775. ++ fsensor_counter;
  776. #endif //FILAMENT_SENSOR
  777. WRITE(E0_STEP_PIN, INVERT_E_STEP_PIN);
  778. #endif
  779. }
  780. if(++ step_events_completed.wide >= current_block->step_event_count.wide)
  781. break;
  782. }
  783. }
  784. // 50us delay
  785. #define LIN_ADV_FIRST_TICK_DELAY 100
  786. FORCE_INLINE void isr() {
  787. //WRITE_NC(LOGIC_ANALYZER_CH0, true);
  788. //if (UVLO) uvlo();
  789. // If there is no current block, attempt to pop one from the buffer
  790. if (current_block == NULL)
  791. stepper_next_block();
  792. LastStepMask = 0;
  793. if (current_block != NULL)
  794. {
  795. stepper_check_endstops();
  796. #ifdef LIN_ADVANCE
  797. e_steps = 0;
  798. #endif /* LIN_ADVANCE */
  799. if (current_block->flag & BLOCK_FLAG_DDA_LOWRES)
  800. stepper_tick_lowres();
  801. else
  802. stepper_tick_highres();
  803. #ifdef LIN_ADVANCE
  804. if (out_bits&(1<<E_AXIS))
  805. // Move in negative direction.
  806. e_steps = - e_steps;
  807. if (current_block->use_advance_lead) {
  808. //int esteps_inc = 0;
  809. //esteps_inc = current_estep_rate - current_adv_steps;
  810. //e_steps += esteps_inc;
  811. e_steps += current_estep_rate - current_adv_steps;
  812. #if 0
  813. if (abs(esteps_inc) > 4) {
  814. LOGIC_ANALYZER_SERIAL_TX_WRITE(esteps_inc);
  815. if (esteps_inc < -511 || esteps_inc > 511)
  816. LOGIC_ANALYZER_SERIAL_TX_WRITE(esteps_inc >> 9);
  817. }
  818. #endif
  819. current_adv_steps = current_estep_rate;
  820. }
  821. // If we have esteps to execute, step some of them now.
  822. if (e_steps) {
  823. //WRITE_NC(LOGIC_ANALYZER_CH7, true);
  824. // Set the step direction.
  825. {
  826. bool neg = e_steps < 0;
  827. bool dir =
  828. #ifdef SNMM
  829. (neg == (snmm_extruder & 1))
  830. #else
  831. neg
  832. #endif
  833. ? INVERT_E0_DIR : !INVERT_E0_DIR; //If we have SNMM, reverse every second extruder.
  834. WRITE_NC(E0_DIR_PIN, dir);
  835. if (neg)
  836. // Flip the e_steps counter to be always positive.
  837. e_steps = - e_steps;
  838. }
  839. // Tick min(step_loops, abs(e_steps)).
  840. estep_loops = (e_steps & 0x0ff00) ? 4 : e_steps;
  841. if (step_loops < estep_loops)
  842. estep_loops = step_loops;
  843. #ifdef FILAMENT_SENSOR
  844. fsensor_counter += estep_loops;
  845. #endif //FILAMENT_SENSOR
  846. do {
  847. WRITE_NC(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  848. -- e_steps;
  849. WRITE_NC(E0_STEP_PIN, INVERT_E_STEP_PIN);
  850. } while (-- estep_loops != 0);
  851. //WRITE_NC(LOGIC_ANALYZER_CH7, false);
  852. MSerial.checkRx(); // Check for serial chars.
  853. }
  854. #endif
  855. // Calculare new timer value
  856. // 13.38-14.63us for steady state,
  857. // 25.12us for acceleration / deceleration.
  858. {
  859. //WRITE_NC(LOGIC_ANALYZER_CH1, true);
  860. if (step_events_completed.wide <= (unsigned long int)current_block->accelerate_until) {
  861. // v = t * a -> acc_step_rate = acceleration_time * current_block->acceleration_rate
  862. MultiU24X24toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  863. acc_step_rate += uint16_t(current_block->initial_rate);
  864. // upper limit
  865. if(acc_step_rate > uint16_t(current_block->nominal_rate))
  866. acc_step_rate = current_block->nominal_rate;
  867. // step_rate to timer interval
  868. uint16_t timer = calc_timer(acc_step_rate);
  869. _NEXT_ISR(timer);
  870. acceleration_time += timer;
  871. #ifdef LIN_ADVANCE
  872. if (current_block->use_advance_lead)
  873. // int32_t = (uint16_t * uint32_t) >> 17
  874. current_estep_rate = ((uint32_t)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  875. #endif
  876. }
  877. else if (step_events_completed.wide > (unsigned long int)current_block->decelerate_after) {
  878. uint16_t step_rate;
  879. MultiU24X24toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  880. step_rate = acc_step_rate - step_rate; // Decelerate from aceleration end point.
  881. if ((step_rate & 0x8000) || step_rate < uint16_t(current_block->final_rate)) {
  882. // Result is negative or too small.
  883. step_rate = uint16_t(current_block->final_rate);
  884. }
  885. // Step_rate to timer interval.
  886. uint16_t timer = calc_timer(step_rate);
  887. _NEXT_ISR(timer);
  888. deceleration_time += timer;
  889. #ifdef LIN_ADVANCE
  890. if (current_block->use_advance_lead)
  891. current_estep_rate = ((uint32_t)step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  892. #endif
  893. }
  894. else {
  895. if (! step_loops_nominal) {
  896. // Calculation of the steady state timer rate has been delayed to the 1st tick of the steady state to lower
  897. // the initial interrupt blocking.
  898. OCR1A_nominal = calc_timer(uint16_t(current_block->nominal_rate));
  899. step_loops_nominal = step_loops;
  900. #ifdef LIN_ADVANCE
  901. if (current_block->use_advance_lead)
  902. current_estep_rate = (current_block->nominal_rate * current_block->abs_adv_steps_multiplier8) >> 17;
  903. #endif
  904. }
  905. _NEXT_ISR(OCR1A_nominal);
  906. }
  907. //WRITE_NC(LOGIC_ANALYZER_CH1, false);
  908. }
  909. #ifdef LIN_ADVANCE
  910. if (e_steps && current_block->use_advance_lead) {
  911. //WRITE_NC(LOGIC_ANALYZER_CH7, true);
  912. MSerial.checkRx(); // Check for serial chars.
  913. // Some of the E steps were not ticked yet. Plan additional interrupts.
  914. uint16_t now = TCNT1;
  915. // Plan the first linear advance interrupt after 50us from now.
  916. uint16_t to_go = nextMainISR - now - LIN_ADV_FIRST_TICK_DELAY;
  917. eISR_Rate = 0;
  918. if ((to_go & 0x8000) == 0) {
  919. // The to_go number is not negative.
  920. // Count the number of 7812,5 ticks, that fit into to_go 2MHz ticks.
  921. uint8_t ticks = to_go >> 8;
  922. if (ticks == 1) {
  923. // Avoid running the following loop for a very short interval.
  924. estep_loops = 255;
  925. eISR_Rate = 1;
  926. } else if ((e_steps & 0x0ff00) == 0) {
  927. // e_steps <= 0x0ff
  928. if (uint8_t(e_steps) <= ticks) {
  929. // Spread the e_steps along the whole go_to interval.
  930. eISR_Rate = to_go / uint8_t(e_steps);
  931. estep_loops = 1;
  932. } else if (ticks != 0) {
  933. // At least one tick fits into the to_go interval. Calculate the e-step grouping.
  934. uint8_t e = uint8_t(e_steps) >> 1;
  935. estep_loops = 2;
  936. while (e > ticks) {
  937. e >>= 1;
  938. estep_loops <<= 1;
  939. }
  940. // Now the estep_loops contains the number of loops of power of 2, that will be sufficient
  941. // to squeeze enough of Linear Advance ticks until nextMainISR.
  942. // Calculate the tick rate.
  943. eISR_Rate = to_go / ticks;
  944. }
  945. } else {
  946. // This is an exterme case with too many e_steps inserted by the linear advance.
  947. // At least one tick fits into the to_go interval. Calculate the e-step grouping.
  948. estep_loops = 2;
  949. uint16_t e = e_steps >> 1;
  950. while (e & 0x0ff00) {
  951. e >>= 1;
  952. estep_loops <<= 1;
  953. }
  954. while (uint8_t(e) > ticks) {
  955. e >>= 1;
  956. estep_loops <<= 1;
  957. }
  958. // Now the estep_loops contains the number of loops of power of 2, that will be sufficient
  959. // to squeeze enough of Linear Advance ticks until nextMainISR.
  960. // Calculate the tick rate.
  961. eISR_Rate = to_go / ticks;
  962. }
  963. }
  964. if (eISR_Rate == 0) {
  965. // There is not enough time to fit even a single additional tick.
  966. // Tick all the extruder ticks now.
  967. #ifdef FILAMENT_SENSOR
  968. fsensor_counter += e_steps;
  969. #endif //FILAMENT_SENSOR
  970. MSerial.checkRx(); // Check for serial chars.
  971. do {
  972. WRITE_NC(E0_STEP_PIN, !INVERT_E_STEP_PIN);
  973. -- e_steps;
  974. WRITE_NC(E0_STEP_PIN, INVERT_E_STEP_PIN);
  975. } while (e_steps);
  976. OCR1A = nextMainISR;
  977. } else {
  978. // Tick the 1st Linear Advance interrupt after 50us from now.
  979. nextMainISR -= LIN_ADV_FIRST_TICK_DELAY;
  980. OCR1A = now + LIN_ADV_FIRST_TICK_DELAY;
  981. }
  982. //WRITE_NC(LOGIC_ANALYZER_CH7, false);
  983. } else
  984. OCR1A = nextMainISR;
  985. #endif
  986. // If current block is finished, reset pointer
  987. if (step_events_completed.wide >= current_block->step_event_count.wide) {
  988. #ifdef FILAMENT_SENSOR
  989. fsensor_st_block_chunk(current_block, fsensor_counter);
  990. fsensor_counter = 0;
  991. #endif //FILAMENT_SENSOR
  992. current_block = NULL;
  993. plan_discard_current_block();
  994. }
  995. #ifdef FILAMENT_SENSOR
  996. else if (fsensor_counter >= fsensor_chunk_len)
  997. {
  998. fsensor_st_block_chunk(current_block, fsensor_counter);
  999. fsensor_counter = 0;
  1000. }
  1001. #endif //FILAMENT_SENSOR
  1002. }
  1003. #ifdef TMC2130
  1004. tmc2130_st_isr(LastStepMask);
  1005. #endif //TMC2130
  1006. //WRITE_NC(LOGIC_ANALYZER_CH0, false);
  1007. }
  1008. #ifdef LIN_ADVANCE
  1009. void clear_current_adv_vars() {
  1010. e_steps = 0; //Should be already 0 at an filament change event, but just to be sure..
  1011. current_adv_steps = 0;
  1012. }
  1013. #endif // LIN_ADVANCE
  1014. void st_init()
  1015. {
  1016. #ifdef TMC2130
  1017. tmc2130_init();
  1018. #endif //TMC2130
  1019. st_current_init(); //Initialize Digipot Motor Current
  1020. microstep_init(); //Initialize Microstepping Pins
  1021. //Initialize Dir Pins
  1022. #if defined(X_DIR_PIN) && X_DIR_PIN > -1
  1023. SET_OUTPUT(X_DIR_PIN);
  1024. #endif
  1025. #if defined(X2_DIR_PIN) && X2_DIR_PIN > -1
  1026. SET_OUTPUT(X2_DIR_PIN);
  1027. #endif
  1028. #if defined(Y_DIR_PIN) && Y_DIR_PIN > -1
  1029. SET_OUTPUT(Y_DIR_PIN);
  1030. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_DIR_PIN) && (Y2_DIR_PIN > -1)
  1031. SET_OUTPUT(Y2_DIR_PIN);
  1032. #endif
  1033. #endif
  1034. #if defined(Z_DIR_PIN) && Z_DIR_PIN > -1
  1035. SET_OUTPUT(Z_DIR_PIN);
  1036. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_DIR_PIN) && (Z2_DIR_PIN > -1)
  1037. SET_OUTPUT(Z2_DIR_PIN);
  1038. #endif
  1039. #endif
  1040. #if defined(E0_DIR_PIN) && E0_DIR_PIN > -1
  1041. SET_OUTPUT(E0_DIR_PIN);
  1042. #endif
  1043. #if defined(E1_DIR_PIN) && (E1_DIR_PIN > -1)
  1044. SET_OUTPUT(E1_DIR_PIN);
  1045. #endif
  1046. #if defined(E2_DIR_PIN) && (E2_DIR_PIN > -1)
  1047. SET_OUTPUT(E2_DIR_PIN);
  1048. #endif
  1049. //Initialize Enable Pins - steppers default to disabled.
  1050. #if defined(X_ENABLE_PIN) && X_ENABLE_PIN > -1
  1051. SET_OUTPUT(X_ENABLE_PIN);
  1052. if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);
  1053. #endif
  1054. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  1055. SET_OUTPUT(X2_ENABLE_PIN);
  1056. if(!X_ENABLE_ON) WRITE(X2_ENABLE_PIN,HIGH);
  1057. #endif
  1058. #if defined(Y_ENABLE_PIN) && Y_ENABLE_PIN > -1
  1059. SET_OUTPUT(Y_ENABLE_PIN);
  1060. if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);
  1061. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_ENABLE_PIN) && (Y2_ENABLE_PIN > -1)
  1062. SET_OUTPUT(Y2_ENABLE_PIN);
  1063. if(!Y_ENABLE_ON) WRITE(Y2_ENABLE_PIN,HIGH);
  1064. #endif
  1065. #endif
  1066. #if defined(Z_ENABLE_PIN) && Z_ENABLE_PIN > -1
  1067. SET_OUTPUT(Z_ENABLE_PIN);
  1068. if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);
  1069. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_ENABLE_PIN) && (Z2_ENABLE_PIN > -1)
  1070. SET_OUTPUT(Z2_ENABLE_PIN);
  1071. if(!Z_ENABLE_ON) WRITE(Z2_ENABLE_PIN,HIGH);
  1072. #endif
  1073. #endif
  1074. #if defined(E0_ENABLE_PIN) && (E0_ENABLE_PIN > -1)
  1075. SET_OUTPUT(E0_ENABLE_PIN);
  1076. if(!E_ENABLE_ON) WRITE(E0_ENABLE_PIN,HIGH);
  1077. #endif
  1078. #if defined(E1_ENABLE_PIN) && (E1_ENABLE_PIN > -1)
  1079. SET_OUTPUT(E1_ENABLE_PIN);
  1080. if(!E_ENABLE_ON) WRITE(E1_ENABLE_PIN,HIGH);
  1081. #endif
  1082. #if defined(E2_ENABLE_PIN) && (E2_ENABLE_PIN > -1)
  1083. SET_OUTPUT(E2_ENABLE_PIN);
  1084. if(!E_ENABLE_ON) WRITE(E2_ENABLE_PIN,HIGH);
  1085. #endif
  1086. //endstops and pullups
  1087. #ifdef TMC2130_SG_HOMING
  1088. SET_INPUT(X_TMC2130_DIAG);
  1089. WRITE(X_TMC2130_DIAG,HIGH);
  1090. SET_INPUT(Y_TMC2130_DIAG);
  1091. WRITE(Y_TMC2130_DIAG,HIGH);
  1092. SET_INPUT(Z_TMC2130_DIAG);
  1093. WRITE(Z_TMC2130_DIAG,HIGH);
  1094. SET_INPUT(E0_TMC2130_DIAG);
  1095. WRITE(E0_TMC2130_DIAG,HIGH);
  1096. #endif
  1097. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  1098. SET_INPUT(X_MIN_PIN);
  1099. #ifdef ENDSTOPPULLUP_XMIN
  1100. WRITE(X_MIN_PIN,HIGH);
  1101. #endif
  1102. #endif
  1103. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  1104. SET_INPUT(Y_MIN_PIN);
  1105. #ifdef ENDSTOPPULLUP_YMIN
  1106. WRITE(Y_MIN_PIN,HIGH);
  1107. #endif
  1108. #endif
  1109. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  1110. SET_INPUT(Z_MIN_PIN);
  1111. #ifdef ENDSTOPPULLUP_ZMIN
  1112. WRITE(Z_MIN_PIN,HIGH);
  1113. #endif
  1114. #endif
  1115. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  1116. SET_INPUT(X_MAX_PIN);
  1117. #ifdef ENDSTOPPULLUP_XMAX
  1118. WRITE(X_MAX_PIN,HIGH);
  1119. #endif
  1120. #endif
  1121. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  1122. SET_INPUT(Y_MAX_PIN);
  1123. #ifdef ENDSTOPPULLUP_YMAX
  1124. WRITE(Y_MAX_PIN,HIGH);
  1125. #endif
  1126. #endif
  1127. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  1128. SET_INPUT(Z_MAX_PIN);
  1129. #ifdef ENDSTOPPULLUP_ZMAX
  1130. WRITE(Z_MAX_PIN,HIGH);
  1131. #endif
  1132. #endif
  1133. #if (defined(FANCHECK) && defined(TACH_0) && (TACH_0 > -1))
  1134. SET_INPUT(TACH_0);
  1135. #ifdef TACH0PULLUP
  1136. WRITE(TACH_0, HIGH);
  1137. #endif
  1138. #endif
  1139. //Initialize Step Pins
  1140. #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
  1141. SET_OUTPUT(X_STEP_PIN);
  1142. WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
  1143. #ifdef DEBUG_XSTEP_DUP_PIN
  1144. SET_OUTPUT(DEBUG_XSTEP_DUP_PIN);
  1145. WRITE(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  1146. #endif //DEBUG_XSTEP_DUP_PIN
  1147. disable_x();
  1148. #endif
  1149. #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
  1150. SET_OUTPUT(X2_STEP_PIN);
  1151. WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
  1152. disable_x();
  1153. #endif
  1154. #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
  1155. SET_OUTPUT(Y_STEP_PIN);
  1156. WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
  1157. #ifdef DEBUG_YSTEP_DUP_PIN
  1158. SET_OUTPUT(DEBUG_YSTEP_DUP_PIN);
  1159. WRITE(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  1160. #endif //DEBUG_YSTEP_DUP_PIN
  1161. #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
  1162. SET_OUTPUT(Y2_STEP_PIN);
  1163. WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
  1164. #endif
  1165. disable_y();
  1166. #endif
  1167. #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
  1168. SET_OUTPUT(Z_STEP_PIN);
  1169. WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
  1170. #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
  1171. SET_OUTPUT(Z2_STEP_PIN);
  1172. WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
  1173. #endif
  1174. disable_z();
  1175. #endif
  1176. #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
  1177. SET_OUTPUT(E0_STEP_PIN);
  1178. WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
  1179. disable_e0();
  1180. #endif
  1181. #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
  1182. SET_OUTPUT(E1_STEP_PIN);
  1183. WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
  1184. disable_e1();
  1185. #endif
  1186. #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
  1187. SET_OUTPUT(E2_STEP_PIN);
  1188. WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
  1189. disable_e2();
  1190. #endif
  1191. // waveform generation = 0100 = CTC
  1192. TCCR1B &= ~(1<<WGM13);
  1193. TCCR1B |= (1<<WGM12);
  1194. TCCR1A &= ~(1<<WGM11);
  1195. TCCR1A &= ~(1<<WGM10);
  1196. // output mode = 00 (disconnected)
  1197. TCCR1A &= ~(3<<COM1A0);
  1198. TCCR1A &= ~(3<<COM1B0);
  1199. // Set the timer pre-scaler
  1200. // Generally we use a divider of 8, resulting in a 2MHz timer
  1201. // frequency on a 16MHz MCU. If you are going to change this, be
  1202. // sure to regenerate speed_lookuptable.h with
  1203. // create_speed_lookuptable.py
  1204. TCCR1B = (TCCR1B & ~(0x07<<CS10)) | (2<<CS10);
  1205. // Plan the first interrupt after 8ms from now.
  1206. OCR1A = 0x4000;
  1207. TCNT1 = 0;
  1208. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1209. #ifdef LIN_ADVANCE
  1210. e_steps = 0;
  1211. current_adv_steps = 0;
  1212. #endif
  1213. enable_endstops(true); // Start with endstops active. After homing they can be disabled
  1214. sei();
  1215. }
  1216. // Block until all buffered steps are executed
  1217. void st_synchronize()
  1218. {
  1219. while(blocks_queued())
  1220. {
  1221. #ifdef TMC2130
  1222. manage_heater();
  1223. // Vojtech: Don't disable motors inside the planner!
  1224. if (!tmc2130_update_sg())
  1225. {
  1226. manage_inactivity(true);
  1227. lcd_update(0);
  1228. }
  1229. #else //TMC2130
  1230. manage_heater();
  1231. // Vojtech: Don't disable motors inside the planner!
  1232. manage_inactivity(true);
  1233. lcd_update(0);
  1234. #endif //TMC2130
  1235. }
  1236. }
  1237. void st_set_position(const long &x, const long &y, const long &z, const long &e)
  1238. {
  1239. CRITICAL_SECTION_START;
  1240. // Copy 4x4B.
  1241. // This block locks the interrupts globally for 4.56 us,
  1242. // which corresponds to a maximum repeat frequency of 219.18 kHz.
  1243. // This blocking is safe in the context of a 10kHz stepper driver interrupt
  1244. // or a 115200 Bd serial line receive interrupt, which will not trigger faster than 12kHz.
  1245. count_position[X_AXIS] = x;
  1246. count_position[Y_AXIS] = y;
  1247. count_position[Z_AXIS] = z;
  1248. count_position[E_AXIS] = e;
  1249. CRITICAL_SECTION_END;
  1250. }
  1251. void st_set_e_position(const long &e)
  1252. {
  1253. CRITICAL_SECTION_START;
  1254. count_position[E_AXIS] = e;
  1255. CRITICAL_SECTION_END;
  1256. }
  1257. long st_get_position(uint8_t axis)
  1258. {
  1259. long count_pos;
  1260. CRITICAL_SECTION_START;
  1261. count_pos = count_position[axis];
  1262. CRITICAL_SECTION_END;
  1263. return count_pos;
  1264. }
  1265. void st_get_position_xy(long &x, long &y)
  1266. {
  1267. CRITICAL_SECTION_START;
  1268. x = count_position[X_AXIS];
  1269. y = count_position[Y_AXIS];
  1270. CRITICAL_SECTION_END;
  1271. }
  1272. float st_get_position_mm(uint8_t axis)
  1273. {
  1274. float steper_position_in_steps = st_get_position(axis);
  1275. return steper_position_in_steps / axis_steps_per_unit[axis];
  1276. }
  1277. void finishAndDisableSteppers()
  1278. {
  1279. st_synchronize();
  1280. disable_x();
  1281. disable_y();
  1282. disable_z();
  1283. disable_e0();
  1284. disable_e1();
  1285. disable_e2();
  1286. }
  1287. void quickStop()
  1288. {
  1289. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1290. while (blocks_queued()) plan_discard_current_block();
  1291. current_block = NULL;
  1292. st_reset_timer();
  1293. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1294. }
  1295. #ifdef BABYSTEPPING
  1296. void babystep(const uint8_t axis,const bool direction)
  1297. {
  1298. //MUST ONLY BE CALLED BY A ISR, it depends on that no other ISR interrupts this
  1299. //store initial pin states
  1300. switch(axis)
  1301. {
  1302. case X_AXIS:
  1303. {
  1304. enable_x();
  1305. uint8_t old_x_dir_pin= READ(X_DIR_PIN); //if dualzstepper, both point to same direction.
  1306. //setup new step
  1307. WRITE(X_DIR_PIN,(INVERT_X_DIR)^direction);
  1308. //perform step
  1309. WRITE(X_STEP_PIN, !INVERT_X_STEP_PIN);
  1310. LastStepMask |= X_AXIS_MASK;
  1311. #ifdef DEBUG_XSTEP_DUP_PIN
  1312. WRITE(DEBUG_XSTEP_DUP_PIN,!INVERT_X_STEP_PIN);
  1313. #endif //DEBUG_XSTEP_DUP_PIN
  1314. {
  1315. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  1316. }
  1317. WRITE(X_STEP_PIN, INVERT_X_STEP_PIN);
  1318. #ifdef DEBUG_XSTEP_DUP_PIN
  1319. WRITE(DEBUG_XSTEP_DUP_PIN,INVERT_X_STEP_PIN);
  1320. #endif //DEBUG_XSTEP_DUP_PIN
  1321. //get old pin state back.
  1322. WRITE(X_DIR_PIN,old_x_dir_pin);
  1323. }
  1324. break;
  1325. case Y_AXIS:
  1326. {
  1327. enable_y();
  1328. uint8_t old_y_dir_pin= READ(Y_DIR_PIN); //if dualzstepper, both point to same direction.
  1329. //setup new step
  1330. WRITE(Y_DIR_PIN,(INVERT_Y_DIR)^direction);
  1331. //perform step
  1332. WRITE(Y_STEP_PIN, !INVERT_Y_STEP_PIN);
  1333. LastStepMask |= Y_AXIS_MASK;
  1334. #ifdef DEBUG_YSTEP_DUP_PIN
  1335. WRITE(DEBUG_YSTEP_DUP_PIN,!INVERT_Y_STEP_PIN);
  1336. #endif //DEBUG_YSTEP_DUP_PIN
  1337. {
  1338. volatile float x=1./float(axis+1)/float(axis+2); //wait a tiny bit
  1339. }
  1340. WRITE(Y_STEP_PIN, INVERT_Y_STEP_PIN);
  1341. #ifdef DEBUG_YSTEP_DUP_PIN
  1342. WRITE(DEBUG_YSTEP_DUP_PIN,INVERT_Y_STEP_PIN);
  1343. #endif //DEBUG_YSTEP_DUP_PIN
  1344. //get old pin state back.
  1345. WRITE(Y_DIR_PIN,old_y_dir_pin);
  1346. }
  1347. break;
  1348. case Z_AXIS:
  1349. {
  1350. enable_z();
  1351. uint8_t old_z_dir_pin= READ(Z_DIR_PIN); //if dualzstepper, both point to same direction.
  1352. //setup new step
  1353. WRITE(Z_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1354. #ifdef Z_DUAL_STEPPER_DRIVERS
  1355. WRITE(Z2_DIR_PIN,(INVERT_Z_DIR)^direction^BABYSTEP_INVERT_Z);
  1356. #endif
  1357. //perform step
  1358. WRITE(Z_STEP_PIN, !INVERT_Z_STEP_PIN);
  1359. LastStepMask |= Z_AXIS_MASK;
  1360. #ifdef Z_DUAL_STEPPER_DRIVERS
  1361. WRITE(Z2_STEP_PIN, !INVERT_Z_STEP_PIN);
  1362. #endif
  1363. //wait a tiny bit
  1364. {
  1365. volatile float x=1./float(axis+1); //absolutely useless
  1366. }
  1367. WRITE(Z_STEP_PIN, INVERT_Z_STEP_PIN);
  1368. #ifdef Z_DUAL_STEPPER_DRIVERS
  1369. WRITE(Z2_STEP_PIN, INVERT_Z_STEP_PIN);
  1370. #endif
  1371. //get old pin state back.
  1372. WRITE(Z_DIR_PIN,old_z_dir_pin);
  1373. #ifdef Z_DUAL_STEPPER_DRIVERS
  1374. WRITE(Z2_DIR_PIN,old_z_dir_pin);
  1375. #endif
  1376. }
  1377. break;
  1378. default: break;
  1379. }
  1380. }
  1381. #endif //BABYSTEPPING
  1382. void digitalPotWrite(int address, int value) // From Arduino DigitalPotControl example
  1383. {
  1384. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  1385. digitalWrite(DIGIPOTSS_PIN,LOW); // take the SS pin low to select the chip
  1386. SPI.transfer(address); // send in the address and value via SPI:
  1387. SPI.transfer(value);
  1388. digitalWrite(DIGIPOTSS_PIN,HIGH); // take the SS pin high to de-select the chip:
  1389. //delay(10);
  1390. #endif
  1391. }
  1392. void EEPROM_read_st(int pos, uint8_t* value, uint8_t size)
  1393. {
  1394. do
  1395. {
  1396. *value = eeprom_read_byte((unsigned char*)pos);
  1397. pos++;
  1398. value++;
  1399. }while(--size);
  1400. }
  1401. void st_current_init() //Initialize Digipot Motor Current
  1402. {
  1403. uint8_t SilentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT);
  1404. SilentModeMenu = SilentMode;
  1405. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1406. pinMode(MOTOR_CURRENT_PWM_XY_PIN, OUTPUT);
  1407. pinMode(MOTOR_CURRENT_PWM_Z_PIN, OUTPUT);
  1408. pinMode(MOTOR_CURRENT_PWM_E_PIN, OUTPUT);
  1409. if((SilentMode == SILENT_MODE_OFF) || (farm_mode) ){
  1410. motor_current_setting[0] = motor_current_setting_loud[0];
  1411. motor_current_setting[1] = motor_current_setting_loud[1];
  1412. motor_current_setting[2] = motor_current_setting_loud[2];
  1413. }else{
  1414. motor_current_setting[0] = motor_current_setting_silent[0];
  1415. motor_current_setting[1] = motor_current_setting_silent[1];
  1416. motor_current_setting[2] = motor_current_setting_silent[2];
  1417. }
  1418. st_current_set(0, motor_current_setting[0]);
  1419. st_current_set(1, motor_current_setting[1]);
  1420. st_current_set(2, motor_current_setting[2]);
  1421. //Set timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1422. TCCR5B = (TCCR5B & ~(_BV(CS50) | _BV(CS51) | _BV(CS52))) | _BV(CS50);
  1423. #endif
  1424. }
  1425. void st_current_set(uint8_t driver, int current)
  1426. {
  1427. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  1428. if (driver == 0) analogWrite(MOTOR_CURRENT_PWM_XY_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1429. if (driver == 1) analogWrite(MOTOR_CURRENT_PWM_Z_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1430. if (driver == 2) analogWrite(MOTOR_CURRENT_PWM_E_PIN, (long)current * 255L / (long)MOTOR_CURRENT_PWM_RANGE);
  1431. #endif
  1432. }
  1433. void microstep_init()
  1434. {
  1435. const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1436. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1437. pinMode(E1_MS1_PIN,OUTPUT);
  1438. pinMode(E1_MS2_PIN,OUTPUT);
  1439. #endif
  1440. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  1441. pinMode(X_MS1_PIN,OUTPUT);
  1442. pinMode(X_MS2_PIN,OUTPUT);
  1443. pinMode(Y_MS1_PIN,OUTPUT);
  1444. pinMode(Y_MS2_PIN,OUTPUT);
  1445. pinMode(Z_MS1_PIN,OUTPUT);
  1446. pinMode(Z_MS2_PIN,OUTPUT);
  1447. pinMode(E0_MS1_PIN,OUTPUT);
  1448. pinMode(E0_MS2_PIN,OUTPUT);
  1449. for(int i=0;i<=4;i++) microstep_mode(i,microstep_modes[i]);
  1450. #endif
  1451. }
  1452. #ifndef TMC2130
  1453. void microstep_ms(uint8_t driver, int8_t ms1, int8_t ms2)
  1454. {
  1455. if(ms1 > -1) switch(driver)
  1456. {
  1457. case 0: digitalWrite( X_MS1_PIN,ms1); break;
  1458. case 1: digitalWrite( Y_MS1_PIN,ms1); break;
  1459. case 2: digitalWrite( Z_MS1_PIN,ms1); break;
  1460. case 3: digitalWrite(E0_MS1_PIN,ms1); break;
  1461. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1462. case 4: digitalWrite(E1_MS1_PIN,ms1); break;
  1463. #endif
  1464. }
  1465. if(ms2 > -1) switch(driver)
  1466. {
  1467. case 0: digitalWrite( X_MS2_PIN,ms2); break;
  1468. case 1: digitalWrite( Y_MS2_PIN,ms2); break;
  1469. case 2: digitalWrite( Z_MS2_PIN,ms2); break;
  1470. case 3: digitalWrite(E0_MS2_PIN,ms2); break;
  1471. #if defined(E1_MS2_PIN) && E1_MS2_PIN > -1
  1472. case 4: digitalWrite(E1_MS2_PIN,ms2); break;
  1473. #endif
  1474. }
  1475. }
  1476. void microstep_mode(uint8_t driver, uint8_t stepping_mode)
  1477. {
  1478. switch(stepping_mode)
  1479. {
  1480. case 1: microstep_ms(driver,MICROSTEP1); break;
  1481. case 2: microstep_ms(driver,MICROSTEP2); break;
  1482. case 4: microstep_ms(driver,MICROSTEP4); break;
  1483. case 8: microstep_ms(driver,MICROSTEP8); break;
  1484. case 16: microstep_ms(driver,MICROSTEP16); break;
  1485. }
  1486. }
  1487. void microstep_readings()
  1488. {
  1489. SERIAL_PROTOCOLPGM("MS1,MS2 Pins\n");
  1490. SERIAL_PROTOCOLPGM("X: ");
  1491. SERIAL_PROTOCOL( digitalRead(X_MS1_PIN));
  1492. SERIAL_PROTOCOLLN( digitalRead(X_MS2_PIN));
  1493. SERIAL_PROTOCOLPGM("Y: ");
  1494. SERIAL_PROTOCOL( digitalRead(Y_MS1_PIN));
  1495. SERIAL_PROTOCOLLN( digitalRead(Y_MS2_PIN));
  1496. SERIAL_PROTOCOLPGM("Z: ");
  1497. SERIAL_PROTOCOL( digitalRead(Z_MS1_PIN));
  1498. SERIAL_PROTOCOLLN( digitalRead(Z_MS2_PIN));
  1499. SERIAL_PROTOCOLPGM("E0: ");
  1500. SERIAL_PROTOCOL( digitalRead(E0_MS1_PIN));
  1501. SERIAL_PROTOCOLLN( digitalRead(E0_MS2_PIN));
  1502. #if defined(E1_MS1_PIN) && E1_MS1_PIN > -1
  1503. SERIAL_PROTOCOLPGM("E1: ");
  1504. SERIAL_PROTOCOL( digitalRead(E1_MS1_PIN));
  1505. SERIAL_PROTOCOLLN( digitalRead(E1_MS2_PIN));
  1506. #endif
  1507. }
  1508. #endif //TMC2130