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 49KB

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