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.

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