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

stepper.cpp 104KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864
  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. /**
  43. * Timer calculations informed by the 'RepRap cartesian firmware' by Zack Smith
  44. * and Philipp Tiefenbacher.
  45. */
  46. /**
  47. * __________________________
  48. * /| |\ _________________ ^
  49. * / | | \ /| |\ |
  50. * / | | \ / | | \ s
  51. * / | | | | | \ p
  52. * / | | | | | \ e
  53. * +-----+------------------------+---+--+---------------+----+ e
  54. * | BLOCK 1 | BLOCK 2 | d
  55. *
  56. * time ----->
  57. *
  58. * The trapezoid is the shape the speed curve over time. It starts at block->initial_rate, accelerates
  59. * first block->accelerate_until step_events_completed, then keeps going at constant speed until
  60. * step_events_completed reaches block->decelerate_after after which it decelerates until the trapezoid generator is reset.
  61. * The slope of acceleration is calculated using v = u + at where t is the accumulated timer values of the steps so far.
  62. */
  63. /**
  64. * Marlin uses the Bresenham algorithm. For a detailed explanation of theory and
  65. * method see https://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html
  66. */
  67. /**
  68. * Jerk controlled movements planner added Apr 2018 by Eduardo José Tagle.
  69. * Equations based on Synthethos TinyG2 sources, but the fixed-point
  70. * implementation is new, as we are running the ISR with a variable period.
  71. * Also implemented the Bézier velocity curve evaluation in ARM assembler,
  72. * to avoid impacting ISR speed.
  73. */
  74. #include "stepper.h"
  75. #ifdef __AVR__
  76. #include "speed_lookuptable.h"
  77. #endif
  78. #include "endstops.h"
  79. #include "planner.h"
  80. #include "motion.h"
  81. #include "../module/temperature.h"
  82. #include "../lcd/ultralcd.h"
  83. #include "../core/language.h"
  84. #include "../gcode/queue.h"
  85. #include "../sd/cardreader.h"
  86. #include "../Marlin.h"
  87. #include "../HAL/shared/Delay.h"
  88. #if MB(ALLIGATOR)
  89. #include "../feature/dac/dac_dac084s085.h"
  90. #endif
  91. #if HAS_DIGIPOTSS
  92. #include <SPI.h>
  93. #endif
  94. #if ENABLED(MIXING_EXTRUDER)
  95. #include "../feature/mixing.h"
  96. #endif
  97. Stepper stepper; // Singleton
  98. #if FILAMENT_RUNOUT_DISTANCE_MM > 0
  99. #include "../feature/runout.h"
  100. #endif
  101. // public:
  102. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || Z_MULTI_ENDSTOPS || ENABLED(Z_STEPPER_AUTO_ALIGN)
  103. bool Stepper::separate_multi_axis = false;
  104. #endif
  105. #if HAS_MOTOR_CURRENT_PWM
  106. uint32_t Stepper::motor_current_setting[3]; // Initialized by settings.load()
  107. #endif
  108. // private:
  109. block_t* Stepper::current_block = NULL; // A pointer to the block currently being traced
  110. uint8_t Stepper::last_direction_bits = 0,
  111. Stepper::axis_did_move;
  112. bool Stepper::abort_current_block;
  113. #if DISABLED(MIXING_EXTRUDER) && EXTRUDERS > 1
  114. uint8_t Stepper::last_moved_extruder = 0xFF;
  115. #endif
  116. #if ENABLED(X_DUAL_ENDSTOPS)
  117. bool Stepper::locked_X_motor = false, Stepper::locked_X2_motor = false;
  118. #endif
  119. #if ENABLED(Y_DUAL_ENDSTOPS)
  120. bool Stepper::locked_Y_motor = false, Stepper::locked_Y2_motor = false;
  121. #endif
  122. #if Z_MULTI_ENDSTOPS || ENABLED(Z_STEPPER_AUTO_ALIGN)
  123. bool Stepper::locked_Z_motor = false, Stepper::locked_Z2_motor = false;
  124. #endif
  125. #if ENABLED(Z_TRIPLE_ENDSTOPS) || (ENABLED(Z_STEPPER_AUTO_ALIGN) && ENABLED(Z_TRIPLE_STEPPER_DRIVERS))
  126. bool Stepper::locked_Z3_motor = false;
  127. #endif
  128. uint32_t Stepper::acceleration_time, Stepper::deceleration_time;
  129. uint8_t Stepper::steps_per_isr;
  130. #if DISABLED(ADAPTIVE_STEP_SMOOTHING)
  131. constexpr
  132. #endif
  133. uint8_t Stepper::oversampling_factor;
  134. int32_t Stepper::delta_error[XYZE] = { 0 };
  135. uint32_t Stepper::advance_dividend[XYZE] = { 0 },
  136. Stepper::advance_divisor = 0,
  137. Stepper::step_events_completed = 0, // The number of step events executed in the current block
  138. Stepper::accelerate_until, // The point from where we need to stop acceleration
  139. Stepper::decelerate_after, // The point from where we need to start decelerating
  140. Stepper::step_event_count; // The total event count for the current block
  141. #if EXTRUDERS > 1 || ENABLED(MIXING_EXTRUDER)
  142. uint8_t Stepper::stepper_extruder;
  143. #else
  144. constexpr uint8_t Stepper::stepper_extruder;
  145. #endif
  146. #if ENABLED(S_CURVE_ACCELERATION)
  147. int32_t __attribute__((used)) Stepper::bezier_A __asm__("bezier_A"); // A coefficient in Bézier speed curve with alias for assembler
  148. int32_t __attribute__((used)) Stepper::bezier_B __asm__("bezier_B"); // B coefficient in Bézier speed curve with alias for assembler
  149. int32_t __attribute__((used)) Stepper::bezier_C __asm__("bezier_C"); // C coefficient in Bézier speed curve with alias for assembler
  150. uint32_t __attribute__((used)) Stepper::bezier_F __asm__("bezier_F"); // F coefficient in Bézier speed curve with alias for assembler
  151. uint32_t __attribute__((used)) Stepper::bezier_AV __asm__("bezier_AV"); // AV coefficient in Bézier speed curve with alias for assembler
  152. #ifdef __AVR__
  153. bool __attribute__((used)) Stepper::A_negative __asm__("A_negative"); // If A coefficient was negative
  154. #endif
  155. bool Stepper::bezier_2nd_half; // =false If Bézier curve has been initialized or not
  156. #endif
  157. uint32_t Stepper::nextMainISR = 0;
  158. #if ENABLED(LIN_ADVANCE)
  159. constexpr uint32_t LA_ADV_NEVER = 0xFFFFFFFF;
  160. uint32_t Stepper::nextAdvanceISR = LA_ADV_NEVER,
  161. Stepper::LA_isr_rate = LA_ADV_NEVER;
  162. uint16_t Stepper::LA_current_adv_steps = 0,
  163. Stepper::LA_final_adv_steps,
  164. Stepper::LA_max_adv_steps;
  165. int8_t Stepper::LA_steps = 0;
  166. bool Stepper::LA_use_advance_lead;
  167. #endif // LIN_ADVANCE
  168. int32_t Stepper::ticks_nominal = -1;
  169. #if DISABLED(S_CURVE_ACCELERATION)
  170. uint32_t Stepper::acc_step_rate; // needed for deceleration start point
  171. #endif
  172. volatile int32_t Stepper::endstops_trigsteps[XYZ];
  173. volatile int32_t Stepper::count_position[NUM_AXIS] = { 0 };
  174. int8_t Stepper::count_direction[NUM_AXIS] = { 0, 0, 0, 0 };
  175. #define DUAL_ENDSTOP_APPLY_STEP(A,V) \
  176. if (separate_multi_axis) { \
  177. if (A##_HOME_DIR < 0) { \
  178. if (!(TEST(endstops.state(), A##_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##_motor) A##_STEP_WRITE(V); \
  179. if (!(TEST(endstops.state(), A##2_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \
  180. } \
  181. else { \
  182. if (!(TEST(endstops.state(), A##_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##_motor) A##_STEP_WRITE(V); \
  183. if (!(TEST(endstops.state(), A##2_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \
  184. } \
  185. } \
  186. else { \
  187. A##_STEP_WRITE(V); \
  188. A##2_STEP_WRITE(V); \
  189. }
  190. #define DUAL_SEPARATE_APPLY_STEP(A,V) \
  191. if (separate_multi_axis) { \
  192. if (!locked_##A##_motor) A##_STEP_WRITE(V); \
  193. if (!locked_##A##2_motor) A##2_STEP_WRITE(V); \
  194. } \
  195. else { \
  196. A##_STEP_WRITE(V); \
  197. A##2_STEP_WRITE(V); \
  198. }
  199. #define TRIPLE_ENDSTOP_APPLY_STEP(A,V) \
  200. if (separate_multi_axis) { \
  201. if (A##_HOME_DIR < 0) { \
  202. if (!(TEST(endstops.state(), A##_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##_motor) A##_STEP_WRITE(V); \
  203. if (!(TEST(endstops.state(), A##2_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \
  204. if (!(TEST(endstops.state(), A##3_MIN) && count_direction[_AXIS(A)] < 0) && !locked_##A##3_motor) A##3_STEP_WRITE(V); \
  205. } \
  206. else { \
  207. if (!(TEST(endstops.state(), A##_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##_motor) A##_STEP_WRITE(V); \
  208. if (!(TEST(endstops.state(), A##2_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##2_motor) A##2_STEP_WRITE(V); \
  209. if (!(TEST(endstops.state(), A##3_MAX) && count_direction[_AXIS(A)] > 0) && !locked_##A##3_motor) A##3_STEP_WRITE(V); \
  210. } \
  211. } \
  212. else { \
  213. A##_STEP_WRITE(V); \
  214. A##2_STEP_WRITE(V); \
  215. A##3_STEP_WRITE(V); \
  216. }
  217. #if ENABLED(X_DUAL_STEPPER_DRIVERS)
  218. #define X_APPLY_DIR(v,Q) do{ X_DIR_WRITE(v); X2_DIR_WRITE((v) != INVERT_X2_VS_X_DIR); }while(0)
  219. #if ENABLED(X_DUAL_ENDSTOPS)
  220. #define X_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(X,v)
  221. #else
  222. #define X_APPLY_STEP(v,Q) do{ X_STEP_WRITE(v); X2_STEP_WRITE(v); }while(0)
  223. #endif
  224. #elif ENABLED(DUAL_X_CARRIAGE)
  225. #define X_APPLY_DIR(v,ALWAYS) \
  226. if (extruder_duplication_enabled || ALWAYS) { \
  227. X_DIR_WRITE(v); \
  228. X2_DIR_WRITE(v); \
  229. } \
  230. else { \
  231. if (movement_extruder()) X2_DIR_WRITE(v); else X_DIR_WRITE(v); \
  232. }
  233. #define X_APPLY_STEP(v,ALWAYS) \
  234. if (extruder_duplication_enabled || ALWAYS) { \
  235. X_STEP_WRITE(v); \
  236. X2_STEP_WRITE(v); \
  237. } \
  238. else { \
  239. if (movement_extruder()) X2_STEP_WRITE(v); else X_STEP_WRITE(v); \
  240. }
  241. #else
  242. #define X_APPLY_DIR(v,Q) X_DIR_WRITE(v)
  243. #define X_APPLY_STEP(v,Q) X_STEP_WRITE(v)
  244. #endif
  245. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  246. #define Y_APPLY_DIR(v,Q) do{ Y_DIR_WRITE(v); Y2_DIR_WRITE((v) != INVERT_Y2_VS_Y_DIR); }while(0)
  247. #if ENABLED(Y_DUAL_ENDSTOPS)
  248. #define Y_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(Y,v)
  249. #else
  250. #define Y_APPLY_STEP(v,Q) do{ Y_STEP_WRITE(v); Y2_STEP_WRITE(v); }while(0)
  251. #endif
  252. #else
  253. #define Y_APPLY_DIR(v,Q) Y_DIR_WRITE(v)
  254. #define Y_APPLY_STEP(v,Q) Y_STEP_WRITE(v)
  255. #endif
  256. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  257. #define Z_APPLY_DIR(v,Q) do{ Z_DIR_WRITE(v); Z2_DIR_WRITE(v); Z3_DIR_WRITE(v); }while(0)
  258. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  259. #define Z_APPLY_STEP(v,Q) TRIPLE_ENDSTOP_APPLY_STEP(Z,v)
  260. #else
  261. #define Z_APPLY_STEP(v,Q) do{ Z_STEP_WRITE(v); Z2_STEP_WRITE(v); Z3_STEP_WRITE(v); }while(0)
  262. #endif
  263. #elif ENABLED(Z_DUAL_STEPPER_DRIVERS)
  264. #define Z_APPLY_DIR(v,Q) do{ Z_DIR_WRITE(v); Z2_DIR_WRITE(v); }while(0)
  265. #if ENABLED(Z_DUAL_ENDSTOPS)
  266. #define Z_APPLY_STEP(v,Q) DUAL_ENDSTOP_APPLY_STEP(Z,v)
  267. #elif ENABLED(Z_STEPPER_AUTO_ALIGN)
  268. #define Z_APPLY_STEP(v,Q) DUAL_SEPARATE_APPLY_STEP(Z,v)
  269. #else
  270. #define Z_APPLY_STEP(v,Q) do{ Z_STEP_WRITE(v); Z2_STEP_WRITE(v); }while(0)
  271. #endif
  272. #else
  273. #define Z_APPLY_DIR(v,Q) Z_DIR_WRITE(v)
  274. #define Z_APPLY_STEP(v,Q) Z_STEP_WRITE(v)
  275. #endif
  276. #if DISABLED(MIXING_EXTRUDER)
  277. #define E_APPLY_STEP(v,Q) E_STEP_WRITE(stepper_extruder, v)
  278. #endif
  279. void Stepper::wake_up() {
  280. // TCNT1 = 0;
  281. ENABLE_STEPPER_DRIVER_INTERRUPT();
  282. }
  283. /**
  284. * Set the stepper direction of each axis
  285. *
  286. * COREXY: X_AXIS=A_AXIS and Y_AXIS=B_AXIS
  287. * COREXZ: X_AXIS=A_AXIS and Z_AXIS=C_AXIS
  288. * COREYZ: Y_AXIS=B_AXIS and Z_AXIS=C_AXIS
  289. */
  290. void Stepper::set_directions() {
  291. #define SET_STEP_DIR(A) \
  292. if (motor_direction(_AXIS(A))) { \
  293. A##_APPLY_DIR(INVERT_## A##_DIR, false); \
  294. count_direction[_AXIS(A)] = -1; \
  295. } \
  296. else { \
  297. A##_APPLY_DIR(!INVERT_## A##_DIR, false); \
  298. count_direction[_AXIS(A)] = 1; \
  299. }
  300. #if HAS_X_DIR
  301. SET_STEP_DIR(X); // A
  302. #endif
  303. #if HAS_Y_DIR
  304. SET_STEP_DIR(Y); // B
  305. #endif
  306. #if HAS_Z_DIR
  307. SET_STEP_DIR(Z); // C
  308. #endif
  309. #if DISABLED(LIN_ADVANCE)
  310. #if ENABLED(MIXING_EXTRUDER)
  311. // Because this is valid for the whole block we don't know
  312. // what e-steppers will step. Likely all. Set all.
  313. if (motor_direction(E_AXIS)) {
  314. MIXER_STEPPER_LOOP(j) REV_E_DIR(j);
  315. count_direction[E_AXIS] = -1;
  316. }
  317. else {
  318. MIXER_STEPPER_LOOP(j) NORM_E_DIR(j);
  319. count_direction[E_AXIS] = 1;
  320. }
  321. #else
  322. if (motor_direction(E_AXIS)) {
  323. REV_E_DIR(stepper_extruder);
  324. count_direction[E_AXIS] = -1;
  325. }
  326. else {
  327. NORM_E_DIR(stepper_extruder);
  328. count_direction[E_AXIS] = 1;
  329. }
  330. #endif
  331. #endif // !LIN_ADVANCE
  332. // A small delay may be needed after changing direction
  333. #if MINIMUM_STEPPER_DIR_DELAY > 0
  334. DELAY_NS(MINIMUM_STEPPER_DIR_DELAY);
  335. #endif
  336. }
  337. #if ENABLED(S_CURVE_ACCELERATION)
  338. /**
  339. * This uses a quintic (fifth-degree) Bézier polynomial for the velocity curve, giving
  340. * a "linear pop" velocity curve; with pop being the sixth derivative of position:
  341. * velocity - 1st, acceleration - 2nd, jerk - 3rd, snap - 4th, crackle - 5th, pop - 6th
  342. *
  343. * The Bézier curve takes the form:
  344. *
  345. * V(t) = P_0 * B_0(t) + P_1 * B_1(t) + P_2 * B_2(t) + P_3 * B_3(t) + P_4 * B_4(t) + P_5 * B_5(t)
  346. *
  347. * Where 0 <= t <= 1, and V(t) is the velocity. P_0 through P_5 are the control points, and B_0(t)
  348. * through B_5(t) are the Bernstein basis as follows:
  349. *
  350. * B_0(t) = (1-t)^5 = -t^5 + 5t^4 - 10t^3 + 10t^2 - 5t + 1
  351. * B_1(t) = 5(1-t)^4 * t = 5t^5 - 20t^4 + 30t^3 - 20t^2 + 5t
  352. * B_2(t) = 10(1-t)^3 * t^2 = -10t^5 + 30t^4 - 30t^3 + 10t^2
  353. * B_3(t) = 10(1-t)^2 * t^3 = 10t^5 - 20t^4 + 10t^3
  354. * B_4(t) = 5(1-t) * t^4 = -5t^5 + 5t^4
  355. * B_5(t) = t^5 = t^5
  356. * ^ ^ ^ ^ ^ ^
  357. * | | | | | |
  358. * A B C D E F
  359. *
  360. * Unfortunately, we cannot use forward-differencing to calculate each position through
  361. * the curve, as Marlin uses variable timer periods. So, we require a formula of the form:
  362. *
  363. * V_f(t) = A*t^5 + B*t^4 + C*t^3 + D*t^2 + E*t + F
  364. *
  365. * Looking at the above B_0(t) through B_5(t) expanded forms, if we take the coefficients of t^5
  366. * through t of the Bézier form of V(t), we can determine that:
  367. *
  368. * A = -P_0 + 5*P_1 - 10*P_2 + 10*P_3 - 5*P_4 + P_5
  369. * B = 5*P_0 - 20*P_1 + 30*P_2 - 20*P_3 + 5*P_4
  370. * C = -10*P_0 + 30*P_1 - 30*P_2 + 10*P_3
  371. * D = 10*P_0 - 20*P_1 + 10*P_2
  372. * E = - 5*P_0 + 5*P_1
  373. * F = P_0
  374. *
  375. * Now, since we will (currently) *always* want the initial acceleration and jerk values to be 0,
  376. * We set P_i = P_0 = P_1 = P_2 (initial velocity), and P_t = P_3 = P_4 = P_5 (target velocity),
  377. * which, after simplification, resolves to:
  378. *
  379. * A = - 6*P_i + 6*P_t = 6*(P_t - P_i)
  380. * B = 15*P_i - 15*P_t = 15*(P_i - P_t)
  381. * C = -10*P_i + 10*P_t = 10*(P_t - P_i)
  382. * D = 0
  383. * E = 0
  384. * F = P_i
  385. *
  386. * As the t is evaluated in non uniform steps here, there is no other way rather than evaluating
  387. * the Bézier curve at each point:
  388. *
  389. * V_f(t) = A*t^5 + B*t^4 + C*t^3 + F [0 <= t <= 1]
  390. *
  391. * Floating point arithmetic execution time cost is prohibitive, so we will transform the math to
  392. * use fixed point values to be able to evaluate it in realtime. Assuming a maximum of 250000 steps
  393. * per second (driver pulses should at least be 2µS hi/2µS lo), and allocating 2 bits to avoid
  394. * overflows on the evaluation of the Bézier curve, means we can use
  395. *
  396. * t: unsigned Q0.32 (0 <= t < 1) |range 0 to 0xFFFFFFFF unsigned
  397. * A: signed Q24.7 , |range = +/- 250000 * 6 * 128 = +/- 192000000 = 0x0B71B000 | 28 bits + sign
  398. * B: signed Q24.7 , |range = +/- 250000 *15 * 128 = +/- 480000000 = 0x1C9C3800 | 29 bits + sign
  399. * C: signed Q24.7 , |range = +/- 250000 *10 * 128 = +/- 320000000 = 0x1312D000 | 29 bits + sign
  400. * F: signed Q24.7 , |range = +/- 250000 * 128 = 32000000 = 0x01E84800 | 25 bits + sign
  401. *
  402. * The trapezoid generator state contains the following information, that we will use to create and evaluate
  403. * the Bézier curve:
  404. *
  405. * blk->step_event_count [TS] = The total count of steps for this movement. (=distance)
  406. * blk->initial_rate [VI] = The initial steps per second (=velocity)
  407. * blk->final_rate [VF] = The ending steps per second (=velocity)
  408. * and the count of events completed (step_events_completed) [CS] (=distance until now)
  409. *
  410. * Note the abbreviations we use in the following formulae are between []s
  411. *
  412. * For Any 32bit CPU:
  413. *
  414. * At the start of each trapezoid, calculate the coefficients A,B,C,F and Advance [AV], as follows:
  415. *
  416. * A = 6*128*(VF - VI) = 768*(VF - VI)
  417. * B = 15*128*(VI - VF) = 1920*(VI - VF)
  418. * C = 10*128*(VF - VI) = 1280*(VF - VI)
  419. * F = 128*VI = 128*VI
  420. * AV = (1<<32)/TS ~= 0xFFFFFFFF / TS (To use ARM UDIV, that is 32 bits) (this is computed at the planner, to offload expensive calculations from the ISR)
  421. *
  422. * And for each point, evaluate the curve with the following sequence:
  423. *
  424. * void lsrs(uint32_t& d, uint32_t s, int cnt) {
  425. * d = s >> cnt;
  426. * }
  427. * void lsls(uint32_t& d, uint32_t s, int cnt) {
  428. * d = s << cnt;
  429. * }
  430. * void lsrs(int32_t& d, uint32_t s, int cnt) {
  431. * d = uint32_t(s) >> cnt;
  432. * }
  433. * void lsls(int32_t& d, uint32_t s, int cnt) {
  434. * d = uint32_t(s) << cnt;
  435. * }
  436. * void umull(uint32_t& rlo, uint32_t& rhi, uint32_t op1, uint32_t op2) {
  437. * uint64_t res = uint64_t(op1) * op2;
  438. * rlo = uint32_t(res & 0xFFFFFFFF);
  439. * rhi = uint32_t((res >> 32) & 0xFFFFFFFF);
  440. * }
  441. * void smlal(int32_t& rlo, int32_t& rhi, int32_t op1, int32_t op2) {
  442. * int64_t mul = int64_t(op1) * op2;
  443. * int64_t s = int64_t(uint32_t(rlo) | ((uint64_t(uint32_t(rhi)) << 32U)));
  444. * mul += s;
  445. * rlo = int32_t(mul & 0xFFFFFFFF);
  446. * rhi = int32_t((mul >> 32) & 0xFFFFFFFF);
  447. * }
  448. * int32_t _eval_bezier_curve_arm(uint32_t curr_step) {
  449. * uint32_t flo = 0;
  450. * uint32_t fhi = bezier_AV * curr_step;
  451. * uint32_t t = fhi;
  452. * int32_t alo = bezier_F;
  453. * int32_t ahi = 0;
  454. * int32_t A = bezier_A;
  455. * int32_t B = bezier_B;
  456. * int32_t C = bezier_C;
  457. *
  458. * lsrs(ahi, alo, 1); // a = F << 31
  459. * lsls(alo, alo, 31); //
  460. * umull(flo, fhi, fhi, t); // f *= t
  461. * umull(flo, fhi, fhi, t); // f>>=32; f*=t
  462. * lsrs(flo, fhi, 1); //
  463. * smlal(alo, ahi, flo, C); // a+=(f>>33)*C
  464. * umull(flo, fhi, fhi, t); // f>>=32; f*=t
  465. * lsrs(flo, fhi, 1); //
  466. * smlal(alo, ahi, flo, B); // a+=(f>>33)*B
  467. * umull(flo, fhi, fhi, t); // f>>=32; f*=t
  468. * lsrs(flo, fhi, 1); // f>>=33;
  469. * smlal(alo, ahi, flo, A); // a+=(f>>33)*A;
  470. * lsrs(alo, ahi, 6); // a>>=38
  471. *
  472. * return alo;
  473. * }
  474. *
  475. * This is rewritten in ARM assembly for optimal performance (43 cycles to execute).
  476. *
  477. * For AVR, the precision of coefficients is scaled so the Bézier curve can be evaluated in real-time:
  478. * Let's reduce precision as much as possible. After some experimentation we found that:
  479. *
  480. * Assume t and AV with 24 bits is enough
  481. * A = 6*(VF - VI)
  482. * B = 15*(VI - VF)
  483. * C = 10*(VF - VI)
  484. * F = VI
  485. * AV = (1<<24)/TS (this is computed at the planner, to offload expensive calculations from the ISR)
  486. *
  487. * Instead of storing sign for each coefficient, we will store its absolute value,
  488. * and flag the sign of the A coefficient, so we can save to store the sign bit.
  489. * It always holds that sign(A) = - sign(B) = sign(C)
  490. *
  491. * So, the resulting range of the coefficients are:
  492. *
  493. * t: unsigned (0 <= t < 1) |range 0 to 0xFFFFFF unsigned
  494. * A: signed Q24 , range = 250000 * 6 = 1500000 = 0x16E360 | 21 bits
  495. * B: signed Q24 , range = 250000 *15 = 3750000 = 0x393870 | 22 bits
  496. * C: signed Q24 , range = 250000 *10 = 2500000 = 0x1312D0 | 21 bits
  497. * F: signed Q24 , range = 250000 = 250000 = 0x0ED090 | 20 bits
  498. *
  499. * And for each curve, estimate its coefficients with:
  500. *
  501. * void _calc_bezier_curve_coeffs(int32_t v0, int32_t v1, uint32_t av) {
  502. * // Calculate the Bézier coefficients
  503. * if (v1 < v0) {
  504. * A_negative = true;
  505. * bezier_A = 6 * (v0 - v1);
  506. * bezier_B = 15 * (v0 - v1);
  507. * bezier_C = 10 * (v0 - v1);
  508. * }
  509. * else {
  510. * A_negative = false;
  511. * bezier_A = 6 * (v1 - v0);
  512. * bezier_B = 15 * (v1 - v0);
  513. * bezier_C = 10 * (v1 - v0);
  514. * }
  515. * bezier_F = v0;
  516. * }
  517. *
  518. * And for each point, evaluate the curve with the following sequence:
  519. *
  520. * // unsigned multiplication of 24 bits x 24bits, return upper 16 bits
  521. * void umul24x24to16hi(uint16_t& r, uint24_t op1, uint24_t op2) {
  522. * r = (uint64_t(op1) * op2) >> 8;
  523. * }
  524. * // unsigned multiplication of 16 bits x 16bits, return upper 16 bits
  525. * void umul16x16to16hi(uint16_t& r, uint16_t op1, uint16_t op2) {
  526. * r = (uint32_t(op1) * op2) >> 16;
  527. * }
  528. * // unsigned multiplication of 16 bits x 24bits, return upper 24 bits
  529. * void umul16x24to24hi(uint24_t& r, uint16_t op1, uint24_t op2) {
  530. * r = uint24_t((uint64_t(op1) * op2) >> 16);
  531. * }
  532. *
  533. * int32_t _eval_bezier_curve(uint32_t curr_step) {
  534. * // To save computing, the first step is always the initial speed
  535. * if (!curr_step)
  536. * return bezier_F;
  537. *
  538. * uint16_t t;
  539. * umul24x24to16hi(t, bezier_AV, curr_step); // t: Range 0 - 1^16 = 16 bits
  540. * uint16_t f = t;
  541. * umul16x16to16hi(f, f, t); // Range 16 bits (unsigned)
  542. * umul16x16to16hi(f, f, t); // Range 16 bits : f = t^3 (unsigned)
  543. * uint24_t acc = bezier_F; // Range 20 bits (unsigned)
  544. * if (A_negative) {
  545. * uint24_t v;
  546. * umul16x24to24hi(v, f, bezier_C); // Range 21bits
  547. * acc -= v;
  548. * umul16x16to16hi(f, f, t); // Range 16 bits : f = t^4 (unsigned)
  549. * umul16x24to24hi(v, f, bezier_B); // Range 22bits
  550. * acc += v;
  551. * umul16x16to16hi(f, f, t); // Range 16 bits : f = t^5 (unsigned)
  552. * umul16x24to24hi(v, f, bezier_A); // Range 21bits + 15 = 36bits (plus sign)
  553. * acc -= v;
  554. * }
  555. * else {
  556. * uint24_t v;
  557. * umul16x24to24hi(v, f, bezier_C); // Range 21bits
  558. * acc += v;
  559. * umul16x16to16hi(f, f, t); // Range 16 bits : f = t^4 (unsigned)
  560. * umul16x24to24hi(v, f, bezier_B); // Range 22bits
  561. * acc -= v;
  562. * umul16x16to16hi(f, f, t); // Range 16 bits : f = t^5 (unsigned)
  563. * umul16x24to24hi(v, f, bezier_A); // Range 21bits + 15 = 36bits (plus sign)
  564. * acc += v;
  565. * }
  566. * return acc;
  567. * }
  568. * These functions are translated to assembler for optimal performance.
  569. * Coefficient calculation takes 70 cycles. Bezier point evaluation takes 150 cycles.
  570. */
  571. #ifdef __AVR__
  572. // For AVR we use assembly to maximize speed
  573. void Stepper::_calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av) {
  574. // Store advance
  575. bezier_AV = av;
  576. // Calculate the rest of the coefficients
  577. uint8_t r2 = v0 & 0xFF;
  578. uint8_t r3 = (v0 >> 8) & 0xFF;
  579. uint8_t r12 = (v0 >> 16) & 0xFF;
  580. uint8_t r5 = v1 & 0xFF;
  581. uint8_t r6 = (v1 >> 8) & 0xFF;
  582. uint8_t r7 = (v1 >> 16) & 0xFF;
  583. uint8_t r4,r8,r9,r10,r11;
  584. __asm__ __volatile__(
  585. /* Calculate the Bézier coefficients */
  586. /* %10:%1:%0 = v0*/
  587. /* %5:%4:%3 = v1*/
  588. /* %7:%6:%10 = temporary*/
  589. /* %9 = val (must be high register!)*/
  590. /* %10 (must be high register!)*/
  591. /* Store initial velocity*/
  592. A("sts bezier_F, %0")
  593. A("sts bezier_F+1, %1")
  594. A("sts bezier_F+2, %10") /* bezier_F = %10:%1:%0 = v0 */
  595. /* Get delta speed */
  596. A("ldi %2,-1") /* %2 = 0xFF, means A_negative = true */
  597. A("clr %8") /* %8 = 0 */
  598. A("sub %0,%3")
  599. A("sbc %1,%4")
  600. A("sbc %10,%5") /* v0 -= v1, C=1 if result is negative */
  601. A("brcc 1f") /* branch if result is positive (C=0), that means v0 >= v1 */
  602. /* Result was negative, get the absolute value*/
  603. A("com %10")
  604. A("com %1")
  605. A("neg %0")
  606. A("sbc %1,%2")
  607. A("sbc %10,%2") /* %10:%1:%0 +1 -> %10:%1:%0 = -(v0 - v1) = (v1 - v0) */
  608. A("clr %2") /* %2 = 0, means A_negative = false */
  609. /* Store negative flag*/
  610. L("1")
  611. A("sts A_negative, %2") /* Store negative flag */
  612. /* Compute coefficients A,B and C [20 cycles worst case]*/
  613. A("ldi %9,6") /* %9 = 6 */
  614. A("mul %0,%9") /* r1:r0 = 6*LO(v0-v1) */
  615. A("sts bezier_A, r0")
  616. A("mov %6,r1")
  617. A("clr %7") /* %7:%6:r0 = 6*LO(v0-v1) */
  618. A("mul %1,%9") /* r1:r0 = 6*MI(v0-v1) */
  619. A("add %6,r0")
  620. A("adc %7,r1") /* %7:%6:?? += 6*MI(v0-v1) << 8 */
  621. A("mul %10,%9") /* r1:r0 = 6*HI(v0-v1) */
  622. A("add %7,r0") /* %7:%6:?? += 6*HI(v0-v1) << 16 */
  623. A("sts bezier_A+1, %6")
  624. A("sts bezier_A+2, %7") /* bezier_A = %7:%6:?? = 6*(v0-v1) [35 cycles worst] */
  625. A("ldi %9,15") /* %9 = 15 */
  626. A("mul %0,%9") /* r1:r0 = 5*LO(v0-v1) */
  627. A("sts bezier_B, r0")
  628. A("mov %6,r1")
  629. A("clr %7") /* %7:%6:?? = 5*LO(v0-v1) */
  630. A("mul %1,%9") /* r1:r0 = 5*MI(v0-v1) */
  631. A("add %6,r0")
  632. A("adc %7,r1") /* %7:%6:?? += 5*MI(v0-v1) << 8 */
  633. A("mul %10,%9") /* r1:r0 = 5*HI(v0-v1) */
  634. A("add %7,r0") /* %7:%6:?? += 5*HI(v0-v1) << 16 */
  635. A("sts bezier_B+1, %6")
  636. A("sts bezier_B+2, %7") /* bezier_B = %7:%6:?? = 5*(v0-v1) [50 cycles worst] */
  637. A("ldi %9,10") /* %9 = 10 */
  638. A("mul %0,%9") /* r1:r0 = 10*LO(v0-v1) */
  639. A("sts bezier_C, r0")
  640. A("mov %6,r1")
  641. A("clr %7") /* %7:%6:?? = 10*LO(v0-v1) */
  642. A("mul %1,%9") /* r1:r0 = 10*MI(v0-v1) */
  643. A("add %6,r0")
  644. A("adc %7,r1") /* %7:%6:?? += 10*MI(v0-v1) << 8 */
  645. A("mul %10,%9") /* r1:r0 = 10*HI(v0-v1) */
  646. A("add %7,r0") /* %7:%6:?? += 10*HI(v0-v1) << 16 */
  647. A("sts bezier_C+1, %6")
  648. " sts bezier_C+2, %7" /* bezier_C = %7:%6:?? = 10*(v0-v1) [65 cycles worst] */
  649. : "+r" (r2),
  650. "+d" (r3),
  651. "=r" (r4),
  652. "+r" (r5),
  653. "+r" (r6),
  654. "+r" (r7),
  655. "=r" (r8),
  656. "=r" (r9),
  657. "=r" (r10),
  658. "=d" (r11),
  659. "+r" (r12)
  660. :
  661. : "r0", "r1", "cc", "memory"
  662. );
  663. }
  664. FORCE_INLINE int32_t Stepper::_eval_bezier_curve(const uint32_t curr_step) {
  665. // If dealing with the first step, save expensive computing and return the initial speed
  666. if (!curr_step)
  667. return bezier_F;
  668. uint8_t r0 = 0; /* Zero register */
  669. uint8_t r2 = (curr_step) & 0xFF;
  670. uint8_t r3 = (curr_step >> 8) & 0xFF;
  671. uint8_t r4 = (curr_step >> 16) & 0xFF;
  672. uint8_t r1,r5,r6,r7,r8,r9,r10,r11; /* Temporary registers */
  673. __asm__ __volatile(
  674. /* umul24x24to16hi(t, bezier_AV, curr_step); t: Range 0 - 1^16 = 16 bits*/
  675. A("lds %9,bezier_AV") /* %9 = LO(AV)*/
  676. A("mul %9,%2") /* r1:r0 = LO(bezier_AV)*LO(curr_step)*/
  677. A("mov %7,r1") /* %7 = LO(bezier_AV)*LO(curr_step) >> 8*/
  678. A("clr %8") /* %8:%7 = LO(bezier_AV)*LO(curr_step) >> 8*/
  679. A("lds %10,bezier_AV+1") /* %10 = MI(AV)*/
  680. A("mul %10,%2") /* r1:r0 = MI(bezier_AV)*LO(curr_step)*/
  681. A("add %7,r0")
  682. A("adc %8,r1") /* %8:%7 += MI(bezier_AV)*LO(curr_step)*/
  683. A("lds r1,bezier_AV+2") /* r11 = HI(AV)*/
  684. A("mul r1,%2") /* r1:r0 = HI(bezier_AV)*LO(curr_step)*/
  685. A("add %8,r0") /* %8:%7 += HI(bezier_AV)*LO(curr_step) << 8*/
  686. A("mul %9,%3") /* r1:r0 = LO(bezier_AV)*MI(curr_step)*/
  687. A("add %7,r0")
  688. A("adc %8,r1") /* %8:%7 += LO(bezier_AV)*MI(curr_step)*/
  689. A("mul %10,%3") /* r1:r0 = MI(bezier_AV)*MI(curr_step)*/
  690. A("add %8,r0") /* %8:%7 += LO(bezier_AV)*MI(curr_step) << 8*/
  691. A("mul %9,%4") /* r1:r0 = LO(bezier_AV)*HI(curr_step)*/
  692. A("add %8,r0") /* %8:%7 += LO(bezier_AV)*HI(curr_step) << 8*/
  693. /* %8:%7 = t*/
  694. /* uint16_t f = t;*/
  695. A("mov %5,%7") /* %6:%5 = f*/
  696. A("mov %6,%8")
  697. /* %6:%5 = f*/
  698. /* umul16x16to16hi(f, f, t); / Range 16 bits (unsigned) [17] */
  699. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  700. A("mov %9,r1") /* store MIL(LO(f) * LO(t)) in %9, we need it for rounding*/
  701. A("clr %10") /* %10 = 0*/
  702. A("clr %11") /* %11 = 0*/
  703. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  704. A("add %9,r0") /* %9 += LO(LO(f) * HI(t))*/
  705. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  706. A("adc %11,%0") /* %11 += carry*/
  707. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  708. A("add %9,r0") /* %9 += LO(HI(f) * LO(t))*/
  709. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t)) */
  710. A("adc %11,%0") /* %11 += carry*/
  711. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  712. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  713. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  714. A("mov %5,%10") /* %6:%5 = */
  715. A("mov %6,%11") /* f = %10:%11*/
  716. /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3 (unsigned) [17]*/
  717. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  718. A("mov %1,r1") /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
  719. A("clr %10") /* %10 = 0*/
  720. A("clr %11") /* %11 = 0*/
  721. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  722. A("add %1,r0") /* %1 += LO(LO(f) * HI(t))*/
  723. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  724. A("adc %11,%0") /* %11 += carry*/
  725. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  726. A("add %1,r0") /* %1 += LO(HI(f) * LO(t))*/
  727. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t))*/
  728. A("adc %11,%0") /* %11 += carry*/
  729. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  730. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  731. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  732. A("mov %5,%10") /* %6:%5 =*/
  733. A("mov %6,%11") /* f = %10:%11*/
  734. /* [15 +17*2] = [49]*/
  735. /* %4:%3:%2 will be acc from now on*/
  736. /* uint24_t acc = bezier_F; / Range 20 bits (unsigned)*/
  737. A("clr %9") /* "decimal place we get for free"*/
  738. A("lds %2,bezier_F")
  739. A("lds %3,bezier_F+1")
  740. A("lds %4,bezier_F+2") /* %4:%3:%2 = acc*/
  741. /* if (A_negative) {*/
  742. A("lds r0,A_negative")
  743. A("or r0,%0") /* Is flag signalling negative? */
  744. A("brne 3f") /* If yes, Skip next instruction if A was negative*/
  745. A("rjmp 1f") /* Otherwise, jump */
  746. /* uint24_t v; */
  747. /* umul16x24to24hi(v, f, bezier_C); / Range 21bits [29] */
  748. /* acc -= v; */
  749. L("3")
  750. A("lds %10, bezier_C") /* %10 = LO(bezier_C)*/
  751. A("mul %10,%5") /* r1:r0 = LO(bezier_C) * LO(f)*/
  752. A("sub %9,r1")
  753. A("sbc %2,%0")
  754. A("sbc %3,%0")
  755. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(LO(bezier_C) * LO(f))*/
  756. A("lds %11, bezier_C+1") /* %11 = MI(bezier_C)*/
  757. A("mul %11,%5") /* r1:r0 = MI(bezier_C) * LO(f)*/
  758. A("sub %9,r0")
  759. A("sbc %2,r1")
  760. A("sbc %3,%0")
  761. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_C) * LO(f)*/
  762. A("lds %1, bezier_C+2") /* %1 = HI(bezier_C)*/
  763. A("mul %1,%5") /* r1:r0 = MI(bezier_C) * LO(f)*/
  764. A("sub %2,r0")
  765. A("sbc %3,r1")
  766. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(bezier_C) * LO(f) << 8*/
  767. A("mul %10,%6") /* r1:r0 = LO(bezier_C) * MI(f)*/
  768. A("sub %9,r0")
  769. A("sbc %2,r1")
  770. A("sbc %3,%0")
  771. A("sbc %4,%0") /* %4:%3:%2:%9 -= LO(bezier_C) * MI(f)*/
  772. A("mul %11,%6") /* r1:r0 = MI(bezier_C) * MI(f)*/
  773. A("sub %2,r0")
  774. A("sbc %3,r1")
  775. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_C) * MI(f) << 8*/
  776. A("mul %1,%6") /* r1:r0 = HI(bezier_C) * LO(f)*/
  777. A("sub %3,r0")
  778. A("sbc %4,r1") /* %4:%3:%2:%9 -= HI(bezier_C) * LO(f) << 16*/
  779. /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3 (unsigned) [17]*/
  780. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  781. A("mov %1,r1") /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
  782. A("clr %10") /* %10 = 0*/
  783. A("clr %11") /* %11 = 0*/
  784. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  785. A("add %1,r0") /* %1 += LO(LO(f) * HI(t))*/
  786. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  787. A("adc %11,%0") /* %11 += carry*/
  788. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  789. A("add %1,r0") /* %1 += LO(HI(f) * LO(t))*/
  790. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t))*/
  791. A("adc %11,%0") /* %11 += carry*/
  792. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  793. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  794. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  795. A("mov %5,%10") /* %6:%5 =*/
  796. A("mov %6,%11") /* f = %10:%11*/
  797. /* umul16x24to24hi(v, f, bezier_B); / Range 22bits [29]*/
  798. /* acc += v; */
  799. A("lds %10, bezier_B") /* %10 = LO(bezier_B)*/
  800. A("mul %10,%5") /* r1:r0 = LO(bezier_B) * LO(f)*/
  801. A("add %9,r1")
  802. A("adc %2,%0")
  803. A("adc %3,%0")
  804. A("adc %4,%0") /* %4:%3:%2:%9 += HI(LO(bezier_B) * LO(f))*/
  805. A("lds %11, bezier_B+1") /* %11 = MI(bezier_B)*/
  806. A("mul %11,%5") /* r1:r0 = MI(bezier_B) * LO(f)*/
  807. A("add %9,r0")
  808. A("adc %2,r1")
  809. A("adc %3,%0")
  810. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_B) * LO(f)*/
  811. A("lds %1, bezier_B+2") /* %1 = HI(bezier_B)*/
  812. A("mul %1,%5") /* r1:r0 = MI(bezier_B) * LO(f)*/
  813. A("add %2,r0")
  814. A("adc %3,r1")
  815. A("adc %4,%0") /* %4:%3:%2:%9 += HI(bezier_B) * LO(f) << 8*/
  816. A("mul %10,%6") /* r1:r0 = LO(bezier_B) * MI(f)*/
  817. A("add %9,r0")
  818. A("adc %2,r1")
  819. A("adc %3,%0")
  820. A("adc %4,%0") /* %4:%3:%2:%9 += LO(bezier_B) * MI(f)*/
  821. A("mul %11,%6") /* r1:r0 = MI(bezier_B) * MI(f)*/
  822. A("add %2,r0")
  823. A("adc %3,r1")
  824. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_B) * MI(f) << 8*/
  825. A("mul %1,%6") /* r1:r0 = HI(bezier_B) * LO(f)*/
  826. A("add %3,r0")
  827. A("adc %4,r1") /* %4:%3:%2:%9 += HI(bezier_B) * LO(f) << 16*/
  828. /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^5 (unsigned) [17]*/
  829. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  830. A("mov %1,r1") /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
  831. A("clr %10") /* %10 = 0*/
  832. A("clr %11") /* %11 = 0*/
  833. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  834. A("add %1,r0") /* %1 += LO(LO(f) * HI(t))*/
  835. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  836. A("adc %11,%0") /* %11 += carry*/
  837. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  838. A("add %1,r0") /* %1 += LO(HI(f) * LO(t))*/
  839. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t))*/
  840. A("adc %11,%0") /* %11 += carry*/
  841. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  842. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  843. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  844. A("mov %5,%10") /* %6:%5 =*/
  845. A("mov %6,%11") /* f = %10:%11*/
  846. /* umul16x24to24hi(v, f, bezier_A); / Range 21bits [29]*/
  847. /* acc -= v; */
  848. A("lds %10, bezier_A") /* %10 = LO(bezier_A)*/
  849. A("mul %10,%5") /* r1:r0 = LO(bezier_A) * LO(f)*/
  850. A("sub %9,r1")
  851. A("sbc %2,%0")
  852. A("sbc %3,%0")
  853. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(LO(bezier_A) * LO(f))*/
  854. A("lds %11, bezier_A+1") /* %11 = MI(bezier_A)*/
  855. A("mul %11,%5") /* r1:r0 = MI(bezier_A) * LO(f)*/
  856. A("sub %9,r0")
  857. A("sbc %2,r1")
  858. A("sbc %3,%0")
  859. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_A) * LO(f)*/
  860. A("lds %1, bezier_A+2") /* %1 = HI(bezier_A)*/
  861. A("mul %1,%5") /* r1:r0 = MI(bezier_A) * LO(f)*/
  862. A("sub %2,r0")
  863. A("sbc %3,r1")
  864. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(bezier_A) * LO(f) << 8*/
  865. A("mul %10,%6") /* r1:r0 = LO(bezier_A) * MI(f)*/
  866. A("sub %9,r0")
  867. A("sbc %2,r1")
  868. A("sbc %3,%0")
  869. A("sbc %4,%0") /* %4:%3:%2:%9 -= LO(bezier_A) * MI(f)*/
  870. A("mul %11,%6") /* r1:r0 = MI(bezier_A) * MI(f)*/
  871. A("sub %2,r0")
  872. A("sbc %3,r1")
  873. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_A) * MI(f) << 8*/
  874. A("mul %1,%6") /* r1:r0 = HI(bezier_A) * LO(f)*/
  875. A("sub %3,r0")
  876. A("sbc %4,r1") /* %4:%3:%2:%9 -= HI(bezier_A) * LO(f) << 16*/
  877. A("jmp 2f") /* Done!*/
  878. L("1")
  879. /* uint24_t v; */
  880. /* umul16x24to24hi(v, f, bezier_C); / Range 21bits [29]*/
  881. /* acc += v; */
  882. A("lds %10, bezier_C") /* %10 = LO(bezier_C)*/
  883. A("mul %10,%5") /* r1:r0 = LO(bezier_C) * LO(f)*/
  884. A("add %9,r1")
  885. A("adc %2,%0")
  886. A("adc %3,%0")
  887. A("adc %4,%0") /* %4:%3:%2:%9 += HI(LO(bezier_C) * LO(f))*/
  888. A("lds %11, bezier_C+1") /* %11 = MI(bezier_C)*/
  889. A("mul %11,%5") /* r1:r0 = MI(bezier_C) * LO(f)*/
  890. A("add %9,r0")
  891. A("adc %2,r1")
  892. A("adc %3,%0")
  893. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_C) * LO(f)*/
  894. A("lds %1, bezier_C+2") /* %1 = HI(bezier_C)*/
  895. A("mul %1,%5") /* r1:r0 = MI(bezier_C) * LO(f)*/
  896. A("add %2,r0")
  897. A("adc %3,r1")
  898. A("adc %4,%0") /* %4:%3:%2:%9 += HI(bezier_C) * LO(f) << 8*/
  899. A("mul %10,%6") /* r1:r0 = LO(bezier_C) * MI(f)*/
  900. A("add %9,r0")
  901. A("adc %2,r1")
  902. A("adc %3,%0")
  903. A("adc %4,%0") /* %4:%3:%2:%9 += LO(bezier_C) * MI(f)*/
  904. A("mul %11,%6") /* r1:r0 = MI(bezier_C) * MI(f)*/
  905. A("add %2,r0")
  906. A("adc %3,r1")
  907. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_C) * MI(f) << 8*/
  908. A("mul %1,%6") /* r1:r0 = HI(bezier_C) * LO(f)*/
  909. A("add %3,r0")
  910. A("adc %4,r1") /* %4:%3:%2:%9 += HI(bezier_C) * LO(f) << 16*/
  911. /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^3 (unsigned) [17]*/
  912. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  913. A("mov %1,r1") /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
  914. A("clr %10") /* %10 = 0*/
  915. A("clr %11") /* %11 = 0*/
  916. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  917. A("add %1,r0") /* %1 += LO(LO(f) * HI(t))*/
  918. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  919. A("adc %11,%0") /* %11 += carry*/
  920. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  921. A("add %1,r0") /* %1 += LO(HI(f) * LO(t))*/
  922. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t))*/
  923. A("adc %11,%0") /* %11 += carry*/
  924. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  925. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  926. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  927. A("mov %5,%10") /* %6:%5 =*/
  928. A("mov %6,%11") /* f = %10:%11*/
  929. /* umul16x24to24hi(v, f, bezier_B); / Range 22bits [29]*/
  930. /* acc -= v;*/
  931. A("lds %10, bezier_B") /* %10 = LO(bezier_B)*/
  932. A("mul %10,%5") /* r1:r0 = LO(bezier_B) * LO(f)*/
  933. A("sub %9,r1")
  934. A("sbc %2,%0")
  935. A("sbc %3,%0")
  936. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(LO(bezier_B) * LO(f))*/
  937. A("lds %11, bezier_B+1") /* %11 = MI(bezier_B)*/
  938. A("mul %11,%5") /* r1:r0 = MI(bezier_B) * LO(f)*/
  939. A("sub %9,r0")
  940. A("sbc %2,r1")
  941. A("sbc %3,%0")
  942. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_B) * LO(f)*/
  943. A("lds %1, bezier_B+2") /* %1 = HI(bezier_B)*/
  944. A("mul %1,%5") /* r1:r0 = MI(bezier_B) * LO(f)*/
  945. A("sub %2,r0")
  946. A("sbc %3,r1")
  947. A("sbc %4,%0") /* %4:%3:%2:%9 -= HI(bezier_B) * LO(f) << 8*/
  948. A("mul %10,%6") /* r1:r0 = LO(bezier_B) * MI(f)*/
  949. A("sub %9,r0")
  950. A("sbc %2,r1")
  951. A("sbc %3,%0")
  952. A("sbc %4,%0") /* %4:%3:%2:%9 -= LO(bezier_B) * MI(f)*/
  953. A("mul %11,%6") /* r1:r0 = MI(bezier_B) * MI(f)*/
  954. A("sub %2,r0")
  955. A("sbc %3,r1")
  956. A("sbc %4,%0") /* %4:%3:%2:%9 -= MI(bezier_B) * MI(f) << 8*/
  957. A("mul %1,%6") /* r1:r0 = HI(bezier_B) * LO(f)*/
  958. A("sub %3,r0")
  959. A("sbc %4,r1") /* %4:%3:%2:%9 -= HI(bezier_B) * LO(f) << 16*/
  960. /* umul16x16to16hi(f, f, t); / Range 16 bits : f = t^5 (unsigned) [17]*/
  961. A("mul %5,%7") /* r1:r0 = LO(f) * LO(t)*/
  962. A("mov %1,r1") /* store MIL(LO(f) * LO(t)) in %1, we need it for rounding*/
  963. A("clr %10") /* %10 = 0*/
  964. A("clr %11") /* %11 = 0*/
  965. A("mul %5,%8") /* r1:r0 = LO(f) * HI(t)*/
  966. A("add %1,r0") /* %1 += LO(LO(f) * HI(t))*/
  967. A("adc %10,r1") /* %10 = HI(LO(f) * HI(t))*/
  968. A("adc %11,%0") /* %11 += carry*/
  969. A("mul %6,%7") /* r1:r0 = HI(f) * LO(t)*/
  970. A("add %1,r0") /* %1 += LO(HI(f) * LO(t))*/
  971. A("adc %10,r1") /* %10 += HI(HI(f) * LO(t))*/
  972. A("adc %11,%0") /* %11 += carry*/
  973. A("mul %6,%8") /* r1:r0 = HI(f) * HI(t)*/
  974. A("add %10,r0") /* %10 += LO(HI(f) * HI(t))*/
  975. A("adc %11,r1") /* %11 += HI(HI(f) * HI(t))*/
  976. A("mov %5,%10") /* %6:%5 =*/
  977. A("mov %6,%11") /* f = %10:%11*/
  978. /* umul16x24to24hi(v, f, bezier_A); / Range 21bits [29]*/
  979. /* acc += v; */
  980. A("lds %10, bezier_A") /* %10 = LO(bezier_A)*/
  981. A("mul %10,%5") /* r1:r0 = LO(bezier_A) * LO(f)*/
  982. A("add %9,r1")
  983. A("adc %2,%0")
  984. A("adc %3,%0")
  985. A("adc %4,%0") /* %4:%3:%2:%9 += HI(LO(bezier_A) * LO(f))*/
  986. A("lds %11, bezier_A+1") /* %11 = MI(bezier_A)*/
  987. A("mul %11,%5") /* r1:r0 = MI(bezier_A) * LO(f)*/
  988. A("add %9,r0")
  989. A("adc %2,r1")
  990. A("adc %3,%0")
  991. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_A) * LO(f)*/
  992. A("lds %1, bezier_A+2") /* %1 = HI(bezier_A)*/
  993. A("mul %1,%5") /* r1:r0 = MI(bezier_A) * LO(f)*/
  994. A("add %2,r0")
  995. A("adc %3,r1")
  996. A("adc %4,%0") /* %4:%3:%2:%9 += HI(bezier_A) * LO(f) << 8*/
  997. A("mul %10,%6") /* r1:r0 = LO(bezier_A) * MI(f)*/
  998. A("add %9,r0")
  999. A("adc %2,r1")
  1000. A("adc %3,%0")
  1001. A("adc %4,%0") /* %4:%3:%2:%9 += LO(bezier_A) * MI(f)*/
  1002. A("mul %11,%6") /* r1:r0 = MI(bezier_A) * MI(f)*/
  1003. A("add %2,r0")
  1004. A("adc %3,r1")
  1005. A("adc %4,%0") /* %4:%3:%2:%9 += MI(bezier_A) * MI(f) << 8*/
  1006. A("mul %1,%6") /* r1:r0 = HI(bezier_A) * LO(f)*/
  1007. A("add %3,r0")
  1008. A("adc %4,r1") /* %4:%3:%2:%9 += HI(bezier_A) * LO(f) << 16*/
  1009. L("2")
  1010. " clr __zero_reg__" /* C runtime expects r1 = __zero_reg__ = 0 */
  1011. : "+r"(r0),
  1012. "+r"(r1),
  1013. "+r"(r2),
  1014. "+r"(r3),
  1015. "+r"(r4),
  1016. "+r"(r5),
  1017. "+r"(r6),
  1018. "+r"(r7),
  1019. "+r"(r8),
  1020. "+r"(r9),
  1021. "+r"(r10),
  1022. "+r"(r11)
  1023. :
  1024. :"cc","r0","r1"
  1025. );
  1026. return (r2 | (uint16_t(r3) << 8)) | (uint32_t(r4) << 16);
  1027. }
  1028. #else
  1029. // For all the other 32bit CPUs
  1030. FORCE_INLINE void Stepper::_calc_bezier_curve_coeffs(const int32_t v0, const int32_t v1, const uint32_t av) {
  1031. // Calculate the Bézier coefficients
  1032. bezier_A = 768 * (v1 - v0);
  1033. bezier_B = 1920 * (v0 - v1);
  1034. bezier_C = 1280 * (v1 - v0);
  1035. bezier_F = 128 * v0;
  1036. bezier_AV = av;
  1037. }
  1038. FORCE_INLINE int32_t Stepper::_eval_bezier_curve(const uint32_t curr_step) {
  1039. #if defined(__ARM__) || defined(__thumb__)
  1040. // For ARM Cortex M3/M4 CPUs, we have the optimized assembler version, that takes 43 cycles to execute
  1041. uint32_t flo = 0;
  1042. uint32_t fhi = bezier_AV * curr_step;
  1043. uint32_t t = fhi;
  1044. int32_t alo = bezier_F;
  1045. int32_t ahi = 0;
  1046. int32_t A = bezier_A;
  1047. int32_t B = bezier_B;
  1048. int32_t C = bezier_C;
  1049. __asm__ __volatile__(
  1050. ".syntax unified" "\n\t" // is to prevent CM0,CM1 non-unified syntax
  1051. A("lsrs %[ahi],%[alo],#1") // a = F << 31 1 cycles
  1052. A("lsls %[alo],%[alo],#31") // 1 cycles
  1053. A("umull %[flo],%[fhi],%[fhi],%[t]") // f *= t 5 cycles [fhi:flo=64bits]
  1054. A("umull %[flo],%[fhi],%[fhi],%[t]") // f>>=32; f*=t 5 cycles [fhi:flo=64bits]
  1055. A("lsrs %[flo],%[fhi],#1") // 1 cycles [31bits]
  1056. A("smlal %[alo],%[ahi],%[flo],%[C]") // a+=(f>>33)*C; 5 cycles
  1057. A("umull %[flo],%[fhi],%[fhi],%[t]") // f>>=32; f*=t 5 cycles [fhi:flo=64bits]
  1058. A("lsrs %[flo],%[fhi],#1") // 1 cycles [31bits]
  1059. A("smlal %[alo],%[ahi],%[flo],%[B]") // a+=(f>>33)*B; 5 cycles
  1060. A("umull %[flo],%[fhi],%[fhi],%[t]") // f>>=32; f*=t 5 cycles [fhi:flo=64bits]
  1061. A("lsrs %[flo],%[fhi],#1") // f>>=33; 1 cycles [31bits]
  1062. A("smlal %[alo],%[ahi],%[flo],%[A]") // a+=(f>>33)*A; 5 cycles
  1063. A("lsrs %[alo],%[ahi],#6") // a>>=38 1 cycles
  1064. : [alo]"+r"( alo ) ,
  1065. [flo]"+r"( flo ) ,
  1066. [fhi]"+r"( fhi ) ,
  1067. [ahi]"+r"( ahi ) ,
  1068. [A]"+r"( A ) , // <== Note: Even if A, B, C, and t registers are INPUT ONLY
  1069. [B]"+r"( B ) , // GCC does bad optimizations on the code if we list them as
  1070. [C]"+r"( C ) , // such, breaking this function. So, to avoid that problem,
  1071. [t]"+r"( t ) // we list all registers as input-outputs.
  1072. :
  1073. : "cc"
  1074. );
  1075. return alo;
  1076. #else
  1077. // For non ARM targets, we provide a fallback implementation. Really doubt it
  1078. // will be useful, unless the processor is fast and 32bit
  1079. uint32_t t = bezier_AV * curr_step; // t: Range 0 - 1^32 = 32 bits
  1080. uint64_t f = t;
  1081. f *= t; // Range 32*2 = 64 bits (unsigned)
  1082. f >>= 32; // Range 32 bits (unsigned)
  1083. f *= t; // Range 32*2 = 64 bits (unsigned)
  1084. f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  1085. int64_t acc = (int64_t) bezier_F << 31; // Range 63 bits (signed)
  1086. acc += ((uint32_t) f >> 1) * (int64_t) bezier_C; // Range 29bits + 31 = 60bits (plus sign)
  1087. f *= t; // Range 32*2 = 64 bits
  1088. f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  1089. acc += ((uint32_t) f >> 1) * (int64_t) bezier_B; // Range 29bits + 31 = 60bits (plus sign)
  1090. f *= t; // Range 32*2 = 64 bits
  1091. f >>= 32; // Range 32 bits : f = t^3 (unsigned)
  1092. acc += ((uint32_t) f >> 1) * (int64_t) bezier_A; // Range 28bits + 31 = 59bits (plus sign)
  1093. acc >>= (31 + 7); // Range 24bits (plus sign)
  1094. return (int32_t) acc;
  1095. #endif
  1096. }
  1097. #endif
  1098. #endif // S_CURVE_ACCELERATION
  1099. /**
  1100. * Stepper Driver Interrupt
  1101. *
  1102. * Directly pulses the stepper motors at high frequency.
  1103. */
  1104. HAL_STEP_TIMER_ISR {
  1105. HAL_timer_isr_prologue(STEP_TIMER_NUM);
  1106. Stepper::isr();
  1107. HAL_timer_isr_epilogue(STEP_TIMER_NUM);
  1108. }
  1109. #ifdef CPU_32_BIT
  1110. #define STEP_MULTIPLY(A,B) MultiU32X24toH32(A, B)
  1111. #else
  1112. #define STEP_MULTIPLY(A,B) MultiU24X32toH16(A, B)
  1113. #endif
  1114. void Stepper::isr() {
  1115. #ifndef __AVR__
  1116. // Disable interrupts, to avoid ISR preemption while we reprogram the period
  1117. // (AVR enters the ISR with global interrupts disabled, so no need to do it here)
  1118. DISABLE_ISRS();
  1119. #endif
  1120. // Program timer compare for the maximum period, so it does NOT
  1121. // flag an interrupt while this ISR is running - So changes from small
  1122. // periods to big periods are respected and the timer does not reset to 0
  1123. HAL_timer_set_compare(STEP_TIMER_NUM, HAL_TIMER_TYPE_MAX);
  1124. // Count of ticks for the next ISR
  1125. hal_timer_t next_isr_ticks = 0;
  1126. // Limit the amount of iterations
  1127. uint8_t max_loops = 10;
  1128. // We need this variable here to be able to use it in the following loop
  1129. hal_timer_t min_ticks;
  1130. do {
  1131. // Enable ISRs to reduce USART processing latency
  1132. ENABLE_ISRS();
  1133. // Run main stepping pulse phase ISR if we have to
  1134. if (!nextMainISR) Stepper::stepper_pulse_phase_isr();
  1135. #if ENABLED(LIN_ADVANCE)
  1136. // Run linear advance stepper ISR if we have to
  1137. if (!nextAdvanceISR) nextAdvanceISR = Stepper::advance_isr();
  1138. #endif
  1139. // ^== Time critical. NOTHING besides pulse generation should be above here!!!
  1140. // Run main stepping block processing ISR if we have to
  1141. if (!nextMainISR) nextMainISR = Stepper::stepper_block_phase_isr();
  1142. uint32_t interval =
  1143. #if ENABLED(LIN_ADVANCE)
  1144. MIN(nextAdvanceISR, nextMainISR) // Nearest time interval
  1145. #else
  1146. nextMainISR // Remaining stepper ISR time
  1147. #endif
  1148. ;
  1149. // Limit the value to the maximum possible value of the timer
  1150. NOMORE(interval, HAL_TIMER_TYPE_MAX);
  1151. // Compute the time remaining for the main isr
  1152. nextMainISR -= interval;
  1153. #if ENABLED(LIN_ADVANCE)
  1154. // Compute the time remaining for the advance isr
  1155. if (nextAdvanceISR != LA_ADV_NEVER) nextAdvanceISR -= interval;
  1156. #endif
  1157. /**
  1158. * This needs to avoid a race-condition caused by interleaving
  1159. * of interrupts required by both the LA and Stepper algorithms.
  1160. *
  1161. * Assume the following tick times for stepper pulses:
  1162. * Stepper ISR (S): 1 1000 2000 3000 4000
  1163. * Linear Adv. (E): 10 1010 2010 3010 4010
  1164. *
  1165. * The current algorithm tries to interleave them, giving:
  1166. * 1:S 10:E 1000:S 1010:E 2000:S 2010:E 3000:S 3010:E 4000:S 4010:E
  1167. *
  1168. * Ideal timing would yield these delta periods:
  1169. * 1:S 9:E 990:S 10:E 990:S 10:E 990:S 10:E 990:S 10:E
  1170. *
  1171. * But, since each event must fire an ISR with a minimum duration, the
  1172. * minimum delta might be 900, so deltas under 900 get rounded up:
  1173. * 900:S d900:E d990:S d900:E d990:S d900:E d990:S d900:E d990:S d900:E
  1174. *
  1175. * It works, but divides the speed of all motors by half, leading to a sudden
  1176. * reduction to 1/2 speed! Such jumps in speed lead to lost steps (not even
  1177. * accounting for double/quad stepping, which makes it even worse).
  1178. */
  1179. // Compute the tick count for the next ISR
  1180. next_isr_ticks += interval;
  1181. /**
  1182. * The following section must be done with global interrupts disabled.
  1183. * We want nothing to interrupt it, as that could mess the calculations
  1184. * we do for the next value to program in the period register of the
  1185. * stepper timer and lead to skipped ISRs (if the value we happen to program
  1186. * is less than the current count due to something preempting between the
  1187. * read and the write of the new period value).
  1188. */
  1189. DISABLE_ISRS();
  1190. /**
  1191. * Get the current tick value + margin
  1192. * Assuming at least 6µs between calls to this ISR...
  1193. * On AVR the ISR epilogue+prologue is estimated at 100 instructions - Give 8µs as margin
  1194. * On ARM the ISR epilogue+prologue is estimated at 20 instructions - Give 1µs as margin
  1195. */
  1196. min_ticks = HAL_timer_get_count(STEP_TIMER_NUM) + hal_timer_t(
  1197. #ifdef __AVR__
  1198. 8
  1199. #else
  1200. 1
  1201. #endif
  1202. * (STEPPER_TIMER_TICKS_PER_US)
  1203. );
  1204. /**
  1205. * NB: If for some reason the stepper monopolizes the MPU, eventually the
  1206. * timer will wrap around (and so will 'next_isr_ticks'). So, limit the
  1207. * loop to 10 iterations. Beyond that, there's no way to ensure correct pulse
  1208. * timing, since the MCU isn't fast enough.
  1209. */
  1210. if (!--max_loops) next_isr_ticks = min_ticks;
  1211. // Advance pulses if not enough time to wait for the next ISR
  1212. } while (next_isr_ticks < min_ticks);
  1213. // Now 'next_isr_ticks' contains the period to the next Stepper ISR - And we are
  1214. // sure that the time has not arrived yet - Warrantied by the scheduler
  1215. // Set the next ISR to fire at the proper time
  1216. HAL_timer_set_compare(STEP_TIMER_NUM, hal_timer_t(next_isr_ticks));
  1217. // Don't forget to finally reenable interrupts
  1218. ENABLE_ISRS();
  1219. }
  1220. /**
  1221. * This phase of the ISR should ONLY create the pulses for the steppers.
  1222. * This prevents jitter caused by the interval between the start of the
  1223. * interrupt and the start of the pulses. DON'T add any logic ahead of the
  1224. * call to this method that might cause variation in the timing. The aim
  1225. * is to keep pulse timing as regular as possible.
  1226. */
  1227. void Stepper::stepper_pulse_phase_isr() {
  1228. // If we must abort the current block, do so!
  1229. if (abort_current_block) {
  1230. abort_current_block = false;
  1231. if (current_block) {
  1232. axis_did_move = 0;
  1233. current_block = NULL;
  1234. planner.discard_current_block();
  1235. }
  1236. }
  1237. // If there is no current block, do nothing
  1238. if (!current_block) return;
  1239. // Count of pending loops and events for this iteration
  1240. const uint32_t pending_events = step_event_count - step_events_completed;
  1241. uint8_t events_to_do = MIN(pending_events, steps_per_isr);
  1242. // Just update the value we will get at the end of the loop
  1243. step_events_completed += events_to_do;
  1244. // Get the timer count and estimate the end of the pulse
  1245. hal_timer_t pulse_end = HAL_timer_get_count(PULSE_TIMER_NUM) + hal_timer_t(MIN_PULSE_TICKS);
  1246. const hal_timer_t added_step_ticks = hal_timer_t(ADDED_STEP_TICKS);
  1247. // Take multiple steps per interrupt (For high speed moves)
  1248. do {
  1249. #define _APPLY_STEP(AXIS) AXIS ##_APPLY_STEP
  1250. #define _INVERT_STEP_PIN(AXIS) INVERT_## AXIS ##_STEP_PIN
  1251. // Start an active pulse, if Bresenham says so, and update position
  1252. #define PULSE_START(AXIS) do{ \
  1253. delta_error[_AXIS(AXIS)] += advance_dividend[_AXIS(AXIS)]; \
  1254. if (delta_error[_AXIS(AXIS)] >= 0) { \
  1255. _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), 0); \
  1256. count_position[_AXIS(AXIS)] += count_direction[_AXIS(AXIS)]; \
  1257. } \
  1258. }while(0)
  1259. // Stop an active pulse, if any, and adjust error term
  1260. #define PULSE_STOP(AXIS) do { \
  1261. if (delta_error[_AXIS(AXIS)] >= 0) { \
  1262. delta_error[_AXIS(AXIS)] -= advance_divisor; \
  1263. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), 0); \
  1264. } \
  1265. }while(0)
  1266. // Pulse start
  1267. #if HAS_X_STEP
  1268. PULSE_START(X);
  1269. #endif
  1270. #if HAS_Y_STEP
  1271. PULSE_START(Y);
  1272. #endif
  1273. #if HAS_Z_STEP
  1274. PULSE_START(Z);
  1275. #endif
  1276. // Pulse Extruders
  1277. // Tick the E axis, correct error term and update position
  1278. #if ENABLED(LIN_ADVANCE) || ENABLED(MIXING_EXTRUDER)
  1279. delta_error[E_AXIS] += advance_dividend[E_AXIS];
  1280. if (delta_error[E_AXIS] >= 0) {
  1281. count_position[E_AXIS] += count_direction[E_AXIS];
  1282. #if ENABLED(LIN_ADVANCE)
  1283. delta_error[E_AXIS] -= advance_divisor;
  1284. // Don't step E here - But remember the number of steps to perform
  1285. motor_direction(E_AXIS) ? --LA_steps : ++LA_steps;
  1286. #else // !LIN_ADVANCE && MIXING_EXTRUDER
  1287. // Don't adjust delta_error[E_AXIS] here!
  1288. // Being positive is the criteria for ending the pulse.
  1289. E_STEP_WRITE(mixer.get_next_stepper(), !INVERT_E_STEP_PIN);
  1290. #endif
  1291. }
  1292. #else // !LIN_ADVANCE && !MIXING_EXTRUDER
  1293. #if HAS_E0_STEP
  1294. PULSE_START(E);
  1295. #endif
  1296. #endif
  1297. #if MINIMUM_STEPPER_PULSE
  1298. // Just wait for the requested pulse duration
  1299. while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
  1300. #endif
  1301. // Add the delay needed to ensure the maximum driver rate is enforced
  1302. if (signed(added_step_ticks) > 0) pulse_end += hal_timer_t(added_step_ticks);
  1303. // Pulse stop
  1304. #if HAS_X_STEP
  1305. PULSE_STOP(X);
  1306. #endif
  1307. #if HAS_Y_STEP
  1308. PULSE_STOP(Y);
  1309. #endif
  1310. #if HAS_Z_STEP
  1311. PULSE_STOP(Z);
  1312. #endif
  1313. #if DISABLED(LIN_ADVANCE)
  1314. #if ENABLED(MIXING_EXTRUDER)
  1315. if (delta_error[E_AXIS] >= 0) {
  1316. delta_error[E_AXIS] -= advance_divisor;
  1317. E_STEP_WRITE(mixer.get_stepper(), INVERT_E_STEP_PIN);
  1318. }
  1319. #else // !MIXING_EXTRUDER
  1320. #if HAS_E0_STEP
  1321. PULSE_STOP(E);
  1322. #endif
  1323. #endif
  1324. #endif // !LIN_ADVANCE
  1325. // Decrement the count of pending pulses to do
  1326. --events_to_do;
  1327. // For minimum pulse time wait after stopping pulses also
  1328. if (events_to_do) {
  1329. // Just wait for the requested pulse duration
  1330. while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
  1331. #if MINIMUM_STEPPER_PULSE
  1332. // Add to the value, the time that the pulse must be active (to be used on the next loop)
  1333. pulse_end += hal_timer_t(MIN_PULSE_TICKS);
  1334. #endif
  1335. }
  1336. } while (events_to_do);
  1337. }
  1338. // This is the last half of the stepper interrupt: This one processes and
  1339. // properly schedules blocks from the planner. This is executed after creating
  1340. // the step pulses, so it is not time critical, as pulses are already done.
  1341. uint32_t Stepper::stepper_block_phase_isr() {
  1342. // If no queued movements, just wait 1ms for the next move
  1343. uint32_t interval = (STEPPER_TIMER_RATE / 1000);
  1344. // If there is a current block
  1345. if (current_block) {
  1346. // If current block is finished, reset pointer
  1347. if (step_events_completed >= step_event_count) {
  1348. #if FILAMENT_RUNOUT_DISTANCE_MM > 0
  1349. runout.block_complete(current_block);
  1350. #endif
  1351. axis_did_move = 0;
  1352. current_block = NULL;
  1353. planner.discard_current_block();
  1354. }
  1355. else {
  1356. // Step events not completed yet...
  1357. // Are we in acceleration phase ?
  1358. if (step_events_completed <= accelerate_until) { // Calculate new timer value
  1359. #if ENABLED(S_CURVE_ACCELERATION)
  1360. // Get the next speed to use (Jerk limited!)
  1361. uint32_t acc_step_rate =
  1362. acceleration_time < current_block->acceleration_time
  1363. ? _eval_bezier_curve(acceleration_time)
  1364. : current_block->cruise_rate;
  1365. #else
  1366. acc_step_rate = STEP_MULTIPLY(acceleration_time, current_block->acceleration_rate) + current_block->initial_rate;
  1367. NOMORE(acc_step_rate, current_block->nominal_rate);
  1368. #endif
  1369. // acc_step_rate is in steps/second
  1370. // step_rate to timer interval and steps per stepper isr
  1371. interval = calc_timer_interval(acc_step_rate, oversampling_factor, &steps_per_isr);
  1372. acceleration_time += interval;
  1373. #if ENABLED(LIN_ADVANCE)
  1374. if (LA_use_advance_lead) {
  1375. // Fire ISR if final adv_rate is reached
  1376. if (LA_steps && LA_isr_rate != current_block->advance_speed) nextAdvanceISR = 0;
  1377. }
  1378. else if (LA_steps) nextAdvanceISR = 0;
  1379. #endif // LIN_ADVANCE
  1380. }
  1381. // Are we in Deceleration phase ?
  1382. else if (step_events_completed > decelerate_after) {
  1383. uint32_t step_rate;
  1384. #if ENABLED(S_CURVE_ACCELERATION)
  1385. // If this is the 1st time we process the 2nd half of the trapezoid...
  1386. if (!bezier_2nd_half) {
  1387. // Initialize the Bézier speed curve
  1388. _calc_bezier_curve_coeffs(current_block->cruise_rate, current_block->final_rate, current_block->deceleration_time_inverse);
  1389. bezier_2nd_half = true;
  1390. // The first point starts at cruise rate. Just save evaluation of the Bézier curve
  1391. step_rate = current_block->cruise_rate;
  1392. }
  1393. else {
  1394. // Calculate the next speed to use
  1395. step_rate = deceleration_time < current_block->deceleration_time
  1396. ? _eval_bezier_curve(deceleration_time)
  1397. : current_block->final_rate;
  1398. }
  1399. #else
  1400. // Using the old trapezoidal control
  1401. step_rate = STEP_MULTIPLY(deceleration_time, current_block->acceleration_rate);
  1402. if (step_rate < acc_step_rate) { // Still decelerating?
  1403. step_rate = acc_step_rate - step_rate;
  1404. NOLESS(step_rate, current_block->final_rate);
  1405. }
  1406. else
  1407. step_rate = current_block->final_rate;
  1408. #endif
  1409. // step_rate is in steps/second
  1410. // step_rate to timer interval and steps per stepper isr
  1411. interval = calc_timer_interval(step_rate, oversampling_factor, &steps_per_isr);
  1412. deceleration_time += interval;
  1413. #if ENABLED(LIN_ADVANCE)
  1414. if (LA_use_advance_lead) {
  1415. // Wake up eISR on first deceleration loop and fire ISR if final adv_rate is reached
  1416. if (step_events_completed <= decelerate_after + steps_per_isr || (LA_steps && LA_isr_rate != current_block->advance_speed)) {
  1417. nextAdvanceISR = 0;
  1418. LA_isr_rate = current_block->advance_speed;
  1419. }
  1420. }
  1421. else if (LA_steps) nextAdvanceISR = 0;
  1422. #endif // LIN_ADVANCE
  1423. }
  1424. // We must be in cruise phase otherwise
  1425. else {
  1426. #if ENABLED(LIN_ADVANCE)
  1427. // If there are any esteps, fire the next advance_isr "now"
  1428. if (LA_steps && LA_isr_rate != current_block->advance_speed) nextAdvanceISR = 0;
  1429. #endif
  1430. // Calculate the ticks_nominal for this nominal speed, if not done yet
  1431. if (ticks_nominal < 0) {
  1432. // step_rate to timer interval and loops for the nominal speed
  1433. ticks_nominal = calc_timer_interval(current_block->nominal_rate, oversampling_factor, &steps_per_isr);
  1434. }
  1435. // The timer interval is just the nominal value for the nominal speed
  1436. interval = ticks_nominal;
  1437. }
  1438. }
  1439. }
  1440. // If there is no current block at this point, attempt to pop one from the buffer
  1441. // and prepare its movement
  1442. if (!current_block) {
  1443. // Anything in the buffer?
  1444. if ((current_block = planner.get_current_block())) {
  1445. // Sync block? Sync the stepper counts and return
  1446. while (TEST(current_block->flag, BLOCK_BIT_SYNC_POSITION)) {
  1447. _set_position(
  1448. current_block->position[A_AXIS], current_block->position[B_AXIS],
  1449. current_block->position[C_AXIS], current_block->position[E_AXIS]
  1450. );
  1451. planner.discard_current_block();
  1452. // Try to get a new block
  1453. if (!(current_block = planner.get_current_block()))
  1454. return interval; // No more queued movements!
  1455. }
  1456. // Flag all moving axes for proper endstop handling
  1457. #if IS_CORE
  1458. // Define conditions for checking endstops
  1459. #define S_(N) current_block->steps[CORE_AXIS_##N]
  1460. #define D_(N) TEST(current_block->direction_bits, CORE_AXIS_##N)
  1461. #endif
  1462. #if CORE_IS_XY || CORE_IS_XZ
  1463. /**
  1464. * Head direction in -X axis for CoreXY and CoreXZ bots.
  1465. *
  1466. * If steps differ, both axes are moving.
  1467. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z, handled below)
  1468. * If DeltaA == DeltaB, the movement is only in the 1st axis (X)
  1469. */
  1470. #if ENABLED(COREXY) || ENABLED(COREXZ)
  1471. #define X_CMP ==
  1472. #else
  1473. #define X_CMP !=
  1474. #endif
  1475. #define X_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) X_CMP D_(2)) )
  1476. #else
  1477. #define X_MOVE_TEST !!current_block->steps[A_AXIS]
  1478. #endif
  1479. #if CORE_IS_XY || CORE_IS_YZ
  1480. /**
  1481. * Head direction in -Y axis for CoreXY / CoreYZ bots.
  1482. *
  1483. * If steps differ, both axes are moving
  1484. * If DeltaA == DeltaB, the movement is only in the 1st axis (X or Y)
  1485. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z)
  1486. */
  1487. #if ENABLED(COREYX) || ENABLED(COREYZ)
  1488. #define Y_CMP ==
  1489. #else
  1490. #define Y_CMP !=
  1491. #endif
  1492. #define Y_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) Y_CMP D_(2)) )
  1493. #else
  1494. #define Y_MOVE_TEST !!current_block->steps[B_AXIS]
  1495. #endif
  1496. #if CORE_IS_XZ || CORE_IS_YZ
  1497. /**
  1498. * Head direction in -Z axis for CoreXZ or CoreYZ bots.
  1499. *
  1500. * If steps differ, both axes are moving
  1501. * If DeltaA == DeltaB, the movement is only in the 1st axis (X or Y, already handled above)
  1502. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Z)
  1503. */
  1504. #if ENABLED(COREZX) || ENABLED(COREZY)
  1505. #define Z_CMP ==
  1506. #else
  1507. #define Z_CMP !=
  1508. #endif
  1509. #define Z_MOVE_TEST ( S_(1) != S_(2) || (S_(1) > 0 && D_(1) Z_CMP D_(2)) )
  1510. #else
  1511. #define Z_MOVE_TEST !!current_block->steps[C_AXIS]
  1512. #endif
  1513. uint8_t axis_bits = 0;
  1514. if (X_MOVE_TEST) SBI(axis_bits, A_AXIS);
  1515. if (Y_MOVE_TEST) SBI(axis_bits, B_AXIS);
  1516. if (Z_MOVE_TEST) SBI(axis_bits, C_AXIS);
  1517. //if (!!current_block->steps[E_AXIS]) SBI(axis_bits, E_AXIS);
  1518. //if (!!current_block->steps[A_AXIS]) SBI(axis_bits, X_HEAD);
  1519. //if (!!current_block->steps[B_AXIS]) SBI(axis_bits, Y_HEAD);
  1520. //if (!!current_block->steps[C_AXIS]) SBI(axis_bits, Z_HEAD);
  1521. axis_did_move = axis_bits;
  1522. // No acceleration / deceleration time elapsed so far
  1523. acceleration_time = deceleration_time = 0;
  1524. uint8_t oversampling = 0; // Assume we won't use it
  1525. #if ENABLED(ADAPTIVE_STEP_SMOOTHING)
  1526. // At this point, we must decide if we can use Stepper movement axis smoothing.
  1527. uint32_t max_rate = current_block->nominal_rate; // Get the maximum rate (maximum event speed)
  1528. while (max_rate < MIN_STEP_ISR_FREQUENCY) {
  1529. max_rate <<= 1;
  1530. if (max_rate >= MAX_STEP_ISR_FREQUENCY_1X) break;
  1531. ++oversampling;
  1532. }
  1533. oversampling_factor = oversampling;
  1534. #endif
  1535. // Based on the oversampling factor, do the calculations
  1536. step_event_count = current_block->step_event_count << oversampling;
  1537. // Initialize Bresenham delta errors to 1/2
  1538. delta_error[X_AXIS] = delta_error[Y_AXIS] = delta_error[Z_AXIS] = delta_error[E_AXIS] = -int32_t(step_event_count);
  1539. // Calculate Bresenham dividends
  1540. advance_dividend[X_AXIS] = current_block->steps[X_AXIS] << 1;
  1541. advance_dividend[Y_AXIS] = current_block->steps[Y_AXIS] << 1;
  1542. advance_dividend[Z_AXIS] = current_block->steps[Z_AXIS] << 1;
  1543. advance_dividend[E_AXIS] = current_block->steps[E_AXIS] << 1;
  1544. // Calculate Bresenham divisor
  1545. advance_divisor = step_event_count << 1;
  1546. // No step events completed so far
  1547. step_events_completed = 0;
  1548. // Compute the acceleration and deceleration points
  1549. accelerate_until = current_block->accelerate_until << oversampling;
  1550. decelerate_after = current_block->decelerate_after << oversampling;
  1551. #if ENABLED(MIXING_EXTRUDER)
  1552. MIXER_STEPPER_SETUP();
  1553. #endif
  1554. #if EXTRUDERS > 1
  1555. stepper_extruder = current_block->extruder;
  1556. #endif
  1557. // Initialize the trapezoid generator from the current block.
  1558. #if ENABLED(LIN_ADVANCE)
  1559. #if DISABLED(MIXING_EXTRUDER) && E_STEPPERS > 1
  1560. // If the now active extruder wasn't in use during the last move, its pressure is most likely gone.
  1561. if (stepper_extruder != last_moved_extruder) LA_current_adv_steps = 0;
  1562. #endif
  1563. if ((LA_use_advance_lead = current_block->use_advance_lead)) {
  1564. LA_final_adv_steps = current_block->final_adv_steps;
  1565. LA_max_adv_steps = current_block->max_adv_steps;
  1566. //Start the ISR
  1567. nextAdvanceISR = 0;
  1568. LA_isr_rate = current_block->advance_speed;
  1569. }
  1570. else LA_isr_rate = LA_ADV_NEVER;
  1571. #endif
  1572. if (current_block->direction_bits != last_direction_bits
  1573. #if DISABLED(MIXING_EXTRUDER)
  1574. || stepper_extruder != last_moved_extruder
  1575. #endif
  1576. ) {
  1577. last_direction_bits = current_block->direction_bits;
  1578. #if EXTRUDERS > 1
  1579. last_moved_extruder = stepper_extruder;
  1580. #endif
  1581. set_directions();
  1582. }
  1583. // At this point, we must ensure the movement about to execute isn't
  1584. // trying to force the head against a limit switch. If using interrupt-
  1585. // driven change detection, and already against a limit then no call to
  1586. // the endstop_triggered method will be done and the movement will be
  1587. // done against the endstop. So, check the limits here: If the movement
  1588. // is against the limits, the block will be marked as to be killed, and
  1589. // on the next call to this ISR, will be discarded.
  1590. endstops.update();
  1591. #if ENABLED(Z_LATE_ENABLE)
  1592. // If delayed Z enable, enable it now. This option will severely interfere with
  1593. // timing between pulses when chaining motion between blocks, and it could lead
  1594. // to lost steps in both X and Y axis, so avoid using it unless strictly necessary!!
  1595. if (current_block->steps[Z_AXIS]) enable_Z();
  1596. #endif
  1597. // Mark the time_nominal as not calculated yet
  1598. ticks_nominal = -1;
  1599. #if DISABLED(S_CURVE_ACCELERATION)
  1600. // Set as deceleration point the initial rate of the block
  1601. acc_step_rate = current_block->initial_rate;
  1602. #endif
  1603. #if ENABLED(S_CURVE_ACCELERATION)
  1604. // Initialize the Bézier speed curve
  1605. _calc_bezier_curve_coeffs(current_block->initial_rate, current_block->cruise_rate, current_block->acceleration_time_inverse);
  1606. // We haven't started the 2nd half of the trapezoid
  1607. bezier_2nd_half = false;
  1608. #endif
  1609. // Calculate the initial timer interval
  1610. interval = calc_timer_interval(current_block->initial_rate, oversampling_factor, &steps_per_isr);
  1611. }
  1612. }
  1613. // Return the interval to wait
  1614. return interval;
  1615. }
  1616. #if ENABLED(LIN_ADVANCE)
  1617. // Timer interrupt for E. LA_steps is set in the main routine
  1618. uint32_t Stepper::advance_isr() {
  1619. uint32_t interval;
  1620. if (LA_use_advance_lead) {
  1621. if (step_events_completed > decelerate_after && LA_current_adv_steps > LA_final_adv_steps) {
  1622. LA_steps--;
  1623. LA_current_adv_steps--;
  1624. interval = LA_isr_rate;
  1625. }
  1626. else if (step_events_completed < decelerate_after && LA_current_adv_steps < LA_max_adv_steps) {
  1627. //step_events_completed <= (uint32_t)accelerate_until) {
  1628. LA_steps++;
  1629. LA_current_adv_steps++;
  1630. interval = LA_isr_rate;
  1631. }
  1632. else
  1633. interval = LA_isr_rate = LA_ADV_NEVER;
  1634. }
  1635. else
  1636. interval = LA_ADV_NEVER;
  1637. #if ENABLED(MIXING_EXTRUDER)
  1638. // We don't know which steppers will be stepped because LA loop follows,
  1639. // with potentially multiple steps. Set all.
  1640. if (LA_steps >= 0)
  1641. MIXER_STEPPER_LOOP(j) NORM_E_DIR(j);
  1642. else
  1643. MIXER_STEPPER_LOOP(j) REV_E_DIR(j);
  1644. #else
  1645. if (LA_steps >= 0)
  1646. NORM_E_DIR(stepper_extruder);
  1647. else
  1648. REV_E_DIR(stepper_extruder);
  1649. #endif
  1650. // Get the timer count and estimate the end of the pulse
  1651. hal_timer_t pulse_end = HAL_timer_get_count(PULSE_TIMER_NUM) + hal_timer_t(MIN_PULSE_TICKS);
  1652. const hal_timer_t added_step_ticks = hal_timer_t(ADDED_STEP_TICKS);
  1653. // Step E stepper if we have steps
  1654. while (LA_steps) {
  1655. // Set the STEP pulse ON
  1656. #if ENABLED(MIXING_EXTRUDER)
  1657. E_STEP_WRITE(mixer.get_next_stepper(), !INVERT_E_STEP_PIN);
  1658. #else
  1659. E_STEP_WRITE(stepper_extruder, !INVERT_E_STEP_PIN);
  1660. #endif
  1661. // Enforce a minimum duration for STEP pulse ON
  1662. #if MINIMUM_STEPPER_PULSE
  1663. // Just wait for the requested pulse duration
  1664. while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
  1665. #endif
  1666. // Add the delay needed to ensure the maximum driver rate is enforced
  1667. if (signed(added_step_ticks) > 0) pulse_end += hal_timer_t(added_step_ticks);
  1668. LA_steps < 0 ? ++LA_steps : --LA_steps;
  1669. // Set the STEP pulse OFF
  1670. #if ENABLED(MIXING_EXTRUDER)
  1671. E_STEP_WRITE(mixer.get_stepper(), INVERT_E_STEP_PIN);
  1672. #else
  1673. E_STEP_WRITE(stepper_extruder, INVERT_E_STEP_PIN);
  1674. #endif
  1675. // For minimum pulse time wait before looping
  1676. // Just wait for the requested pulse duration
  1677. if (LA_steps) {
  1678. while (HAL_timer_get_count(PULSE_TIMER_NUM) < pulse_end) { /* nada */ }
  1679. #if MINIMUM_STEPPER_PULSE
  1680. // Add to the value, the time that the pulse must be active (to be used on the next loop)
  1681. pulse_end += hal_timer_t(MIN_PULSE_TICKS);
  1682. #endif
  1683. }
  1684. } // LA_steps
  1685. return interval;
  1686. }
  1687. #endif // LIN_ADVANCE
  1688. // Check if the given block is busy or not - Must not be called from ISR contexts
  1689. // The current_block could change in the middle of the read by an Stepper ISR, so
  1690. // we must explicitly prevent that!
  1691. bool Stepper::is_block_busy(const block_t* const block) {
  1692. #ifdef __AVR__
  1693. // A SW memory barrier, to ensure GCC does not overoptimize loops
  1694. #define sw_barrier() asm volatile("": : :"memory");
  1695. // Keep reading until 2 consecutive reads return the same value,
  1696. // meaning there was no update in-between caused by an interrupt.
  1697. // This works because stepper ISRs happen at a slower rate than
  1698. // successive reads of a variable, so 2 consecutive reads with
  1699. // the same value means no interrupt updated it.
  1700. block_t* vold, *vnew = current_block;
  1701. sw_barrier();
  1702. do {
  1703. vold = vnew;
  1704. vnew = current_block;
  1705. sw_barrier();
  1706. } while (vold != vnew);
  1707. #else
  1708. block_t *vnew = current_block;
  1709. #endif
  1710. // Return if the block is busy or not
  1711. return block == vnew;
  1712. }
  1713. void Stepper::init() {
  1714. // Init Digipot Motor Current
  1715. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  1716. digipot_init();
  1717. #endif
  1718. #if MB(ALLIGATOR)
  1719. const float motor_current[] = MOTOR_CURRENT;
  1720. unsigned int digipot_motor = 0;
  1721. for (uint8_t i = 0; i < 3 + EXTRUDERS; i++) {
  1722. digipot_motor = 255 * (motor_current[i] / 2.5);
  1723. dac084s085::setValue(i, digipot_motor);
  1724. }
  1725. #endif//MB(ALLIGATOR)
  1726. // Init Microstepping Pins
  1727. #if HAS_MICROSTEPS
  1728. microstep_init();
  1729. #endif
  1730. // Init Dir Pins
  1731. #if HAS_X_DIR
  1732. X_DIR_INIT;
  1733. #endif
  1734. #if HAS_X2_DIR
  1735. X2_DIR_INIT;
  1736. #endif
  1737. #if HAS_Y_DIR
  1738. Y_DIR_INIT;
  1739. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_DIR
  1740. Y2_DIR_INIT;
  1741. #endif
  1742. #endif
  1743. #if HAS_Z_DIR
  1744. Z_DIR_INIT;
  1745. #if Z_MULTI_STEPPER_DRIVERS && HAS_Z2_DIR
  1746. Z2_DIR_INIT;
  1747. #endif
  1748. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS) && HAS_Z3_DIR
  1749. Z3_DIR_INIT;
  1750. #endif
  1751. #endif
  1752. #if HAS_E0_DIR
  1753. E0_DIR_INIT;
  1754. #endif
  1755. #if HAS_E1_DIR
  1756. E1_DIR_INIT;
  1757. #endif
  1758. #if HAS_E2_DIR
  1759. E2_DIR_INIT;
  1760. #endif
  1761. #if HAS_E3_DIR
  1762. E3_DIR_INIT;
  1763. #endif
  1764. #if HAS_E4_DIR
  1765. E4_DIR_INIT;
  1766. #endif
  1767. #if HAS_E5_DIR
  1768. E5_DIR_INIT;
  1769. #endif
  1770. // Init Enable Pins - steppers default to disabled.
  1771. #if HAS_X_ENABLE
  1772. X_ENABLE_INIT;
  1773. if (!X_ENABLE_ON) X_ENABLE_WRITE(HIGH);
  1774. #if (ENABLED(DUAL_X_CARRIAGE) || ENABLED(X_DUAL_STEPPER_DRIVERS)) && HAS_X2_ENABLE
  1775. X2_ENABLE_INIT;
  1776. if (!X_ENABLE_ON) X2_ENABLE_WRITE(HIGH);
  1777. #endif
  1778. #endif
  1779. #if HAS_Y_ENABLE
  1780. Y_ENABLE_INIT;
  1781. if (!Y_ENABLE_ON) Y_ENABLE_WRITE(HIGH);
  1782. #if ENABLED(Y_DUAL_STEPPER_DRIVERS) && HAS_Y2_ENABLE
  1783. Y2_ENABLE_INIT;
  1784. if (!Y_ENABLE_ON) Y2_ENABLE_WRITE(HIGH);
  1785. #endif
  1786. #endif
  1787. #if HAS_Z_ENABLE
  1788. Z_ENABLE_INIT;
  1789. if (!Z_ENABLE_ON) Z_ENABLE_WRITE(HIGH);
  1790. #if Z_MULTI_STEPPER_DRIVERS && HAS_Z2_ENABLE
  1791. Z2_ENABLE_INIT;
  1792. if (!Z_ENABLE_ON) Z2_ENABLE_WRITE(HIGH);
  1793. #endif
  1794. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS) && HAS_Z3_ENABLE
  1795. Z3_ENABLE_INIT;
  1796. if (!Z_ENABLE_ON) Z3_ENABLE_WRITE(HIGH);
  1797. #endif
  1798. #endif
  1799. #if HAS_E0_ENABLE
  1800. E0_ENABLE_INIT;
  1801. if (!E_ENABLE_ON) E0_ENABLE_WRITE(HIGH);
  1802. #endif
  1803. #if HAS_E1_ENABLE
  1804. E1_ENABLE_INIT;
  1805. if (!E_ENABLE_ON) E1_ENABLE_WRITE(HIGH);
  1806. #endif
  1807. #if HAS_E2_ENABLE
  1808. E2_ENABLE_INIT;
  1809. if (!E_ENABLE_ON) E2_ENABLE_WRITE(HIGH);
  1810. #endif
  1811. #if HAS_E3_ENABLE
  1812. E3_ENABLE_INIT;
  1813. if (!E_ENABLE_ON) E3_ENABLE_WRITE(HIGH);
  1814. #endif
  1815. #if HAS_E4_ENABLE
  1816. E4_ENABLE_INIT;
  1817. if (!E_ENABLE_ON) E4_ENABLE_WRITE(HIGH);
  1818. #endif
  1819. #if HAS_E5_ENABLE
  1820. E5_ENABLE_INIT;
  1821. if (!E_ENABLE_ON) E5_ENABLE_WRITE(HIGH);
  1822. #endif
  1823. #define _STEP_INIT(AXIS) AXIS ##_STEP_INIT
  1824. #define _WRITE_STEP(AXIS, HIGHLOW) AXIS ##_STEP_WRITE(HIGHLOW)
  1825. #define _DISABLE(AXIS) disable_## AXIS()
  1826. #define AXIS_INIT(AXIS, PIN) \
  1827. _STEP_INIT(AXIS); \
  1828. _WRITE_STEP(AXIS, _INVERT_STEP_PIN(PIN)); \
  1829. _DISABLE(AXIS)
  1830. #define E_AXIS_INIT(NUM) AXIS_INIT(E## NUM, E)
  1831. // Init Step Pins
  1832. #if HAS_X_STEP
  1833. #if ENABLED(X_DUAL_STEPPER_DRIVERS) || ENABLED(DUAL_X_CARRIAGE)
  1834. X2_STEP_INIT;
  1835. X2_STEP_WRITE(INVERT_X_STEP_PIN);
  1836. #endif
  1837. AXIS_INIT(X, X);
  1838. #endif
  1839. #if HAS_Y_STEP
  1840. #if ENABLED(Y_DUAL_STEPPER_DRIVERS)
  1841. Y2_STEP_INIT;
  1842. Y2_STEP_WRITE(INVERT_Y_STEP_PIN);
  1843. #endif
  1844. AXIS_INIT(Y, Y);
  1845. #endif
  1846. #if HAS_Z_STEP
  1847. #if Z_MULTI_STEPPER_DRIVERS
  1848. Z2_STEP_INIT;
  1849. Z2_STEP_WRITE(INVERT_Z_STEP_PIN);
  1850. #endif
  1851. #if ENABLED(Z_TRIPLE_STEPPER_DRIVERS)
  1852. Z3_STEP_INIT;
  1853. Z3_STEP_WRITE(INVERT_Z_STEP_PIN);
  1854. #endif
  1855. AXIS_INIT(Z, Z);
  1856. #endif
  1857. #if E_STEPPERS > 0 && HAS_E0_STEP
  1858. E_AXIS_INIT(0);
  1859. #endif
  1860. #if E_STEPPERS > 1 && HAS_E1_STEP
  1861. E_AXIS_INIT(1);
  1862. #endif
  1863. #if E_STEPPERS > 2 && HAS_E2_STEP
  1864. E_AXIS_INIT(2);
  1865. #endif
  1866. #if E_STEPPERS > 3 && HAS_E3_STEP
  1867. E_AXIS_INIT(3);
  1868. #endif
  1869. #if E_STEPPERS > 4 && HAS_E4_STEP
  1870. E_AXIS_INIT(4);
  1871. #endif
  1872. #if E_STEPPERS > 5 && HAS_E5_STEP
  1873. E_AXIS_INIT(5);
  1874. #endif
  1875. // Init Stepper ISR to 122 Hz for quick starting
  1876. HAL_timer_start(STEP_TIMER_NUM, 122);
  1877. ENABLE_STEPPER_DRIVER_INTERRUPT();
  1878. sei();
  1879. set_directions(); // Init directions to last_direction_bits = 0 Keeps Z from being reversed
  1880. }
  1881. /**
  1882. * Set the stepper positions directly in steps
  1883. *
  1884. * The input is based on the typical per-axis XYZ steps.
  1885. * For CORE machines XYZ needs to be translated to ABC.
  1886. *
  1887. * This allows get_axis_position_mm to correctly
  1888. * derive the current XYZ position later on.
  1889. */
  1890. void Stepper::_set_position(const int32_t &a, const int32_t &b, const int32_t &c, const int32_t &e) {
  1891. #if CORE_IS_XY
  1892. // corexy positioning
  1893. // these equations follow the form of the dA and dB equations on http://www.corexy.com/theory.html
  1894. count_position[A_AXIS] = a + b;
  1895. count_position[B_AXIS] = CORESIGN(a - b);
  1896. count_position[Z_AXIS] = c;
  1897. #elif CORE_IS_XZ
  1898. // corexz planning
  1899. count_position[A_AXIS] = a + c;
  1900. count_position[Y_AXIS] = b;
  1901. count_position[C_AXIS] = CORESIGN(a - c);
  1902. #elif CORE_IS_YZ
  1903. // coreyz planning
  1904. count_position[X_AXIS] = a;
  1905. count_position[B_AXIS] = b + c;
  1906. count_position[C_AXIS] = CORESIGN(b - c);
  1907. #else
  1908. // default non-h-bot planning
  1909. count_position[X_AXIS] = a;
  1910. count_position[Y_AXIS] = b;
  1911. count_position[Z_AXIS] = c;
  1912. #endif
  1913. count_position[E_AXIS] = e;
  1914. }
  1915. /**
  1916. * Get a stepper's position in steps.
  1917. */
  1918. int32_t Stepper::position(const AxisEnum axis) {
  1919. #ifdef __AVR__
  1920. // Protect the access to the position. Only required for AVR, as
  1921. // any 32bit CPU offers atomic access to 32bit variables
  1922. const bool was_enabled = STEPPER_ISR_ENABLED();
  1923. if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
  1924. #endif
  1925. const int32_t v = count_position[axis];
  1926. #ifdef __AVR__
  1927. // Reenable Stepper ISR
  1928. if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
  1929. #endif
  1930. return v;
  1931. }
  1932. // Signal endstops were triggered - This function can be called from
  1933. // an ISR context (Temperature, Stepper or limits ISR), so we must
  1934. // be very careful here. If the interrupt being preempted was the
  1935. // Stepper ISR (this CAN happen with the endstop limits ISR) then
  1936. // when the stepper ISR resumes, we must be very sure that the movement
  1937. // is properly cancelled
  1938. void Stepper::endstop_triggered(const AxisEnum axis) {
  1939. const bool was_enabled = STEPPER_ISR_ENABLED();
  1940. if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
  1941. #if IS_CORE
  1942. endstops_trigsteps[axis] = 0.5f * (
  1943. axis == CORE_AXIS_2 ? CORESIGN(count_position[CORE_AXIS_1] - count_position[CORE_AXIS_2])
  1944. : count_position[CORE_AXIS_1] + count_position[CORE_AXIS_2]
  1945. );
  1946. #else // !COREXY && !COREXZ && !COREYZ
  1947. endstops_trigsteps[axis] = count_position[axis];
  1948. #endif // !COREXY && !COREXZ && !COREYZ
  1949. // Discard the rest of the move if there is a current block
  1950. quick_stop();
  1951. if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
  1952. }
  1953. int32_t Stepper::triggered_position(const AxisEnum axis) {
  1954. #ifdef __AVR__
  1955. // Protect the access to the position. Only required for AVR, as
  1956. // any 32bit CPU offers atomic access to 32bit variables
  1957. const bool was_enabled = STEPPER_ISR_ENABLED();
  1958. if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
  1959. #endif
  1960. const int32_t v = endstops_trigsteps[axis];
  1961. #ifdef __AVR__
  1962. // Reenable Stepper ISR
  1963. if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
  1964. #endif
  1965. return v;
  1966. }
  1967. void Stepper::report_positions() {
  1968. // Protect the access to the position.
  1969. const bool was_enabled = STEPPER_ISR_ENABLED();
  1970. if (was_enabled) DISABLE_STEPPER_DRIVER_INTERRUPT();
  1971. const int32_t xpos = count_position[X_AXIS],
  1972. ypos = count_position[Y_AXIS],
  1973. zpos = count_position[Z_AXIS];
  1974. if (was_enabled) ENABLE_STEPPER_DRIVER_INTERRUPT();
  1975. #if CORE_IS_XY || CORE_IS_XZ || ENABLED(DELTA) || IS_SCARA
  1976. SERIAL_PROTOCOLPGM(MSG_COUNT_A);
  1977. #else
  1978. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1979. #endif
  1980. SERIAL_PROTOCOL(xpos);
  1981. #if CORE_IS_XY || CORE_IS_YZ || ENABLED(DELTA) || IS_SCARA
  1982. SERIAL_PROTOCOLPGM(" B:");
  1983. #else
  1984. SERIAL_PROTOCOLPGM(" Y:");
  1985. #endif
  1986. SERIAL_PROTOCOL(ypos);
  1987. #if CORE_IS_XZ || CORE_IS_YZ || ENABLED(DELTA)
  1988. SERIAL_PROTOCOLPGM(" C:");
  1989. #else
  1990. SERIAL_PROTOCOLPGM(" Z:");
  1991. #endif
  1992. SERIAL_PROTOCOL(zpos);
  1993. SERIAL_EOL();
  1994. }
  1995. #if ENABLED(BABYSTEPPING)
  1996. #if MINIMUM_STEPPER_PULSE
  1997. #define STEP_PULSE_CYCLES ((MINIMUM_STEPPER_PULSE) * CYCLES_PER_MICROSECOND)
  1998. #else
  1999. #define STEP_PULSE_CYCLES 0
  2000. #endif
  2001. #if ENABLED(DELTA)
  2002. #define CYCLES_EATEN_BABYSTEP (2 * 15)
  2003. #else
  2004. #define CYCLES_EATEN_BABYSTEP 0
  2005. #endif
  2006. #define EXTRA_CYCLES_BABYSTEP (STEP_PULSE_CYCLES - (CYCLES_EATEN_BABYSTEP))
  2007. #define _ENABLE(AXIS) enable_## AXIS()
  2008. #define _READ_DIR(AXIS) AXIS ##_DIR_READ
  2009. #define _INVERT_DIR(AXIS) INVERT_## AXIS ##_DIR
  2010. #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
  2011. #if EXTRA_CYCLES_BABYSTEP > 20
  2012. #define _SAVE_START const hal_timer_t pulse_start = HAL_timer_get_count(PULSE_TIMER_NUM)
  2013. #define _PULSE_WAIT while (EXTRA_CYCLES_BABYSTEP > (uint32_t)(HAL_timer_get_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ }
  2014. #else
  2015. #define _SAVE_START NOOP
  2016. #if EXTRA_CYCLES_BABYSTEP > 0
  2017. #define _PULSE_WAIT DELAY_NS(EXTRA_CYCLES_BABYSTEP * NANOSECONDS_PER_CYCLE)
  2018. #elif STEP_PULSE_CYCLES > 0
  2019. #define _PULSE_WAIT NOOP
  2020. #elif ENABLED(DELTA)
  2021. #define _PULSE_WAIT DELAY_US(2);
  2022. #else
  2023. #define _PULSE_WAIT DELAY_US(4);
  2024. #endif
  2025. #endif
  2026. #define BABYSTEP_AXIS(AXIS, INVERT, DIR) { \
  2027. const uint8_t old_dir = _READ_DIR(AXIS); \
  2028. _ENABLE(AXIS); \
  2029. _APPLY_DIR(AXIS, _INVERT_DIR(AXIS)^DIR^INVERT); \
  2030. DELAY_NS(MINIMUM_STEPPER_DIR_DELAY); \
  2031. _SAVE_START; \
  2032. _APPLY_STEP(AXIS)(!_INVERT_STEP_PIN(AXIS), true); \
  2033. _PULSE_WAIT; \
  2034. _APPLY_STEP(AXIS)(_INVERT_STEP_PIN(AXIS), true); \
  2035. _APPLY_DIR(AXIS, old_dir); \
  2036. }
  2037. // MUST ONLY BE CALLED BY AN ISR,
  2038. // No other ISR should ever interrupt this!
  2039. void Stepper::babystep(const AxisEnum axis, const bool direction) {
  2040. cli();
  2041. switch (axis) {
  2042. #if ENABLED(BABYSTEP_XY)
  2043. case X_AXIS:
  2044. #if CORE_IS_XY
  2045. BABYSTEP_AXIS(X, false, direction);
  2046. BABYSTEP_AXIS(Y, false, direction);
  2047. #elif CORE_IS_XZ
  2048. BABYSTEP_AXIS(X, false, direction);
  2049. BABYSTEP_AXIS(Z, false, direction);
  2050. #else
  2051. BABYSTEP_AXIS(X, false, direction);
  2052. #endif
  2053. break;
  2054. case Y_AXIS:
  2055. #if CORE_IS_XY
  2056. BABYSTEP_AXIS(X, false, direction);
  2057. BABYSTEP_AXIS(Y, false, direction^(CORESIGN(1)<0));
  2058. #elif CORE_IS_YZ
  2059. BABYSTEP_AXIS(Y, false, direction);
  2060. BABYSTEP_AXIS(Z, false, direction^(CORESIGN(1)<0));
  2061. #else
  2062. BABYSTEP_AXIS(Y, false, direction);
  2063. #endif
  2064. break;
  2065. #endif
  2066. case Z_AXIS: {
  2067. #if CORE_IS_XZ
  2068. BABYSTEP_AXIS(X, BABYSTEP_INVERT_Z, direction);
  2069. BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction^(CORESIGN(1)<0));
  2070. #elif CORE_IS_YZ
  2071. BABYSTEP_AXIS(Y, BABYSTEP_INVERT_Z, direction);
  2072. BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction^(CORESIGN(1)<0));
  2073. #elif DISABLED(DELTA)
  2074. BABYSTEP_AXIS(Z, BABYSTEP_INVERT_Z, direction);
  2075. #else // DELTA
  2076. const bool z_direction = direction ^ BABYSTEP_INVERT_Z;
  2077. enable_X();
  2078. enable_Y();
  2079. enable_Z();
  2080. const uint8_t old_x_dir_pin = X_DIR_READ,
  2081. old_y_dir_pin = Y_DIR_READ,
  2082. old_z_dir_pin = Z_DIR_READ;
  2083. X_DIR_WRITE(INVERT_X_DIR ^ z_direction);
  2084. Y_DIR_WRITE(INVERT_Y_DIR ^ z_direction);
  2085. Z_DIR_WRITE(INVERT_Z_DIR ^ z_direction);
  2086. #if MINIMUM_STEPPER_DIR_DELAY > 0
  2087. DELAY_NS(MINIMUM_STEPPER_DIR_DELAY);
  2088. #endif
  2089. _SAVE_START;
  2090. X_STEP_WRITE(!INVERT_X_STEP_PIN);
  2091. Y_STEP_WRITE(!INVERT_Y_STEP_PIN);
  2092. Z_STEP_WRITE(!INVERT_Z_STEP_PIN);
  2093. _PULSE_WAIT;
  2094. X_STEP_WRITE(INVERT_X_STEP_PIN);
  2095. Y_STEP_WRITE(INVERT_Y_STEP_PIN);
  2096. Z_STEP_WRITE(INVERT_Z_STEP_PIN);
  2097. // Restore direction bits
  2098. X_DIR_WRITE(old_x_dir_pin);
  2099. Y_DIR_WRITE(old_y_dir_pin);
  2100. Z_DIR_WRITE(old_z_dir_pin);
  2101. #endif
  2102. } break;
  2103. default: break;
  2104. }
  2105. sei();
  2106. }
  2107. #endif // BABYSTEPPING
  2108. /**
  2109. * Software-controlled Stepper Motor Current
  2110. */
  2111. #if HAS_DIGIPOTSS
  2112. // From Arduino DigitalPotControl example
  2113. void Stepper::digitalPotWrite(const int16_t address, const int16_t value) {
  2114. WRITE(DIGIPOTSS_PIN, LOW); // Take the SS pin low to select the chip
  2115. SPI.transfer(address); // Send the address and value via SPI
  2116. SPI.transfer(value);
  2117. WRITE(DIGIPOTSS_PIN, HIGH); // Take the SS pin high to de-select the chip
  2118. //delay(10);
  2119. }
  2120. #endif // HAS_DIGIPOTSS
  2121. #if HAS_MOTOR_CURRENT_PWM
  2122. void Stepper::refresh_motor_power() {
  2123. LOOP_L_N(i, COUNT(motor_current_setting)) {
  2124. switch (i) {
  2125. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  2126. case 0:
  2127. #endif
  2128. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  2129. case 1:
  2130. #endif
  2131. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  2132. case 2:
  2133. #endif
  2134. digipot_current(i, motor_current_setting[i]);
  2135. default: break;
  2136. }
  2137. }
  2138. }
  2139. #endif // HAS_MOTOR_CURRENT_PWM
  2140. #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
  2141. void Stepper::digipot_current(const uint8_t driver, const int current) {
  2142. #if HAS_DIGIPOTSS
  2143. const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
  2144. digitalPotWrite(digipot_ch[driver], current);
  2145. #elif HAS_MOTOR_CURRENT_PWM
  2146. if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1))
  2147. motor_current_setting[driver] = current; // update motor_current_setting
  2148. #define _WRITE_CURRENT_PWM(P) analogWrite(MOTOR_CURRENT_PWM_## P ##_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE))
  2149. switch (driver) {
  2150. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  2151. case 0: _WRITE_CURRENT_PWM(XY); break;
  2152. #endif
  2153. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  2154. case 1: _WRITE_CURRENT_PWM(Z); break;
  2155. #endif
  2156. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  2157. case 2: _WRITE_CURRENT_PWM(E); break;
  2158. #endif
  2159. }
  2160. #endif
  2161. }
  2162. void Stepper::digipot_init() {
  2163. #if HAS_DIGIPOTSS
  2164. static const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
  2165. SPI.begin();
  2166. SET_OUTPUT(DIGIPOTSS_PIN);
  2167. for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++) {
  2168. //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
  2169. digipot_current(i, digipot_motor_current[i]);
  2170. }
  2171. #elif HAS_MOTOR_CURRENT_PWM
  2172. #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
  2173. SET_OUTPUT(MOTOR_CURRENT_PWM_XY_PIN);
  2174. #endif
  2175. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  2176. SET_OUTPUT(MOTOR_CURRENT_PWM_Z_PIN);
  2177. #endif
  2178. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  2179. SET_OUTPUT(MOTOR_CURRENT_PWM_E_PIN);
  2180. #endif
  2181. refresh_motor_power();
  2182. // Set Timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
  2183. SET_CS5(PRESCALER_1);
  2184. #endif
  2185. }
  2186. #endif
  2187. #if HAS_MICROSTEPS
  2188. /**
  2189. * Software-controlled Microstepping
  2190. */
  2191. void Stepper::microstep_init() {
  2192. #if HAS_X_MICROSTEPS
  2193. SET_OUTPUT(X_MS1_PIN);
  2194. SET_OUTPUT(X_MS2_PIN);
  2195. #if PIN_EXISTS(X_MS3)
  2196. SET_OUTPUT(X_MS3_PIN);
  2197. #endif
  2198. #endif
  2199. #if HAS_X2_MICROSTEPS
  2200. SET_OUTPUT(X2_MS1_PIN);
  2201. SET_OUTPUT(X2_MS2_PIN);
  2202. #if PIN_EXISTS(X2_MS3)
  2203. SET_OUTPUT(X2_MS3_PIN);
  2204. #endif
  2205. #endif
  2206. #if HAS_Y_MICROSTEPS
  2207. SET_OUTPUT(Y_MS1_PIN);
  2208. SET_OUTPUT(Y_MS2_PIN);
  2209. #if PIN_EXISTS(Y_MS3)
  2210. SET_OUTPUT(Y_MS3_PIN);
  2211. #endif
  2212. #endif
  2213. #if HAS_Y2_MICROSTEPS
  2214. SET_OUTPUT(Y2_MS1_PIN);
  2215. SET_OUTPUT(Y2_MS2_PIN);
  2216. #if PIN_EXISTS(Y2_MS3)
  2217. SET_OUTPUT(Y2_MS3_PIN);
  2218. #endif
  2219. #endif
  2220. #if HAS_Z_MICROSTEPS
  2221. SET_OUTPUT(Z_MS1_PIN);
  2222. SET_OUTPUT(Z_MS2_PIN);
  2223. #if PIN_EXISTS(Z_MS3)
  2224. SET_OUTPUT(Z_MS3_PIN);
  2225. #endif
  2226. #endif
  2227. #if HAS_Z2_MICROSTEPS
  2228. SET_OUTPUT(Z2_MS1_PIN);
  2229. SET_OUTPUT(Z2_MS2_PIN);
  2230. #if PIN_EXISTS(Z2_MS3)
  2231. SET_OUTPUT(Z2_MS3_PIN);
  2232. #endif
  2233. #endif
  2234. #if HAS_Z3_MICROSTEPS
  2235. SET_OUTPUT(Z3_MS1_PIN);
  2236. SET_OUTPUT(Z3_MS2_PIN);
  2237. #if PIN_EXISTS(Z3_MS3)
  2238. SET_OUTPUT(Z3_MS3_PIN);
  2239. #endif
  2240. #endif
  2241. #if HAS_E0_MICROSTEPS
  2242. SET_OUTPUT(E0_MS1_PIN);
  2243. SET_OUTPUT(E0_MS2_PIN);
  2244. #if PIN_EXISTS(E0_MS3)
  2245. SET_OUTPUT(E0_MS3_PIN);
  2246. #endif
  2247. #endif
  2248. #if HAS_E1_MICROSTEPS
  2249. SET_OUTPUT(E1_MS1_PIN);
  2250. SET_OUTPUT(E1_MS2_PIN);
  2251. #if PIN_EXISTS(E1_MS3)
  2252. SET_OUTPUT(E1_MS3_PIN);
  2253. #endif
  2254. #endif
  2255. #if HAS_E2_MICROSTEPS
  2256. SET_OUTPUT(E2_MS1_PIN);
  2257. SET_OUTPUT(E2_MS2_PIN);
  2258. #if PIN_EXISTS(E2_MS3)
  2259. SET_OUTPUT(E2_MS3_PIN);
  2260. #endif
  2261. #endif
  2262. #if HAS_E3_MICROSTEPS
  2263. SET_OUTPUT(E3_MS1_PIN);
  2264. SET_OUTPUT(E3_MS2_PIN);
  2265. #if PIN_EXISTS(E3_MS3)
  2266. SET_OUTPUT(E3_MS3_PIN);
  2267. #endif
  2268. #endif
  2269. #if HAS_E4_MICROSTEPS
  2270. SET_OUTPUT(E4_MS1_PIN);
  2271. SET_OUTPUT(E4_MS2_PIN);
  2272. #if PIN_EXISTS(E4_MS3)
  2273. SET_OUTPUT(E4_MS3_PIN);
  2274. #endif
  2275. #endif
  2276. #if HAS_E5_MICROSTEPS
  2277. SET_OUTPUT(E5_MS1_PIN);
  2278. SET_OUTPUT(E5_MS2_PIN);
  2279. #if PIN_EXISTS(E5_MS3)
  2280. SET_OUTPUT(E5_MS3_PIN);
  2281. #endif
  2282. #endif
  2283. static const uint8_t microstep_modes[] = MICROSTEP_MODES;
  2284. for (uint16_t i = 0; i < COUNT(microstep_modes); i++)
  2285. microstep_mode(i, microstep_modes[i]);
  2286. }
  2287. void Stepper::microstep_ms(const uint8_t driver, const int8_t ms1, const int8_t ms2, const int8_t ms3) {
  2288. if (ms1 >= 0) switch (driver) {
  2289. #if HAS_X_MICROSTEPS || HAS_X2_MICROSTEPS
  2290. case 0:
  2291. #if HAS_X_MICROSTEPS
  2292. WRITE(X_MS1_PIN, ms1);
  2293. #endif
  2294. #if HAS_X2_MICROSTEPS
  2295. WRITE(X2_MS1_PIN, ms1);
  2296. #endif
  2297. break;
  2298. #endif
  2299. #if HAS_Y_MICROSTEPS || HAS_Y2_MICROSTEPS
  2300. case 1:
  2301. #if HAS_Y_MICROSTEPS
  2302. WRITE(Y_MS1_PIN, ms1);
  2303. #endif
  2304. #if HAS_Y2_MICROSTEPS
  2305. WRITE(Y2_MS1_PIN, ms1);
  2306. #endif
  2307. break;
  2308. #endif
  2309. #if HAS_Z_MICROSTEPS || HAS_Z2_MICROSTEPS || HAS_Z3_MICROSTEPS
  2310. case 2:
  2311. #if HAS_Z_MICROSTEPS
  2312. WRITE(Z_MS1_PIN, ms1);
  2313. #endif
  2314. #if HAS_Z2_MICROSTEPS
  2315. WRITE(Z2_MS1_PIN, ms1);
  2316. #endif
  2317. #if HAS_Z3_MICROSTEPS
  2318. WRITE(Z3_MS1_PIN, ms1);
  2319. #endif
  2320. break;
  2321. #endif
  2322. #if HAS_E0_MICROSTEPS
  2323. case 3: WRITE(E0_MS1_PIN, ms1); break;
  2324. #endif
  2325. #if HAS_E1_MICROSTEPS
  2326. case 4: WRITE(E1_MS1_PIN, ms1); break;
  2327. #endif
  2328. #if HAS_E2_MICROSTEPS
  2329. case 5: WRITE(E2_MS1_PIN, ms1); break;
  2330. #endif
  2331. #if HAS_E3_MICROSTEPS
  2332. case 6: WRITE(E3_MS1_PIN, ms1); break;
  2333. #endif
  2334. #if HAS_E4_MICROSTEPS
  2335. case 7: WRITE(E4_MS1_PIN, ms1); break;
  2336. #endif
  2337. #if HAS_E5_MICROSTEPS
  2338. case 8: WRITE(E5_MS1_PIN, ms1); break;
  2339. #endif
  2340. }
  2341. if (ms2 >= 0) switch (driver) {
  2342. #if HAS_X_MICROSTEPS || HAS_X2_MICROSTEPS
  2343. case 0:
  2344. #if HAS_X_MICROSTEPS
  2345. WRITE(X_MS2_PIN, ms2);
  2346. #endif
  2347. #if HAS_X2_MICROSTEPS
  2348. WRITE(X2_MS2_PIN, ms2);
  2349. #endif
  2350. break;
  2351. #endif
  2352. #if HAS_Y_MICROSTEPS || HAS_Y2_MICROSTEPS
  2353. case 1:
  2354. #if HAS_Y_MICROSTEPS
  2355. WRITE(Y_MS2_PIN, ms2);
  2356. #endif
  2357. #if HAS_Y2_MICROSTEPS
  2358. WRITE(Y2_MS2_PIN, ms2);
  2359. #endif
  2360. break;
  2361. #endif
  2362. #if HAS_Z_MICROSTEPS || HAS_Z2_MICROSTEPS || HAS_Z3_MICROSTEPS
  2363. case 2:
  2364. #if HAS_Z_MICROSTEPS
  2365. WRITE(Z_MS2_PIN, ms2);
  2366. #endif
  2367. #if HAS_Z2_MICROSTEPS
  2368. WRITE(Z2_MS2_PIN, ms2);
  2369. #endif
  2370. #if HAS_Z3_MICROSTEPS
  2371. WRITE(Z3_MS2_PIN, ms2);
  2372. #endif
  2373. break;
  2374. #endif
  2375. #if HAS_E0_MICROSTEPS
  2376. case 3: WRITE(E0_MS2_PIN, ms2); break;
  2377. #endif
  2378. #if HAS_E1_MICROSTEPS
  2379. case 4: WRITE(E1_MS2_PIN, ms2); break;
  2380. #endif
  2381. #if HAS_E2_MICROSTEPS
  2382. case 5: WRITE(E2_MS2_PIN, ms2); break;
  2383. #endif
  2384. #if HAS_E3_MICROSTEPS
  2385. case 6: WRITE(E3_MS2_PIN, ms2); break;
  2386. #endif
  2387. #if HAS_E4_MICROSTEPS
  2388. case 7: WRITE(E4_MS2_PIN, ms2); break;
  2389. #endif
  2390. #if HAS_E5_MICROSTEPS
  2391. case 8: WRITE(E5_MS2_PIN, ms2); break;
  2392. #endif
  2393. }
  2394. if (ms3 >= 0) switch (driver) {
  2395. #if HAS_X_MICROSTEPS || HAS_X2_MICROSTEPS
  2396. case 0:
  2397. #if HAS_X_MICROSTEPS && PIN_EXISTS(X_MS3)
  2398. WRITE(X_MS3_PIN, ms3);
  2399. #endif
  2400. #if HAS_X2_MICROSTEPS && PIN_EXISTS(X2_MS3)
  2401. WRITE(X2_MS3_PIN, ms3);
  2402. #endif
  2403. break;
  2404. #endif
  2405. #if HAS_Y_MICROSTEPS || HAS_Y2_MICROSTEPS
  2406. case 1:
  2407. #if HAS_Y_MICROSTEPS && PIN_EXISTS(Y_MS3)
  2408. WRITE(Y_MS3_PIN, ms3);
  2409. #endif
  2410. #if HAS_Y2_MICROSTEPS && PIN_EXISTS(Y2_MS3)
  2411. WRITE(Y2_MS3_PIN, ms3);
  2412. #endif
  2413. break;
  2414. #endif
  2415. #if HAS_Z_MICROSTEPS || HAS_Z2_MICROSTEPS || HAS_Z3_MICROSTEPS
  2416. case 2:
  2417. #if HAS_Z_MICROSTEPS && PIN_EXISTS(Z_MS3)
  2418. WRITE(Z_MS3_PIN, ms3);
  2419. #endif
  2420. #if HAS_Z2_MICROSTEPS && PIN_EXISTS(Z2_MS3)
  2421. WRITE(Z2_MS3_PIN, ms3);
  2422. #endif
  2423. #if HAS_Z3_MICROSTEPS && PIN_EXISTS(Z3_MS3)
  2424. WRITE(Z3_MS3_PIN, ms3);
  2425. #endif
  2426. break;
  2427. #endif
  2428. #if HAS_E0_MICROSTEPS && PIN_EXISTS(E0_MS3)
  2429. case 3: WRITE(E0_MS3_PIN, ms3); break;
  2430. #endif
  2431. #if HAS_E1_MICROSTEPS && PIN_EXISTS(E1_MS3)
  2432. case 4: WRITE(E1_MS3_PIN, ms3); break;
  2433. #endif
  2434. #if HAS_E2_MICROSTEPS && PIN_EXISTS(E2_MS3)
  2435. case 5: WRITE(E2_MS3_PIN, ms3); break;
  2436. #endif
  2437. #if HAS_E3_MICROSTEPS && PIN_EXISTS(E3_MS3)
  2438. case 6: WRITE(E3_MS3_PIN, ms3); break;
  2439. #endif
  2440. #if HAS_E4_MICROSTEPS && PIN_EXISTS(E4_MS3)
  2441. case 7: WRITE(E4_MS3_PIN, ms3); break;
  2442. #endif
  2443. #if HAS_E5_MICROSTEPS && PIN_EXISTS(E5_MS3)
  2444. case 8: WRITE(E5_MS3_PIN, ms3); break;
  2445. #endif
  2446. }
  2447. }
  2448. void Stepper::microstep_mode(const uint8_t driver, const uint8_t stepping_mode) {
  2449. switch (stepping_mode) {
  2450. #if HAS_MICROSTEP1
  2451. case 1: microstep_ms(driver, MICROSTEP1); break;
  2452. #endif
  2453. #if HAS_MICROSTEP2
  2454. case 2: microstep_ms(driver, MICROSTEP2); break;
  2455. #endif
  2456. #if HAS_MICROSTEP4
  2457. case 4: microstep_ms(driver, MICROSTEP4); break;
  2458. #endif
  2459. #if HAS_MICROSTEP8
  2460. case 8: microstep_ms(driver, MICROSTEP8); break;
  2461. #endif
  2462. #if HAS_MICROSTEP16
  2463. case 16: microstep_ms(driver, MICROSTEP16); break;
  2464. #endif
  2465. #if HAS_MICROSTEP32
  2466. case 32: microstep_ms(driver, MICROSTEP32); break;
  2467. #endif
  2468. #if HAS_MICROSTEP64
  2469. case 64: microstep_ms(driver, MICROSTEP64); break;
  2470. #endif
  2471. #if HAS_MICROSTEP128
  2472. case 128: microstep_ms(driver, MICROSTEP128); break;
  2473. #endif
  2474. default: SERIAL_ERROR_START(); SERIAL_ERRORLNPGM("Microsteps unavailable"); break;
  2475. }
  2476. }
  2477. void Stepper::microstep_readings() {
  2478. SERIAL_PROTOCOLLNPGM("MS1,MS2,MS3 Pins");
  2479. SERIAL_PROTOCOLPGM("X: ");
  2480. #if HAS_X_MICROSTEPS
  2481. SERIAL_PROTOCOL(READ(X_MS1_PIN));
  2482. SERIAL_PROTOCOL(READ(X_MS2_PIN));
  2483. #if PIN_EXISTS(X_MS3)
  2484. SERIAL_PROTOCOLLN(READ(X_MS3_PIN));
  2485. #endif
  2486. #endif
  2487. #if HAS_Y_MICROSTEPS
  2488. SERIAL_PROTOCOLPGM("Y: ");
  2489. SERIAL_PROTOCOL(READ(Y_MS1_PIN));
  2490. SERIAL_PROTOCOL(READ(Y_MS2_PIN));
  2491. #if PIN_EXISTS(Y_MS3)
  2492. SERIAL_PROTOCOLLN(READ(Y_MS3_PIN));
  2493. #endif
  2494. #endif
  2495. #if HAS_Z_MICROSTEPS
  2496. SERIAL_PROTOCOLPGM("Z: ");
  2497. SERIAL_PROTOCOL(READ(Z_MS1_PIN));
  2498. SERIAL_PROTOCOL(READ(Z_MS2_PIN));
  2499. #if PIN_EXISTS(Z_MS3)
  2500. SERIAL_PROTOCOLLN(READ(Z_MS3_PIN));
  2501. #endif
  2502. #endif
  2503. #if HAS_E0_MICROSTEPS
  2504. SERIAL_PROTOCOLPGM("E0: ");
  2505. SERIAL_PROTOCOL(READ(E0_MS1_PIN));
  2506. SERIAL_PROTOCOL(READ(E0_MS2_PIN));
  2507. #if PIN_EXISTS(E0_MS3)
  2508. SERIAL_PROTOCOLLN(READ(E0_MS3_PIN));
  2509. #endif
  2510. #endif
  2511. #if HAS_E1_MICROSTEPS
  2512. SERIAL_PROTOCOLPGM("E1: ");
  2513. SERIAL_PROTOCOL(READ(E1_MS1_PIN));
  2514. SERIAL_PROTOCOL(READ(E1_MS2_PIN));
  2515. #if PIN_EXISTS(E1_MS3)
  2516. SERIAL_PROTOCOLLN(READ(E1_MS3_PIN));
  2517. #endif
  2518. #endif
  2519. #if HAS_E2_MICROSTEPS
  2520. SERIAL_PROTOCOLPGM("E2: ");
  2521. SERIAL_PROTOCOL(READ(E2_MS1_PIN));
  2522. SERIAL_PROTOCOL(READ(E2_MS2_PIN));
  2523. #if PIN_EXISTS(E2_MS3)
  2524. SERIAL_PROTOCOLLN(READ(E2_MS3_PIN));
  2525. #endif
  2526. #endif
  2527. #if HAS_E3_MICROSTEPS
  2528. SERIAL_PROTOCOLPGM("E3: ");
  2529. SERIAL_PROTOCOL(READ(E3_MS1_PIN));
  2530. SERIAL_PROTOCOL(READ(E3_MS2_PIN));
  2531. #if PIN_EXISTS(E3_MS3)
  2532. SERIAL_PROTOCOLLN(READ(E3_MS3_PIN));
  2533. #endif
  2534. #endif
  2535. #if HAS_E4_MICROSTEPS
  2536. SERIAL_PROTOCOLPGM("E4: ");
  2537. SERIAL_PROTOCOL(READ(E4_MS1_PIN));
  2538. SERIAL_PROTOCOL(READ(E4_MS2_PIN));
  2539. #if PIN_EXISTS(E4_MS3)
  2540. SERIAL_PROTOCOLLN(READ(E4_MS3_PIN));
  2541. #endif
  2542. #endif
  2543. #if HAS_E5_MICROSTEPS
  2544. SERIAL_PROTOCOLPGM("E5: ");
  2545. SERIAL_PROTOCOL(READ(E5_MS1_PIN));
  2546. SERIAL_PROTOCOLLN(READ(E5_MS2_PIN));
  2547. #if PIN_EXISTS(E5_MS3)
  2548. SERIAL_PROTOCOLLN(READ(E5_MS3_PIN));
  2549. #endif
  2550. #endif
  2551. }
  2552. #endif // HAS_MICROSTEPS