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 122KB

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