stepper.cpp 51 KB

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