stepper.cpp 52 KB

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