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

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