stepper.cpp 52 KB

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