My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stepper.cpp 50KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * stepper.cpp - A singleton object to execute motion plans using stepper motors
  24. * Marlin Firmware
  25. *
  26. * Derived from Grbl
  27. * Copyright (c) 2009-2011 Simen Svale Skogsrud
  28. *
  29. * Grbl is free software: you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation, either version 3 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * Grbl is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with Grbl. If not, see <http://www.gnu.org/licenses/>.
  41. */
  42. /* The timer calculations of this module informed by the 'RepRap cartesian firmware' by Zack Smith
  43. and Philipp Tiefenbacher. */
  44. #include "stepper.h"
  45. #ifdef __AVR__
  46. #include "speed_lookuptable.h"
  47. #endif
  48. #include "endstops.h"
  49. #include "planner.h"
  50. #include "motion.h"
  51. #include "../module/temperature.h"
  52. #include "../lcd/ultralcd.h"
  53. #include "../core/language.h"
  54. #include "../gcode/queue.h"
  55. #include "../sd/cardreader.h"
  56. #include "../Marlin.h"
  57. #if MB(ALLIGATOR)
  58. #include "../feature/dac/dac_dac084s085.h"
  59. #endif
  60. #if HAS_DIGIPOTSS
  61. #include <SPI.h>
  62. #endif
  63. Stepper stepper; // Singleton
  64. // public:
  65. block_t* Stepper::current_block = NULL; // A pointer to the block currently being traced
  66. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  67. bool Stepper::abort_on_endstop_hit = false;
  68. #endif
  69. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  70. bool Stepper::performing_homing = false;
  71. #endif
  72. #if HAS_MOTOR_CURRENT_PWM
  73. uint32_t Stepper::motor_current_setting[3]; // Initialized by settings.load()
  74. #endif
  75. // private:
  76. uint8_t Stepper::last_direction_bits = 0; // The next stepping-bits to be output
  77. int16_t Stepper::cleaning_buffer_counter = 0;
  78. #if ENABLED(X_DUAL_ENDSTOPS)
  79. bool Stepper::locked_x_motor = false, Stepper::locked_x2_motor = false;
  80. #endif
  81. #if ENABLED(Y_DUAL_ENDSTOPS)
  82. bool Stepper::locked_y_motor = false, Stepper::locked_y2_motor = false;
  83. #endif
  84. #if ENABLED(Z_DUAL_ENDSTOPS)
  85. bool Stepper::locked_z_motor = false, Stepper::locked_z2_motor = false;
  86. #endif
  87. long Stepper::counter_X = 0,
  88. Stepper::counter_Y = 0,
  89. Stepper::counter_Z = 0,
  90. Stepper::counter_E = 0;
  91. volatile uint32_t Stepper::step_events_completed = 0; // The number of step events executed in the current block
  92. #if ENABLED(LIN_ADVANCE)
  93. uint32_t Stepper::LA_decelerate_after;
  94. constexpr hal_timer_t ADV_NEVER = HAL_TIMER_TYPE_MAX;
  95. hal_timer_t Stepper::nextMainISR = 0,
  96. Stepper::nextAdvanceISR = ADV_NEVER,
  97. Stepper::eISR_Rate = ADV_NEVER;
  98. uint16_t Stepper::current_adv_steps = 0,
  99. Stepper::final_adv_steps,
  100. Stepper::max_adv_steps;
  101. int8_t Stepper::e_steps = 0,
  102. Stepper::LA_active_extruder; // Copy from current executed block. Needed because current_block is set to NULL "too early".
  103. bool Stepper::use_advance_lead;
  104. #endif // LIN_ADVANCE
  105. long Stepper::acceleration_time, Stepper::deceleration_time;
  106. volatile long Stepper::count_position[NUM_AXIS] = { 0 };
  107. volatile signed char Stepper::count_direction[NUM_AXIS] = { 1, 1, 1, 1 };
  108. #if ENABLED(MIXING_EXTRUDER)
  109. long Stepper::counter_m[MIXING_STEPPERS];
  110. #endif
  111. uint8_t Stepper::step_loops, Stepper::step_loops_nominal;
  112. hal_timer_t Stepper::OCR1A_nominal,
  113. Stepper::acc_step_rate; // needed for deceleration start point
  114. volatile long Stepper::endstops_trigsteps[XYZ];
  115. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  116. #define LOCKED_X_MOTOR locked_x_motor
  117. #define LOCKED_Y_MOTOR locked_y_motor
  118. #define LOCKED_Z_MOTOR locked_z_motor
  119. #define LOCKED_X2_MOTOR locked_x2_motor
  120. #define LOCKED_Y2_MOTOR locked_y2_motor
  121. #define LOCKED_Z2_MOTOR locked_z2_motor
  122. #define DUAL_ENDSTOP_APPLY_STEP(AXIS,v) \
  123. if (performing_homing) { \
  124. if (AXIS##_HOME_DIR < 0) { \
  125. if (!(TEST(endstops.old_endstop_bits, AXIS##_MIN) && (count_direction[AXIS##_AXIS] < 0)) && !LOCKED_##AXIS##_MOTOR) AXIS##_STEP_WRITE(v); \
  126. if (!(TEST(endstops.old_endstop_bits, AXIS##2_MIN) && (count_direction[AXIS##_AXIS] < 0)) && !LOCKED_##AXIS##2_MOTOR) AXIS##2_STEP_WRITE(v); \
  127. } \
  128. else { \
  129. if (!(TEST(endstops.old_endstop_bits, AXIS##_MAX) && (count_direction[AXIS##_AXIS] > 0)) && !LOCKED_##AXIS##_MOTOR) AXIS##_STEP_WRITE(v); \
  130. if (!(TEST(endstops.old_endstop_bits, AXIS##2_MAX) && (count_direction[AXIS##_AXIS] > 0)) && !LOCKED_##AXIS##2_MOTOR) AXIS##2_STEP_WRITE(v); \
  131. } \
  132. } \
  133. else { \
  134. AXIS##_STEP_WRITE(v); \
  135. AXIS##2_STEP_WRITE(v); \
  136. }
  137. #endif
  138. #if ENABLED(X_DUAL_STEPPER_DRIVERS)
  139. #define X_APPLY_DIR(v,Q) do{ X_DIR_WRITE(v); X2_DIR_WRITE((v) != INVERT_X2_VS_X_DIR); }while(0)
  140. #if ENABLED(X_DUAL_ENDSTOPS)
  141. #define X_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(X,v)
  142. #else
  143. #define X_APPLY_STEP(v,Q) do{ X_STEP_WRITE(v); X2_STEP_WRITE(v); }while(0)
  144. #endif
  145. #elif ENABLED(DUAL_X_CARRIAGE)
  146. #define X_APPLY_DIR(v,ALWAYS) \
  147. if (extruder_duplication_enabled || ALWAYS) { \
  148. X_DIR_WRITE(v); \
  149. X2_DIR_WRITE(v); \
  150. } \
  151. else { \
  152. if (current_block->active_extruder) X2_DIR_WRITE(v); else X_DIR_WRITE(v); \
  153. }
  154. #define X_APPLY_STEP(v,ALWAYS) \
  155. if (extruder_duplication_enabled || ALWAYS) { \
  156. X_STEP_WRITE(v); \
  157. X2_STEP_WRITE(v); \
  158. } \
  159. else { \
  160. if (current_block->active_extruder) X2_STEP_WRITE(v); else X_STEP_WRITE(v); \
  161. }
  162. #else
  163. #define X_APPLY_DIR(v,Q) X_DIR_WRITE(v)
  164. #define X_APPLY_STEP(v,Q) X_STEP_WRITE(v)
  165. #endif
  166. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  167. #define Y_APPLY_DIR(v,Q) do{ Y_DIR_WRITE(v); Y2_DIR_WRITE((v) != INVERT_Y2_VS_Y_DIR); }while(0)
  168. #if ENABLED(Y_DUAL_ENDSTOPS)
  169. #define Y_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(Y,v)
  170. #else
  171. #define Y_APPLY_STEP(v,Q) do{ Y_STEP_WRITE(v); Y2_STEP_WRITE(v); }while(0)
  172. #endif
  173. #else
  174. #define Y_APPLY_DIR(v,Q) Y_DIR_WRITE(v)
  175. #define Y_APPLY_STEP(v,Q) Y_STEP_WRITE(v)
  176. #endif
  177. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  178. #define Z_APPLY_DIR(v,Q) do{ Z_DIR_WRITE(v); Z2_DIR_WRITE(v); }while(0)
  179. #if ENABLED(Z_DUAL_ENDSTOPS)
  180. #define Z_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(Z,v)
  181. #else
  182. #define Z_APPLY_STEP(v,Q) do{ Z_STEP_WRITE(v); Z2_STEP_WRITE(v); }while(0)
  183. #endif
  184. #else
  185. #define Z_APPLY_DIR(v,Q) Z_DIR_WRITE(v)
  186. #define Z_APPLY_STEP(v,Q) Z_STEP_WRITE(v)
  187. #endif
  188. #if DISABLED(MIXING_EXTRUDER)
  189. #define E_APPLY_STEP(v,Q) E_STEP_WRITE(v)
  190. #endif
  191. /**
  192. * __________________________
  193. * /| |\ _________________ ^
  194. * / | | \ /| |\ |
  195. * / | | \ / | | \ s
  196. * / | | | | | \ p
  197. * / | | | | | \ e
  198. * +-----+------------------------+---+--+---------------+----+ e
  199. * | BLOCK 1 | BLOCK 2 | d
  200. *
  201. * time ----->
  202. *
  203. * The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  204. * first block->accelerate_until step_events_completed, then keeps going at constant speed until
  205. * step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  206. * The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
  207. */
  208. void Stepper::wake_up() {
  209. // TCNT1 = 0;
  210. ENABLE_STEPPER_DRIVER_INTERRUPT();
  211. }
  212. /**
  213. * Set the stepper direction of each axis
  214. *
  215. * COREXY: X_AXIS=A_AXIS and Y_AXIS=B_AXIS
  216. * COREXZ: X_AXIS=A_AXIS and Z_AXIS=C_AXIS
  217. * COREYZ: Y_AXIS=B_AXIS and Z_AXIS=C_AXIS
  218. */
  219. void Stepper::set_directions() {
  220. #define SET_STEP_DIR(AXIS) \
  221. if (motor_direction(AXIS ##_AXIS)) { \
  222. AXIS ##_APPLY_DIR(INVERT_## AXIS ##_DIR, false); \
  223. count_direction[AXIS ##_AXIS] = -1; \
  224. } \
  225. else { \
  226. AXIS ##_APPLY_DIR(!INVERT_## AXIS ##_DIR, false); \
  227. count_direction[AXIS ##_AXIS] = 1; \
  228. }
  229. #if HAS_X_DIR
  230. SET_STEP_DIR(X); // A
  231. #endif
  232. #if HAS_Y_DIR
  233. SET_STEP_DIR(Y); // B
  234. #endif
  235. #if HAS_Z_DIR
  236. SET_STEP_DIR(Z); // C
  237. #endif
  238. #if DISABLED(LIN_ADVANCE)
  239. if (motor_direction(E_AXIS)) {
  240. REV_E_DIR();
  241. count_direction[E_AXIS] = -1;
  242. }
  243. else {
  244. NORM_E_DIR();
  245. count_direction[E_AXIS] = 1;
  246. }
  247. #endif // !LIN_ADVANCE
  248. }
  249. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  250. extern volatile uint8_t e_hit;
  251. #endif
  252. /**
  253. * Stepper Driver Interrupt
  254. *
  255. * Directly pulses the stepper motors at high frequency.
  256. *
  257. * AVR :
  258. * Timer 1 runs at a base frequency of 2MHz, with this ISR using OCR1A compare mode.
  259. *
  260. * OCR1A Frequency
  261. * 1 2 MHz
  262. * 50 40 KHz
  263. * 100 20 KHz - capped max rate
  264. * 200 10 KHz - nominal max rate
  265. * 2000 1 KHz - sleep rate
  266. * 4000 500 Hz - init rate
  267. */
  268. HAL_STEP_TIMER_ISR {
  269. HAL_timer_isr_prologue(STEP_TIMER_NUM);
  270. #if ENABLED(LIN_ADVANCE)
  271. Stepper::advance_isr_scheduler();
  272. #else
  273. Stepper::isr();
  274. #endif
  275. }
  276. void Stepper::isr() {
  277. #define ENDSTOP_NOMINAL_OCR_VAL 1500 * HAL_TICKS_PER_US // Check endstops every 1.5ms to guarantee two stepper ISRs within 5ms for BLTouch
  278. #define OCR_VAL_TOLERANCE 500 * HAL_TICKS_PER_US // First max delay is 2.0ms, last min delay is 0.5ms, all others 1.5ms
  279. #if DISABLED(LIN_ADVANCE)
  280. // Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
  281. DISABLE_TEMPERATURE_INTERRUPT(); // Temperature ISR
  282. DISABLE_STEPPER_DRIVER_INTERRUPT();
  283. #ifndef CPU_32_BIT
  284. sei();
  285. #endif
  286. #endif
  287. hal_timer_t ocr_val;
  288. static uint32_t step_remaining = 0; // SPLIT function always runs. This allows 16 bit timers to be
  289. // used to generate the stepper ISR.
  290. #define SPLIT(L) do { \
  291. if (L > ENDSTOP_NOMINAL_OCR_VAL) { \
  292. const uint32_t remainder = (uint32_t)L % (ENDSTOP_NOMINAL_OCR_VAL); \
  293. ocr_val = (remainder < OCR_VAL_TOLERANCE) ? ENDSTOP_NOMINAL_OCR_VAL + remainder : ENDSTOP_NOMINAL_OCR_VAL; \
  294. step_remaining = (uint32_t)L - ocr_val; \
  295. } \
  296. else \
  297. ocr_val = L;\
  298. }while(0)
  299. // Time remaining before the next step?
  300. if (step_remaining) {
  301. // Make sure endstops are updated
  302. if (ENDSTOPS_ENABLED) endstops.update();
  303. // Next ISR either for endstops or stepping
  304. ocr_val = step_remaining <= ENDSTOP_NOMINAL_OCR_VAL ? step_remaining : ENDSTOP_NOMINAL_OCR_VAL;
  305. step_remaining -= ocr_val;
  306. _NEXT_ISR(ocr_val);
  307. #if DISABLED(LIN_ADVANCE)
  308. #ifdef CPU_32_BIT
  309. HAL_timer_set_compare(STEP_TIMER_NUM, ocr_val);
  310. #else
  311. NOLESS(OCR1A, TCNT1 + 16);
  312. #endif
  313. HAL_ENABLE_ISRs(); // re-enable ISRs
  314. #endif
  315. return;
  316. }
  317. //
  318. // When cleaning, discard the current block and run fast
  319. //
  320. if (cleaning_buffer_counter) {
  321. if (cleaning_buffer_counter < 0) { // Count up for endstop hit
  322. if (current_block) planner.discard_current_block(); // Discard the active block that led to the trigger
  323. if (!planner.discard_continued_block()) // Discard next CONTINUED block
  324. cleaning_buffer_counter = 0; // Keep discarding until non-CONTINUED
  325. }
  326. else {
  327. planner.discard_current_block();
  328. --cleaning_buffer_counter; // Count down for abort print
  329. #if ENABLED(SD_FINISHED_STEPPERRELEASE) && defined(SD_FINISHED_RELEASECOMMAND)
  330. if (!cleaning_buffer_counter) enqueue_and_echo_commands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  331. #endif
  332. }
  333. current_block = NULL; // Prep to get a new block after cleaning
  334. _NEXT_ISR(HAL_STEPPER_TIMER_RATE / 10000); // Run at max speed - 10 KHz
  335. HAL_ENABLE_ISRs();
  336. return;
  337. }
  338. // If there is no current block, attempt to pop one from the buffer
  339. if (!current_block) {
  340. // Anything in the buffer?
  341. if ((current_block = planner.get_current_block())) {
  342. trapezoid_generator_reset();
  343. // Initialize Bresenham counters to 1/2 the ceiling
  344. counter_X = counter_Y = counter_Z = counter_E = -(current_block->step_event_count >> 1);
  345. #if ENABLED(MIXING_EXTRUDER)
  346. MIXING_STEPPERS_LOOP(i)
  347. counter_m[i] = -(current_block->mix_event_count[i] >> 1);
  348. #endif
  349. step_events_completed = 0;
  350. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  351. e_hit = 2; // Needed for the case an endstop is already triggered before the new move begins.
  352. // No 'change' can be detected.
  353. #endif
  354. #if ENABLED(Z_LATE_ENABLE)
  355. if (current_block->steps[Z_AXIS] > 0) {
  356. enable_Z();
  357. _NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz
  358. HAL_ENABLE_ISRs(); // re-enable ISRs
  359. return;
  360. }
  361. #endif
  362. }
  363. else {
  364. _NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz
  365. HAL_ENABLE_ISRs(); // re-enable ISRs
  366. return;
  367. }
  368. }
  369. // Update endstops state, if enabled
  370. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  371. if (e_hit && ENDSTOPS_ENABLED) {
  372. endstops.update();
  373. e_hit--;
  374. }
  375. #else
  376. if (ENDSTOPS_ENABLED) endstops.update();
  377. #endif
  378. // Take multiple steps per interrupt (For high speed moves)
  379. bool all_steps_done = false;
  380. for (uint8_t i = step_loops; i--;) {
  381. #if ENABLED(LIN_ADVANCE)
  382. counter_E += current_block->steps[E_AXIS];
  383. if (counter_E > 0) {
  384. counter_E -= current_block->step_event_count;
  385. #if DISABLED(MIXING_EXTRUDER)
  386. // Don't step E here for mixing extruder
  387. count_position[E_AXIS] += count_direction[E_AXIS];
  388. motor_direction(E_AXIS) ? --e_steps : ++e_steps;
  389. #endif
  390. }
  391. #if ENABLED(MIXING_EXTRUDER)
  392. // Step mixing steppers proportionally
  393. const bool dir = motor_direction(E_AXIS);
  394. MIXING_STEPPERS_LOOP(j) {
  395. counter_m[j] += current_block->steps[E_AXIS];
  396. if (counter_m[j] > 0) {
  397. counter_m[j] -= current_block->mix_event_count[j];
  398. dir ? --e_steps[j] : ++e_steps[j];
  399. }
  400. }
  401. #endif
  402. #endif // LIN_ADVANCE
  403. #define _COUNTER(AXIS) counter_## AXIS
  404. #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
  405. #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
  406. // Advance the Bresenham counter; start a pulse if the axis needs a step
  407. #define PULSE_START(AXIS) \
  408. _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
  409. if (_COUNTER(AXIS) > 0) { _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS),0); }
  410. // Stop an active pulse, reset the Bresenham counter, update the position
  411. #define PULSE_STOP(AXIS) \
  412. if (_COUNTER(AXIS) > 0) { \
  413. _COUNTER(AXIS) -= current_block->step_event_count; \
  414. count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
  415. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS),0); \
  416. }
  417. /**
  418. * Estimate the number of cycles that the stepper logic already takes
  419. * up between the start and stop of the X stepper pulse.
  420. *
  421. * Currently this uses very modest estimates of around 5 cycles.
  422. * True values may be derived by careful testing.
  423. *
  424. * Once any delay is added, the cost of the delay code itself
  425. * may be subtracted from this value to get a more accurate delay.
  426. * Delays under 20 cycles (1.25µs) will be very accurate, using NOPs.
  427. * Longer delays use a loop. The resolution is 8 cycles.
  428. */
  429. #if HAS_X_STEP
  430. #define _CYCLE_APPROX_1 5
  431. #else
  432. #define _CYCLE_APPROX_1 0
  433. #endif
  434. #if ENABLED(X_DUAL_STEPPER_DRIVERS)
  435. #define _CYCLE_APPROX_2 _CYCLE_APPROX_1 + 4
  436. #else
  437. #define _CYCLE_APPROX_2 _CYCLE_APPROX_1
  438. #endif
  439. #if HAS_Y_STEP
  440. #define _CYCLE_APPROX_3 _CYCLE_APPROX_2 + 5
  441. #else
  442. #define _CYCLE_APPROX_3 _CYCLE_APPROX_2
  443. #endif
  444. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  445. #define _CYCLE_APPROX_4 _CYCLE_APPROX_3 + 4
  446. #else
  447. #define _CYCLE_APPROX_4 _CYCLE_APPROX_3
  448. #endif
  449. #if HAS_Z_STEP
  450. #define _CYCLE_APPROX_5 _CYCLE_APPROX_4 + 5
  451. #else
  452. #define _CYCLE_APPROX_5 _CYCLE_APPROX_4
  453. #endif
  454. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  455. #define _CYCLE_APPROX_6 _CYCLE_APPROX_5 + 4
  456. #else
  457. #define _CYCLE_APPROX_6 _CYCLE_APPROX_5
  458. #endif
  459. #if DISABLED(LIN_ADVANCE)
  460. #if ENABLED(MIXING_EXTRUDER)
  461. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6 + (MIXING_STEPPERS) * 6
  462. #else
  463. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6 + 5
  464. #endif
  465. #else
  466. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6
  467. #endif
  468. #define CYCLES_EATEN_XYZE _CYCLE_APPROX_7
  469. #define EXTRA_CYCLES_XYZE (STEP_PULSE_CYCLES - (CYCLES_EATEN_XYZE))
  470. /**
  471. * If a minimum pulse time was specified get the timer 0 value.
  472. *
  473. * On AVR the TCNT0 timer has an 8x prescaler, so it increments every 8 cycles.
  474. * That's every 0.5µs on 16MHz and every 0.4µs on 20MHz.
  475. * 20 counts of TCNT0 -by itself- is a good pulse delay.
  476. * 10µs = 160 or 200 cycles.
  477. */
  478. #if EXTRA_CYCLES_XYZE > 20
  479. hal_timer_t pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  480. #endif
  481. #if HAS_X_STEP
  482. PULSE_START(X);
  483. #endif
  484. #if HAS_Y_STEP
  485. PULSE_START(Y);
  486. #endif
  487. #if HAS_Z_STEP
  488. PULSE_START(Z);
  489. #endif
  490. // For non-advance use linear interpolation for E also
  491. #if DISABLED(LIN_ADVANCE)
  492. #if ENABLED(MIXING_EXTRUDER)
  493. // Keep updating the single E axis
  494. counter_E += current_block->steps[E_AXIS];
  495. // Tick the counters used for this mix
  496. MIXING_STEPPERS_LOOP(j) {
  497. // Step mixing steppers (proportionally)
  498. counter_m[j] += current_block->steps[E_AXIS];
  499. // Step when the counter goes over zero
  500. if (counter_m[j] > 0) En_STEP_WRITE(j, !INVERT_E_STEP_PIN);
  501. }
  502. #else // !MIXING_EXTRUDER
  503. PULSE_START(E);
  504. #endif
  505. #endif // !LIN_ADVANCE
  506. // For minimum pulse time wait before stopping pulses
  507. #if EXTRA_CYCLES_XYZE > 20
  508. while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  509. pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  510. #elif EXTRA_CYCLES_XYZE > 0
  511. DELAY_NOPS(EXTRA_CYCLES_XYZE);
  512. #endif
  513. #if HAS_X_STEP
  514. PULSE_STOP(X);
  515. #endif
  516. #if HAS_Y_STEP
  517. PULSE_STOP(Y);
  518. #endif
  519. #if HAS_Z_STEP
  520. PULSE_STOP(Z);
  521. #endif
  522. #if DISABLED(LIN_ADVANCE)
  523. #if ENABLED(MIXING_EXTRUDER)
  524. // Always step the single E axis
  525. if (counter_E > 0) {
  526. counter_E -= current_block->step_event_count;
  527. count_position[E_AXIS] += count_direction[E_AXIS];
  528. }
  529. MIXING_STEPPERS_LOOP(j) {
  530. if (counter_m[j] > 0) {
  531. counter_m[j] -= current_block->mix_event_count[j];
  532. En_STEP_WRITE(j, INVERT_E_STEP_PIN);
  533. }
  534. }
  535. #else // !MIXING_EXTRUDER
  536. PULSE_STOP(E);
  537. #endif
  538. #endif // !LIN_ADVANCE
  539. if (++step_events_completed >= current_block->step_event_count) {
  540. all_steps_done = true;
  541. break;
  542. }
  543. // For minimum pulse time wait after stopping pulses also
  544. #if EXTRA_CYCLES_XYZE > 20
  545. if (i) while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  546. #elif EXTRA_CYCLES_XYZE > 0
  547. if (i) DELAY_NOPS(EXTRA_CYCLES_XYZE);
  548. #endif
  549. } // steps_loop
  550. // Calculate new timer value
  551. if (step_events_completed <= (uint32_t)current_block->accelerate_until) {
  552. #ifdef CPU_32_BIT
  553. MultiU32X24toH32(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  554. #else
  555. MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  556. #endif
  557. acc_step_rate += current_block->initial_rate;
  558. // upper limit
  559. NOMORE(acc_step_rate, current_block->nominal_rate);
  560. // step_rate to timer interval
  561. const hal_timer_t interval = calc_timer_interval(acc_step_rate);
  562. SPLIT(interval); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  563. _NEXT_ISR(ocr_val);
  564. acceleration_time += interval;
  565. #if ENABLED(LIN_ADVANCE)
  566. if (current_block->use_advance_lead) {
  567. if (step_events_completed == step_loops || (e_steps && eISR_Rate != current_block->advance_speed)) {
  568. nextAdvanceISR = 0; // Wake up eISR on first acceleration loop and fire ISR if final adv_rate is reached
  569. eISR_Rate = current_block->advance_speed;
  570. }
  571. }
  572. else {
  573. eISR_Rate = ADV_NEVER;
  574. if (e_steps) nextAdvanceISR = 0;
  575. }
  576. #endif // LIN_ADVANCE
  577. }
  578. else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
  579. hal_timer_t step_rate;
  580. #ifdef CPU_32_BIT
  581. MultiU32X24toH32(step_rate, deceleration_time, current_block->acceleration_rate);
  582. #else
  583. MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  584. #endif
  585. if (step_rate < acc_step_rate) { // Still decelerating?
  586. step_rate = acc_step_rate - step_rate;
  587. NOLESS(step_rate, current_block->final_rate);
  588. }
  589. else
  590. step_rate = current_block->final_rate;
  591. // step_rate to timer interval
  592. const hal_timer_t interval = calc_timer_interval(step_rate);
  593. SPLIT(interval); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  594. _NEXT_ISR(ocr_val);
  595. deceleration_time += interval;
  596. #if ENABLED(LIN_ADVANCE)
  597. if (current_block->use_advance_lead) {
  598. if (step_events_completed <= (uint32_t)current_block->decelerate_after + step_loops || (e_steps && eISR_Rate != current_block->advance_speed)) {
  599. nextAdvanceISR = 0; // Wake up eISR on first deceleration loop
  600. eISR_Rate = current_block->advance_speed;
  601. }
  602. }
  603. else {
  604. eISR_Rate = ADV_NEVER;
  605. if (e_steps) nextAdvanceISR = 0;
  606. }
  607. #endif // LIN_ADVANCE
  608. }
  609. else {
  610. #if ENABLED(LIN_ADVANCE)
  611. // If we have esteps to execute, fire the next advance_isr "now"
  612. if (e_steps && eISR_Rate != current_block->advance_speed) nextAdvanceISR = 0;
  613. #endif
  614. SPLIT(OCR1A_nominal); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  615. _NEXT_ISR(ocr_val);
  616. // ensure we're running at the correct step rate, even if we just came off an acceleration
  617. step_loops = step_loops_nominal;
  618. }
  619. #if DISABLED(LIN_ADVANCE)
  620. #ifdef CPU_32_BIT
  621. // Make sure stepper interrupt does not monopolise CPU by adjusting count to give about 8 us room
  622. hal_timer_t stepper_timer_count = HAL_timer_get_compare(STEP_TIMER_NUM),
  623. stepper_timer_current_count = HAL_timer_get_count(STEP_TIMER_NUM) + 8 * HAL_TICKS_PER_US;
  624. HAL_timer_set_compare(STEP_TIMER_NUM, max(stepper_timer_count, stepper_timer_current_count));
  625. #else
  626. NOLESS(OCR1A, TCNT1 + 16);
  627. #endif
  628. #endif
  629. // If current block is finished, reset pointer
  630. if (all_steps_done) {
  631. current_block = NULL;
  632. planner.discard_current_block();
  633. }
  634. #if DISABLED(LIN_ADVANCE)
  635. HAL_ENABLE_ISRs(); // re-enable ISRs
  636. #endif
  637. }
  638. #if ENABLED(LIN_ADVANCE)
  639. #define CYCLES_EATEN_E (E_STEPPERS * 5)
  640. #define EXTRA_CYCLES_E (STEP_PULSE_CYCLES - (CYCLES_EATEN_E))
  641. // Timer interrupt for E. e_steps is set in the main routine;
  642. void Stepper::advance_isr() {
  643. #if ENABLED(MK2_MULTIPLEXER)
  644. // Even-numbered steppers are reversed
  645. #define SET_E_STEP_DIR(INDEX) \
  646. if (e_steps) E## INDEX ##_DIR_WRITE(e_steps < 0 ? !INVERT_E## INDEX ##_DIR ^ TEST(INDEX, 0) : INVERT_E## INDEX ##_DIR ^ TEST(INDEX, 0))
  647. #else
  648. #define SET_E_STEP_DIR(INDEX) \
  649. if (e_steps) E## INDEX ##_DIR_WRITE(e_steps < 0 ? INVERT_E## INDEX ##_DIR : !INVERT_E## INDEX ##_DIR)
  650. #endif
  651. #define START_E_PULSE(INDEX) \
  652. if (e_steps) E## INDEX ##_STEP_WRITE(!INVERT_E_STEP_PIN)
  653. #define STOP_E_PULSE(INDEX) \
  654. if (e_steps) { \
  655. e_steps < 0 ? ++e_steps : --e_steps; \
  656. E## INDEX ##_STEP_WRITE(INVERT_E_STEP_PIN); \
  657. }
  658. if (current_block->use_advance_lead) {
  659. if (step_events_completed > LA_decelerate_after && current_adv_steps > final_adv_steps) {
  660. e_steps--;
  661. current_adv_steps--;
  662. nextAdvanceISR = eISR_Rate;
  663. }
  664. else if (step_events_completed < LA_decelerate_after && current_adv_steps < max_adv_steps) {
  665. //step_events_completed <= (uint32_t)current_block->accelerate_until) {
  666. e_steps++;
  667. current_adv_steps++;
  668. nextAdvanceISR = eISR_Rate;
  669. }
  670. else {
  671. nextAdvanceISR = ADV_NEVER;
  672. eISR_Rate = ADV_NEVER;
  673. }
  674. }
  675. else
  676. nextAdvanceISR = ADV_NEVER;
  677. switch(LA_active_extruder) {
  678. case 0: SET_E_STEP_DIR(0); break;
  679. #if EXTRUDERS > 1
  680. case 1: SET_E_STEP_DIR(1); break;
  681. #if EXTRUDERS > 2
  682. case 2: SET_E_STEP_DIR(2); break;
  683. #if EXTRUDERS > 3
  684. case 3: SET_E_STEP_DIR(3); break;
  685. #if EXTRUDERS > 4
  686. case 4: SET_E_STEP_DIR(4); break;
  687. #endif // EXTRUDERS > 4
  688. #endif // EXTRUDERS > 3
  689. #endif // EXTRUDERS > 2
  690. #endif // EXTRUDERS > 1
  691. }
  692. // Step E stepper if we have steps
  693. while (e_steps) {
  694. #if EXTRA_CYCLES_E > 20
  695. hal_timer_t pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  696. #endif
  697. switch(LA_active_extruder) {
  698. case 0: START_E_PULSE(0); break;
  699. #if EXTRUDERS > 1
  700. case 1: START_E_PULSE(1); break;
  701. #if EXTRUDERS > 2
  702. case 2: START_E_PULSE(2); break;
  703. #if EXTRUDERS > 3
  704. case 3: START_E_PULSE(3); break;
  705. #if EXTRUDERS > 4
  706. case 4: START_E_PULSE(4); break;
  707. #endif // EXTRUDERS > 4
  708. #endif // EXTRUDERS > 3
  709. #endif // EXTRUDERS > 2
  710. #endif // EXTRUDERS > 1
  711. }
  712. // For minimum pulse time wait before stopping pulses
  713. #if EXTRA_CYCLES_E > 20
  714. while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  715. pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  716. #elif EXTRA_CYCLES_E > 0
  717. DELAY_NOPS(EXTRA_CYCLES_E);
  718. #endif
  719. switch(LA_active_extruder) {
  720. case 0: STOP_E_PULSE(0); break;
  721. #if EXTRUDERS > 1
  722. case 1: STOP_E_PULSE(1); break;
  723. #if EXTRUDERS > 2
  724. case 2: STOP_E_PULSE(2); break;
  725. #if EXTRUDERS > 3
  726. case 3: STOP_E_PULSE(3); break;
  727. #if EXTRUDERS > 4
  728. case 4: STOP_E_PULSE(4); break;
  729. #endif // EXTRUDERS > 4
  730. #endif // EXTRUDERS > 3
  731. #endif // EXTRUDERS > 2
  732. #endif // EXTRUDERS > 1
  733. }
  734. // For minimum pulse time wait before looping
  735. #if EXTRA_CYCLES_E > 20
  736. while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  737. #elif EXTRA_CYCLES_E > 0
  738. DELAY_NOPS(EXTRA_CYCLES_E);
  739. #endif
  740. } // e_steps
  741. }
  742. void Stepper::advance_isr_scheduler() {
  743. // Disable Timer0 ISRs and enable global ISR again to capture UART events (incoming chars)
  744. DISABLE_TEMPERATURE_INTERRUPT(); // Temperature ISR
  745. DISABLE_STEPPER_DRIVER_INTERRUPT();
  746. sei();
  747. // Run main stepping ISR if flagged
  748. if (!nextMainISR) isr();
  749. // Run Advance stepping ISR if flagged
  750. if (!nextAdvanceISR) advance_isr();
  751. // Is the next advance ISR scheduled before the next main ISR?
  752. if (nextAdvanceISR <= nextMainISR) {
  753. // Set up the next interrupt
  754. HAL_timer_set_compare(STEP_TIMER_NUM, nextAdvanceISR);
  755. // New interval for the next main ISR
  756. if (nextMainISR) nextMainISR -= nextAdvanceISR;
  757. // Will call Stepper::advance_isr on the next interrupt
  758. nextAdvanceISR = 0;
  759. }
  760. else {
  761. // The next main ISR comes first
  762. HAL_timer_set_compare(STEP_TIMER_NUM, nextMainISR);
  763. // New interval for the next advance ISR, if any
  764. if (nextAdvanceISR && nextAdvanceISR != ADV_NEVER)
  765. nextAdvanceISR -= nextMainISR;
  766. // Will call Stepper::isr on the next interrupt
  767. nextMainISR = 0;
  768. }
  769. // Don't run the ISR faster than possible
  770. #ifdef CPU_32_BIT
  771. // Make sure stepper interrupt does not monopolise CPU by adjusting count to give about 8 us room
  772. uint32_t stepper_timer_count = HAL_timer_get_compare(STEP_TIMER_NUM),
  773. stepper_timer_current_count = HAL_timer_get_count(STEP_TIMER_NUM) + 8 * HAL_TICKS_PER_US;
  774. HAL_timer_set_compare(STEP_TIMER_NUM, max(stepper_timer_count, stepper_timer_current_count));
  775. #else
  776. NOLESS(OCR1A, TCNT1 + 16);
  777. #endif
  778. // Restore original ISR settings
  779. HAL_ENABLE_ISRs();
  780. }
  781. #endif // LIN_ADVANCE
  782. void Stepper::init() {
  783. // Init Digipot Motor Current
  784. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  785. digipot_init();
  786. #endif
  787. #if MB(ALLIGATOR)
  788. const float motor_current[] = MOTOR_CURRENT;
  789. unsigned int digipot_motor = 0;
  790. for (uint8_t i = 0; i < 3 + EXTRUDERS; i++) {
  791. digipot_motor = 255 * (motor_current[i] / 2.5);
  792. dac084s085::setValue(i, digipot_motor);
  793. }
  794. #endif//MB(ALLIGATOR)
  795. // Init Microstepping Pins
  796. #if HAS_MICROSTEPS
  797. microstep_init();
  798. #endif
  799. // Init TMC Steppers
  800. #if ENABLED(HAVE_TMCDRIVER)
  801. tmc_init();
  802. #endif
  803. // Init TMC2130 Steppers
  804. #if ENABLED(HAVE_TMC2130)
  805. tmc2130_init();
  806. #endif
  807. // Init TMC2208 Steppers
  808. #if ENABLED(HAVE_TMC2208)
  809. tmc2208_init();
  810. #endif
  811. // TRAMS, TMC2130 and TMC2208 advanced settings
  812. #if HAS_TRINAMIC
  813. TMC_ADV()
  814. #endif
  815. // Init L6470 Steppers
  816. #if ENABLED(HAVE_L6470DRIVER)
  817. L6470_init();
  818. #endif
  819. // Init Dir Pins
  820. #if HAS_X_DIR
  821. X_DIR_INIT;
  822. #endif
  823. #if HAS_X2_DIR
  824. X2_DIR_INIT;
  825. #endif
  826. #if HAS_Y_DIR
  827. Y_DIR_INIT;
  828. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_DIR
  829. Y2_DIR_INIT;
  830. #endif
  831. #endif
  832. #if HAS_Z_DIR
  833. Z_DIR_INIT;
  834. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_DIR
  835. Z2_DIR_INIT;
  836. #endif
  837. #endif
  838. #if HAS_E0_DIR
  839. E0_DIR_INIT;
  840. #endif
  841. #if HAS_E1_DIR
  842. E1_DIR_INIT;
  843. #endif
  844. #if HAS_E2_DIR
  845. E2_DIR_INIT;
  846. #endif
  847. #if HAS_E3_DIR
  848. E3_DIR_INIT;
  849. #endif
  850. #if HAS_E4_DIR
  851. E4_DIR_INIT;
  852. #endif
  853. // Init Enable Pins - steppers default to disabled.
  854. #if HAS_X_ENABLE
  855. X_ENABLE_INIT;
  856. if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
  857. #if (ENABLED(DUAL_X_CARRIAGE) || ENABLED(X_DUAL_STEPPER_DRIVERS)) && HAS_X2_ENABLE
  858. X2_ENABLE_INIT;
  859. if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
  860. #endif
  861. #endif
  862. #if HAS_Y_ENABLE
  863. Y_ENABLE_INIT;
  864. if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
  865. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_ENABLE
  866. Y2_ENABLE_INIT;
  867. if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
  868. #endif
  869. #endif
  870. #if HAS_Z_ENABLE
  871. Z_ENABLE_INIT;
  872. if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
  873. #if ENABLED(Z_DUAL_STEPPER_DRIVERS) && HAS_Z2_ENABLE
  874. Z2_ENABLE_INIT;
  875. if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
  876. #endif
  877. #endif
  878. #if HAS_E0_ENABLE
  879. E0_ENABLE_INIT;
  880. if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
  881. #endif
  882. #if HAS_E1_ENABLE
  883. E1_ENABLE_INIT;
  884. if (!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
  885. #endif
  886. #if HAS_E2_ENABLE
  887. E2_ENABLE_INIT;
  888. if (!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
  889. #endif
  890. #if HAS_E3_ENABLE
  891. E3_ENABLE_INIT;
  892. if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
  893. #endif
  894. #if HAS_E4_ENABLE
  895. E4_ENABLE_INIT;
  896. if (!E_ENABLE_ON) E4_ENABLE_WRITE(HIGH);
  897. #endif
  898. // Init endstops and pullups
  899. endstops.init();
  900. #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
  901. #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
  902. #define _DISABLE(AXIS) disable_## AXIS()
  903. #define AXIS_INIT(AXIS, PIN) \
  904. _STEP_INIT(AXIS); \
  905. _WRITE_STEP(AXIS, _INVERT_STEP_PIN(PIN)); \
  906. _DISABLE(AXIS)
  907. #define E_AXIS_INIT(NUM) AXIS_INIT(E## NUM, E)
  908. // Init Step Pins
  909. #if HAS_X_STEP
  910. #if ENABLED(X_DUAL_STEPPER_DRIVERS) || ENABLED(DUAL_X_CARRIAGE)
  911. X2_STEP_INIT;
  912. X2_STEP_WRITE(INVERT_X_STEP_PIN);
  913. #endif
  914. AXIS_INIT(X, X);
  915. #endif
  916. #if HAS_Y_STEP
  917. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  918. Y2_STEP_INIT;
  919. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  920. #endif
  921. AXIS_INIT(Y, Y);
  922. #endif
  923. #if HAS_Z_STEP
  924. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  925. Z2_STEP_INIT;
  926. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  927. #endif
  928. AXIS_INIT(Z, Z);
  929. #endif
  930. #if HAS_E0_STEP
  931. E_AXIS_INIT(0);
  932. #endif
  933. #if HAS_E1_STEP
  934. E_AXIS_INIT(1);
  935. #endif
  936. #if HAS_E2_STEP
  937. E_AXIS_INIT(2);
  938. #endif
  939. #if HAS_E3_STEP
  940. E_AXIS_INIT(3);
  941. #endif
  942. #if HAS_E4_STEP
  943. E_AXIS_INIT(4);
  944. #endif
  945. #ifdef __AVR__
  946. // waveform generation = 0100 = CTC
  947. SET_WGM(1, CTC_OCRnA);
  948. // output mode = 00 (disconnected)
  949. SET_COMA(1, NORMAL);
  950. // Set the timer pre-scaler
  951. // Generally we use a divider of 8, resulting in a 2MHz timer
  952. // frequency on a 16MHz MCU. If you are going to change this, be
  953. // sure to regenerate speed_lookuptable.h with
  954. // create_speed_lookuptable.py
  955. SET_CS(1, PRESCALER_8); // CS 2 = 1/8 prescaler
  956. // Init Stepper ISR to 122 Hz for quick starting
  957. OCR1A = 0x4000;
  958. TCNT1 = 0;
  959. #else
  960. // Init Stepper ISR to 122 Hz for quick starting
  961. HAL_timer_start(STEP_TIMER_NUM, 122);
  962. #endif
  963. ENABLE_STEPPER_DRIVER_INTERRUPT();
  964. endstops.enable(true); // Start with endstops active. After homing they can be disabled
  965. sei();
  966. set_directions(); // Init directions to last_direction_bits = 0
  967. }
  968. /**
  969. * Block until all buffered steps are executed / cleaned
  970. */
  971. void Stepper::synchronize() { while (planner.blocks_queued() || cleaning_buffer_counter) idle(); }
  972. /**
  973. * Set the stepper positions directly in steps
  974. *
  975. * The input is based on the typical per-axis XYZ steps.
  976. * For CORE machines XYZ needs to be translated to ABC.
  977. *
  978. * This allows get_axis_position_mm to correctly
  979. * derive the current XYZ position later on.
  980. */
  981. void Stepper::set_position(const long &a, const long &b, const long &c, const long &e) {
  982. synchronize(); // Bad to set stepper counts in the middle of a move
  983. CRITICAL_SECTION_START;
  984. #if CORE_IS_XY
  985. // corexy positioning
  986. // these equations follow the form of the dA and dB equations on http://www.corexy.com/theory.html
  987. count_position[A_AXIS] = a + b;
  988. count_position[B_AXIS] = CORESIGN(a - b);
  989. count_position[Z_AXIS] = c;
  990. #elif CORE_IS_XZ
  991. // corexz planning
  992. count_position[A_AXIS] = a + c;
  993. count_position[Y_AXIS] = b;
  994. count_position[C_AXIS] = CORESIGN(a - c);
  995. #elif CORE_IS_YZ
  996. // coreyz planning
  997. count_position[X_AXIS] = a;
  998. count_position[B_AXIS] = b + c;
  999. count_position[C_AXIS] = CORESIGN(b - c);
  1000. #else
  1001. // default non-h-bot planning
  1002. count_position[X_AXIS] = a;
  1003. count_position[Y_AXIS] = b;
  1004. count_position[Z_AXIS] = c;
  1005. #endif
  1006. count_position[E_AXIS] = e;
  1007. CRITICAL_SECTION_END;
  1008. }
  1009. void Stepper::set_position(const AxisEnum &axis, const long &v) {
  1010. CRITICAL_SECTION_START;
  1011. count_position[axis] = v;
  1012. CRITICAL_SECTION_END;
  1013. }
  1014. void Stepper::set_e_position(const long &e) {
  1015. CRITICAL_SECTION_START;
  1016. count_position[E_AXIS] = e;
  1017. CRITICAL_SECTION_END;
  1018. }
  1019. /**
  1020. * Get a stepper's position in steps.
  1021. */
  1022. long Stepper::position(const AxisEnum axis) {
  1023. CRITICAL_SECTION_START;
  1024. const long count_pos = count_position[axis];
  1025. CRITICAL_SECTION_END;
  1026. return count_pos;
  1027. }
  1028. /**
  1029. * Get an axis position according to stepper position(s)
  1030. * For CORE machines apply translation from ABC to XYZ.
  1031. */
  1032. float Stepper::get_axis_position_mm(const AxisEnum axis) {
  1033. float axis_steps;
  1034. #if IS_CORE
  1035. // Requesting one of the "core" axes?
  1036. if (axis == CORE_AXIS_1 || axis == CORE_AXIS_2) {
  1037. CRITICAL_SECTION_START;
  1038. // ((a1+a2)+(a1-a2))/2 -> (a1+a2+a1-a2)/2 -> (a1+a1)/2 -> a1
  1039. // ((a1+a2)-(a1-a2))/2 -> (a1+a2-a1+a2)/2 -> (a2+a2)/2 -> a2
  1040. axis_steps = 0.5f * (
  1041. axis == CORE_AXIS_2 ? CORESIGN(count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2])
  1042. : count_position[CORE_AXIS_1] + count_position[CORE_AXIS_2]
  1043. );
  1044. CRITICAL_SECTION_END;
  1045. }
  1046. else
  1047. axis_steps = position(axis);
  1048. #else
  1049. axis_steps = position(axis);
  1050. #endif
  1051. return axis_steps * planner.steps_to_mm[axis];
  1052. }
  1053. void Stepper::finish_and_disable() {
  1054. synchronize();
  1055. disable_all_steppers();
  1056. }
  1057. void Stepper::quick_stop() {
  1058. cleaning_buffer_counter = 5000;
  1059. DISABLE_STEPPER_DRIVER_INTERRUPT();
  1060. while (planner.blocks_queued()) planner.discard_current_block();
  1061. current_block = NULL;
  1062. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1063. #if ENABLED(ULTRA_LCD)
  1064. planner.clear_block_buffer_runtime();
  1065. #endif
  1066. }
  1067. void Stepper::endstop_triggered(const AxisEnum axis) {
  1068. #if IS_CORE
  1069. endstops_trigsteps[axis] = 0.5f * (
  1070. axis == CORE_AXIS_2 ? CORESIGN(count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2])
  1071. : count_position[CORE_AXIS_1] + count_position[CORE_AXIS_2]
  1072. );
  1073. #else // !COREXY && !COREXZ && !COREYZ
  1074. endstops_trigsteps[axis] = count_position[axis];
  1075. #endif // !COREXY && !COREXZ && !COREYZ
  1076. kill_current_block();
  1077. cleaning_buffer_counter = -1; // Discard the rest of the move
  1078. }
  1079. void Stepper::report_positions() {
  1080. CRITICAL_SECTION_START;
  1081. const long xpos = count_position[X_AXIS],
  1082. ypos = count_position[Y_AXIS],
  1083. zpos = count_position[Z_AXIS];
  1084. CRITICAL_SECTION_END;
  1085. #if CORE_IS_XY || CORE_IS_XZ || IS_SCARA
  1086. SERIAL_PROTOCOLPGM(MSG_COUNT_A);
  1087. #else
  1088. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1089. #endif
  1090. SERIAL_PROTOCOL(xpos);
  1091. #if CORE_IS_XY || CORE_IS_YZ || IS_SCARA
  1092. SERIAL_PROTOCOLPGM(" B:");
  1093. #else
  1094. SERIAL_PROTOCOLPGM(" Y:");
  1095. #endif
  1096. SERIAL_PROTOCOL(ypos);
  1097. #if CORE_IS_XZ || CORE_IS_YZ
  1098. SERIAL_PROTOCOLPGM(" C:");
  1099. #else
  1100. SERIAL_PROTOCOLPGM(" Z:");
  1101. #endif
  1102. SERIAL_PROTOCOL(zpos);
  1103. SERIAL_EOL();
  1104. }
  1105. #if ENABLED(BABYSTEPPING)
  1106. #if ENABLED(DELTA)
  1107. #define CYCLES_EATEN_BABYSTEP (2 * 15)
  1108. #else
  1109. #define CYCLES_EATEN_BABYSTEP 0
  1110. #endif
  1111. #define EXTRA_CYCLES_BABYSTEP (STEP_PULSE_CYCLES - (CYCLES_EATEN_BABYSTEP))
  1112. #define _ENABLE(AXIS) enable_## AXIS()
  1113. #define _READ_DIR(AXIS) AXIS ##_DIR_READ
  1114. #define _INVERT_DIR(AXIS) INVERT_## AXIS ##_DIR
  1115. #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
  1116. #if EXTRA_CYCLES_BABYSTEP > 20
  1117. #define _SAVE_START const hal_timer_t pulse_start = HAL_timer_get_count(STEP_TIMER_NUM)
  1118. #define _PULSE_WAIT while (EXTRA_CYCLES_BABYSTEP > (uint32_t)(HAL_timer_get_count(STEP_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  1119. #else
  1120. #define _SAVE_START NOOP
  1121. #if EXTRA_CYCLES_BABYSTEP > 0
  1122. #define _PULSE_WAIT DELAY_NOPS(EXTRA_CYCLES_BABYSTEP)
  1123. #elif STEP_PULSE_CYCLES > 0
  1124. #define _PULSE_WAIT NOOP
  1125. #elif ENABLED(DELTA)
  1126. #define _PULSE_WAIT delayMicroseconds(2);
  1127. #else
  1128. #define _PULSE_WAIT delayMicroseconds(4);
  1129. #endif
  1130. #endif
  1131. #define BABYSTEP_AXIS(AXIS, INVERT) { \
  1132. const uint8_t old_dir = _READ_DIR(AXIS); \
  1133. _ENABLE(AXIS); \
  1134. _SAVE_START; \
  1135. _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^direction^INVERT); \
  1136. _PULSE_WAIT; \
  1137. _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), true); \
  1138. _PULSE_WAIT; \
  1139. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), true); \
  1140. _APPLY_DIR(AXIS, old_dir); \
  1141. }
  1142. // MUST ONLY BE CALLED BY AN ISR,
  1143. // No other ISR should ever interrupt this!
  1144. void Stepper::babystep(const AxisEnum axis, const bool direction) {
  1145. cli();
  1146. switch (axis) {
  1147. #if ENABLED(BABYSTEP_XY)
  1148. case X_AXIS:
  1149. BABYSTEP_AXIS(X, false);
  1150. break;
  1151. case Y_AXIS:
  1152. BABYSTEP_AXIS(Y, false);
  1153. break;
  1154. #endif
  1155. case Z_AXIS: {
  1156. #if DISABLED(DELTA)
  1157. BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z);
  1158. #else // DELTA
  1159. const bool z_direction = direction ^ BABYSTEP_INVERT_Z;
  1160. enable_X();
  1161. enable_Y();
  1162. enable_Z();
  1163. const uint8_t old_x_dir_pin = X_DIR_READ,
  1164. old_y_dir_pin = Y_DIR_READ,
  1165. old_z_dir_pin = Z_DIR_READ;
  1166. X_DIR_WRITE(INVERT_X_DIR ^ z_direction);
  1167. Y_DIR_WRITE(INVERT_Y_DIR ^ z_direction);
  1168. Z_DIR_WRITE(INVERT_Z_DIR ^ z_direction);
  1169. _SAVE_START;
  1170. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  1171. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  1172. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  1173. _PULSE_WAIT;
  1174. X_STEP_WRITE(INVERT_X_STEP_PIN);
  1175. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  1176. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  1177. // Restore direction bits
  1178. X_DIR_WRITE(old_x_dir_pin);
  1179. Y_DIR_WRITE(old_y_dir_pin);
  1180. Z_DIR_WRITE(old_z_dir_pin);
  1181. #endif
  1182. } break;
  1183. default: break;
  1184. }
  1185. sei();
  1186. }
  1187. #endif // BABYSTEPPING
  1188. /**
  1189. * Software-controlled Stepper Motor Current
  1190. */
  1191. #if HAS_DIGIPOTSS
  1192. // From Arduino DigitalPotControl example
  1193. void Stepper::digitalPotWrite(const int16_t address, const int16_t value) {
  1194. WRITE(DIGIPOTSS_PIN, LOW); // Take the SS pin low to select the chip
  1195. SPI.transfer(address); // Send the address and value via SPI
  1196. SPI.transfer(value);
  1197. WRITE(DIGIPOTSS_PIN, HIGH); // Take the SS pin high to de-select the chip
  1198. //delay(10);
  1199. }
  1200. #endif // HAS_DIGIPOTSS
  1201. #if HAS_MOTOR_CURRENT_PWM
  1202. void Stepper::refresh_motor_power() {
  1203. for (uint8_t i = 0; i < COUNT(motor_current_setting); ++i) {
  1204. switch (i) {
  1205. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  1206. case 0:
  1207. #endif
  1208. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  1209. case 1:
  1210. #endif
  1211. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  1212. case 2:
  1213. #endif
  1214. digipot_current(i, motor_current_setting[i]);
  1215. default: break;
  1216. }
  1217. }
  1218. }
  1219. #endif // HAS_MOTOR_CURRENT_PWM
  1220. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  1221. void Stepper::digipot_current(const uint8_t driver, const int current) {
  1222. #if HAS_DIGIPOTSS
  1223. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  1224. digitalPotWrite(digipot_ch[driver], current);
  1225. #elif HAS_MOTOR_CURRENT_PWM
  1226. if (WITHIN(driver, 0, 2))
  1227. motor_current_setting[driver] = current; // update motor_current_setting
  1228. #define _WRITE_CURRENT_PWM(P) analogWrite(MOTOR_CURRENT_PWM_## P ##_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE))
  1229. switch (driver) {
  1230. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  1231. case 0: _WRITE_CURRENT_PWM(XY); break;
  1232. #endif
  1233. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  1234. case 1: _WRITE_CURRENT_PWM(Z); break;
  1235. #endif
  1236. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  1237. case 2: _WRITE_CURRENT_PWM(E); break;
  1238. #endif
  1239. }
  1240. #endif
  1241. }
  1242. void Stepper::digipot_init() {
  1243. #if HAS_DIGIPOTSS
  1244. static const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  1245. SPI.begin();
  1246. SET_OUTPUT(DIGIPOTSS_PIN);
  1247. for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++) {
  1248. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  1249. digipot_current(i, digipot_motor_current[i]);
  1250. }
  1251. #elif HAS_MOTOR_CURRENT_PWM
  1252. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  1253. SET_OUTPUT(MOTOR_CURRENT_PWM_XY_PIN);
  1254. #endif
  1255. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  1256. SET_OUTPUT(MOTOR_CURRENT_PWM_Z_PIN);
  1257. #endif
  1258. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  1259. SET_OUTPUT(MOTOR_CURRENT_PWM_E_PIN);
  1260. #endif
  1261. refresh_motor_power();
  1262. // Set Timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  1263. SET_CS5(PRESCALER_1);
  1264. #endif
  1265. }
  1266. #endif
  1267. #if HAS_MICROSTEPS
  1268. /**
  1269. * Software-controlled Microstepping
  1270. */
  1271. void Stepper::microstep_init() {
  1272. SET_OUTPUT(X_MS1_PIN);
  1273. SET_OUTPUT(X_MS2_PIN);
  1274. #if HAS_Y_MICROSTEPS
  1275. SET_OUTPUT(Y_MS1_PIN);
  1276. SET_OUTPUT(Y_MS2_PIN);
  1277. #endif
  1278. #if HAS_Z_MICROSTEPS
  1279. SET_OUTPUT(Z_MS1_PIN);
  1280. SET_OUTPUT(Z_MS2_PIN);
  1281. #endif
  1282. #if HAS_E0_MICROSTEPS
  1283. SET_OUTPUT(E0_MS1_PIN);
  1284. SET_OUTPUT(E0_MS2_PIN);
  1285. #endif
  1286. #if HAS_E1_MICROSTEPS
  1287. SET_OUTPUT(E1_MS1_PIN);
  1288. SET_OUTPUT(E1_MS2_PIN);
  1289. #endif
  1290. #if HAS_E2_MICROSTEPS
  1291. SET_OUTPUT(E2_MS1_PIN);
  1292. SET_OUTPUT(E2_MS2_PIN);
  1293. #endif
  1294. #if HAS_E3_MICROSTEPS
  1295. SET_OUTPUT(E3_MS1_PIN);
  1296. SET_OUTPUT(E3_MS2_PIN);
  1297. #endif
  1298. #if HAS_E4_MICROSTEPS
  1299. SET_OUTPUT(E4_MS1_PIN);
  1300. SET_OUTPUT(E4_MS2_PIN);
  1301. #endif
  1302. static const uint8_t microstep_modes[] = MICROSTEP_MODES;
  1303. for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
  1304. microstep_mode(i, microstep_modes[i]);
  1305. }
  1306. void Stepper::microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2) {
  1307. if (ms1 >= 0) switch (driver) {
  1308. case 0: WRITE(X_MS1_PIN, ms1); break;
  1309. #if HAS_Y_MICROSTEPS
  1310. case 1: WRITE(Y_MS1_PIN, ms1); break;
  1311. #endif
  1312. #if HAS_Z_MICROSTEPS
  1313. case 2: WRITE(Z_MS1_PIN, ms1); break;
  1314. #endif
  1315. #if HAS_E0_MICROSTEPS
  1316. case 3: WRITE(E0_MS1_PIN, ms1); break;
  1317. #endif
  1318. #if HAS_E1_MICROSTEPS
  1319. case 4: WRITE(E1_MS1_PIN, ms1); break;
  1320. #endif
  1321. #if HAS_E2_MICROSTEPS
  1322. case 5: WRITE(E2_MS1_PIN, ms1); break;
  1323. #endif
  1324. #if HAS_E3_MICROSTEPS
  1325. case 6: WRITE(E3_MS1_PIN, ms1); break;
  1326. #endif
  1327. #if HAS_E4_MICROSTEPS
  1328. case 7: WRITE(E4_MS1_PIN, ms1); break;
  1329. #endif
  1330. }
  1331. if (ms2 >= 0) switch (driver) {
  1332. case 0: WRITE(X_MS2_PIN, ms2); break;
  1333. #if HAS_Y_MICROSTEPS
  1334. case 1: WRITE(Y_MS2_PIN, ms2); break;
  1335. #endif
  1336. #if HAS_Z_MICROSTEPS
  1337. case 2: WRITE(Z_MS2_PIN, ms2); break;
  1338. #endif
  1339. #if HAS_E0_MICROSTEPS
  1340. case 3: WRITE(E0_MS2_PIN, ms2); break;
  1341. #endif
  1342. #if HAS_E1_MICROSTEPS
  1343. case 4: WRITE(E1_MS2_PIN, ms2); break;
  1344. #endif
  1345. #if HAS_E2_MICROSTEPS
  1346. case 5: WRITE(E2_MS2_PIN, ms2); break;
  1347. #endif
  1348. #if HAS_E3_MICROSTEPS
  1349. case 6: WRITE(E3_MS2_PIN, ms2); break;
  1350. #endif
  1351. #if HAS_E4_MICROSTEPS
  1352. case 7: WRITE(E4_MS2_PIN, ms2); break;
  1353. #endif
  1354. }
  1355. }
  1356. void Stepper::microstep_mode(const uint8_t driver, const uint8_t stepping_mode) {
  1357. switch (stepping_mode) {
  1358. case 1: microstep_ms(driver, MICROSTEP1); break;
  1359. #if ENABLED(HEROIC_STEPPER_DRIVERS)
  1360. case 128: microstep_ms(driver, MICROSTEP128); break;
  1361. #else
  1362. case 2: microstep_ms(driver, MICROSTEP2); break;
  1363. case 4: microstep_ms(driver, MICROSTEP4); break;
  1364. #endif
  1365. case 8: microstep_ms(driver, MICROSTEP8); break;
  1366. case 16: microstep_ms(driver, MICROSTEP16); break;
  1367. #if MB(ALLIGATOR)
  1368. case 32: microstep_ms(driver, MICROSTEP32); break;
  1369. #endif
  1370. default: SERIAL_ERROR_START(); SERIAL_ERRORLNPGM("Microsteps unavailable"); break;
  1371. }
  1372. }
  1373. void Stepper::microstep_readings() {
  1374. SERIAL_PROTOCOLLNPGM("MS1,MS2 Pins");
  1375. SERIAL_PROTOCOLPGM("X: ");
  1376. SERIAL_PROTOCOL(READ(X_MS1_PIN));
  1377. SERIAL_PROTOCOLLN(READ(X_MS2_PIN));
  1378. #if HAS_Y_MICROSTEPS
  1379. SERIAL_PROTOCOLPGM("Y: ");
  1380. SERIAL_PROTOCOL(READ(Y_MS1_PIN));
  1381. SERIAL_PROTOCOLLN(READ(Y_MS2_PIN));
  1382. #endif
  1383. #if HAS_Z_MICROSTEPS
  1384. SERIAL_PROTOCOLPGM("Z: ");
  1385. SERIAL_PROTOCOL(READ(Z_MS1_PIN));
  1386. SERIAL_PROTOCOLLN(READ(Z_MS2_PIN));
  1387. #endif
  1388. #if HAS_E0_MICROSTEPS
  1389. SERIAL_PROTOCOLPGM("E0: ");
  1390. SERIAL_PROTOCOL(READ(E0_MS1_PIN));
  1391. SERIAL_PROTOCOLLN(READ(E0_MS2_PIN));
  1392. #endif
  1393. #if HAS_E1_MICROSTEPS
  1394. SERIAL_PROTOCOLPGM("E1: ");
  1395. SERIAL_PROTOCOL(READ(E1_MS1_PIN));
  1396. SERIAL_PROTOCOLLN(READ(E1_MS2_PIN));
  1397. #endif
  1398. #if HAS_E2_MICROSTEPS
  1399. SERIAL_PROTOCOLPGM("E2: ");
  1400. SERIAL_PROTOCOL(READ(E2_MS1_PIN));
  1401. SERIAL_PROTOCOLLN(READ(E2_MS2_PIN));
  1402. #endif
  1403. #if HAS_E3_MICROSTEPS
  1404. SERIAL_PROTOCOLPGM("E3: ");
  1405. SERIAL_PROTOCOL(READ(E3_MS1_PIN));
  1406. SERIAL_PROTOCOLLN(READ(E3_MS2_PIN));
  1407. #endif
  1408. #if HAS_E4_MICROSTEPS
  1409. SERIAL_PROTOCOLPGM("E4: ");
  1410. SERIAL_PROTOCOL(READ(E4_MS1_PIN));
  1411. SERIAL_PROTOCOLLN(READ(E4_MS2_PIN));
  1412. #endif
  1413. }
  1414. #endif // HAS_MICROSTEPS