My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

stepper.cpp 104KB

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