stepper.cpp 49 KB

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