stepper.cpp 52 KB

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