My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

stepper.cpp 50KB

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