stepper.cpp 52 KB

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