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

stepper.cpp 108KB

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