My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stepper.cpp 49KB

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