My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

stepper.cpp 50KB

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