stepper.cpp 52 KB

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