My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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