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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  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. #define _COUNTER(AXIS) counter_## AXIS
  382. #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
  383. #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
  384. // Advance the Bresenham counter; start a pulse if the axis needs a step
  385. #define PULSE_START(AXIS) do{ \
  386. _COUNTER(AXIS) += current_block->steps[_AXIS(AXIS)]; \
  387. if (_COUNTER(AXIS) > 0) _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), 0); }while(0)
  388. // Advance the Bresenham counter; start a pulse if the axis needs a step
  389. #define STEP_TICK(AXIS) \
  390. if (_COUNTER(AXIS) > 0) { \
  391. _COUNTER(AXIS) -= current_block->step_event_count; \
  392. count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; }
  393. // Stop an active pulse, if any
  394. #define PULSE_STOP(AXIS) _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), 0)
  395. /**
  396. * Estimate the number of cycles that the stepper logic already takes
  397. * up between the start and stop of the X stepper pulse.
  398. *
  399. * Currently this uses very modest estimates of around 5 cycles.
  400. * True values may be derived by careful testing.
  401. *
  402. * Once any delay is added, the cost of the delay code itself
  403. * may be subtracted from this value to get a more accurate delay.
  404. * Delays under 20 cycles (1.25µs) will be very accurate, using NOPs.
  405. * Longer delays use a loop. The resolution is 8 cycles.
  406. */
  407. #if HAS_X_STEP
  408. #define _CYCLE_APPROX_1 5
  409. #else
  410. #define _CYCLE_APPROX_1 0
  411. #endif
  412. #if ENABLED(X_DUAL_STEPPER_DRIVERS)
  413. #define _CYCLE_APPROX_2 _CYCLE_APPROX_1 + 4
  414. #else
  415. #define _CYCLE_APPROX_2 _CYCLE_APPROX_1
  416. #endif
  417. #if HAS_Y_STEP
  418. #define _CYCLE_APPROX_3 _CYCLE_APPROX_2 + 5
  419. #else
  420. #define _CYCLE_APPROX_3 _CYCLE_APPROX_2
  421. #endif
  422. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  423. #define _CYCLE_APPROX_4 _CYCLE_APPROX_3 + 4
  424. #else
  425. #define _CYCLE_APPROX_4 _CYCLE_APPROX_3
  426. #endif
  427. #if HAS_Z_STEP
  428. #define _CYCLE_APPROX_5 _CYCLE_APPROX_4 + 5
  429. #else
  430. #define _CYCLE_APPROX_5 _CYCLE_APPROX_4
  431. #endif
  432. #if ENABLED(Z_DUAL_STEPPER_DRIVERS)
  433. #define _CYCLE_APPROX_6 _CYCLE_APPROX_5 + 4
  434. #else
  435. #define _CYCLE_APPROX_6 _CYCLE_APPROX_5
  436. #endif
  437. #if DISABLED(LIN_ADVANCE)
  438. #if ENABLED(MIXING_EXTRUDER)
  439. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6 + (MIXING_STEPPERS) * 6
  440. #else
  441. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6 + 5
  442. #endif
  443. #else
  444. #define _CYCLE_APPROX_7 _CYCLE_APPROX_6
  445. #endif
  446. #define CYCLES_EATEN_XYZE _CYCLE_APPROX_7
  447. #define EXTRA_CYCLES_XYZE (STEP_PULSE_CYCLES - (CYCLES_EATEN_XYZE))
  448. /**
  449. * If a minimum pulse time was specified get the timer 0 value.
  450. *
  451. * On AVR the TCNT0 timer has an 8x prescaler, so it increments every 8 cycles.
  452. * That's every 0.5µs on 16MHz and every 0.4µs on 20MHz.
  453. * 20 counts of TCNT0 -by itself- is a good pulse delay.
  454. * 10µs = 160 or 200 cycles.
  455. */
  456. #if EXTRA_CYCLES_XYZE > 20
  457. hal_timer_t pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  458. #endif
  459. #if HAS_X_STEP
  460. PULSE_START(X);
  461. #endif
  462. #if HAS_Y_STEP
  463. PULSE_START(Y);
  464. #endif
  465. #if HAS_Z_STEP
  466. PULSE_START(Z);
  467. #endif
  468. #if ENABLED(LIN_ADVANCE)
  469. counter_E += current_block->steps[E_AXIS];
  470. if (counter_E > 0) {
  471. #if DISABLED(MIXING_EXTRUDER)
  472. // Don't step E here for mixing extruder
  473. motor_direction(E_AXIS) ? --e_steps : ++e_steps;
  474. #endif
  475. }
  476. #if ENABLED(MIXING_EXTRUDER)
  477. // Step mixing steppers proportionally
  478. const bool dir = motor_direction(E_AXIS);
  479. MIXING_STEPPERS_LOOP(j) {
  480. counter_m[j] += current_block->steps[E_AXIS];
  481. if (counter_m[j] > 0) {
  482. counter_m[j] -= current_block->mix_event_count[j];
  483. dir ? --e_steps[j] : ++e_steps[j];
  484. }
  485. }
  486. #endif
  487. #else // !LIN_ADVANCE - use linear interpolation for E also
  488. #if ENABLED(MIXING_EXTRUDER)
  489. // Keep updating the single E axis
  490. counter_E += current_block->steps[E_AXIS];
  491. // Tick the counters used for this mix
  492. MIXING_STEPPERS_LOOP(j) {
  493. // Step mixing steppers (proportionally)
  494. counter_m[j] += current_block->steps[E_AXIS];
  495. // Step when the counter goes over zero
  496. if (counter_m[j] > 0) En_STEP_WRITE(j, !INVERT_E_STEP_PIN);
  497. }
  498. #else // !MIXING_EXTRUDER
  499. PULSE_START(E);
  500. #endif
  501. #endif // !LIN_ADVANCE
  502. #if HAS_X_STEP
  503. STEP_TICK(X);
  504. #endif
  505. #if HAS_Y_STEP
  506. STEP_TICK(Y);
  507. #endif
  508. #if HAS_Z_STEP
  509. STEP_TICK(Z);
  510. #endif
  511. STEP_TICK(E); // Always tick the single E axis
  512. // For minimum pulse time wait before stopping pulses
  513. #if EXTRA_CYCLES_XYZE > 20
  514. while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  515. pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM);
  516. #elif EXTRA_CYCLES_XYZE > 0
  517. DELAY_NOPS(EXTRA_CYCLES_XYZE);
  518. #endif
  519. #if HAS_X_STEP
  520. PULSE_STOP(X);
  521. #endif
  522. #if HAS_Y_STEP
  523. PULSE_STOP(Y);
  524. #endif
  525. #if HAS_Z_STEP
  526. PULSE_STOP(Z);
  527. #endif
  528. #if DISABLED(LIN_ADVANCE)
  529. #if ENABLED(MIXING_EXTRUDER)
  530. MIXING_STEPPERS_LOOP(j) {
  531. if (counter_m[j] > 0) {
  532. counter_m[j] -= current_block->mix_event_count[j];
  533. En_STEP_WRITE(j, INVERT_E_STEP_PIN);
  534. }
  535. }
  536. #else // !MIXING_EXTRUDER
  537. PULSE_STOP(E);
  538. #endif
  539. #endif // !LIN_ADVANCE
  540. if (++step_events_completed >= current_block->step_event_count) {
  541. all_steps_done = true;
  542. break;
  543. }
  544. // For minimum pulse time wait after stopping pulses also
  545. #if EXTRA_CYCLES_XYZE > 20
  546. if (i) while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  547. #elif EXTRA_CYCLES_XYZE > 0
  548. if (i) DELAY_NOPS(EXTRA_CYCLES_XYZE);
  549. #endif
  550. } // steps_loop
  551. // Calculate new timer value
  552. if (step_events_completed <= (uint32_t)current_block->accelerate_until) {
  553. #ifdef CPU_32_BIT
  554. MultiU32X24toH32(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  555. #else
  556. MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate);
  557. #endif
  558. acc_step_rate += current_block->initial_rate;
  559. // upper limit
  560. NOMORE(acc_step_rate, current_block->nominal_rate);
  561. // step_rate to timer interval
  562. const hal_timer_t interval = calc_timer_interval(acc_step_rate);
  563. SPLIT(interval); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  564. _NEXT_ISR(ocr_val);
  565. acceleration_time += interval;
  566. #if ENABLED(LIN_ADVANCE)
  567. if (current_block->use_advance_lead) {
  568. if (step_events_completed == step_loops || (e_steps && eISR_Rate != current_block->advance_speed)) {
  569. nextAdvanceISR = 0; // Wake up eISR on first acceleration loop and fire ISR if final adv_rate is reached
  570. eISR_Rate = current_block->advance_speed;
  571. }
  572. }
  573. else {
  574. eISR_Rate = ADV_NEVER;
  575. if (e_steps) nextAdvanceISR = 0;
  576. }
  577. #endif // LIN_ADVANCE
  578. }
  579. else if (step_events_completed > (uint32_t)current_block->decelerate_after) {
  580. hal_timer_t step_rate;
  581. #ifdef CPU_32_BIT
  582. MultiU32X24toH32(step_rate, deceleration_time, current_block->acceleration_rate);
  583. #else
  584. MultiU24X32toH16(step_rate, deceleration_time, current_block->acceleration_rate);
  585. #endif
  586. if (step_rate < acc_step_rate) { // Still decelerating?
  587. step_rate = acc_step_rate - step_rate;
  588. NOLESS(step_rate, current_block->final_rate);
  589. }
  590. else
  591. step_rate = current_block->final_rate;
  592. // step_rate to timer interval
  593. const hal_timer_t interval = calc_timer_interval(step_rate);
  594. SPLIT(interval); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  595. _NEXT_ISR(ocr_val);
  596. deceleration_time += interval;
  597. #if ENABLED(LIN_ADVANCE)
  598. if (current_block->use_advance_lead) {
  599. if (step_events_completed <= (uint32_t)current_block->decelerate_after + step_loops || (e_steps && eISR_Rate != current_block->advance_speed)) {
  600. nextAdvanceISR = 0; // Wake up eISR on first deceleration loop
  601. eISR_Rate = current_block->advance_speed;
  602. }
  603. }
  604. else {
  605. eISR_Rate = ADV_NEVER;
  606. if (e_steps) nextAdvanceISR = 0;
  607. }
  608. #endif // LIN_ADVANCE
  609. }
  610. else {
  611. #if ENABLED(LIN_ADVANCE)
  612. // If we have esteps to execute, fire the next advance_isr "now"
  613. if (e_steps && eISR_Rate != current_block->advance_speed) nextAdvanceISR = 0;
  614. #endif
  615. SPLIT(OCR1A_nominal); // split step into multiple ISRs if larger than ENDSTOP_NOMINAL_OCR_VAL
  616. _NEXT_ISR(ocr_val);
  617. // ensure we're running at the correct step rate, even if we just came off an acceleration
  618. step_loops = step_loops_nominal;
  619. }
  620. #if DISABLED(LIN_ADVANCE)
  621. #ifdef CPU_32_BIT
  622. // Make sure stepper interrupt does not monopolise CPU by adjusting count to give about 8 us room
  623. hal_timer_t stepper_timer_count = HAL_timer_get_compare(STEP_TIMER_NUM),
  624. stepper_timer_current_count = HAL_timer_get_count(STEP_TIMER_NUM) + 8 * HAL_TICKS_PER_US;
  625. HAL_timer_set_compare(STEP_TIMER_NUM, max(stepper_timer_count, stepper_timer_current_count));
  626. #else
  627. NOLESS(OCR1A, TCNT1 + 16);
  628. #endif
  629. #endif
  630. // If current block is finished, reset pointer
  631. if (all_steps_done) {
  632. current_block = NULL;
  633. planner.discard_current_block();
  634. }
  635. #if DISABLED(LIN_ADVANCE)
  636. HAL_ENABLE_ISRs(); // re-enable ISRs
  637. #endif
  638. }
  639. #if ENABLED(LIN_ADVANCE)
  640. #define CYCLES_EATEN_E (E_STEPPERS * 5)
  641. #define EXTRA_CYCLES_E (STEP_PULSE_CYCLES - (CYCLES_EATEN_E))
  642. // Timer interrupt for E. e_steps is set in the main routine;
  643. void Stepper::advance_isr() {
  644. #if ENABLED(MK2_MULTIPLEXER) // For SNMM even-numbered steppers are reversed
  645. #define SET_E_STEP_DIR(INDEX) do{ if (e_steps) E## INDEX ##_DIR_WRITE(e_steps < 0 ? !INVERT_E## INDEX ##_DIR ^ TEST(INDEX, 0) : INVERT_E## INDEX ##_DIR ^ TEST(INDEX, 0)); }while(0)
  646. #elif ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  647. #define SET_E_STEP_DIR(INDEX) do{ if (e_steps) { e_steps < 0 ? REV_E_DIR() : NORM_E_DIR(); } }while(0)
  648. #else
  649. #define SET_E_STEP_DIR(INDEX) do{ if (e_steps) E## INDEX ##_DIR_WRITE(e_steps < 0 ? INVERT_E## INDEX ##_DIR : !INVERT_E## INDEX ##_DIR); }while(0)
  650. #endif
  651. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  652. #define START_E_PULSE(INDEX) do{ if (e_steps) E_STEP_WRITE(!INVERT_E_STEP_PIN); }while(0)
  653. #define STOP_E_PULSE(INDEX) do{ if (e_steps) { E_STEP_WRITE(INVERT_E_STEP_PIN); e_steps < 0 ? ++e_steps : --e_steps; } }while(0)
  654. #else
  655. #define START_E_PULSE(INDEX) do{ if (e_steps) E## INDEX ##_STEP_WRITE(!INVERT_E_STEP_PIN); }while(0)
  656. #define STOP_E_PULSE(INDEX) do { if (e_steps) { E## INDEX ##_STEP_WRITE(INVERT_E_STEP_PIN); e_steps < 0 ? ++e_steps : --e_steps; } }while(0)
  657. #endif
  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. if (e_steps) 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. if (e_steps) 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